use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class GossipDeviceStore method removeDevice.
@Override
public synchronized DeviceEvent removeDevice(DeviceId deviceId) {
final NodeId myId = clusterService.getLocalNode().id();
NodeId master = mastershipService.getMasterFor(deviceId);
// if there exist a master, forward
// if there is no master, try to become one and process
boolean relinquishAtEnd = false;
if (master == null) {
final MastershipRole myRole = mastershipService.getLocalRole(deviceId);
if (myRole != MastershipRole.NONE) {
relinquishAtEnd = true;
}
log.debug("Temporarily requesting role for {} to remove", deviceId);
if (mastershipService.requestRoleFor(deviceId).join() == MastershipRole.MASTER) {
master = myId;
}
}
boolean isMaster = myId.equals(master);
// If this node is not the master, forward the request.
if (!isMaster) {
log.debug("{} has control of {}, forwarding remove request", master, deviceId);
// TODO check unicast return value
clusterCommunicator.unicast(deviceId, DEVICE_REMOVE_REQ, SERIALIZER::encode, master);
/* error log:
log.error("Failed to forward {} remove request to {}", deviceId, master, e);
*/
}
// If this node is the master, get a timestamp. Otherwise, default to the current device timestamp.
Timestamp timestamp = isMaster ? deviceClockService.getTimestamp(deviceId) : null;
DeviceEvent event = removeDeviceInternal(deviceId, timestamp);
// If this node is the master, update peers.
if (isMaster && event != null) {
log.debug("Notifying peers of a device removed topology event for deviceId: {}", deviceId);
notifyPeers(new InternalDeviceRemovedEvent(deviceId, timestamp));
}
notifyDelegateIfNotNull(event);
// Relinquish mastership if acquired to remove the device.
if (relinquishAtEnd) {
log.debug("Relinquishing temporary role acquired for {}", deviceId);
mastershipService.relinquishMastership(deviceId);
}
return event;
}
use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class UpgradeManagerTest method createUpgradeManager.
/**
* Creates a new upgrade manager to test.
*
* @param version the local node version
* @param state the initial upgrade state
* @param versions a list of controller node versions
* @return the activated upgrade manager
*/
@SuppressWarnings("unchecked")
private UpgradeManager createUpgradeManager(Version version, Upgrade state, List<Version> versions) {
UpgradeManager upgradeManager = new UpgradeManager();
injectEventDispatcher(upgradeManager, new TestEventDispatcher());
upgradeManager.membershipService = new MembershipServiceAdapter() {
@Override
public MembershipGroup getLocalGroup() {
return getGroups().stream().filter(group -> group.version().equals(version)).findFirst().get();
}
@Override
public Collection<MembershipGroup> getGroups() {
AtomicInteger nodeCounter = new AtomicInteger();
Map<Version, Set<Member>> groups = Maps.newHashMap();
versions.stream().forEach(version -> {
groups.computeIfAbsent(version, k -> Sets.newHashSet()).add(new Member(NodeId.nodeId(String.valueOf(nodeCounter.getAndIncrement())), version));
});
return Maps.transformEntries(groups, MembershipGroup::new).values();
}
};
upgradeManager.clusterService = new ClusterServiceAdapter() {
@Override
public Set<ControllerNode> getNodes() {
AtomicInteger nodeCounter = new AtomicInteger();
return versions.stream().map(v -> {
int nodeId = nodeCounter.getAndIncrement();
return new DefaultControllerNode(NodeId.nodeId(String.valueOf(nodeId)), IpAddress.valueOf("127.0.0.1"), nodeId);
}).collect(Collectors.toSet());
}
@Override
public ControllerNode getNode(NodeId nodeId) {
return getNodes().stream().filter(node -> node.id().equals(nodeId)).findFirst().orElse(null);
}
@Override
public Version getVersion(NodeId nodeId) {
return versions.get(Integer.parseInt(nodeId.id()));
}
};
upgradeManager.versionService = new VersionServiceAdapter() {
@Override
public Version version() {
return version;
}
};
upgradeManager.coordinationService = new CoordinationServiceAdapter() {
@Override
public <V> AtomicValueBuilder<V> atomicValueBuilder() {
return new AtomicValueBuilder<V>() {
@Override
public AsyncAtomicValue<V> build() {
return new AsyncAtomicValueAdapter() {
@Override
public AtomicValue asAtomicValue() {
return new AtomicValueAdapter() {
private Object value = state;
@Override
public void set(Object value) {
this.value = value;
}
@Override
public Object get() {
return value;
}
@Override
public boolean compareAndSet(Object expect, Object update) {
if ((value == null && expect == null) || (value != null && value.equals(expect))) {
value = update;
return true;
}
return false;
}
};
}
};
}
};
}
};
upgradeManager.activate();
return upgradeManager;
}
use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class RoleInfoProtoTranslator method translate.
/**
* Translates gRPC RoleInfo to {@link RoleInfo}.
*
* @param roleInfo gRPC message
* @return {@link RoleInfo}
*/
public static RoleInfo translate(RoleInfoProtoOuterClass.RoleInfoProto roleInfo) {
NodeId master = NodeIdProtoTranslator.translate(roleInfo.getMaster());
List<NodeId> backups = Lists.newArrayList();
backups = roleInfo.getBackupsList().stream().map(r -> NodeIdProtoTranslator.translate(r)).collect(Collectors.toList());
return new RoleInfo(master, backups);
}
use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class CoordinationManager method eventuallyConsistentMapBuilder.
@Override
public <K, V> EventuallyConsistentMapBuilder<K, V> eventuallyConsistentMapBuilder() {
checkPermission(STORAGE_WRITE);
final NodeId localNodeId = clusterService.getLocalNode().id();
Supplier<List<NodeId>> peersSupplier = () -> clusterService.getNodes().stream().map(ControllerNode::id).filter(nodeId -> !nodeId.equals(localNodeId)).filter(id -> clusterService.getState(id).isActive()).collect(Collectors.toList());
Supplier<List<NodeId>> bootstrapPeersSupplier = () -> clusterService.getNodes().stream().map(ControllerNode::id).filter(id -> !localNodeId.equals(id)).filter(id -> clusterService.getState(id).isActive()).collect(Collectors.toList());
return new EventuallyConsistentMapBuilderImpl<>(localNodeId, clusterCommunicator, persistenceService, peersSupplier, bootstrapPeersSupplier);
}
use of org.onosproject.cluster.NodeId in project onos by opennetworkinglab.
the class EventuallyConsistentMapImpl method antiEntropyCheckLocalItems.
/**
* Processes anti-entropy ad from peer by taking following actions:
* 1. If peer has an old entry, updates peer.
* 2. If peer indicates an entry is removed and has a more recent
* timestamp than the local entry, update local state.
*/
private List<EventuallyConsistentMapEvent<K, V>> antiEntropyCheckLocalItems(AntiEntropyAdvertisement<K> ad) {
final List<EventuallyConsistentMapEvent<K, V>> externalEvents = Lists.newLinkedList();
final NodeId sender = ad.sender();
final List<NodeId> peers = ImmutableList.of(sender);
Set<K> staleOrMissing = new HashSet<>();
Set<K> locallyUnknown = new HashSet<>(ad.digest().keySet());
items.forEach((key, localValue) -> {
locallyUnknown.remove(key);
MapValue.Digest remoteValueDigest = ad.digest().get(key);
if (remoteValueDigest == null || localValue.isNewerThan(remoteValueDigest.timestamp())) {
// local value is more recent, push to sender
queueUpdate(new UpdateEntry<>(key, localValue), peers);
} else if (remoteValueDigest.isNewerThan(localValue.digest()) && remoteValueDigest.isTombstone()) {
// remote value is more recent and a tombstone: update local value
MapValue<V> tombstone = MapValue.tombstone(remoteValueDigest.timestamp());
MapValue<V> previousValue = removeInternal(key, Optional.empty(), Optional.of(tombstone));
if (previousValue != null && previousValue.isAlive()) {
externalEvents.add(new EventuallyConsistentMapEvent<>(mapName, REMOVE, key, previousValue.get()));
}
} else if (remoteValueDigest.isNewerThan(localValue.digest())) {
// Not a tombstone and remote is newer
staleOrMissing.add(key);
}
});
// Keys missing in local map
staleOrMissing.addAll(locallyUnknown);
// Request updates that we missed out on
sendUpdateRequestToPeer(sender, staleOrMissing);
return externalEvents;
}
Aggregations