io.atomix.cluster.MemberId Java Examples

The following examples show how to use io.atomix.cluster.MemberId. 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: RaftClusterContext.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> bootstrap(Collection<MemberId> cluster) {
  if (joinFuture != null) {
    return joinFuture;
  }

  if (configuration == null) {
    member.setType(RaftMember.Type.ACTIVE);

    // Create a set of active members.
    Set<RaftMember> activeMembers = cluster.stream()
        .filter(m -> !m.equals(member.memberId()))
        .map(m -> new DefaultRaftMember(m, RaftMember.Type.ACTIVE, member.getLastUpdated()))
        .collect(Collectors.toSet());

    // Add the local member to the set of active members.
    activeMembers.add(member);

    // Create a new configuration and store it on disk to ensure the cluster can fall back to the configuration.
    configure(new Configuration(0, 0, member.getLastUpdated().toEpochMilli(), activeMembers));
  }
  return join();
}
 
Example #2
Source File: AbstractRole.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Forwards the given request to the leader if possible.
 */
protected <T extends RaftRequest, U extends RaftResponse> CompletableFuture<U> forward(T request, BiFunction<MemberId, T, CompletableFuture<U>> function) {
  CompletableFuture<U> future = new CompletableFuture<>();
  DefaultRaftMember leader = raft.getLeader();
  if (leader == null) {
    return Futures.exceptionalFuture(new RaftException.NoLeader("No leader found"));
  }

  function.apply(leader.memberId(), request).whenCompleteAsync((response, error) -> {
    if (error == null) {
      future.complete(response);
    } else {
      future.completeExceptionally(error);
    }
  }, raft.getThreadContext());
  return future;
}
 
Example #3
Source File: DefaultRaftClient.java    From atomix with Apache License 2.0 6 votes vote down vote up
public DefaultRaftClient(
    String clientId,
    PartitionId partitionId,
    MemberId memberId,
    Collection<MemberId> cluster,
    RaftClientProtocol protocol,
    ThreadContextFactory threadContextFactory,
    boolean closeThreadFactoryOnClose) {
  this.clientId = checkNotNull(clientId, "clientId cannot be null");
  this.partitionId = checkNotNull(partitionId, "partitionId cannot be null");
  this.cluster = checkNotNull(cluster, "cluster cannot be null");
  this.protocol = checkNotNull(protocol, "protocol cannot be null");
  this.threadContextFactory = checkNotNull(threadContextFactory, "threadContextFactory cannot be null");
  this.threadContext = threadContextFactory.createContext();
  this.metadata = new DefaultRaftMetadataClient(clientId, protocol, selectorManager, threadContextFactory.createContext());
  this.sessionManager = new RaftSessionManager(clientId, memberId, protocol, selectorManager, threadContextFactory);
  this.closeThreadFactoryOnClose = closeThreadFactoryOnClose;
}
 
Example #4
Source File: DistributedLogSession.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Registers the consumer with the given leader.
 *
 * @param leader the leader with which to register the consumer
 */
private CompletableFuture<Void> register(MemberId leader) {
  CompletableFuture<Void> future = new CompletableFuture<>();
  this.leader = leader;
  protocol.consume(leader, ConsumeRequest.request(memberId, subject, index + 1))
      .whenCompleteAsync((response, error) -> {
        if (error == null) {
          if (response.status() == LogResponse.Status.OK) {
            future.complete(null);
          } else {
            future.completeExceptionally(new PrimitiveException.Unavailable());
          }
        } else {
          future.completeExceptionally(error);
        }
      }, threadContext);
  return future;
}
 
Example #5
Source File: RaftPartitionClient.java    From atomix with Apache License 2.0 5 votes vote down vote up
public RaftPartitionClient(
    RaftPartition partition,
    MemberId localMemberId,
    RaftClientProtocol protocol,
    ThreadContextFactory threadContextFactory) {
  this.partition = partition;
  this.localMemberId = localMemberId;
  this.protocol = protocol;
  this.threadContextFactory = threadContextFactory;
}
 
