Source:
private synchronized CompletableFuture<Void> sendHeartbeats() { Member localMember = getLocalMember(); if (!localMember.metadata().equals(localMetadata)) { localMetadata = localMember.metadata(); post(new ClusterMembershipEvent(ClusterMembershipEvent.Type.MEMBER_UPDATED, localMember)); } Stream<StatefulMember> clusterMembers = this.members.values() .stream() .filter(member -> !member.id().equals(localMember.id())); Stream<StatefulMember> bootstrapMembers = this.bootstrapMembers.stream() .filter(member -> !member.id().equals(localMember.id()) && !members.containsKey(member.id())) .map(member -> new StatefulMember( member.id(), member.address(), member.zone(), member.rack(), member.host(), member.metadata())); byte[] payload = SERIALIZER.encode(new ClusterHeartbeat( localMember.id(), localMember.zone(), localMember.rack(), localMember.host(), localMember.metadata())); return Futures.allOf(Stream.concat(clusterMembers, bootstrapMembers).map(member -> { LOGGER.trace("{} - Sending heartbeat: {}", localMember.id(), member.id()); CompletableFuture<Void> future = sendHeartbeat(member.address(), payload); PhiAccrualFailureDetector failureDetector = failureDetectors.computeIfAbsent(member.id(), n -> new PhiAccrualFailureDetector()); double phi = failureDetector.phi(); if (phi >= phiFailureThreshold || (phi == 0.0 && failureDetector.lastUpdated() > 0 && System.currentTimeMillis() - failureDetector.lastUpdated() > failureTimeout)) { if (member.getState() == State.ACTIVE) { deactivateMember(member); } } else { if (member.getState() == State.INACTIVE) { activateMember(member); } } return future.exceptionally(v -> null); }).collect(Collectors.toList())) .thenApply(v -> null); }

Target:
private CompletableFuture<Void> sendHeartbeats() { Member localMember = getLocalMember(); if (!localMember.metadata().equals(localMetadata)) { synchronized (this) { if (!localMember.metadata().equals(localMetadata)) { localMetadata = localMember.metadata(); post(new ClusterMembershipEvent(ClusterMembershipEvent.Type.MEMBER_UPDATED, localMember)); } } } Stream<StatefulMember> clusterMembers = this.members.values() .stream() .filter(member -> !member.id().equals(localMember.id())); Stream<StatefulMember> bootstrapMembers = this.bootstrapMembers.stream() .filter(member -> !member.id().equals(localMember.id()) && !members.containsKey(member.id())) .map(member -> new StatefulMember( member.id(), member.address(), member.zone(), member.rack(), member.host(), member.metadata())); byte[] payload = SERIALIZER.encode(new ClusterHeartbeat( localMember.id(), localMember.zone(), localMember.rack(), localMember.host(), localMember.metadata())); return Futures.allOf(Stream.concat(clusterMembers, bootstrapMembers).map(member -> { LOGGER.trace("{} - Sending heartbeat: {}", localMember.id(), member.id()); CompletableFuture<Void> future = sendHeartbeat(member.address(), payload); PhiAccrualFailureDetector failureDetector = failureDetectors.computeIfAbsent(member.id(), n -> new PhiAccrualFailureDetector()); double phi = failureDetector.phi(); if (phi >= phiFailureThreshold || (phi == 0.0 && failureDetector.lastUpdated() > 0 && System.currentTimeMillis() - failureDetector.lastUpdated() > failureTimeout)) { if (member.getState() == State.ACTIVE) { deactivateMember(member); } } else { if (member.getState() == State.INACTIVE) { activateMember(member); } } return future.exceptionally(v -> null); }).collect(Collectors.toList())) .thenApply(v -> null); }