use of org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener in project controller by opendaylight.
the class EntityOwnershipListenerActorTest method testOnEntityOwnershipChangedWithListenerEx.
@Test
public void testOnEntityOwnershipChangedWithListenerEx() {
DOMEntityOwnershipListener mockListener = mock(DOMEntityOwnershipListener.class);
DOMEntity entity1 = new DOMEntity("test", YangInstanceIdentifier.of(QName.create("test", "id1")));
doThrow(new RuntimeException("mock")).when(mockListener).ownershipChanged(ownershipChange(entity1, false, true, true));
DOMEntity entity2 = new DOMEntity("test", YangInstanceIdentifier.of(QName.create("test", "id2")));
doNothing().when(mockListener).ownershipChanged(ownershipChange(entity2, true, false, false));
TestActorRef<EntityOwnershipListenerActor> listenerActor = actorFactory.createTestActor(EntityOwnershipListenerActor.props(mockListener), actorFactory.generateActorId("listener"));
listenerActor.tell(new DOMEntityOwnershipChange(entity1, EntityOwnershipChangeState.from(false, true, true)), ActorRef.noSender());
listenerActor.tell(new DOMEntityOwnershipChange(entity2, EntityOwnershipChangeState.from(true, false, false)), ActorRef.noSender());
verify(mockListener, timeout(5000)).ownershipChanged(ownershipChange(entity2, true, false, false));
}
use of org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener in project controller by opendaylight.
the class EntityOwnershipListenerSupportTest method testNotifyEntityOwnershipListeners.
@Test
public void testNotifyEntityOwnershipListeners() {
EntityOwnershipListenerSupport support = new EntityOwnershipListenerSupport(actorContext, "test");
DOMEntityOwnershipListener mockListener1 = mock(DOMEntityOwnershipListener.class, "EntityOwnershipListener1");
DOMEntityOwnershipListener mockListener2 = mock(DOMEntityOwnershipListener.class, "EntityOwnershipListener2");
DOMEntityOwnershipListener mockListener12 = mock(DOMEntityOwnershipListener.class, "EntityOwnershipListener1_2");
String entityType1 = "type1";
String entityType2 = "type2";
final DOMEntity entity1 = new DOMEntity(entityType1, YangInstanceIdentifier.of(QName.create("test", "id1")));
final DOMEntity entity2 = new DOMEntity(entityType2, YangInstanceIdentifier.of(QName.create("test", "id2")));
final DOMEntity entity3 = new DOMEntity("noListener", YangInstanceIdentifier.of(QName.create("test", "id5")));
// Add EntityOwnershipListener registrations.
support.addEntityOwnershipListener(entityType1, mockListener1);
// register again - should be noop
support.addEntityOwnershipListener(entityType1, mockListener1);
support.addEntityOwnershipListener(entityType1, mockListener12);
support.addEntityOwnershipListener(entityType2, mockListener2);
// Notify entity1 changed and verify appropriate listeners are notified.
support.notifyEntityOwnershipListeners(entity1, false, true, true);
verify(mockListener1, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, true, true));
verify(mockListener12, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, true, true));
Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
verify(mockListener2, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
assertEquals("# of listener actors", 2, actorContext.children().size());
reset(mockListener1, mockListener2, mockListener12);
// Notify entity2 changed and verify appropriate listeners are notified.
support.notifyEntityOwnershipListeners(entity2, false, true, true);
verify(mockListener2, timeout(5000)).ownershipChanged(ownershipChange(entity2, false, true, true));
Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
verify(mockListener1, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
verify(mockListener12, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
assertEquals("# of listener actors", 3, actorContext.children().size());
reset(mockListener1, mockListener2, mockListener12);
// Notify entity3 changed and verify no listeners are notified.
support.notifyEntityOwnershipListeners(entity3, true, false, true);
Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
verify(mockListener1, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
verify(mockListener2, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
verify(mockListener12, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
reset(mockListener1, mockListener2, mockListener12);
Iterable<ActorRef> listenerActors = actorContext.children();
assertEquals("# of listener actors", 3, listenerActors.size());
// Unregister mockListener1, issue a change for entity1 and verify only remaining listeners are notified.
support.removeEntityOwnershipListener(entityType1, mockListener1);
support.notifyEntityOwnershipListeners(entity1, true, false, true);
verify(mockListener12, timeout(5000)).ownershipChanged(ownershipChange(entity1, true, false, true));
Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
verify(mockListener1, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
reset(mockListener1, mockListener2, mockListener12);
// Unregister all listeners and verify their listener actors are destroyed.
List<TestKit> watchers = new ArrayList<>();
for (Iterator<ActorRef> iter = listenerActors.iterator(); iter.hasNext(); ) {
TestKit kit = new TestKit(getSystem());
kit.watch(iter.next());
watchers.add(kit);
}
support.removeEntityOwnershipListener(entityType1, mockListener12);
// un-register again - should be noop
support.removeEntityOwnershipListener(entityType1, mockListener12);
support.removeEntityOwnershipListener(entityType2, mockListener2);
Iterator<ActorRef> iter = listenerActors.iterator();
for (TestKit kit : watchers) {
kit.expectTerminated(kit.duration("3 seconds"), iter.next());
}
assertEquals("# of listener actors", 0, actorContext.children().size());
// Re-register mockListener1 and verify it is notified.
reset(mockListener1, mockListener2);
support.addEntityOwnershipListener(entityType1, mockListener1);
support.notifyEntityOwnershipListeners(entity1, false, false, true);
verify(mockListener1, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, false, true));
verify(mockListener12, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
verify(mockListener2, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
// Quickly register and unregister mockListener2 - expecting no exceptions.
support.addEntityOwnershipListener(entityType1, mockListener2);
support.removeEntityOwnershipListener(entityType1, mockListener2);
}
use of org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener in project controller by opendaylight.
the class EntityOwnershipShardTest method testListenerRegistration.
@Test
public void testListenerRegistration() throws Exception {
testLog.info("testListenerRegistration starting");
ShardTestKit kit = new ShardTestKit(getSystem());
ShardIdentifier leaderId = newShardId(LOCAL_MEMBER_NAME);
ShardIdentifier peerId = newShardId(PEER_MEMBER_1_NAME);
TestActorRef<TestEntityOwnershipShard> peer = actorFactory.createTestActor(TestEntityOwnershipShard.props(newShardBuilder(peerId, peerMap(leaderId.toString()), PEER_MEMBER_1_NAME)), peerId.toString());
peer.underlyingActor().startDroppingMessagesOfType(ElectionTimeout.class);
TestActorRef<EntityOwnershipShard> leader = actorFactory.createTestActor(newShardProps(leaderId, peerMap(peerId.toString()), LOCAL_MEMBER_NAME), leaderId.toString());
ShardTestKit.waitUntilLeader(leader);
String otherEntityType = "otherEntityType";
final DOMEntity entity1 = new DOMEntity(ENTITY_TYPE, ENTITY_ID1);
final DOMEntity entity2 = new DOMEntity(ENTITY_TYPE, ENTITY_ID2);
final DOMEntity entity3 = new DOMEntity(ENTITY_TYPE, ENTITY_ID3);
final DOMEntity entity4 = new DOMEntity(otherEntityType, ENTITY_ID3);
DOMEntityOwnershipListener listener = mock(DOMEntityOwnershipListener.class);
// Register listener
leader.tell(new RegisterListenerLocal(listener, ENTITY_TYPE), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
// Register a couple candidates for the desired entity type and verify listener is notified.
leader.tell(new RegisterCandidateLocal(entity1), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
verify(listener, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, true, true));
leader.tell(new RegisterCandidateLocal(entity2), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
verify(listener, timeout(5000)).ownershipChanged(ownershipChange(entity2, false, true, true));
reset(listener);
// Register another candidate for another entity type and verify listener is not notified.
leader.tell(new RegisterCandidateLocal(entity4), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
verify(listener, never()).ownershipChanged(ownershipChange(entity4));
// Register remote candidate for entity1
peer.tell(new RegisterCandidateLocal(entity1), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
verifyCommittedEntityCandidate(leader, ENTITY_TYPE, entity1.getIdentifier(), PEER_MEMBER_1_NAME);
// Unregister the local candidate for entity1 and verify listener is notified
leader.tell(new UnregisterCandidateLocal(entity1), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
verify(listener, timeout(5000)).ownershipChanged(ownershipChange(entity1, true, false, true));
reset(listener);
// Unregister the listener, add a candidate for entity3 and verify listener isn't notified
leader.tell(new UnregisterListenerLocal(listener, ENTITY_TYPE), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
leader.tell(new RegisterCandidateLocal(entity3), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
verifyOwner(leader, ENTITY_TYPE, entity3.getIdentifier(), LOCAL_MEMBER_NAME);
Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
verify(listener, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
// Re-register the listener and verify it gets notified of currently owned entities
reset(listener);
leader.tell(new RegisterListenerLocal(listener, ENTITY_TYPE), kit.getRef());
kit.expectMsgClass(SuccessReply.class);
verify(listener, timeout(5000).times(2)).ownershipChanged(or(ownershipChange(entity2, false, true, true), ownershipChange(entity3, false, true, true)));
Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
verify(listener, never()).ownershipChanged(ownershipChange(entity4));
verify(listener, times(1)).ownershipChanged(ownershipChange(entity1));
testLog.info("testListenerRegistration ending");
}
use of org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener 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.mdsal.eos.dom.api.DOMEntityOwnershipListener in project controller by opendaylight.
the class LegacyEntityOwnershipServiceAdapterTest method testRegisterListener.
@Test
public void testRegisterListener() {
DOMEntityOwnershipListenerRegistration mockDOMReg = mock(DOMEntityOwnershipListenerRegistration.class);
doNothing().when(mockDOMReg).close();
doReturn(mockDOMReg).when(mockDOMService).registerListener(eq(DOM_ENTITY.getType()), any(DOMEntityOwnershipListener.class));
EntityOwnershipListener mockListener = mock(EntityOwnershipListener.class);
doNothing().when(mockListener).ownershipChanged(any(EntityOwnershipChange.class));
EntityOwnershipListenerRegistration reg = adapter.registerListener(LEGACY_ENTITY.getType(), mockListener);
assertNotNull("registerListener returned null", reg);
assertEquals("getInstance", mockListener, reg.getInstance());
assertEquals("getEntityType", LEGACY_ENTITY.getType(), reg.getEntityType());
ArgumentCaptor<DOMEntityOwnershipListener> domListenerCaptor = ArgumentCaptor.forClass(DOMEntityOwnershipListener.class);
verify(mockDOMService).registerListener(eq(DOM_ENTITY.getType()), domListenerCaptor.capture());
DOMEntityOwnershipChange domOwnershipChange = new DOMEntityOwnershipChange(DOM_ENTITY, EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED);
domListenerCaptor.getValue().ownershipChanged(domOwnershipChange);
ArgumentCaptor<EntityOwnershipChange> ownershipChangeCaptor = ArgumentCaptor.forClass(EntityOwnershipChange.class);
verify(mockListener).ownershipChanged(ownershipChangeCaptor.capture());
EntityOwnershipChange change = ownershipChangeCaptor.getValue();
assertEquals("getEntity", LEGACY_ENTITY, change.getEntity());
assertEquals("wasOwner", false, change.wasOwner());
assertEquals("isOwner", true, change.isOwner());
assertEquals("hasOwner", true, change.hasOwner());
reg.close();
verify(mockDOMReg).close();
}
Aggregations