Search in sources :

Example 41 with ActorSelection

use of akka.actor.ActorSelection in project controller by opendaylight.

the class AbstractLeader method initiateCaptureSnapshot.

/**
 * Initiates a snapshot capture to install on a follower.
 *
 * <p>
 * Install Snapshot works as follows
 *   1. Leader initiates the capture snapshot by calling createSnapshot on the RaftActor.
 *   2. On receipt of the CaptureSnapshotReply message, the RaftActor persists the snapshot and makes a call to
 *      the Leader's handleMessage with a SendInstallSnapshot message.
 *   3. The Leader obtains and stores the Snapshot from the SendInstallSnapshot message and sends it in chunks to
 *      the Follower via InstallSnapshot messages.
 *   4. For each chunk, the Follower sends back an InstallSnapshotReply.
 *   5. On receipt of the InstallSnapshotReply for the last chunk, the Leader marks the install complete for that
 *      follower.
 *   6. If another follower requires a snapshot and a snapshot has been collected (via SendInstallSnapshot)
 *      then send the existing snapshot in chunks to the follower.
 *
 * @param followerId the id of the follower.
 * @return true if capture was initiated, false otherwise.
 */
public boolean initiateCaptureSnapshot(final String followerId) {
    FollowerLogInformation followerLogInfo = followerToLog.get(followerId);
    if (snapshotHolder.isPresent()) {
        // If a snapshot is present in the memory, most likely another install is in progress no need to capture
        // snapshot. This could happen if another follower needs an install when one is going on.
        final ActorSelection followerActor = context.getPeerActorSelection(followerId);
        // Note: sendSnapshotChunk will set the LeaderInstallSnapshotState.
        sendSnapshotChunk(followerActor, followerLogInfo);
        return true;
    }
    boolean captureInitiated = context.getSnapshotManager().captureToInstall(context.getReplicatedLog().last(), this.getReplicatedToAllIndex(), followerId);
    if (captureInitiated) {
        followerLogInfo.setLeaderInstallSnapshotState(new LeaderInstallSnapshotState(context.getConfigParams().getSnapshotChunkSize(), logName()));
    }
    return captureInitiated;
}
Also used : FollowerLogInformation(org.opendaylight.controller.cluster.raft.FollowerLogInformation) ActorSelection(akka.actor.ActorSelection)

Example 42 with ActorSelection

use of akka.actor.ActorSelection in project controller by opendaylight.

the class ShardTest method testClusteredDataTreeChangeListenerWithDelayedRegistrationClosed.

@Test
public void testClusteredDataTreeChangeListenerWithDelayedRegistrationClosed() throws Exception {
    new ShardTestKit(getSystem()) {

        {
            final String testName = "testClusteredDataTreeChangeListenerWithDelayedRegistrationClosed";
            dataStoreContextBuilder.shardElectionTimeoutFactor(1000).customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
            final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(0);
            final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, TestModel.TEST_PATH), actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
            setupInMemorySnapshotStore();
            final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()), actorFactory.generateActorId(testName + "-shard"));
            waitUntilNoLeader(shard);
            shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), getRef());
            final RegisterDataTreeNotificationListenerReply reply = expectMsgClass(duration("5 seconds"), RegisterDataTreeNotificationListenerReply.class);
            assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
            final ActorSelection regActor = getSystem().actorSelection(reply.getListenerRegistrationPath());
            regActor.tell(CloseDataTreeNotificationListenerRegistration.getInstance(), getRef());
            expectMsgClass(CloseDataTreeNotificationListenerRegistrationReply.class);
            shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build()).customRaftPolicyImplementation(null).build(), ActorRef.noSender());
            listener.expectNoMoreChanges("Received unexpected change after close");
        }
    };
}
Also used : RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) MockDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener) ActorSelection(akka.actor.ActorSelection) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) RegisterDataTreeChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener) Test(org.junit.Test)

Example 43 with ActorSelection

use of akka.actor.ActorSelection in project controller by opendaylight.

the class DistributedDataStoreRemotingIntegrationTest method testReadyLocalTransactionForwardedToLeader.

@SuppressWarnings("unchecked")
@Test
public void testReadyLocalTransactionForwardedToLeader() throws Exception {
    initDatastoresWithCars("testReadyLocalTransactionForwardedToLeader");
    followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), "cars");
    final Optional<ActorRef> carsFollowerShard = followerDistributedDataStore.getActorContext().findLocalShard("cars");
    assertEquals("Cars follower shard found", true, carsFollowerShard.isPresent());
    final DataTree dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
    // Send a tx with immediate commit.
    DataTreeModification modification = dataTree.takeSnapshot().newModification();
    new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification);
    new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification);
    final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
    new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
    modification.ready();
    ReadyLocalTransaction readyLocal = new ReadyLocalTransaction(tx1, modification, true);
    carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
    Object resp = followerTestKit.expectMsgClass(Object.class);
    if (resp instanceof akka.actor.Status.Failure) {
        throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure) resp).cause());
    }
    assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
    verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
    // Send another tx without immediate commit.
    modification = dataTree.takeSnapshot().newModification();
    MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000));
    new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
    modification.ready();
    readyLocal = new ReadyLocalTransaction(tx2, modification, false);
    carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
    resp = followerTestKit.expectMsgClass(Object.class);
    if (resp instanceof akka.actor.Status.Failure) {
        throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure) resp).cause());
    }
    assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
    final ActorSelection txActor = leaderDistributedDataStore.getActorContext().actorSelection(((ReadyTransactionReply) resp).getCohortPath());
    final Supplier<Short> versionSupplier = Mockito.mock(Supplier.class);
    Mockito.doReturn(DataStoreVersions.CURRENT_VERSION).when(versionSupplier).get();
    ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy(leaderDistributedDataStore.getActorContext(), Arrays.asList(new ThreePhaseCommitCohortProxy.CohortInfo(Futures.successful(txActor), versionSupplier)), tx2);
    cohort.canCommit().get(5, TimeUnit.SECONDS);
    cohort.preCommit().get(5, TimeUnit.SECONDS);
    cohort.commit().get(5, TimeUnit.SECONDS);
    verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
}
Also used : DataTreeModification(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification) WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) ActorRef(akka.actor.ActorRef) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) InMemoryDataTreeFactory(org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory) MergeModification(org.opendaylight.controller.cluster.datastore.modification.MergeModification) ReadyLocalTransaction(org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction) ActorSelection(akka.actor.ActorSelection) GetShardDataTree(org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) Test(org.junit.Test)

Aggregations

ActorSelection (akka.actor.ActorSelection)43 ActorRef (akka.actor.ActorRef)9 Test (org.junit.Test)6 PrimaryShardInfo (org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo)5 ActorContext (org.opendaylight.controller.cluster.datastore.utils.ActorContext)4 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)4 InvalidActorNameException (akka.actor.InvalidActorNameException)3 Status (akka.actor.Status)3 TestActorRef (akka.testkit.TestActorRef)3 TestKit (akka.testkit.javadsl.TestKit)3 Timeout (akka.util.Timeout)3 ArrayList (java.util.ArrayList)3 ClientActorContext (org.opendaylight.controller.cluster.access.client.ClientActorContext)3 AkkaException (akka.AkkaException)2 ActorIdentity (akka.actor.ActorIdentity)2 Identify (akka.actor.Identify)2 Failure (akka.actor.Status.Failure)2 Success (akka.actor.Status.Success)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 AbstractActorTest (org.opendaylight.controller.cluster.datastore.AbstractActorTest)2