io.atomix.cluster.Member Java Examples

The following examples show how to use io.atomix.cluster.Member. 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: AtomixClusterStore.java    From onos with Apache License 2.0 6 votes vote down vote up
private void updateState(ControllerNode node, Member member) {
    String state = member.properties().getProperty(STATE_KEY);
    if (state == null || !state.equals(ControllerNode.State.READY.name())) {
        if (states.put(node.id(), ControllerNode.State.ACTIVE) != ControllerNode.State.ACTIVE) {
            log.info("Updated node {} state to {}", node.id(), ControllerNode.State.ACTIVE);
            markUpdated(node.id());
            notifyDelegate(new ClusterEvent(ClusterEvent.Type.INSTANCE_ACTIVATED, node));
        }
    } else {
        if (states.put(node.id(), ControllerNode.State.READY) != ControllerNode.State.READY) {
            log.info("Updated node {} state to {}", node.id(), ControllerNode.State.READY);
            markUpdated(node.id());
            notifyDelegate(new ClusterEvent(ClusterEvent.Type.INSTANCE_READY, node));
        }
    }
}
 
Example #2
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 #3
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 #4
Source File: RaftPerformanceTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a set of Raft servers.
 */
private List<RaftServer> createServers(int nodes) throws Exception {
  List<RaftServer> servers = new ArrayList<>();

  for (int i = 0; i < nodes; i++) {
    members.add(nextNode());
  }

  CountDownLatch latch = new CountDownLatch(nodes);
  for (int i = 0; i < nodes; i++) {
    RaftServer server = createServer(members.get(i), Lists.newArrayList(members));
    server.bootstrap(members.stream().map(Member::id).collect(Collectors.toList())).thenRun(latch::countDown);
    servers.add(server);
  }

  latch.await(30000, TimeUnit.MILLISECONDS);

  return servers;
}
 
Example #5
Source File: RaftPerformanceTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #6
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 #7
Source File: HeartbeatMembershipProtocol.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> leave(Member member) {
  if (started.compareAndSet(true, false)) {
    discoveryService.removeListener(discoveryEventListener);
    heartbeatFuture.cancel(true);
    heartbeatScheduler.shutdownNow();
    eventExecutor.shutdownNow();
    LOGGER.info("{} - Member deactivated: {}", localMember.id(), localMember);
    localMember.setActive(false);
    localMember.setReachable(false);
    members.clear();
    bootstrapService.getMessagingService().unregisterHandler(HEARTBEAT_MESSAGE);
    LOGGER.info("Stopped");
  }
  return CompletableFuture.completedFuture(null);
}
 
Example #8
Source File: SwimMembershipProtocol.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Sends probes to all members or to the next member in round robin fashion.
 */
private void probe() {
  // First get a sorted list of discovery service nodes that are not present in the SWIM members.
  // This is necessary to ensure we attempt to probe all nodes that are provided by the discovery provider.
  List<SwimMember> probeMembers = Lists.newArrayList(discoveryService.getNodes().stream()
      .map(node -> new SwimMember(MemberId.from(node.id().id()), node.address()))
      .filter(member -> !members.containsKey(member.id()) || members.get(member.id()).getState() == State.DEAD)
      .filter(member -> !member.id().equals(localMember.id()))
      .sorted(Comparator.comparing(Member::id))
      .collect(Collectors.toList()));

  // Then add the randomly sorted list of SWIM members.
  probeMembers.addAll(randomMembers);

  // If there are members to probe, select the next member to probe using a counter for round robin probes.
  if (!probeMembers.isEmpty()) {
    SwimMember probeMember = probeMembers.get(Math.abs(probeCounter.incrementAndGet() % probeMembers.size()));
    probe(probeMember.copy());
  }
}
 
Example #9
Source File: SwimMembershipProtocol.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> leave(Member member) {
  if (started.compareAndSet(true, false)) {
    discoveryService.removeListener(discoveryEventListener);
    gossipFuture.cancel(false);
    probeFuture.cancel(false);
    swimScheduler.shutdownNow();
    eventExecutor.shutdownNow();
    LOGGER.info("{} - Member deactivated: {}", localMember.id(), localMember);
    localMember.setState(State.DEAD);
    members.clear();
    unregisterHandlers();
    LOGGER.info("Stopped");
  }
  return CompletableFuture.completedFuture(null);
}
 
