io.atomix.protocols.raft.partition.RaftPartitionGroup Java Examples

The following examples show how to use io.atomix.protocols.raft.partition.RaftPartitionGroup. 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: 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 #2
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 #3
Source File: AtomixTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoleChangedListener() throws Exception {
  // given
  final CompletableFuture<Void> roleChanged = new CompletableFuture<>();

  final CompletableFuture<Atomix> nodeOneFuture = startAtomix(1, Arrays.asList(1),
      builder -> {
        final RaftPartitionGroup partitionGroup = RaftPartitionGroup.builder("system")
            .withNumPartitions(1)
            .withMembers(String.valueOf(1))
            .withDataDirectory(new File(new File(DATA_DIR, "log"), "1"))
            .build();

        final Atomix atomix = builder.withManagementGroup(partitionGroup).build();

        final DefaultPartitionService partitionService = (DefaultPartitionService) atomix.getPartitionService();
        final RaftPartitionGroup raftPartitionGroup = (RaftPartitionGroup) partitionService.getSystemPartitionGroup();

        // when
        raftPartitionGroup.getPartitions().forEach(
            partition -> {
              final RaftPartition raftPartition = (RaftPartition) partition;
              raftPartition.addRoleChangeListener((role) -> roleChanged.complete(null));
            });
        return atomix;
      });

  // then
  nodeOneFuture.get();
  roleChanged.get();
}
 
Example #4
Source File: AtomixTest.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Test
public void testLogPrimitive() throws Exception {
  CompletableFuture<Atomix> future1 = startAtomix(1, Arrays.asList(1, 2), builder ->
      builder.withManagementGroup(RaftPartitionGroup.builder("system")
          .withNumPartitions(1)
          .withMembers(String.valueOf(1), String.valueOf(2))
          .withDataDirectory(new File(new File(DATA_DIR, "log"), "1"))
          .build())
          .withPartitionGroups(LogPartitionGroup.builder("log")
              .withNumPartitions(3)
              .build())
          .build());

  CompletableFuture<Atomix> future2 = startAtomix(2, Arrays.asList(1, 2), builder ->
      builder.withManagementGroup(RaftPartitionGroup.builder("system")
          .withNumPartitions(1)
          .withMembers(String.valueOf(1), String.valueOf(2))
          .withDataDirectory(new File(new File(DATA_DIR, "log"), "2"))
          .build())
          .withPartitionGroups(LogPartitionGroup.builder("log")
              .withNumPartitions(3)
              .build())
          .build());

  Atomix atomix1 = future1.get();
  Atomix atomix2 = future2.get();

  DistributedLog<String> log1 = atomix1.<String>logBuilder()
      .withProtocol(DistributedLogProtocol.builder()
          .build())
      .build();

  DistributedLog<String> log2 = atomix2.<String>logBuilder()
      .withProtocol(DistributedLogProtocol.builder()
          .build())
      .build();

  assertEquals(3, log1.getPartitions().size());
  assertEquals(1, log1.getPartitions().get(0).id());
  assertEquals(2, log1.getPartitions().get(1).id());
  assertEquals(3, log1.getPartitions().get(2).id());
  assertEquals(1, log2.getPartition(1).id());
  assertEquals(2, log2.getPartition(2).id());
  assertEquals(3, log2.getPartition(3).id());

  DistributedLogPartition<String> partition1 = log1.getPartition("Hello world!");
  DistributedLogPartition<String> partition2 = log2.getPartition("Hello world!");
  assertEquals(partition1.id(), partition2.id());

  CountDownLatch latch = new CountDownLatch(2);
  partition2.consume(record -> {
    assertEquals("Hello world!", record.value());
    latch.countDown();
  });
  log2.consume(record -> {
    assertEquals("Hello world!", record.value());
    latch.countDown();
  });
  partition1.produce("Hello world!");
  latch.await(10, TimeUnit.SECONDS);
  assertEquals(0, latch.getCount());
}
 
Example #5
Source File: AtomixTest.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Test
public void testLogBasedPrimitives() throws Exception {
  CompletableFuture<Atomix> future1 = startAtomix(1, Arrays.asList(1, 2), builder ->
      builder.withManagementGroup(RaftPartitionGroup.builder("system")
          .withNumPartitions(1)
          .withMembers(String.valueOf(1), String.valueOf(2))
          .withDataDirectory(new File(new File(DATA_DIR, "log"), "1"))
          .build())
          .withPartitionGroups(LogPartitionGroup.builder("log")
              .withNumPartitions(3)
              .build())
          .build());

  CompletableFuture<Atomix> future2 = startAtomix(2, Arrays.asList(1, 2), builder ->
      builder.withManagementGroup(RaftPartitionGroup.builder("system")
          .withNumPartitions(1)
          .withMembers(String.valueOf(1), String.valueOf(2))
          .withDataDirectory(new File(new File(DATA_DIR, "log"), "2"))
          .build())
          .withPartitionGroups(LogPartitionGroup.builder("log")
              .withNumPartitions(3)
              .build())
          .build());

  Atomix atomix1 = future1.get();
  Atomix atomix2 = future2.get();

  DistributedMap<String, String> map1 = atomix1.<String, String>mapBuilder("test-map")
      .withProtocol(DistributedLogProtocol.builder().build())
      .build();

  DistributedMap<String, String> map2 = atomix2.<String, String>mapBuilder("test-map")
      .withProtocol(DistributedLogProtocol.builder().build())
      .build();

  CountDownLatch latch = new CountDownLatch(1);
  map2.addListener(event -> {
    map2.async().get("foo").thenAccept(value -> {
      assertEquals("bar", value);
      latch.countDown();
    });
  });
  map1.put("foo", "bar");
  latch.await(10, TimeUnit.SECONDS);
  assertEquals(0, latch.getCount());

  AtomicCounter counter1 = atomix1.atomicCounterBuilder("test-counter")
      .withProtocol(DistributedLogProtocol.builder().build())
      .build();

  AtomicCounter counter2 = atomix2.atomicCounterBuilder("test-counter")
      .withProtocol(DistributedLogProtocol.builder().build())
      .build();

  assertEquals(1, counter1.incrementAndGet());
  assertEquals(1, counter1.get());
  Thread.sleep(1000);
  assertEquals(1, counter2.get());
  assertEquals(2, counter2.incrementAndGet());
}
 
Example #6
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();
    }
}