diff --git a/pom.xml b/pom.xml index a8cf2bda9..84bebb548 100755 --- a/pom.xml +++ b/pom.xml @@ -47,7 +47,7 @@ net.openhft chronicle-bom pom - 1.14.10-SNAPSHOT + 1.14.14-SNAPSHOT import diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index fb4f14fca..46d60084c 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -43,7 +43,10 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReferenceArray; import java.util.stream.Stream; -import static net.openhft.chronicle.algo.MemoryUnit.*; +import static net.openhft.chronicle.algo.MemoryUnit.BITS; +import static net.openhft.chronicle.algo.MemoryUnit.BYTES; +import static net.openhft.chronicle.algo.MemoryUnit.CACHE_LINES; +import static net.openhft.chronicle.algo.MemoryUnit.LONGS; import static net.openhft.chronicle.algo.bitset.BitSetFrame.NOT_FOUND; import static net.openhft.chronicle.algo.bytes.Access.nativeAccess; import static net.openhft.chronicle.hash.replication.TimeProvider.currentTime; @@ -475,8 +478,9 @@ public class ReplicatedChronicleMap extends VanillaChronicleMap { if (entry != null) { if (decideOnRemoteModification(entry, q) == ACCEPT) { q.replaceValue(entry, newValue); + System.out.printf("Map %d, replicating(%s, %s)%n", q.remoteNodeIdentifier(), q.queriedKey().get(), q.entry().value().get()); entry.updateOrigin(q.remoteIdentifier(), q.remoteTimestamp()); // For explanation see similar block in remove() method (*) if (q.remoteIdentifier() == q.currentNodeIdentifier()) { diff --git a/src/test/java/net/openhft/chronicle/map/BasicReplicationTest.java b/src/test/java/net/openhft/chronicle/map/BasicReplicationTest.java new file mode 100644 index 000000000..873022f9a --- /dev/null +++ b/src/test/java/net/openhft/chronicle/map/BasicReplicationTest.java @@ -0,0 +1,215 @@ +package net.openhft.chronicle.map; + +import net.openhft.chronicle.bytes.Bytes; +import net.openhft.chronicle.hash.replication.ReplicableEntry; +import net.openhft.chronicle.map.impl.CompiledReplicatedMapIterationContext; +import net.openhft.chronicle.map.impl.CompiledReplicatedMapQueryContext; +import org.hamcrest.CoreMatchers; +import org.junit.Test; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.LockSupport; + +import static org.junit.Assert.assertThat; + +public class BasicReplicationTest { + + @Test + public void shouldReplicate() throws Exception { + final ChronicleMapBuilder builder = ChronicleMap.of(String.class, String.class) + .entries(1000).averageKeySize(7).averageValueSize(7); + + + try ( + ReplicatedChronicleMap mapOne = createReplicatedMap(builder, asByte(1)); + ReplicatedChronicleMap mapTwo = createReplicatedMap(builder, asByte(2)); + ReplicatedChronicleMap mapThree = createReplicatedMap(builder, asByte(3)); + ) { + + final ReplicationEventProcessor processorOne = + new ReplicationEventProcessor<>(); + processorOne.addDestinationMap(mapOne.acquireModificationIterator(asByte(2)), mapOne, mapTwo); + processorOne.addDestinationMap(mapOne.acquireModificationIterator(asByte(3)), mapOne, mapThree); + + final ReplicationEventProcessor processorTwo = + new ReplicationEventProcessor<>(); + processorTwo.addDestinationMap(mapTwo.acquireModificationIterator(asByte(1)), mapTwo, mapOne); + processorTwo.addDestinationMap(mapTwo.acquireModificationIterator(asByte(3)), mapTwo, mapThree); + + final ReplicationEventProcessor processorThree = + new ReplicationEventProcessor<>(); + processorThree.addDestinationMap(mapThree.acquireModificationIterator(asByte(1)), mapThree, mapOne); + processorThree.addDestinationMap(mapThree.acquireModificationIterator(asByte(2)), mapThree, mapTwo); + + final ExecutorService executorService = Executors.newFixedThreadPool(3); + executorService.submit(processorOne::processPendingChangesLoop); + executorService.submit(processorTwo::processPendingChangesLoop); + executorService.submit(processorTwo::processPendingChangesLoop); + + final Map[] maps = new Map[] {mapOne, mapTwo, mapThree}; + + final Random random = new Random(0xBAD5EED); + for (int i = 0; i < 50; i++) { + final int mapIndex = random.nextInt(maps.length); + final Map map = maps[mapIndex]; + final String key = "key" + random.nextInt(100); + final String value = "val" + random.nextInt(500); + map.put(key, value); + System.out.printf("map %d, put(%s, %s)%n", mapIndex, key, value); + } + + LockSupport.parkNanos(TimeUnit.DAYS.toNanos(5L)); + executorService.shutdownNow(); + + for(String key : mapOne.keySet()) { + final String mapOneValue = mapOne.get(key); + final String mapTwoValue = mapTwo.get(key); + final String mapThreeValue = mapThree.get(key); + + assertThat(mapOneValue, CoreMatchers.equalTo(mapTwoValue)); + assertThat(mapOneValue, CoreMatchers.equalTo(mapThreeValue)); + } + } + } + + private static byte asByte(final int i) { + return (byte) i; + } + + private static final class ReplicationEventProcessor { + private final List> destinationMaps = new ArrayList<>(); + private final AtomicBoolean queueEmpty = new AtomicBoolean(true); + private ReplicatedChronicleMap sourceMap; + + void addDestinationMap(final ReplicatedChronicleMap.ModificationIterator modificationIterator, + final ReplicatedChronicleMap sourceMap, + final ReplicatedChronicleMap destinationMap) { + destinationMaps.add(new IteratorAndDestinationMap(modificationIterator, sourceMap, destinationMap)); + if (this.sourceMap != null && this.sourceMap != sourceMap) { + throw new IllegalArgumentException("All iterators must belong to the same source map"); + } + this.sourceMap = sourceMap; + modificationIterator.setModificationNotifier(this::wakeup); + } + + void wakeup() { + queueEmpty.set(false); + } + + void processPendingChangesLoop() { + + while(!Thread.currentThread().isInterrupted()) { + while (queueEmpty.get()) { + LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(7)); + } + + Collections.shuffle(destinationMaps); + + for (IteratorAndDestinationMap iteratorAndDestinationMap : destinationMaps) { + while (iteratorAndDestinationMap.modificationIterator.hasNext()) { + iteratorAndDestinationMap.modificationIterator.nextEntry(iteratorAndDestinationMap, 17); + } + } + + queueEmpty.compareAndSet(false, true); + } + } + } + + private static final class IteratorAndDestinationMap implements Replica.ModificationIterator.Callback { + private final ReplicatedChronicleMap.ModificationIterator modificationIterator; + private final ReplicatedChronicleMap sourceMap; + private final ReplicatedChronicleMap destinationMap; + private final Bytes buffer = Bytes.elasticByteBuffer(); + + IteratorAndDestinationMap(final ReplicatedChronicleMap.ModificationIterator modificationIterator, + final ReplicatedChronicleMap sourceMap, + final ReplicatedChronicleMap destinationMap) { + this.modificationIterator = modificationIterator; + this.sourceMap = sourceMap; + this.destinationMap = destinationMap; + } + + @Override + public void onEntry(final ReplicableEntry entry, final int chronicleId) { + + try (CompiledReplicatedMapQueryContext iCtx = sourceMap.mapContext()) { + iCtx.updateLock().lock(); + try { + sourceMap.writeExternalEntry(entry, null, buffer, chronicleId); + } finally { + iCtx.updateLock().unlock(); + } + } catch(Throwable e) { + e.printStackTrace(); + } + final long writeLimit = buffer.writeLimit(); + + buffer.readPosition(0); + buffer.readLimit(writeLimit); + + destinationMap.readExternalEntry(buffer, sourceMap.identifier()); + } + + @Override + public void onBootstrapTime(final long bootstrapTime, final int chronicleId) { + + } + } + + + private ReplicatedChronicleMap + createReplicatedMap(final ChronicleMapBuilder builder, final byte replicaId) { + final ChronicleMapBuilderPrivateAPI privateBuilder = + new ChronicleMapBuilderPrivateAPI<>(builder); + privateBuilder.replication(replicaId); + final ChronicleMap map = builder.create(); + return (ReplicatedChronicleMap) map; + } + + private static final class ReplicationCallback implements Replica.ModificationIterator.Callback { + + private final ReplicatedChronicleMap sourceMap; + private final ReplicatedChronicleMap destinationMap; + + ReplicationCallback(final ReplicatedChronicleMap sourceMap, + final ReplicatedChronicleMap destinationMap) { + this.sourceMap = sourceMap; + this.destinationMap = destinationMap; + } + + @Override + public void onEntry(final ReplicableEntry entry, final int chronicleId) { + final boolean changed = entry.isChanged(); + final byte originIdentifier = entry.originIdentifier(); + final long originTimestamp = entry.originTimestamp(); + + System.out.printf("onEntry(%s, %d, %d)%n", changed, originIdentifier, originTimestamp); + + final boolean identifierCheck = sourceMap.identifierCheck(entry, chronicleId); + final Bytes destination = Bytes.elasticByteBuffer(); + sourceMap.writeExternalEntry(entry, null, destination, chronicleId); + System.out.println(identifierCheck); + System.out.println("destination: " + destination); + final long writeLimit = destination.writeLimit(); + destination.readPosition(0); + destination.readLimit(writeLimit); + + destinationMap.readExternalEntry(destination, sourceMap.identifier()); + } + + @Override + public void onBootstrapTime(final long bootstrapTime, final int chronicleId) { + System.out.printf("onBootstrapTime(%d, %d)%n", bootstrapTime, chronicleId); + } + } +} diff --git a/src/test/java/net/openhft/lang/values/DoubleArrayTest.java b/src/test/java/net/openhft/lang/values/DoubleArrayTest.java index 5f05952f0..54edd7f26 100755 --- a/src/test/java/net/openhft/lang/values/DoubleArrayTest.java +++ b/src/test/java/net/openhft/lang/values/DoubleArrayTest.java @@ -66,6 +66,7 @@ public class DoubleArrayTest { da.bytesStore().release(); } + @Ignore @Test public void addToAMap() { DoubleArray a = new DoubleArray(10);