Example #10
Source File: DefaultClusterEventService.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Sends an update to the given node.
 *
 * @param member the node to which to send the update
 */
private CompletableFuture<Void> updateNode(Member member) {
  long updateTime = System.currentTimeMillis();
  long lastUpdateTime = updateTimes.getOrDefault(member.id(), 0L);

  Collection<InternalSubscriptionInfo> subscriptions = topics.values()
      .stream()
      .flatMap(t -> t.remoteSubscriptions().stream().filter(subscriber -> subscriber.timestamp().unixTimestamp() >= lastUpdateTime))
      .collect(Collectors.toList());

  CompletableFuture<Void> future = new CompletableFuture<>();
  messagingService.sendAndReceive(member.address(), GOSSIP_MESSAGE_SUBJECT, SERIALIZER.encode(subscriptions))
      .whenComplete((result, error) -> {
        if (error == null) {
          updateTimes.put(member.id(), updateTime);
        }
        future.complete(null);
      });
  return future;
}
 
Example #11
Source File: DefaultClusterMembershipService.java    From atomix with Apache License 2.0 6 votes vote down vote up
public DefaultClusterMembershipService(
    Member localMember,
    Version version,
    ManagedNodeDiscoveryService discoveryService,
    BootstrapService bootstrapService,
    GroupMembershipProtocol protocol) {
  this.discoveryService = checkNotNull(discoveryService, "discoveryService cannot be null");
  this.bootstrapService = checkNotNull(bootstrapService, "bootstrapService cannot be null");
  this.protocol = checkNotNull(protocol, "protocol cannot be null");
  this.localMember = new StatefulMember(
      localMember.id(),
      localMember.address(),
      localMember.zone(),
      localMember.rack(),
      localMember.host(),
      localMember.properties(),
      version);
}
 
Example #12
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 #13
Source File: ClusterResource.java    From atomix with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/nodes/{node}")
@Produces(MediaType.APPLICATION_JSON)
public Response getNodeInfo(@PathParam("node") String nodeId, @Context ClusterMembershipService clusterMembershipService) {
  Member member = clusterMembershipService.getMember(MemberId.from(nodeId));
  if (member == null) {
    return Response.status(Status.NOT_FOUND).build();
  }
  return Response.ok(new NodeInfo(member)).build();
}
 
Example #14
Source File: DefaultClusterEventService.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public <M, R> CompletableFuture<R> send(String topic, M message, Function<M, byte[]> encoder, Function<byte[], R> decoder, Duration timeout) {
  MemberId memberId = getNextMemberId(topic);
  if (memberId != null) {
    Member member = membershipService.getMember(memberId);
    if (member != null && member.isReachable()) {
      byte[] payload = SERIALIZER.encode(new InternalMessage(InternalMessage.Type.DIRECT, encoder.apply(message)));
      return messagingService.sendAndReceive(member.address(), topic, payload, timeout).thenApply(decoder);
    }
  }
  return Futures.exceptionalFuture(new MessagingException.NoRemoteHandler());
}
 
Example #15
Source File: DefaultClusterEventService.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a gossip message to an active peer.
 */
private void gossip() {
  List<Member> members = membershipService.getMembers()
      .stream()
      .filter(node -> !localMemberId.equals(node.id()))
      .filter(node -> node.isReachable())
      .collect(Collectors.toList());

  if (!members.isEmpty()) {
    Collections.shuffle(members);
    Member member = members.get(0);
    updateNode(member);
  }
}
 
Example #16
Source File: SwimProtocolTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
private void checkEvents(Member member, GroupMembershipEvent... types) throws InterruptedException {
  Multiset<GroupMembershipEvent> events = HashMultiset.create(Arrays.asList(types));
  for (int i = 0; i < types.length; i++) {
    GroupMembershipEvent event = nextEvent(member);
    if (!events.remove(event)) {
      throw new AssertionError();
    }
  }
}
 
