io.atomix.protocols.raft.session.CommunicationStrategy Java Examples

The following examples show how to use io.atomix.protocols.raft.session.CommunicationStrategy. 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: DefaultRaftSessionClient.java    From atomix with Apache License 2.0 6 votes vote down vote up
public DefaultRaftSessionClient(
    String serviceName,
    PrimitiveType primitiveType,
    ServiceConfig serviceConfig,
    PartitionId partitionId,
    RaftClientProtocol protocol,
    MemberSelectorManager selectorManager,
    RaftSessionManager sessionManager,
    ReadConsistency readConsistency,
    CommunicationStrategy communicationStrategy,
    ThreadContext context,
    Duration minTimeout,
    Duration maxTimeout) {
  this.serviceName = checkNotNull(serviceName, "serviceName cannot be null");
  this.primitiveType = checkNotNull(primitiveType, "serviceType cannot be null");
  this.serviceConfig = checkNotNull(serviceConfig, "serviceConfig cannot be null");
  this.partitionId = checkNotNull(partitionId, "partitionId cannot be null");
  this.protocol = checkNotNull(protocol, "protocol cannot be null");
  this.selectorManager = checkNotNull(selectorManager, "selectorManager cannot be null");
  this.readConsistency = checkNotNull(readConsistency, "readConsistency cannot be null");
  this.communicationStrategy = checkNotNull(communicationStrategy, "communicationStrategy cannot be null");
  this.context = checkNotNull(context, "context cannot be null");
  this.minTimeout = checkNotNull(minTimeout, "minTimeout cannot be null");
  this.maxTimeout = checkNotNull(maxTimeout, "maxTimeout cannot be null");
  this.sessionManager = checkNotNull(sessionManager, "sessionManager cannot be null");
}
 
Example #2
Source File: RaftSessionManager.java    From atomix with Apache License 2.0 6 votes vote down vote up
public RaftSessionManager(String clientId, MemberId memberId, RaftClientProtocol protocol, MemberSelectorManager selectorManager, ThreadContextFactory threadContextFactory) {
  this.clientId = checkNotNull(clientId, "clientId cannot be null");
  this.memberId = checkNotNull(memberId, "memberId cannot be null");
  this.protocol = checkNotNull(protocol, "protocol cannot be null");
  this.selectorManager = checkNotNull(selectorManager, "selectorManager cannot be null");
  this.threadContext = threadContextFactory.createContext();
  this.log = ContextualLoggerFactory.getLogger(getClass(), LoggerContext.builder(RaftClient.class)
      .addValue(clientId)
      .build());

  this.connection = new RaftSessionConnection(
      protocol,
      selectorManager.createSelector(CommunicationStrategy.LEADER),
      threadContextFactory.createContext(),
      LoggerContext.builder(RaftClient.class)
          .addValue(clientId)
          .build());
  protocol.registerHeartbeatHandler(this::handleHeartbeat);
  this.threadContextFactory = checkNotNull(threadContextFactory, "threadContextFactory cannot be null");
}
 
Example #3
Source File: ClusterManager.java    From submarine with Apache License 2.0 5 votes vote down vote up
private SessionClient createProxy(RaftClient client) {
  return client.sessionBuilder(ClusterPrimitiveType.PRIMITIVE_NAME,
      ClusterPrimitiveType.INSTANCE, new ServiceConfig())
      .withReadConsistency(ReadConsistency.SEQUENTIAL)
      .withCommunicationStrategy(CommunicationStrategy.LEADER)
      .build()
      .connect()
      .join();
}
 
Example #4
Source File: ClusterManager.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private SessionClient createProxy(RaftClient client) {
  return client.sessionBuilder(ClusterPrimitiveType.PRIMITIVE_NAME,
      ClusterPrimitiveType.INSTANCE, new ServiceConfig())
      .withReadConsistency(ReadConsistency.SEQUENTIAL)
      .withCommunicationStrategy(CommunicationStrategy.LEADER)
      .build()
      .connect()
      .join();
}
 
