io.atomix.utils.net.Address Java Examples
The following examples show how to use
io.atomix.utils.net.Address.
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: ClusterServer.java From submarine with Apache License 2.0 | 6 votes |
@VisibleForTesting void initTestCluster(String clusterAddrList, String host, int port) { isTest = true; this.serverHost = host; this.raftServerPort = port; // clear clusterNodes.clear(); raftAddressMap.clear(); clusterMemberIds.clear(); String cluster[] = clusterAddrList.split(","); for (int i = 0; i < cluster.length; i++) { String[] parts = cluster[i].split(":"); String clusterHost = parts[0]; int clusterPort = Integer.valueOf(parts[1]); String memberId = clusterHost + ":" + clusterPort; Address address = Address.from(clusterHost, clusterPort); Node node = Node.builder().withId(memberId).withAddress(address).build(); clusterNodes.add(node); raftAddressMap.put(MemberId.from(memberId), address); clusterMemberIds.add(MemberId.from(memberId)); } }
Example #2
Source File: NettyMessagingServiceTest.java From atomix with Apache License 2.0 | 6 votes |
@Test public void testTransientSendAndReceive() { String subject = nextSubject(); AtomicBoolean handlerInvoked = new AtomicBoolean(false); AtomicReference<byte[]> request = new AtomicReference<>(); AtomicReference<Address> sender = new AtomicReference<>(); BiFunction<Address, byte[], byte[]> handler = (ep, data) -> { handlerInvoked.set(true); sender.set(ep); request.set(data); return "hello there".getBytes(); }; netty2.registerHandler(subject, handler, MoreExecutors.directExecutor()); CompletableFuture<byte[]> response = netty1.sendAndReceive(address2, subject, "hello world".getBytes(), false); assertTrue(Arrays.equals("hello there".getBytes(), response.join())); assertTrue(handlerInvoked.get()); assertTrue(Arrays.equals(request.get(), "hello world".getBytes())); assertEquals(address1.address(), sender.get().address()); }
Example #3
Source File: NettyMessagingServiceTest.java From atomix with Apache License 2.0 | 6 votes |
@Test public void testSendAndReceive() { String subject = nextSubject(); AtomicBoolean handlerInvoked = new AtomicBoolean(false); AtomicReference<byte[]> request = new AtomicReference<>(); AtomicReference<Address> sender = new AtomicReference<>(); BiFunction<Address, byte[], byte[]> handler = (ep, data) -> { handlerInvoked.set(true); sender.set(ep); request.set(data); return "hello there".getBytes(); }; netty2.registerHandler(subject, handler, MoreExecutors.directExecutor()); CompletableFuture<byte[]> response = netty1.sendAndReceive(address2, subject, "hello world".getBytes()); assertTrue(Arrays.equals("hello there".getBytes(), response.join())); assertTrue(handlerInvoked.get()); assertTrue(Arrays.equals(request.get(), "hello world".getBytes())); assertEquals(address1.address(), sender.get().address()); }
Example #4
Source File: HeartbeatMembershipProtocol.java From atomix with Apache License 2.0 | 5 votes |
/** * Handles a heartbeat message. */ private byte[] handleHeartbeat(Address address, byte[] message) { GossipMember remoteMember = SERIALIZER.decode(message); LOGGER.trace("{} - Received heartbeat: {}", localMember.id(), remoteMember); failureDetectors.computeIfAbsent(remoteMember.id(), n -> new PhiAccrualFailureDetector()).report(); updateMember(remoteMember, true); // Return only reachable members to avoid populating removed members on remote nodes from unreachable members. return SERIALIZER.encode(Lists.newArrayList(members.values() .stream() .filter(member -> member.isReachable()) .collect(Collectors.toList()))); }
Example #5
Source File: AtomixVertxTestHelper.java From atomix-vertx with Apache License 2.0 | 5 votes |
/** * Creates an Atomix instance. */ private Atomix createAtomix(int id, int... ids) { Collection<Node> nodes = IntStream.of(ids) .mapToObj(memberId -> Node.builder() .withId(String.valueOf(memberId)) .withAddress(Address.from("localhost", BASE_PORT + memberId)) .build()) .collect(Collectors.toList()); return Atomix.builder() .withClusterId("test") .withMemberId(String.valueOf(id)) .withHost("localhost") .withPort(BASE_PORT + id) .withMembershipProtocol(SwimMembershipProtocol.builder() .withBroadcastDisputes(true) .withBroadcastUpdates(true) .withProbeInterval(Duration.ofMillis(100)) .withNotifySuspect(true) .withFailureTimeout(Duration.ofSeconds(3)) .build()) .withMembershipProvider(new BootstrapDiscoveryProvider(nodes)) .withManagementGroup(RaftPartitionGroup.builder("system") .withNumPartitions(1) .withPartitionSize(ids.length) .withMembers(nodes.stream().map(node -> node.id().id()).collect(Collectors.toSet())) .withDataDirectory(new File("target/test-logs/" + id + "/system")) .build()) .withPartitionGroups(RaftPartitionGroup.builder("test") .withNumPartitions(3) .withPartitionSize(ids.length) .withMembers(nodes.stream().map(node -> node.id().id()).collect(Collectors.toSet())) .withDataDirectory(new File("target/test-logs/" + id + "/test")) .build()) .build(); }
Example #6
Source File: DefaultClusterCommunicationService.java From atomix with Apache License 2.0 | 5 votes |
@Override public <M> CompletableFuture<Void> subscribe(String subject, Function<byte[], M> decoder, Consumer<M> handler, Executor executor) { messagingService.registerHandler(subject, new InternalMessageConsumer<>(decoder, handler), executor); BiConsumer<Address, byte[]> unicastConsumer = new InternalMessageConsumer<>(decoder, handler); unicastConsumers.put(subject, unicastConsumer); unicastService.addListener(subject, unicastConsumer, executor); return CompletableFuture.completedFuture(null); }
Example #7
Source File: TestMessagingService.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<Void> sendAsync(Address address, String type, byte[] payload, boolean keepAlive) { if (isPartitioned(address)) { return Futures.exceptionalFuture(new ConnectException()); } return getHandler(address, type).apply(this.address, payload).thenApply(v -> null); }
Example #8
Source File: RaftMessagingProtocol.java From zeppelin with Apache License 2.0 | 5 votes |
public RaftMessagingProtocol(MessagingService messagingService, Serializer serializer, Function<MemberId, Address> addressProvider) { this.messagingService = messagingService; this.serializer = serializer; this.addressProvider = addressProvider; }
Example #9
Source File: AtomixAgent.java From atomix with Apache License 2.0 | 5 votes |
static Address parseAddress(String address) { int startIndex = address.indexOf('@'); if (startIndex == -1) { try { return Address.from(address); } catch (MalformedAddressException e) { return Address.local(); } } else { return Address.from(address.substring(startIndex + 1)); } }
Example #10
Source File: TestMessagingService.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<byte[]> sendAndReceive(Address address, String type, byte[] payload, boolean keepAlive, Duration timeout, Executor executor) { if (isPartitioned(address)) { return Futures.exceptionalFuture(new ConnectException()); } ComposableFuture<byte[]> future = new ComposableFuture<>(); sendAndReceive(address, type, payload).whenCompleteAsync(future, executor); return future; }
Example #11
Source File: NettyMessagingServiceTest.java From atomix with Apache License 2.0 | 5 votes |
@Test @Ignore public void testSendAndReceiveWithExecutor() { String subject = nextSubject(); ExecutorService completionExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, "completion-thread")); ExecutorService handlerExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, "handler-thread")); AtomicReference<String> handlerThreadName = new AtomicReference<>(); AtomicReference<String> completionThreadName = new AtomicReference<>(); final CountDownLatch latch = new CountDownLatch(1); BiFunction<Address, byte[], byte[]> handler = (ep, data) -> { handlerThreadName.set(Thread.currentThread().getName()); try { latch.await(); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); fail("InterruptedException"); } return "hello there".getBytes(); }; netty2.registerHandler(subject, handler, handlerExecutor); CompletableFuture<byte[]> response = netty1.sendAndReceive(address2, subject, "hello world".getBytes(), completionExecutor); response.whenComplete((r, e) -> { completionThreadName.set(Thread.currentThread().getName()); }); latch.countDown(); // Verify that the message was request handling and response completion happens on the correct thread. assertTrue(Arrays.equals("hello there".getBytes(), response.join())); assertEquals("completion-thread", completionThreadName.get()); assertEquals("handler-thread", handlerThreadName.get()); }
Example #12
Source File: NettyMessagingService.java From atomix with Apache License 2.0 | 5 votes |
@Override public void registerHandler(String type, BiFunction<Address, byte[], byte[]> handler, Executor executor) { handlers.register(type, (message, connection) -> executor.execute(() -> { byte[] responsePayload = null; ProtocolReply.Status status = ProtocolReply.Status.OK; try { responsePayload = handler.apply(message.sender(), message.payload()); } catch (Exception e) { log.warn("An error occurred in a message handler: {}", e); status = ProtocolReply.Status.ERROR_HANDLER_EXCEPTION; } connection.reply(message, status, Optional.ofNullable(responsePayload)); })); }
Example #13
Source File: VertxRestServiceTest.java From atomix with Apache License 2.0 | 5 votes |
@Before public void beforeTest() throws Exception { deleteData(); List<CompletableFuture<Atomix>> instanceFutures = new ArrayList<>(3); instances = new ArrayList<>(3); for (int i = 1; i <= 3; i++) { Atomix atomix = buildAtomix(i); instanceFutures.add(atomix.start().thenApply(v -> atomix)); instances.add(atomix); } CompletableFuture.allOf(instanceFutures.toArray(new CompletableFuture[instanceFutures.size()])).get(30, TimeUnit.SECONDS); List<CompletableFuture<RestService>> serviceFutures = new ArrayList<>(3); services = new ArrayList<>(3); for (int i = 0; i < 3; i++) { ManagedRestService restService = new VertxRestService(instances.get(i), Address.from("localhost", findAvailablePort(BASE_PORT))); serviceFutures.add(restService.start()); services.add(restService); } CompletableFuture.allOf(serviceFutures.toArray(new CompletableFuture[serviceFutures.size()])).get(30, TimeUnit.SECONDS); specs = new ArrayList<>(3); for (int i = 0; i < 3; i++) { RequestSpecification spec = new RequestSpecBuilder() .setContentType(ContentType.TEXT) .setBaseUri(String.format("http://%s/v1/", services.get(i).address().toString())) .addFilter(new ResponseLoggingFilter()) .addFilter(new RequestLoggingFilter()) .build(); specs.add(spec); } }
Example #14
Source File: TestMessagingService.java From atomix with Apache License 2.0 | 5 votes |
/** * Returns the given handler for the given address. */ private BiFunction<Address, byte[], CompletableFuture<byte[]>> getHandler(Address address, String type) { TestMessagingService service = getService(address); if (service == null) { return (e, p) -> Futures.exceptionalFuture(new NoRemoteHandler()); } BiFunction<Address, byte[], CompletableFuture<byte[]>> handler = service.handlers.get(checkNotNull(type)); if (handler == null) { return (e, p) -> Futures.exceptionalFuture(new NoRemoteHandler()); } return handler; }
Example #15
Source File: TestMessagingService.java From atomix with Apache License 2.0 | 5 votes |
@Override public void registerHandler(String type, BiFunction<Address, byte[], byte[]> handler, Executor executor) { checkNotNull(type); checkNotNull(handler); handlers.put(type, (e, p) -> { CompletableFuture<byte[]> future = new CompletableFuture<>(); try { executor.execute(() -> future.complete(handler.apply(e, p))); } catch (RejectedExecutionException e2) { future.completeExceptionally(e2); } return future; }); }
Example #16
Source File: AtomicMapPerformanceTest.java From atomix with Apache License 2.0 | 5 votes |
/** * Returns the next unique member identifier. * * @return The next unique member identifier. */ private Member nextNode() { Address address = Address.from("localhost", ++port); return Member.builder(MemberId.from(String.valueOf(++nextId))) .withAddress(address) .build(); }
Example #17
Source File: TestMessagingServiceFactory.java From atomix with Apache License 2.0 | 5 votes |
/** * Heals a partition of the service at the given address. * * @param address the address of the service to heal */ public void heal(Address address) { TestMessagingService service = services.get(address); services.values().stream() .filter(s -> !s.address().equals(address)) .forEach(s -> { service.heal(s.address()); s.heal(service.address()); }); }
Example #18
Source File: TestMessagingService.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<byte[]> sendAndReceive(Address address, String type, byte[] payload, boolean keepAlive, Executor executor) { if (isPartitioned(address)) { return Futures.exceptionalFuture(new ConnectException()); } ComposableFuture<byte[]> future = new ComposableFuture<>(); sendAndReceive(address, type, payload).whenCompleteAsync(future, executor); return future; }
Example #19
Source File: DefaultClusterCommunicationService.java From atomix with Apache License 2.0 | 5 votes |
@Override public <M> CompletableFuture<Void> subscribe(String subject, Function<byte[], M> decoder, BiConsumer<MemberId, M> handler, Executor executor) { messagingService.registerHandler(subject, new InternalMessageBiConsumer<>(decoder, handler), executor); BiConsumer<Address, byte[]> unicastConsumer = new InternalMessageBiConsumer<>(decoder, handler); unicastConsumers.put(subject, unicastConsumer); unicastService.addListener(subject, unicastConsumer, executor); return CompletableFuture.completedFuture(null); }
Example #20
Source File: TestMessagingService.java From atomix with Apache License 2.0 | 5 votes |
@Override public void registerHandler(String type, BiFunction<Address, byte[], byte[]> handler, Executor executor) { checkNotNull(type); checkNotNull(handler); handlers.put(type, (e, p) -> { CompletableFuture<byte[]> future = new CompletableFuture<>(); try { executor.execute(() -> future.complete(handler.apply(e, p))); } catch (RejectedExecutionException e2) { future.completeExceptionally(e2); } return future; }); }
Example #21
Source File: BootstrapDiscoveryBuilder.java From atomix with Apache License 2.0 | 5 votes |
/** * Sets the bootstrap nodes. * * @param nodes the bootstrap nodes * @return the location provider builder */ public BootstrapDiscoveryBuilder withNodes(Address... nodes) { return withNodes(Stream.of(nodes) .map(address -> Node.builder() .withAddress(address) .build()) .collect(Collectors.toSet())); }
Example #22
Source File: NodeConfig.java From atomix with Apache License 2.0 | 5 votes |
/** * Sets the node address. * * @param address the node address * @return the node configuration */ @Deprecated public NodeConfig setAddress(Address address) { this.host = address.host(); this.port = address.port(); return this; }
Example #23
Source File: TestUnicastServiceFactory.java From atomix with Apache License 2.0 | 5 votes |
/** * Heals a partition of the service at the given address. * * @param address the address of the service to heal */ public void heal(Address address) { TestUnicastService service = services.get(address); services.values().stream() .filter(s -> !s.address().equals(address)) .forEach(s -> { service.heal(s.address()); s.heal(service.address()); }); }
Example #24
Source File: TestMessagingService.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<byte[]> sendAndReceive(Address address, String type, byte[] payload, boolean keepAlive, Duration timeout) { if (isPartitioned(address)) { return Futures.exceptionalFuture(new ConnectException()); } return getHandler(address, type).apply(this.address, payload); }
Example #25
Source File: TestMessagingServiceFactory.java From atomix with Apache License 2.0 | 5 votes |
/** * Creates a bi-directional partition between two services. * * @param address1 the first service * @param address2 the second service */ public void partition(Address address1, Address address2) { TestMessagingService service1 = services.get(address1); TestMessagingService service2 = services.get(address2); service1.partition(service2.address()); service2.partition(service1.address()); }
Example #26
Source File: TestUnicastServiceFactory.java From atomix with Apache License 2.0 | 5 votes |
/** * Partitions the service at the given address. * * @param address the address of the service to partition */ public void partition(Address address) { TestUnicastService service = services.get(address); services.values().stream() .filter(s -> !s.address().equals(address)) .forEach(s -> { service.partition(s.address()); s.partition(service.address()); }); }
Example #27
Source File: DefaultClusterCommunicationService.java From atomix with Apache License 2.0 | 5 votes |
@Override public void unsubscribe(String subject) { messagingService.unregisterHandler(subject); BiConsumer<Address, byte[]> consumer = unicastConsumers.get(subject); if (consumer != null) { unicastService.removeListener(subject, consumer); } }
Example #28
Source File: NettyMessagingService.java From atomix with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<byte[]> sendAndReceive(Address address, String type, byte[] payload, boolean keepAlive, Duration timeout, Executor executor) { long messageId = messageIdGenerator.incrementAndGet(); ProtocolRequest message = new ProtocolRequest( messageId, returnAddress, type, payload); if (keepAlive) { return executeOnPooledConnection(address, type, c -> c.sendAndReceive(message, timeout), executor); } else { return executeOnTransientConnection(address, c -> c.sendAndReceive(message, timeout), executor); } }
Example #29
Source File: AtomixCluster.java From atomix with Apache License 2.0 | 5 votes |
/** * Builds a default broadcast service. */ protected static ManagedBroadcastService buildBroadcastService(ClusterConfig config) { return NettyBroadcastService.builder() .withLocalAddress(config.getNodeConfig().getAddress()) .withGroupAddress(new Address( config.getMulticastConfig().getGroup().getHostAddress(), config.getMulticastConfig().getPort(), config.getMulticastConfig().getGroup())) .withEnabled(config.getMulticastConfig().isEnabled()) .build(); }
Example #30
Source File: AtomixTest.java From atomix with Apache License 2.0 | 5 votes |
@Test public void testDiscoverData() throws Exception { Address multicastAddress = Address.from("230.0.0.1", findAvailablePort(1234)); Atomix atomix1 = startAtomix(1, Arrays.asList(), builder -> builder.withProfiles(Profile.dataGrid()) .withMulticastEnabled() .withMulticastAddress(multicastAddress) .build()) .get(30, TimeUnit.SECONDS); Atomix atomix2 = startAtomix(2, Arrays.asList(), builder -> builder.withProfiles(Profile.dataGrid()) .withMulticastEnabled() .withMulticastAddress(multicastAddress) .build()) .get(30, TimeUnit.SECONDS); Atomix atomix3 = startAtomix(3, Arrays.asList(), builder -> builder.withProfiles(Profile.dataGrid()) .withMulticastEnabled() .withMulticastAddress(multicastAddress) .build()) .get(30, TimeUnit.SECONDS); Thread.sleep(5000); assertEquals(3, atomix1.getMembershipService().getMembers().size()); assertEquals(3, atomix2.getMembershipService().getMembers().size()); assertEquals(3, atomix3.getMembershipService().getMembers().size()); }