Example #17
Source File: SwimProtocolTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
private void checkEvent(Member member, GroupMembershipEvent.Type type, Member value) throws InterruptedException {
  GroupMembershipEvent event = nextEvent(member);
  assertEquals(type, event.type());
  if (value != null) {
    assertEquals(value, event.member());
  }
}
 
Example #18
Source File: AtomixTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Tests a client properties.
 */
@Test
public void testClientProperties() throws Exception {
  List<CompletableFuture<Atomix>> futures = new ArrayList<>();
  futures.add(startAtomix(1, Arrays.asList(1, 2, 3), ConsensusProfile.builder()
      .withMembers("1", "2", "3")
      .withDataPath(new File(new File(DATA_DIR, "client-properties"), "1"))
      .build()));
  futures.add(startAtomix(2, Arrays.asList(1, 2, 3), ConsensusProfile.builder()
      .withMembers("1", "2", "3")
      .withDataPath(new File(new File(DATA_DIR, "client-properties"), "2"))
      .build()));
  futures.add(startAtomix(3, Arrays.asList(1, 2, 3), ConsensusProfile.builder()
      .withMembers("1", "2", "3")
      .withDataPath(new File(new File(DATA_DIR, "client-properties"), "3"))
      .build()));
  Futures.allOf(futures).get(30, TimeUnit.SECONDS);

  TestClusterMembershipEventListener dataListener = new TestClusterMembershipEventListener();
  instances.get(0).getMembershipService().addListener(dataListener);

  Properties properties = new Properties();
  properties.setProperty("a-key", "a-value");
  Atomix client1 = startAtomix(4, Arrays.asList(1, 2, 3), properties, Profile.client()).get(30, TimeUnit.SECONDS);
  assertEquals(1, client1.getPartitionService().getPartitionGroups().size());

  // client1 added to data node
  ClusterMembershipEvent event1 = dataListener.event();
  assertEquals(ClusterMembershipEvent.Type.MEMBER_ADDED, event1.type());

  Member member = event1.subject();

  assertNotNull(member.properties());
  assertEquals(1, member.properties().size());
  assertEquals("a-value", member.properties().get("a-key"));
}
 
Example #19
Source File: AtomixClusterStore.java    From onos with Apache License 2.0 5 votes vote down vote up
private void updateVersion(ControllerNode node, Member member) {
    String versionString = member.properties().getProperty(VERSION_KEY);
    if (versionString != null) {
        Version version = Version.version(versionString);
        if (!Objects.equals(versions.put(node.id(), version), version)) {
            log.info("Updated node {} version to {}", node.id(), version);
        }
    }
}
 
Example #20
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 #21
Source File: DefaultClusterCommunicationService.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(Address sender, byte[] bytes) {
  Member member = membershipService.getMember(sender);
  if (member != null) {
    consumer.accept(member.id(), decoder.apply(bytes));
  }
}
 
Example #22
Source File: DefaultClusterCommunicationService.java    From atomix with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<byte[]> sendAndReceive(String subject, byte[] payload, MemberId toMemberId, Duration timeout) {
  Member member = membershipService.getMember(toMemberId);
  if (member == null) {
    return Futures.exceptionalFuture(CONNECT_EXCEPTION);
  }
  return messagingService.sendAndReceive(member.address(), subject, payload, timeout);
}
 
Example #23
Source File: DefaultClusterCommunicationService.java    From atomix with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<Void> doUnicast(String subject, byte[] payload, MemberId toMemberId, boolean reliable) {
  Member member = membershipService.getMember(toMemberId);
  if (member == null) {
    return Futures.exceptionalFuture(CONNECT_EXCEPTION);
  }
  if (reliable) {
    return messagingService.sendAsync(member.address(), subject, payload);
  } else {
    unicastService.unicast(member.address(), subject, payload);
    return CompletableFuture.completedFuture(null);
  }
}
 
Example #24
Source File: DefaultClusterCommunicationService.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public <M> void broadcastIncludeSelf(
    String subject,
    M message,
    Function<M, byte[]> encoder,
    boolean reliable) {
  multicast(subject, message, encoder, membershipService.getMembers()
      .stream()
      .map(Member::id)
      .collect(Collectors.toSet()),
      reliable);
}
 
