@Test public void testSyncToPassiveBatchedByDefault() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); assertSuccess(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration))); ByteBuffer payload = ByteBuffer.allocate(512); // Put keys that maps to the same concurrency key assertSuccess(activeEntity.invokeActive(context, new ServerStoreOpMessage.AppendMessage(1L, payload))); assertSuccess(activeEntity.invokeActive(context, new ServerStoreOpMessage.AppendMessage(-2L, payload))); assertSuccess(activeEntity.invokeActive(context, new ServerStoreOpMessage.AppendMessage(17L, payload))); @SuppressWarnings("unchecked") PassiveSynchronizationChannel<EhcacheEntityMessage> syncChannel = mock(PassiveSynchronizationChannel.class); activeEntity.synchronizeKeyToPassive(syncChannel, 3); verify(syncChannel).synchronizeToPassive(any(EhcacheDataSyncMessage.class)); }
@Test public void testLoadExistingRegistersEvictionListener() throws Exception { EhcacheStateService stateService = mock(EhcacheStateService.class); ServerSideServerStore store = mock(ServerSideServerStore.class); when(stateService.loadStore(eq(defaultStoreName), any())).thenReturn(store); IEntityMessenger<EhcacheEntityMessage, EhcacheEntityResponse> entityMessenger = mock(IEntityMessenger.class); ServiceRegistry registry = getCustomMockedServiceRegistry(stateService, null, entityMessenger, null, null); ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(registry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.loadExisting(); verify(store).setEvictionListener(any(ServerStoreEvictionListener.class)); }
@Test public void testDataSyncToPassiveCustomBatchSize() throws Exception { System.setProperty(ClusterTierActiveEntity.SYNC_DATA_SIZE_PROP, "512"); try { prepareAndRunActiveEntityForPassiveSync((activeEntity, concurrencyKey) -> { @SuppressWarnings("unchecked") PassiveSynchronizationChannel<EhcacheEntityMessage> syncChannel = mock(PassiveSynchronizationChannel.class); activeEntity.synchronizeKeyToPassive(syncChannel, concurrencyKey); verify(syncChannel, atLeast(2)).synchronizeToPassive(any(EhcacheDataSyncMessage.class)); }); } finally { System.clearProperty(ClusterTierActiveEntity.SYNC_DATA_SIZE_PROP); } }
assertSuccess( activeEntity.invokeActive(context1, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context2, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context3, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context1, new ServerStoreOpMessage.ClearMessage()) ); assertSuccess( activeEntity.invokeActive(context2, new ServerStoreOpMessage.ClientInvalidationAllAck(activeEntity.getClientsWaitingForInvalidation().keySet().iterator().next())) ); assertSuccess( activeEntity.invokeActive(context3, new ServerStoreOpMessage.ClientInvalidationAllAck(activeEntity.getClientsWaitingForInvalidation().keySet().iterator().next())) );
@Test public void testValidateDedicatedServerStoreBad() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); assertFailure(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, new ServerStoreConfigBuilder() .dedicated(defaultResource, 8, MemoryUnit.MEGABYTES) .build())), InvalidServerStoreConfigurationException.class); }
@Test(expected = ConfigurationException.class) public void testConfigNull() throws Exception { new ClusterTierActiveEntity(mock(ServiceRegistry.class), null, DEFAULT_MAPPER); }
@Test @SuppressWarnings("unchecked") public void testReplicationMessageAndOriginalServerStoreOpMessageHasSameConcurrency() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); IEntityMessenger<EhcacheEntityMessage, EhcacheEntityResponse> entityMessenger = defaultRegistry.getEntityMessenger(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); assertSuccess(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration))); reset(entityMessenger); EhcacheEntityMessage getAndAppend = new ServerStoreOpMessage.GetAndAppendMessage(1L, createPayload(1L)); activeEntity.invokeActive(context, getAndAppend); ArgumentCaptor<PassiveReplicationMessage.ChainReplicationMessage> captor = ArgumentCaptor.forClass(PassiveReplicationMessage.ChainReplicationMessage.class); verify(entityMessenger).messageSelfAndDeferRetirement(isNotNull(), captor.capture()); PassiveReplicationMessage.ChainReplicationMessage replicatedMessage = captor.getValue(); assertThat(replicatedMessage.concurrencyKey(), is(((ConcurrentEntityMessage) getAndAppend).concurrencyKey())); }
@Test public void testValidateServerStore_DedicatedStoresDifferentSizes() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); ServerStoreConfiguration storeConfiguration = new ServerStoreConfigBuilder() .dedicated(defaultResource, 2, MemoryUnit.MEGABYTES) .build(); String expectedMessageContent = "Existing ServerStore configuration is not compatible with the desired configuration: " + "\n\t" + "resourcePoolType existing: " + defaultStoreConfiguration.getPoolAllocation() + ", desired: " + storeConfiguration.getPoolAllocation(); assertFailure(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, storeConfiguration)), InvalidServerStoreConfigurationException.class, expectedMessageContent); }
if (serviceConfiguration.getServiceType().equals(ClientCommunicator.class)) { if (this.clientCommunicator == null) { this.clientCommunicator = mock(ClientCommunicator.class); } else if (serviceConfiguration.getServiceType().equals(IEntityMessenger.class)) { if (this.entityMessenger == null) { this.entityMessenger = mock(IEntityMessenger.class);
assertSuccess( activeEntity.invokeActive(context1, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context2, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context3, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context1, new ServerStoreOpMessage.AppendMessage(1L, createPayload(1L))) ); assertSuccess( activeEntity.invokeActive(context2, new ServerStoreOpMessage.ClientInvalidationAck(1L, activeEntity.getClientsWaitingForInvalidation().keySet().iterator().next())) ); assertSuccess( activeEntity.invokeActive(context3, new ServerStoreOpMessage.ClientInvalidationAck(1L, activeEntity.getClientsWaitingForInvalidation().keySet().iterator().next())) );
@Test public void testDataSyncToPassiveCustomGets() throws Exception { System.setProperty(ClusterTierActiveEntity.SYNC_DATA_GETS_PROP, "2"); try { prepareAndRunActiveEntityForPassiveSync((activeEntity, concurrencyKey) -> { @SuppressWarnings("unchecked") PassiveSynchronizationChannel<EhcacheEntityMessage> syncChannel = mock(PassiveSynchronizationChannel.class); activeEntity.synchronizeKeyToPassive(syncChannel, concurrencyKey); verify(syncChannel, atLeast(2)).synchronizeToPassive(any(EhcacheDataSyncMessage.class)); }); } finally { System.clearProperty(ClusterTierActiveEntity.SYNC_DATA_GETS_PROP); } }
@Test public void testSyncToPassiveNoData() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); assertSuccess(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration))); @SuppressWarnings("unchecked") PassiveSynchronizationChannel<EhcacheEntityMessage> syncChannel = mock(PassiveSynchronizationChannel.class); activeEntity.synchronizeKeyToPassive(syncChannel, 3); verifyZeroInteractions(syncChannel); }
@Test public void testValidateServerStore_DedicatedStoreResourceNamesDifferent() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); ServerStoreConfiguration storeConfiguration = new ServerStoreConfigBuilder() .dedicated("otherResource", 1, MemoryUnit.MEGABYTES) .build(); String expectedMessageContent = "Existing ServerStore configuration is not compatible with the desired configuration: " + "\n\t" + "resourcePoolType existing: " + defaultStoreConfiguration.getPoolAllocation() + ", desired: " + storeConfiguration.getPoolAllocation(); assertFailure(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, storeConfiguration)), InvalidServerStoreConfigurationException.class, expectedMessageContent); }
assertSuccess( activeEntity.invokeActive(context1, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context2, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context3, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context1, new ServerStoreOpMessage.ClearMessage()) );
@Test public void testDataSyncToPassiveException() throws Exception { System.setProperty(ClusterTierActiveEntity.SYNC_DATA_GETS_PROP, "1"); try { prepareAndRunActiveEntityForPassiveSync((activeEntity, concurrencyKey) -> { @SuppressWarnings("unchecked") PassiveSynchronizationChannel<EhcacheEntityMessage> syncChannel = mock(PassiveSynchronizationChannel.class); activeEntity.destroy(); try { activeEntity.synchronizeKeyToPassive(syncChannel, concurrencyKey); fail("Destroyed entity not expected to sync"); } catch (RuntimeException e) { assertThat(e.getCause(), instanceOf(ExecutionException.class)); } }); } finally { System.clearProperty(ClusterTierActiveEntity.SYNC_DATA_GETS_PROP); } }
@Test public void testValidateServerStore_DifferentSharedPools() throws Exception { defaultRegistry.addSharedPool(defaultSharedPool, MemoryUnit.MEGABYTES.toBytes(2), defaultResource); ServerStoreConfiguration storeConfiguration = new ServerStoreConfigBuilder() .shared(defaultSharedPool) .build(); ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, new ClusterTierEntityConfiguration(identifier, defaultStoreName, storeConfiguration), DEFAULT_MAPPER); activeEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); ServerStoreConfiguration otherConfiguration = new ServerStoreConfigBuilder() .shared("other") .build(); String expectedMessageContent = "Existing ServerStore configuration is not compatible with the desired configuration: " + "\n\t" + "resourcePoolType existing: " + storeConfiguration.getPoolAllocation() + ", desired: " + otherConfiguration.getPoolAllocation(); assertFailure(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, otherConfiguration)),InvalidServerStoreConfigurationException.class,expectedMessageContent); }
@Test public void testWithAttachmentSucceedsInvokingServerStoreOperation() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); // attach to the store assertSuccess( activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context, new ServerStoreOpMessage.AppendMessage(1L, createPayload(1L))) ); EhcacheEntityResponse response = activeEntity.invokeActive(context, new ServerStoreOpMessage.GetMessage(1L)); assertThat(response, instanceOf(EhcacheEntityResponse.GetResponse.class)); EhcacheEntityResponse.GetResponse getResponse = (EhcacheEntityResponse.GetResponse) response; assertThat(getResponse.getChain().isEmpty(), is(false)); }
assertSuccess( activeEntity.invokeActive(context1, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context2, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context3, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertSuccess( activeEntity.invokeActive(context1, new ServerStoreOpMessage.AppendMessage(1L, createPayload(1L))) );
@Test public void testCreateDedicatedServerStore() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); assertThat(defaultRegistry.getStoreManagerService().getDedicatedResourcePoolIds(), containsInAnyOrder(defaultStoreName)); assertThat(defaultRegistry.getResource(defaultResource).getUsed(), is(MemoryUnit.MEGABYTES.toBytes(1L))); assertThat(activeEntity.getConnectedClients(), empty()); assertThat(defaultRegistry.getStoreManagerService().getStores(), containsInAnyOrder(defaultStoreName)); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); assertSuccess( activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration)) ); assertThat(activeEntity.getConnectedClients(), contains(context.getClientDescriptor())); /* * Ensure the dedicated resource pool remains after client disconnect. */ activeEntity.disconnected(context.getClientDescriptor()); assertThat(defaultRegistry.getStoreManagerService().getDedicatedResourcePoolIds(), containsInAnyOrder(defaultStoreName)); assertThat(activeEntity.getConnectedClients(), empty()); assertThat(defaultRegistry.getStoreManagerService().getStores(), containsInAnyOrder(defaultStoreName)); }
@Test public void testValidateDedicatedServerStore() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); activeEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); TestInvokeContext context2 = new TestInvokeContext(); activeEntity.connected(context2.getClientDescriptor()); assertSuccess(activeEntity.invokeActive(context2, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration))); assertThat(defaultRegistry.getStoreManagerService().getDedicatedResourcePoolIds(), containsInAnyOrder(defaultStoreName)); assertThat(defaultRegistry.getResource(defaultResource).getUsed(), is(MemoryUnit.MEGABYTES.toBytes(1L))); assertThat(activeEntity.getConnectedClients(), hasSize(2)); assertThat(activeEntity.getConnectedClients(), containsInAnyOrder(context.getClientDescriptor(), context2.getClientDescriptor())); assertThat(defaultRegistry.getStoreManagerService().getStores(), contains(defaultStoreName)); }