use of org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded in project controller by opendaylight.
the class CandidateListChangeListener method onDataTreeChanged.
@Override
public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
for (DataTreeCandidate change : changes) {
DataTreeCandidateNode changeRoot = change.getRootNode();
ModificationType type = changeRoot.getModificationType();
LOG.debug("{}: Candidate node changed: {}, {}", logId, type, change.getRootPath());
NodeIdentifierWithPredicates candidateKey = (NodeIdentifierWithPredicates) change.getRootPath().getLastPathArgument();
String candidate = candidateKey.getKeyValues().get(CANDIDATE_NAME_QNAME).toString();
YangInstanceIdentifier entityId = extractEntityPath(change.getRootPath());
if (type == ModificationType.WRITE || type == ModificationType.APPEARED) {
LOG.debug("{}: Candidate {} was added for entity {}", logId, candidate, entityId);
Collection<String> newCandidates = addToCurrentCandidates(entityId, candidate);
shard.tell(new CandidateAdded(entityId, candidate, new ArrayList<>(newCandidates)), shard);
} else if (type == ModificationType.DELETE || type == ModificationType.DISAPPEARED) {
LOG.debug("{}: Candidate {} was removed for entity {}", logId, candidate, entityId);
Collection<String> newCandidates = removeFromCurrentCandidates(entityId, candidate);
shard.tell(new CandidateRemoved(entityId, candidate, new ArrayList<>(newCandidates)), shard);
}
}
}
use of org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded in project controller by opendaylight.
the class EntityOwnershipShardTest method testLeaderIsolationWithPendingCandidateAdded.
@Test
public void testLeaderIsolationWithPendingCandidateAdded() throws Exception {
testLog.info("testLeaderIsolationWithPendingCandidateAdded starting");
final ShardTestKit kit = new ShardTestKit(getSystem());
ShardIdentifier leaderId = newShardId(LOCAL_MEMBER_NAME);
ShardIdentifier peerId1 = newShardId(PEER_MEMBER_1_NAME);
ShardIdentifier peerId2 = newShardId(PEER_MEMBER_2_NAME);
dataStoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(4).shardIsolatedLeaderCheckIntervalInMillis(100000);
TestActorRef<TestEntityOwnershipShard> peer1 = actorFactory.createTestActor(TestEntityOwnershipShard.props(newShardBuilder(peerId1, peerMap(leaderId.toString(), peerId2.toString()), PEER_MEMBER_1_NAME), actorFactory.createActor(MessageCollectorActor.props())), peerId1.toString());
peer1.underlyingActor().startDroppingMessagesOfType(ElectionTimeout.class);
TestActorRef<TestEntityOwnershipShard> peer2 = actorFactory.createTestActor(TestEntityOwnershipShard.props(newShardBuilder(peerId2, peerMap(leaderId.toString(), peerId1.toString()), PEER_MEMBER_2_NAME), actorFactory.createTestActor(MessageCollectorActor.props())), peerId2.toString());
peer2.underlyingActor().startDroppingMessagesOfType(ElectionTimeout.class);
dataStoreContextBuilder = DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build()).shardIsolatedLeaderCheckIntervalInMillis(500);
TestActorRef<TestEntityOwnershipShard> leader = actorFactory.createTestActor(TestEntityOwnershipShard.props(newShardBuilder(leaderId, peerMap(peerId1.toString(), peerId2.toString()), LOCAL_MEMBER_NAME), actorFactory.createTestActor(MessageCollectorActor.props())), leaderId.toString());
ShardTestKit.waitUntilLeader(leader);
// Add listeners on all members
DOMEntityOwnershipListener leaderListener = mock(DOMEntityOwnershipListener.class, "DOMEntityOwnershipListener-" + LOCAL_MEMBER_NAME);
leader.tell(new RegisterListenerLocal(leaderListener, ENTITY_TYPE), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
DOMEntityOwnershipListener peer1Listener = mock(DOMEntityOwnershipListener.class, "DOMEntityOwnershipListener-" + PEER_MEMBER_1_NAME);
peer1.tell(new RegisterListenerLocal(peer1Listener, ENTITY_TYPE), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
DOMEntityOwnershipListener peer2Listener = mock(DOMEntityOwnershipListener.class, "DOMEntityOwnershipListener-" + PEER_MEMBER_2_NAME);
peer2.tell(new RegisterListenerLocal(peer2Listener, ENTITY_TYPE), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
// Drop the CandidateAdded message to the leader for now.
leader.underlyingActor().startDroppingMessagesOfType(CandidateAdded.class);
// Add an entity candidates for the leader. Since we've blocked the CandidateAdded message, it won't be
// assigned the owner.
DOMEntity entity1 = new DOMEntity(ENTITY_TYPE, ENTITY_ID1);
leader.tell(new RegisterCandidateLocal(entity1), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
verifyCommittedEntityCandidate(leader, entity1.getType(), entity1.getIdentifier(), LOCAL_MEMBER_NAME);
verifyCommittedEntityCandidate(peer1, entity1.getType(), entity1.getIdentifier(), LOCAL_MEMBER_NAME);
verifyCommittedEntityCandidate(peer2, entity1.getType(), entity1.getIdentifier(), LOCAL_MEMBER_NAME);
DOMEntity entity2 = new DOMEntity(ENTITY_TYPE, ENTITY_ID2);
leader.tell(new RegisterCandidateLocal(entity2), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
verifyCommittedEntityCandidate(leader, entity2.getType(), entity2.getIdentifier(), LOCAL_MEMBER_NAME);
verifyCommittedEntityCandidate(peer1, entity2.getType(), entity2.getIdentifier(), LOCAL_MEMBER_NAME);
verifyCommittedEntityCandidate(peer2, entity2.getType(), entity2.getIdentifier(), LOCAL_MEMBER_NAME);
// Capture the CandidateAdded messages.
final List<CandidateAdded> candidateAdded = expectMatching(leader.underlyingActor().collectorActor(), CandidateAdded.class, 2);
// Drop AppendEntries to the followers containing a log entry, which will be for the owner writes after we
// forward the CandidateAdded messages to the leader. This will leave the pending owner write tx's uncommitted.
peer1.underlyingActor().startDroppingMessagesOfType(AppendEntries.class, ae -> ae.getEntries().size() > 0);
peer2.underlyingActor().startDroppingMessagesOfType(AppendEntries.class, ae -> ae.getEntries().size() > 0);
// Now forward the CandidateAdded messages to the leader and wait for it to send out the AppendEntries.
leader.underlyingActor().stopDroppingMessagesOfType(CandidateAdded.class);
leader.tell(candidateAdded.get(0), leader);
leader.tell(candidateAdded.get(1), leader);
expectMatching(peer1.underlyingActor().collectorActor(), AppendEntries.class, 2, ae -> ae.getEntries().size() > 0);
// Verify no owner assigned.
verifyNoOwnerSet(leader, entity1.getType(), entity1.getIdentifier());
verifyNoOwnerSet(leader, entity2.getType(), entity2.getIdentifier());
// Isolate the leader by dropping AppendEntries to the followers and incoming messages from the followers.
leader.underlyingActor().startDroppingMessagesOfType(RequestVote.class);
leader.underlyingActor().startDroppingMessagesOfType(AppendEntries.class);
peer2.underlyingActor().startDroppingMessagesOfType(AppendEntries.class, ae -> ae.getLeaderId().equals(leaderId.toString()));
peer1.underlyingActor().startDroppingMessagesOfType(AppendEntries.class);
// Send PeerDown to the isolated leader - should be no-op since there's no owned entities.
leader.tell(new PeerDown(peerId1.getMemberName(), peerId1.toString()), ActorRef.noSender());
leader.tell(new PeerDown(peerId2.getMemberName(), peerId2.toString()), ActorRef.noSender());
// Verify the leader transitions to IsolatedLeader.
verifyRaftState(leader, state -> assertEquals("getRaftState", RaftState.IsolatedLeader.toString(), state.getRaftState()));
// Send PeerDown to the new leader peer1.
peer1.tell(new PeerDown(leaderId.getMemberName(), leaderId.toString()), ActorRef.noSender());
// Make peer1 start an election and become leader by sending the TimeoutNow message.
peer1.tell(TimeoutNow.INSTANCE, ActorRef.noSender());
// Verify the peer1 transitions to Leader.
verifyRaftState(peer1, state -> assertEquals("getRaftState", RaftState.Leader.toString(), state.getRaftState()));
verifyNoOwnerSet(peer1, entity1.getType(), entity1.getIdentifier());
verifyNoOwnerSet(peer2, entity1.getType(), entity2.getIdentifier());
verifyNoMoreInteractions(peer1Listener);
verifyNoMoreInteractions(peer2Listener);
// Add candidate peer1 candidate for entity2.
peer1.tell(new RegisterCandidateLocal(entity2), kit.getRef());
verifyOwner(peer1, entity2.getType(), entity2.getIdentifier(), PEER_MEMBER_1_NAME);
verify(peer1Listener, timeout(5000)).ownershipChanged(ownershipChange(entity2, false, true, true));
verify(peer2Listener, timeout(5000)).ownershipChanged(ownershipChange(entity2, false, false, true));
reset(leaderListener, peer1Listener, peer2Listener);
// Remove the isolation.
leader.underlyingActor().stopDroppingMessagesOfType(RequestVote.class);
leader.underlyingActor().stopDroppingMessagesOfType(AppendEntries.class);
peer2.underlyingActor().stopDroppingMessagesOfType(AppendEntries.class);
peer1.underlyingActor().stopDroppingMessagesOfType(AppendEntries.class);
// Previous leader should switch to Follower.
verifyRaftState(leader, state -> assertEquals("getRaftState", RaftState.Follower.toString(), state.getRaftState()));
// Send PeerUp to peer1 and peer2.
peer1.tell(new PeerUp(leaderId.getMemberName(), leaderId.toString()), ActorRef.noSender());
peer2.tell(new PeerUp(leaderId.getMemberName(), leaderId.toString()), ActorRef.noSender());
// The previous leader should become the owner of entity1.
verifyOwner(leader, entity1.getType(), entity1.getIdentifier(), LOCAL_MEMBER_NAME);
// The previous leader's DOMEntityOwnershipListener should get 4 total notifications:
// - inJeopardy cleared for entity1 (wasOwner=false, isOwner=false, hasOwner=false, inJeopardy=false)
// - inJeopardy cleared for entity2 (wasOwner=false, isOwner=false, hasOwner=false, inJeopardy=false)
// - local owner granted for entity1 (wasOwner=false, isOwner=true, hasOwner=true, inJeopardy=false)
// - remote owner for entity2 (wasOwner=false, isOwner=false, hasOwner=true, inJeopardy=false)
verify(leaderListener, timeout(5000).times(4)).ownershipChanged(or(or(ownershipChange(entity1, false, false, false), ownershipChange(entity2, false, false, false)), or(ownershipChange(entity1, false, true, true), ownershipChange(entity2, false, false, true))));
verify(peer1Listener, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, false, true));
verify(peer2Listener, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, false, true));
// Verify entity2's owner doesn't change.
Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
verifyOwner(peer1, entity2.getType(), entity2.getIdentifier(), PEER_MEMBER_1_NAME);
verifyNoMoreInteractions(leaderListener);
verifyNoMoreInteractions(peer1Listener);
verifyNoMoreInteractions(peer2Listener);
testLog.info("testLeaderIsolationWithPendingCandidateAdded ending");
}
use of org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded in project controller by opendaylight.
the class CandidateListChangeListenerTest method testOnDataTreeChanged.
@Test
public void testOnDataTreeChanged() throws Exception {
TestKit kit = new TestKit(getSystem());
new CandidateListChangeListener(kit.getRef(), "test").init(shardDataTree);
String memberName1 = "member-1";
writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
CandidateAdded candidateAdded = kit.expectMsgClass(CandidateAdded.class);
assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
assertEquals("getAllCandidates", ImmutableSet.of(memberName1), ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
String memberName2 = "member-2";
writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName2));
candidateAdded = kit.expectMsgClass(CandidateAdded.class);
assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
assertEquals("getNewCandidate", memberName2, candidateAdded.getNewCandidate());
assertEquals("getAllCandidates", ImmutableSet.of(memberName1, memberName2), ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, memberName1));
candidateAdded = kit.expectMsgClass(CandidateAdded.class);
assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID2), candidateAdded.getEntityPath());
assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
assertEquals("getAllCandidates", ImmutableSet.of(memberName1), ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName1));
CandidateRemoved candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
assertEquals("getRemovedCandidate", memberName1, candidateRemoved.getRemovedCandidate());
assertEquals("getRemainingCandidates", ImmutableSet.of(memberName2), ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName2));
candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
assertEquals("getRemovedCandidate", memberName2, candidateRemoved.getRemovedCandidate());
assertEquals("getRemainingCandidates", ImmutableSet.of(), ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
}
Aggregations