import akka.cluster.Member;
import akka.cluster.MemberStatus;
import akka.pattern.Patterns;
-import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.opendaylight.controller.cluster.access.concepts.MemberName;
import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategy;
import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig;
+import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
import org.opendaylight.controller.cluster.datastore.messages.PeerDown;
import org.opendaylight.controller.cluster.datastore.messages.PeerUp;
import org.opendaylight.controller.cluster.datastore.modification.Modification;
import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
import org.opendaylight.controller.cluster.raft.RaftState;
+import org.opendaylight.controller.cluster.raft.VotingState;
import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
super.onLeaderChanged(oldLeader, newLeader);
}
+ @Override
+ protected void onVotingStateChangeComplete() {
+ // Re-evaluate ownership for all entities - if a member changed from voting to non-voting it should lose
+ // ownership and vice versa it now is a candidate to become owner.
+ final List<Modification> modifications = new ArrayList<>();
+ searchForEntities((entityTypeNode, entityNode) -> {
+ YangInstanceIdentifier entityPath = YangInstanceIdentifier.builder(ENTITY_TYPES_PATH)
+ .node(entityTypeNode.getIdentifier()).node(ENTITY_NODE_ID).node(entityNode.getIdentifier())
+ .node(ENTITY_OWNER_NODE_ID).build();
+
+ Optional<String> possibleOwner =
+ entityNode.getChild(ENTITY_OWNER_NODE_ID).map(node -> node.getValue().toString());
+ String newOwner = newOwner(possibleOwner.orElse(null), getCandidateNames(entityNode),
+ getEntityOwnerElectionStrategy(entityPath));
+
+ if (!newOwner.equals(possibleOwner.orElse(""))) {
+ modifications.add(new WriteModification(entityPath,
+ ImmutableNodes.leafNode(ENTITY_OWNER_NODE_ID, newOwner)));
+ }
+ });
+
+ commitCoordinator.commitModifications(modifications, this);
+ }
+
private void initializeDownPeerMemberNamesFromClusterState() {
- java.util.Optional<Cluster> cluster = getRaftActorContext().getCluster();
+ Optional<Cluster> cluster = getRaftActorContext().getCluster();
if (!cluster.isPresent()) {
return;
}
} else {
LOG.debug("{}: Found entity {} but no other candidates - not clearing owner", persistenceId(),
- entityPath, newOwner);
+ entityPath);
}
});
}
private static Collection<String> getCandidateNames(final MapEntryNode entity) {
- Collection<MapEntryNode> candidates = ((MapNode)entity.getChild(CANDIDATE_NODE_ID).get()).getValue();
- Collection<String> candidateNames = new ArrayList<>(candidates.size());
- for (MapEntryNode candidate: candidates) {
- candidateNames.add(candidate.getChild(CANDIDATE_NAME_NODE_ID).get().getValue().toString());
- }
-
- return candidateNames;
+ return entity.getChild(CANDIDATE_NODE_ID).map(child -> {
+ Collection<MapEntryNode> candidates = ((MapNode) child).getValue();
+ Collection<String> candidateNames = new ArrayList<>(candidates.size());
+ for (MapEntryNode candidate: candidates) {
+ candidateNames.add(candidate.getChild(CANDIDATE_NAME_NODE_ID).get().getValue().toString());
+ }
+ return candidateNames;
+ }).orElse(ImmutableList.of());
}
private void searchForEntitiesOwnedBy(final Set<String> ownedBy, final EntityWalker walker) {
final List<Modification> modifications = new ArrayList<>();
searchForEntities((entityTypeNode, entityNode) -> {
if (hasCandidate(entityNode, member)) {
- YangInstanceIdentifier entityId =
- (YangInstanceIdentifier) entityNode.getIdentifier().getKeyValues().get(ENTITY_ID_QNAME);
- YangInstanceIdentifier candidatePath = candidatePath(
- entityTypeNode.getIdentifier().getKeyValues().get(ENTITY_TYPE_QNAME).toString(),
- entityId, member.getName());
+ YangInstanceIdentifier entityId = (YangInstanceIdentifier) entityNode.getIdentifier()
+ .getValue(ENTITY_ID_QNAME);
+ YangInstanceIdentifier candidatePath = candidatePath(entityTypeNode.getIdentifier()
+ .getValue(ENTITY_TYPE_QNAME).toString(), entityId, member.getName());
LOG.info("{}: Found entity {}, removing candidate {}, path {}", persistenceId(), entityId,
member, candidatePath);
}
private static boolean hasCandidate(final MapEntryNode entity, final MemberName candidateName) {
- return ((MapNode)entity.getChild(CANDIDATE_NODE_ID).get()).getChild(candidateNodeKey(candidateName.getName()))
+ return entity.getChild(CANDIDATE_NODE_ID)
+ .flatMap(child -> ((MapNode)child).getChild(candidateNodeKey(candidateName.getName())))
.isPresent();
}
return;
}
- for (MapEntryNode entityType: ((MapNode) possibleEntityTypes.get()).getValue()) {
+ for (MapEntryNode entityType : ((MapNode) possibleEntityTypes.get()).getValue()) {
Optional<DataContainerChild<?, ?>> possibleEntities = entityType.getChild(ENTITY_NODE_ID);
if (!possibleEntities.isPresent()) {
// shouldn't happen but handle anyway
}
private Collection<String> getViableCandidates(final Collection<String> candidates) {
+ Map<MemberName, VotingState> memberToVotingState = new HashMap<>();
+ getRaftActorContext().getPeers().forEach(peerInfo -> memberToVotingState.put(
+ ShardIdentifier.fromShardIdString(peerInfo.getId()).getMemberName(), peerInfo.getVotingState()));
+
Collection<String> viableCandidates = new ArrayList<>();
for (String candidate : candidates) {
- if (!downPeerMemberNames.contains(MemberName.forName(candidate))) {
+ MemberName memberName = MemberName.forName(candidate);
+ if (memberToVotingState.get(memberName) != VotingState.NON_VOTING
+ && !downPeerMemberNames.contains(memberName)) {
viableCandidates.add(candidate);
}
}
}
private String getCurrentOwner(final YangInstanceIdentifier entityId) {
- Optional<NormalizedNode<?, ?>> optionalEntityOwner = getDataStore().readNode(entityId.node(ENTITY_OWNER_QNAME));
- if (optionalEntityOwner.isPresent()) {
- return optionalEntityOwner.get().getValue().toString();
- }
- return null;
+ return getDataStore().readNode(entityId.node(ENTITY_OWNER_QNAME))
+ .map(owner -> owner.getValue().toString())
+ .orElse(null);
}
@FunctionalInterface