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.HashMap;
import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
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.messages.SuccessReply;
import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
+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;
private void onRegisterCandidateLocal(final RegisterCandidateLocal registerCandidate) {
LOG.debug("{}: onRegisterCandidateLocal: {}", persistenceId(), registerCandidate);
- listenerSupport.setHasCandidateForEntity(registerCandidate.getEntity());
-
NormalizedNode<?, ?> entityOwners = entityOwnersWithCandidate(registerCandidate.getEntity().getType(),
registerCandidate.getEntity().getIdentifier(), localMemberName.getName());
commitCoordinator.commitModification(new MergeModification(ENTITY_OWNERS_PATH, entityOwners), this);
LOG.debug("{}: onUnregisterCandidateLocal: {}", persistenceId(), unregisterCandidate);
DOMEntity entity = unregisterCandidate.getEntity();
- listenerSupport.unsetHasCandidateForEntity(entity);
-
YangInstanceIdentifier candidatePath = candidatePath(entity.getType(), entity.getIdentifier(),
localMemberName.getName());
commitCoordinator.commitModification(new DeleteModification(candidatePath), this);
getSender().tell(SuccessReply.INSTANCE, getSelf());
searchForEntities((entityTypeNode, entityNode) -> {
- Optional<DataContainerChild<?, ?>> possibleType = entityTypeNode.getChild(ENTITY_TYPE_NODE_ID);
+ java.util.Optional<DataContainerChild<?, ?>> possibleType = entityTypeNode.getChild(ENTITY_TYPE_NODE_ID);
String entityType = possibleType.isPresent() ? possibleType.get().getValue().toString() : null;
if (registerListener.getEntityType().equals(entityType)) {
final boolean hasOwner;
final boolean isOwner;
- Optional<DataContainerChild<?, ?>> possibleOwner = entityNode.getChild(ENTITY_OWNER_NODE_ID);
+ java.util.Optional<DataContainerChild<?, ?>> possibleOwner = entityNode.getChild(ENTITY_OWNER_NODE_ID);
if (possibleOwner.isPresent()) {
isOwner = localMemberName.getName().equals(possibleOwner.get().getValue().toString());
hasOwner = true;
private void notifyAllListeners() {
searchForEntities((entityTypeNode, entityNode) -> {
- Optional<DataContainerChild<?, ?>> possibleType = entityTypeNode.getChild(ENTITY_TYPE_NODE_ID);
+ java.util.Optional<DataContainerChild<?, ?>> possibleType = entityTypeNode.getChild(ENTITY_TYPE_NODE_ID);
if (possibleType.isPresent()) {
final boolean hasOwner;
final boolean isOwner;
- Optional<DataContainerChild<?, ?>> possibleOwner = entityNode.getChild(ENTITY_OWNER_NODE_ID);
+ java.util.Optional<DataContainerChild<?, ?>> possibleOwner = entityNode.getChild(ENTITY_OWNER_NODE_ID);
if (possibleOwner.isPresent()) {
isOwner = localMemberName.getName().equals(possibleOwner.get().getValue().toString());
hasOwner = true;
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();
+
+ java.util.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();
if (!cluster.isPresent()) {
}
private void selectNewOwnerForEntitiesOwnedBy(final Set<String> ownedBy) {
- final BatchedModifications modifications = commitCoordinator.newBatchedModifications();
+ final List<Modification> modifications = new ArrayList<>();
searchForEntitiesOwnedBy(ownedBy, (entityTypeNode, entityNode) -> {
YangInstanceIdentifier entityPath = YangInstanceIdentifier.builder(ENTITY_TYPES_PATH)
.node(entityTypeNode.getIdentifier()).node(ENTITY_NODE_ID).node(entityNode.getIdentifier())
if (!newOwner.isEmpty()) {
LOG.debug("{}: Found entity {}, writing new owner {}", persistenceId(), entityPath, newOwner);
- modifications.addModification(new WriteModification(entityPath,
- ImmutableNodes.leafNode(ENTITY_OWNER_NODE_ID, newOwner)));
+ modifications.add(new WriteModification(entityPath,
+ ImmutableNodes.leafNode(ENTITY_OWNER_NODE_ID, newOwner)));
} 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) {
LOG.debug("{}: Searching for entities owned by {}", persistenceId(), ownedBy);
searchForEntities((entityTypeNode, entityNode) -> {
- Optional<DataContainerChild<? extends PathArgument, ?>> possibleOwner =
+ java.util.Optional<DataContainerChild<? extends PathArgument, ?>> possibleOwner =
entityNode.getChild(ENTITY_OWNER_NODE_ID);
String currentOwner = possibleOwner.isPresent() ? possibleOwner.get().getValue().toString() : "";
if (ownedBy.contains(currentOwner)) {
}
private void removeCandidateFromEntities(final MemberName member) {
- final BatchedModifications modifications = commitCoordinator.newBatchedModifications();
+ final List<Modification> modifications = new ArrayList<>();
searchForEntities((entityTypeNode, entityNode) -> {
if (hasCandidate(entityNode, member)) {
YangInstanceIdentifier entityId =
LOG.info("{}: Found entity {}, removing candidate {}, path {}", persistenceId(), entityId,
member, candidatePath);
- modifications.addModification(new DeleteModification(candidatePath));
+ modifications.add(new DeleteModification(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();
}
}
for (MapEntryNode entityType: ((MapNode) possibleEntityTypes.get()).getValue()) {
- Optional<DataContainerChild<?, ?>> possibleEntities = entityType.getChild(ENTITY_NODE_ID);
+ java.util.Optional<DataContainerChild<?, ?>> possibleEntities = entityType.getChild(ENTITY_NODE_ID);
if (!possibleEntities.isPresent()) {
// shouldn't happen but handle anyway
continue;
}
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);
}
}