All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.servicefabric.cluster.ClusterMembershipTable Maven / Gradle / Ivy

There is a newer version: 0.0.4
Show newest version
package io.servicefabric.cluster;

import static com.google.common.collect.Maps.filterValues;
import static com.google.common.collect.Maps.newHashMap;
import static io.servicefabric.cluster.ClusterMemberStatus.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.google.common.base.Predicate;
import io.servicefabric.cluster.fdetector.FailureDetectorEvent;

final class ClusterMembershipTable {
	private final ConcurrentMap membership = new ConcurrentHashMap<>();

	List merge(ClusterMembershipData data) {
		List updates = new ArrayList<>();
		for (ClusterMember record : data.getMembership()) {
			updates.addAll(merge(record));
		}
		return updates;
	}

	List merge(ClusterMember r1) {
		List updates = new ArrayList<>(1);
		ClusterMember r0 = membership.putIfAbsent(r1.endpoint(), r1);
		if (r0 == null) {
			updates.add(r1);
		}
		else if (r0.compareTo(r1) < 0) {
			if (membership.replace(r1.endpoint(), r0, r1)) {
				updates.add(r1);
			} else {
				return merge(r1);
			}
		}
		return updates;
	}

	ClusterMember get(ClusterEndpoint member) {
		return membership.get(member);
	}

	List remove(ClusterEndpoint member) {
		List updates = new ArrayList<>(1);
		ClusterMember r0 = membership.remove(member);
		if (r0 != null) {
			updates.add(new ClusterMember(member, REMOVED, r0.metadata()));
		}
		return updates;
	}

	List merge(FailureDetectorEvent event) {
		ClusterMember r0 = membership.get(event.endpoint());
		if (r0 != null) {
			return merge(new ClusterMember(event.endpoint(), event.status(), r0.metadata()));
		} else {
			return Collections.emptyList();
		}
	}

	List asList() {
		return new ArrayList<>(membership.values());
	}

	/** Getting {@code TRUSTED} or {@code SUSPECTED} snapshot of {@link #membership}. */
	Map getTrustedOrSuspected() {
		return newHashMap(filterValues(membership, new Predicate() {
			@Override
			public boolean apply(ClusterMember input) {
				return input.status() == TRUSTED || input.status() == SUSPECTED;
			}
		}));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy