io.atomix.cluster.discovery.BootstrapDiscoveryProvider Java Examples

The following examples show how to use io.atomix.cluster.discovery.BootstrapDiscoveryProvider. 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: YfsConfig.java    From yfs with Apache License 2.0 6 votes vote down vote up
@Bean(name = "gatewayAtomix")
public Atomix getGatewayAtomix() {
    List<Member> ms = gatewayMembers.apply(clusterProperties);
    Atomix atomix = Atomix.builder()
            .withMemberId(clusterProperties.getLocal())
            .withAddress(clusterProperties.getGateway().getIp(), clusterProperties.getGateway().getPort())
            .withMembershipProvider(BootstrapDiscoveryProvider.builder()
                    .withNodes((Collection) ms)
                    .build())
            .withProfiles(Profile.client())
            .withZone(CommonConstant.storeZone)
            .withRack(clusterProperties.getGroup())
            .build();

    atomix.start().join();
    storeInfoMap = atomix.<String, StoreInfo>atomicMapBuilder(CommonConstant.storeInfoMapName)
            .withProtocol(MultiRaftProtocol.builder()
                    .withReadConsistency(ReadConsistency.LINEARIZABLE)
                    .build())
            .withSerializer(CommonConstant.protocolSerializer)
            .build();
    return atomix;
}
 
Example #2
Source File: ClusterConfig.java    From yfs with Apache License 2.0 6 votes vote down vote up
public ClusterConfig() {
    this.clusterProperties = getClusterProperties();
    Member m = gatewayMember.apply(clusterProperties);
    List<Member> ms = gatewayMembers.apply(clusterProperties);
    Atomix atomix = Atomix.builder()
            .withMemberId(clusterProperties.getLocal())
            .withAddress(m.address())
            .withMembershipProvider(BootstrapDiscoveryProvider.builder()
                    .withNodes((Collection) ms)
                    .build())
            .withManagementGroup(gatewayManagementGroup.apply(clusterProperties))
            .withPartitionGroups(gatewayDataGroup.apply(clusterProperties))
            .withZone(CommonConstant.gatewayZone)
            .build();
    atomix.start().join();
    this.atomicMap = atomix.<String, StoreInfo>atomicMapBuilder(CommonConstant.storeInfoMapName)
            .withProtocol(MultiRaftProtocol.builder()
                    .withReadConsistency(ReadConsistency.LINEARIZABLE)
                    .build())
            .withSerializer(CommonConstant.protocolSerializer)
            .build();
    ;
    this.atomix = atomix;
    LOGGER.info("Atomix[{},{}]启动成功", m.id(), m.address().toString());
}
 
Example #3
Source File: AtomicMapPerformanceTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an Atomix client.
 */
private Atomix createClient() {
  Member member = nextNode();

  Atomix atomix = Atomix.builder()
      .withMemberId(member.id())
      .withAddress(member.address())
      .withMembershipProvider(BootstrapDiscoveryProvider.builder()
          .withNodes((Collection) members)
          .build())
      .withProfiles(Profile.client())
      .build();

  atomix.start().join();
  clients.add(atomix);
  return atomix;
}
 
Example #4
Source File: SwimProtocolTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
private SwimMembershipProtocol startProtocol(Member member) {
  SwimMembershipProtocol protocol = new SwimMembershipProtocol(new SwimMembershipProtocolConfig()
      .setFailureTimeout(Duration.ofSeconds(2)));
  TestGroupMembershipEventListener listener = new TestGroupMembershipEventListener();
  listeners.put(member.id(), listener);
  protocol.addListener(listener);
  BootstrapService bootstrap = new TestBootstrapService(
      messagingServiceFactory.newMessagingService(member.address()).start().join(),
      unicastServiceFactory.newUnicastService(member.address()).start().join(),
      broadcastServiceFactory.newBroadcastService().start().join());
  NodeDiscoveryProvider provider = new BootstrapDiscoveryProvider(nodes);
  provider.join(bootstrap, member).join();
  NodeDiscoveryService discovery = new DefaultNodeDiscoveryService(bootstrap, member, provider).start().join();
  protocol.join(bootstrap, discovery, member).join();
  protocols.put(member.id(), protocol);
  return protocol;
}
 
Example #5
Source File: AbstractAtomixTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an Atomix instance.
 */
protected static AtomixBuilder buildAtomix(int id, List<Integer> memberIds, Properties properties) {
  Collection<Node> nodes = memberIds.stream()
      .map(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)
      .withProperties(properties)
      .withMulticastEnabled()
      .withMembershipProvider(!nodes.isEmpty() ? new BootstrapDiscoveryProvider(nodes) : new MulticastDiscoveryProvider());
}
 
Example #6
Source File: SimpleRegistryTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Test
public void testStaticRegistryBuilder() throws Exception {
  AtomixRegistry registry = SimpleRegistry.builder()
      .addProfileType(ConsensusProfile.TYPE)
      .addDiscoveryProviderType(BootstrapDiscoveryProvider.TYPE)
      .addPrimitiveType(AtomicCounterType.instance())
      .addProtocolType(MultiRaftProtocol.TYPE)
      .addPartitionGroupType(RaftPartitionGroup.TYPE)
      .build();

  assertEquals(ConsensusProfile.TYPE, registry.getType(Profile.Type.class, "consensus"));
  assertEquals(BootstrapDiscoveryProvider.TYPE, registry.getType(NodeDiscoveryProvider.Type.class, "bootstrap"));
  assertEquals(AtomicCounterType.instance(), registry.getType(PrimitiveType.class, "atomic-counter"));
  assertEquals(MultiRaftProtocol.TYPE, registry.getType(PrimitiveProtocol.Type.class, "multi-raft"));
  assertEquals(RaftPartitionGroup.TYPE, registry.getType(PartitionGroup.Type.class, "raft"));
}
 
Example #7
Source File: Cluster1Test.java    From yfs with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    List<Node> members = Lists.newArrayList();
    members.add(Member.builder().withId("gateway1").withAddress("127.0.0.1", 6001).build());
    members.add(Member.builder().withId("gateway2").withAddress("127.0.0.1", 6002).build());
    members.add(Member.builder().withId("gateway3").withAddress("127.0.0.1", 6003).build());

    Member member = Member.builder().withId("store").withAddress("localhost", 7001).build();
    Atomix atomix = Atomix.builder()
            .withMemberId(member.id())
            .withAddress(member.address())
            .withMembershipProvider(BootstrapDiscoveryProvider.builder()
                    .withNodes((Collection) members)
                    .build())
            .withProfiles(Profile.client())
            .build();

    atomix.start().join();


    AtomicMap<String, String> a = atomix.getAtomicMap("store-info");

    a.put("a", "test");
    try {
        Thread.sleep(1000 * 60 * 60 * 24);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}
 
Example #8
Source File: AtomixVertxTestHelper.java    From atomix-vertx with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #9
Source File: VertxRestServiceTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
protected Atomix buildAtomix(int memberId) {
  return Atomix.builder()
      .withClusterId("test")
      .withMemberId(String.valueOf(memberId))
      .withHost("localhost")
      .withPort(5000 + memberId)
      .withMulticastEnabled()
      .withMembershipProvider(new BootstrapDiscoveryProvider(
          Node.builder()
              .withId("1")
              .withHost("localhost")
              .withPort(5001)
              .build(),
          Node.builder()
              .withId("2")
              .withHost("localhost")
              .withPort(5002)
              .build(),
          Node.builder()
              .withId("3")
              .withHost("localhost")
              .withPort(5003)
              .build()))
      .withManagementGroup(PrimaryBackupPartitionGroup.builder("system")
          .withNumPartitions(1)
          .build())
      .addPartitionGroup(PrimaryBackupPartitionGroup.builder("data")
          .withNumPartitions(3)
          .build())
      .build();
}
 
