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;
}
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();
}
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);
}
}
}
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);
}
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;
}
}
Aggregations