From 308abc5b72c39941bddc48335253268b05e86692 Mon Sep 17 00:00:00 2001 From: Rob Austin Date: Tue, 11 Nov 2014 14:01:00 +0000 Subject: [PATCH] HCOLL-200 Set tcp replication dirty when using acquireUsing --- .../chronicle/map/ReplicatedChronicleMap.java | 19 ++- .../map/TCPSocketReplicationNativeTest.java | 149 ++++++++++++++++++ 2 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 src/test/java/net/openhft/chronicle/map/TCPSocketReplicationNativeTest.java diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index f9e3f1989..d21e669b1 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -418,11 +418,14 @@ final class ReplicatedChronicleMap, final long initialLimit = entry.limit(); final long keySize = keySizeMarshaller.readSize(entry); + + final long keyPosition = entry.position(); entry.skip(keySize); final long keyLimit = entry.position(); final long timeStamp = entry.readLong(); + final byte identifier = entry.readByte(); if (identifier != localIdentifier) { // although unlikely, this may occur if the entry has been updated @@ -440,11 +443,12 @@ final class ReplicatedChronicleMap, final long valuePosition = entry.position(); keySizeMarshaller.writeSize(destination, keySize); + System.out.println("keySize= " + keySize); + valueSizeMarshaller.writeSize(destination, valueSize); destination.writeStopBit(timeStamp); - // we store the isDeleted flag in the identifier - // ( when the identifier is negative it is deleted ) + destination.writeByte(identifier); destination.writeBoolean(isDeleted); @@ -491,8 +495,19 @@ final class ReplicatedChronicleMap, public void readExternalEntry(@NotNull Bytes source) { final long keySize = keySizeMarshaller.readSize(source); + System.out.println("read keySize= " + keySize); + + // long valueSize = source.readStopBit(); + final long valueSize = valueSizeMarshaller.readSize(source); + System.out.println("read valueSize= " + valueSize); + + final long timeStamp = source.readStopBit(); + + + System.out.println("read timestamp= " + timeStamp); + final byte id = source.readByte(); final boolean isDeleted = source.readBoolean(); diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationNativeTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationNativeTest.java new file mode 100644 index 000000000..d84fe0f53 --- /dev/null +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationNativeTest.java @@ -0,0 +1,149 @@ +/* + * Copyright 2014 Higher Frequency Trading + * + * http://www.higherfrequencytrading.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.openhft.chronicle.map; + +import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; +import net.openhft.lang.model.DataValueClasses; +import net.openhft.lang.values.LongValue; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.Closeable; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Test ReplicatedChronicleMap where the Replicated is over a TCP Socket + * + * @author Rob Austin. + */ + +public class TCPSocketReplicationNativeTest { + + + private ChronicleMap map1; + private ChronicleMap map2; + + + @Before + public void setup() throws IOException { + InetSocketAddress endpoint = new InetSocketAddress("localhost", 8077); + + TcpTransportAndNetworkConfig tcpConfig1 = TcpTransportAndNetworkConfig.forSendingNode(8076, endpoint) + .heartBeatInterval(1L, TimeUnit.SECONDS); + + map1 = OffHeapUpdatableChronicleMapBuilder.of(Integer.class, LongValue.class) + .replication((byte) 1, tcpConfig1).create(); + + TcpTransportAndNetworkConfig tcpConfig2 = TcpTransportAndNetworkConfig.forReceivingOnlyNode(8077) + .heartBeatInterval(1L, TimeUnit.SECONDS); + + map2 = OffHeapUpdatableChronicleMapBuilder.of(Integer.class, LongValue.class) + .replication((byte) 2, tcpConfig2).create(); + + } + + @After + public void tearDown() throws InterruptedException { + + for (final Closeable closeable : new Closeable[]{map1, map2}) { + try { + closeable.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + + @Test + public void test() throws IOException, InterruptedException { + + LongValue value = DataValueClasses.newDirectReference(LongValue.class); + + try (WriteContext c = map1 + .acquireUsingLocked(1, value)) { + + value.setValue(10); + } + + + + // allow time for the recompilation to resolve + waitTillEqual(5000); + + assertEquals(map1, map2); + assertTrue(!map1.isEmpty()); + + } + + + + @Test + public void test2Puts() throws IOException, InterruptedException { + + LongValue value = DataValueClasses.newDirectReference(LongValue.class); + + try (WriteContext c = map1 + .acquireUsingLocked(1, value)) { + + value.setValue(10); + } + + try (WriteContext c = map1 + .acquireUsingLocked(1, value)) { + + value.setValue(20); + } + + // allow time for the recompilation to resolve + waitTillEqual(5000); + + assertEquals(map1, map2); + assertTrue(!map1.isEmpty()); + + } + + /** + * + * * waits until map1 and map2 show the same value + * + * @param timeOutMs timeout in milliseconds + * @throws InterruptedException + */ + private void waitTillEqual(final int timeOutMs) throws InterruptedException { + int t = 0; + for (; t < timeOutMs; t++) { + if (map1.equals(map2)) + break; + Thread.sleep(1); + } + + } + + +} + + +