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); }
@Override public Iterator<Element> reverseIterator() { return Util.reverseIterator(elements); }
private <T> void unmarshallingCheck(T t, Predicate<Class<?>> isClassPermitted) { @SuppressWarnings("unchecked") T unmarshalled = (T) Util.unmarshall(ByteBuffer.wrap(Util.marshall(t)), isClassPermitted); Assert.assertThat(unmarshalled, Matchers.is(t)); }
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); }
public Chain build() { ByteBuffer[] elements = new ByteBuffer[buffers.size()]; buffers.toArray(elements); return Util.getChain(false, elements); }
@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 testDataSyncMessageEncodeDecode() throws Exception { Map<Long, Chain> chainMap = new HashMap<>(); Chain chain = getChain(true, createPayload(10L), createPayload(100L), createPayload(1000L)); chainMap.put(1L, chain); chainMap.put(2L, chain); chainMap.put(3L, chain); EhcacheDataSyncMessage message = new EhcacheDataSyncMessage(chainMap); byte[] encodedMessage = codec.encode(0, message); EhcacheDataSyncMessage decoded = (EhcacheDataSyncMessage) codec.decode(0, encodedMessage); Map<Long, Chain> decodedChainMap = decoded.getChainMap(); assertThat(decodedChainMap).hasSize(3); assertThat(chainsEqual(decodedChainMap.get(1L), chain)).isTrue(); assertThat(chainsEqual(decodedChainMap.get(2L), chain)).isTrue(); assertThat(chainsEqual(decodedChainMap.get(3L), chain)).isTrue(); }
@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 testConcurrencyKeysEqualForDifferentCachesSameKey() throws Exception { ConcurrentEntityMessage m1 = new ServerStoreOpMessage.AppendMessage(1L, createPayload(1L)); ConcurrentEntityMessage m2 = new ServerStoreOpMessage.GetAndAppendMessage(1L, createPayload(1L)); assertThat(m1.concurrencyKey(), is(m2.concurrencyKey())); }
private byte[] encodeStateRepoSync(EhcacheStateRepoSyncMessage syncMessage) { StructEncoder<Void> encoder = STATE_REPO_SYNC_STRUCT.encoder(); encoder.enm(SYNC_MESSAGE_TYPE_FIELD_NAME, STATE_REPO) .string(SERVER_STORE_NAME_FIELD, syncMessage.getCacheId()) .string(STATE_REPO_MAP_NAME_FIELD, syncMessage.getMapId()); encoder.structs(STATE_REPO_ENTRIES_SUB_STRUCT, syncMessage.getMappings().entrySet(), (entryEncoder, entry) -> entryEncoder.byteBuffer(KEY_FIELD, wrap(marshall(entry.getKey()))) .byteBuffer(STATE_REPO_VALUE_FIELD, wrap(marshall(entry.getValue())))); return encoder.encode().array(); }
private StateRepositoryOpMessage.PutIfAbsentMessage decodePutIfAbsentMessage(ByteBuffer messageBuffer) { StructDecoder<Void> decoder = PUT_IF_ABSENT_MESSAGE_STRUCT.decoder(messageBuffer); String storeName = decoder.string(SERVER_STORE_NAME_FIELD); String mapId = decoder.string(MAP_ID_FIELD); ByteBuffer keyBuffer = decoder.byteBuffer(KEY_FIELD); Object key = Util.unmarshall(keyBuffer, WHITELIST_PREDICATE); ByteBuffer valueBuffer = decoder.byteBuffer(VALUE_FIELD); Object value = Util.unmarshall(valueBuffer, WHITELIST_PREDICATE); return new StateRepositoryOpMessage.PutIfAbsentMessage(storeName, mapId, key, value); }
@Test public void testAppendKeyNotPresent() throws Exception { ClusterTierClientEntity clientEntity = createClientEntity("testAppendKeyNotPresent"); CommonServerStoreProxy serverStoreProxy = new CommonServerStoreProxy("testAppendKeyNotPresent", clientEntity, mock(ServerCallback.class)); serverStoreProxy.append(2, createPayload(2)); Chain chain = serverStoreProxy.get(2); assertThat(chain.isEmpty(), is(false)); assertThat(readPayLoad(chain.iterator().next().getPayload()), is(2L)); }
serverStoreProxy1.append(i, createPayload(i, 512 * 1024)); Chain elements1 = serverStoreProxy1.get(i); Chain elements2 = serverStoreProxy2.get(i); assertThat(chainsEqual(elements1, elements2), is(true)); if (!elements1.isEmpty()) { entryCount++;
private static void assertChainHas(Chain chain, long... payLoads) { Iterator<Element> elements = chain.iterator(); for (long payLoad : payLoads) { assertThat(readPayLoad(elements.next().getPayload()), is(Long.valueOf(payLoad))); } assertThat(elements.hasNext(), is(false)); }
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 decode(StructDecoder<?> decoder) { StructArrayDecoder<? extends StructDecoder<?>> elementsDecoder = decoder.structs("elements"); final List<Element> elements = new ArrayList<>(); for (int i = 0; i < elementsDecoder.length(); i++) { StructDecoder<?> elementDecoder = elementsDecoder.next(); Long sequence = elementDecoder.int64("sequence"); ByteBuffer byteBuffer = elementDecoder.byteBuffer("payload"); elementDecoder.end(); if (sequence == null) { elements.add(Util.getElement(byteBuffer)); } else { elements.add(Util.getElement(sequence, byteBuffer)); } } elementsDecoder.end(); return Util.getChain(elements); } }
@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 testChainWithSingleSequencedElement() { Chain chain = getChain(true, 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)); assertSameSequenceChain(chain, decoded); }
@Test public void testGetAndAppendMultipleTimesOnSameKey() throws Exception { ClusterTierClientEntity clientEntity = createClientEntity("testGetAndAppendMultipleTimesOnSameKey"); CommonServerStoreProxy serverStoreProxy = new CommonServerStoreProxy("testGetAndAppendMultipleTimesOnSameKey", clientEntity, mock(ServerCallback.class)); serverStoreProxy.getAndAppend(5L, createPayload(5L)); serverStoreProxy.getAndAppend(5L, createPayload(55L)); serverStoreProxy.getAndAppend(5L, createPayload(555L)); Chain chain = serverStoreProxy.getAndAppend(5l, createPayload(5555L)); assertThat(chain.isEmpty(), is(false)); assertChainHas(chain, 5L, 55L, 555L); }
private byte[] encodePutIfAbsentMessage(StateRepositoryOpMessage.PutIfAbsentMessage message) { StructEncoder<Void> encoder = PUT_IF_ABSENT_MESSAGE_STRUCT.encoder(); messageCodecUtils.encodeMandatoryFields(encoder, message); encoder.string(SERVER_STORE_NAME_FIELD, message.getCacheId()); encoder.string(MAP_ID_FIELD, message.getCacheId()); // TODO this needs to change - serialization needs to happen in the StateRepo not here, though we need the hashcode for server side comparison. encoder.byteBuffer(KEY_FIELD, wrap(Util.marshall(message.getKey()))); encoder.byteBuffer(VALUE_FIELD, wrap(Util.marshall(message.getValue()))); return encoder.encode().array(); }