io.atomix.protocols.raft.RaftClient Java Examples

The following examples show how to use io.atomix.protocols.raft.RaftClient. 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: 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 #2
Source File: RaftPartitionGroup.java    From atomix with Apache License 2.0 6 votes vote down vote up
public RaftPartitionGroup(RaftPartitionGroupConfig config) {
  Logger log = ContextualLoggerFactory.getLogger(DefaultRaftClient.class, LoggerContext.builder(RaftClient.class)
      .addValue(config.getName())
      .build());
  this.name = config.getName();
  this.config = config;
  this.partitionSize = config.getPartitionSize();

  int threadPoolSize = Math.max(Math.min(Runtime.getRuntime().availableProcessors() * 2, 16), 4);
  this.threadContextFactory = new BlockingAwareThreadPoolContextFactory(
      "raft-partition-group-" + name + "-%d", threadPoolSize, log);
  this.snapshotSubject = "raft-partition-group-" + name + "-snapshot";

  buildPartitions(config, threadContextFactory).forEach(p -> {
    this.partitions.put(p.id(), p);
    this.sortedPartitionIds.add(p.id());
  });
  Collections.sort(sortedPartitionIds);
}
 
Example #3
Source File: DefaultRaftClient.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public RaftClient build() {
  checkNotNull(memberId, "memberId cannot be null");
  Logger log = ContextualLoggerFactory.getLogger(DefaultRaftClient.class, LoggerContext.builder(RaftClient.class)
      .addValue(clientId)
      .build());

  // If a ThreadContextFactory was not provided, create one and ensure it's closed when the client is stopped.
  boolean closeThreadFactoryOnClose;
  ThreadContextFactory threadContextFactory;
  if (this.threadContextFactory == null) {
    threadContextFactory = threadModel.factory("raft-client-" + clientId + "-%d", threadPoolSize, log);
    closeThreadFactoryOnClose = true;
  } else {
    threadContextFactory = this.threadContextFactory;
    closeThreadFactoryOnClose = false;
  }

  return new DefaultRaftClient(clientId, partitionId, memberId, cluster, protocol, threadContextFactory, closeThreadFactoryOnClose);
}
 
Example #4
Source File: RaftPerformanceTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Raft client.
 */
private RaftClient createClient() throws Exception {
  Member member = nextNode();

  RaftClientProtocol protocol;
  if (USE_NETTY) {
    MessagingService messagingService = new NettyMessagingService("test", member.address(), new MessagingConfig()).start().join();
    protocol = new RaftClientMessagingProtocol(messagingService, PROTOCOL_SERIALIZER, addressMap::get);
  } else {
    protocol = protocolFactory.newClientProtocol(member.id());
  }

  RaftClient client = RaftClient.builder()
      .withMemberId(member.id())
      .withPartitionId(PartitionId.from("test", 1))
      .withProtocol(protocol)
      .withThreadModel(ThreadModel.SHARED_THREAD_POOL)
      .build();

  client.connect(members.stream().map(Member::id).collect(Collectors.toList())).join();
  clients.add(client);
  return client;
}
 
Example #5
Source File: RaftFuzzTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Raft client.
 */
private RaftClient createClient() throws Exception {
  MemberId memberId = nextNodeId();

  RaftClientProtocol protocol;
  if (USE_NETTY) {
    Address address = Address.from(++port);
    MessagingService messagingManager = new NettyMessagingService("test", address, new MessagingConfig()).start().join();
    addressMap.put(memberId, address);
    protocol = new RaftClientMessagingProtocol(messagingManager, PROTOCOL_SERIALIZER, addressMap::get);
  } else {
    protocol = protocolFactory.newClientProtocol(memberId);
  }

  RaftClient client = RaftClient.builder()
      .withMemberId(memberId)
      .withProtocol(protocol)
      .build();

  client.connect(members.stream().map(RaftMember::memberId).collect(Collectors.toList())).join();
  clients.add(client);
  return client;
}
 
Example #6
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 #7
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 #8
Source File: RaftPartitionClient.java    From atomix with Apache License 2.0 5 votes vote down vote up
private RaftClient newRaftClient(RaftClientProtocol protocol) {
  return RaftClient.builder()
      .withClientId(partition.name())
      .withPartitionId(partition.id())
      .withMemberId(localMemberId)
      .withProtocol(protocol)
      .withThreadContextFactory(threadContextFactory)
      .build();
}
 
Example #9
Source File: DefaultRaftClient.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized CompletableFuture<RaftClient> connect(Collection<MemberId> cluster) {
  CompletableFuture<RaftClient> future = new CompletableFuture<>();

  // If the provided cluster list is null or empty, use the default list.
  if (cluster == null || cluster.isEmpty()) {
    cluster = this.cluster;
  }

  // If the default list is null or empty, use the default host:port.
  if (cluster == null || cluster.isEmpty()) {
    throw new IllegalArgumentException("No cluster specified");
  }

  // Reset the connection list to allow the selection strategy to prioritize connections.
  sessionManager.resetConnections(null, cluster);

  // Register the session manager.
  sessionManager.open().whenCompleteAsync((result, error) -> {
    if (error == null) {
      future.complete(this);
    } else {
      future.completeExceptionally(error);
    }
  }, threadContext);
  return future;
}
 
Example #10
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 #11
Source File: RaftPerformanceTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Runs a single performance test iteration, returning the iteration run time.
 */
@SuppressWarnings("unchecked")
private long runIteration() throws Exception {
  reset();

  createServers(3);

  CompletableFuture<Void>[] futures = new CompletableFuture[NUM_CLIENTS];
  RaftClient[] clients = new RaftClient[NUM_CLIENTS];
  SessionClient[] proxies = new SessionClient[NUM_CLIENTS];
  for (int i = 0; i < NUM_CLIENTS; i++) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    clients[i] = createClient();
    proxies[i] = createProxy(clients[i]).connect().join();
    futures[i] = future;
  }

  long startTime = System.currentTimeMillis();
  for (int i = 0; i < clients.length; i++) {
    runProxy(proxies[i], futures[i]);
  }
  CompletableFuture.allOf(futures).join();
  long endTime = System.currentTimeMillis();
  long runTime = endTime - startTime;
  System.out.println(String.format("readCount: %d/%d, writeCount: %d/%d, runTime: %dms",
      readCount.get(),
      TOTAL_OPERATIONS,
      writeCount.get(),
      TOTAL_OPERATIONS,
      runTime));
  return runTime;
}
 
Example #12
Source File: RaftPerformanceTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test session.
 */
private SessionClient createProxy(RaftClient client) {
  return client.sessionBuilder("raft-performance-test", TestPrimitiveType.INSTANCE, new ServiceConfig())
      .withReadConsistency(READ_CONSISTENCY)
      .withCommunicationStrategy(COMMUNICATION_STRATEGY)
      .build();
}
 
Example #13
Source File: RaftFuzzTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test session.
 */
private SessionClient createProxy(RaftClient client, ReadConsistency consistency) {
  return client.sessionBuilder("raft-fuzz-test", TestPrimitiveType.INSTANCE, new ServiceConfig())
      .withReadConsistency(consistency)
      .withCommunicationStrategy(COMMUNICATION_STRATEGY)
      .build()
      .connect()
      .join();
}