corrected race-condition on our LongValue.addAtomic(+1);

This commit is contained in:
cotton-ben
2020-07-16 17:18:39 -04:00
parent dcf7446293
commit bd58ea4894
4 changed files with 48 additions and 76 deletions
@@ -1,11 +1,9 @@
package net.openhft.chronicle.map.fromdocs.acid.revelations;
import net.openhft.affinity.AffinitySupport;
import net.openhft.chronicle.core.values.LongValue;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.chronicle.values.Values;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.locks.StampedLock;
@@ -17,28 +15,26 @@ import static net.openhft.chronicle.values.Values.newNativeReference;
* <p>
* A totally hacked impl, awaiting OpenHFT's official ChronicleStampedLock API
* <p>
* A usable impl will more properly belong in the Chronicle-Algorithms repo
* A usable 'reference' impl will more properly belong in the Chronicle-Algorithms/ repo
*/
public class ChronicleStampedLock extends StampedLock {
ChronicleMap<String, ChronicleStampedLockVOInterface> chm;
ChronicleMap<String, LongValue> chmR;
ChronicleMap<String, ChronicleStampedLockVOInterface> chm; //custody of StampedLock semantics
ChronicleMap<String, LongValue> chmR; //Chronicle AtomicLong re: Reader set custody
ChronicleStampedLockVOInterface offHeapLock =
newNativeReference(ChronicleStampedLockVOInterface.class);
newNativeReference(ChronicleStampedLockVOInterface.class); //the off-heap ChronicleStampedLock
ChronicleStampedLockVOInterface lastWriterT =
newNativeReference(ChronicleStampedLockVOInterface.class);
LongValue readLockHolderCount = Values.newNativeReference(LongValue.class);
newNativeReference(ChronicleStampedLockVOInterface.class); //needed to facilitate validate(stamp)
LongValue readLockHolderCount = Values.newNativeReference(LongValue.class); //ReaderSet cardinality
ChronicleStampedLock(String chronicelStampedLockLocality) {
ChronicleStampedLock(String chronicelStampedLockLocality) { // path of Operand set i.e. /dev/shm/
try {
chm = offHeapLock(chronicelStampedLockLocality);
chmR = offHeapLockReaderCount(chronicelStampedLockLocality+"=ReaderCount");
chm.acquireUsing("Stamp ", offHeapLock);
chm.acquireUsing("LastWriterTime ", lastWriterT);
chm.acquireUsing("Stamp ", offHeapLock); // K="Stamp ", V=OffHeapLock impl
chm.acquireUsing("LastWriterTime ", lastWriterT); //needed for validate
chmR.acquireUsing("ReaderCount ", readLockHolderCount);
//readLockHolderCount.setValue(0L);
System.out.println(
" ,@t=" + System.currentTimeMillis() +
" ChronicleStampedLock constructed" +
@@ -51,34 +47,16 @@ public class ChronicleStampedLock extends StampedLock {
@Override
public long tryOptimisticRead() {
long l = 0;
offHeapLock = chm.get("Stamp ");
//do {
l = (offHeapLock = chm.get("Stamp ")).getEntryLockState();
// System.out.println(
// " ,,@t=" + System.currentTimeMillis() +
// " ChronicleStampedLock waiting for unlockWrite()... " +
// ""
// );
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// } while ( /*l < 0L*/); // do not loop! @vgrazi confirms that in JCA rebooted app.
offHeapLock.setEntryLockState(0L);
(offHeapLock = chm.get("Stamp ")).setEntryLockState(0L);
long t = System.currentTimeMillis();
chm.put("Stamp ", offHeapLock);
System.out.println(
" ,@t=" + System.currentTimeMillis() +
" ChronicleStampedLock saw stamp=["+l+"]"+
" ChronicleStampedLock saw stamp=["+0L+"]"+
" tryOptmisticRead() returning stamp=" +
t +
","
);
return (t);
}
@@ -87,7 +65,15 @@ public class ChronicleStampedLock extends StampedLock {
offHeapLock = chm.get("Stamp ");
lastWriterT = chm.get("LastWriterTime ");
boolean ret = false;
if (lastWriterT.getEntryLockState() > stamp || offHeapLock.getEntryLockState() <0L) {
/**
* If *any* Writer interacted with the offHeapLock, since event=tryOptimisticRead(),
* then FAIL the validate() invoke.
*
* Upon FAILURE, the thread on tryOptimisticRead() must apply its PESSIMISTIC policy (Thread
* has endured a DIRTY_READ.)
*
*/
if (lastWriterT.getEntryLockState() > stamp || offHeapLock.getEntryLockState() < 0L) {
ret = Boolean.FALSE;
} else {
ret = Boolean.TRUE;
@@ -110,7 +96,7 @@ public class ChronicleStampedLock extends StampedLock {
@Override
public long tryWriteLock() {
long l = 0;
long l = 0L;
offHeapLock = chm.get("Stamp ");
lastWriterT = chm.get("LastWriterTime ");
@@ -118,7 +104,7 @@ public class ChronicleStampedLock extends StampedLock {
do {
System.out.println(
" ,@t=" + System.currentTimeMillis() +
" ChronicleStampedLock tryWriterLock() WAITING " +
" ChronicleStampedLock tryWriterLock() ?WAITING " +
" on offHeapLock.unlock(" +
offHeapLock.getEntryLockState() +
") ,"
@@ -130,18 +116,13 @@ public class ChronicleStampedLock extends StampedLock {
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (l != 0L || readLockHolderCount.getValue() > 0);
} while (l != 0L || readLockHolderCount.getVolatileValue() > 0);
System.out.println(
" ,@t=" + System.currentTimeMillis() +
" ChronicleStampedLock tryWriterLock() RESUMING " +
" ChronicleStampedLock tryWriterLock() PROCEEDING " +
" ,"
);
long t = System.currentTimeMillis();
try {
Thread.sleep((long) (Math.random() * 1000)); //sleep up to 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
offHeapLock.setEntryLockState(-t); //negative ==> Writer holds StampedLock
lastWriterT.setEntryLockState(t);
chm.put("Stamp ", offHeapLock);
@@ -157,7 +138,7 @@ public class ChronicleStampedLock extends StampedLock {
@Override
public long tryReadLock() {
long l = 0;
long l = 0L;
offHeapLock = chm.get("Stamp ");
readLockHolderCount = chmR.get("ReaderCount ");
@@ -165,7 +146,7 @@ public class ChronicleStampedLock extends StampedLock {
do {
System.out.println(
" ,@t=" + System.currentTimeMillis() +
" ChronicleStampedLock tryReadLock() WAITING on Writer.unlock(" +
" ChronicleStampedLock tryReadLock() ?WAITING on Writer.unlock(" +
offHeapLock.getEntryLockState() +
") ,"
);
@@ -176,33 +157,25 @@ public class ChronicleStampedLock extends StampedLock {
e.printStackTrace();
}
} while (l < 0L);
try {
Thread.sleep((long) (Math.random() * 2000)); //sleep up to 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(
" ,@t=" + System.currentTimeMillis() +
" ChronicleStampedLock tryReadLock() RESUMING " +
" ChronicleStampedLock tryReadLock() PROCEEDING " +
//" readerCount=[" + readerCount.getReaderCount() + "]" +
" readerCount=[" + readLockHolderCount.getVolatileValue() + "]" +
" readerCount=[" +
(readLockHolderCount = chmR.get("ReaderCount ")).getVolatileValue() + "]" +
" BEFORE addAtomic(1) "+
","
);
readLockHolderCount.addAtomicValue(1);
readLockHolderCount.addAtomicValue(1); // INCREMENT the cardinality of the Reader set
chmR.put("ReaderCount ", readLockHolderCount); // and make it IPC visible
try {
Thread.sleep((long) (Math.random() * 1000)); //sleep up to 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
offHeapLock.setEntryLockState(
readLockHolderCount.getVolatileValue()
); //negative ==> Writer holds StampedLock
); // assign the Lock to most-recent Reader
chm.put("Stamp ", offHeapLock);
chm.put("Stamp ", offHeapLock); // make it IPC visible
System.out.println(
" ,@t=" + System.currentTimeMillis() +
@@ -212,7 +185,6 @@ public class ChronicleStampedLock extends StampedLock {
" AFTER addAtomic(1) "+
","
);
chmR.put("ReaderCount ", readLockHolderCount);
return (offHeapLock.getEntryLockState());
}
@@ -224,20 +196,24 @@ public class ChronicleStampedLock extends StampedLock {
System.out.println(
" ,@t=" + System.currentTimeMillis() +
" ChronicleStampedLock unlockRead(" + stamp + ") unlocking.." +
"ReaderCount=[" + readLockHolderCount.getVolatileValue() + "]" +
"ReaderCount=[" +
(readLockHolderCount = chmR.get("ReaderCount ")).getVolatileValue() + "]" +
" BEFORE addAtomic(-1) "+
","
);
readLockHolderCount.addAtomicValue(-1);
readLockHolderCount.addAtomicValue(-1); // DECREMENT the cardinality of the Reader set
chmR.put("ReaderCount ", readLockHolderCount); // make it IPC visible
System.out.println(
" ,@t=" + System.currentTimeMillis() +
" ChronicleStampedLock unlockRead(" + stamp + ") unlocking.." +
"ReaderCount=[" + readLockHolderCount.getVolatileValue() + "]" +
"ReaderCount=[" +
(readLockHolderCount = chmR.get("ReaderCount ")).getVolatileValue() + "]" +
" AFTER addAtomic(-1) "+
","
);
offHeapLock.setEntryLockState(readLockHolderCount.getVolatileValue());
offHeapLock.setEntryLockState(
(readLockHolderCount = chmR.get("ReaderCount ")).getVolatileValue()
);
System.out.println(
" ,@t=" + System.currentTimeMillis() +
"offHeapLock=[" + offHeapLock.getEntryLockState() + "]" +
@@ -247,7 +223,6 @@ public class ChronicleStampedLock extends StampedLock {
chmR.put("ReaderCount ", readLockHolderCount);
}
@Override
public void unlockWrite(long stamp) {
System.out.println(
@@ -260,7 +235,7 @@ public class ChronicleStampedLock extends StampedLock {
System.out.println(
" ,@t=" + System.currentTimeMillis() +
" ChronicleStampedLock unlockWrite(" +
chm.get("Stamp ").getEntryLockState() +
(offHeapLock = chm.get("Stamp ")).getEntryLockState() +
") unlocked. set to Zero" +
","
);
@@ -269,7 +244,6 @@ public class ChronicleStampedLock extends StampedLock {
static ChronicleMap<String, ChronicleStampedLockVOInterface> offHeapLock(String operand)
throws IOException {
AffinitySupport.setThreadId();
return ChronicleMapBuilder.of(String.class, ChronicleStampedLockVOInterface.class)
.entries(16)
.averageKeySize("123456789".length())
@@ -283,7 +257,6 @@ public class ChronicleStampedLock extends StampedLock {
static ChronicleMap<String, LongValue> offHeapLockReaderCount(String operand)
throws IOException {
AffinitySupport.setThreadId();
return ChronicleMapBuilder.of(String.class, LongValue.class)
.entries(16)
.averageKeySize("123456789".length())
@@ -1,6 +1,6 @@
package net.openhft.chronicle.map.fromdocs.acid.revelations;
import net.openhft.affinity.AffinitySupport;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.chronicle.map.fromdocs.BondVOInterface;
@@ -27,8 +27,7 @@ public class DirtyReadTolerance<K,V> {
}
static ChronicleMap<String, BondVOInterface> offHeap(String operand) throws IOException {
// ensure thread ids are globally unique.
AffinitySupport.setThreadId();
return ChronicleMapBuilder.of(String.class, BondVOInterface.class)
.entries(16)
.averageKeySize("123456789".length())