io.atomix.primitive.PrimitiveState Java Examples
The following examples show how to use
io.atomix.primitive.PrimitiveState.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DistributedLogClient.java From atomix with Apache License 2.0 | 6 votes |
private synchronized void onStateChange(PartitionId partitionId, PrimitiveState state) { states.put(partitionId, state); switch (state) { case CONNECTED: if (!states.containsValue(PrimitiveState.SUSPENDED) && !states.containsValue(PrimitiveState.CLOSED)) { changeState(PrimitiveState.CONNECTED); } break; case SUSPENDED: changeState(PrimitiveState.SUSPENDED); break; case CLOSED: changeState(PrimitiveState.CLOSED); break; } }
Example #2
Source File: AtomicLockProxy.java From atomix with Apache License 2.0 | 6 votes |
@Override public CompletableFuture<Optional<Version>> tryLock() { // If the proxy is currently disconnected from the cluster, we can just fail the lock attempt here. PrimitiveState state = getProxyClient().getPartition(name()).getState(); if (state != PrimitiveState.CONNECTED) { return CompletableFuture.completedFuture(Optional.empty()); } // Create and register a new attempt and invoke the LOCK operation on teh replicated state machine with // a 0 timeout. The timeout will cause the state machine to immediately reject the request if the lock is // already owned by another process. LockAttempt attempt = new LockAttempt(); getProxyClient().acceptBy(name(), service -> service.lock(attempt.id(), 0)).whenComplete((result, error) -> { if (error != null) { attempt.completeExceptionally(error); } }); return attempt.thenApply(Optional::ofNullable); }
Example #3
Source File: CachingAsyncDistributedCollection.java From atomix with Apache License 2.0 | 6 votes |
/** * Constructor to configure cache size. * * @param backingCollection a distributed collection for backing * @param cacheConfig the cache configuration */ public CachingAsyncDistributedCollection(AsyncDistributedCollection<E> backingCollection, CacheConfig cacheConfig) { super(backingCollection); cache = CacheBuilder.newBuilder() .maximumSize(cacheConfig.getSize()) .build(CacheLoader.from(CachingAsyncDistributedCollection.super::contains)); cacheUpdater = event -> { cache.invalidate(event.element()); eventListeners.forEach((listener, executor) -> executor.execute(() -> listener.event(event))); }; statusListener = status -> { log.debug("{} status changed to {}", this.name(), status); // If the status of the underlying map is SUSPENDED or INACTIVE // we can no longer guarantee that the cache will be in sync. if (status == PrimitiveState.SUSPENDED || status == PrimitiveState.CLOSED) { cache.invalidateAll(); } }; super.addListener(cacheUpdater, MoreExecutors.directExecutor()); super.addStateChangeListener(statusListener); }
Example #4
Source File: AbstractProxyClient.java From atomix with Apache License 2.0 | 6 votes |
public AbstractProxyClient( String name, PrimitiveType type, PrimitiveProtocol protocol, Collection<ProxySession<S>> partitions) { this.name = checkNotNull(name, "name cannot be null"); this.type = checkNotNull(type, "type cannot be null"); this.protocol = checkNotNull(protocol, "protocol cannot be null"); partitions.forEach(partition -> { this.partitionIds.add(partition.partitionId()); this.partitions.put(partition.partitionId(), partition); states.put(partition.partitionId(), PrimitiveState.CLOSED); partition.addStateChangeListener(state -> onStateChange(partition.partitionId(), state)); }); Collections.sort(partitionIds); }
Example #5
Source File: AbstractAtomicMapProxy.java From atomix with Apache License 2.0 | 6 votes |
@Override public CompletableFuture<P> connect() { return super.connect() .thenCompose(v -> Futures.allOf(getPartitions().stream() .map(partitionId -> { ProxySession<? extends AtomicMapService<K>> partition = getProxyClient().getPartition(partitionId); return partition.connect() .thenRun(() -> { partition.addStateChangeListener(state -> { if (state == PrimitiveState.CONNECTED && isListening()) { partition.accept(service -> service.listen()); } }); partition.addStateChangeListener(this::onStateChange); }); }))) .thenApply(v -> (P) this); }
Example #6
Source File: TestSessionClient.java From atomix with Apache License 2.0 | 6 votes |
@Override public synchronized CompletableFuture<Void> close() { CompletableFuture<Void> future = new CompletableFuture<>(); if (state == PrimitiveState.CLOSED) { future.complete(null); } else { service.close(sessionId).whenCompleteAsync((result, error) -> { if (error == null) { changeState(PrimitiveState.CLOSED); future.complete(null); } else { future.completeExceptionally(error); } }, context); } return future; }
Example #7
Source File: KeyLock.java From atomix with Apache License 2.0 | 6 votes |
/** * Attempts to lock the key. * * @return a future to be completed once the lock attempt is complete */ CompletableFuture<OptionalLong> tryLock() { // If the proxy is currently disconnected from the cluster, we can just fail the lock attempt here. PrimitiveState state = client.getPartition(partitionId).getState(); if (state != PrimitiveState.CONNECTED) { return CompletableFuture.completedFuture(OptionalLong.empty()); } // Create and register a new attempt and invoke the LOCK operation on teh replicated state machine with // a 0 timeout. The timeout will cause the state machine to immediately reject the request if the lock is // already owned by another process. LockFuture future = new LockFuture(); client.acceptOn(partitionId, service -> service.lock(key, future.id(), 0)).whenComplete((result, error) -> { if (error != null) { future.completeExceptionally(error); } }); return future.thenApply(v -> v != null ? OptionalLong.of(v) : OptionalLong.empty()); }
Example #8
Source File: RaftSessionState.java From atomix with Apache License 2.0 | 6 votes |
/** * Updates the session state. * * @param state The updates session state. */ public void setState(PrimitiveState state) { if (this.state != state) { if (this.state != PrimitiveState.EXPIRED && this.state != PrimitiveState.CLOSED) { this.state = state; if (state == PrimitiveState.SUSPENDED) { if (suspendedTime == null) { suspendedTime = System.currentTimeMillis(); } } else { suspendedTime = null; } changeListeners.forEach(l -> l.accept(state)); } } else if (this.state == PrimitiveState.SUSPENDED) { if (System.currentTimeMillis() - suspendedTime > timeout) { setState(PrimitiveState.EXPIRED); } } }
Example #9
Source File: RaftSessionInvoker.java From atomix with Apache License 2.0 | 6 votes |
@Override public void accept(QueryResponse response, Throwable error) { if (error == null) { if (response.status() == RaftResponse.Status.OK) { complete(response); } else if (response.error().type() == RaftError.Type.UNKNOWN_CLIENT || response.error().type() == RaftError.Type.UNKNOWN_SESSION) { complete(response.error().createException()); state.setState(PrimitiveState.EXPIRED); } else if (response.error().type() == RaftError.Type.UNKNOWN_SERVICE || response.error().type() == RaftError.Type.CLOSED_SESSION) { complete(response.error().createException()); state.setState(PrimitiveState.CLOSED); } else { complete(response.error().createException()); } } else { fail(error); } }
Example #10
Source File: PrimaryBackupSessionClient.java From atomix with Apache License 2.0 | 5 votes |
/** * Handles a cluster event. */ private void handleClusterEvent(ClusterMembershipEvent event) { PrimaryTerm term = this.term; if (term != null && event.type() == ClusterMembershipEvent.Type.MEMBER_REMOVED && event.subject().id().equals(term.primary().memberId())) { threadContext.execute(() -> { state = PrimitiveState.SUSPENDED; stateChangeListeners.forEach(l -> l.accept(state)); }); } }
Example #11
Source File: RaftSessionInvoker.java From atomix with Apache License 2.0 | 5 votes |
/** * Fails the attempt with the given exception. * * @param t The exception with which to fail the attempt. */ public void fail(Throwable t) { sequence(null, () -> { state.setCommandResponse(request.sequenceNumber()); future.completeExceptionally(t); }); // If the session has been expired or closed, update the client's state. if (EXPIRED_PREDICATE.test(t)) { state.setState(PrimitiveState.EXPIRED); } else if (CLOSED_PREDICATE.test(t)) { state.setState(PrimitiveState.CLOSED); } }
Example #12
Source File: AtomicMultimapProxy.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<AsyncAtomicMultimap<String, byte[]>> connect() { return super.connect() .thenCompose(v -> Futures.allOf(getProxyClient().getPartitions().stream().map(ProxySession::connect))) .thenRun(() -> getProxyClient().getPartitionIds().forEach(partition -> { getProxyClient().getPartition(partition).addStateChangeListener(state -> { if (state == PrimitiveState.CONNECTED && isListening()) { getProxyClient().acceptOn(partition, service -> service.listen()); } }); })) .thenApply(v -> this); }
Example #13
Source File: WorkQueueProxy.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<AsyncWorkQueue<byte[]>> connect() { return super.connect() .thenCompose(v -> getProxyClient().getPartition(name()).connect()) .thenRun(() -> getProxyClient().getPartition(name()).addStateChangeListener(state -> { if (state == PrimitiveState.CONNECTED && isRegistered.get()) { getProxyClient().acceptBy(name(), service -> service.register()); } })) .thenApply(v -> this); }
Example #14
Source File: DistributedCollectionProxy.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<A> connect() { return super.connect() .thenCompose(v -> getProxyClient().getPartition(name()).connect()) .thenRun(() -> { ProxySession<S> partition = getProxyClient().getPartition(name()); partition.addStateChangeListener(state -> { if (state == PrimitiveState.CONNECTED && isListening()) { partition.accept(service -> service.listen()); } }); }) .thenApply(v -> (A) this); }
Example #15
Source File: LogProxySession.java From atomix with Apache License 2.0 | 5 votes |
@Override public <R> CompletableFuture<R> apply(Function<S, R> operation) { if (session.getState() == PrimitiveState.CLOSED) { return Futures.exceptionalFuture(new PrimitiveException.ClosedSession()); } return proxy.apply(operation); }
Example #16
Source File: TestSessionClient.java From atomix with Apache License 2.0 | 5 votes |
@Override public synchronized CompletableFuture<SessionClient> connect() { CompletableFuture<SessionClient> future = new CompletableFuture<>(); service.open(sessionId, this).whenCompleteAsync((result, error) -> { if (error == null) { changeState(PrimitiveState.CONNECTED); future.complete(this); } else { future.completeExceptionally(error); } }, context); return future; }
Example #17
Source File: RetryingSessionClient.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<byte[]> execute(PrimitiveOperation operation) { if (getState() == PrimitiveState.CLOSED) { return Futures.exceptionalFuture(new PrimitiveException.ClosedSession()); } CompletableFuture<byte[]> future = new CompletableFuture<>(); execute(operation, 1, future); return future; }
Example #18
Source File: ClusterManagerServer.java From zeppelin with Apache License 2.0 | 5 votes |
@Override public boolean raftInitialized() { if (null != raftServer && raftServer.isRunning() && null != raftClient && null != raftSessionClient && raftSessionClient.getState() == PrimitiveState.CONNECTED) { return true; } return false; }
Example #19
Source File: PartitionedDistributedCollectionProxy.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<A> connect() { return super.connect() .thenCompose(v -> Futures.allOf(getProxyClient().getPartitions().stream().map(ProxySession::connect))) .thenRun(() -> getProxyClient().getPartitions().forEach(partition -> { partition.addStateChangeListener(state -> { if (state == PrimitiveState.CONNECTED && isListening()) { partition.accept(service -> service.listen()); } }); })) .thenApply(v -> (A) this); }
Example #20
Source File: CachingAsyncAtomicMap.java From atomix with Apache License 2.0 | 5 votes |
/** * Constructor to configure cache size. * * @param backingMap a distributed, strongly consistent map for backing * @param cacheConfig the cache configuration */ public CachingAsyncAtomicMap(AsyncAtomicMap<K, V> backingMap, CacheConfig cacheConfig) { super(backingMap); this.backingMap = backingMap; cache = CacheBuilder.newBuilder() .maximumSize(cacheConfig.getSize()) .build(CacheLoader.from(CachingAsyncAtomicMap.super::get)); cacheUpdater = event -> { Versioned<V> newValue = event.newValue(); if (newValue == null) { cache.invalidate(event.key()); } else { cache.put(event.key(), CompletableFuture.completedFuture(newValue)); } mapEventListeners.forEach((listener, executor) -> executor.execute(() -> listener.event(event))); }; statusListener = status -> { log.debug("{} status changed to {}", this.name(), status); // If the status of the underlying map is SUSPENDED or INACTIVE // we can no longer guarantee that the cache will be in sync. if (status == PrimitiveState.SUSPENDED || status == PrimitiveState.CLOSED) { cache.invalidateAll(); } }; super.addListener(cacheUpdater, MoreExecutors.directExecutor()); super.addStateChangeListener(statusListener); }
Example #21
Source File: DefaultRaftSessionClient.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<Void> delete() { if (state != null) { return sessionManager.closeSession(state.getSessionId(), true) .whenComplete((result, error) -> state.setState(PrimitiveState.CLOSED)); } return CompletableFuture.completedFuture(null); }
Example #22
Source File: RaftSessionInvoker.java From atomix with Apache License 2.0 | 5 votes |
/** * Submits an operation attempt. * * @param attempt The attempt to submit. */ private <T extends OperationRequest, U extends OperationResponse> void invoke(OperationAttempt<T, U> attempt) { if (state.getState() == PrimitiveState.CLOSED) { attempt.fail(new PrimitiveException.ClosedSession("session closed")); } else { attempts.put(attempt.sequence, attempt); attempt.send(); attempt.future.whenComplete((r, e) -> attempts.remove(attempt.sequence)); } }
Example #23
Source File: LeaderElectionProxy.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<AsyncLeaderElection<byte[]>> connect() { return super.connect() .thenCompose(v -> getProxyClient().getPartition(name()).connect()) .thenRun(() -> getProxyClient().getPartitions().forEach(partition -> { partition.addStateChangeListener(state -> { if (state == PrimitiveState.CONNECTED && isListening()) { partition.accept(service -> service.listen()); } }); })) .thenApply(v -> this); }
Example #24
Source File: RaftTest.java From atomix with Apache License 2.0 | 5 votes |
/** * Tests keeping a client session alive. */ @Test public void testClientKeepAlive() throws Throwable { createServers(3); RaftClient client = createClient(); SessionClient session = createSession(client); Thread.sleep(Duration.ofSeconds(10).toMillis()); threadAssertTrue(session.getState() == PrimitiveState.CONNECTED); }
Example #25
Source File: DistributedLogSession.java From atomix with Apache License 2.0 | 5 votes |
/** * Handles a cluster event. */ private void handleClusterEvent(ClusterMembershipEvent event) { PrimaryTerm term = this.term; if (term != null && event.type() == ClusterMembershipEvent.Type.MEMBER_REMOVED && event.subject().id().equals(term.primary().memberId())) { changeState(PrimitiveState.SUSPENDED); } }
Example #26
Source File: LeaderElectorProxy.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<AsyncLeaderElector<byte[]>> connect() { return super.connect() .thenCompose(v -> Futures.allOf(getProxyClient().getPartitions().stream().map(ProxySession::connect))) .thenRun(() -> getProxyClient().getPartitions().forEach(partition -> { partition.addStateChangeListener(state -> { if (state == PrimitiveState.CONNECTED && isListening()) { partition.accept(service -> service.listen()); } }); })) .thenApply(v -> this); }
Example #27
Source File: DistributedLogSession.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<Void> close() { CompletableFuture<Void> future = new CompletableFuture<>(); threadContext.execute(() -> { changeState(PrimitiveState.CLOSED); future.complete(null); }); return future; }
Example #28
Source File: DistributedLogClient.java From atomix with Apache License 2.0 | 5 votes |
public DistributedLogClient( LogProtocol protocol, Collection<LogSession> partitions, Partitioner<String> partitioner) { this.protocol = checkNotNull(protocol, "protocol cannot be null"); this.partitioner = checkNotNull(partitioner, "partitioner cannot be null"); partitions.forEach(partition -> { this.partitionIds.add(partition.partitionId()); this.partitions.put(partition.partitionId(), partition); this.sortedPartitions.add(partition); states.put(partition.partitionId(), PrimitiveState.CLOSED); partition.addStateChangeListener(state -> onStateChange(partition.partitionId(), state)); }); }
Example #29
Source File: KeyLock.java From atomix with Apache License 2.0 | 5 votes |
/** * Handles a primitive state change. * * @param state the primitive state change */ void change(PrimitiveState state) { if (state != PrimitiveState.CONNECTED) { for (LockFuture future : futures.values()) { client.acceptOn(partitionId, service -> service.unlock(key, future.id())); future.completeExceptionally(new PrimitiveException.Unavailable()); } } }
Example #30
Source File: DistributedLogClient.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<LogClient> connect() { return Futures.allOf(partitions.values().stream().map(LogSession::connect)).thenApply(v -> { changeState(PrimitiveState.CONNECTED); return this; }); }