Search in sources :

Example 11 with Member

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));
    }
}
Also used : Arrays(java.util.Arrays) Address(io.scalecube.transport.Address) MembershipProtocol(io.scalecube.cluster.membership.MembershipProtocol) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) MemberStatus(io.scalecube.cluster.membership.MemberStatus) SUSPECT(io.scalecube.cluster.membership.MemberStatus.SUSPECT) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ClusterConfig(io.scalecube.cluster.ClusterConfig) List(java.util.List) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) ALIVE(io.scalecube.cluster.membership.MemberStatus.ALIVE) Member(io.scalecube.cluster.Member) Transport(io.scalecube.transport.Transport) TransportConfig(io.scalecube.transport.TransportConfig) DummyMembershipProtocol(io.scalecube.cluster.membership.DummyMembershipProtocol) BaseTest(io.scalecube.testlib.BaseTest) Assert.assertEquals(org.junit.Assert.assertEquals) Address(io.scalecube.transport.Address)

Example 12 with 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);
}
Also used : Message(io.scalecube.transport.Message) Member(io.scalecube.cluster.Member)

Example 13 with Member

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);
}
Also used : HashMap(java.util.HashMap) Member(io.scalecube.cluster.Member)

Example 14 with Member

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);
}
Also used : Member(io.scalecube.cluster.Member)

Example 15 with Member

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);
    }
}
Also used : Member(io.scalecube.cluster.Member)

Aggregations

Member (io.scalecube.cluster.Member)17 Message (io.scalecube.transport.Message)8 TimeUnit (java.util.concurrent.TimeUnit)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)5 Transport (io.scalecube.transport.Transport)5 MemberStatus (io.scalecube.cluster.membership.MemberStatus)4 MembershipProtocol (io.scalecube.cluster.membership.MembershipProtocol)4 BaseTest (io.scalecube.testlib.BaseTest)4 Address (io.scalecube.transport.Address)4 Collections (java.util.Collections)4 Test (org.junit.Test)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)3 ALIVE (io.scalecube.cluster.membership.MemberStatus.ALIVE)3 Collection (java.util.Collection)3 Map (java.util.Map)3 Executors (java.util.concurrent.Executors)3