Example #5
Source File: MemberSelector.java    From atomix with Apache License 2.0 5 votes vote down vote up
public MemberSelector(MemberId leader, Collection<MemberId> members, CommunicationStrategy strategy, MemberSelectorManager selectors) {
  this.leader = leader;
  this.members = new LinkedHashSet<>(members);
  this.strategy = checkNotNull(strategy, "strategy cannot be null");
  this.selectors = checkNotNull(selectors, "selectors cannot be null");
  this.selections = strategy.selectConnections(leader, Lists.newLinkedList(members));
}
 
Example #6
Source File: DefaultRaftMetadataClient.java    From atomix with Apache License 2.0 5 votes vote down vote up
public DefaultRaftMetadataClient(String clientId, RaftClientProtocol protocol, MemberSelectorManager selectorManager, ThreadContext context) {
  this.selectorManager = checkNotNull(selectorManager, "selectorManager cannot be null");
  this.connection = new RaftSessionConnection(
      protocol,
      selectorManager.createSelector(CommunicationStrategy.LEADER),
      context,
      LoggerContext.builder(RaftClient.class)
          .addValue(clientId)
          .build());
}
 
Example #7
Source File: MemberSelectorTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Tests selecting members using the ANY selector.
 */
@Test
public void testSelectAny() throws Exception {
  MemberSelectorManager selectorManager = new MemberSelectorManager();
  MemberSelector selector = selectorManager.createSelector(CommunicationStrategy.ANY);

  assertNull(selector.leader());
  assertFalse(selector.hasNext());

  selectorManager.resetAll(null, Arrays.asList(MemberId.from("a"), MemberId.from("b"), MemberId.from("c")));
  assertNull(selector.leader());
  assertTrue(selector.hasNext());
  selector.hasNext();
  assertTrue(selector.hasNext());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertFalse(selector.hasNext());
  selector.reset();
  assertTrue(selector.hasNext());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertFalse(selector.hasNext());

  selectorManager.resetAll(MemberId.from("a"), Arrays.asList(MemberId.from("a"), MemberId.from("b"), MemberId.from("c")));
  assertNotNull(selector.leader());
  assertTrue(selector.hasNext());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertFalse(selector.hasNext());
  selector.reset();
  assertTrue(selector.hasNext());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertFalse(selector.hasNext());
}
 
Example #8
Source File: MemberSelectorTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Tests selecting members using the FOLLOWER selector.
 */
@Test
public void testSelectFollower() throws Exception {
  MemberSelectorManager selectorManager = new MemberSelectorManager();
  MemberSelector selector = selectorManager.createSelector(CommunicationStrategy.FOLLOWERS);

  assertNull(selector.leader());
  assertFalse(selector.hasNext());

  selectorManager.resetAll(null, Arrays.asList(MemberId.from("a"), MemberId.from("b"), MemberId.from("c")));
  assertNull(selector.leader());
  assertTrue(selector.hasNext());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertFalse(selector.hasNext());
  selector.reset();
  assertTrue(selector.hasNext());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertFalse(selector.hasNext());

  selectorManager.resetAll(MemberId.from("a"), Arrays.asList(MemberId.from("a"), MemberId.from("b"), MemberId.from("c")));
  assertNotNull(selector.leader());
  assertTrue(selector.hasNext());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertFalse(selector.hasNext());
}
 
Example #9
Source File: MemberSelectorTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the member selector.
 */
@Test
public void testSelectLeader() throws Exception {
  MemberSelectorManager selectorManager = new MemberSelectorManager();
  MemberSelector selector = selectorManager.createSelector(CommunicationStrategy.LEADER);

  assertNull(selector.leader());
  assertFalse(selector.hasNext());

  selectorManager.resetAll(null, Arrays.asList(MemberId.from("a"), MemberId.from("b"), MemberId.from("c")));
  assertNull(selector.leader());
  assertTrue(selector.hasNext());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertFalse(selector.hasNext());
  selector.reset();
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertNotNull(selector.next());
  assertFalse(selector.hasNext());

  selectorManager.resetAll(MemberId.from("a"), Arrays.asList(MemberId.from("a"), MemberId.from("b"), MemberId.from("c")));
  assertEquals(MemberId.from("a"), selector.leader());
  assertEquals(3, selector.members().size());
  assertTrue(selector.hasNext());
  assertNotNull(selector.next());
  assertFalse(selector.hasNext());

  selectorManager.resetAll(null, Arrays.asList(MemberId.from("a"), MemberId.from("b"), MemberId.from("c")));
  assertNull(selector.leader());
  assertTrue(selector.hasNext());

  selectorManager.resetAll(MemberId.from("a"), Arrays.asList(MemberId.from("b"), MemberId.from("c")));
  assertNull(selector.leader());
  assertTrue(selector.hasNext());
}
 
