use of org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo in project controller by opendaylight.
the class ActorContextTest method testFindPrimaryExceptions.
@SuppressWarnings("checkstyle:IllegalCatch")
private static void testFindPrimaryExceptions(final Object expectedException) throws Exception {
ActorRef shardManager = getSystem().actorOf(MessageCollectorActor.props());
DatastoreContext dataStoreContext = DatastoreContext.newBuilder().logicalStoreType(LogicalDatastoreType.CONFIGURATION).shardLeaderElectionTimeout(100, TimeUnit.MILLISECONDS).build();
ActorContext actorContext = new ActorContext(getSystem(), shardManager, mock(ClusterWrapper.class), mock(Configuration.class), dataStoreContext, new PrimaryShardInfoFutureCache()) {
@Override
protected Future<Object> doAsk(final ActorRef actorRef, final Object message, final Timeout timeout) {
return Futures.successful(expectedException);
}
};
Future<PrimaryShardInfo> foobar = actorContext.findPrimaryShardAsync("foobar");
try {
Await.result(foobar, Duration.apply(100, TimeUnit.MILLISECONDS));
fail("Expected" + expectedException.getClass().toString());
} catch (Exception e) {
if (!expectedException.getClass().isInstance(e)) {
fail("Expected Exception of type " + expectedException.getClass().toString());
}
}
Future<PrimaryShardInfo> cached = actorContext.getPrimaryShardInfoCache().getIfPresent("foobar");
assertNull(cached);
}
use of org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo in project controller by opendaylight.
the class TransactionChainProxy method findPrimaryShard.
/**
* This method is overridden to ensure the previous Tx's ready operations complete
* before we initiate the next Tx in the chain to avoid creation failures if the
* previous Tx's ready operations haven't completed yet.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected Future<PrimaryShardInfo> findPrimaryShard(final String shardName, final TransactionIdentifier txId) {
// Read current state atomically
final State localState = currentState;
// There are no outstanding futures, shortcut
Future<?> previous = localState.previousFuture();
if (previous == null) {
return combineFutureWithPossiblePriorReadOnlyTxFutures(parent.findPrimaryShard(shardName, txId), txId);
}
final String previousTransactionId;
if (localState instanceof Pending) {
previousTransactionId = ((Pending) localState).getIdentifier().toString();
LOG.debug("Tx: {} - waiting for ready futures with pending Tx {}", txId, previousTransactionId);
} else {
previousTransactionId = "";
LOG.debug("Waiting for ready futures on chain {}", getHistoryId());
}
previous = combineFutureWithPossiblePriorReadOnlyTxFutures(previous, txId);
// Add a callback for completion of the combined Futures.
final Promise<PrimaryShardInfo> returnPromise = Futures.promise();
final OnComplete onComplete = new OnComplete() {
@Override
public void onComplete(final Throwable failure, final Object notUsed) {
if (failure != null) {
// A Ready Future failed so fail the returned Promise.
LOG.error("Tx: {} - ready future failed for previous Tx {}", txId, previousTransactionId);
returnPromise.failure(failure);
} else {
LOG.debug("Tx: {} - previous Tx {} readied - proceeding to FindPrimaryShard", txId, previousTransactionId);
// Send the FindPrimaryShard message and use the resulting Future to complete the
// returned Promise.
returnPromise.completeWith(parent.findPrimaryShard(shardName, txId));
}
}
};
previous.onComplete(onComplete, getActorContext().getClientDispatcher());
return returnPromise.future();
}
use of org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo in project controller by opendaylight.
the class ShardManagerTest method testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable.
@Test
public void testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable() throws Exception {
LOG.info("testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable starting");
String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-256", "member-2")).build());
// Create an ActorSystem, ShardManager and actor for member-256.
final ActorSystem system256 = newActorSystem("Member256");
// 2562 is the tcp port of Member256 in src/test/resources/application.conf.
Cluster.get(system256).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2562"));
final ActorRef mockShardActor256 = newMockShardActor(system256, Shard.DEFAULT_NAME, "member-256");
final PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache();
// ShardManager must be created with shard configuration to let its localShards has shards.
final TestActorRef<TestShardManager> shardManager256 = TestActorRef.create(system256, newTestShardMgrBuilder(mockConfig).shardActor(mockShardActor256).cluster(new ClusterWrapperImpl(system256)).primaryShardInfoCache(primaryShardInfoCache).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
// Create an ActorSystem, ShardManager and actor for member-2 whose name is contained in member-256.
final ActorSystem system2 = newActorSystem("Member2");
// Join member-2 into the cluster of member-256.
Cluster.get(system2).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2562"));
final ActorRef mockShardActor2 = newMockShardActor(system2, Shard.DEFAULT_NAME, "member-2");
final TestActorRef<TestShardManager> shardManager2 = TestActorRef.create(system2, newTestShardMgrBuilder(mockConfig).shardActor(mockShardActor2).cluster(new ClusterWrapperImpl(system2)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
new TestKit(system256) {
{
shardManager256.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager2.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager256.tell(new ActorInitialized(), mockShardActor256);
shardManager2.tell(new ActorInitialized(), mockShardActor2);
String memberId256 = "member-256-shard-default-" + shardMrgIDSuffix;
String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
shardManager256.tell(new ShardLeaderStateChanged(memberId256, memberId256, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor256);
shardManager256.tell(new RoleChangeNotification(memberId256, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor256);
shardManager2.tell(new ShardLeaderStateChanged(memberId2, memberId256, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor2);
shardManager2.tell(new RoleChangeNotification(memberId2, RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor2);
shardManager256.underlyingActor().waitForMemberUp();
shardManager256.tell(new FindPrimary("default", true), getRef());
LocalPrimaryShardFound found = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
String path = found.getPrimaryPath();
assertTrue("Unexpected primary path " + path + " which must on member-256", path.contains("member-256-shard-default-config"));
PrimaryShardInfo primaryShardInfo = new PrimaryShardInfo(system256.actorSelection(mockShardActor256.path()), DataStoreVersions.CURRENT_VERSION);
primaryShardInfoCache.putSuccessful("default", primaryShardInfo);
// Simulate member-2 become unreachable.
shardManager256.tell(MockClusterWrapper.createUnreachableMember("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
shardManager256.underlyingActor().waitForUnreachableMember();
// Make sure leader shard on member-256 is still leader and still in the cache.
shardManager256.tell(new FindPrimary("default", true), getRef());
found = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
path = found.getPrimaryPath();
assertTrue("Unexpected primary path " + path + " which must still not on member-256", path.contains("member-256-shard-default-config"));
Future<PrimaryShardInfo> futurePrimaryShard = primaryShardInfoCache.getIfPresent("default");
futurePrimaryShard.onComplete(new OnComplete<PrimaryShardInfo>() {
@Override
public void onComplete(final Throwable failure, final PrimaryShardInfo futurePrimaryShardInfo) {
if (failure != null) {
assertTrue("Primary shard info is unexpectedly removed from primaryShardInfoCache", false);
} else {
assertEquals("Expected primaryShardInfoCache entry", primaryShardInfo, futurePrimaryShardInfo);
}
}
}, system256.dispatchers().defaultGlobalDispatcher());
}
};
LOG.info("testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable ending");
}
use of org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo in project controller by opendaylight.
the class ActorContextTest method testFindPrimaryShardAsyncLocalPrimaryFound.
@Test
public void testFindPrimaryShardAsyncLocalPrimaryFound() throws Exception {
ActorRef shardManager = getSystem().actorOf(MessageCollectorActor.props());
DatastoreContext dataStoreContext = DatastoreContext.newBuilder().logicalStoreType(LogicalDatastoreType.CONFIGURATION).shardLeaderElectionTimeout(100, TimeUnit.MILLISECONDS).build();
final DataTree mockDataTree = Mockito.mock(DataTree.class);
final String expPrimaryPath = "akka://test-system/find-primary-shard";
ActorContext actorContext = new ActorContext(getSystem(), shardManager, mock(ClusterWrapper.class), mock(Configuration.class), dataStoreContext, new PrimaryShardInfoFutureCache()) {
@Override
protected Future<Object> doAsk(final ActorRef actorRef, final Object message, final Timeout timeout) {
return Futures.successful((Object) new LocalPrimaryShardFound(expPrimaryPath, mockDataTree));
}
};
Future<PrimaryShardInfo> foobar = actorContext.findPrimaryShardAsync("foobar");
PrimaryShardInfo actual = Await.result(foobar, Duration.apply(5000, TimeUnit.MILLISECONDS));
assertNotNull(actual);
assertEquals("LocalShardDataTree present", true, actual.getLocalShardDataTree().isPresent());
assertSame("LocalShardDataTree", mockDataTree, actual.getLocalShardDataTree().get());
assertTrue("Unexpected PrimaryShardActor path " + actual.getPrimaryShardActor().path(), expPrimaryPath.endsWith(actual.getPrimaryShardActor().pathString()));
assertEquals("getPrimaryShardVersion", DataStoreVersions.CURRENT_VERSION, actual.getPrimaryShardVersion());
Future<PrimaryShardInfo> cached = actorContext.getPrimaryShardInfoCache().getIfPresent("foobar");
PrimaryShardInfo cachedInfo = Await.result(cached, FiniteDuration.apply(1, TimeUnit.MILLISECONDS));
assertEquals(cachedInfo, actual);
actorContext.getPrimaryShardInfoCache().remove("foobar");
cached = actorContext.getPrimaryShardInfoCache().getIfPresent("foobar");
assertNull(cached);
}
use of org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo in project controller by opendaylight.
the class PrimaryShardInfoFutureCacheTest method testOperations.
@Test
public void testOperations() {
PrimaryShardInfoFutureCache cache = new PrimaryShardInfoFutureCache();
assertEquals("getIfPresent", null, cache.getIfPresent("foo"));
PrimaryShardInfo shardInfo = new PrimaryShardInfo(mock(ActorSelection.class), DataStoreVersions.CURRENT_VERSION);
cache.putSuccessful("foo", shardInfo);
Future<PrimaryShardInfo> future = cache.getIfPresent("foo");
assertNotNull("Null future", future);
assertEquals("getIfPresent", shardInfo, future.value().get().get());
cache.remove("foo");
assertEquals("getIfPresent", null, cache.getIfPresent("foo"));
}
Aggregations