Example #6
Source File: AsynchronousReplicator.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> replicate(BackupOperation operation) {
  for (MemberId backup : context.backups()) {
    queues.computeIfAbsent(backup, BackupQueue::new).add(operation);
  }
  context.setCommitIndex(operation.index());
  return CompletableFuture.completedFuture(null);
}
 
Example #7
Source File: TestRaftServerProtocol.java    From atomix with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<TestRaftServerProtocol> getServer(MemberId memberId) {
  TestRaftServerProtocol server = server(memberId);
  if (server != null) {
    return Futures.completedFuture(server);
  } else {
    return Futures.exceptionalFuture(new ConnectException());
  }
}
 
Example #8
Source File: CoreTransactionService.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively recovers transactions using the given iterator.
 *
 * @param iterator the asynchronous iterator from which to recover transactions
 * @param memberId the transaction member ID
 */
private void recoverTransactions(AsyncIterator<Map.Entry<TransactionId, Versioned<TransactionInfo>>> iterator, MemberId memberId) {
  iterator.next().thenAccept(entry -> {
    if (entry.getValue().value().coordinator.equals(memberId)) {
      recoverTransaction(entry.getKey(), entry.getValue().value());
    }
    recoverTransactions(iterator, memberId);
  });
}
 
Example #9
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 #10
Source File: RaftTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
private RaftServer createServer(MemberId memberId, Function<RaftServer.Builder, RaftServer.Builder> configurator) {
  final RaftServer.Builder defaults =
      RaftServer.builder(memberId)
          .withMembershipService(mock(ClusterMembershipService.class))
          .withProtocol(protocolFactory.newServerProtocol(memberId));
  final RaftServer server = configurator.apply(defaults).build();

  servers.add(server);
  return server;
}
 
Example #11
Source File: RaftServer.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new Raft server builder using the default host:port.
 * <p>
 * The server will be constructed at 0.0.0.0:8700.
 *
 * @return The server builder.
 */
static Builder builder() {
  try {
    InetAddress address = InetAddress.getByName("0.0.0.0");
    return builder(MemberId.from(address.getHostName()));
  } catch (UnknownHostException e) {
    throw new ConfigurationException("Cannot configure local node", e);
  }
}
 
Example #12
Source File: RaftMessagingProtocol.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
protected <T, U> CompletableFuture<U> sendAndReceive(MemberId memberId,
                                                     String type, T request) {
  Address address = address(memberId);
  if (address == null) {
    return Futures.exceptionalFuture(new ConnectException());
  }
  return messagingService.sendAndReceive(address, type, serializer.encode(request))
      .thenApply(serializer::decode);
}
 
Example #13
Source File: TestPrimaryBackupClientProtocol.java    From atomix with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<TestPrimaryBackupServerProtocol> getServer(MemberId memberId) {
  TestPrimaryBackupServerProtocol server = server(memberId);
  if (server != null) {
    return Futures.completedFuture(server);
  } else {
    return Futures.exceptionalFuture(new ConnectException());
  }
}
 
Example #14
Source File: RaftServiceManager.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Applies an open session entry to the state machine.
 */
private long applyOpenSession(Indexed<OpenSessionEntry> entry) {
  PrimitiveType primitiveType = raft.getPrimitiveTypes().getPrimitiveType(entry.entry().serviceType());

  // Get the state machine executor or create one if it doesn't already exist.
  RaftServiceContext service = getOrInitializeService(
      PrimitiveId.from(entry.index()),
      primitiveType,
      entry.entry().serviceName(),
      entry.entry().serviceConfig());

  if (service == null) {
    throw new RaftException.UnknownService("Unknown service type " + entry.entry().serviceType());
  }

  SessionId sessionId = SessionId.from(entry.index());
  RaftSession session = raft.getSessions().addSession(new RaftSession(
      sessionId,
      MemberId.from(entry.entry().memberId()),
      entry.entry().serviceName(),
      primitiveType,
      entry.entry().readConsistency(),
      entry.entry().minTimeout(),
      entry.entry().maxTimeout(),
      entry.entry().timestamp(),
      service.serializer(),
      service,
      raft,
      threadContextFactory));
  return service.openSession(entry.index(), entry.entry().timestamp(), session);
}
 
