@Override public CompletableFuture<Void> start() { registerListeners(); compactTimer = threadContext.schedule(Duration.ofSeconds(30), this::compact); return memberGroupService.start().thenComposeAsync(v -> { MemberGroup group = memberGroupService.getMemberGroup(clusterMembershipService.getLocalMember()); primaryElection.addListener(primaryElectionListener); if (group != null) { return primaryElection.enter(new GroupMember(clusterMembershipService.getLocalMember().id(), group.id())) .thenApply(term -> { changeRole(term); return null; }); } return CompletableFuture.completedFuture(null); }, threadContext).thenApply(v -> { started.set(true); return null; }); }
@Override public Collection<MemberId> members() { return Futures.get(election.getTerm()) .candidates() .stream() .map(GroupMember::memberId) .collect(Collectors.toList()); }
/** * Closes the service. */ public CompletableFuture<Void> close() { CompletableFuture<Void> future = new CompletableFuture<>(); threadContext.execute(() -> { try { clusterMembershipService.removeListener(membershipEventListener); primaryElection.removeListener(primaryElectionListener); role.close(); } finally { future.complete(null); } }); return future.thenRunAsync(() -> threadContext.close()); } }
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()); }
@Override public CompletableFuture<Void> start() { registerListeners(); return memberGroupService.start().thenCompose(v -> { MemberGroup group = memberGroupService.getMemberGroup(clusterMembershipService.getLocalMember()); if (group != null) { return primaryElection.enter(new GroupMember(clusterMembershipService.getLocalMember().id(), group.id())); } return CompletableFuture.completedFuture(null); }).thenApply(v -> { started.set(true); return null; }); }
.build()); clusterMembershipService.addListener(membershipEventListener); primaryElection.addListener(primaryElectionListener);
@Override public Collection<MemberId> backups() { return Futures.get(election.getTerm()) .candidates() .stream() .map(GroupMember::memberId) .collect(Collectors.toList()); }
@Override public CompletableFuture<Void> start() { registerListeners(); compactTimer = threadContext.schedule(Duration.ofSeconds(30), this::compact); return memberGroupService.start().thenComposeAsync(v -> { MemberGroup group = memberGroupService.getMemberGroup(clusterMembershipService.getLocalMember()); primaryElection.addListener(primaryElectionListener); if (group != null) { return primaryElection.enter(new GroupMember(clusterMembershipService.getLocalMember().id(), group.id())) .thenApply(term -> { changeRole(term); return null; }); } return CompletableFuture.completedFuture(null); }, threadContext).thenApply(v -> { started.set(true); return null; }); }
@Override public CompletableFuture<Void> stop() { unregisterListeners(); primaryElection.removeListener(primaryElectionListener); if (compactTimer != null) { compactTimer.cancel(); } journal.close(); started.set(false); return memberGroupService.stop().exceptionally(throwable -> { log.error("Failed stopping member group service", throwable); return null; }).thenRunAsync(() -> { if (closeOnStop) { threadContextFactory.close(); } }); } }
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()); }
@Override public Collection<MemberId> backups() { return Futures.get(election.getTerm()) .candidates() .stream() .map(GroupMember::memberId) .collect(Collectors.toList()); }
@Override public CompletableFuture<Void> close() { CompletableFuture<Void> future = new CompletableFuture<>(); PrimaryTerm term = this.term; if (term.primary() != null) { protocol.close(term.primary().memberId(), new CloseRequest(descriptor, sessionId.id())) .whenCompleteAsync((response, error) -> { try { protocol.unregisterEventListener(sessionId); clusterMembershipService.removeListener(membershipEventListener); primaryElection.removeListener(primaryElectionListener); } finally { future.complete(null); } }, threadContext); } else { future.complete(null); } return future; }
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()); }
@Override public Collection<MemberId> members() { return Futures.get(election.getTerm()) .candidates() .stream() .map(GroupMember::memberId) .collect(Collectors.toList()); }
@Override public CompletableFuture<Void> stop() { unregisterListeners(); primaryElection.removeListener(primaryElectionListener); if (compactTimer != null) { compactTimer.cancel(); } journal.close(); started.set(false); return memberGroupService.stop().exceptionally(throwable -> { log.error("Failed stopping member group service", throwable); return null; }).thenRunAsync(() -> { if (closeOnStop) { threadContextFactory.close(); } }); } }
/** * Opens the service context. * * @return a future to be completed once the service context has been opened */ public CompletableFuture<Void> open() { return primaryElection.getTerm() .thenAccept(this::changeRole) .thenRun(() -> service.init(this)); }
@Override public long term() { return Futures.get(election.getTerm()).term(); }
@Override public long term() { return Futures.get(election.getTerm()).term(); }
/** * Returns the current primary term. * * @return the current primary term */ private CompletableFuture<PrimaryTerm> term() { CompletableFuture<PrimaryTerm> future = new CompletableFuture<>(); threadContext.execute(() -> { if (term != null) { future.complete(term); } else { primaryElection.getTerm().whenCompleteAsync((term, error) -> { if (term != null) { this.term = term; future.complete(term); } else { future.completeExceptionally(new PrimitiveException.Unavailable()); } }); } }); return future; }
@Override public MemberId primary() { return Futures.get(election.getTerm()) .primary() .memberId(); }