2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.entityownership;
10 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.CANDIDATE_NODE_ID;
11 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_NODE_ID;
12 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
13 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_NODE_ID;
14 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
15 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNER_NODE_ID;
16 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNER_QNAME;
17 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_TYPES_PATH;
18 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_TYPE_NODE_ID;
19 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_TYPE_QNAME;
20 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.candidateMapEntry;
21 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.candidateNodeKey;
22 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.candidatePath;
23 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.createEntity;
24 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
25 import akka.actor.ActorRef;
26 import akka.actor.ActorSelection;
27 import akka.actor.Cancellable;
28 import akka.pattern.Patterns;
29 import com.google.common.base.Optional;
30 import com.google.common.base.Preconditions;
31 import com.google.common.base.Strings;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.HashMap;
35 import java.util.HashSet;
38 import java.util.concurrent.TimeUnit;
39 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
40 import org.opendaylight.controller.cluster.datastore.Shard;
41 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded;
42 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved;
43 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
44 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
45 import org.opendaylight.controller.cluster.datastore.entityownership.messages.SelectOwner;
46 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
47 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
48 import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategy;
49 import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig;
50 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
51 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
52 import org.opendaylight.controller.cluster.datastore.messages.PeerDown;
53 import org.opendaylight.controller.cluster.datastore.messages.PeerUp;
54 import org.opendaylight.controller.cluster.datastore.messages.SuccessReply;
55 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
56 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
57 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
58 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
60 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
61 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
62 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
64 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
65 import scala.concurrent.Future;
66 import scala.concurrent.duration.FiniteDuration;
69 * Special Shard for EntityOwnership.
71 * @author Thomas Pantelis
73 class EntityOwnershipShard extends Shard {
74 private final String localMemberName;
75 private final EntityOwnershipShardCommitCoordinator commitCoordinator;
76 private final EntityOwnershipListenerSupport listenerSupport;
77 private final Set<String> downPeerMemberNames = new HashSet<>();
78 private final Map<String, String> peerIdToMemberNames = new HashMap<>();
79 private final EntityOwnerSelectionStrategyConfig strategyConfig;
80 private final Map<YangInstanceIdentifier, Cancellable> entityToScheduledOwnershipTask = new HashMap<>();
81 private final EntityOwnershipStatistics entityOwnershipStatistics;
83 private static DatastoreContext noPersistenceDatastoreContext(DatastoreContext datastoreContext) {
84 return DatastoreContext.newBuilderFrom(datastoreContext).persistent(false).build();
87 protected EntityOwnershipShard(Builder builder) {
89 this.localMemberName = builder.localMemberName;
90 this.commitCoordinator = new EntityOwnershipShardCommitCoordinator(builder.localMemberName, LOG);
91 this.listenerSupport = new EntityOwnershipListenerSupport(getContext(), persistenceId());
92 this.strategyConfig = builder.ownerSelectionStrategyConfig;
93 this.entityOwnershipStatistics = new EntityOwnershipStatistics();
94 this.entityOwnershipStatistics.init(getDataStore());
96 for(String peerId: getRaftActorContext().getPeerIds()) {
97 ShardIdentifier shardId = ShardIdentifier.builder().fromShardIdString(peerId).build();
98 peerIdToMemberNames.put(peerId, shardId.getMemberName());
103 protected void onDatastoreContext(DatastoreContext context) {
104 super.onDatastoreContext(noPersistenceDatastoreContext(context));
108 protected void onRecoveryComplete() {
109 super.onRecoveryComplete();
111 new CandidateListChangeListener(getSelf(), persistenceId()).init(getDataStore());
112 new EntityOwnerChangeListener(localMemberName, listenerSupport).init(getDataStore());
116 public void onReceiveCommand(final Object message) throws Exception {
117 if(message instanceof RegisterCandidateLocal) {
118 onRegisterCandidateLocal((RegisterCandidateLocal) message);
119 } else if(message instanceof UnregisterCandidateLocal) {
120 onUnregisterCandidateLocal((UnregisterCandidateLocal)message);
121 } else if(message instanceof CandidateAdded){
122 onCandidateAdded((CandidateAdded) message);
123 } else if(message instanceof CandidateRemoved){
124 onCandidateRemoved((CandidateRemoved) message);
125 } else if(message instanceof PeerDown) {
126 onPeerDown((PeerDown) message);
127 } else if(message instanceof PeerUp) {
128 onPeerUp((PeerUp) message);
129 } else if(message instanceof RegisterListenerLocal) {
130 onRegisterListenerLocal((RegisterListenerLocal)message);
131 } else if(message instanceof UnregisterListenerLocal) {
132 onUnregisterListenerLocal((UnregisterListenerLocal) message);
133 } else if(message instanceof SelectOwner) {
134 onSelectOwner((SelectOwner) message);
135 } else if(!commitCoordinator.handleMessage(message, this)) {
136 super.onReceiveCommand(message);
140 private void onSelectOwner(SelectOwner selectOwner) {
141 LOG.debug("{}: onSelectOwner: {}", persistenceId(), selectOwner);
143 String currentOwner = getCurrentOwner(selectOwner.getEntityPath());
144 if(Strings.isNullOrEmpty(currentOwner)) {
145 String entityType = EntityOwnersModel.entityTypeFromEntityPath(selectOwner.getEntityPath());
146 writeNewOwner(selectOwner.getEntityPath(), newOwner(currentOwner, selectOwner.getAllCandidates(),
147 entityOwnershipStatistics.byEntityType(entityType),
148 selectOwner.getOwnerSelectionStrategy()));
150 Cancellable cancellable = entityToScheduledOwnershipTask.get(selectOwner.getEntityPath());
151 if(cancellable != null){
152 if(!cancellable.isCancelled()){
153 cancellable.cancel();
155 entityToScheduledOwnershipTask.remove(selectOwner.getEntityPath());
160 private void onRegisterCandidateLocal(RegisterCandidateLocal registerCandidate) {
161 LOG.debug("{}: onRegisterCandidateLocal: {}", persistenceId(), registerCandidate);
163 listenerSupport.setHasCandidateForEntity(registerCandidate.getEntity());
165 NormalizedNode<?, ?> entityOwners = entityOwnersWithCandidate(registerCandidate.getEntity().getType(),
166 registerCandidate.getEntity().getId(), localMemberName);
167 commitCoordinator.commitModification(new MergeModification(ENTITY_OWNERS_PATH, entityOwners), this);
169 getSender().tell(SuccessReply.INSTANCE, getSelf());
172 private void onUnregisterCandidateLocal(UnregisterCandidateLocal unregisterCandidate) {
173 LOG.debug("{}: onUnregisterCandidateLocal: {}", persistenceId(), unregisterCandidate);
175 Entity entity = unregisterCandidate.getEntity();
176 listenerSupport.unsetHasCandidateForEntity(entity);
178 YangInstanceIdentifier candidatePath = candidatePath(entity.getType(), entity.getId(), localMemberName);
179 commitCoordinator.commitModification(new DeleteModification(candidatePath), this);
181 getSender().tell(SuccessReply.INSTANCE, getSelf());
184 private void onRegisterListenerLocal(final RegisterListenerLocal registerListener) {
185 LOG.debug("{}: onRegisterListenerLocal: {}", persistenceId(), registerListener);
187 listenerSupport.addEntityOwnershipListener(registerListener.getEntityType(), registerListener.getListener());
189 getSender().tell(SuccessReply.INSTANCE, getSelf());
191 searchForEntities(new EntityWalker() {
193 public void onEntity(MapEntryNode entityTypeNode, MapEntryNode entityNode) {
194 Optional<DataContainerChild<?, ?>> possibleType = entityTypeNode.getChild(ENTITY_TYPE_NODE_ID);
195 String entityType = possibleType.isPresent() ? possibleType.get().getValue().toString() : null;
196 if (registerListener.getEntityType().equals(entityType)) {
197 final boolean hasOwner;
198 final boolean isOwner;
200 Optional<DataContainerChild<?, ?>> possibleOwner = entityNode.getChild(ENTITY_OWNER_NODE_ID);
201 if (possibleOwner.isPresent()) {
202 isOwner = localMemberName.equals(possibleOwner.get().getValue().toString());
209 Entity entity = new Entity(entityType,
210 (YangInstanceIdentifier) entityNode.getChild(ENTITY_ID_NODE_ID).get().getValue());
212 listenerSupport.notifyEntityOwnershipListener(entity, false, isOwner, hasOwner,
213 registerListener.getListener());
219 private void onUnregisterListenerLocal(UnregisterListenerLocal unregisterListener) {
220 LOG.debug("{}: onUnregisterListenerLocal: {}", persistenceId(), unregisterListener);
222 listenerSupport.removeEntityOwnershipListener(unregisterListener.getEntityType(), unregisterListener.getListener());
224 getSender().tell(SuccessReply.INSTANCE, getSelf());
227 void tryCommitModifications(final BatchedModifications modifications) {
229 LOG.debug("{}: Committing BatchedModifications {} locally", persistenceId(), modifications.getTransactionID());
231 // Note that it's possible the commit won't get consensus and will timeout and not be applied
232 // to the state. However we don't need to retry it in that case b/c it will be committed to
233 // the journal first and, once a majority of followers come back on line and it is replicated,
234 // it will be applied at that point.
235 handleBatchedModificationsLocal(modifications, self());
237 final ActorSelection leader = getLeader();
238 if (leader != null) {
239 if(LOG.isDebugEnabled()) {
240 LOG.debug("{}: Sending BatchedModifications {} to leader {}", persistenceId(),
241 modifications.getTransactionID(), leader);
244 Future<Object> future = Patterns.ask(leader, modifications, TimeUnit.SECONDS.toMillis(
245 getDatastoreContext().getShardTransactionCommitTimeoutInSeconds()));
247 Patterns.pipe(future, getContext().dispatcher()).pipeTo(getSelf(), ActorRef.noSender());
252 boolean hasLeader() {
253 return getLeader() != null && !isIsolatedLeader();
257 protected void onStateChanged() {
258 super.onStateChanged();
260 boolean isLeader = isLeader();
261 if(LOG.isDebugEnabled()) {
262 LOG.debug("{}: onStateChanged: isLeader: {}, hasLeader: {}", persistenceId(), isLeader, hasLeader());
265 commitCoordinator.onStateChanged(this, isLeader);
269 protected void onLeaderChanged(String oldLeader, String newLeader) {
270 super.onLeaderChanged(oldLeader, newLeader);
272 boolean isLeader = isLeader();
273 LOG.debug("{}: onLeaderChanged: oldLeader: {}, newLeader: {}, isLeader: {}", persistenceId(), oldLeader,
274 newLeader, isLeader);
278 // Clear all existing strategies so that they get re-created when we call createStrategy again
279 // This allows the strategies to be re-initialized with existing statistics maintained by
280 // EntityOwnershipStatistics
281 strategyConfig.clearStrategies();
282 // We were just elected leader. If the old leader is down, select new owners for the entities
283 // owned by the down leader.
285 String oldLeaderMemberName = peerIdToMemberNames.get(oldLeader);
287 LOG.debug("{}: oldLeaderMemberName: {}", persistenceId(), oldLeaderMemberName);
289 if(downPeerMemberNames.contains(oldLeaderMemberName)) {
290 removeCandidateFromEntities(oldLeaderMemberName);
293 // The leader changed - notify the coordinator to check if pending modifications need to be sent.
294 // While onStateChanged also does this, this method handles the case where the shard hears from a
295 // leader and stays in the follower state. In that case no behavior state change occurs.
296 commitCoordinator.onStateChanged(this, isLeader);
300 private void onCandidateRemoved(CandidateRemoved message) {
301 LOG.debug("{}: onCandidateRemoved: {}", persistenceId(), message);
304 String currentOwner = getCurrentOwner(message.getEntityPath());
305 if(message.getRemovedCandidate().equals(currentOwner) || message.getRemainingCandidates().size() == 0){
306 String entityType = EntityOwnersModel.entityTypeFromEntityPath(message.getEntityPath());
307 writeNewOwner(message.getEntityPath(),
308 newOwner(currentOwner, message.getRemainingCandidates(), entityOwnershipStatistics.byEntityType(entityType),
309 getEntityOwnerElectionStrategy(message.getEntityPath())));
312 // We're not the leader. If the removed candidate is our local member then check if we actually
313 // have a local candidate registered. If we do then we must have been partitioned from the leader
314 // and the leader removed our candidate since the leader can't tell the difference between a
315 // temporary network partition and a node's process actually restarted. So, in that case, re-add
317 if(localMemberName.equals(message.getRemovedCandidate()) &&
318 listenerSupport.hasCandidateForEntity(createEntity(message.getEntityPath()))) {
319 LOG.debug("Local candidate member was removed but a local candidate is registered for {}" +
320 " - adding back local candidate", message.getEntityPath());
322 commitCoordinator.commitModification(new MergeModification(
323 candidatePath(message.getEntityPath(), localMemberName),
324 candidateMapEntry(localMemberName)), this);
329 private EntityOwnerSelectionStrategy getEntityOwnerElectionStrategy(YangInstanceIdentifier entityPath) {
330 final String entityType = EntityOwnersModel.entityTypeFromEntityPath(entityPath);
331 return strategyConfig.createStrategy(entityType, entityOwnershipStatistics.byEntityType(entityType));
334 private void onCandidateAdded(CandidateAdded message) {
339 LOG.debug("{}: onCandidateAdded: {}", persistenceId(), message);
341 // Since a node's candidate member is only added by the node itself, we can assume the node is up so
342 // remove it from the downPeerMemberNames.
343 downPeerMemberNames.remove(message.getNewCandidate());
345 final String currentOwner = getCurrentOwner(message.getEntityPath());
346 final EntityOwnerSelectionStrategy strategy = getEntityOwnerElectionStrategy(message.getEntityPath());
347 final String entityType = EntityOwnersModel.entityTypeFromEntityPath(message.getEntityPath());
349 // Available members is all the known peers - the number of peers that are down + self
350 // So if there are 2 peers and 1 is down then availableMembers will be 2
351 final int availableMembers = (peerIdToMemberNames.size() - downPeerMemberNames.size()) + 1;
353 LOG.debug("{}: Using strategy {} to select owner", persistenceId(), strategy);
354 if(Strings.isNullOrEmpty(currentOwner)){
355 if(strategy.getSelectionDelayInMillis() == 0L) {
356 writeNewOwner(message.getEntityPath(), newOwner(currentOwner, message.getAllCandidates(),
357 entityOwnershipStatistics.byEntityType(entityType), strategy));
358 } else if(message.getAllCandidates().size() == availableMembers) {
359 LOG.debug("{}: Received the maximum candidates requests : {} writing new owner",
360 persistenceId(), availableMembers);
361 cancelOwnerSelectionTask(message.getEntityPath());
362 writeNewOwner(message.getEntityPath(), newOwner(currentOwner, message.getAllCandidates(),
363 entityOwnershipStatistics.byEntityType(entityType), strategy));
365 scheduleOwnerSelection(message.getEntityPath(), message.getAllCandidates(), strategy);
370 private void onPeerDown(PeerDown peerDown) {
371 LOG.info("{}: onPeerDown: {}", persistenceId(), peerDown);
373 String downMemberName = peerDown.getMemberName();
374 if(downPeerMemberNames.add(downMemberName) && isLeader()) {
375 // Remove the down peer as a candidate from all entities.
376 removeCandidateFromEntities(downMemberName);
380 private void onPeerUp(PeerUp peerUp) {
381 LOG.debug("{}: onPeerUp: {}", persistenceId(), peerUp);
383 peerIdToMemberNames.put(peerUp.getPeerId(), peerUp.getMemberName());
384 downPeerMemberNames.remove(peerUp.getMemberName());
386 // Notify the coordinator to check if pending modifications need to be sent. We do this here
387 // to handle the case where the leader's peer address isn't now yet when a prior state or
388 // leader change occurred.
389 commitCoordinator.onStateChanged(this, isLeader());
392 private void removeCandidateFromEntities(final String owner) {
393 final BatchedModifications modifications = commitCoordinator.newBatchedModifications();
394 searchForEntities(new EntityWalker() {
396 public void onEntity(MapEntryNode entityTypeNode, MapEntryNode entityNode) {
397 if (hasCandidate(entityNode, owner)) {
398 YangInstanceIdentifier entityId =
399 (YangInstanceIdentifier) entityNode.getIdentifier().getKeyValues().get(ENTITY_ID_QNAME);
400 YangInstanceIdentifier candidatePath = candidatePath(
401 entityTypeNode.getIdentifier().getKeyValues().get(ENTITY_TYPE_QNAME).toString(),
404 LOG.info("{}: Found entity {}, removing candidate {}, path {}", persistenceId(), entityId,
405 owner, candidatePath);
407 modifications.addModification(new DeleteModification(candidatePath));
412 commitCoordinator.commitModifications(modifications, this);
415 private static boolean hasCandidate(MapEntryNode entity, String candidateName) {
416 return ((MapNode)entity.getChild(CANDIDATE_NODE_ID).get()).getChild(candidateNodeKey(candidateName)).isPresent();
419 private void searchForEntities(EntityWalker walker) {
420 Optional<NormalizedNode<?, ?>> possibleEntityTypes = getDataStore().readNode(ENTITY_TYPES_PATH);
421 if(!possibleEntityTypes.isPresent()) {
425 for(MapEntryNode entityType: ((MapNode) possibleEntityTypes.get()).getValue()) {
426 Optional<DataContainerChild<?, ?>> possibleEntities = entityType.getChild(ENTITY_NODE_ID);
427 if(!possibleEntities.isPresent()) {
428 // shouldn't happen but handle anyway
432 for(MapEntryNode entity: ((MapNode) possibleEntities.get()).getValue()) {
433 walker.onEntity(entityType, entity);
438 private void writeNewOwner(YangInstanceIdentifier entityPath, String newOwner) {
439 LOG.debug("{}: Writing new owner {} for entity {}", persistenceId(), newOwner, entityPath);
441 commitCoordinator.commitModification(new WriteModification(entityPath.node(ENTITY_OWNER_QNAME),
442 ImmutableNodes.leafNode(ENTITY_OWNER_NODE_ID, newOwner)), this);
446 * Schedule a new owner selection job. Cancelling any outstanding job if it has not been cancelled.
449 * @param allCandidates
451 public void scheduleOwnerSelection(YangInstanceIdentifier entityPath, Collection<String> allCandidates,
452 EntityOwnerSelectionStrategy strategy){
453 cancelOwnerSelectionTask(entityPath);
455 LOG.debug("{}: Scheduling owner selection after {} ms", persistenceId(), strategy.getSelectionDelayInMillis());
457 final Cancellable lastScheduledTask = context().system().scheduler().scheduleOnce(
458 FiniteDuration.apply(strategy.getSelectionDelayInMillis(), TimeUnit.MILLISECONDS)
459 , self(), new SelectOwner(entityPath, allCandidates, strategy)
460 , context().system().dispatcher(), self());
462 entityToScheduledOwnershipTask.put(entityPath, lastScheduledTask);
465 private void cancelOwnerSelectionTask(YangInstanceIdentifier entityPath){
466 final Cancellable lastScheduledTask = entityToScheduledOwnershipTask.get(entityPath);
467 if(lastScheduledTask != null && !lastScheduledTask.isCancelled()){
468 lastScheduledTask.cancel();
472 private String newOwner(String currentOwner, Collection<String> candidates, Map<String, Long> statistics, EntityOwnerSelectionStrategy ownerSelectionStrategy) {
473 Collection<String> viableCandidates = getViableCandidates(candidates);
474 if(viableCandidates.size() == 0){
477 return ownerSelectionStrategy.newOwner(currentOwner, viableCandidates);
480 private Collection<String> getViableCandidates(Collection<String> candidates) {
481 Collection<String> viableCandidates = new ArrayList<>();
483 for (String candidate : candidates) {
484 if (!downPeerMemberNames.contains(candidate)) {
485 viableCandidates.add(candidate);
488 return viableCandidates;
491 private String getCurrentOwner(YangInstanceIdentifier entityId) {
492 Optional<NormalizedNode<?, ?>> optionalEntityOwner = getDataStore().readNode(entityId.node(ENTITY_OWNER_QNAME));
493 if(optionalEntityOwner.isPresent()){
494 return optionalEntityOwner.get().getValue().toString();
499 private static interface EntityWalker {
500 void onEntity(MapEntryNode entityTypeNode, MapEntryNode entityNode);
503 public static Builder newBuilder() {
504 return new Builder();
507 static class Builder extends Shard.AbstractBuilder<Builder, EntityOwnershipShard> {
508 private String localMemberName;
509 private EntityOwnerSelectionStrategyConfig ownerSelectionStrategyConfig;
511 protected Builder() {
512 super(EntityOwnershipShard.class);
515 Builder localMemberName(String localMemberName) {
517 this.localMemberName = localMemberName;
521 Builder ownerSelectionStrategyConfig(EntityOwnerSelectionStrategyConfig ownerSelectionStrategyConfig){
523 this.ownerSelectionStrategyConfig = ownerSelectionStrategyConfig;
528 protected void verify() {
530 Preconditions.checkNotNull(localMemberName, "localMemberName should not be null");
531 Preconditions.checkNotNull(ownerSelectionStrategyConfig, "ownerSelectionStrategyConfig should not be null");