/** * Returns a new Raft client builder. * <p> * The provided set of members will be used to connect to the Raft cluster. The members list does not have to represent * the complete list of servers in the cluster, but it must have at least one reachable member that can communicate with * the cluster's leader. * * @return The client builder. */ @SuppressWarnings("unchecked") static Builder builder() { return builder(Collections.EMPTY_LIST); }
@Override public CompletableFuture<Void> stop() { return client != null ? client.close() : CompletableFuture.completedFuture(null); }
/** * Connects the client to Raft cluster via the default server address. * <p> * If the client was built with a default cluster list, the default server addresses will be used. Otherwise, the client * will attempt to connect to localhost:8700. * <p> * The client will connect to servers in the cluster according to the pattern specified by the configured * {@link CommunicationStrategy}. * * @return A completable future to be completed once the client is registered. */ default CompletableFuture<RaftClient> connect() { return connect((Collection<MemberId>) null); }
/** * 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; }
@Override public RaftSessionClient.Builder sessionBuilder(String primitiveName, PrimitiveType primitiveType, ServiceConfig serviceConfig) { return client.sessionBuilder(primitiveName, primitiveType, serviceConfig); }
/** * Returns the partition leader. * * @return the partition leader */ public MemberId leader() { return client != null ? client.leader() : null; }
/** * Returns the partition term. * * @return the partition term */ public long term() { return client != null ? client.term() : 0; }
@Override public void run() { LOGGER.info("RaftClientThread run() >>>"); int raftClientPort = 0; try { raftClientPort = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces(); } catch (IOException e) { LOGGER.error(e.getMessage()); } MemberId memberId = MemberId.from(ZEPL_CLIENT_ID + zeplServerHost + ":" + raftClientPort); Address address = Address.from(zeplServerHost, raftClientPort); raftAddressMap.put(memberId, address); MessagingService messagingManager = NettyMessagingService.builder().withAddress(address).build().start().join(); RaftClientProtocol protocol = new RaftClientMessagingProtocol( messagingManager, protocolSerializer, raftAddressMap::get); raftClient = RaftClient.builder() .withMemberId(memberId) .withPartitionId(PartitionId.from("partition", 1)) .withProtocol(protocol) .build(); raftClient.connect(clusterMemberIds).join(); raftSessionClient = createProxy(raftClient); LOGGER.info("RaftClientThread run() <<<"); } }).start();
private SessionClient createProxy(RaftClient client) { return client.sessionBuilder(ClusterPrimitiveType.PRIMITIVE_NAME, ClusterPrimitiveType.INSTANCE, new ServiceConfig()) .withReadConsistency(ReadConsistency.SEQUENTIAL) .withCommunicationStrategy(CommunicationStrategy.LEADER) .build() .connect() .join(); }
/** * 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; }
/** * 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(); }
public void shutdown() { if (!zconf.isClusterMode()) { return; } running.set(false); try { if (null != raftSessionClient) { raftSessionClient.close().get(3, TimeUnit.SECONDS); } if (null != raftClient) { raftClient.close().get(3, TimeUnit.SECONDS); } } catch (InterruptedException e) { LOGGER.error(e.getMessage()); } catch (ExecutionException e) { LOGGER.error(e.getMessage()); } catch (TimeoutException e) { LOGGER.error(e.getMessage()); } }
/** * Returns a new Raft client builder. * <p> * The provided set of members will be used to connect to the Raft cluster. The members list does not have to represent * the complete list of servers in the cluster, but it must have at least one reachable member that can communicate with * the cluster's leader. * * @param cluster The cluster to which to connect. * @return The client builder. */ static Builder builder(MemberId... cluster) { return builder(Arrays.asList(cluster)); }
/** * Connects the client to Raft cluster via the provided server addresses. * <p> * The client will connect to servers in the cluster according to the pattern specified by the configured * {@link CommunicationStrategy}. * * @param members A set of server addresses to which to connect. * @return A completable future to be completed once the client is registered. */ default CompletableFuture<RaftClient> connect(MemberId... members) { if (members == null || members.length == 0) { return connect(); } else { return connect(Arrays.asList(members)); } }
/** * 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(); }
clients.forEach(c -> { try { c.close().get(10, TimeUnit.SECONDS); } catch (Exception e) {
private RaftClient newRaftClient(RaftClientProtocol protocol) { return RaftClient.builder() .withClientId(partition.name()) .withPartitionId(partition.id()) .withMemberId(localMemberId) .withProtocol(protocol) .withThreadContextFactory(threadContextFactory) .build(); } }
@Override public CompletableFuture<RaftPartitionClient> start() { synchronized (RaftPartitionClient.this) { client = newRaftClient(protocol); } return client.connect(partition.members()).whenComplete((r, e) -> { if (e == null) { log.debug("Successfully started client for partition {}", partition.id()); } else { log.warn("Failed to start client for partition {}", partition.id(), e); } }).thenApply(v -> null); }
c.close().get(10, TimeUnit.SECONDS); } catch (Exception e) {