Example #10
Source File: MultiRaftProtocolConfigTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfig() throws Exception {
  MultiRaftProtocolConfig config = new MultiRaftProtocolConfig();
  assertEquals(MultiRaftProtocol.TYPE, config.getType());
  assertNull(config.getGroup());
  assertSame(Partitioner.MURMUR3, config.getPartitioner());
  assertEquals(Duration.ofMillis(250), config.getMinTimeout());
  assertEquals(Duration.ofSeconds(30), config.getMaxTimeout());
  assertEquals(ReadConsistency.SEQUENTIAL, config.getReadConsistency());
  assertEquals(CommunicationStrategy.LEADER, config.getCommunicationStrategy());
  assertEquals(Recovery.RECOVER, config.getRecoveryStrategy());
  assertEquals(0, config.getMaxRetries());
  assertEquals(Duration.ofMillis(100), config.getRetryDelay());

  Partitioner<String> partitioner = (k, p) -> null;
  config.setGroup("test");
  config.setPartitioner(partitioner);
  config.setMinTimeout(Duration.ofSeconds(1));
  config.setMaxTimeout(Duration.ofSeconds(10));
  config.setReadConsistency(ReadConsistency.LINEARIZABLE);
  config.setCommunicationStrategy(CommunicationStrategy.ANY);
  config.setRecoveryStrategy(Recovery.CLOSE);
  config.setMaxRetries(5);
  config.setRetryDelay(Duration.ofSeconds(1));

  assertEquals("test", config.getGroup());
  assertSame(partitioner, config.getPartitioner());
  assertEquals(Duration.ofSeconds(1), config.getMinTimeout());
  assertEquals(Duration.ofSeconds(10), config.getMaxTimeout());
  assertEquals(ReadConsistency.LINEARIZABLE, config.getReadConsistency());
  assertEquals(CommunicationStrategy.ANY, config.getCommunicationStrategy());
  assertEquals(Recovery.CLOSE, config.getRecoveryStrategy());
  assertEquals(5, config.getMaxRetries());
  assertEquals(Duration.ofSeconds(1), config.getRetryDelay());
}
 
Example #11
Source File: DefaultRaftSessionClient.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<SessionClient> connect() {
  return sessionManager.openSession(
      serviceName,
      primitiveType,
      serviceConfig,
      readConsistency,
      communicationStrategy,
      minTimeout,
      maxTimeout)
      .thenApply(state -> {
        this.state = state;

        // Create command/query connections.
        RaftSessionConnection leaderConnection = new RaftSessionConnection(
            protocol,
            selectorManager.createSelector(CommunicationStrategy.LEADER),
            context,
            LoggerContext.builder(SessionClient.class)
                .addValue(state.getSessionId())
                .add("type", state.getPrimitiveType())
                .add("name", state.getPrimitiveName())
                .build());
        RaftSessionConnection sessionConnection = new RaftSessionConnection(
            protocol,
            selectorManager.createSelector(communicationStrategy),
            context,
            LoggerContext.builder(SessionClient.class)
                .addValue(state.getSessionId())
                .add("type", state.getPrimitiveType())
                .add("name", state.getPrimitiveName())
                .build());

        // Create proxy submitter/listener.
        RaftSessionSequencer sequencer = new RaftSessionSequencer(state);
        this.proxyListener = new RaftSessionListener(
            protocol,
            selectorManager.createSelector(CommunicationStrategy.ANY),
            state,
            sequencer,
            context);
        this.proxyInvoker = new RaftSessionInvoker(
            leaderConnection,
            sessionConnection,
            state,
            sequencer,
            sessionManager,
            context);

        selectorManager.addLeaderChangeListener(leaderChangeListener);
        state.addStateChangeListener(s -> {
          if (s == PrimitiveState.EXPIRED || s == PrimitiveState.CLOSED) {
            selectorManager.removeLeaderChangeListener(leaderChangeListener);
            proxyListener.close();
            proxyInvoker.close();
          }
        });

        return this;
      });
}
 