Example #10
Source File: AtomicMapPerformanceTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an Atomix server node.
 */
private Atomix createServer(Member member, List<Node> members) {
  Atomix atomix = Atomix.builder()
      .withMemberId(member.id())
      .withAddress(member.address())
      .withMembershipProvider(BootstrapDiscoveryProvider.builder()
          .withNodes(members)
          .build())
      .withManagementGroup(managementGroup.apply(member))
      .withPartitionGroups(dataGroup.apply(member))
      .build();
  servers.add(atomix);
  return atomix;
}
 
Example #11
Source File: AtomixCluster.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a member location provider.
 */
@SuppressWarnings("unchecked")
protected static NodeDiscoveryProvider buildLocationProvider(ClusterConfig config) {
  NodeDiscoveryConfig discoveryProviderConfig = config.getDiscoveryConfig();
  if (discoveryProviderConfig != null) {
    return discoveryProviderConfig.getType().newProvider(discoveryProviderConfig);
  }
  if (config.getMulticastConfig().isEnabled()) {
    return new MulticastDiscoveryProvider(new MulticastDiscoveryConfig());
  } else {
    return new BootstrapDiscoveryProvider(Collections.emptyList());
  }
}
 
Example #12
Source File: RaftPerformanceTest.java    From atomix with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a Raft server.
 */
private RaftServer createServer(Member member, List<Node> members) {
  RaftServerProtocol protocol;
  ManagedMessagingService messagingService;
  if (USE_NETTY) {
    messagingService = (ManagedMessagingService) new NettyMessagingService("test", member.address(), new MessagingConfig())
        .start()
        .join();
    messagingServices.add(messagingService);
    protocol = new RaftServerMessagingProtocol(messagingService, PROTOCOL_SERIALIZER, addressMap::get);
  } else {
    protocol = protocolFactory.newServerProtocol(member.id());
  }

  BootstrapService bootstrapService = new BootstrapService() {
    @Override
    public MessagingService getMessagingService() {
      return messagingService;
    }

    @Override
    public UnicastService getUnicastService() {
      return new UnicastServiceAdapter();
    }

    @Override
    public BroadcastService getBroadcastService() {
      return new BroadcastServiceAdapter();
    }
  };

  RaftServer.Builder builder = RaftServer.builder(member.id())
      .withProtocol(protocol)
      .withThreadModel(ThreadModel.SHARED_THREAD_POOL)
      .withMembershipService(new DefaultClusterMembershipService(
          member,
          Version.from("1.0.0"),
          new DefaultNodeDiscoveryService(bootstrapService, member, new BootstrapDiscoveryProvider(members)),
          bootstrapService,
          new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig())))
      .withStorage(RaftStorage.builder()
          .withStorageLevel(StorageLevel.DISK)
          .withDirectory(new File(String.format("target/perf-logs/%s", member.id())))
          .withNamespace(STORAGE_NAMESPACE)
          .withMaxSegmentSize(1024 * 1024 * 64)
          .withDynamicCompaction()
          .withFlushOnCommit(false)
          .build());

  RaftServer server = builder.build();
  servers.add(server);
  return server;
}
 
