use of io.scalecube.cluster.Member in project scalecube by scalecube.
the class FailureDetectorTest method assertStatus.
/**
* @param address target member to expect on
* @param status expected listen status
* @param events events collection of failure detector events
* @param expected expected members of the given listenStatus
*/
private void assertStatus(Address address, MemberStatus status, Collection<FailureDetectorEvent> events, Address... expected) {
List<Address> actual = events.stream().filter(event -> event.status() == status).map(FailureDetectorEvent::member).map(Member::address).collect(Collectors.toList());
String msg1 = String.format("Node %s expected %s %s members %s, but was: %s", address, expected.length, status, Arrays.toString(expected), events);
assertEquals(msg1, expected.length, actual.size());
for (Address member : expected) {
String msg2 = String.format("Node %s expected as %s %s, but was: %s", address, status, member, events);
assertTrue(msg2, actual.contains(member));
}
}
use of io.scalecube.cluster.Member in project scalecube by scalecube.
the class FailureDetectorImpl method onPing.
/**
* Listens to PING message and answers with ACK.
*/
private void onPing(Message message) {
LOGGER.trace("Received Ping: {}", message);
PingData data = message.data();
Member localMember = membership.member();
if (!data.getTo().id().equals(localMember.id())) {
LOGGER.warn("Received Ping to {}, but local member is {}", data.getTo(), localMember);
return;
}
String correlationId = message.correlationId();
Message ackMessage = Message.withData(data).qualifier(PING_ACK).correlationId(correlationId).build();
LOGGER.trace("Send PingAck to {}", data.getFrom().address());
transport.send(data.getFrom().address(), ackMessage);
}
use of io.scalecube.cluster.Member in project scalecube by scalecube.
the class MembershipProtocolImpl method onUpdateMetadataProperty.
// ================================================
// ============== Event Listeners =================
// ================================================
private void onUpdateMetadataProperty(String key, String value) {
// Update local member reference
Member curMember = memberRef.get();
Map<String, String> metadata = new HashMap<>(curMember.metadata());
metadata.put(key, value);
onUpdateMetadata(metadata);
}
use of io.scalecube.cluster.Member in project scalecube by scalecube.
the class MembershipProtocolImpl method onLeave.
private CompletableFuture<String> onLeave() {
Member curMember = memberRef.get();
String memberId = curMember.id();
MembershipRecord curRecord = membershipTable.get(memberId);
MembershipRecord newRecord = new MembershipRecord(this.member(), DEAD, curRecord.incarnation() + 1);
membershipTable.put(memberId, newRecord);
return spreadMembershipGossip(newRecord);
}
use of io.scalecube.cluster.Member in project scalecube by scalecube.
the class MembershipProtocolImpl method updateMembership.
/**
* Try to update membership table with the given record.
*
* @param r1 new membership record which compares with existing r0 record
* @param reason indicating the reason for updating membership table
*/
private void updateMembership(MembershipRecord r1, MembershipUpdateReason reason) {
Preconditions.checkArgument(r1 != null, "Membership record can't be null");
// Get current record
MembershipRecord r0 = membershipTable.get(r1.id());
// Check if new record r1 overrides existing membership record r0
if (!r1.isOverrides(r0)) {
return;
}
// If received updated for local member then increase incarnation number and spread Alive gossip
Member localMember = memberRef.get();
if (r1.member().id().equals(localMember.id())) {
int currentIncarnation = Math.max(r0.incarnation(), r1.incarnation());
MembershipRecord r2 = new MembershipRecord(localMember, r0.status(), currentIncarnation + 1);
membershipTable.put(localMember.id(), r2);
LOGGER.debug("Local membership record r0={}, but received r1={}, spread r2={}", r0, r1, r2);
spreadMembershipGossip(r2);
return;
}
// Update membership
if (r1.isDead()) {
membershipTable.remove(r1.id());
} else {
membershipTable.put(r1.id(), r1);
}
// Schedule/cancel suspicion timeout task
if (r1.isSuspect()) {
scheduleSuspicionTimeoutTask(r1);
} else {
cancelSuspicionTimeoutTask(r1.id());
}
// Emit membership event
if (r1.isDead()) {
subject.onNext(MembershipEvent.createRemoved(r1.member()));
} else if (r0 == null && r1.isAlive()) {
subject.onNext(MembershipEvent.createAdded(r1.member()));
} else if (r0 != null && !r0.member().equals(r1.member())) {
subject.onNext(MembershipEvent.createUpdated(r0.member(), r1.member()));
}
// Spread gossip (unless already gossiped)
if (reason != MembershipUpdateReason.MEMBERSHIP_GOSSIP && reason != MembershipUpdateReason.INITIAL_SYNC) {
spreadMembershipGossip(r1);
}
}
Aggregations