Example #12
Source File: RaftSessionManager.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Opens a new session.
 *
 * @param serviceName           The session name.
 * @param primitiveType         The session type.
 * @param communicationStrategy The strategy with which to communicate with servers.
 * @param minTimeout            The minimum session timeout.
 * @param maxTimeout            The maximum session timeout.
 * @return A completable future to be completed once the session has been opened.
 */
public CompletableFuture<RaftSessionState> openSession(
    String serviceName,
    PrimitiveType primitiveType,
    ServiceConfig config,
    ReadConsistency readConsistency,
    CommunicationStrategy communicationStrategy,
    Duration minTimeout,
    Duration maxTimeout) {
  checkNotNull(serviceName, "serviceName cannot be null");
  checkNotNull(primitiveType, "serviceType cannot be null");
  checkNotNull(communicationStrategy, "communicationStrategy cannot be null");
  checkNotNull(maxTimeout, "timeout cannot be null");

  log.debug("Opening session; name: {}, type: {}", serviceName, primitiveType);
  OpenSessionRequest request = OpenSessionRequest.builder()
      .withMemberId(memberId)
      .withServiceName(serviceName)
      .withServiceType(primitiveType)
      .withServiceConfig(Serializer.using(primitiveType.namespace()).encode(config))
      .withReadConsistency(readConsistency)
      .withMinTimeout(minTimeout.toMillis())
      .withMaxTimeout(maxTimeout.toMillis())
      .build();

  CompletableFuture<RaftSessionState> future = new CompletableFuture<>();
  ThreadContext proxyContext = threadContextFactory.createContext();
  connection.openSession(request).whenCompleteAsync((response, error) -> {
    if (error == null) {
      if (response.status() == RaftResponse.Status.OK) {
        // Create and store the proxy state.
        RaftSessionState state = new RaftSessionState(
            clientId,
            SessionId.from(response.session()),
            serviceName,
            primitiveType,
            response.timeout());
        sessions.put(state.getSessionId().id(), state);

        state.addStateChangeListener(s -> {
          if (s == PrimitiveState.EXPIRED || s == PrimitiveState.CLOSED) {
            sessions.remove(state.getSessionId().id());
          }
        });

        // Ensure the proxy session info is reset and the session is kept alive.
        keepAliveSessions(System.currentTimeMillis(), state.getSessionTimeout());

        future.complete(state);
      } else {
        future.completeExceptionally(new RaftException.Unavailable(response.error().message()));
      }
    } else {
      future.completeExceptionally(new RaftException.Unavailable(error.getMessage()));
    }
  }, proxyContext);
  return future;
}
 
Example #13
Source File: MemberSelectorManager.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new address selector.
 *
 * @param selectionStrategy The server selection strategy.
 * @return A new address selector.
 */
public MemberSelector createSelector(CommunicationStrategy selectionStrategy) {
  MemberSelector selector = new MemberSelector(leader, members, selectionStrategy, this);
  selectors.add(selector);
  return selector;
}
 
Example #14
Source File: MultiRaftProtocolBuilder.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the communication strategy.
 *
 * @param communicationStrategy the communication strategy
 * @return the Raft protocol builder
 */
public MultiRaftProtocolBuilder withCommunicationStrategy(CommunicationStrategy communicationStrategy) {
  config.setCommunicationStrategy(communicationStrategy);
  return this;
}
 
Example #15
Source File: MultiRaftProtocolConfig.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the client communication strategy.
 *
 * @return the client communication strategy
 */
public CommunicationStrategy getCommunicationStrategy() {
  return communicationStrategy;
}
 
Example #16
Source File: MultiRaftProtocolConfig.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the client communication strategy.
 *
 * @param communicationStrategy the client communication strategy
 * @return the Raft protocol configuration
 */
public MultiRaftProtocolConfig setCommunicationStrategy(CommunicationStrategy communicationStrategy) {
  this.communicationStrategy = communicationStrategy;
  return this;
}