Java Code Examples for io.atomix.cluster.ClusterMembershipService#addListener()

The following examples show how to use io.atomix.cluster.ClusterMembershipService#addListener() . 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: HashBasedPrimaryElection.java    From atomix with Apache License 2.0 6 votes vote down vote up
public HashBasedPrimaryElection(
    PartitionId partitionId,
    ClusterMembershipService clusterMembershipService,
    PartitionGroupMembershipService groupMembershipService,
    ClusterCommunicationService communicationService,
    ScheduledExecutorService executor) {
  this.partitionId = partitionId;
  this.clusterMembershipService = clusterMembershipService;
  this.groupMembershipService = groupMembershipService;
  this.communicationService = communicationService;
  this.subject = String.format("primary-election-counter-%s-%d", partitionId.group(), partitionId.id());
  recomputeTerm(groupMembershipService.getMembership(partitionId.group()));
  groupMembershipService.addListener(groupMembershipEventListener);
  clusterMembershipService.addListener(clusterMembershipEventListener);
  communicationService.subscribe(subject, SERIALIZER::decode, this::updateCounters, executor);
  broadcastFuture = executor.scheduleAtFixedRate(this::broadcastCounters, BROADCAST_INTERVAL, BROADCAST_INTERVAL, TimeUnit.MILLISECONDS);
}
 
Example 2
Source File: PrimaryBackupSessionClient.java    From atomix with Apache License 2.0 6 votes vote down vote up
public PrimaryBackupSessionClient(
    String clientName,
    PartitionId partitionId,
    SessionId sessionId,
    PrimitiveType primitiveType,
    PrimitiveDescriptor descriptor,
    ClusterMembershipService clusterMembershipService,
    PrimaryBackupClientProtocol protocol,
    PrimaryElection primaryElection,
    ThreadContext threadContext) {
  this.partitionId = checkNotNull(partitionId);
  this.sessionId = checkNotNull(sessionId);
  this.primitiveType = primitiveType;
  this.descriptor = descriptor;
  this.clusterMembershipService = clusterMembershipService;
  this.protocol = protocol;
  this.primaryElection = primaryElection;
  this.threadContext = threadContext;
  clusterMembershipService.addListener(membershipEventListener);
  primaryElection.addListener(primaryElectionListener);
  this.log = ContextualLoggerFactory.getLogger(getClass(), LoggerContext.builder(SessionClient.class)
      .addValue(clientName)
      .add("type", primitiveType.name())
      .add("name", descriptor.name())
      .build());
}
 
Example 3
Source File: DistributedLogSession.java    From atomix with Apache License 2.0 6 votes vote down vote up
public DistributedLogSession(
    PartitionId partitionId,
    SessionId sessionId,
    ClusterMembershipService clusterMembershipService,
    LogClientProtocol protocol,
    PrimaryElection primaryElection,
    ThreadContext threadContext) {
  this.partitionId = checkNotNull(partitionId, "partitionId cannot be null");
  this.sessionId = checkNotNull(sessionId, "sessionId cannot be null");
  this.protocol = checkNotNull(protocol, "protocol cannot be null");
  this.primaryElection = checkNotNull(primaryElection, "primaryElection cannot be null");
  this.threadContext = checkNotNull(threadContext, "threadContext cannot be null");
  this.memberId = clusterMembershipService.getLocalMember().id();
  this.subject = String.format("%s-%s-%s", partitionId.group(), partitionId.id(), sessionId);
  clusterMembershipService.addListener(membershipEventListener);
  primaryElection.addListener(primaryElectionListener);
  this.log = ContextualLoggerFactory.getLogger(getClass(), LoggerContext.builder(DistributedLogProducer.class)
      .addValue(partitionId.group() != null
          ? String.format("%s-%d", partitionId.group(), partitionId.id())
          : partitionId.id())
      .build());
}
 
Example 4
Source File: ClusterResource.java    From atomix with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/events")
@Produces(MediaType.APPLICATION_JSON)
public void getEvent(@Context ClusterMembershipService clusterMembershipService, @Context EventManager events, @Suspended AsyncResponse response) {
  EventLog<ClusterMembershipEventListener, ClusterMembershipEvent> eventLog = events.getOrCreateEventLog(ClusterResource.class, "", l -> e -> l.addEvent(e));
  if (eventLog.open()) {
    clusterMembershipService.addListener(eventLog.listener());
  }

  eventLog.nextEvent().whenComplete((result, error) -> {
    if (error == null) {
      response.resume(Response.ok(new NodeEvent(result.subject().id(), result.type())));
    } else {
      LOGGER.warn("{}", error);
      response.resume(Response.serverError().build());
    }
  });
}
 
