Java Code Examples for io.atomix.primitive.PrimitiveState#CLOSED

The following examples show how to use io.atomix.primitive.PrimitiveState#CLOSED . 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: RecoveringSessionClient.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the session state.
 *
 * @param state the session state
 */
private synchronized void onStateChange(PrimitiveState state) {
  if (this.state != state) {
    if (state == PrimitiveState.EXPIRED) {
      if (connected) {
        onStateChange(PrimitiveState.SUSPENDED);
        recover();
      } else {
        log.debug("State changed: {}", state);
        this.state = state;
        stateChangeListeners.forEach(l -> l.accept(state));
      }
    } else {
      log.debug("State changed: {}", state);
      this.state = state;
      stateChangeListeners.forEach(l -> l.accept(state));
      if (state == PrimitiveState.CLOSED) {
        connectFuture = Futures.exceptionalFuture(new PrimitiveException.ClosedSession());
        session = null;
      }
    }
  }
}
 
Example 2
Source File: AbstractProxyClient.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Handles a partition proxy state change.
 */
private synchronized void onStateChange(PartitionId partitionId, PrimitiveState state) {
  states.put(partitionId, state);
  switch (state) {
    case CONNECTED:
      if (this.state != PrimitiveState.CONNECTED && !states.containsValue(PrimitiveState.SUSPENDED) && !states.containsValue(PrimitiveState.CLOSED)) {
        this.state = PrimitiveState.CONNECTED;
        stateChangeListeners.forEach(l -> l.accept(PrimitiveState.CONNECTED));
      }
      break;
    case SUSPENDED:
      if (this.state == PrimitiveState.CONNECTED) {
        this.state = PrimitiveState.SUSPENDED;
        stateChangeListeners.forEach(l -> l.accept(PrimitiveState.SUSPENDED));
      }
      break;
    case CLOSED:
      if (this.state != PrimitiveState.CLOSED) {
        this.state = PrimitiveState.CLOSED;
        stateChangeListeners.forEach(l -> l.accept(PrimitiveState.CLOSED));
      }
      break;
  }
}
 
Example 3
Source File: RaftSessionState.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * 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 4
Source File: CachingAsyncLeaderElector.java    From atomix with Apache License 2.0 6 votes vote down vote up
public CachingAsyncLeaderElector(AsyncLeaderElector<T> delegateLeaderElector, CacheConfig cacheConfig) {
  super(delegateLeaderElector);
  cache = CacheBuilder.newBuilder()
      .maximumSize(cacheConfig.getSize())
      .build(CacheLoader.from(super::getLeadership));

  cacheUpdater = event -> {
    Leadership<T> leadership = event.newLeadership();
    cache.put(event.topic(), CompletableFuture.completedFuture(leadership));
  };
  statusListener = status -> {
    if (status == PrimitiveState.SUSPENDED || status == PrimitiveState.CLOSED) {
      cache.invalidateAll();
    }
  };
  addListener(cacheUpdater);
  addStateChangeListener(statusListener);
}
 
Example 5
Source File: CachingAsyncDistributedCollection.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * 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 6
Source File: TestSessionClient.java    From atomix with Apache License 2.0 6 votes vote down vote up
@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: RetryingSessionClient.java    From atomix with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: LogProxySession.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> accept(Consumer<S> operation) {
  if (session.getState() == PrimitiveState.CLOSED) {
    return Futures.exceptionalFuture(new PrimitiveException.ClosedSession());
  }
  return proxy.accept(operation);
}
 
Example 9
Source File: LogProxySession.java    From atomix with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: RaftSessionInvoker.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * 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 11
Source File: CachingAsyncAtomicMap.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}