Example #13
Source File: AtomixClusterTest.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Test
public void testBootstrap() throws Exception {
  Collection<Node> bootstrapLocations = Arrays.asList(
      Node.builder().withId("foo").withAddress(Address.from("localhost:5000")).build(),
      Node.builder().withId("bar").withAddress(Address.from("localhost:5001")).build(),
      Node.builder().withId("baz").withAddress(Address.from("localhost:5002")).build());

  AtomixCluster cluster1 = AtomixCluster.builder()
      .withMemberId("foo")
      .withHost("localhost")
      .withPort(5000)
      .withMembershipProvider(BootstrapDiscoveryProvider.builder()
          .withNodes(bootstrapLocations)
          .build())
      .build();
  cluster1.start().join();

  assertEquals("foo", cluster1.getMembershipService().getLocalMember().id().id());

  AtomixCluster cluster2 = AtomixCluster.builder()
      .withMemberId("bar")
      .withHost("localhost")
      .withPort(5001)
      .withMembershipProvider(BootstrapDiscoveryProvider.builder()
          .withNodes(bootstrapLocations)
          .build())
      .build();
  cluster2.start().join();

  assertEquals("bar", cluster2.getMembershipService().getLocalMember().id().id());

  AtomixCluster cluster3 = AtomixCluster.builder()
      .withMemberId("baz")
      .withHost("localhost")
      .withPort(5002)
      .withMembershipProvider(BootstrapDiscoveryProvider.builder()
          .withNodes(bootstrapLocations)
          .build())
      .build();
  cluster3.start().join();

  assertEquals("baz", cluster3.getMembershipService().getLocalMember().id().id());

  List<CompletableFuture<Void>> futures = Stream.of(cluster1, cluster2, cluster3).map(AtomixCluster::stop)
      .collect(Collectors.toList());
  try {
    CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).join();
  } catch (Exception e) {
    // Do nothing
  }
}
 
Example #14
Source File: AtomixManager.java    From onos with Apache License 2.0 4 votes vote down vote up
private Atomix createAtomix() {
    ClusterMetadata metadata = metadataService.getClusterMetadata();

    // If a storage DNS service was provided, use the DNS service for service discovery.
    // Otherwise, use a static list of storage nodes.
    NodeDiscoveryProvider discovery;
    if (metadata.getStorageDnsService() != null) {
        discovery = DnsDiscoveryProvider.builder()
            .withService(metadata.getStorageDnsService())
            .build();
    } else {
        discovery = BootstrapDiscoveryProvider.builder()
            .withNodes(metadata.getStorageNodes().stream()
                .map(node -> Node.builder()
                    .withId(node.id().id())
                    .withHost(node.host())
                    .withPort(node.tcpPort())
                    .build())
                .collect(Collectors.toList()))
            .build();
    }

    if (!metadata.getStorageNodes().isEmpty()) {
        // If storage nodes are defined, construct an instance that connects to them for service discovery.
        return Atomix.builder(getClass().getClassLoader())
            .withClusterId(metadata.getName())
            .withMemberId(metadataService.getLocalNode().id().id())
            .withHost(metadata.getLocalNode().host())
            .withPort(metadata.getLocalNode().tcpPort())
            .withProperty("type", "onos")
            .withMembershipProvider(discovery)
            .build();
    } else {
        log.warn("No storage nodes found in cluster metadata!");
        log.warn("Bootstrapping ONOS cluster in test mode! For production use, configure external storage nodes.");

        // If storage nodes are not defined, construct a local instance with a Raft partition group.
        List<String> raftMembers = !metadata.getControllerNodes().isEmpty()
            ? metadata.getControllerNodes()
            .stream()
            .map(node -> node.id().id())
            .collect(Collectors.toList())
            : Collections.singletonList(metadataService.getLocalNode().id().id());
        return Atomix.builder(getClass().getClassLoader())
            .withClusterId(metadata.getName())
            .withMemberId(metadataService.getLocalNode().id().id())
            .withHost(metadata.getLocalNode().host())
            .withPort(metadata.getLocalNode().tcpPort())
            .withProperty("type", "onos")
            .withMembershipProvider(discovery)
            .withManagementGroup(RaftPartitionGroup.builder("system")
                .withNumPartitions(1)
                .withDataDirectory(new File(LOCAL_DATA_DIR, "system"))
                .withMembers(raftMembers)
                .build())
            .addPartitionGroup(RaftPartitionGroup.builder("raft")
                .withNumPartitions(raftMembers.size())
                .withDataDirectory(new File(LOCAL_DATA_DIR, "data"))
                .withMembers(raftMembers)
                .build())
            .build();
    }
}