Make TimeProvider non-extensible and non-configurable; Fix a bug in

ReplicatedQueryAlloc.CleanupAction
This commit is contained in:
leventov
2015-12-27 21:32:26 +03:00
parent 123fb177c6
commit 1221f11097
16 changed files with 116 additions and 244 deletions
@@ -183,8 +183,6 @@ public final class ChronicleMapBuilder<K, V> implements
private boolean putReturnsNull = false;
private boolean removeReturnsNull = false;
// replication
private TimeProvider timeProvider = NanosecondPrecisionSystemTimeProvider.instance();
/**
* Default timeout is 1 minute. Even loopback tests converge often in the course of seconds,
* let alone WAN replication over many nodes might take tens of seconds.
@@ -1134,7 +1132,6 @@ public final class ChronicleMapBuilder<K, V> implements
", entries=" + entries() +
", putReturnsNull=" + putReturnsNull() +
", removeReturnsNull=" + removeReturnsNull() +
", timeProvider=" + timeProvider() +
", keyBuilder=" + keyBuilder +
", valueBuilder=" + valueBuilder +
'}';
@@ -1151,12 +1148,6 @@ public final class ChronicleMapBuilder<K, V> implements
return toString().hashCode();
}
@Override
public ChronicleMapBuilder<K, V> timeProvider(TimeProvider timeProvider) {
this.timeProvider = timeProvider;
return this;
}
@Override
public ChronicleMapBuilder<K, V> removedEntryCleanupTimeout(
long removedEntryCleanupTimeout, TimeUnit unit) {
@@ -1175,10 +1166,6 @@ public final class ChronicleMapBuilder<K, V> implements
return this;
}
TimeProvider timeProvider() {
return timeProvider;
}
@Override
public ChronicleMapBuilder<K, V> keyReaderAndDataAccess(
SizedReader<K> keyReader, @NotNull DataAccess<K> keyDataAccess) {
@@ -1,79 +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.TimeProvider;
import java.io.ObjectStreamException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
final class NanosecondPrecisionSystemTimeProvider extends TimeProvider {
private static final long serialVersionUID = 0L;
private static final AtomicLong lastTimeHolder = new AtomicLong();
private static final NanosecondPrecisionSystemTimeProvider INSTANCE =
new NanosecondPrecisionSystemTimeProvider();
public static NanosecondPrecisionSystemTimeProvider instance() {
return INSTANCE;
}
private NanosecondPrecisionSystemTimeProvider() {}
@Override
public long currentTime() {
long now = MILLISECONDS.toNanos(System.currentTimeMillis());
while (true) {
long lastTime = lastTimeHolder.get();
if (now <= lastTime)
return lastTime;
if (lastTimeHolder.compareAndSet(lastTime, now))
return now;
}
}
@Override
public long systemTimeIntervalBetween(
long earlierTimeNanos, long laterTimeNanos, TimeUnit systemTimeIntervalUnit) {
long intervalNanos = laterTimeNanos - earlierTimeNanos;
return systemTimeIntervalUnit.convert(intervalNanos, NANOSECONDS);
}
@Override
public boolean equals(Object o) {
return o != null && o.getClass() == getClass();
}
@Override
public int hashCode() {
return getClass().hashCode();
}
@Override
public String toString() {
return getClass().getSimpleName();
}
private Object readResolve() throws ObjectStreamException {
return INSTANCE;
}
}
@@ -19,7 +19,6 @@ package net.openhft.chronicle.map;
import net.openhft.chronicle.core.io.Closeable;
import net.openhft.chronicle.hash.ReplicatedHashSegmentContext;
import net.openhft.chronicle.hash.replication.ReplicableEntry;
import net.openhft.chronicle.hash.replication.TimeProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -29,6 +28,9 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import java.util.function.Predicate;
import static net.openhft.chronicle.hash.replication.TimeProvider.currentTime;
import static net.openhft.chronicle.hash.replication.TimeProvider.systemTimeIntervalBetween;
class OldDeletedEntriesCleanup implements Runnable, Closeable, Predicate<ReplicableEntry> {
private static final Logger LOG = LoggerFactory.getLogger(OldDeletedEntriesCleanup.class);
@@ -52,10 +54,9 @@ class OldDeletedEntriesCleanup implements Runnable, Closeable, Predicate<Replica
while (!shutdown) {
int segmentIndex = map.globalMutableState().getCurrentCleanupSegmentIndex();
int nextSegmentIndex;
TimeProvider timeProvider = map.timeProvider;
try (MapSegmentContext<?, ?, ?> context = map.segmentContext(segmentIndex)) {
if (segmentIndex == 0)
prevSegment0ScanStart = timeProvider.currentTime();
prevSegment0ScanStart = currentTime();
removedCompletely = 0;
if (((ReplicatedHashSegmentContext<?, ?>) context)
.forEachSegmentReplicableEntryWhile(this)) {
@@ -70,9 +71,9 @@ class OldDeletedEntriesCleanup implements Runnable, Closeable, Predicate<Replica
}
}
if (nextSegmentIndex == 0) {
long currentTime = timeProvider.currentTime();
long currentTime = currentTime();
TimeUnit cleanupTimeoutUnit = map.cleanupTimeoutUnit;
long mapScanTime = timeProvider.systemTimeIntervalBetween(
long mapScanTime = systemTimeIntervalBetween(
prevSegment0ScanStart, currentTime, cleanupTimeoutUnit);
LOG.debug("Old deleted entries scan time: {} {}", mapScanTime, cleanupTimeoutUnit);
long cleanupTimeout = map.cleanupTimeout;
@@ -93,8 +94,8 @@ class OldDeletedEntriesCleanup implements Runnable, Closeable, Predicate<Replica
if (shutdown)
return false;
if (e instanceof MapAbsentEntry) {
long deleteTimeout = map.timeProvider.systemTimeIntervalBetween(
e.originTimestamp(), map.timeProvider.currentTime(), map.cleanupTimeoutUnit);
long deleteTimeout = systemTimeIntervalBetween(
e.originTimestamp(), currentTime(), map.cleanupTimeoutUnit);
if (deleteTimeout > map.cleanupTimeout && !e.isChanged()) {
e.doRemoveCompletely();
removedCompletely++;
@@ -26,7 +26,6 @@ import net.openhft.chronicle.hash.impl.VanillaChronicleHash;
import net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface;
import net.openhft.chronicle.hash.replication.AbstractReplication;
import net.openhft.chronicle.hash.replication.ReplicableEntry;
import net.openhft.chronicle.hash.replication.TimeProvider;
import net.openhft.chronicle.map.impl.CompiledReplicatedMapIterationContext;
import net.openhft.chronicle.map.impl.CompiledReplicatedMapQueryContext;
import net.openhft.chronicle.map.replication.MapRemoteOperations;
@@ -48,6 +47,7 @@ import static net.openhft.chronicle.algo.MemoryUnit.*;
import static net.openhft.chronicle.algo.bitset.BitSetFrame.NOT_FOUND;
import static net.openhft.chronicle.algo.bytes.Access.checkedBytesStoreAccess;
import static net.openhft.chronicle.algo.bytes.Access.nativeAccess;
import static net.openhft.chronicle.hash.replication.TimeProvider.currentTime;
/**
* <h2>A Replicating Multi Master HashMap</h2> <p>Each remote hash map, mirrors its changes over to
@@ -97,7 +97,6 @@ public class ReplicatedChronicleMap<K, V, R> extends VanillaChronicleMap<K, V, R
private static final long LAST_UPDATED_HEADER_SIZE = 128L * 8L;
private static final int SIZE_OF_BOOTSTRAP_TIME_STAMP = 8;
public transient TimeProvider timeProvider;
/**
* Default value is 0, that corresponds to "unset" identifier value (valid ids are positive)
*/
@@ -168,7 +167,6 @@ public class ReplicatedChronicleMap<K, V, R> extends VanillaChronicleMap<K, V, R
void initTransientsFromBuilder(ChronicleMapBuilder<K, V> builder) {
super.initTransientsFromBuilder(builder);
this.remoteOperations = (MapRemoteOperations<K, V, R>) builder.remoteOperations;
this.timeProvider = builder.timeProvider();
cleanupRemovedEntries = builder.cleanupRemovedEntries;
cleanupTimeout = builder.cleanupTimeout;
cleanupTimeoutUnit = builder.cleanupTimeoutUnit;
@@ -490,7 +488,7 @@ public class ReplicatedChronicleMap<K, V, R> extends VanillaChronicleMap<K, V, R
// 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 = timeProvider.currentTime();
private long lastBootStrapTimeStamp = currentTime();
// records the current position of the cursor in the bitset
// TODO volatile?
@@ -551,9 +549,6 @@ public class ReplicatedChronicleMap<K, V, R> extends VanillaChronicleMap<K, V, R
} else {
raiseExtraTierChange(tierIndex, bitIndex);
}
// assert timestamp > timeProvider.currentTime() - TimeUnit.SECONDS.toMillis(1) &&
// timestamp <= timeProvider.currentTime() : "timeStamp=" + timestamp + ", " +
// "currentTime=" + timeProvider.currentTime();
// todo improve this - use the timestamp from the entry its self
bootStrapTimeStamp.compareAndSet(0, timestamp);
if (modificationNotifier != null)
@@ -25,6 +25,7 @@ import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
import static net.openhft.chronicle.hash.replication.TimeProvider.currentTime;
import static net.openhft.chronicle.map.ReplicatedChronicleMap.ADDITIONAL_ENTRY_BYTES;
@Staged
@@ -128,7 +129,7 @@ public abstract class ReplicatedMapEntryStages<K, V> extends MapEntryStages<K, V
public void updatedReplicationStateOnPresentEntry() {
if (!ru.replicationUpdateInit()) {
s.innerWriteLock.lock();
long timestamp = Math.max(timestamp() + 1, mh.m().timeProvider.currentTime());
long timestamp = Math.max(timestamp() + 1, currentTime());
updateReplicationState(mh.m().identifier(), timestamp);
}
}
@@ -136,7 +137,7 @@ public abstract class ReplicatedMapEntryStages<K, V> extends MapEntryStages<K, V
public void updatedReplicationStateOnAbsentEntry() {
if (!ru.replicationUpdateInit()) {
s.innerWriteLock.lock();
updateReplicationState(mh.m().identifier(), mh.m().timeProvider.currentTime());
updateReplicationState(mh.m().identifier(), currentTime());
}
}
@@ -30,6 +30,9 @@ import net.openhft.sg.Staged;
import java.util.function.Consumer;
import static net.openhft.chronicle.hash.replication.TimeProvider.currentTime;
import static net.openhft.chronicle.hash.replication.TimeProvider.systemTimeIntervalBetween;
@Staged
public class ReplicatedQueryAlloc extends QueryAlloc {
@@ -64,15 +67,17 @@ public class ReplicatedQueryAlloc extends QueryAlloc {
@Override
public void accept(ReplicableEntry e) {
ReplicatedChronicleMap<?, ?, ?> map = mh.m();
if (e instanceof MapAbsentEntry && iterationContext.pos() != posToSkip) {
long deleteTimeout = map.timeProvider.currentTime() - e.originTimestamp();
map.timeProvider.systemTimeIntervalBetween(e.originTimestamp(),
map.timeProvider.currentTime(), map.cleanupTimeoutUnit);
if (deleteTimeout > map.cleanupTimeout && !e.isChanged()) {
e.doRemoveCompletely();
removedCompletely++;
}
}
if (!(e instanceof MapAbsentEntry) || iterationContext.pos() == posToSkip)
return;
long currentTime = currentTime();
if (e.originTimestamp() > currentTime)
return; // presumably unsynchronized clocks
long deleteTimeout = systemTimeIntervalBetween(
e.originTimestamp(), currentTime, map.cleanupTimeoutUnit);
if (deleteTimeout <= map.cleanupTimeout || e.isChanged())
return;
e.doRemoveCompletely();
removedCompletely++;
}
}