Example #15
Source File: RaftClientMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return sendAndReceive(memberId, "query", request);
}
 
Example #16
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<CloseSessionResponse> closeSession(MemberId memberId,
                                                            CloseSessionRequest request) {
  return sendAndReceive(memberId, "close-session", request);
}
 
Example #17
Source File: RaftClientCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<MetadataResponse> metadata(MemberId memberId, MetadataRequest request) {
  return sendAndReceive(context.metadataSubject, request, memberId);
}
 
Example #18
Source File: HeartbeatRequest.java    From atomix with Apache License 2.0 4 votes vote down vote up
public HeartbeatRequest(MemberId leader, Collection<MemberId> members) {
  this.leader = leader;
  this.members = members;
}
 
Example #19
Source File: RaftServerCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<OpenSessionResponse> openSession(MemberId memberId, OpenSessionRequest request) {
  return sendAndReceive(context.openSessionSubject, request, memberId);
}
 
Example #20
Source File: LocalRaftServerProtocol.java    From submarine with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<ConfigureResponse> configure(MemberId memberId,
                                                      ConfigureRequest request) {
  return getServer(memberId).thenCompose(listener ->
      listener.configure(encode(request))).thenApply(this::decode);
}
 
Example #21
Source File: LocalRaftClientProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<OpenSessionResponse> openSession(MemberId memberId, OpenSessionRequest request) {
  return getServer(memberId).thenCompose(protocol -> protocol.openSession(encode(request))).thenApply(this::decode);
}
 
Example #22
Source File: ResetRequest.java    From atomix with Apache License 2.0 4 votes vote down vote up
public static ResetRequest request(MemberId memberId, String subject, long index) {
  return new ResetRequest(memberId, subject, index);
}
 
Example #23
Source File: ConsumeRequest.java    From atomix with Apache License 2.0 4 votes vote down vote up
public MemberId memberId() {
  return memberId;
}
 
Example #24
Source File: ResetRequest.java    From atomix with Apache License 2.0 4 votes vote down vote up
public MemberId memberId() {
  return memberId;
}
 
Example #25
Source File: RaftServerMessagingProtocol.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<QueryResponse> query(MemberId memberId, QueryRequest request) {
  return sendAndReceive(memberId, "query", request);
}
 
Example #26
Source File: TestLogServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
public TestLogServerProtocol(MemberId memberId, Map<MemberId, TestLogServerProtocol> servers, Map<MemberId, TestLogClientProtocol> clients) {
  super(servers, clients);
  servers.put(memberId, this);
}
 
Example #27
Source File: LocalRaftClientProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<CommandResponse> command(MemberId memberId, CommandRequest request) {
  return getServer(memberId).thenCompose(protocol -> protocol.command(encode(request))).thenApply(this::decode);
}
 
Example #28
Source File: LogClientCommunicator.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public void reset(MemberId memberId, ResetRequest request) {
  unicast(context.resetSubject, request, memberId);
}
 
Example #29
Source File: RaftServer.java    From atomix with Apache License 2.0 4 votes vote down vote up
protected Builder(MemberId localMemberId) {
  this.localMemberId = checkNotNull(localMemberId, "localMemberId cannot be null");
}
 
Example #30
Source File: LocalRaftServerProtocol.java    From atomix with Apache License 2.0 4 votes vote down vote up
public LocalRaftServerProtocol(MemberId memberId, Serializer serializer, Map<MemberId, LocalRaftServerProtocol> servers, Map<MemberId, LocalRaftClientProtocol> clients) {
  super(serializer, servers, clients);
  servers.put(memberId, this);
}