Example 5
Source File: ClusterResource.java    From atomix with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/nodes/{node}/events")
@Produces(MediaType.APPLICATION_JSON)
public void getNodeEvent(@PathParam("node") String memberId, @Context ClusterMembershipService clusterMembershipService, @Context EventManager events, @Suspended AsyncResponse response) {
  EventLog<ClusterMembershipEventListener, ClusterMembershipEvent> eventLog = events.getOrCreateEventLog(ClusterResource.class, memberId, l -> e -> {
    if (e.subject().id().id().equals(memberId)) {
      l.addEvent(e);
    }
  });
  if (eventLog.open()) {
    clusterMembershipService.addListener(eventLog.listener());
  }

  eventLog.nextEvent().whenComplete((result, error) -> {
    if (error == null) {
      response.resume(Response.ok(new NodeEvent(result.subject().id(), result.type())));
    } else {
      LOGGER.warn("{}", error);
      response.resume(Response.serverError().build());
    }
  });
}
 
Example 6
Source File: PrimaryBackupServiceContext.java    From atomix with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public PrimaryBackupServiceContext(
    String serverName,
    PrimitiveId primitiveId,
    PrimitiveType primitiveType,
    PrimitiveDescriptor descriptor,
    ThreadContext threadContext,
    ClusterMembershipService clusterMembershipService,
    MemberGroupService memberGroupService,
    PrimaryBackupServerProtocol protocol,
    PrimaryElection primaryElection) {
  this.localMemberId = clusterMembershipService.getLocalMember().id();
  this.serverName = checkNotNull(serverName);
  this.primitiveId = checkNotNull(primitiveId);
  this.primitiveType = checkNotNull(primitiveType);
  this.serviceConfig = Serializer.using(primitiveType.namespace()).decode(descriptor.config());
  this.descriptor = checkNotNull(descriptor);
  this.service = primitiveType.newService(serviceConfig);
  this.threadContext = checkNotNull(threadContext);
  this.clusterMembershipService = checkNotNull(clusterMembershipService);
  this.memberGroupService = checkNotNull(memberGroupService);
  this.protocol = checkNotNull(protocol);
  this.primaryElection = checkNotNull(primaryElection);
  this.log = ContextualLoggerFactory.getLogger(getClass(), LoggerContext.builder(PrimitiveService.class)
      .addValue(serverName)
      .add("type", descriptor.type())
      .add("name", descriptor.name())
      .build());
  clusterMembershipService.addListener(membershipEventListener);
  primaryElection.addListener(primaryElectionListener);
}
 
Example 7
Source File: ClusterResource.java    From atomix with Apache License 2.0 5 votes vote down vote up
@POST
@Path("/events")
@Produces(MediaType.APPLICATION_JSON)
public Response addListener(@Context ClusterMembershipService clusterMembershipService, @Context EventManager events) {
  String listenerId = UUID.randomUUID().toString();
  EventLog<ClusterMembershipEventListener, ClusterMembershipEvent> eventLog = events.getOrCreateEventLog(ClusterResource.class, listenerId, l -> e -> l.addEvent(e));
  if (eventLog.open()) {
    clusterMembershipService.addListener(eventLog.listener());
  }
  return Response.ok(listenerId).build();
}
 
Example 8
Source File: ClusterResource.java    From atomix with Apache License 2.0 5 votes vote down vote up
@POST
@Path("/nodes/{node}/events")
@Produces(MediaType.APPLICATION_JSON)
public Response addNodeListener(@PathParam("node") String memberId, @Context ClusterMembershipService clusterMembershipService, @Context EventManager events) {
  String id = UUID.randomUUID().toString();
  EventLog<ClusterMembershipEventListener, ClusterMembershipEvent> eventLog = events.getOrCreateEventLog(ClusterResource.class, getNodeListener(memberId, id), l -> e -> {
    if (e.subject().id().id().equals(memberId)) {
      l.addEvent(e);
    }
  });
  if (eventLog.open()) {
    clusterMembershipService.addListener(eventLog.listener());
  }
  return Response.ok(id).build();
}