@Test public void testInvalidMessageThrowsError() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); try { activeEntity.invokeActive(context, new InvalidMessage()); fail("Invalid message should result in AssertionError"); } catch (AssertionError e) { assertThat(e.getMessage(), containsString("Unsupported")); } }
@Test public void testActiveMessageTracking() throws Exception { ClusterTierActiveEntity activeEntity = new ClusterTierActiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); EhcacheStateServiceImpl ehcacheStateService = defaultRegistry.getStoreManagerService(); ehcacheStateService.createStore(defaultStoreName, defaultStoreConfiguration, false); //hack to enable message tracking on active TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); assertSuccess(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, defaultStoreConfiguration))); context.incrementCurrentTransactionId(); ServerStoreOpMessage.AppendMessage message = new ServerStoreOpMessage.AppendMessage(1L, createPayload(1L)); EhcacheEntityResponse expected = activeEntity.invokeActive(context, message); // create another message that has the same message ID message = new ServerStoreOpMessage.AppendMessage(1L, createPayload(1L)); EhcacheEntityResponse actual = activeEntity.invokeActive(context, message); // this invoke should be rejected due to duplicate message id assertThat(actual, sameInstance(expected)); }
@Test public void testInvalidMessageThrowsError() throws Exception { ClusterTierPassiveEntity passiveEntity = new ClusterTierPassiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); TestInvokeContext context = new TestInvokeContext(); try { passiveEntity.invokePassive(context, new InvalidMessage()); fail("Invalid message should result in AssertionError"); } catch (AssertionError e) { assertThat(e.getMessage(), containsString("Unsupported")); } }
@Test public void testOversizeChainReplicationMessage() throws Exception { ClusterTierPassiveEntity passiveEntity = new ClusterTierPassiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); passiveEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); long key = 2L; Chain oversizeChain = Util.getChain(true, createPayload(key, 1024 * 1024)); PassiveReplicationMessage oversizeMsg = new PassiveReplicationMessage.ChainReplicationMessage(key, oversizeChain, 2L, 1L, (long) 3); passiveEntity.invokePassive(context, oversizeMsg); // Should be cleared, the value is oversize. assertThat(passiveEntity.getStateService().getStore(passiveEntity.getStoreIdentifier()).get(key).isEmpty(), is(true)); }
@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)); }
@Test public void testOversizeReplaceAtHeadMessage() throws Exception { ClusterTierPassiveEntity passiveEntity = new ClusterTierPassiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); passiveEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); int key = 2; Chain chain = Util.getChain(true, createPayload(1L)); PassiveReplicationMessage message = new PassiveReplicationMessage.ChainReplicationMessage(key, chain, 2L, 1L, 3L); passiveEntity.invokePassive(context, message); Chain oversizeChain = Util.getChain(true, createPayload(2L, 1024 * 1024)); ServerStoreOpMessage.ReplaceAtHeadMessage oversizeMsg = new ServerStoreOpMessage.ReplaceAtHeadMessage(key, chain, oversizeChain); passiveEntity.invokePassive(context, oversizeMsg); // Should be evicted, the value is oversize. assertThat(passiveEntity.getStateService().getStore(passiveEntity.getStoreIdentifier()).get(key).isEmpty(), is(true)); }
@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 testPassiveTracksMessageDuplication() throws Exception { ClusterTierPassiveEntity passiveEntity = new ClusterTierPassiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); passiveEntity.createNew(); Chain chain = Util.getChain(true, createPayload(1L)); TestInvokeContext context = new TestInvokeContext(); long clientId = 3; PassiveReplicationMessage message1 = new PassiveReplicationMessage.ChainReplicationMessage(2, chain, 2L, 1L, clientId); passiveEntity.invokePassive(context, message1); // Should be added assertThat(passiveEntity.getStateService().getStore(passiveEntity.getStoreIdentifier()).get(2).isEmpty(), is(false)); Chain emptyChain = Util.getChain(true); PassiveReplicationMessage message2 = new PassiveReplicationMessage.ChainReplicationMessage(2, emptyChain, 2L, 1L, clientId); passiveEntity.invokePassive(context, message2); // Should not be cleared, message is a duplicate assertThat(passiveEntity.getStateService().getStore(passiveEntity.getStoreIdentifier()).get(2).isEmpty(), is(false)); PassiveReplicationMessage message3 = new PassiveReplicationMessage.ChainReplicationMessage(2, chain, 3L, 1L, clientId); passiveEntity.invokePassive(context, message3); // Should be added as well, different message id assertThat(passiveEntity.getStateService().getStore(passiveEntity.getStoreIdentifier()).get(2).isEmpty(), is(false)); }
activeEntity.createNew(); TestInvokeContext context1 = new TestInvokeContext(); TestInvokeContext context2 = new TestInvokeContext(); TestInvokeContext context3 = new TestInvokeContext(); activeEntity.connected(context1.getClientDescriptor()); activeEntity.connected(context2.getClientDescriptor()); activeEntity.connected(context3.getClientDescriptor()); activeEntity.disconnected(context2.getClientDescriptor()); assertThat(invalidationHolder.clientDescriptorWaitingForInvalidation, is(context1.getClientDescriptor())); assertThat(invalidationHolder.clientsHavingToInvalidate.size(), is(1)); assertThat(invalidationHolder.clientsHavingToInvalidate, contains(context3.getClientDescriptor())); activeEntity.disconnected(context3.getClientDescriptor());
activeEntity.createNew(); TestInvokeContext context1 = new TestInvokeContext(); TestInvokeContext context2 = new TestInvokeContext(); TestInvokeContext context3 = new TestInvokeContext(); activeEntity.connected(context1.getClientDescriptor()); activeEntity.connected(context2.getClientDescriptor()); activeEntity.connected(context3.getClientDescriptor()); activeEntity.disconnected(context2.getClientDescriptor()); assertThat(invalidationHolder.clientDescriptorWaitingForInvalidation, is(context1.getClientDescriptor())); assertThat(invalidationHolder.clientsHavingToInvalidate.size(), is(1)); assertThat(invalidationHolder.clientsHavingToInvalidate, contains(context3.getClientDescriptor())); activeEntity.disconnected(context3.getClientDescriptor());
@Test public void testInvalidMessageThrowsError() throws Exception { final OffHeapIdentifierRegistry registry = new OffHeapIdentifierRegistry(); registry.addResource("serverResource1", 8, MemoryUnit.MEGABYTES); EhcacheStateService ehcacheStateService = registry.getService(new EhcacheStateServiceConfig(blankConfiguration, registry, DEFAULT_MAPPER)); final ClusterTierManagerActiveEntity activeEntity = new ClusterTierManagerActiveEntity(blankConfiguration, ehcacheStateService, management); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); try { activeEntity.invokeActive(context, new InvalidMessage()); fail("Invalid message should result in AssertionError"); } catch (AssertionError e) { assertThat(e.getMessage(), containsString("Unsupported")); } }
@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())); }
activeEntity.createNew(); TestInvokeContext context1 = new TestInvokeContext(); TestInvokeContext context2 = new TestInvokeContext(); TestInvokeContext context3 = new TestInvokeContext(); activeEntity.connected(context1.getClientDescriptor()); activeEntity.connected(context2.getClientDescriptor()); activeEntity.connected(context3.getClientDescriptor()); assertThat(invalidationHolder.clientDescriptorWaitingForInvalidation, is(context1.getClientDescriptor())); assertThat(invalidationHolder.clientsHavingToInvalidate.size(), is(2)); assertThat(invalidationHolder.clientsHavingToInvalidate, containsInAnyOrder(context2.getClientDescriptor(), context3.getClientDescriptor())); assertThat(invalidationHolder.clientDescriptorWaitingForInvalidation, is(context1.getClientDescriptor())); assertThat(invalidationHolder.clientsHavingToInvalidate.size(), is(1)); assertThat(invalidationHolder.clientsHavingToInvalidate, contains(context3.getClientDescriptor()));
activeEntity.createNew(); TestInvokeContext context1 = new TestInvokeContext(); TestInvokeContext context2 = new TestInvokeContext(); TestInvokeContext context3 = new TestInvokeContext(); activeEntity.connected(context1.getClientDescriptor()); activeEntity.connected(context2.getClientDescriptor()); activeEntity.connected(context3.getClientDescriptor()); assertThat(invalidationHolder.clientDescriptorWaitingForInvalidation, is(context1.getClientDescriptor())); assertThat(invalidationHolder.clientsHavingToInvalidate.size(), is(2)); assertThat(invalidationHolder.clientsHavingToInvalidate, containsInAnyOrder(context2.getClientDescriptor(), context3.getClientDescriptor())); assertThat(invalidationHolder.clientDescriptorWaitingForInvalidation, is(context1.getClientDescriptor())); assertThat(invalidationHolder.clientsHavingToInvalidate.size(), is(1)); assertThat(invalidationHolder.clientsHavingToInvalidate, contains(context3.getClientDescriptor()));
@Test public void testValidateSharedServerStore() 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()); assertSuccess(activeEntity.invokeActive(context, new LifecycleMessage.ValidateServerStore(defaultStoreName, storeConfiguration))); assertThat(activeEntity.getConnectedClients(), contains(context.getClientDescriptor())); }
@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 testPrepareForDestroyInProgress() throws Exception { final OffHeapIdentifierRegistry registry = new OffHeapIdentifierRegistry(); registry.addResource("serverResource1", 8, MemoryUnit.MEGABYTES); EhcacheStateService ehcacheStateService = registry.getService(new EhcacheStateServiceConfig(blankConfiguration, registry, DEFAULT_MAPPER)); ehcacheStateService.prepareForDestroy(); final ClusterTierManagerActiveEntity activeEntity = new ClusterTierManagerActiveEntity(blankConfiguration, ehcacheStateService, management); TestInvokeContext context = new TestInvokeContext(); activeEntity.connected(context.getClientDescriptor()); assertFailure(activeEntity.invokeActive(context, MESSAGE_FACTORY.validateStoreManager(null)), DestroyInProgressException.class, "in progress for destroy"); }
@Test public void testValidateUnknown() 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, new ServerStoreConfigBuilder().unknown().build()))); }
@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); }
activeEntity.createNew(); TestInvokeContext context1 = new TestInvokeContext(); TestInvokeContext context2 = new TestInvokeContext(); TestInvokeContext context3 = new TestInvokeContext(); activeEntity.connected(context1.getClientDescriptor()); activeEntity.connected(context2.getClientDescriptor()); activeEntity.connected(context3.getClientDescriptor()); activeEntity.disconnected(context1.getClientDescriptor());