use of org.infinispan.remoting.transport.MockTransport in project infinispan by infinispan.
the class RpcManagerMBeanTest method testSuccessRatio.
@Test(dependsOnMethods = "testEnableJmxStats")
public void testSuccessRatio() throws Exception {
Cache<MagicKey, Object> cache1 = manager(0).getCache();
Cache<MagicKey, Object> cache2 = manager(1).getCache();
MBeanServer mBeanServer = mBeanServerLookup.getMBeanServer();
ObjectName rpcManager1 = getCacheObjectName(jmxDomain1, getDefaultCacheName() + "(repl_sync)", "RpcManager");
// the previous test has reset the statistics
assertEquals(mBeanServer.getAttribute(rpcManager1, "ReplicationCount"), (long) 0);
assertEquals(mBeanServer.getAttribute(rpcManager1, "ReplicationFailures"), (long) 0);
assertEquals(mBeanServer.getAttribute(rpcManager1, "SuccessRatio"), "N/A");
RpcManagerImpl rpcManager = (RpcManagerImpl) extractComponent(cache1, RpcManager.class);
Transport originalTransport = rpcManager.getTransport();
try {
MockTransport transport = new MockTransport(address(0));
transport.init(originalTransport.getViewId(), originalTransport.getMembers());
rpcManager.setTransport(transport);
CompletableFuture<Object> put1 = cache1.putAsync(new MagicKey("a1", cache1), "b1");
timeService.advance(50);
transport.expectCommand(SingleRpcCommand.class).singleResponse(address(2), SuccessfulResponse.SUCCESSFUL_EMPTY_RESPONSE);
put1.get(10, TimeUnit.SECONDS);
CompletableFuture<Object> put2 = cache1.putAsync(new MagicKey("a2", cache2), "b2");
timeService.advance(10);
transport.expectCommand(SingleRpcCommand.class).singleResponse(address(2), SuccessfulResponse.SUCCESSFUL_EMPTY_RESPONSE);
put2.get(10, TimeUnit.SECONDS);
assertEquals(mBeanServer.getAttribute(rpcManager1, "ReplicationCount"), (long) 2);
assertEquals(mBeanServer.getAttribute(rpcManager1, "SuccessRatio"), "100%");
long avgReplTime = (long) mBeanServer.getAttribute(rpcManager1, "AverageReplicationTime");
assertEquals(avgReplTime, 30);
// If cache1 is the primary owner it will be a broadcast, otherwise a unicast
CompletableFuture<Object> put3 = cache1.putAsync(new MagicKey("a3", cache1), "b3");
transport.expectCommand(SingleRpcCommand.class).throwException(new RuntimeException());
Exceptions.expectCompletionException(CacheException.class, put3);
CompletableFuture<Object> put4 = cache1.putAsync(new MagicKey("a4", cache2), "b4");
transport.expectCommand(SingleRpcCommand.class).throwException(new RuntimeException());
Exceptions.expectCompletionException(CacheException.class, put4);
assertEquals(mBeanServer.getAttribute(rpcManager1, "SuccessRatio"), ("50%"));
} finally {
rpcManager.setTransport(originalTransport);
}
}
use of org.infinispan.remoting.transport.MockTransport in project infinispan by infinispan.
the class ClusterTopologyManagerImplTest method testClusterStartupWith2Nodes.
/**
* Start two nodes and make both join the cache.
*/
public void testClusterStartupWith2Nodes() throws Exception {
// Create global component registry with dependencies
GlobalConfiguration gc = GlobalConfigurationBuilder.defaultClusteredBuilder().build();
EmbeddedCacheManager cacheManager = mock(EmbeddedCacheManager.class);
GlobalComponentRegistry gcr = new GlobalComponentRegistry(gc, cacheManager, Collections.emptySet(), TestModuleRepository.defaultModuleRepository(), mock(ConfigurationManager.class));
BasicComponentRegistry gbcr = gcr.getComponent(BasicComponentRegistry.class);
CacheManagerNotifierImpl managerNotifier = new CacheManagerNotifierImpl();
gbcr.replaceComponent(CacheManagerNotifier.class.getName(), managerNotifier, false);
managerNotifier.start();
MockTransport transport = new MockTransport(A);
gbcr.replaceComponent(Transport.class.getName(), transport, false);
PersistentUUIDManager persistentUUIDManager = new PersistentUUIDManagerImpl();
gbcr.replaceComponent(PersistentUUIDManager.class.getName(), persistentUUIDManager, false);
gbcr.replaceComponent(KnownComponentNames.NON_BLOCKING_EXECUTOR, executor, false);
gbcr.replaceComponent(KnownComponentNames.TIMEOUT_SCHEDULE_EXECUTOR, scheduledExecutor, false);
MockLocalTopologyManager ltm = new MockLocalTopologyManager(CACHE_NAME);
gbcr.replaceComponent(LocalTopologyManager.class.getName(), ltm, false);
// Initial conditions
transport.init(1, singletonList(A));
ltm.init(null, null, null, null);
// Component under test: ClusterTopologyManagerImpl on the coordinator (A)
ClusterTopologyManagerImpl ctm = new ClusterTopologyManagerImpl();
gbcr.replaceComponent(ClusterTopologyManager.class.getName(), ctm, false);
gcr.rewire();
ctm.start();
// CTMI becomes coordinator and fetches the cluster status
transport.expectCommand(CacheStatusRequestCommand.class).finish();
// No caches, so no topology update is expected here
Thread.sleep(1);
transport.verifyNoErrors();
// First node joins the cache
CacheStatusResponse joinResponseA = CompletionStages.join(ctm.handleJoin(CACHE_NAME, A, joinInfoA, 1));
assertEquals(1, joinResponseA.getCacheTopology().getTopologyId());
assertCHMembers(joinResponseA.getCacheTopology().getCurrentCH(), A);
assertNull(joinResponseA.getCacheTopology().getPendingCH());
// LTMI normally updates the topology when receiving the join response
ltm.handleTopologyUpdate(CACHE_NAME, joinResponseA.getCacheTopology(), joinResponseA.getAvailabilityMode(), 1, A);
ltm.expectTopology(1, singletonList(A), null, CacheTopology.Phase.NO_REBALANCE);
// CTMI replies to the initial stable topology broadcast
transport.expectCommand(TopologyUpdateStableCommand.class, c -> {
assertCHMembers(c.getCurrentCH(), A);
assertNull(c.getPendingCH());
}).finish();
// Add a second node
transport.updateView(2, asList(A, B));
managerNotifier.notifyViewChange(asList(A, B), singletonList(A), A, 2);
// CTMI confirms availability
transport.expectHeartBeatCommand().finish();
// Second node tries to join with old view and is rejected
CacheStatusResponse joinResponseB1 = CompletionStages.join(ctm.handleJoin(CACHE_NAME, B, joinInfoB, 1));
assertNull(joinResponseB1);
// Second node joins the cache with correct view id, receives the initial topology
CacheStatusResponse joinResponseB = CompletionStages.join(ctm.handleJoin(CACHE_NAME, B, joinInfoB, 2));
assertEquals(1, joinResponseB.getCacheTopology().getTopologyId());
assertCHMembers(joinResponseB.getCacheTopology().getCurrentCH(), A);
assertNull(joinResponseB.getCacheTopology().getPendingCH());
verifyRebalance(transport, ltm, ctm, 2, 1, singletonList(A), asList(A, B));
transport.verifyNoErrors();
gcr.stop();
}
use of org.infinispan.remoting.transport.MockTransport in project infinispan by infinispan.
the class ClusterTopologyManagerImplTest method testCoordinatorLostDuringRebalance.
/**
* Assume there are already 2 nodes and the coordinator leaves during rebalance
*/
public void testCoordinatorLostDuringRebalance() throws Exception {
// Create global component registry with dependencies
GlobalConfiguration gc = GlobalConfigurationBuilder.defaultClusteredBuilder().build();
EmbeddedCacheManager cacheManager = mock(EmbeddedCacheManager.class);
GlobalComponentRegistry gcr = new GlobalComponentRegistry(gc, cacheManager, Collections.emptySet(), TestModuleRepository.defaultModuleRepository(), mock(ConfigurationManager.class));
BasicComponentRegistry gbcr = gcr.getComponent(BasicComponentRegistry.class);
CacheManagerNotifierImpl managerNotifier = new CacheManagerNotifierImpl();
gbcr.replaceComponent(CacheManagerNotifier.class.getName(), managerNotifier, false);
managerNotifier.start();
MockTransport transport = new MockTransport(B);
gbcr.replaceComponent(Transport.class.getName(), transport, false);
PersistentUUIDManager persistentUUIDManager = new PersistentUUIDManagerImpl();
gbcr.replaceComponent(PersistentUUIDManager.class.getName(), persistentUUIDManager, false);
gbcr.replaceComponent(KnownComponentNames.NON_BLOCKING_EXECUTOR, executor, false);
gbcr.replaceComponent(KnownComponentNames.TIMEOUT_SCHEDULE_EXECUTOR, scheduledExecutor, false);
MockLocalTopologyManager ltm = new MockLocalTopologyManager(CACHE_NAME);
gbcr.replaceComponent(LocalTopologyManager.class.getName(), ltm, false);
// Initial conditions (rebalance in phase 3, READ_NEW_WRITE_ALL)
transport.init(2, asList(A, B));
ConsistentHash stableCH = replicatedChf.create(joinInfoA.getNumOwners(), joinInfoA.getNumSegments(), singletonList(A), null);
ConsistentHash pendingCH = replicatedChf.create(joinInfoA.getNumOwners(), joinInfoA.getNumSegments(), asList(A, B), null);
CacheTopology initialTopology = new CacheTopology(4, 2, stableCH, pendingCH, CacheTopology.Phase.READ_NEW_WRITE_ALL, asList(A, B), asList(joinInfoA.getPersistentUUID(), joinInfoB.getPersistentUUID()));
CacheTopology stableTopology = new CacheTopology(1, 1, stableCH, null, CacheTopology.Phase.NO_REBALANCE, singletonList(A), singletonList(joinInfoA.getPersistentUUID()));
ltm.init(joinInfoA, initialTopology, stableTopology, AvailabilityMode.AVAILABLE);
// Normally LocalTopologyManagerImpl.start()/doHandleTopologyUpdate() registers the persistent UUIDs
// TODO Write test with asymmetric caches leaving the PersistentUUIDManager cache incomplete
persistentUUIDManager.addPersistentAddressMapping(A, joinInfoA.getPersistentUUID());
persistentUUIDManager.addPersistentAddressMapping(B, joinInfoB.getPersistentUUID());
// Component under test: ClusterTopologyManagerImpl on the new coordinator (B)
ClusterTopologyManagerImpl ctm = new ClusterTopologyManagerImpl();
gbcr.replaceComponent(ClusterTopologyManager.class.getName(), ctm, false);
gcr.rewire();
// When CTMI starts as regular member it requests the rebalancing status from the coordinator
runConcurrently(ctm::start, () -> transport.expectCommand(RebalanceStatusRequestCommand.class).singleResponse(A, SuccessfulResponse.create(true)));
// Wait for the initial view update in CTMI to finish
eventuallyEquals(ClusterTopologyManager.ClusterManagerStatus.REGULAR_MEMBER, ctm::getStatus);
// The coordinator (node A) leaves the cluster
transport.updateView(3, singletonList(B));
managerNotifier.notifyViewChange(singletonList(B), asList(A, B), B, 3);
// Node B becomes coordinator and CTMI tries to recover the cluster status
transport.expectCommand(CacheStatusRequestCommand.class).finish();
// CTMI gets a single cache topology with READ_NEW and broadcasts a new topology with only the read CH
ltm.expectTopology(5, asList(A, B), null, CacheTopology.Phase.NO_REBALANCE);
transport.expectCommand(TopologyUpdateCommand.class, c -> {
assertEquals(5, c.getTopologyId());
assertCHMembers(c.getCurrentCH(), A, B);
assertNull(c.getPendingCH());
});
transport.expectCommand(TopologyUpdateStableCommand.class, c -> {
assertEquals(1, c.getTopologyId());
assertCHMembers(c.getCurrentCH(), A);
assertNull(c.getPendingCH());
});
// CTMI broadcasts a new cache topology with only node B
ltm.expectTopology(6, singletonList(B), null, CacheTopology.Phase.NO_REBALANCE);
transport.expectCommand(TopologyUpdateCommand.class, c -> {
assertEquals(6, c.getTopologyId());
assertCHMembers(c.getCurrentCH(), B);
assertNull(c.getPendingCH());
});
// The new topology doesn't need rebalancing, so CTMI updates the stable topology
transport.expectCommand(TopologyUpdateStableCommand.class, c -> {
assertEquals(6, c.getTopologyId());
assertCHMembers(c.getCurrentCH(), B);
assertNull(c.getPendingCH());
});
// Shouldn't send any more commands here
Thread.sleep(1);
transport.verifyNoErrors();
// Node A restarts
transport.updateView(4, asList(B, A));
managerNotifier.notifyViewChange(asList(B, A), singletonList(B), A, 4);
// CTMI confirms members are available in case it needs to starts a rebalance
transport.expectHeartBeatCommand().finish();
// Node A rejoins
ctm.handleJoin(CACHE_NAME, A, joinInfoA, 4);
verifyRebalance(transport, ltm, ctm, 7, 4, singletonList(B), asList(B, A));
transport.verifyNoErrors();
gcr.stop();
}
Aggregations