Java Code Examples for io.atomix.utils.net.Address#from()

The following examples show how to use io.atomix.utils.net.Address#from() . 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 vote down vote up
@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 vote down vote up
@Before
public void setUp() throws Exception {
  address1 = Address.from(findAvailablePort(5001));
  netty1 = (ManagedMessagingService) new NettyMessagingService("test", address1, new MessagingConfig()).start().join();

  address2 = Address.from(findAvailablePort(5002));
  netty2 = (ManagedMessagingService) new NettyMessagingService("test", address2, new MessagingConfig()).start().join();

  addressv11 = Address.from(findAvailablePort(5003));
  nettyv11 = (ManagedMessagingService) new NettyMessagingService("test", addressv11, new MessagingConfig(), ProtocolVersion.V1).start().join();

  addressv12 = Address.from(findAvailablePort(5004));
  nettyv12 = (ManagedMessagingService) new NettyMessagingService("test", addressv12, new MessagingConfig(), ProtocolVersion.V1).start().join();

  addressv21 = Address.from(findAvailablePort(5005));
  nettyv21 = (ManagedMessagingService) new NettyMessagingService("test", addressv21, new MessagingConfig(), ProtocolVersion.V2).start().join();

  addressv22 = Address.from(findAvailablePort(5006));
  nettyv22 = (ManagedMessagingService) new NettyMessagingService("test", addressv22, new MessagingConfig(), ProtocolVersion.V2).start().join();

  invalidAddress = Address.from(IP_STRING, 5007);
}
 
Example 3
Source File: NettyBroadcastServiceTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  localAddress1 = Address.from("127.0.0.1", findAvailablePort(5001));
  localAddress2 = Address.from("127.0.0.1", findAvailablePort(5002));
  groupAddress = Address.from("230.0.0.1", findAvailablePort(1234));

  netty1 = (ManagedBroadcastService) NettyBroadcastService.builder()
      .withLocalAddress(localAddress1)
      .withGroupAddress(groupAddress)
      .build()
      .start()
      .join();

  netty2 = (ManagedBroadcastService) NettyBroadcastService.builder()
      .withLocalAddress(localAddress2)
      .withGroupAddress(groupAddress)
      .build()
      .start()
      .join();
}
 
Example 4
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 5
Source File: RaftFuzzTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: ClusterManagerServer.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public void unicastClusterEvent(String host, int port, String topic, String msg) {
  LOGGER.info("send unicastClusterEvent host:{} port:{} topic:{} message:{}",
      host, port, topic, msg);

  Address address = Address.from(host, port);
  CompletableFuture<byte[]> response = messagingService.sendAndReceive(address,
      topic, msg.getBytes(), Duration.ofSeconds(2));
  response.whenComplete((r, e) -> {
    if (null == e) {
      LOGGER.error(e.getMessage(), e);
    }
  });
}
 
Example 7
Source File: ClusterManagerServer.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public void initTestCluster(String clusterAddrList, String host, int port) {
  isTest = true;
  this.zeplServerHost = 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 8
Source File: AtomixAgent.java    From atomix with Apache License 2.0 5 votes vote down vote up
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 9
Source File: VertxRestService.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public ManagedRestService build() {
  if (address == null) {
    address = Address.from(DEFAULT_HOST, DEFAULT_PORT);
  }
  return new VertxRestService(atomix, address);
}
 
Example 10
Source File: VertxRestServiceTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@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 11
Source File: RaftPerformanceTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the next unique member identifier.
 *
 * @return The next unique member identifier.
 */
private Member nextNode() {
  Address address = Address.from("localhost", ++port);
  Member member = Member.builder(MemberId.from(String.valueOf(++nextId)))
      .withAddress(address)
      .build();
  addressMap.put(member.id(), address);
  return member;
}
 
Example 12
Source File: AtomicMapPerformanceTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * 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 13
Source File: NettyUnicastServiceTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  address1 = Address.from("127.0.0.1", findAvailablePort(5001));
  address2 = Address.from("127.0.0.1", findAvailablePort(5002));

  service1 = new NettyUnicastService(address1, new MessagingConfig());
  service1.start().join();

  service2 = new NettyUnicastService(address2, new MessagingConfig());
  service2.start().join();
}
 
Example 14
Source File: ClusterServer.java    From submarine with Apache License 2.0 5 votes vote down vote up
public void unicastClusterEvent(String host, int port, String topic, String msg) {
  LOG.info("send unicastClusterEvent host:{} port:{} topic:{} message:{}",
      host, port, topic, msg);

  Address address = Address.from(host, port);
  CompletableFuture<byte[]> response = messagingService.sendAndReceive(address,
      topic, msg.getBytes(), Duration.ofSeconds(2));
  response.whenComplete((r, e) -> {
    if (null == e) {
      LOG.error(e.getMessage(), e);
    }
  });
}
 
Example 15
Source File: ClusterManager.java    From submarine with Apache License 2.0 5 votes vote down vote up
protected ClusterManager() {
  try {
    this.serverHost = NetworkUtils.findAvailableHostAddress();
    String clusterAddr = sconf.getClusterAddress();
    LOG.info("clusterAddr = {}", clusterAddr);
    if (!StringUtils.isEmpty(clusterAddr)) {
      String cluster[] = clusterAddr.split(",");

      for (int i = 0; i < cluster.length; i++) {
        String[] parts = cluster[i].split(":");
        String clusterHost = parts[0];
        int clusterPort = Integer.valueOf(parts[1]);
        if (this.serverHost.equalsIgnoreCase(clusterHost)) {
          raftServerPort = clusterPort;
        }

        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));
      }
    }
  } catch (UnknownHostException | SocketException e) {
    LOG.error(e.getMessage(), e);
  }
}
 
Example 16
Source File: NettyMessagingServiceTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendAsyncToUnresolvable() {
  final Address unresolvable = Address.from("unknown.local", address1.port());
  final String subject = nextSubject();
  final CompletableFuture<Void> response = netty1.sendAsync(unresolvable, subject, "hello world".getBytes());
  assertTrue(response.isCompletedExceptionally());
}
 
Example 17
Source File: AtomixTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 18
Source File: TestAtomixFactory.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new Atomix instance.
 *
 * @return a new Atomix instance
 */
public Atomix newInstance() {
  int id = memberId.incrementAndGet();
  return new TestAtomix(
      MemberId.from(String.valueOf(id)),
      Address.from("localhost", 5000 + id),
      messagingServiceFactory,
      unicastServiceFactory,
      broadcastServiceFactory);
}
 
Example 19
Source File: AddressSerializer.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public Address read(Kryo kryo, Input input, Class<Address> type) {
  String host = input.readString();
  int port = input.readInt();
  return Address.from(host, port);
}
 
Example 20
Source File: NodeConfig.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the node address.
 *
 * @return the node address
 */
public Address getAddress() {
  return Address.from(host, port);
}