use of io.atomix.cluster.messaging.MessagingService in project atomix by atomix.
the class RaftPerformanceTest method createClient.
/**
* 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;
}
use of io.atomix.cluster.messaging.MessagingService in project atomix by atomix.
the class RaftFuzzTest method createClient.
/**
* 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;
}
use of io.atomix.cluster.messaging.MessagingService in project zeppelin by apache.
the class ClusterManager method start.
public void start() {
if (!zConf.isClusterMode()) {
return;
}
// RaftClient Thread
new Thread(new Runnable() {
@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(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();
// Cluster Meta Consume Thread
new Thread(new Runnable() {
@Override
public void run() {
try {
while (getRunning().get()) {
ClusterMetaEntity metaEntity = clusterMetaQueue.peek();
if (null != metaEntity) {
// Determine whether the client is connected
int retry = 0;
while (!raftInitialized()) {
retry++;
if (0 == retry % 30) {
LOGGER.warn("Raft incomplete initialization! retry[{}]", retry);
}
Thread.sleep(100);
}
boolean success = false;
switch(metaEntity.getOperation()) {
case DELETE_OPERATION:
success = deleteClusterMeta(metaEntity);
break;
case PUT_OPERATION:
success = putClusterMeta(metaEntity);
break;
}
if (true == success) {
// The operation was successfully deleted
clusterMetaQueue.remove(metaEntity);
LOGGER.info("Cluster Meta Consume success! {}", metaEntity);
} else {
LOGGER.error("Cluster Meta Consume faild!");
}
} else {
Thread.sleep(100);
}
}
} catch (InterruptedException e) {
LOGGER.error(e.getMessage());
}
}
}).start();
}
use of io.atomix.cluster.messaging.MessagingService in project atomix by atomix.
the class DefaultClusterEventServiceTest method testClusterEventService.
@Test
public void testClusterEventService() throws Exception {
TestMessagingServiceFactory messagingServiceFactory = new TestMessagingServiceFactory();
TestUnicastServiceFactory unicastServiceFactory = new TestUnicastServiceFactory();
TestBroadcastServiceFactory broadcastServiceFactory = new TestBroadcastServiceFactory();
Collection<Node> bootstrapLocations = buildBootstrapNodes(3);
Member localMember1 = buildNode(1);
MessagingService messagingService1 = messagingServiceFactory.newMessagingService(localMember1.address()).start().join();
BootstrapService bootstrapService1 = new TestBootstrapService(messagingService1, unicastServiceFactory.newUnicastService(localMember1.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
ManagedClusterMembershipService clusterService1 = new DefaultClusterMembershipService(localMember1, Version.from("1.0.0"), new DefaultNodeDiscoveryService(bootstrapService1, localMember1, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService1, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig()));
ClusterMembershipService clusterMembershipService1 = clusterService1.start().join();
ManagedClusterEventService clusterEventingService1 = new DefaultClusterEventService(clusterMembershipService1, messagingService1);
ClusterEventService eventService1 = clusterEventingService1.start().join();
Member localMember2 = buildNode(2);
MessagingService messagingService2 = messagingServiceFactory.newMessagingService(localMember2.address()).start().join();
BootstrapService bootstrapService2 = new TestBootstrapService(messagingService2, unicastServiceFactory.newUnicastService(localMember2.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
ManagedClusterMembershipService clusterService2 = new DefaultClusterMembershipService(localMember2, Version.from("1.0.0"), new DefaultNodeDiscoveryService(bootstrapService2, localMember2, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService2, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig()));
ClusterMembershipService clusterMembershipService2 = clusterService2.start().join();
ManagedClusterEventService clusterEventingService2 = new DefaultClusterEventService(clusterMembershipService2, messagingService2);
ClusterEventService eventService2 = clusterEventingService2.start().join();
Member localMember3 = buildNode(3);
MessagingService messagingService3 = messagingServiceFactory.newMessagingService(localMember3.address()).start().join();
BootstrapService bootstrapService3 = new TestBootstrapService(messagingService3, unicastServiceFactory.newUnicastService(localMember1.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
ManagedClusterMembershipService clusterService3 = new DefaultClusterMembershipService(localMember3, Version.from("1.0.0"), new DefaultNodeDiscoveryService(bootstrapService3, localMember3, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService3, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig()));
ClusterMembershipService clusterMembershipService3 = clusterService3.start().join();
ManagedClusterEventService clusterEventingService3 = new DefaultClusterEventService(clusterMembershipService3, messagingService3);
ClusterEventService eventService3 = clusterEventingService3.start().join();
Thread.sleep(100);
Set<Integer> events = new CopyOnWriteArraySet<>();
eventService1.<String>subscribe("test1", SERIALIZER::decode, message -> {
assertEquals("Hello world!", message);
events.add(1);
}, MoreExecutors.directExecutor()).join();
eventService2.<String>subscribe("test1", SERIALIZER::decode, message -> {
assertEquals("Hello world!", message);
events.add(2);
}, MoreExecutors.directExecutor()).join();
eventService2.<String>subscribe("test1", SERIALIZER::decode, message -> {
assertEquals("Hello world!", message);
events.add(3);
}, MoreExecutors.directExecutor()).join();
eventService3.broadcast("test1", "Hello world!", SERIALIZER::encode);
Thread.sleep(100);
assertEquals(3, events.size());
events.clear();
eventService3.unicast("test1", "Hello world!");
Thread.sleep(100);
assertEquals(1, events.size());
assertTrue(events.contains(3));
events.clear();
eventService3.unicast("test1", "Hello world!");
Thread.sleep(100);
assertEquals(1, events.size());
assertTrue(events.contains(1));
events.clear();
eventService3.unicast("test1", "Hello world!");
Thread.sleep(100);
assertEquals(1, events.size());
assertTrue(events.contains(2));
events.clear();
eventService3.unicast("test1", "Hello world!");
Thread.sleep(100);
assertEquals(1, events.size());
assertTrue(events.contains(3));
events.clear();
eventService1.<String, String>subscribe("test2", SERIALIZER::decode, message -> {
events.add(1);
return message;
}, SERIALIZER::encode, MoreExecutors.directExecutor()).join();
eventService2.<String, String>subscribe("test2", SERIALIZER::decode, message -> {
events.add(2);
return message;
}, SERIALIZER::encode, MoreExecutors.directExecutor()).join();
assertEquals("Hello world!", eventService3.send("test2", "Hello world!").join());
assertEquals(1, events.size());
assertTrue(events.contains(1));
events.clear();
assertEquals("Hello world!", eventService3.send("test2", "Hello world!").join());
assertEquals(1, events.size());
assertTrue(events.contains(2));
events.clear();
assertEquals("Hello world!", eventService3.send("test2", "Hello world!").join());
assertEquals(1, events.size());
assertTrue(events.contains(1));
CompletableFuture.allOf(new CompletableFuture[] { clusterEventingService1.stop(), clusterEventingService2.stop(), clusterEventingService3.stop() }).join();
CompletableFuture.allOf(new CompletableFuture[] { clusterService1.stop(), clusterService2.stop(), clusterService3.stop() }).join();
}
use of io.atomix.cluster.messaging.MessagingService in project atomix by atomix.
the class RaftFuzzTest method createServer.
/**
* Creates a Raft server.
*/
private RaftServer createServer(RaftMember member) {
RaftServerProtocol protocol;
if (USE_NETTY) {
Address address = Address.from(++port);
MessagingService messagingManager = new NettyMessagingService("test", address, new MessagingConfig()).start().join();
messagingServices.add(messagingManager);
addressMap.put(member.memberId(), address);
protocol = new RaftServerMessagingProtocol(messagingManager, PROTOCOL_SERIALIZER, addressMap::get);
} else {
protocol = protocolFactory.newServerProtocol(member.memberId());
}
RaftServer.Builder builder = RaftServer.builder(member.memberId()).withProtocol(protocol).withStorage(RaftStorage.builder().withStorageLevel(StorageLevel.DISK).withDirectory(new File(String.format("target/fuzz-logs/%s", member.memberId()))).withNamespace(STORAGE_NAMESPACE).withMaxSegmentSize(1024 * 1024).build());
RaftServer server = builder.build();
servers.add(server);
return server;
}
Aggregations