Example #25
Source File: DefaultClusterCommunicationService.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public <M> void broadcast(
    String subject,
    M message,
    Function<M, byte[]> encoder,
    boolean reliable) {
  multicast(subject, message, encoder, membershipService.getMembers()
      .stream()
      .filter(node -> !Objects.equal(node, membershipService.getLocalMember()))
      .map(Member::id)
      .collect(Collectors.toSet()),
      reliable);
}
 
Example #26
Source File: SwimMembershipProtocol.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
  return toStringHelper(Member.class)
      .add("id", id())
      .add("address", address())
      .add("properties", properties())
      .add("version", version())
      .add("timestamp", timestamp())
      .add("state", state())
      .add("incarnationNumber", incarnationNumber())
      .toString();
}
 
Example #27
Source File: SwimMembershipProtocol.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> join(BootstrapService bootstrap, NodeDiscoveryService discovery, Member member) {
  if (started.compareAndSet(false, true)) {
    this.bootstrapService = bootstrap;
    this.discoveryService = discovery;
    this.localMember = new SwimMember(
        member.id(),
        member.address(),
        member.zone(),
        member.rack(),
        member.host(),
        member.properties(),
        member.version(),
        System.currentTimeMillis());
    this.localProperties.putAll(localMember.properties());
    discoveryService.addListener(discoveryEventListener);

    LOGGER.info("{} - Member activated: {}", localMember.id(), localMember);
    localMember.setState(State.ALIVE);
    members.put(localMember.id(), localMember);
    post(new GroupMembershipEvent(GroupMembershipEvent.Type.MEMBER_ADDED, localMember));

    registerHandlers();
    gossipFuture = swimScheduler.scheduleAtFixedRate(
        this::gossip, 0, config.getGossipInterval().toMillis(), TimeUnit.MILLISECONDS);
    probeFuture = swimScheduler.scheduleAtFixedRate(
        this::probe, 0, config.getProbeInterval().toMillis(), TimeUnit.MILLISECONDS);
    swimScheduler.execute(this::sync);
    LOGGER.info("Started");
  }
  return CompletableFuture.completedFuture(null);
}
 
Example #28
Source File: DefaultPartitionGroupMembershipService.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Bootstraps the service from the given node.
 */
@SuppressWarnings("unchecked")
private CompletableFuture<Void> bootstrap(Member member, CompletableFuture<Void> future) {
  LOGGER.debug("{} - Bootstrapping from member {}", membershipService.getLocalMember().id(), member);
  messagingService.<PartitionGroupInfo, PartitionGroupInfo>send(
      BOOTSTRAP_SUBJECT,
      new PartitionGroupInfo(membershipService.getLocalMember().id(), systemGroup, Lists.newArrayList(groups.values())),
      serializer::encode,
      serializer::decode,
      member.id())
      .whenCompleteAsync((info, error) -> {
        if (error == null) {
          try {
            updatePartitionGroups(info);
            future.complete(null);
          } catch (Exception e) {
            future.completeExceptionally(e);
          }
        } else {
          error = Throwables.getRootCause(error);
          if (error instanceof MessagingException.NoRemoteHandler || error instanceof TimeoutException) {
            threadContext.schedule(Duration.ofSeconds(1), () -> bootstrap(member, future));
          } else {
            LOGGER.debug("{} - Failed to bootstrap from member {}", membershipService.getLocalMember().id(), member, error);
            future.complete(null);
          }
        }
      }, threadContext);
  return future;
}
 
Example #29
Source File: SwimMembershipProtocol.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public Member getMember(MemberId memberId) {
  Member member = members.get(memberId);
  if (((SwimMember) member).getState() == State.DEAD) {
    return null;
  }
  return member;
}
 
Example #30
Source File: DefaultClusterEventService.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public <M> CompletableFuture<Void> unicast(String topic, M message, Function<M, byte[]> encoder) {
  MemberId memberId = getNextMemberId(topic);
  if (memberId != null) {
    Member member = membershipService.getMember(memberId);
    if (member != null && member.isReachable()) {
      byte[] payload = SERIALIZER.encode(new InternalMessage(InternalMessage.Type.DIRECT, encoder.apply(message)));
      return messagingService.sendAsync(member.address(), topic, payload);
    }
  }
  return CompletableFuture.completedFuture(null);
}