/** * 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); }
/** * 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)); } }
@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); }
/** * 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 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();
/** * 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; }