Search in sources :

Example 11 with ShardTestKit

use of org.opendaylight.controller.cluster.datastore.ShardTestKit in project controller by opendaylight.

the class EntityOwnershipShardTest method testOnRegisterCandidateLocal.

@Test
public void testOnRegisterCandidateLocal() throws Exception {
    testLog.info("testOnRegisterCandidateLocal starting");
    ShardTestKit kit = new ShardTestKit(getSystem());
    TestActorRef<EntityOwnershipShard> shard = actorFactory.createTestActor(newLocalShardProps());
    ShardTestKit.waitUntilLeader(shard);
    YangInstanceIdentifier entityId = ENTITY_ID1;
    DOMEntity entity = new DOMEntity(ENTITY_TYPE, entityId);
    shard.tell(new RegisterCandidateLocal(entity), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    verifyCommittedEntityCandidate(shard, ENTITY_TYPE, entityId, LOCAL_MEMBER_NAME);
    verifyOwner(shard, ENTITY_TYPE, entityId, LOCAL_MEMBER_NAME);
    testLog.info("testOnRegisterCandidateLocal ending");
}
Also used : RegisterCandidateLocal(org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal) ShardTestKit(org.opendaylight.controller.cluster.datastore.ShardTestKit) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 12 with ShardTestKit

use of org.opendaylight.controller.cluster.datastore.ShardTestKit in project controller by opendaylight.

the class EntityOwnershipShardTest method testLeaderIsolation.

@Test
public void testLeaderIsolation() throws Exception {
    testLog.info("testLeaderIsolation 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)), 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)), 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)), leaderId.toString());
    ShardTestKit.waitUntilLeader(leader);
    // Add entity1 candidates for all members with the leader as 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);
    peer1.tell(new RegisterCandidateLocal(entity1), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    verifyCommittedEntityCandidate(leader, entity1.getType(), entity1.getIdentifier(), PEER_MEMBER_1_NAME);
    peer2.tell(new RegisterCandidateLocal(entity1), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    verifyCommittedEntityCandidate(leader, entity1.getType(), entity1.getIdentifier(), PEER_MEMBER_2_NAME);
    verifyOwner(leader, entity1.getType(), entity1.getIdentifier(), LOCAL_MEMBER_NAME);
    verifyOwner(peer1, entity1.getType(), entity1.getIdentifier(), LOCAL_MEMBER_NAME);
    verifyOwner(peer2, entity1.getType(), entity1.getIdentifier(), LOCAL_MEMBER_NAME);
    // Add entity2 candidates for all members with peer1 as the owner
    DOMEntity entity2 = new DOMEntity(ENTITY_TYPE, ENTITY_ID2);
    peer1.tell(new RegisterCandidateLocal(entity2), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    verifyCommittedEntityCandidate(leader, entity2.getType(), entity2.getIdentifier(), PEER_MEMBER_1_NAME);
    peer2.tell(new RegisterCandidateLocal(entity2), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    verifyCommittedEntityCandidate(leader, entity2.getType(), entity2.getIdentifier(), PEER_MEMBER_2_NAME);
    leader.tell(new RegisterCandidateLocal(entity2), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    verifyCommittedEntityCandidate(leader, entity2.getType(), entity2.getIdentifier(), LOCAL_MEMBER_NAME);
    verifyOwner(leader, entity2.getType(), entity2.getIdentifier(), PEER_MEMBER_1_NAME);
    verifyOwner(peer1, entity2.getType(), entity2.getIdentifier(), PEER_MEMBER_1_NAME);
    verifyOwner(peer2, entity2.getType(), entity2.getIdentifier(), PEER_MEMBER_1_NAME);
    // Add entity3 candidates for all members with peer2 as the owner
    DOMEntity entity3 = new DOMEntity(ENTITY_TYPE, ENTITY_ID3);
    peer2.tell(new RegisterCandidateLocal(entity3), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    verifyCommittedEntityCandidate(leader, entity3.getType(), entity3.getIdentifier(), PEER_MEMBER_2_NAME);
    leader.tell(new RegisterCandidateLocal(entity3), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    verifyCommittedEntityCandidate(leader, entity3.getType(), entity3.getIdentifier(), LOCAL_MEMBER_NAME);
    peer1.tell(new RegisterCandidateLocal(entity3), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    verifyCommittedEntityCandidate(leader, entity3.getType(), entity3.getIdentifier(), PEER_MEMBER_1_NAME);
    verifyOwner(leader, entity3.getType(), entity3.getIdentifier(), PEER_MEMBER_2_NAME);
    verifyOwner(peer1, entity3.getType(), entity3.getIdentifier(), PEER_MEMBER_2_NAME);
    verifyOwner(peer2, entity3.getType(), entity3.getIdentifier(), PEER_MEMBER_2_NAME);
    // Add listeners on all members
    DOMEntityOwnershipListener leaderListener = mock(DOMEntityOwnershipListener.class);
    leader.tell(new RegisterListenerLocal(leaderListener, ENTITY_TYPE), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    verify(leaderListener, timeout(5000).times(3)).ownershipChanged(or(or(ownershipChange(entity1, false, true, true), ownershipChange(entity2, false, false, true)), ownershipChange(entity3, false, false, true)));
    reset(leaderListener);
    DOMEntityOwnershipListener peer1Listener = mock(DOMEntityOwnershipListener.class);
    peer1.tell(new RegisterListenerLocal(peer1Listener, ENTITY_TYPE), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    verify(peer1Listener, timeout(5000).times(3)).ownershipChanged(or(or(ownershipChange(entity1, false, false, true), ownershipChange(entity2, false, true, true)), ownershipChange(entity3, false, false, true)));
    reset(peer1Listener);
    DOMEntityOwnershipListener peer2Listener = mock(DOMEntityOwnershipListener.class);
    peer2.tell(new RegisterListenerLocal(peer2Listener, ENTITY_TYPE), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    verify(peer2Listener, timeout(5000).times(3)).ownershipChanged(or(or(ownershipChange(entity1, false, false, true), ownershipChange(entity2, false, false, true)), ownershipChange(entity3, false, true, true)));
    reset(peer2Listener);
    // 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);
    // Make peer1 start an election and become leader by enabling the ElectionTimeout message.
    peer1.underlyingActor().stopDroppingMessagesOfType(ElectionTimeout.class);
    // Send PeerDown to the isolated leader so it tries to re-assign ownership for the entities owned by the
    // isolated peers.
    leader.tell(new PeerDown(peerId1.getMemberName(), peerId1.toString()), ActorRef.noSender());
    leader.tell(new PeerDown(peerId2.getMemberName(), peerId2.toString()), ActorRef.noSender());
    verifyRaftState(leader, state -> assertEquals("getRaftState", RaftState.IsolatedLeader.toString(), state.getRaftState()));
    // Expect inJeopardy notification on the isolated leader.
    verify(leaderListener, timeout(5000).times(3)).ownershipChanged(or(or(ownershipChange(entity1, true, true, true, true), ownershipChange(entity2, false, false, true, true)), ownershipChange(entity3, false, false, true, true)));
    reset(leaderListener);
    verifyRaftState(peer1, state -> assertEquals("getRaftState", RaftState.Leader.toString(), state.getRaftState()));
    // Send PeerDown to the new leader peer1 so it re-assigns ownership for the entities owned by the
    // isolated leader.
    peer1.tell(new PeerDown(leaderId.getMemberName(), leaderId.toString()), ActorRef.noSender());
    verifyOwner(peer1, entity1.getType(), entity1.getIdentifier(), PEER_MEMBER_1_NAME);
    verify(peer1Listener, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, true, true));
    reset(peer1Listener);
    verify(peer2Listener, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, false, true));
    reset(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 and send inJeopardy cleared notifications for all entities.
    verifyRaftState(leader, state -> assertEquals("getRaftState", RaftState.Follower.toString(), state.getRaftState()));
    verify(leaderListener, timeout(5000).times(3)).ownershipChanged(or(or(ownershipChange(entity1, true, true, true), ownershipChange(entity2, false, false, true)), ownershipChange(entity3, false, false, true)));
    verifyOwner(leader, entity1.getType(), entity1.getIdentifier(), PEER_MEMBER_1_NAME);
    verify(leaderListener, timeout(5000)).ownershipChanged(ownershipChange(entity1, true, false, true));
    Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
    verifyOwner(leader, entity2.getType(), entity2.getIdentifier(), PEER_MEMBER_1_NAME);
    verifyOwner(leader, entity3.getType(), entity3.getIdentifier(), PEER_MEMBER_2_NAME);
    verifyNoMoreInteractions(leaderListener);
    verifyNoMoreInteractions(peer1Listener);
    verifyNoMoreInteractions(peer2Listener);
    testLog.info("testLeaderIsolation ending");
}
Also used : DOMEntityOwnershipListener(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener) RegisterCandidateLocal(org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal) ShardTestKit(org.opendaylight.controller.cluster.datastore.ShardTestKit) PeerDown(org.opendaylight.controller.cluster.datastore.messages.PeerDown) ShardIdentifier(org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) RegisterListenerLocal(org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal) Test(org.junit.Test)

Example 13 with ShardTestKit

use of org.opendaylight.controller.cluster.datastore.ShardTestKit in project controller by opendaylight.

the class EntityOwnershipShardTest method testOnRegisterCandidateLocalWithIsolatedLeader.

@Test
public void testOnRegisterCandidateLocalWithIsolatedLeader() throws Exception {
    testLog.info("testOnRegisterCandidateLocalWithIsolatedLeader starting");
    final ShardTestKit kit = new ShardTestKit(getSystem());
    dataStoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(2).shardIsolatedLeaderCheckIntervalInMillis(50);
    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());
    TestEntityOwnershipShard peerShard = peer.underlyingActor();
    peerShard.startDroppingMessagesOfType(ElectionTimeout.class);
    TestActorRef<EntityOwnershipShard> leader = actorFactory.createTestActor(newShardProps(leaderId, peerMap(peerId.toString()), LOCAL_MEMBER_NAME));
    ShardTestKit.waitUntilLeader(leader);
    // Drop AppendEntries and wait enough time for the shard to switch to IsolatedLeader.
    peerShard.startDroppingMessagesOfType(AppendEntries.class);
    verifyRaftState(leader, state -> assertEquals("getRaftState", RaftState.IsolatedLeader.toString(), state.getRaftState()));
    YangInstanceIdentifier entityId = ENTITY_ID1;
    DOMEntity entity = new DOMEntity(ENTITY_TYPE, entityId);
    leader.tell(new RegisterCandidateLocal(entity), kit.getRef());
    kit.expectMsgClass(SuccessReply.class);
    // Resume AppendEntries - the candidate write should now be committed.
    peerShard.stopDroppingMessagesOfType(AppendEntries.class);
    verifyCommittedEntityCandidate(leader, ENTITY_TYPE, entityId, LOCAL_MEMBER_NAME);
    verifyOwner(leader, ENTITY_TYPE, entityId, LOCAL_MEMBER_NAME);
    testLog.info("testOnRegisterCandidateLocalWithIsolatedLeader ending");
}
Also used : RegisterCandidateLocal(org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal) ShardTestKit(org.opendaylight.controller.cluster.datastore.ShardTestKit) ShardIdentifier(org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)13 ShardTestKit (org.opendaylight.controller.cluster.datastore.ShardTestKit)13 RegisterCandidateLocal (org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal)13 DOMEntity (org.opendaylight.mdsal.eos.dom.api.DOMEntity)13 ShardIdentifier (org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier)11 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)5 RegisterListenerLocal (org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal)3 UnregisterCandidateLocal (org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal)3 PeerDown (org.opendaylight.controller.cluster.datastore.messages.PeerDown)3 DOMEntityOwnershipListener (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener)3 EntityOwnerSelectionStrategyConfig (org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig)2 PeerUp (org.opendaylight.controller.cluster.datastore.messages.PeerUp)2 ArrayList (java.util.ArrayList)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 CandidateAdded (org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded)1 UnregisterListenerLocal (org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal)1 PeerAddressResolved (org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved)1 DOMEntityOwnershipChange (org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange)1