HCOLL-410 map event listener expose the identifer, timestamp and hasValueChanged

This commit is contained in:
Rob Austin
2015-12-11 17:20:22 +00:00
parent 6d869e2f86
commit 3647c0a555
18 changed files with 396 additions and 1634 deletions
@@ -63,6 +63,14 @@ public abstract class BasicCopyingMetaBytesInterop<E, W> implements MetaBytesInt
return h;
}
@Override
public boolean isEqual(W write, Bytes bytes, E e, long size) {
if (buffer.buffer.remaining() != size)
return false;
return bytes.compare(bytes.position(), buffer.buffer, buffer.buffer.position(), size);
}
@Override
public void write(W writer, Bytes bytes, E e) {
bytes.write(buffer.buffer, buffer.buffer.position(), buffer.buffer.remaining());
@@ -31,22 +31,22 @@ public enum ByteBufferMarshaller implements BytesInterop<ByteBuffer>, BytesReade
@Override
public boolean startsWith(Bytes bytes, ByteBuffer bb) {
int inputRemaining = bb.remaining();
if(bytes.capacity() - bytes.position() < (long) inputRemaining)
if (bytes.capacity() - bytes.position() < (long) inputRemaining)
return false;
long pos = bytes.position();
int inputPos = bb.position();
int i;
for(i = 0; i < inputRemaining - 7; i += 8) {
for (i = 0; i < inputRemaining - 7; i += 8) {
if (bytes.readLong(pos + (long) i) != bb.getLong(inputPos + i))
return false;
}
for(; i < inputRemaining - 3; i += 4) {
for (; i < inputRemaining - 3; i += 4) {
if (bytes.readInt(pos + (long) i) != bb.getInt(inputPos + i))
return false;
}
for(; i < inputRemaining; i++) {
for (; i < inputRemaining; i++) {
if (bytes.readByte(pos + (long) i) != bb.get(inputPos + i))
return false;
}
@@ -192,6 +192,13 @@ public abstract class ByteableMarshaller<E extends Byteable>
super(tClass);
initSize();
}
@Override
public boolean isEqual(Object write, Bytes bytes, E e, long size) {
// todo
return false;
}
}
private static class WithCustomFactory<E extends Byteable> extends ByteableMarshaller<E> {
@@ -201,7 +208,7 @@ public abstract class ByteableMarshaller<E extends Byteable>
private final ObjectFactory<E> factory;
WithCustomFactory(@NotNull Class<E> tClass,
@NotNull ObjectFactory<E> factory) {
@NotNull ObjectFactory<E> factory) {
super(tClass);
this.factory = factory;
initSize();
@@ -212,5 +219,12 @@ public abstract class ByteableMarshaller<E extends Byteable>
E getInstance() throws Exception {
return factory.create();
}
@Override
public boolean isEqual(Object write, Bytes bytes, E e, long size) {
// todo
return false;
}
}
}
@@ -52,6 +52,12 @@ public final class DelegatingMetaBytesInterop<E, I extends BytesInterop<E>>
interop.write(bytes, e);
}
@Override
public boolean isEqual(I interop, Bytes bytes, E e, long size) {
// todo
return false;
}
private Object readResolve() {
return INSTANCE;
}
@@ -136,5 +136,10 @@ class DirectBytesBuffer
private ForDataValueWriter() {
super(DirectBytesBuffer.this);
}
@Override
public boolean isEqual(BytesWriter<E> write, Bytes bytes, E e, long size) {
return false;
}
}
}
@@ -24,4 +24,16 @@ public interface MetaBytesWriter<E, W> extends Serializable {
long size(W writer, E e);
void write(W writer, Bytes bytes, E e);
/**
* returns true if the {@code bytes} equals {@code e}
*
* @param write
* @param bytes
* @param e
* @param size
* @return
*/
boolean isEqual(W write, Bytes bytes, E e, long size);
}
@@ -17,7 +17,6 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.ChronicleHashBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -64,10 +63,43 @@ public abstract class MapEventListener<K, V> implements Serializable {
return new LoggingMapEventListener(prefix);
}
/**
* This method is called whenever a new value is put for the key in the map during calls of such
* methods as {@link ChronicleMap#put put}, {@link ChronicleMap#putIfAbsent putIfAbsent}, {@link
* ChronicleMap#replace(Object, Object, Object) replace}, etc. When a new value is {@linkplain
* ChronicleMapBuilder#defaultValue(Object) default} for the map or obtained during {@link
* ChronicleMap#acquireUsing acquireUsing} call is put for the key, this method is called as
* well.
*
* <p>This method is called when put is already happened.
*
* @param key the key the given value is put for
* @param newValue the value which is now associated with the given key
* @param replacedValue the value which was replaced by {@code newValue}, {@code null} if
* the key was absent in the map before current {@code ChronicleMap}
* @param hasValueChanged {@code true} if the {@code newValue } equals {@code replacedValue
* }
* @param identifier the identifer used for replicaiton or zero if not definded
* @param replacedIdentifier the replaced identifer used for replicaiton or zero if not
* definded
* @param timeStamp the timestamp used for replicaiton or zero if not definded
* @param replacedTimeStamp the replaced timestamp used for replicaiton or zero if not
* definded
*/
public void onPut(K key,
V newValue,
@Nullable V replacedValue,
boolean replicationEvent,
boolean added,
boolean hasValueChanged,
byte identifier,
byte replacedIdentifier, long timeStamp, long replacedTimeStamp) {
}
private static class LoggingMapEventListener extends MapEventListener {
private static final long serialVersionUID = 0L;
public final static Logger LOGGER = LoggerFactory.getLogger(LoggingMapEventListener.class);
private final String prefix;
private LoggingMapEventListener(String prefix) {
@@ -81,8 +113,8 @@ public abstract class MapEventListener<K, V> implements Serializable {
@Override
public void onPut(Object key, Object value, Object replacedValue, boolean
replicationEvent,
@NotNull boolean updateResult) {
replicationEvent, boolean updateResult, boolean hasValueChanged, byte identifier,
byte replacedIdentifier, long timeStamp, long replacedTimeStamp) {
LOGGER.info("{} put {} => {}", prefix, key, value);
}
@@ -114,18 +146,24 @@ public abstract class MapEventListener<K, V> implements Serializable {
*
* <p>This method is called when put is already happened.
*
* @param key the key the given value is put for
* @param newValue the value which is now associated with the given key
* @param replacedValue the value which was replaced by {@code newValue}, {@code null} if the
* key was absent in the map before current {@code ChronicleMap}
* @param key the key the given value is put for
* @param newValue the value which is now associated with the given key
* @param replacedValue the value which was replaced by {@code newValue}, {@code null}
* if the key was absent in the map before current {@code
* ChronicleMap}
* @param replicationEvent
* @param lastModifiedTimeStamp
* @param lastId
* @param timestamp
* @param identifier
*/
public void onPut(K key,
V newValue,
@Nullable V replacedValue,
boolean replicationEvent,
boolean added) {
// do nothing
boolean added, long lastModifiedTimeStamp, byte lastId, long timestamp,
byte identifier, boolean hasValueChanged) {
}
/**
File diff suppressed because it is too large Load Diff
@@ -53,4 +53,10 @@ final class PrepareValueBytesAsWriter<K>
public void write(PrepareValueBytesAsWriter<K> writer, Bytes bytes, K k) {
write(bytes, k);
}
@Override
public boolean isEqual(PrepareValueBytesAsWriter<K> write, Bytes bytes, K k, long size) {
// todo
return false;
}
}
@@ -45,7 +45,6 @@ import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReferenceArray;
@@ -901,7 +900,10 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
// because locking might take time > 1 ms. but making current time a param
// of acquireWithoutLock would complicate code much, requiring putting
// lookupUsing() back into ReplicatedChMap, and other methods
entry.writeLong(currentTime());
final long timestamp = currentTime();
final long replacedTimestamp = entry.readLong(entry.position());
entry.writeLong(timestamp);
byte replacedIdentifier = entry.readByte(entry.position());
entry.writeByte(localIdentifier);
// deleted flag
entry.writeBoolean(false);
@@ -931,7 +933,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
} else {
throw defaultValueOrPrepareBytesShouldBeSpecified();
}
putValue(pos, offset, entry, valueSizePos, entryEndAddr, isDeleted,
boolean hasValueChanged = putValue(pos, offset, entry, valueSizePos,
entryEndAddr, isDeleted,
segmentState,
metaElemWriter, elemWriter, elem, metaElemWriter.size(elemWriter, elem),
hashLookup, sizeOfEverythingBeforeValue);
@@ -952,7 +955,9 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
}
if (eventListener != null) {
V valueInstance = toValue.toInstance(copies, v, valueSize);
eventListener.onPut(keyInstance, valueInstance, null, false, true);
eventListener.onPut(keyInstance, valueInstance, null, false, true,
hasValueChanged, localIdentifier, replacedIdentifier, timestamp,
replacedTimestamp);
}
entryCreated(lock);
return v;
@@ -1109,7 +1114,10 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
boolean isDeleted = entry.readBoolean();
entry.positionAddr(timeStampPosAddr);
final long replacedTimestamp = entry.readLong(entry.position());
entry.writeLong(timeStamp);
byte replacedIdentifier = entry.readByte(entry.position());
entry.writeByte(identifier);
// deleted flag
entry.writeBoolean(false);
@@ -1123,6 +1131,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
long prevValueSize = valueSizeMarshaller.readSize(entry);
long sizeOfEverythingBeforeValue = entry.position();
alignment.alignPositionAddr(entry);
boolean hasValueChanged = false;
try {
if (!isDeleted && prevValueSize == valueSize &&
metaValueInterop.startsWith(valueInterop, entry, value)) {
@@ -1132,7 +1141,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
long entryEndAddr = valueAddr + prevValueSize;
// putValue may relocate entry and change offset
putValue(pos, offset, entry, valueSizePos, entryEndAddr, isDeleted,
hasValueChanged = putValue(pos, offset, entry, valueSizePos,
entryEndAddr, isDeleted,
segmentState,
metaValueInterop, valueInterop, value, valueSize, hashLookup,
sizeOfEverythingBeforeValue);
@@ -1147,7 +1157,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
if (eventListener != null) {
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false,
isDeleted);
isDeleted, hasValueChanged, identifier, replacedIdentifier, timeStamp, replacedTimestamp);
}
// for DRY (reusing replaceValueAndNotifyPut() method),
@@ -1171,10 +1181,12 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
MVBI metaValueInterop =
getValueInterops.getMetaValueInterop(copies, valueInterop, value);
long valueSize = metaValueInterop.size(valueInterop, value);
putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
final long l = putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
metaValueInterop, valueInterop, value, entry, false);
entry.position(segmentState.valueSizePos - ADDITIONAL_ENTRY_BYTES);
final long replacedTimestamp = entry.readLong(entry.position());
entry.writeLong(timeStamp);
byte replacedIdentifier = entry.readByte(entry.position());
entry.writeByte(identifier);
entry.writeBoolean(false);
@@ -1185,7 +1197,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
segmentState.valueSizePos, true, false);
if (eventListener != null)
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false, true);
toValue.toInstance(copies, value, valueSize), null, false, true,
l < 0, identifier, replacedIdentifier, timeStamp, replacedTimestamp);
return UpdateResult.INSERT;
} finally {
@@ -1203,7 +1216,9 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
InstanceOrBytesToInstance<KB, K> toKey,
GetValueInterops<VB, VBI, MVBI> getValueInterops, VB value,
InstanceOrBytesToInstance<? super VB, V> toValue, long hash2,
boolean replaceIfPresent, ReadValue<RV> readValue, boolean resultUnused) {
boolean replaceIfPresent, ReadValue<RV> readValue, boolean resultUnused,
final byte identifier, final byte replacedIdentifier,
final long timestamp, final long replacedTimestamp) {
return putWithoutLock(copies, segmentState, metaKeyInterop, keyInterop, key, keySize,
toKey, getValueInterops, value, toValue, hash2, replaceIfPresent, readValue,
resultUnused, localIdentifier, currentTime(), false);
@@ -1249,7 +1264,9 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
if (replaceIfPresent || isDeleted) {
entry.positionAddr(timeStampPosAddr);
long replacedTimestamp = entry.readLong(entry.position());
entry.writeLong(timestamp);
final byte replacedIdentifier = entry.readByte(entry.position());
entry.writeByte(identifier);
// deleted flag
entry.writeBoolean(false);
@@ -1258,7 +1275,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
key, keySize, toKey,
getValueInterops, value, toValue,
entry, pos, offset, hashLookup, readValue,
resultUnused, isDeleted, remote);
resultUnused, isDeleted, remote, identifier,
replacedIdentifier, timestamp, replacedTimestamp);
// for DRY (reusing replaceValueAndNotifyPut() method),
// size is updated AFTER callbacks are called.
// however this shouldn't be an issue because exclusive segment lock
@@ -1287,10 +1305,13 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
MVBI metaValueInterop =
getValueInterops.getMetaValueInterop(copies, valueInterop, value);
long valueSize = metaValueInterop.size(valueInterop, value);
putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
final long l = putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
metaValueInterop, valueInterop, value, entry, false);
boolean hasValueChanged = (l < 0);
entry.position(segmentState.valueSizePos - ADDITIONAL_ENTRY_BYTES);
final long replacedTimestamp = entry.readLong(entry.position());
entry.writeLong(timestamp);
final byte replacedIdentifier = entry.readByte(entry.position());
entry.writeByte(identifier);
entry.writeBoolean(false);
@@ -1301,7 +1322,9 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
segmentState.valueSizePos, true, remote);
if (eventListener != null)
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, remote, true);
toValue.toInstance(copies, value, valueSize), null, remote, true,
hasValueChanged, identifier, replacedIdentifier, timestamp,
replacedTimestamp);
return resultUnused ? null : readValue.readNull();
}
@@ -1787,9 +1810,10 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
long timeStamp = segment.timeStamp(pos);
assert timeStamp > currentTime() - TimeUnit.SECONDS.toMillis(1) &&
timeStamp <= currentTime() : "timeStamp=" + timeStamp + ", " +
"currentTime=" + currentTime();
// this is not true for bootstraping of replicaiton events
// assert timeStamp > currentTime() - TimeUnit.SECONDS.toMillis(1) &&
// timeStamp <= currentTime() : "timeStamp=" + timeStamp + ", " +
// "currentTime=" + currentTime();
return timeStamp;
}
@@ -1,173 +0,0 @@
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.ChronicleHash;
import net.openhft.chronicle.hash.ChronicleHashBuilder;
import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder;
import net.openhft.chronicle.hash.replication.ReplicationChannel;
import net.openhft.chronicle.hash.replication.ReplicationHub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author Rob Austin.
*/
class ReplicationHubFindByName<K> implements FindByName {
public static final Logger LOG = LoggerFactory.getLogger(ReplicationHubFindByName.class.getName());
public static final int MAP_BY_NAME_CHANNEL = 1;
private final AtomicInteger nextFreeChannel = new AtomicInteger(2);
private final Map<String, MapInstanceBuilder> map;
private final ReplicationHub replicationHub;
/**
* @throws IOException
*/
public ReplicationHubFindByName(ReplicationHub replicationHub) throws IOException {
LOG.info("connecting to replicationHub=" + replicationHub);
this.replicationHub = replicationHub;
ReplicationChannel channel = replicationHub.createChannel((short) MAP_BY_NAME_CHANNEL);
final MapEventListener<CharSequence, MapInstanceBuilder> listener =
new MapEventListener<CharSequence, MapInstanceBuilder>() {
// creates a map based on the details that are sent to the map of builders
@Override
public void onPut(CharSequence key, MapInstanceBuilder value,
MapInstanceBuilder replacedValue, boolean replicationEvent,
boolean added0) {
super.onPut(key, value, replacedValue, replicationEvent, added0);
boolean added = replacedValue == null;
if (!added || value == null)
return;
// establish the new map based on this channel ID
LOG.info("create new map for name=" + value.name
+ ",channelId=" + value.channel.channelId());
try {
toReplicatedViaChannel(value.mapBuilder, value.channel.channelId()).create();
} catch (IllegalStateException e) {
// channel is already created
LOG.debug("while creating channel for name=" + value.name
+ ",channelId=" + value.channel.channelId(), e);
} catch (IOException e) {
LOG.error("", e);
}
}
};
this.map = (Map) ChronicleMapBuilder
.of(CharSequence.class, MapInstanceBuilder.class)
.averageKeySize(10)
.averageValueSize(4000)
.entries(128)
.eventListener(listener)
.instance()
.replicatedViaChannel(channel)
.create();
if (LOG.isDebugEnabled())
LOG.debug("map=" + map);
}
public <T extends ChronicleHash> T create(
MapInstanceBuilder<CharSequence, CharSequence> config)
throws TimeoutException, InterruptedException {
int withChannelId = nextFreeChannel.incrementAndGet();
map.put(config.name,
config.replicatedViaChannel(replicationHub.createChannel(withChannelId)));
return (T) get(config.name).mapBuilder.create();
}
public <T extends ChronicleHash> T from(String name) throws IllegalArgumentException,
IOException, TimeoutException, InterruptedException {
return (T) get(name).create();
}
MapInstanceBuilder get(String name) throws IllegalArgumentException,
TimeoutException,
InterruptedException {
MapInstanceBuilder config = waitTillEntryReceived(5000, name);
if (config == null)
throw new IllegalArgumentException("A map name=" + name + " can not be found.");
return config;
}
/**
* * waits until map1 and map2 show the same value
*
* @param timeOutMs timeout in milliseconds
* @throws InterruptedException
*/
private MapInstanceBuilder waitTillEntryReceived(final int timeOutMs, String name)
throws TimeoutException, InterruptedException {
int t = 0;
for (; t < timeOutMs; t++) {
MapInstanceBuilder config = map.get(name);
if (config != null)
return config;
Thread.sleep(1);
}
throw new TimeoutException("timed out wait for map name=" + name);
}
/**
* similar to {@link ChronicleMapBuilder#createPersistedTo(File file) }
*
* @param name the name of the map to be created
* @param file the file which will be used as shared off heap storage
* @param <T>
* @return a new instance of ChronicleMap
* @throws IllegalArgumentException
* @throws IOException
* @see ChronicleMapBuilder#createPersistedTo(File file)
*/
public <T extends ChronicleHash> T createPersistedTo(String name, File file)
throws IllegalArgumentException,
IOException, TimeoutException, InterruptedException {
return (T) get(name).persistedTo(file).create();
}
private ChronicleHashInstanceBuilder toReplicatedViaChannel(ChronicleMapBuilder builder) {
int channelId = nextFreeChannel.incrementAndGet();
if (channelId > replicationHub.maxNumberOfChannels())
throw new IllegalStateException("There are no more free channels, you can increase the number " +
"of changes in the replicationHub by calling replicationHub.maxNumberOfChannels(..);");
return builder.instance().replicatedViaChannel(replicationHub.createChannel((short) channelId));
}
private ChronicleHashInstanceBuilder toReplicatedViaChannel(ChronicleHashBuilder<K, ?, ?> builder, int
withChannelId) {
return builder.instance().replicatedViaChannel(replicationHub.createChannel((short) withChannelId));
}
}
@@ -1376,6 +1376,15 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
public void write(Void writer, Bytes bytes, Bytes value) {
bytes.write(value, value.position(), value.remaining());
}
@Override
public boolean isEqual(Void write, Bytes bytes, Bytes bytes2, long size) {
if (size != bytes2.remaining())
return false;
return bytes.compare(bytes.position(), bytes2, bytes2.position(), bytes2.remaining());
}
}
public static final class SegmentState implements StatefulCopyable<SegmentState>,
@@ -2091,6 +2100,12 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
throw defaultValueOrPrepareBytesShouldBeSpecified();
}
boolean hasValueChanged = valueSize < 0;
if (valueSize < 0)
valueSize = valueSize * -1;
entry.positionAddr(entry.positionAddr() - valueSize);
RV v = readValue.readValue(copies, entry, usingValue, valueSize);
@@ -2102,7 +2117,9 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
}
if (eventListener != null) {
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, v, valueSize), null, false, true);
toValue.toInstance(copies, v, valueSize), null, false, true, hasValueChanged,
(byte) 0,
(byte) 0, 0, 0);
}
return v;
@@ -2171,6 +2188,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
long prevValueSize = valueSizeMarshaller.readSize(entry);
long sizeOfEverythingBeforeValue = entry.position();
alignment.alignPositionAddr(entry);
boolean hasValueChanged = false;
try {
if (prevValueSize == valueSize &&
metaValueInterop.startsWith(valueInterop, entry, value)) {
@@ -2193,7 +2212,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
if (eventListener != null) {
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false,
false);
false, hasValueChanged, (byte) 0, (byte) 0, 0,
0);
}
}
}
@@ -2202,7 +2222,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
MVBI metaValueInterop =
getValueInterops.getMetaValueInterop(copies, valueInterop, value);
long valueSize = metaValueInterop.size(valueInterop, value);
putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
long l = putEntry(segmentState, metaKeyInterop, keyInterop, key,
keySize,
metaValueInterop, valueInterop, value, entry, false);
// put callbacks
@@ -2212,7 +2233,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
segmentState.valueSizePos, true, false);
if (eventListener != null)
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false, true);
toValue.toInstance(copies, value, valueSize), null, false, true, l <
0, (byte) 0, (byte) 0, 0, 0);
return UpdateResult.INSERT;
} finally {
@@ -2241,7 +2263,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
return putWithoutLock(copies, segmentState,
metaKeyInterop, keyInterop, key, keySize, toKey,
getValueInterops, value, toValue,
hash2, replaceIfPresent, readValue, resultUnused);
hash2, replaceIfPresent, readValue, resultUnused, (byte) 0, (byte) 0, 0,
0);
} finally {
segmentState.close();
writeUnlock();
@@ -2257,7 +2280,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
GetValueInterops<VB, VBI, MVBI> getValueInterops, VB value,
InstanceOrBytesToInstance<? super VB, V> toValue,
long hash2, boolean replaceIfPresent,
ReadValue<RV> readValue, boolean resultUnused) {
ReadValue<RV> readValue, boolean resultUnused, final byte identifier,
final byte replacedIdentifier, final long timestamp, final long replacedTimestamp) {
SearchState searchState = segmentState.searchState;
hashLookup.startSearch(hash2, searchState);
@@ -2274,7 +2298,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
key, keySize, toKey,
getValueInterops, value, toValue,
entry, pos, offset, hashLookup, readValue, resultUnused,
false, false);
false, false, identifier, replacedIdentifier, timestamp, replacedTimestamp);
} else {
long valueSize = readValueSize(entry);
return resultUnused ? null :
@@ -2286,7 +2310,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
MVBI metaValueInterop =
getValueInterops.getMetaValueInterop(copies, valueInterop, value);
long valueSize = metaValueInterop.size(valueInterop, value);
putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
long l = putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
metaValueInterop, valueInterop, value, entry, false);
// put callbacks
@@ -2296,7 +2320,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
segmentState.valueSizePos, true, false);
if (eventListener != null)
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false, true);
toValue.toInstance(copies, value, valueSize), null, false, true, l < 0,
identifier, (byte) 0, 0, 0);
return resultUnused ? null : readValue.readNull();
}
@@ -2309,7 +2334,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
InstanceOrBytesToInstance<? super VB, V> toValue,
MultiStoreBytes entry, long pos, long offset, MultiMap searchedHashLookup,
ReadValue<RV> readValue, boolean resultUnused,
boolean entryIsDeleted, boolean remote) {
boolean entryIsDeleted, boolean remote, final byte identifier, byte replacedIdentifier, final long timestamp, final long replacedTimestamp) {
VBI valueInterop = getValueInterops.getValueInterop(copies);
MVBI metaValueInterop = getValueInterops.getMetaValueInterop(
copies, valueInterop, value);
@@ -2335,7 +2360,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
}
// putValue may relocate entry and change offset
putValue(pos, offset, entry, valueSizePos, entryEndAddr, entryIsDeleted, segmentState,
boolean hasValueChanged = putValue(pos, offset, entry, valueSizePos, entryEndAddr,
entryIsDeleted, segmentState,
metaValueInterop, valueInterop, value, valueSize, searchedHashLookup,
sizeOfEverythingBeforeValue);
@@ -2346,7 +2372,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
if (eventListener != null) {
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), prevValueInstance, remote,
entryIsDeleted);
entryIsDeleted, hasValueChanged, identifier, replacedIdentifier,
timestamp, replacedTimestamp);
}
return resultUnused ? null : prevValue;
@@ -2364,6 +2391,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
* Returns value size, writes the entry (key, value, sizes) to the entry, after this method
* call entry positioned after value bytes written (i. e. at the end of entry), sets entry
* position (in segment) and value size position in the given segmentState
*
* @return the size or negivtive size if the value has changed.
*/
final <KB, KBI, MKBI extends MetaBytesInterop<KB, ? super KBI>, E, EW>
long putEntry(SegmentState segmentState,
@@ -2379,14 +2408,18 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
clearMetaData(offset);
reuse(entry, offset);
keySizeMarshaller.writeSize(entry, keySize);
metaKeyInterop.write(keyInterop, entry, key);
manageReplicationBytes(entry, writeDefaultInitialReplicationValues, false);
segmentState.valueSizePos = entry.position();
final long size = valueSizeMarshaller.readSize(entry);
entry.position(segmentState.valueSizePos);
valueSizeMarshaller.writeSize(entry, valueSize);
alignment.alignPositionAddr(entry);
boolean hasValueChanged = !metaElemWriter.isEqual(elemWriter, entry, elem, size);
metaElemWriter.write(elemWriter, entry, elem);
freeExtraAllocatedChunks(pos, allocatedChunks, entry);
@@ -2394,7 +2427,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
hashLookup.putAfterFailedSearch(segmentState.searchState, pos);
incrementSize();
return valueSize;
return hasValueChanged ? -valueSize : valueSize;
}
final void freeExtraAllocatedChunks(long pos, int allocatedChunks, Bytes entry) {
@@ -2714,7 +2747,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
copies, valueInterop, newValue);
long newValueSize = metaValueInterop.size(valueInterop, newValue);
boolean entryIsDeleted = false; // couldn't replace deleted entry
putValue(pos, offset, entry, valueSizePos, entryEndAddr, entryIsDeleted, segmentState,
boolean hasValueChanged = putValue(pos, offset, entry, valueSizePos, entryEndAddr, entryIsDeleted, segmentState,
metaValueInterop, valueInterop, newValue, newValueSize, searchedHashLookup,
sizeOfEverythingBeforeValue);
@@ -2727,7 +2760,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
if (eventListener != null)
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, newValue, newValueSize),
toValue.toInstance(copies, prevValue, valueSize), false, true);
toValue.toInstance(copies, prevValue, valueSize), false, true,
hasValueChanged, (byte) 0, (byte) 0, 0, 0);
return expectedValue == null ? prevValue : Boolean.TRUE;
}
@@ -2742,16 +2776,16 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
* @param entry relative pointer in Segment bytes
* @param valueSizePos relative position of value size in entry
* @param entryEndAddr absolute address of the entry end
* @return relative offset of the entry in Segment bytes after putting value (that may cause
* entry relocation)
* @return true if the value has change
*/
final <E, EW> long putValue(
final <E, EW> boolean putValue(
long pos, long offset, MultiStoreBytes entry, long valueSizePos, long entryEndAddr,
boolean entryIsDeleted,
SegmentState segmentState,
MetaBytesWriter<E, ? super EW> metaElemWriter, EW elemWriter, E newElem,
long newElemSize,
MultiMap searchedHashLookup, long sizeOfEverythingBeforeValue) {
final boolean hasValueChanged;
long entryStartAddr = entry.address();
long valueSizeAddr = entryStartAddr + valueSizePos;
long newValueAddr = alignment.alignAddr(
@@ -2789,14 +2823,20 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
NativeBytes.UNSAFE.copyMemory(entryStartAddr,
newEntryStartAddr, valueSizeAddr - entryStartAddr);
entry.position(valueSizePos);
final long originalSize = valueSizeMarshaller.readSize(entry);
entry.position(valueSizePos);
valueSizeMarshaller.writeSize(entry, newElemSize);
alignment.alignPositionAddr(entry);
metaElemWriter.write(elemWriter, entry, newElem);
hasValueChanged = !metaElemWriter.isEqual(elemWriter, entry, newElem,
originalSize);
if (hasValueChanged)
metaElemWriter.write(elemWriter, entry, newElem);
freeExtraAllocatedChunks(newPos, allocatedChunks, entry);
segmentState.pos = newPos;
return offset;
return hasValueChanged;
// END OF RELOCATION
} else if (newSizeInChunks < oldSizeInChunks) {
// Freeing extra chunks
@@ -2809,11 +2849,15 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
}
// Common code for all cases
entry.position(valueSizePos);
final long originalSize = valueSizeMarshaller.readSize(entry);
entry.position(valueSizePos);
valueSizeMarshaller.writeSize(entry, newElemSize);
alignment.alignPositionAddr(entry);
hasValueChanged = !metaElemWriter.isEqual(elemWriter, entry, newElem, originalSize);
metaElemWriter.write(elemWriter, entry, newElem);
segmentState.pos = pos;
return offset;
return hasValueChanged;
}
private void clear() {
@@ -55,7 +55,9 @@ public class EventListenerWithTCPSocketReplicationTest {
@Override
public void onPut(Integer key, CharSequence value, CharSequence replacedValue,
boolean replicationEvent, boolean added) {
boolean replicationEvent, boolean added, boolean hasValueChanged,
byte identifier, byte replacedIdentifier, long timeStamp,
long replacedTimeStamp) {
putWasCalled.getAndSet(true);
keyRef.set(key);
valueRef.set(value);
@@ -1,139 +0,0 @@
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.replication.ReplicationHub;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import static junit.framework.Assert.assertEquals;
/**
* @author Rob Austin.
*/
public class MapByNameTest {
private ReplicationHubFindByName<CharSequence> findMapByName;
@Before
public void setUp() throws IOException {
final ReplicationHub replicationHub = ReplicationHub.builder()
.tcpTransportAndNetwork(TcpTransportAndNetworkConfig.of(8243))
.createWithId((byte) 1);
findMapByName = new ReplicationHubFindByName(replicationHub);
}
@Test
public void testSerializingBuilder() throws TimeoutException, InterruptedException, IOException {
final MapInstanceBuilder<CharSequence, CharSequence> config =
(MapInstanceBuilder<CharSequence, CharSequence>)
ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
.averageKeySize("hello".length()).averageValueSize("world".length())
.entries(2)
.minSegments(2)
.replication((byte) 1, TcpTransportAndNetworkConfig.of(8244))
.removeReturnsNull(true)
.instance().name("map1");
final ChronicleMap<CharSequence, CharSequence> map = findMapByName.create(config);
map.put("hello", "world");
assertEquals(map.get("hello"), "world");
}
// @Test(expected = IllegalArgumentException.class)
// public void testSerializingBuilderUnknownMap() throws TimeoutException, InterruptedException {
// findMapByName.get("hello");
// }
@Test
@Ignore("currently work in progress")
public void testConnectByName() throws IOException, InterruptedException, TimeoutException {
NodeDiscovery nodeDiscovery = new NodeDiscovery();
MapInstanceBuilder<CharSequence, CharSequence> config =
(MapInstanceBuilder<CharSequence, CharSequence>)
ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
.minSegments(2)
.removeReturnsNull(true)
.instance()
.name("myMap");
ReplicationHubFindByName<CharSequence> mapByName = nodeDiscovery.mapByName();
// mapByName.add(config);
ChronicleMap<CharSequence, CharSequence> myMap2 = mapByName.from("myMap");
}
@Ignore("causing issues on windows")
@Test
public void testReplicationHubSerialization() throws IOException, InterruptedException,
TimeoutException {
NodeDiscovery nodeDiscovery = new NodeDiscovery();
final ReplicationHubFindByName<CharSequence> mapByName = nodeDiscovery.mapByName();
Thread.sleep(2000);
MapInstanceBuilder<CharSequence, CharSequence> config =
(MapInstanceBuilder<CharSequence, CharSequence>)
ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
.minSegments(2)
.entries(2)
.removeReturnsNull(true)
.instance()
.name("myMap6");
mapByName.create(config);
}
public static void main(String... args) throws IOException, InterruptedException, TimeoutException {
NodeDiscovery nodeDiscovery = new NodeDiscovery();
final ReplicationHubFindByName<CharSequence> mapByName = nodeDiscovery.mapByName();
Thread.sleep(2000);
MapInstanceBuilder<CharSequence, CharSequence> config =
(MapInstanceBuilder<CharSequence, CharSequence>)
ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
.minSegments(2)
.entries(2)
.removeReturnsNull(true)
.instance()
.name("myMap6");
ChronicleMap<String, String> map = mapByName.create(config);
map.put("hello", "world");
// final ChronicleMap<CharSequence, CharSequence> map = mapByName.from("myMap5");
// allow time for replication
Thread.sleep(2000);
System.out.print(map);
map.close();
}
}
@@ -31,7 +31,9 @@ public class MapEventListenerOnPutAddedFlagTest {
@Override
public void onPut(Object key, Object newValue, @Nullable Object replacedValue,
boolean replicationEvent, boolean added) {
boolean replicationEvent, boolean added, boolean hasValueChanged,
byte identifier, byte replacedIdentifier, long timeStamp,
long replacedTimeStamp) {
Assert.assertEquals(expected, added);
}
}
@@ -1,187 +0,0 @@
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.map;
import junit.framework.TestCase;
import net.openhft.lang.io.ByteBufferBytes;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.concurrent.atomic.AtomicReference;
import static java.nio.ByteBuffer.allocate;
public class NodeDiscoveryBroadcasterTest extends TestCase {
public static final int SERVER2_IDENTIFER = 10;
public static final int PROPOSED_IDENTIFIER = 5;
@Test
public void testsTheSerializationOfTheNodeDiscovery() throws IOException, InterruptedException {
AtomicReference ref = new AtomicReference();
final AddressAndPort ourAddressAndPort1 = new AddressAndPort(InetAddress.getLocalHost()
.getAddress(),
(short) 1024);
final AddressAndPort ourAddressAndPort2 = new AddressAndPort(InetAddress.getLocalHost()
.getAddress(),
(short) 1025);
// write broadcast our address on the bootstrap method
byte[] server1Address = Inet4Address.getLocalHost().getAddress();
AddressAndPort server1AddressAndPort = new AddressAndPort(server1Address, (short) 1234);
// write broadcast our address on the bootstrap method
byte[] server2Address = Inet4Address.getByName("129.168.0.2").getAddress();
AddressAndPort server2AddressAndPort = new AddressAndPort(server1Address, (short) 1234);
KnownNodes server1KnownNodes = new KnownNodes();
DiscoveryNodeBytesMarshallable server1BytesExternalizable = new DiscoveryNodeBytesMarshallable
(server1KnownNodes, ref, ourAddressAndPort1);
// for the unit test we are using a ByteBufferBytes, but iltimately this data would have been send
// and received via UDP
ByteBufferBytes udpData = new ByteBufferBytes(allocate(1024));
// SERER1 - is the new node join the cluster
// we will first send the bootstrap along with our host and port
{
final DiscoveryNodeBytesMarshallable.ProposedNodes proposedNodes = new
DiscoveryNodeBytesMarshallable.ProposedNodes(server1AddressAndPort, (byte) -1);
server1BytesExternalizable.sendBootStrap(proposedNodes);
server1BytesExternalizable.writeMarshallable(udpData);
}
KnownNodes server2KnownNodes = new KnownNodes();
server2KnownNodes.add(server2AddressAndPort, (byte) SERVER2_IDENTIFER);
DiscoveryNodeBytesMarshallable server2BytesExternalizable = new DiscoveryNodeBytesMarshallable
(server2KnownNodes, ref, ourAddressAndPort2);
// add our identifier and host:port to the list of known identifiers
// SERVER 2 - is the node already in the cluster
// read the bootstrap along with the host and port it came from
{
udpData.flip();
server2BytesExternalizable.readMarshallable(udpData);
Assert.assertTrue(server2BytesExternalizable.proposedIdentifiersWithHost.values().contains
(new DiscoveryNodeBytesMarshallable.ProposedNodes(server1AddressAndPort, (byte) -1)));
}
// SERVER 2
// write/broadcast the the response to receiving a bootstrap message
{
//ByteBufferBytes bitSetBytes = new ByteBufferBytes(allocate(128 / 8));
// final DirectBitSet knownIdentifiers = new ATSDirectBitSet(bitSetBytes);
// RemoteNodes remoteNodes = new RemoteNodes(bitSetBytes);
udpData.clear();
// broadcasts in response to the bootstrap all the host:ports and identifiers that it knows about
// in this case given that there is only this node in the grid and one node joining,
// it will only be the node that has the bootstrap and this node, who's data will be sent
server2BytesExternalizable.writeMarshallable(udpData);
}
// SERER1
// receive
// read the bootstrap along with the host and port it came from
{
udpData.flip();
server1BytesExternalizable.readMarshallable(udpData);
Assert.assertTrue(server1BytesExternalizable.getRemoteNodes().addressAndPorts().contains
(server2AddressAndPort));
Assert.assertTrue(server1BytesExternalizable.getRemoteNodes().identifiers().get
(SERVER2_IDENTIFER));
}
// SERVER 1
// send/broadcast - propose an identifier to use, by choosing one that is not used already
// for the purpose of this test we wont choose one at random ( as the code does ) but just pick
// identifier=5
{
udpData.clear();
KnownNodes knownNodes = new KnownNodes();
DiscoveryNodeBytesMarshallable bytesExternalizable = new DiscoveryNodeBytesMarshallable
(knownNodes, ref, ourAddressAndPort1);
DiscoveryNodeBytesMarshallable.ProposedNodes proposedNodes = new DiscoveryNodeBytesMarshallable.ProposedNodes(server1AddressAndPort, (byte) PROPOSED_IDENTIFIER);
// send the bootstrap along with this newly proposed identifier
bytesExternalizable.sendBootStrap(proposedNodes);
bytesExternalizable.writeMarshallable(udpData);
}
// SERVER 2 - is the node already in the cluster
// read the bootstrap along with the proposed identifier and host:port it came from
{
udpData.flip();
server2BytesExternalizable.readMarshallable(udpData);
Assert.assertTrue(server2BytesExternalizable.proposedIdentifiersWithHost.values().contains
(new DiscoveryNodeBytesMarshallable.ProposedNodes(server1AddressAndPort, (byte) PROPOSED_IDENTIFIER)));
}
// SERVER 2
// write/broadcast the the response to receiving a bootstrap message
{
//ByteBufferBytes bitSetBytes = new ByteBufferBytes(allocate(128 / 8));
// final DirectBitSet knownIdentifiers = new ATSDirectBitSet(bitSetBytes);
// RemoteNodes remoteNodes = new RemoteNodes(bitSetBytes);
udpData.clear();
// broadcasts in response to the bootstrap all the host:ports and identifiers that it knows about
// in this case given that there is only this node in the grid and one node joining,
// it will only be the node that has the bootstrap and this node, who's data will be sent
server2BytesExternalizable.writeMarshallable(udpData);
}
// SERVER 1
// read the bootstrap along with all the proposed identifiers ( this will include our
// PROPOSED_IDENTIFIER and any that come from other server
{
udpData.flip();
server1BytesExternalizable.readMarshallable(udpData);
Assert.assertTrue(server1BytesExternalizable.getRemoteNodes().addressAndPorts().contains
(server2AddressAndPort));
Assert.assertTrue(server1BytesExternalizable.getRemoteNodes().identifiers().get
(SERVER2_IDENTIFER));
Assert.assertTrue(server1BytesExternalizable.proposedIdentifiersWithHost.values().contains
(new DiscoveryNodeBytesMarshallable.ProposedNodes(server1AddressAndPort, (byte) PROPOSED_IDENTIFIER)));
}
}
}
@@ -0,0 +1,181 @@
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.lang.io.ByteBufferBytes;
import net.openhft.lang.model.Byteable;
import net.openhft.lang.model.DataValueClasses;
import net.openhft.lang.values.IntValue;
import org.jetbrains.annotations.Nullable;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
/**
* Test ReplicatedChronicleMap where the Replicated is over a TCP Socket
*
* @author Rob Austin.
*/
public class ReplicationTest {
static int s_port = 8093;
Set<Thread> threads;
private ChronicleMap<Integer, CharSequence> map1;
final AtomicLong count = new AtomicLong();
final AtomicBoolean hasValueChanged = new AtomicBoolean();
final AtomicInteger identifier = new AtomicInteger();
final AtomicInteger replacedIdentifier = new AtomicInteger();
final AtomicLong timeStamp = new AtomicLong();
final AtomicLong replacedTimeStamp = new AtomicLong();
private IntValue value;
@Before
public void setup() throws IOException {
value = DataValueClasses.newDirectReference(IntValue.class);
((Byteable) value).bytes(new ByteBufferBytes(ByteBuffer.allocateDirect(4)), 0);
final InetSocketAddress endpoint = new InetSocketAddress("localhost", s_port + 1);
{
final TcpTransportAndNetworkConfig tcpConfig1 = TcpTransportAndNetworkConfig.of(s_port,
endpoint).autoReconnectedUponDroppedConnection(true).name(" map1")
.heartBeatInterval(1, TimeUnit.SECONDS)
.tcpBufferSize(1024 * 64);
final MapEventListener<Integer, CharSequence> integerCharSequenceMapEventListener =
new MapEventListener<Integer, CharSequence>() {
public void onPut(Integer key,
CharSequence newValue,
@Nullable CharSequence replacedValue,
boolean replicationEvent,
boolean added,
boolean hasValueChanged,
byte identifier,
byte replacedIdentifier,
long timeStamp,
long replacedTimeStamp) {
System.out.println("key=" + key + ", value=" + value + ", replacedValue=" + replacedValue + ", " +
"replicationEvent=" + replicationEvent + ", added=" +
added + ", hasValueChanged=" + hasValueChanged + ", " +
"identifier=" + identifier + ", replacedIdentifier=" +
replacedIdentifier + ", timeStamp=" + timeStamp + ", " +
"replacedTimeStamp=" + replacedTimeStamp);
count.incrementAndGet();
ReplicationTest.this.identifier.set(identifier);
ReplicationTest.this.replacedIdentifier.set(replacedIdentifier);
ReplicationTest.this.timeStamp.set(timeStamp);
ReplicationTest.this.replacedTimeStamp.set(replacedTimeStamp);
ReplicationTest.this.hasValueChanged.set(hasValueChanged);
}
};
map1 = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(Builder.SIZE + Builder.SIZE)
.actualSegments(1)
.replication((byte) 1)
.eventListener(integerCharSequenceMapEventListener)
.instance()
.name("map1")
.create();
}
s_port += 2;
}
@After
public void tearDown() throws InterruptedException {
System.gc();
}
@Before
public void sampleThreads() {
threads = Thread.getAllStackTraces().keySet();
}
@After
public void checkThreadsShutdown() {
StatelessClientTest.checkThreadsShutdown(threads);
}
/**
* tests that remote puts (in otherwords, puts that provide and identifier only replicate on
* events with with same identifier as the local-identifier
*
* @throws IOException
* @throws InterruptedException
*/
@Test
public void test() throws IOException, InterruptedException {
ReplicatedChronicleMap map = (ReplicatedChronicleMap) map1;
final long ts = 123;
map.put(1, "test", (byte) 2, ts);
Assert.assertTrue(hasValueChanged.get());
Assert.assertEquals((byte) 2, identifier.get());
Assert.assertEquals((byte) 0, replacedIdentifier.get());
Assert.assertEquals(123, timeStamp.get());
Assert.assertEquals((byte) 0, replacedTimeStamp.get());
map.put(1, "test", (byte) 3, 124);
Assert.assertFalse(hasValueChanged.get());
Assert.assertEquals((byte) 3, identifier.get());
Assert.assertEquals((byte) 2, replacedIdentifier.get());
Assert.assertEquals(124, timeStamp.get());
Assert.assertEquals(123, replacedTimeStamp.get());
map.put(1, "test2", (byte) 2, 125);
Assert.assertTrue(hasValueChanged.get());
Assert.assertEquals((byte) 2, identifier.get());
Assert.assertEquals((byte) 3, replacedIdentifier.get());
Assert.assertEquals(125, timeStamp.get());
Assert.assertEquals(124, replacedTimeStamp.get());
map.put(1, "test2", (byte) 2, 125);
Assert.assertFalse(hasValueChanged.get());
Assert.assertEquals((byte) 2, identifier.get());
Assert.assertEquals((byte) 2, replacedIdentifier.get());
Assert.assertEquals(125, timeStamp.get());
Assert.assertEquals(125, replacedTimeStamp.get());
}
}
@@ -118,7 +118,7 @@ public class TCPReplicationCheckUpdatesFromOtherMapsAreNotPopulated {
* @throws InterruptedException
*/
@Test
public void test3() throws IOException, InterruptedException {
public void test() throws IOException, InterruptedException {
ReplicatedChronicleMap map = (ReplicatedChronicleMap) map1;
@@ -135,7 +135,6 @@ public class TCPReplicationCheckUpdatesFromOtherMapsAreNotPopulated {
Assert.assertTrue(map2.size() == 1);
Assert.assertEquals("test2", map2.get(1));
}