public Chain build() { ByteBuffer[] elements = new ByteBuffer[buffers.size()]; buffers.toArray(elements); return Util.getChain(false, elements); }
private Chain dropLastElement(Chain chain) { if (!chain.isEmpty()) { List<Element> elements = StreamSupport.stream(chain.spliterator(), false) .collect(Collectors.toList()); elements.remove(elements.size() - 1); // remove last return Util.getChain(elements); } else { return chain; } }
public static Chain getChain(boolean isSequenced, ByteBuffer... buffers) { List<Element> elements = new ArrayList<>(); long counter = 0; for (final ByteBuffer buffer : buffers) { if (isSequenced) { elements.add(getElement(counter++, buffer)); } else { elements.add(getElement(buffer)); } } return getChain(elements); }
private LockSuccess getLockSuccessResponse() { ByteBuffer[] buffers = new ByteBuffer[3]; for (int i = 1; i <= 3; i++) { buffers[i-1] = Util.createPayload(i); } Chain chain = Util.getChain(false, buffers); return EhcacheEntityResponse.lockSuccess(chain); }
@Test public void untrackedMessageAreNotStored() throws Exception { // Nothing tracked assertThat(activeMessageHandler.getTrackedClients().count()).isZero(); List<Element> elements = new ArrayList<>(1); elements.add(getElement(createPayload(44L))); // Send a replace message, those are not tracked storeProxy.replaceAtHead(44L, getChain(elements), getChain(new ArrayList<>(0))); // Not tracked as well storeProxy.get(42L); assertThat(activeMessageHandler.getTrackedClients().count()).isZero(); }
@Test public void testConcurrencyKeysEqualForSameCacheAndKey() throws Exception { ConcurrentEntityMessage m1 = new ServerStoreOpMessage.AppendMessage(1L, createPayload(1L)); ConcurrentEntityMessage m2 = new ServerStoreOpMessage.GetAndAppendMessage(1L, createPayload(1L)); ConcurrentEntityMessage m3 = new ServerStoreOpMessage.ReplaceAtHeadMessage(1L, getChain(Collections.<Element>emptyList()), getChain(Collections.<Element>emptyList())); assertThat(m1.concurrencyKey(), is(m2.concurrencyKey())); assertThat(m2.concurrencyKey(), is(m3.concurrencyKey())); }
@Test public void testGetValuePresentInCache() throws Exception { ServerStoreProxy storeProxy = mock(LockingServerStoreProxy.class); @SuppressWarnings("unchecked") CacheLoaderWriter<Long, String> loaderWriter = mock(CacheLoaderWriter.class); PutOperation<Long, String> operation = new PutOperation<>(1L, "one", System.currentTimeMillis()); Chain toReturn = Util.getChain(false, codec.encode(operation)); when(storeProxy.get(anyLong())).thenReturn(toReturn); ClusteredLoaderWriterStore<Long, String> store = new ClusteredLoaderWriterStore<>(configuration, codec, resolver, storeProxy, timeSource, loaderWriter); assertThat(store.get(1L).get(), equalTo("one")); verify(loaderWriter, times(0)).load(anyLong()); verifyZeroInteractions(loaderWriter); }
@Test public void testReplace2ArgsValuePresentInCachePresentInSOR() throws Exception { LockingServerStoreProxy storeProxy = mock(LockingServerStoreProxy.class); @SuppressWarnings("unchecked") CacheLoaderWriter<Long, String> loaderWriter = mock(CacheLoaderWriter.class); PutOperation<Long, String> operation = new PutOperation<>(1L, "one", System.currentTimeMillis()); Chain toReturn = Util.getChain(false, codec.encode(operation)); when(storeProxy.lock(anyLong())).thenReturn(toReturn); ClusteredLoaderWriterStore<Long, String> store = new ClusteredLoaderWriterStore<>(configuration, codec, resolver, storeProxy, timeSource, loaderWriter); assertThat(store.replace(1L, "one", "Again"), is(Store.ReplaceStatus.HIT)); verify(storeProxy, times(1)).append(anyLong(), ArgumentMatchers.any(ByteBuffer.class)); verify(loaderWriter, times(0)).load(anyLong()); verify(loaderWriter, times(1)).write(anyLong(), anyString()); }
@Test public void testReplaceValuePresentInCachePresentInSOR() throws Exception { LockingServerStoreProxy storeProxy = mock(LockingServerStoreProxy.class); TestCacheLoaderWriter loaderWriter = new TestCacheLoaderWriter(); PutOperation<Long, String> operation = new PutOperation<>(1L, "one", System.currentTimeMillis()); Chain toReturn = Util.getChain(false, codec.encode(operation)); when(storeProxy.lock(anyLong())).thenReturn(toReturn); ClusteredLoaderWriterStore<Long, String> store = new ClusteredLoaderWriterStore<>(configuration, codec, resolver, storeProxy, timeSource, loaderWriter); loaderWriter.storeMap.put(1L, "one"); assertThat(store.replace(1L, "Again").get(), equalTo("one")); verify(storeProxy, times(1)).append(anyLong(), ArgumentMatchers.any(ByteBuffer.class)); assertThat(loaderWriter.storeMap.get(1L), equalTo("Again")); }
@Test public void testPufIfAbsentValuePresentInCachePresentInSOR() throws Exception { LockingServerStoreProxy storeProxy = mock(LockingServerStoreProxy.class); TestCacheLoaderWriter loaderWriter = new TestCacheLoaderWriter(); PutOperation<Long, String> operation = new PutOperation<>(1L, "one", System.currentTimeMillis()); Chain toReturn = Util.getChain(false, codec.encode(operation)); when(storeProxy.lock(anyLong())).thenReturn(toReturn); ClusteredLoaderWriterStore<Long, String> store = new ClusteredLoaderWriterStore<>(configuration, codec, resolver, storeProxy, timeSource, loaderWriter); loaderWriter.storeMap.put(1L, "one"); assertThat(store.putIfAbsent(1L, "Again", null).get(), equalTo("one")); verify(storeProxy, times(0)).append(anyLong(), ArgumentMatchers.any(ByteBuffer.class)); assertThat(loaderWriter.storeMap.get(1L), equalTo("one")); }
@Test public void testEmptyChain() { Chain decoded = ChainCodec.decode(ChainCodec.encode(getChain(false))); assertThat(decoded.isEmpty(), is(true)); }
@Test public void testChainWithSingleElement() { Chain chain = getChain(false, createPayload(1L)); assertThat(chain.isEmpty(), is(false)); Iterator<Element> chainIterator = chain.iterator(); assertThat(readPayLoad(chainIterator.next().getPayload()), is(1L)); assertThat(chainIterator.hasNext(), is(false)); Chain decoded = ChainCodec.decode(ChainCodec.encode(chain)); assertThat(decoded.isEmpty(), is(false)); chainIterator = decoded.iterator(); assertThat(readPayLoad(chainIterator.next().getPayload()), is(1L)); assertThat(chainIterator.hasNext(), is(false)); }
@Test public void testReplaceAtHeadMessageCodec() { ServerStoreOpMessage replaceAtHeadMessage = new ServerStoreOpMessage.ReplaceAtHeadMessage(10L, getChain(true, createPayload(10L), createPayload(100L), createPayload(1000L)), getChain(false, createPayload(2000L))); byte[] encoded = STORE_OP_CODEC.encode(replaceAtHeadMessage); EhcacheEntityMessage decodedMsg = STORE_OP_CODEC.decode(replaceAtHeadMessage.getMessageType(), wrap(encoded)); ServerStoreOpMessage.ReplaceAtHeadMessage decodedReplaceAtHeadMessage = (ServerStoreOpMessage.ReplaceAtHeadMessage) decodedMsg; assertThat(decodedReplaceAtHeadMessage.getKey(), is(10L)); Util.assertChainHas(decodedReplaceAtHeadMessage.getExpect(), 10L, 100L, 1000L); Util.assertChainHas(decodedReplaceAtHeadMessage.getUpdate(), 2000L); assertThat(decodedReplaceAtHeadMessage.getMessageType(), is(EhcacheMessageType.REPLACE)); }
@Test public void testResolveRequest() throws Exception { long hash = 42L; EhcacheEntityResponse.ResolveRequest response = new EhcacheEntityResponse.ResolveRequest(hash, getChain(false, createPayload(1L), createPayload(11L), createPayload(111L))); byte[] encoded = RESPONSE_CODEC.encode(response); EhcacheEntityResponse.ResolveRequest decodedResponse = (EhcacheEntityResponse.ResolveRequest) RESPONSE_CODEC.decode(encoded); assertThat(decodedResponse.getResponseType(), is(EhcacheResponseType.RESOLVE_REQUEST)); assertThat(decodedResponse.getKey(), is(42L)); Util.assertChainHas(decodedResponse.getChain(), 1L, 11L, 111L); }
@Test public void testChainWithMultipleSequencedElements() { Chain chain = getChain(true, createPayload(1L), createPayload(2L), createPayload(3L)); assertThat(chain.isEmpty(), is(false)); Util.assertChainHas(chain, 1L, 2L, 3L); Chain decoded = ChainCodec.decode(ChainCodec.encode(chain)); assertThat(decoded.isEmpty(), is(false)); Util.assertChainHas(decoded, 1L, 2L, 3L); assertSameSequenceChain(chain, decoded); }
@Test public void testChainWithMultipleElements() { Chain chain = getChain(false, createPayload(1L), createPayload(2L), createPayload(3L)); assertThat(chain.isEmpty(), is(false)); Util.assertChainHas(chain, 1L, 2L, 3L); Chain decoded = ChainCodec.decode(ChainCodec.encode(chain)); assertThat(decoded.isEmpty(), is(false)); Util.assertChainHas(decoded, 1L, 2L, 3L); }
@Test public void testChainReplicationMessageCodec() { Chain chain = getChain(false, createPayload(2L), createPayload(20L)); ChainReplicationMessage chainReplicationMessage = new ChainReplicationMessage(2L, chain, 200L, 100L, 1L); byte[] encoded = codec.encode(chainReplicationMessage); ChainReplicationMessage decodedMsg = (ChainReplicationMessage) codec.decode(EhcacheMessageType.CHAIN_REPLICATION_OP, wrap(encoded)); assertThat(decodedMsg.getClientId(), is(chainReplicationMessage.getClientId())); assertThat(decodedMsg.getTransactionId(), is(chainReplicationMessage.getTransactionId())); assertThat(decodedMsg.getOldestTransactionId(), is(chainReplicationMessage.getOldestTransactionId())); assertThat(decodedMsg.getKey(), is(chainReplicationMessage.getKey())); assertTrue(chainsEqual(decodedMsg.getChain(), chainReplicationMessage.getChain())); }
@Test public void testLockResponse() { EhcacheEntityResponse.LockSuccess lockSuccess = new EhcacheEntityResponse.LockSuccess(getChain(false, createPayload(1L), createPayload(10L))); byte[] sucessEncoded = RESPONSE_CODEC.encode(lockSuccess); EhcacheEntityResponse.LockSuccess successDecoded = (EhcacheEntityResponse.LockSuccess) RESPONSE_CODEC.decode(sucessEncoded); assertThat(successDecoded.getResponseType(), is(EhcacheResponseType.LOCK_SUCCESS)); Util.assertChainHas(successDecoded.getChain(), 1L, 10L); EhcacheEntityResponse.LockFailure lockFailure = EhcacheEntityResponse.lockFailure(); byte[] failureEncoded = RESPONSE_CODEC.encode(lockFailure); EhcacheEntityResponse.LockFailure failureDecoded = (EhcacheEntityResponse.LockFailure) RESPONSE_CODEC.decode(failureEncoded); assertThat(failureDecoded.getResponseType(), is(EhcacheResponseType.LOCK_FAILURE)); } }
@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 testGetResponseCodec() { EhcacheEntityResponse getResponse = getResponse(getChain(false, createPayload(1L), createPayload(11L), createPayload(111L))); EhcacheEntityResponse decoded = RESPONSE_CODEC.decode(RESPONSE_CODEC.encode(getResponse)); Chain decodedChain = ((EhcacheEntityResponse.GetResponse) decoded).getChain(); Util.assertChainHas(decodedChain, 1L, 11L, 111L); }