Move classes in algos which need map into maps, closes https://github.com/OpenHFT/Chronicle-Algorithms/issues/10

This commit is contained in:
Peter Lawrey
2020-09-18 17:03:09 +01:00
parent a78378343c
commit e4d7a50eee
17 changed files with 2480 additions and 0 deletions
@@ -0,0 +1,93 @@
/*
* Copyright 2012-2018 Chronicle Map Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.values.Array;
import net.openhft.chronicle.values.Group;
import net.openhft.chronicle.values.MaxUtf8Length;
public interface BondVOInterface {
@Group(0)
long getEntryLockState();
void setEntryLockState(long entryLockState);
@Group(1)
long getIssueDate();
void setIssueDate(long issueDate); /* time in millis */
@Group(1)
long getMaturityDate();
void setMaturityDate(long maturityDate); /* time in millis */
long addAtomicMaturityDate(long toAdd);
@Group(1)
boolean compareAndSwapCoupon(double expected, double value);
double getCoupon();
void setCoupon(double coupon);
double addAtomicCoupon(double toAdd);
@Group(1)
String getSymbol();
void setSymbol(@MaxUtf8Length(20) String symbol);
// OpenHFT Off-Heap array[ ] processing notice ‘At’ suffix
@Group(1)
@Array(length = 7)
void setMarketPxIntraDayHistoryAt(int tradingDayHour, MarketPx mPx);
/* 7 Hours in the Trading Day:
* index_0 = 9.30am,
* index_1 = 10.30am,
…,
* index_6 = 4.30pm
*/
MarketPx getMarketPxIntraDayHistoryAt(int tradingDayHour);
/* nested interface - empowering an Off-Heap hierarchical “TIER of prices”
as array[ ] value */
interface MarketPx {
double getCallPx();
void setCallPx(double px);
double getParPx();
void setParPx(double px);
double getMaturityPx();
void setMaturityPx(double px);
double getBidPx();
void setBidPx(double px);
double getAskPx();
void setAskPx(double px);
}
}
@@ -0,0 +1,99 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.map.ChronicleMap;
import java.util.concurrent.locks.StampedLock;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadIntolerant {
public static void main(String[] args) {
try {
String isoLevel = args[0];
long sleepMock = Long.parseLong(args[1]);
long holdTime = Long.parseLong(args[2]);
/**
* ben.cotton@rutgers.edu START
*/
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
args[3]
+ "OPERAND_CHRONICLE_MAP"
);
Double coupon = 0.00;
BondVOInterface bond = newNativeReference(BondVOInterface.class);
//BondVOInterface cslMock = newNativeReference(BondVOInterface.class); //mock'd
long stamp = 0;
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant ENTERING offHeapLock.readLock()"
);
StampedLock offHeapLock =
new ChronicleStampedLock(
args[3]
+ "OPERAND_ChronicleStampedLock"
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant sleeping " + sleepMock + " seconds"
);
Thread.sleep(sleepMock * 1_000);
while ((stamp = offHeapLock.readLock()) < 0) {
}
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant ENTERED offHeapLock.readLock() " +
" stamp=[" +
stamp +
"]"
);
try {
chm.acquireUsing("369604101", bond);
//chm.acquireUsing("Offender ", cslMock); //mock'd
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant calling chm.get('369604101').getCoupon()"
);
bond = chm.get("369604101");
coupon = bond.getCoupon();
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant coupon=[" + coupon + "] read."
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant sleeping " + holdTime + " seconds"
);
Thread.sleep(holdTime * 1_000);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant awakening"
);
} finally {
offHeapLock.unlockRead(stamp);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant offHeapLock.unlockRead(" +
stamp + ") completed."
);
}
/**
* ben.cotton@rutgers.edu END
*/
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant got() coupon=" +
coupon + " "
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant COMMITTED"
);
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
}
@@ -0,0 +1,120 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.map.ChronicleMap;
import org.junit.Assert;
import org.junit.Test;
import static net.openhft.chronicle.values.Values.newNativeReference;
/**
* This Test efforts to ensure that a READERS-only set of requests to access the CSL
* is always granted (i.e. csl.tryReadLock() ALWAYS returns true)
*/
public class DirtyReadIntolerant_ReaderReader_Test {
@Test
public void main() {
try {
long sleepMock = Long.parseLong("5");
long holdTime = Long.parseLong("25");
Thread tooThread = new Thread(new ReaderToo());
tooThread.start();
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
OS.TARGET + "/shm-OPERAND_CHRONICLE_MAP"
);
Double coupon = 0.00;
BondVOInterface bond = newNativeReference(BondVOInterface.class);
//BondVOInterface cslMock = newNativeReference(BondVOInterface.class); //mock'd
long stamp = 0;
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant ENTERING offHeapLock.readLock()"
);
ChronicleStampedLock offHeapLock =
new ChronicleStampedLock(
OS.TARGET + "/shm-"
+ "OPERAND_ChronicleStampedLock"
);
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant sleeping " + sleepMock + " seconds"
);
Thread.sleep(sleepMock * 1_000);
while ((stamp = offHeapLock.tryReadLock()) < 0) {
Assert.assertEquals(Boolean.TRUE, false); // we failed!
}
Assert.assertEquals(Boolean.TRUE, true); // we passed!
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant ENTERED offHeapLock.readLock() " +
" stamp=[" +
stamp +
"]"
);
try {
chm.acquireUsing("369604101", bond);
//chm.acquireUsing("Offender ", cslMock); //mock'd
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant calling chm.get('369604101').getCoupon()"
);
bond = (BondVOInterface) chm.get("369604101");
coupon = bond.getCoupon();
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant coupon=[" + coupon + "] read."
);
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant sleeping " + holdTime + " seconds"
);
Thread.sleep(holdTime * 1_000);
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant awakening"
);
} finally {
offHeapLock.unlockRead(stamp);
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant offHeapLock.unlockRead(" +
stamp + ") completed."
);
}
/**
* ben.cotton@rutgers.edu END
*/
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant got() coupon=" +
coupon + " "
);
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant COMMITTED"
);
chm.close();
offHeapLock.closeChronicle();
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
}
@@ -0,0 +1,112 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.map.ChronicleMap;
import java.util.Scanner;
import java.util.concurrent.locks.StampedLock;
import static net.openhft.chronicle.values.Values.newNativeReference;
//import net.openhft.chronicle.map.fromdocs.BondVOInterface;
public class DirtyReadOffender {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
String isoLevel = args[0];
long sleepT = Long.parseLong(args[1]);
long holdTime = Long.parseLong(args[2]);
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
args[3]
+ "OPERAND_CHRONICLE_MAP"
);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender established chm "
);
StampedLock offHeapLock = new ChronicleStampedLock(
args[3]
+ "OPERAND_ChronicleStampedLock"
);
BondVOInterface bond = newNativeReference(BondVOInterface.class);
//BondVOInterface cslMock = newNativeReference(BondVOInterface.class);
chm.acquireUsing("369604101", bond);
//chm.acquireUsing("Offender ", cslMock); // mock ChronicleStampLock
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + sleepT + " seconds "
);
Thread.sleep(sleepT * 1_000);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender awakening "
);
/**
* ben.cotton@rutgers.edu ... anticipate Chronicle (www.OpenHFT.net)
* providing a j.u.c.l.StampedLock API for off-heap enthusiasts
*
* START
*
*/
long stamp = 0;
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRING offHeapLock.writeLock();"
);
while ((stamp = offHeapLock.writeLock()) == 0) {
}
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRED offHeapLock.writeLock();"
);
try {
double newCoupon = 3.5 + Math.random();
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender " +
" calling chm.put('369604101'," + newCoupon + ") "
);
bond.setCoupon(newCoupon);
chm.put("369604101", bond);
//cslMock.setEntryLockState(System.currentTimeMillis()); //mock'd
// chm.put("Offender ",cslMock); //mock'd
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender coupon=[" +
bond.getCoupon() +
"] written. "
);
} finally {
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + holdTime + " seconds "
);
Thread.sleep(holdTime * 1_000);
offHeapLock.unlockWrite(stamp);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender called " +
"offHeapLock.unlockWrite(" + stamp + ");"
);
}
/**
* ben.cotton@rutgers.edu
*
* END
*
*/
} catch (Exception throwables) {
throwables.printStackTrace();
} finally {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadOffender COMMITTED"
);
}
}
}
@@ -0,0 +1,118 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.map.ChronicleMap;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.locks.StampedLock;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadOffenderIPCTest implements Runnable {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void run() {
try {
long sleepT = Long.parseLong("5");
long holdTime = Long.parseLong("10");
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
OS.TARGET + "/shm-"
+ "OPERAND_CHRONICLE_MAP"
);
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender established chm "
);
StampedLock offHeapLock = new ChronicleStampedLock(
OS.TARGET + "/shm-"
+ "OPERAND_ChronicleStampedLock"
);
BondVOInterface bond = newNativeReference(BondVOInterface.class);
chm.acquireUsing("369604101", bond);
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + sleepT + " seconds "
);
Thread.sleep(sleepT * 1_000);
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender awakening "
);
/**
* ben.cotton@rutgers.edu ... anticipate Chronicle (www.OpenHFT.net)
* providing a j.u.c.l.StampedLock API for off-heap enthusiasts
*
* START
*
*/
long stamp = 0;
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRING offHeapLock.writeLock();"
);
while ((stamp = offHeapLock.writeLock()) == 0) {
;
}
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRED offHeapLock.writeLock();"
);
try {
double newCoupon = 3.5 + Math.random();
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender " +
" calling chm.put('369604101'," + newCoupon + ") "
);
bond.setCoupon(newCoupon);
chm.put("369604101", bond);
//cslMock.setEntryLockState(System.currentTimeMillis()); //mock'd
// chm.put("Offender ",cslMock); //mock'd
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender coupon=[" +
bond.getCoupon() +
"] written. "
);
} finally {
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + holdTime + " seconds "
);
Thread.sleep(holdTime * 1_000);
offHeapLock.unlockWrite(stamp);
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender called " +
"offHeapLock.unlockWrite(" + stamp + ");"
);
}
/**
* ben.cotton@rutgers.edu
*
* END
*
*/
} catch (Exception throwables) {
throwables.printStackTrace();
} finally {
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender COMMITTED"
);
}
}
}
@@ -0,0 +1,109 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.map.ChronicleMap;
import java.util.concurrent.locks.StampedLock;
import static net.openhft.chronicle.values.Values.newNativeReference;
class DirtyReadOffenderTest implements Runnable {
public void run() {
try {
long sleepT = Long.parseLong("5");
long holdTime = Long.parseLong("10");
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
OS.TARGET + "/shm-"
+ "OPERAND_CHRONICLE_MAP"
);
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender established chm "
);
StampedLock offHeapLock = new ChronicleStampedLock(
OS.TARGET + "/shm-"
+ "OPERAND_ChronicleStampedLock"
);
BondVOInterface bond = newNativeReference(BondVOInterface.class);
chm.acquireUsing("369604101", bond);
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + sleepT + " seconds "
);
Thread.sleep(sleepT * 1_000);
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender awakening "
);
/**
* ben.cotton@rutgers.edu ... anticipate Chronicle (www.OpenHFT.net)
* providing a j.u.c.l.StampedLock API for off-heap enthusiasts
*
* START
*
*/
long stamp = 0;
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRING offHeapLock.writeLock();"
);
while ((stamp = offHeapLock.writeLock()) == 0) {
;
}
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRED offHeapLock.writeLock();"
);
try {
double newCoupon = 3.5 + Math.random();
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender " +
" calling chm.put('369604101'," + newCoupon + ") "
);
bond.setCoupon(newCoupon);
chm.put("369604101", bond);
//cslMock.setEntryLockState(System.currentTimeMillis()); //mock'd
// chm.put("Offender ",cslMock); //mock'd
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender coupon=[" +
bond.getCoupon() +
"] written. "
);
} finally {
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + holdTime + " seconds "
);
Thread.sleep(holdTime * 1_000);
offHeapLock.unlockWrite(stamp);
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender called " +
"offHeapLock.unlockWrite(" + stamp + ");"
);
}
/**
* ben.cotton@rutgers.edu
*
* END
*
*/
chm.close();
} catch (Exception throwables) {
throwables.printStackTrace();
} finally {
System.out.println(
"..... @t=" + System.currentTimeMillis() +
" DirtyReadOffender COMMITTED"
);
}
}
}
@@ -0,0 +1,143 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.map.ChronicleMap;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadOffender_ReaderWriterTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void main() {
try {
long sleepT = Long.parseLong("8");
long holdTime = Long.parseLong("20");
Thread tooThread = new Thread(new ReaderToo());
tooThread.start();
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
OS.TARGET + "/shm-" +
"OPERAND_CHRONICLE_MAP"
);
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender established chm "
);
ChronicleStampedLock offHeapLock = new ChronicleStampedLock(
OS.TARGET + "/shm-"
+ "OPERAND_ChronicleStampedLock"
);
Assert.assertNotEquals(offHeapLock, null);
BondVOInterface bond = newNativeReference(BondVOInterface.class);
//BondVOInterface cslMock = newNativeReference(BondVOInterface.class);
chm.acquireUsing("369604101", bond);
//chm.acquireUsing("Offender ", cslMock); // mock ChronicleStampLock
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + sleepT + " seconds "
);
Thread.sleep(sleepT * 1_000);
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender awakening "
);
/**
* ben.cotton@rutgers.edu ... anticipate Chronicle (www.OpenHFT.net)
* providing a j.u.c.l.StampedLock API for off-heap enthusiasts
*
* START
*
*/
long stamp = 0;
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRING offHeapLock.tryWriteLock();"
);
int blockedByHoldingReaderCount = 0;
while ((stamp = offHeapLock.tryWriteLock()) == 0) { //Ben?
Thread.sleep(1_000);
++blockedByHoldingReaderCount;
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" WAITING in offHeapLock.tryWriteLock()... "
);
}
Assert.assertNotEquals(blockedByHoldingReaderCount, 0);
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRED offHeapLock.tryWriteLock();"
);
try {
double newCoupon = 3.5 + Math.random();
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender " +
" calling chm.put('369604101'," + newCoupon + ") "
);
bond.setCoupon(newCoupon);
chm.put("369604101", bond);
//cslMock.setEntryLockState(System.currentTimeMillis()); //mock'd
// chm.put("Offender ",cslMock); //mock'd
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender coupon=[" +
bond.getCoupon() +
"] written. "
);
} finally {
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + holdTime + " seconds "
);
Thread.sleep(holdTime * 1_000);
offHeapLock.unlockWrite(stamp);
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender called " +
"offHeapLock.unlockWrite(" + stamp + ");"
);
}
/**
* ben.cotton@rutgers.edu
*
* END
*
*/
chm.close();
offHeapLock.closeChronicle();
} catch (Exception throwables) {
throwables.printStackTrace();
} finally {
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadOffender COMMITTED"
);
}
}
}
@@ -0,0 +1,143 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.map.ChronicleMap;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadOffender_WriterReaderTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void main() {
try {
long sleepT = Long.parseLong("8");
long holdTime = Long.parseLong("20");
Thread tooThread = new Thread(new WriterToo());
tooThread.start();
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
OS.TARGET + "/shm-" +
"OPERAND_CHRONICLE_MAP"
);
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender established chm "
);
ChronicleStampedLock offHeapLock = new ChronicleStampedLock(
OS.TARGET + "/shm-"
+ "OPERAND_ChronicleStampedLock"
);
Assert.assertNotEquals(offHeapLock, null);
BondVOInterface bond = newNativeReference(BondVOInterface.class);
//BondVOInterface cslMock = newNativeReference(BondVOInterface.class);
chm.acquireUsing("369604101", bond);
//chm.acquireUsing("Offender ", cslMock); // mock ChronicleStampLock
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + sleepT + " seconds "
);
Thread.sleep(sleepT * 1_000);
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender awakening "
);
/**
* ben.cotton@rutgers.edu ... anticipate Chronicle (www.OpenHFT.net)
* providing a j.u.c.l.StampedLock API for off-heap enthusiasts
*
* START
*
*/
long stamp = 0;
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRING offHeapLock.tryReadLock();"
);
int blockedByHoldingWriterCount = 0;
while ((stamp = offHeapLock.tryReadLock()) == 0) {
Thread.sleep(1_000);
++blockedByHoldingWriterCount;
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" WAITING in offHeapLock.tryReadLock()... "
);
}
Assert.assertNotEquals(blockedByHoldingWriterCount, 0);
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRED offHeapLock.tryReadLock();"
);
try {
double newCoupon = 3.5 + Math.random();
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender " +
" calling chm.put('369604101'," + newCoupon + ") "
);
bond.setCoupon(newCoupon);
chm.put("369604101", bond);
//cslMock.setEntryLockState(System.currentTimeMillis()); //mock'd
// chm.put("Offender ",cslMock); //mock'd
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender coupon=[" +
bond.getCoupon() +
"] written. "
);
} finally {
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + holdTime + " seconds "
);
Thread.sleep(holdTime * 1_000);
offHeapLock.unlockRead(stamp);
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender called " +
"offHeapLock.unlockWrite(" + stamp + ");"
);
}
/**
* ben.cotton@rutgers.edu
*
* END
*
*/
chm.close();
offHeapLock.closeChronicle();
} catch (Exception throwables) {
throwables.printStackTrace();
} finally {
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadOffender COMMITTED"
);
}
}
}
@@ -0,0 +1,143 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.map.ChronicleMap;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadOffender_WriterWriterTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void main() {
try {
long sleepT = Long.parseLong("8");
long holdTime = Long.parseLong("20");
Thread tooThread = new Thread(new WriterToo());
tooThread.start();
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
OS.TARGET + "/shm-" +
"OPERAND_CHRONICLE_MAP"
);
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender established chm "
);
ChronicleStampedLock offHeapLock = new ChronicleStampedLock(
OS.TARGET + "/shm-"
+ "OPERAND_ChronicleStampedLock"
);
Assert.assertNotEquals(offHeapLock, null);
BondVOInterface bond = newNativeReference(BondVOInterface.class);
//BondVOInterface cslMock = newNativeReference(BondVOInterface.class);
chm.acquireUsing("369604101", bond);
//chm.acquireUsing("Offender ", cslMock); // mock ChronicleStampLock
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + sleepT + " seconds "
);
Thread.sleep(sleepT * 1_000);
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender awakening "
);
/**
* ben.cotton@rutgers.edu ... anticipate Chronicle (www.OpenHFT.net)
* providing a j.u.c.l.StampedLock API for off-heap enthusiasts
*
* START
*
*/
long stamp = 0;
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRING offHeapLock.writeLock();"
);
int blockedByHoldingWriterCount = 0;
while ((stamp = offHeapLock.tryWriteLock()) == 0) {
Thread.sleep(1_000);
++blockedByHoldingWriterCount;
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" WAITING in offHeapLock.tryWriteLock()... "
);
}
Assert.assertNotEquals(blockedByHoldingWriterCount, 0);
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRED offHeapLock.writeLock();"
);
try {
double newCoupon = 3.5 + Math.random();
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender " +
" calling chm.put('369604101'," + newCoupon + ") "
);
bond.setCoupon(newCoupon);
chm.put("369604101", bond);
//cslMock.setEntryLockState(System.currentTimeMillis()); //mock'd
// chm.put("Offender ",cslMock); //mock'd
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender coupon=[" +
bond.getCoupon() +
"] written. "
);
} finally {
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + holdTime + " seconds "
);
Thread.sleep(holdTime * 1_000);
offHeapLock.unlockWrite(stamp);
System.out.println(
" " +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender called " +
"offHeapLock.unlockWrite(" + stamp + ");"
);
}
/**
* ben.cotton@rutgers.edu
*
* END
*
*/
chm.close();
offHeapLock.closeChronicle();
} catch (Exception throwables) {
throwables.printStackTrace();
} finally {
System.out.println(
" " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadOffender COMMITTED"
);
}
}
}
@@ -0,0 +1,33 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import java.io.File;
import java.io.IOException;
public class DirtyReadTolerance<K, V> {
public static void main(String[] args) throws Exception {
System.out.println("DirtyReadOffender,chrAig[" + args[0] + "]coupon=3.50,DirtyReadVictim");
String isoLevel = args[0];
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
args[2]
+ "OPERAND_CHRONICLE_MAP"
);
System.out.println(",Established chm,");
}
static ChronicleMap<String, BondVOInterface> offHeap(String operand) throws IOException {
return ChronicleMapBuilder.of(String.class, BondVOInterface.class)
.entries(16)
.averageKeySize("123456789".length())
.createPersistedTo(
new File(operand)
);
//.create();
}
}
@@ -0,0 +1,122 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.map.ChronicleMap;
import java.util.concurrent.locks.StampedLock;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadVictim {
public static void main(String[] args) {
try {
String isoLevel = args[0];
long sleepMock = Long.parseLong(args[1]);
long holdTime = Long.parseLong(args[2]);
/**
* ben.cotton@rutgers.edu START
*/
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
args[3]
+ "OPERAND_CHRONICLE_MAP"
);
Double coupon = 0.00;
BondVOInterface bond = newNativeReference(BondVOInterface.class);
//BondVOInterface cslMock = newNativeReference(BondVOInterface.class); //mock'd
long stamp = 0;
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim ENTERING offHeapLock.tryOptimisticRead()"
);
StampedLock offHeapLock =
new ChronicleStampedLock(
args[3]
+ "OPERAND_ChronicleStampedLock"
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim sleeping " + sleepMock + " seconds"
);
Thread.sleep(sleepMock * 1_000);
while ((stamp = offHeapLock.tryOptimisticRead()) < 0) {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim waiting for unlockWrite()... " +
""
);
Thread.sleep(1000);
}
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim ENTERED offHeapLock.tryOptimisticRead() " +
" stamp=[" +
stamp +
"]"
);
try {
chm.acquireUsing("369604101", bond);
//chm.acquireUsing("Offender ", cslMock); //mock'd
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim calling chm.get('369604101').getCoupon()"
);
bond = chm.get("369604101");
coupon = bond.getCoupon();
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim coupon=[" + coupon + "] read."
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim sleeping " + holdTime + " seconds"
);
Thread.sleep(holdTime * 1_000);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim awakening"
);
} finally {
if (offHeapLock.validate(stamp)) {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim OPTIMISTICALLY_READ coupon=" +
coupon + " " +
"stamp = [" + stamp + "]"
);
} else {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim FAILED offHeapLock.validate(stamp) " +
" must apply PESSIMISTIC_POLICY (dirty read endured)" +
" coupon=[" + coupon + "] is *DIRTY*. " +
" stamp = [" + stamp + "]"
);
}
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim EXITED offHeapLock.tryOptimisticRead()"
);
}
/**
* ben.cotton@rutgers.edu END
*/
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim got() coupon=" +
coupon + " "
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim COMMITTED"
);
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
}
@@ -0,0 +1,249 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.map.ChronicleMap;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadVictimIPCTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
/**
* ben.cotton@rutgers.edu -- should we even try to Test IPC compliance via this hack?
* <p>
* Conjecture: Ben believes that: iff the operand set's members
* are all Chronicle Off-Heap resouces
* (e.g. Chronicle LongValue.class), then we may test IPC safety via an on-Heap j.l.Runnable
* operator bridge.
* <p>
* this belief is STRICTLY conjecture.
*
* @throws IOException
*/
@Test
public void mainOptimisticNegative() throws IOException {
try {
System.out.println("\n***** Optimistic (-) Test\n");
// ProcessBuilder pb = new ProcessBuilder(
// "/usr/bin/mkdir -p "+
// " C:\\Users\\buddy\\dev\\shm\\ "
// );
// Process p = pb.start();
// Scanner scan = new Scanner(p.getInputStream());
// while (scan.hasNext()) {
// System.out.println(
// " ,,@t=" +
// System.currentTimeMillis() +
// " DirtyReadVictimTest CALLING [" +
// scan.next() +
// "]"
// );
// }
// Thread.sleep(1_000);
// p.destroyForcibly();
// System.out.println(
// " ,,@t=" +
// System.currentTimeMillis() +
// " DirtyReadVictimTest called [\n" +
// "mkdir -p " +
// " C:\\Users\\buddy\\dev\\shm\\ " +
// "\n" +
// "]"
// );
/**
* ben.cotton@rutgers.edu START
*/
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
OS.TARGET + "/shm-OPERAND_CHRONICLE_MAP"
);
Double coupon = 0.00;
BondVOInterface bond = newNativeReference(BondVOInterface.class);
long stamp;
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim CALLING offHeapLock.tryOptimisticRead()"
);
ChronicleStampedLock offHeapLock = new ChronicleStampedLock(
OS.TARGET + "/shm-" +
"OPERAND_ChronicleStampedLock"
);
while ((stamp = offHeapLock.tryOptimisticRead()) == 0) {
;
}
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim CALLED offHeapLock.tryOptimisticRead()"
);
try {
chm.acquireUsing("369604101", bond);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim calling chm.get('369604101').getCoupon()"
);
bond = chm.get("369604101");
coupon = bond.getCoupon();
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim coupon=[" + coupon + "] read."
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim sleeping 20 seconds"
);
Thread offendingWriter = new Thread(
new DirtyReadOffenderIPCTest()
);
offendingWriter.start();
Thread.sleep(20_000);
} finally {
boolean r;
if ((r = offHeapLock.validate(stamp))) {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim OPTIMISTICALLY_READ coupon=" +
coupon + " "
);
// THIS Test will/must FAIL. i.e. OPTIMISM tested (-) in this case
Assert.assertEquals(
Boolean.FALSE,
r
);
} else {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim FAILED offHeapLock.validate(stamp) " +
" must apply PESSIMISTIC_POLICY (dirty read endured)" +
" coupon=[" + coupon + "] is *DIRTY*. "
);
Assert.assertNotEquals(
Boolean.TRUE,
r
);
}
//offHeapLock.unlockWrite(writerStamp);
}
/**
* ben.cotton@rutgers.edu END
*/
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim got() coupon=" +
coupon + " "
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim COMMITTED"
);
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
@Test
public void mainOptimisticPositive() {
System.out.println("\n***** Optimistic (+) Test\n");
try {
/**
* ben.cotton@rutgers.edu START
*/
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
OS.TARGET + "/shm-OPERAND_CHRONICLE_MAP"
);
Double coupon = 0.00;
BondVOInterface bond = newNativeReference(BondVOInterface.class);
long stamp = 0;
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim CALLING offHeapLock.tryOptimisticRead()"
);
ChronicleStampedLock offHeapLock = new ChronicleStampedLock(
OS.TARGET + "/shm-"
+ "OPERAND_ChronicleStampedLock"
);
while ((stamp = offHeapLock.tryOptimisticRead()) == 0) {
;
}
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim CALLED offHeapLock.tryOptimisticRead()"
);
try {
chm.acquireUsing("369604101", bond);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim calling chm.get('369604101').getCoupon()"
);
bond = (BondVOInterface) chm.get("369604101");
coupon = bond.getCoupon();
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim coupon=[" + coupon + "] read."
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim sleeping 2 seconds"
);
Thread.sleep(2_000);
} finally {
if (offHeapLock.validate(stamp)) {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim OPTIMISTICALLY_READ coupon=" +
coupon + " "
);
// THIS Test will pass when ChronicleStampedLock is GA
Assert.assertEquals(
Boolean.TRUE,
true
);
} else {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim FAILED offHeapLock.validate(stamp) " +
" must apply PESSIMISTIC_POLICY (dirty read endured)" +
" coupon=[" + coupon + "] is *DIRTY*. "
);
// THIS Test will execute pass when ChronicleStampedLock is GA
Assert.assertNotEquals(
Boolean.TRUE,
false
);
}
}
/*
* ben.cotton@rutgers.edu END
*/
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim got() coupon=" +
coupon + " "
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim COMMITTED"
);
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
}
@@ -0,0 +1,209 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.map.ChronicleMap;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadVictimTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void mainOptimisticNegative() throws IOException {
try {
System.out.println("\n***** Optimistic (-) Test\n");
/**
* ben.cotton@rutgers.edu START
*/
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
OS.TARGET + "/shm-OPERAND_CHRONICLE_MAP"
);
Double coupon = 0.00;
BondVOInterface bond = newNativeReference(BondVOInterface.class);
long stamp;
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim CALLING offHeapLock.tryOptimisticRead()"
);
ChronicleStampedLock offHeapLock = new ChronicleStampedLock(
OS.TARGET + "/shm-" +
"OPERAND_ChronicleStampedLock"
);
while ((stamp = offHeapLock.tryOptimisticRead()) == 0) {
}
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim CALLED offHeapLock.tryOptimisticRead()"
);
try {
chm.acquireUsing("369604101", bond);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim calling chm.get('369604101').getCoupon()"
);
bond = chm.get("369604101");
coupon = bond.getCoupon();
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim coupon=[" + coupon + "] read."
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim sleeping 20 seconds"
);
Thread offendingWriter = new Thread(
new DirtyReadOffenderTest()
);
offendingWriter.start();
Thread.sleep(20_000);
} finally {
boolean r;
if ((r = offHeapLock.validate(stamp))) {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim OPTIMISTICALLY_READ coupon=" +
coupon + " "
);
// THIS Test will/must FAIL. i.e. OPTIMISM tested (-) in this case
Assert.assertEquals(
Boolean.FALSE,
r
);
} else {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim FAILED offHeapLock.validate(stamp) " +
" must apply PESSIMISTIC_POLICY (dirty read endured)" +
" coupon=[" + coupon + "] is *DIRTY*. "
);
Assert.assertNotEquals(
Boolean.TRUE,
r
);
}
//offHeapLock.unlockWrite(writerStamp);
}
/**
* ben.cotton@rutgers.edu END
*/
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim got() coupon=" +
coupon + " "
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim COMMITTED"
);
chm.close();
offHeapLock.closeChronicle();
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
@Test
public void mainOptimisticPositive() {
System.out.println("\n***** Optimistic (+) Test\n");
try {
/**
* ben.cotton@rutgers.edu START
*/
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
OS.TARGET + "/shm-OPERAND_CHRONICLE_MAP"
);
Double coupon = 0.00;
BondVOInterface bond = newNativeReference(BondVOInterface.class);
long stamp = 0;
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim CALLING offHeapLock.tryOptimisticRead()"
);
ChronicleStampedLock offHeapLock = new ChronicleStampedLock(
OS.TARGET + "/shm-"
+ "OPERAND_ChronicleStampedLock"
);
while ((stamp = offHeapLock.tryOptimisticRead()) == 0) {
}
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim CALLED offHeapLock.tryOptimisticRead()"
);
try {
chm.acquireUsing("369604101", bond);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim calling chm.get('369604101').getCoupon()"
);
bond = chm.get("369604101");
coupon = bond.getCoupon();
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim coupon=[" + coupon + "] read."
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim sleeping 2 seconds"
);
Thread.sleep(2_000);
} finally {
if (offHeapLock.validate(stamp)) {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim OPTIMISTICALLY_READ coupon=" +
coupon + " "
);
// THIS Test will pass when ChronicleStampedLock is GA
Assert.assertEquals(
Boolean.TRUE,
true
);
} else {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim FAILED offHeapLock.validate(stamp) " +
" must apply PESSIMISTIC_POLICY (dirty read endured)" +
" coupon=[" + coupon + "] is *DIRTY*. "
);
// THIS Test will execute pass when ChronicleStampedLock is GA
Assert.assertNotEquals(
Boolean.TRUE,
false
);
}
}
/**
* ben.cotton@rutgers.edu END
*/
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim got() coupon=" +
coupon + " "
);
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim COMMITTED"
);
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
}
@@ -0,0 +1,114 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.map.ChronicleMap;
import static net.openhft.chronicle.values.Values.newNativeReference;
class ReaderToo implements Runnable {
@Override
public void run() {
try {
String isoLevel = "READER_TOO";
long sleepMock = Long.parseLong("0");
long holdTime = Long.parseLong("20");
/**
* ben.cotton@rutgers.edu START
*/
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
OS.TARGET + "/shm-OPERAND_CHRONICLE_MAP"
);
Double coupon = 0.00;
BondVOInterface bond = newNativeReference(BondVOInterface.class);
//BondVOInterface cslMock = newNativeReference(BondVOInterface.class); //mock'd
long stamp = 0;
System.out.println(
"READER_TOO " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant ENTERING offHeapLock.readLock()"
);
ChronicleStampedLock offHeapLock =
new ChronicleStampedLock(
OS.TARGET + "/shm-"
+ "OPERAND_ChronicleStampedLock"
);
System.out.println(
"READER_TOO " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant sleeping " + sleepMock + " seconds"
);
Thread.sleep(sleepMock * 1_000);
while ((stamp = offHeapLock.tryReadLock()) < 0) {
//Assert.assertEquals(Boolean.TRUE, false); // we failed!;
}
//Assert.assertEquals(Boolean.TRUE, true); // we passed
System.out.println(
"READER_TOO " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant ENTERED offHeapLock.readLock() " +
" stamp=[" +
stamp +
"]"
);
try {
chm.acquireUsing("369604101", bond);
//chm.acquireUsing("Offender ", cslMock); //mock'd
System.out.println(
"READER_TOO " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant calling chm.get('369604101').getCoupon()"
);
bond = chm.get("369604101");
coupon = bond.getCoupon();
System.out.println(
"READER_TOO " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant coupon=[" + coupon + "] read."
);
System.out.println(
"READER_TOO " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant sleeping " + holdTime + " seconds"
);
Thread.sleep(holdTime * 1_000);
System.out.println(
"READER_TOO " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant awakening"
);
} finally {
offHeapLock.unlockRead(stamp);
System.out.println(
"READER_TOO " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant offHeapLock.unlockRead(" +
stamp + ") completed."
);
}
/**
* ben.cotton@rutgers.edu END
*/
System.out.println(
"READER_TOO " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant got() coupon=" +
coupon + " "
);
System.out.println(
"READER_TOO " +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadIntolerant COMMITTED"
);
chm.close();
offHeapLock.closeChronicle();
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
}
@@ -0,0 +1,119 @@
package net.openhft.chronicle.map.locks;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.map.ChronicleMap;
import org.junit.Assert;
import static net.openhft.chronicle.values.Values.newNativeReference;
class WriterToo implements Runnable {
@Override
public void run() {
try {
long sleepT = 0;
long holdTime = 20;
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
OS.TARGET + "/shm-" +
"OPERAND_CHRONICLE_MAP"
);
System.out.println(
"WRITER TOO" +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender established chm "
);
ChronicleStampedLock offHeapLock = new ChronicleStampedLock(
OS.TARGET + "/shm-"
+ "OPERAND_ChronicleStampedLock"
);
Assert.assertNotEquals(offHeapLock, null);
BondVOInterface bond = newNativeReference(BondVOInterface.class);
//BondVOInterface cslMock = newNativeReference(BondVOInterface.class);
chm.acquireUsing("369604101", bond);
//chm.acquireUsing("Offender ", cslMock); // mock ChronicleStampLock
System.out.println(
"WRITER TOO" +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + sleepT + " seconds "
);
Thread.sleep(sleepT * 1_000);
System.out.println(
"WRITER TOO" +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender awakening "
);
/*
*anticipate Chronicle (www.OpenHFT.net)
* providing a j.u.c.l.StampedLock API for off-heap enthusiasts
*
* START
*
*/
long stamp = 0;
System.out.println(
"WRITER TOO" +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRING offHeapLock.writeLock();"
);
while ((stamp = offHeapLock.writeLock()) == 0) {
}
System.out.println(
"WRITER TOO" +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender ACQUIRED offHeapLock.writeLock();"
);
try {
double newCoupon = 3.5 + Math.random();
System.out.println(
"WRITER TOO" +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender " +
" calling chm.put('369604101'," + newCoupon + ") "
);
bond.setCoupon(newCoupon);
chm.put("369604101", bond);
//cslMock.setEntryLockState(System.currentTimeMillis()); //mock'd
// chm.put("Offender ",cslMock); //mock'd
System.out.println(
"WRITER TOO" +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender coupon=[" +
bond.getCoupon() +
"] written. "
);
} finally {
System.out.println(
"WRITER TOO" +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping " + holdTime + " seconds "
);
Thread.sleep(holdTime * 1_000);
offHeapLock.unlockWrite(stamp);
System.out.println(
"WRITER TOO" +
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender called " +
"offHeapLock.unlockWrite(" + stamp + ");"
);
}
/**
* ben.cotton@rutgers.edu
*
* END
*
*/
chm.close();
offHeapLock.closeChronicle();
} catch (Exception throwables) {
throwables.printStackTrace();
} finally {
System.out.println(
"WRITER TOO" +
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadOffender COMMITTED"
);
}
}
}