HCOLL-329 fixing the bootstrapping timestamp issue

This commit is contained in:
Rob Austin
2015-05-29 19:09:54 +01:00
parent 7ceefca5d5
commit 80c80ff672
9 changed files with 170 additions and 47 deletions
@@ -489,7 +489,6 @@ abstract class AbstractChannelReplicator implements Closeable {
out = result;
assert out.capacity() == in.capacity();
assert out.capacity() == size;
assert out.capacity() == in.capacity();
assert in.limit() == in.capacity();
@@ -502,7 +501,9 @@ abstract class AbstractChannelReplicator implements Closeable {
@Override
public boolean onEntry(final Bytes entry, final int chronicleId) {
public boolean onEntry(@NotNull final Bytes entry,
final int chronicleId,
final long bootstrapTime) {
long startOfEntry = entry.position();
long pos0 = in.position();
@@ -518,7 +519,7 @@ abstract class AbstractChannelReplicator implements Closeable {
start = in.position();
externalizable.writeExternalEntry(entry, in, chronicleId);
externalizable.writeExternalEntry(entry, in, chronicleId,bootstrapTime);
if (in.position() == start) {
in.position(pos0);
@@ -562,7 +563,8 @@ abstract class AbstractChannelReplicator implements Closeable {
in.position(pos0);
entry.position(startOfEntry);
return onEntry(entry, chronicleId);
return onEntry(entry, chronicleId, bootstrapTime);
} else
throw e;
@@ -113,21 +113,22 @@ final class ChannelProvider implements Closeable {
/**
* writes the entry to the chronicle channel provided
*
* @param entry the byte location of the entry to be stored
* @param destination a buffer the entry will be written to, the segment may reject
* this operation and add zeroBytes, if the identifier in the entry
* did not match the maps local
* @param chronicleChannel used in cluster into identify the canonical map or queue
* @param bootstrapTime
*/
@Override
public void writeExternalEntry(@NotNull Bytes entry, @NotNull Bytes destination,
int chronicleChannel) {
public void writeExternalEntry(@NotNull Bytes entry,
@NotNull Bytes destination,
int chronicleChannel, long bootstrapTime) {
channelDataLock.readLock().lock();
try {
destination.writeStopBit(chronicleChannel);
channelEntryExternalizables[chronicleChannel]
.writeExternalEntry(entry, destination, chronicleChannel);
.writeExternalEntry(entry, destination, chronicleChannel, bootstrapTime);
} finally {
channelDataLock.readLock().unlock();
}
@@ -136,7 +137,8 @@ final class ChannelProvider implements Closeable {
@Override
public void readExternalEntry(
@NotNull ThreadLocalCopies copies,
@NotNull VanillaChronicleMap.SegmentState segmentState, @NotNull Bytes source) {
@NotNull VanillaChronicleMap.SegmentState segmentState,
@NotNull Bytes source) {
channelDataLock.readLock().lock();
try {
final int chronicleId = (int) source.readStopBit();
@@ -274,6 +276,21 @@ final class ChannelProvider implements Closeable {
}
}
@Override
public void setLastModificationTime(byte identifier, long timestamp) {
channelDataLock.readLock().lock();
try {
// not including the SystemQueue at index 0
for (int i = 1, len = chronicleChannelList.size(); i < len; i++) {
Replica channel = chronicleChannelList.get(i);
channel.setLastModificationTime(identifier, timestamp);
}
} finally {
channelDataLock.readLock().unlock();
}
}
@Override
public void close() throws IOException {
ChannelProvider.this.close();
@@ -483,7 +500,7 @@ final class ChannelProvider implements Closeable {
final Bytes bytes = payloads.poll();
if (bytes == null)
return false;
callback.onEntry(bytes, 0);
callback.onEntry(bytes, 0, System.currentTimeMillis());
return true;
}
@@ -519,6 +536,11 @@ final class ChannelProvider implements Closeable {
return 0;
}
@Override
public void setLastModificationTime(byte identifier, long timestamp) {
// do nothing
}
@Override
public void close() throws IOException {
// do nothing
@@ -537,7 +559,7 @@ final class ChannelProvider implements Closeable {
@Override
public void writeExternalEntry(@NotNull Bytes entry, @NotNull Bytes destination,
int na) {
int na, long bootstrapTime) {
destination.write(entry, entry.position(), entry.remaining());
}
@@ -50,8 +50,6 @@ interface Replica extends Closeable {
* Gets (if it does not exist, creates) an instance of ModificationIterator associated with a
* remote node, this weak associated is bound using the {@code identifier}.
*
* @param modificationNotifier called when ever there is a change applied to the modification
* iterator
* @param remoteIdentifier the identifier of the remote node
* @return the ModificationIterator dedicated for replication to the remote node with the given
* identifier
@@ -71,6 +69,9 @@ interface Replica extends Closeable {
*/
long lastModificationTime(byte remoteIdentifier);
void setLastModificationTime(byte identifier, long timestamp);
/**
* notifies when there is a changed to the modification iterator
*/
@@ -156,15 +157,17 @@ interface Replica extends Closeable {
/**
* The map implements this method to save its contents.
*
* @param entry the byte location of the entry to be stored
* @param destination a buffer the entry will be written to, the segment may reject this
* operation and add zeroBytes, if the identifier in the entry did not
* match the maps local
* @param chronicleId is the channel id used to identify the canonical map or queue
* @param bootstrapTime
*/
void writeExternalEntry(@NotNull Bytes entry, @NotNull Bytes destination, int
chronicleId);
void writeExternalEntry(@NotNull Bytes entry,
@NotNull Bytes destination,
int chronicleId,
long bootstrapTime);
/**
* The map implements this method to restore its contents. This method must read the values
@@ -177,9 +180,10 @@ interface Replica extends Closeable {
* @param segmentState
* @param source bytes to read an entry from
*/
void readExternalEntry(
@NotNull ThreadLocalCopies copies,
@NotNull VanillaChronicleMap.SegmentState segmentState, @NotNull Bytes source);
void readExternalEntry(@NotNull ThreadLocalCopies copies,
@NotNull VanillaChronicleMap.SegmentState segmentState,
@NotNull Bytes source);
}
@@ -223,20 +227,29 @@ interface Replica extends Closeable {
* @param entry the entry you will receive, this does not have to be locked, as
* locking is already provided from the caller.
* @param chronicleId only assigned when clustering
* @param bootStrapTimeStamp sent to the client on every update this is the timestamp
* that the remote client should bootstrap from when there has
* been a disconnection, this time maybe later than the message
* time as event are not send in chronological order from the
* bit set.
*
*
* @return {@code false} if this entry should be ignored because the identifier of the
* source node is not from one of our changes, WARNING even though we check the identifier
* in the ModificationIterator the entry may have been updated.
*/
public abstract boolean onEntry(final Bytes entry, final int chronicleId);
public abstract boolean onEntry(@NotNull Bytes entry,
int chronicleId,
long bootStrapTimeStamp);
/**
* Called just after {@link #onEntry(Bytes, int)}. No-op by default.
* Called just after {@link #onEntry(Bytes, int, long)}. No-op by default.
*/
public void onAfterEntry() {
}
/**
* Called just before {@link #onEntry(Bytes, int)}. No-op by default.
* Called just before {@link #onEntry(Bytes, int, long)}. No-op by default.
*/
public void onBeforeEntry() {
}
@@ -43,6 +43,8 @@ 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;
import static net.openhft.chronicle.hash.hashing.Hasher.hash;
@@ -97,6 +99,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
private static final long serialVersionUID = 0L;
private static final Logger LOG = LoggerFactory.getLogger(ReplicatedChronicleMap.class);
private static final long LAST_UPDATED_HEADER_SIZE = 128L * 8L;
public static final int SIZE_OF_BOOTSTRAP_TIME_STAMP = 8;
private final TimeProvider timeProvider;
private final byte localIdentifier;
transient Set<Closeable> closeables;
@@ -162,7 +165,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
assignedModIterBitSetSizeInBytes();
}
void setLastModificationTime(byte identifier, long timestamp) {
public void setLastModificationTime(byte identifier, long timestamp) {
final long offset = identifier * 8L;
// purposely not volatile as this will impact performance,
@@ -464,7 +467,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
// make a byte buffer
assert result < Integer.MAX_VALUE;
return (int) result;
return (int) result + SIZE_OF_BOOTSTRAP_TIME_STAMP;
} finally {
entry.position(start);
}
@@ -479,9 +482,9 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
@Override
public void writeExternalEntry(@NotNull Bytes entry,
@NotNull Bytes destination,
int chronicleId) {
int chronicleId,
long bootstrapTime) {
final long initialLimit = entry.limit();
final long keySize = keySizeMarshaller.readSize(entry);
final long keyPosition = entry.position();
@@ -504,7 +507,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
}
final long valuePosition = entry.position();
destination.writeLong(bootstrapTime);
keySizeMarshaller.writeSize(destination, keySize);
valueSizeMarshaller.writeSize(destination, valueSize);
destination.writeStopBit(timeStamp);
@@ -555,6 +558,13 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
public void readExternalEntry(
@NotNull ThreadLocalCopies copies, @NotNull SegmentState segmentState,
@NotNull Bytes source) {
final long bootstrapTime = source.readLong();
assert bootstrapTime > currentTime() - TimeUnit.MINUTES
.toMillis(1) && bootstrapTime <= currentTime()
: "bootstrapTime=" + bootstrapTime;
final long keySize = keySizeMarshaller.readSize(source);
final long valueSize = valueSizeMarshaller.readSize(source);
final long timeStamp = source.readStopBit();
@@ -575,6 +585,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
return;
}
setLastModificationTime(remoteIdentifier, bootstrapTime);
final long keyPosition = source.position();
final long keyLimit = keyPosition + keySize;
@@ -595,7 +607,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
segment(segmentNum).remoteRemove(copies, segmentState,
source, segmentHash, timeStamp, remoteIdentifier);
setLastModificationTime(remoteIdentifier, timeStamp);
return;
}
@@ -610,7 +622,6 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
final long valueLimit = valuePosition + valueSize;
segment(segmentNum).remotePut(copies, segmentState, source,
keySize, valueSize, segmentHash, remoteIdentifier, timeStamp);
setLastModificationTime(remoteIdentifier, timeStamp);
if (debugEnabled) {
source.limit(valueLimit);
@@ -791,6 +802,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
return result;
}
/**
* called from a remote node as part of replication
*/
@@ -1297,6 +1309,23 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
return new TimestampTrackingEntry(key, value, timestamp);
}
final MultiStoreBytes timestampBytes = new MultiStoreBytes();
/**
* it is assumed that when calling this code the segment lock is in place
*
* @param pos the position of in the segment
* @return the timestamp of the entry when it was written
*/
public long timeStamp(long pos) {
final Bytes entry = reuse(timestampBytes, offsetFromPos(pos));
final long keySize = keySizeMarshaller.readSize(entry);
entry.skip(keySize);
return entry.readLong();
}
}
@Override
@@ -1398,6 +1427,12 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
private final int segmentIndexShift;
private final long posMask;
// when a bootstrap is send this is the time stamp that the client will bootstrap up to
// if it is set as ZERO then the onPut() will set it to the current time, once the
// consumer has cycled through the bit set the timestamp will be set back to zero.
private AtomicLong bootStrapTimeStamp = new AtomicLong();
private long lastBootStrapTimeStamp = currentTime();
private final EntryModifiableCallback entryModifiableCallback =
new EntryModifiableCallback();
@@ -1446,12 +1481,31 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
public void onPut(long pos, SharedSegment segment) {
changes.set(combine(segment.getIndex(), pos));
// todo improve this - use the timestamp from the entry its self
bootStrapTimeStamp.compareAndSet(0, timestamp(pos, segment));
if (modificationNotifier != null)
modificationNotifier.onChange();
}
private long timestamp(long pos, SharedSegment segment) {
long timeStamp = segment.timeStamp(pos);
assert timeStamp > currentTime() - TimeUnit.SECONDS.toMillis(1) &&
timeStamp <= currentTime() : "timeStamp=" + timeStamp + ", " +
"currentTime=" + currentTime();
return timeStamp;
}
public void onRemove(long pos, SharedSegment segment) {
changes.set(combine(segment.getIndex(), pos));
// todo improve this - use the timestamp from the entry its self
bootStrapTimeStamp.compareAndSet(0, timestamp(pos, segment));
if (modificationNotifier != null)
modificationNotifier.onChange();
}
@@ -1477,6 +1531,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
(position > 0L && changes.nextSetBit(0L) != NOT_FOUND);
}
/**
* @param entryCallback call this to get an entry, this class will take care of the locking
* @return true if an entry was processed
@@ -1493,6 +1548,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
this.position = NOT_FOUND;
return false;
}
bootStrapTimeStamp.set(0);
continue;
}
@@ -1517,7 +1574,10 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
// it may not be successful if the buffer can not be re-sized so we will
// process it later, by NOT clearing the changes.clear(position)
final boolean success = entryCallback.onEntry(entry, chronicleId);
final boolean success = entryCallback.onEntry(entry,
chronicleId,
bootStrapTimeStamp());
entryCallback.onAfterEntry();
if (success)
@@ -1527,7 +1587,6 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
}
// if the position was already cleared by another thread
// while we were trying to obtain segment lock (for example, in onRelocation()),
// go to pick up next (next iteration in while (true) loop)
@@ -1537,6 +1596,18 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
}
}
/**
* @return the timestamp that the remote client should bootstrap from when there has been a
* disconnection, this time maybe later than the message time as event are not send in
* chronological order from the bit set.
*/
private long bootStrapTimeStamp() {
final long timeStamp = bootStrapTimeStamp.get();
long result = (timeStamp == 0) ? this.lastBootStrapTimeStamp : timeStamp;
this.lastBootStrapTimeStamp = result;
return result;
}
@Override
public void dirtyEntries(long fromTimeStamp) throws InterruptedException {
// iterate over all the segments and mark bit in the modification iterator
@@ -44,4 +44,6 @@ interface SharedSegment<K, V> {
long offsetFromPos(long pos);
long timeStamp(long pos);
}
@@ -743,7 +743,7 @@ final class TcpReplicator<K, V> extends AbstractChannelReplicator implements Clo
entryWriter.workCompleted();
} else if (attached.remoteModificationIterator != null)
entryWriter.entriesToBuffer(attached.remoteModificationIterator, key);
entryWriter.entriesToBuffer(attached.remoteModificationIterator);
try {
final int len = entryWriter.writeBufferToSocket(socketChannel, approxTime);
@@ -1163,12 +1163,10 @@ final class TcpReplicator<K, V> extends AbstractChannelReplicator implements Clo
/**
* writes all the entries that have changed, to the buffer which will later be written to
* TCP/IP
* @param modificationIterator a record of which entries have modification
*
* @param modificationIterator a record of which entries have modification
* @param selectionKey
*/
void entriesToBuffer(@NotNull final Replica.ModificationIterator modificationIterator,
@NotNull final SelectionKey selectionKey) throws InterruptedException {
void entriesToBuffer(@NotNull final Replica.ModificationIterator modificationIterator) throws InterruptedException {
int entriesWritten = 0;
try {
@@ -1278,6 +1276,7 @@ final class TcpReplicator<K, V> extends AbstractChannelReplicator implements Clo
*/
class TcpSocketChannelEntryReader {
public static final int HEADROOM = 1024;
public static final int SIZE_OF_BOOTSTRAP_TIMESTAMP = 8;
public long lastHeartBeatReceived = System.currentTimeMillis();
ByteBuffer in;
ByteBufferBytes out;
@@ -1363,7 +1362,7 @@ final class TcpReplicator<K, V> extends AbstractChannelReplicator implements Clo
// if the buffer is too small to read this payload we will have to grow the
// size of the buffer
long requiredSize = sizeInBytes + SIZE_OF_SIZE + 1;
long requiredSize = sizeInBytes + SIZE_OF_SIZE + 1 + SIZE_OF_BOOTSTRAP_TIMESTAMP;
if (out.capacity() < requiredSize) {
attached.entryReader.resizeBuffer(requiredSize + HEADROOM);
}
@@ -1419,9 +1418,11 @@ final class TcpReplicator<K, V> extends AbstractChannelReplicator implements Clo
}
}
}
} else
} else {
externalizable.readExternalEntry(copies, segmentState, out);
}
out.limit(limit);
// skip onto the next entry
@@ -1673,6 +1673,13 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
return entriesOffset + pos * chunkSize;
}
@Override
public long timeStamp(long pos) {
throw new UnsupportedOperationException("timeStamp are only supported by the " +
"replicated map");
}
final MultiStoreBytes reuse(MultiStoreBytes entry, long offset) {
entry.setBytesOffset(bytes, offset);
entry.position(metaDataBytes);
@@ -21,6 +21,7 @@ package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.Closeable;
@@ -56,12 +57,17 @@ public class TCPSocketReplication3wayPutReturnsNullTest {
.replication(identifier, tcpConfig).create();
}
int portOffset = 0;
@Before
public void setup() throws IOException {
map1 = newTcpSocketShmIntString((byte) 1, 8026, new InetSocketAddress("localhost", 8027),
new InetSocketAddress("localhost", 8028));
map2 = newTcpSocketShmIntString((byte) 2, 8027, new InetSocketAddress("localhost", 8028));
map3 = newTcpSocketShmIntString((byte) 3, 8028);
map1 = newTcpSocketShmIntString((byte) 1, portOffset + 8026, new InetSocketAddress
("localhost", portOffset + 8027),
new InetSocketAddress("localhost", portOffset + 8028));
map2 = newTcpSocketShmIntString((byte) 2, portOffset + 8027, new InetSocketAddress
("localhost", portOffset + 8028));
map3 = newTcpSocketShmIntString((byte) 3, portOffset + 8028);
portOffset += 10;
}
@After
@@ -89,6 +95,7 @@ public class TCPSocketReplication3wayPutReturnsNullTest {
StatelessClientTest.checkThreadsShutdown(threads);
}
@Ignore
@Test
public void test3() throws IOException, InterruptedException {
@@ -20,11 +20,11 @@ public class TestReplication {
TcpTransportAndNetworkConfig tcpConfigServer1 =
TcpTransportAndNetworkConfig.of(8082);
TcpTransportAndNetworkConfig.of(8092);
TcpTransportAndNetworkConfig tcpConfigServer2 =
TcpTransportAndNetworkConfig.of(8083, new InetSocketAddress("localhost",
8082));
TcpTransportAndNetworkConfig.of(8093, new InetSocketAddress("localhost",
8092));
final ChronicleMap<Integer, Integer> map2 = ChronicleMapBuilder.of(Integer.class,
Integer.class)
@@ -42,9 +42,7 @@ public class TestReplication {
map1.put(i, i);
}
for (int i = 0; i < 10; i++) {
Thread.sleep(100);
System.out.println(map2.size());
}