Search in sources :

Example 81 with Member

use of com.hazelcast.cluster.Member in project hazelcast by hazelcast.

the class ClientClusterServiceImpl method detectMembershipEvents.

private List<MembershipEvent> detectMembershipEvents(Collection<Member> prevMembers, Set<Member> currentMembers) {
    List<Member> newMembers = new LinkedList<>();
    Set<Member> deadMembers = new HashSet<>(prevMembers);
    for (Member member : currentMembers) {
        if (!deadMembers.remove(member)) {
            newMembers.add(member);
        }
    }
    List<MembershipEvent> events = new LinkedList<>();
    for (Member member : deadMembers) {
        events.add(new MembershipEvent(client.getCluster(), member, MembershipEvent.MEMBER_REMOVED, currentMembers));
        Connection connection = connectionManager.getConnection(member.getUuid());
        if (connection != null) {
            connection.close(null, new TargetDisconnectedException("The client has closed the connection to this member," + " after receiving a member left event from the cluster. " + connection));
        }
    }
    for (Member member : newMembers) {
        events.add(new MembershipEvent(client.getCluster(), member, MembershipEvent.MEMBER_ADDED, currentMembers));
    }
    if (events.size() != 0) {
        MemberListSnapshot snapshot = memberListSnapshot.get();
        if (snapshot.members.values().size() != 0) {
            logger.info(membersString(snapshot));
        }
    }
    return events;
}
Also used : InitialMembershipEvent(com.hazelcast.cluster.InitialMembershipEvent) MembershipEvent(com.hazelcast.cluster.MembershipEvent) Connection(com.hazelcast.internal.nio.Connection) TcpClientConnection(com.hazelcast.client.impl.connection.tcp.TcpClientConnection) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException) Member(com.hazelcast.cluster.Member) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 82 with Member

use of com.hazelcast.cluster.Member in project hazelcast by hazelcast.

the class ClientClusterServiceImpl method membersString.

private String membersString(MemberListSnapshot snapshot) {
    Collection<Member> members = snapshot.members.values();
    StringBuilder sb = new StringBuilder("\n\nMembers [");
    sb.append(members.size());
    sb.append("] {");
    for (Member member : members) {
        sb.append("\n\t").append(member);
    }
    sb.append("\n}\n");
    return sb.toString();
}
Also used : Member(com.hazelcast.cluster.Member)

Example 83 with Member

use of com.hazelcast.cluster.Member in project hazelcast by hazelcast.

the class ClientClusterServiceImpl method applyInitialState.

private void applyInitialState(int version, Collection<MemberInfo> memberInfos) {
    MemberListSnapshot snapshot = createSnapshot(version, memberInfos);
    translateToPublicAddress.refresh(client.getClusterDiscoveryService().current().getAddressProvider(), memberInfos);
    memberListSnapshot.set(snapshot);
    logger.info(membersString(snapshot));
    Set<Member> members = toUnmodifiableHasSet(snapshot.members.values());
    InitialMembershipEvent event = new InitialMembershipEvent(client.getCluster(), members);
    for (MembershipListener listener : listeners.values()) {
        if (listener instanceof InitialMembershipListener) {
            ((InitialMembershipListener) listener).init(event);
        }
    }
}
Also used : InitialMembershipListener(com.hazelcast.cluster.InitialMembershipListener) InitialMembershipListener(com.hazelcast.cluster.InitialMembershipListener) MembershipListener(com.hazelcast.cluster.MembershipListener) Member(com.hazelcast.cluster.Member) InitialMembershipEvent(com.hazelcast.cluster.InitialMembershipEvent)

Example 84 with Member

use of com.hazelcast.cluster.Member in project hazelcast by hazelcast.

the class ClientClusterServiceImpl method createSnapshot.

private MemberListSnapshot createSnapshot(int memberListVersion, Collection<MemberInfo> memberInfos) {
    LinkedHashMap<UUID, Member> newMembers = new LinkedHashMap<>();
    for (MemberInfo memberInfo : memberInfos) {
        MemberImpl.Builder memberBuilder;
        Map<EndpointQualifier, Address> addressMap = memberInfo.getAddressMap();
        if (addressMap == null || addressMap.isEmpty()) {
            memberBuilder = new MemberImpl.Builder(memberInfo.getAddress());
        } else {
            memberBuilder = new MemberImpl.Builder(addressMap).address(addressMap.getOrDefault(CLIENT, addressMap.get(MEMBER)));
        }
        memberBuilder.version(memberInfo.getVersion()).uuid(memberInfo.getUuid()).attributes(memberInfo.getAttributes()).liteMember(memberInfo.isLiteMember()).memberListJoinVersion(memberInfo.getMemberListJoinVersion());
        newMembers.put(memberInfo.getUuid(), memberBuilder.build());
    }
    return new MemberListSnapshot(memberListVersion, newMembers);
}
Also used : MemberInfo(com.hazelcast.internal.cluster.MemberInfo) Address(com.hazelcast.cluster.Address) InetSocketAddress(java.net.InetSocketAddress) MemberImpl(com.hazelcast.cluster.impl.MemberImpl) EndpointQualifier(com.hazelcast.instance.EndpointQualifier) UUID(java.util.UUID) Member(com.hazelcast.cluster.Member) LinkedHashMap(java.util.LinkedHashMap)

Example 85 with Member

use of com.hazelcast.cluster.Member in project hazelcast by hazelcast.

the class ClientClusterServiceImpl method addMembershipListener.

@Nonnull
@Override
public UUID addMembershipListener(@Nonnull MembershipListener listener) {
    checkNotNull(listener, "Listener can't be null");
    synchronized (clusterViewLock) {
        UUID id = addMembershipListenerWithoutInit(listener);
        if (listener instanceof InitialMembershipListener) {
            Cluster cluster = client.getCluster();
            Collection<Member> members = memberListSnapshot.get().members.values();
            // it will be redirected to listeners when it arrives see #handleInitialMembershipEvent
            if (!members.isEmpty()) {
                InitialMembershipEvent event = new InitialMembershipEvent(cluster, toUnmodifiableHasSet(members));
                ((InitialMembershipListener) listener).init(event);
            }
        }
        return id;
    }
}
Also used : InitialMembershipListener(com.hazelcast.cluster.InitialMembershipListener) Cluster(com.hazelcast.cluster.Cluster) UUID(java.util.UUID) Member(com.hazelcast.cluster.Member) InitialMembershipEvent(com.hazelcast.cluster.InitialMembershipEvent) Nonnull(javax.annotation.Nonnull)

Aggregations

Member (com.hazelcast.cluster.Member)429 Test (org.junit.Test)210 QuickTest (com.hazelcast.test.annotation.QuickTest)191 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)164 HazelcastInstance (com.hazelcast.core.HazelcastInstance)116 IExecutorService (com.hazelcast.core.IExecutorService)73 Address (com.hazelcast.cluster.Address)62 ArrayList (java.util.ArrayList)55 Future (java.util.concurrent.Future)53 UUID (java.util.UUID)43 Config (com.hazelcast.config.Config)42 CountDownLatch (java.util.concurrent.CountDownLatch)38 Map (java.util.Map)33 HashMap (java.util.HashMap)31 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)28 MultiExecutionCallback (com.hazelcast.core.MultiExecutionCallback)25 List (java.util.List)25 MemberImpl (com.hazelcast.cluster.impl.MemberImpl)24 Operation (com.hazelcast.spi.impl.operationservice.Operation)24 IMap (com.hazelcast.map.IMap)23