begin to adapt test for Chronicle's anticipated delivery of an off-heap j.u.c.l.StampedLock API

This commit is contained in:
cotton-ben
2020-07-01 04:36:29 -04:00
parent 5fc427d594
commit 341f9b2858
67 changed files with 1641 additions and 834 deletions
@@ -43,8 +43,7 @@ public class AutoResizeTest {
Assert.assertNotEquals(0.0, map.remainingAutoResizes());
System.out.println(actual);
}
}
}
@Test
public void testAutoResizeNotZeroUponRestart2() throws IOException {
@@ -59,7 +58,6 @@ public class AutoResizeTest {
Assert.assertNotEquals(0.0, actual);
System.out.println(actual);
}
}
}
}
@@ -18,6 +18,7 @@ package net.openhft.chronicle.map;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.hash.replication.ReplicableEntry;
import net.openhft.chronicle.threads.NamedThreadFactory;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
@@ -65,7 +66,8 @@ public class BasicReplicationTest {
processorThree.addDestinationMap(mapThree.acquireModificationIterator(asByte(1)), mapThree, mapOne);
processorThree.addDestinationMap(mapThree.acquireModificationIterator(asByte(2)), mapThree, mapTwo);
final ExecutorService executorService = Executors.newFixedThreadPool(3);
final ExecutorService executorService = Executors.newFixedThreadPool(3,
new NamedThreadFactory("test"));
executorService.submit(processorOne::processPendingChangesLoop);
executorService.submit(processorTwo::processPendingChangesLoop);
executorService.submit(processorThree::processPendingChangesLoop);
@@ -175,7 +177,8 @@ public class BasicReplicationTest {
private final ReplicatedChronicleMap<K, V, ?> sourceMap;
private final ReplicatedChronicleMap<K, V, ?> destinationMap;
private final Bytes<ByteBuffer> buffer = Bytes.elasticByteBuffer(4096);
private final ExecutorService delayedExecutor = Executors.newSingleThreadExecutor();
private final ExecutorService delayedExecutor = Executors.newSingleThreadExecutor(
new NamedThreadFactory("delayed"));
private final AtomicInteger messagesInflight = new AtomicInteger(0);
private final ArrayList<String> keys = new ArrayList<>();
@@ -42,7 +42,6 @@ public class BloatFactorTest {
Assert.assertEquals(EXPECTED_MAX_BLOAT_FACTOR, maxBloatFactor,0.0);
}
}
}
}
@@ -17,6 +17,7 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.threads.NamedThreadFactory;
import org.junit.Test;
import java.io.File;
@@ -47,10 +48,12 @@ public class ChronicleMapSanityCheckTest {
file.getAbsolutePath().toString());
ScheduledExecutorService producerExecutor =
Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() - 1);
Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() - 1,
new NamedThreadFactory("producer"));
ScheduledExecutorService consumerExecutor =
Executors.newSingleThreadScheduledExecutor();
Executors.newSingleThreadScheduledExecutor(
new NamedThreadFactory("consumer"));
int N = 1000;
@@ -23,6 +23,7 @@ import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.core.values.IntValue;
import net.openhft.chronicle.core.values.LongValue;
import net.openhft.chronicle.set.Builder;
import net.openhft.chronicle.threads.NamedThreadFactory;
import net.openhft.chronicle.values.Values;
import org.junit.Ignore;
import org.junit.Test;
@@ -917,7 +918,7 @@ public class ChronicleMapTest {
// int runs = Integer.getInteger("runs", 10);
int procs = 1; // Runtime.getRuntime().availableProcessors();
int threads = procs * 3;
ExecutorService es = Executors.newFixedThreadPool(procs);
ExecutorService es = Executors.newFixedThreadPool(procs, new NamedThreadFactory("test"));
for (int runs : new int[]{1, /*10, 250, 500, 1000, 2500*/}) {
for (int entrySize : new int[]{240, 256}) {
int valuePadding = entrySize - 16;
@@ -1009,7 +1010,7 @@ public class ChronicleMapTest {
int procs = Runtime.getRuntime().availableProcessors();
if (procs > 8) procs--;
int threads = procs * 3;
ExecutorService es = Executors.newFixedThreadPool(procs);
ExecutorService es = Executors.newFixedThreadPool(procs, new NamedThreadFactory("test"));
for (int runs : new int[]{1, 2, 5, 10, 25, 50, 100, 500, 1000, 2500}) {
final long entries = runs * 1000 * 1000L;
@@ -1106,7 +1107,7 @@ public class ChronicleMapTest {
// int runs = Integer.getInteger("runs", 10);
int procs = Runtime.getRuntime().availableProcessors();
int threads = procs * 3; // runs > 100 ? procs / 2 : procs;
ExecutorService es = Executors.newFixedThreadPool(procs);
ExecutorService es = Executors.newFixedThreadPool(procs, new NamedThreadFactory("test"));
for (int runs : new int[]{10, 50, 100, 250, 500, 1000, 2500}) {
// JAVA 8 produces more garbage than previous versions for internal work.
// System.gc();
@@ -1185,7 +1186,7 @@ public class ChronicleMapTest {
for (int j = 0; j < count; j++) {
final ConcurrentMap<String, AtomicInteger> map = new ConcurrentHashMap<String, AtomicInteger>((int) (entries * 5 / 4), 1.0f, 1024);
long start = System.currentTimeMillis();
ExecutorService es = Executors.newFixedThreadPool(procs);
ExecutorService es = Executors.newFixedThreadPool(procs, new NamedThreadFactory("test"));
for (int i = 0; i < threads; i++) {
final int t = i;
es.submit(new Runnable() {
@@ -17,6 +17,7 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.core.values.LongValue;
import net.openhft.chronicle.threads.NamedThreadFactory;
import org.junit.Ignore;
import org.junit.Test;
@@ -116,7 +117,7 @@ public class EntryCountMapTest {
public void testSmall() throws IOException, ExecutionException, InterruptedException {
System.out.print("testSmall seeds");
int procs = Runtime.getRuntime().availableProcessors();
ExecutorService es = Executors.newFixedThreadPool(procs);
ExecutorService es = Executors.newFixedThreadPool(procs, new NamedThreadFactory("test"));
for (int t = 0; t < ecmTests; t++) {
List<Future<?>> futures = new ArrayList<>();
{
@@ -160,7 +161,7 @@ public class EntryCountMapTest {
public void testMedium() throws IOException, ExecutionException, InterruptedException {
System.out.print("testMedium seeds");
int procs = Runtime.getRuntime().availableProcessors();
ExecutorService es = Executors.newFixedThreadPool(procs);
ExecutorService es = Executors.newFixedThreadPool(procs, new NamedThreadFactory("test"));
for (int t = 0; t < ecmTests; t++) {
// regression test.
List<Future<?>> futures = new ArrayList<>();
@@ -17,7 +17,6 @@
package net.openhft.chronicle.map;
import com.google.common.base.Preconditions;
import net.bytebuddy.implementation.bytecode.Throw;
import net.openhft.chronicle.core.OS;
import org.junit.Rule;
import org.junit.Test;
@@ -36,7 +35,7 @@ import static org.junit.Assert.*;
public class ExitHookTest {
private static final int KEY = 1;
private static final int JVM_STARTUP_WAIT_TIME_MS = 15_000;
private static final int JVM_STARTUP_WAIT_TIME_MS = 5_000;
private static final String PRE_SHUTDOWN_ACTION_EXECUTED = "PRE_SHUTDOWN_ACTION_EXECUTED";
private static final String USER_SHUTDOWN_HOOK_EXECUTED = "USER_SHUTDOWN_HOOK_EXECUTED";
@@ -44,64 +43,60 @@ public class ExitHookTest {
public TemporaryFolder folder = new TemporaryFolder();
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("Other process started, yo");
File mapFile = new File(args[0]);
System.out.println("1");
File shutdownActionConfirmationFile = new File(args[1]);
System.out.println("2");
ChronicleMapBuilder<Integer, Integer> mapBuilder = null;
try {
System.out.println("Other process started, yo");
File mapFile = new File(args[0]);
System.out.println("1");
File shutdownActionConfirmationFile = new File(args[1]);
System.out.println("2");
ChronicleMapBuilder<Integer, Integer> mapBuilder = null;
try {
mapBuilder = createMapBuilder();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("3");
boolean skipCloseOnExitHook = false;
System.out.println("4");
if (Boolean.parseBoolean(args[2])) {
skipCloseOnExitHook = true;
}
AtomicReference<ChronicleMap<Integer, Integer>> mapReference = new AtomicReference<>();
System.out.println("exit hook" + skipCloseOnExitHook);
if (skipCloseOnExitHook) {
mapBuilder.skipCloseOnExitHook(skipCloseOnExitHook);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
// In practice, do something with map before shutdown - like create a new map using data from this map.
mapReference.get().close();
try {
System.out.println("Executing user defined shutdown hook");
Preconditions.checkState(shutdownActionConfirmationFile.exists());
Files.write(shutdownActionConfirmationFile.toPath(),
USER_SHUTDOWN_HOOK_EXECUTED.getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
} else {
mapBuilder.setPreShutdownAction(() -> {
try {
System.out.println("Executing pre-shutdown action");
Preconditions.checkState(shutdownActionConfirmationFile.exists());
Files.write(shutdownActionConfirmationFile.toPath(),
PRE_SHUTDOWN_ACTION_EXECUTED.getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
mapReference.set(mapBuilder.createPersistedTo(mapFile));
try (ExternalMapQueryContext<Integer, Integer, ?> c = mapReference.get().queryContext(KEY)) {
c.writeLock().lock();
Thread.sleep(30_000);
}
} catch (Exception e) {
mapBuilder = createMapBuilder();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("3");
boolean skipCloseOnExitHook = false;
System.out.println("4");
if (Boolean.parseBoolean(args[2])) {
skipCloseOnExitHook = true;
}
AtomicReference<ChronicleMap<Integer, Integer>> mapReference = new AtomicReference<>();
System.out.println("exit hook" + skipCloseOnExitHook);
if (skipCloseOnExitHook) {
mapBuilder.skipCloseOnExitHook(skipCloseOnExitHook);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
// In practice, do something with map before shutdown - like create a new map using data from this map.
mapReference.get().close();
try {
System.out.println("Executing user defined shutdown hook");
Preconditions.checkState(shutdownActionConfirmationFile.exists());
Files.write(shutdownActionConfirmationFile.toPath(),
USER_SHUTDOWN_HOOK_EXECUTED.getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
} else {
mapBuilder.setPreShutdownAction(() -> {
try {
System.out.println("Executing pre-shutdown action");
Preconditions.checkState(shutdownActionConfirmationFile.exists());
Files.write(shutdownActionConfirmationFile.toPath(),
PRE_SHUTDOWN_ACTION_EXECUTED.getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
mapReference.set(mapBuilder.createPersistedTo(mapFile));
try (ExternalMapQueryContext<Integer, Integer, ?> c = mapReference.get().queryContext(KEY)) {
c.writeLock().lock();
Thread.sleep(30_000);
}
}
private static ChronicleMapBuilder<Integer, Integer> createMapBuilder()
throws IOException {
private static ChronicleMapBuilder<Integer, Integer> createMapBuilder() {
return ChronicleMapBuilder
.of(Integer.class, Integer.class)
.entries(1);
@@ -203,7 +198,7 @@ public class ExitHookTest {
System.out.println("Classpath: " + classpath);
String className = ExitHookTest.class.getCanonicalName();
String[] command = new String[] {javaBin, "-cp", classpath, className,
String[] command = new String[]{javaBin, "-cp", classpath, className,
mapFile.getAbsolutePath(), outputFile.getAbsolutePath(), String.valueOf(skipCloseOnExitHook)};
ProcessBuilder builder = new ProcessBuilder(command);
builder.inheritIO();
@@ -16,6 +16,7 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.threads.NamedThreadFactory;
import org.junit.Ignore;
import org.junit.Test;
@@ -51,7 +52,7 @@ public class LargeEntriesTest {
.createPersistedTo(file)) {
warmUpCompression(ENTRY_SIZE);
int threads = 4; //Runtime.getRuntime().availableProcessors();
ExecutorService es = Executors.newFixedThreadPool(threads);
ExecutorService es = Executors.newFixedThreadPool(threads, new NamedThreadFactory("test"));
final int block = ENTRIES / threads;
for (int i = 0; i < 3; i++) {
long start = System.currentTimeMillis();
@@ -119,7 +120,8 @@ public class LargeEntriesTest {
{
// warmUpCompression(ENTRY_SIZE);
int threads = Runtime.getRuntime().availableProcessors();
ExecutorService es = Executors.newFixedThreadPool(threads);
ExecutorService es = Executors.newFixedThreadPool(threads,
new NamedThreadFactory("test"));
final int block = ENTRIES / threads;
for (int i = 0; i < 3; i++) {
long start = System.currentTimeMillis();
@@ -18,6 +18,7 @@ package net.openhft.chronicle.map;
import com.google.common.collect.Lists;
import net.openhft.chronicle.bytes.NoBytesStore;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.core.values.IntValue;
import net.openhft.chronicle.hash.impl.util.Cleaner;
@@ -39,6 +40,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@RunWith(Parameterized.class)
public class MemoryLeaksTest {
@@ -56,6 +58,7 @@ public class MemoryLeaksTest {
private boolean persisted;
private ChronicleMapBuilder<IntValue, String> builder;
private boolean closeWithinContext;
public MemoryLeaksTest(String testType, boolean replicated, boolean persisted, boolean closeWithinContext) {
this.persisted = persisted;
this.closeWithinContext = closeWithinContext;
@@ -92,50 +95,53 @@ public class MemoryLeaksTest {
@Before
public void resetSerializerCount() {
System.err.println("This test is expect to print 'ChronicleMap ... is not closed manually, cleaned up from Cleaner'");
serializerCount.set(0);
}
@Test(timeout = 60_000)
@Test(timeout = 10_000)
public void testChronicleMapCollectedAndDirectMemoryReleased()
throws IOException, InterruptedException {
if (!OS.isWindows()) {
// This test is flaky in Linux and Mac OS apparently because some native memory from
// running previous/concurrent tests is released during this test, that infers with
// the (*) check below. The aim of this test is to check that native memory is not
// leaked and it is proven if it succeeds at least sometimes at least in some OSes.
// This tests is successful always in Windows and successful in Linux and OS X when run
// alone, rather than along all other Chronicle Map's tests.
return;
}
assertFalse(OS.isMacOSX());
// This test is flaky in Linux and Mac OS apparently because some native memory from
// running previous/concurrent tests is released during this test, that infers with
// the (*) check below. The aim of this test is to check that native memory is not
// leaked and it is proven if it succeeds at least sometimes at least in some OSes.
// This tests is successful always in Windows and successful in Linux and OS X when run
// alone, rather than along all other Chronicle Map's tests.
System.gc();
Jvm.pause(100);
long nativeMemoryUsedBeforeMap = nativeMemoryUsed();
int serializersBeforeMap = serializerCount.get();
try (ChronicleMap<IntValue, String> map = getMap()) {
long expectedNativeMemory = nativeMemoryUsedBeforeMap + map.offHeapMemoryUsed();
assertEquals(expectedNativeMemory, nativeMemoryUsed());
tryCloseFromContext(map);
WeakReference<ChronicleMap<IntValue, String>> ref = new WeakReference<>(map);
Assert.assertNotNull(ref.get());
// map = null;
// the purpose of the test is to find maps which are not closed properly.
ChronicleMap<IntValue, String> map = getMap();
long expectedNativeMemory = nativeMemoryUsedBeforeMap + map.offHeapMemoryUsed();
assertEquals(expectedNativeMemory, nativeMemoryUsed());
tryCloseFromContext(map);
WeakReference<ChronicleMap<IntValue, String>> ref = new WeakReference<>(map);
Assert.assertNotNull(ref.get());
//noinspection UnusedAssignment
map = null;
// Wait until Map is collected by GC
while (ref.get() != null) {
System.gc();
byte[] garbage = new byte[10_000_000];
Thread.yield();
// Wait until Map is collected by GC
// Wait until Cleaner is called and memory is returned to the system
for (int i = 1; i <= 10; i++) {
if (ref.get() == null &&
nativeMemoryUsedBeforeMap >= nativeMemoryUsed() && // (*)
serializerCount.get() == serializersBeforeMap) {
break;
}
// Wait until Cleaner is called and memory is returned to the system
for (int i = 0; i < 6_000; i++) {
if (nativeMemoryUsedBeforeMap == nativeMemoryUsed() && // (*)
serializerCount.get() == serializersBeforeMap) {
break;
}
System.gc();
byte[] garbage = new byte[10_000_000];
Thread.sleep(10);
}
Assert.assertEquals(nativeMemoryUsedBeforeMap, nativeMemoryUsed());
Assert.assertEquals(serializersBeforeMap, serializerCount.get());
System.gc();
Jvm.pause(i * 10);
System.out.println("ref.get()=" + (ref.get() == null));
System.out.println(nativeMemoryUsedBeforeMap + " <=> " + nativeMemoryUsed());
System.out.println(serializerCount.get() + " <=> " + serializersBeforeMap);
}
if (nativeMemoryUsedBeforeMap < nativeMemoryUsed())
Assert.assertEquals(nativeMemoryUsedBeforeMap, nativeMemoryUsed());
Assert.assertEquals(serializersBeforeMap, serializerCount.get());
}
private long nativeMemoryUsed() {
@@ -20,6 +20,7 @@ import net.openhft.chronicle.core.values.LongValue;
import net.openhft.chronicle.hash.ChecksumEntry;
import net.openhft.chronicle.hash.ChronicleHashBuilderPrivateAPI;
import net.openhft.chronicle.hash.ChronicleHashCorruption;
import net.openhft.chronicle.threads.NamedThreadFactory;
import net.openhft.chronicle.values.Values;
import org.junit.Assert;
import org.junit.Ignore;
@@ -69,7 +70,8 @@ public class RecoverTest {
// acquires read lock successfully
assertNull(map.get(0));
ExecutorService executorService = Executors.newSingleThreadExecutor();
ExecutorService executorService = Executors.newSingleThreadExecutor(
new NamedThreadFactory("recoverTest"));
executorService.submit(() -> {
ExternalMapQueryContext<Integer, Integer, ?> c = map.queryContext(0);
@@ -17,6 +17,7 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.core.values.IntValue;
import net.openhft.chronicle.threads.NamedThreadFactory;
import net.openhft.chronicle.values.Values;
import org.junit.Test;
@@ -65,14 +66,16 @@ public class TrickyContextCasesTest {
try (ExternalMapQueryContext<Integer, byte[], ?> q = map.queryContext(1)) {
MapEntry<Integer, byte[]> entry = q.entry(); // acquires read lock implicitly
assertNotNull(entry);
Executors.newFixedThreadPool(1).submit(() -> {
// this call should try to acquire write lock, that should lead to dead lock
// but if not...
// relocates the entry for the key 1 after the entry for 2, under update lock
map.put(1, new byte[]{1, 2, 3, 4, 5});
// puts the entry for 3 at the place of the entry for the key 1, under update lock
map.put(3, new byte[]{3});
}).get();
Executors.newFixedThreadPool(1,
new NamedThreadFactory("test"))
.submit(() -> {
// this call should try to acquire write lock, that should lead to dead lock
// but if not...
// relocates the entry for the key 1 after the entry for 2, under update lock
map.put(1, new byte[]{1, 2, 3, 4, 5});
// puts the entry for 3 at the place of the entry for the key 1, under update lock
map.put(3, new byte[]{3});
}).get();
// prints [3]
System.out.println(Arrays.toString(entry.value().get()));
}
@@ -18,6 +18,7 @@ package net.openhft.chronicle.map.example;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.chronicle.threads.NamedThreadFactory;
import net.openhft.chronicle.values.Array;
import net.openhft.chronicle.values.Values;
@@ -59,7 +60,8 @@ public class LotsOfEntriesMain {
.averageKeySize((Math.log(1.024) - Math.log(0.024)) * 24 + 2)
.createPersistedTo(file);
int threads = Runtime.getRuntime().availableProcessors();
ExecutorService es = Executors.newFixedThreadPool(threads);
ExecutorService es = Executors.newFixedThreadPool(threads,
new NamedThreadFactory("test"));
long block = (entries + threads - 1) / threads;
final long start = System.nanoTime();
@@ -52,8 +52,7 @@ public class StringArrayExample {
// to string all the values
System.out.println(map.getUsing(1, charSequenceArray).toString());
}
}
}
private CharSequenceArray setToHello(final Integer integer, final CharSequenceArray charSequenceArray) {
charSequenceArray.getCharSequenceWrapperAt(1).setCharSequence("hello");
@@ -0,0 +1,13 @@
package net.openhft.chronicle.map.fromdocs.acid;
public interface ChronicleAcidIsolation extends java.sql.Connection{
int LOWEST_LATENCY = ChronicleAcidIsolation.TRANSACTION_NONE;
int DIRTY_READ_OPTIMISTIC = ChronicleAcidIsolation.TRANSACTION_READ_UNCOMMITTED;
int DIRTY_READ_INTOLERANT = ChronicleAcidIsolation.TRANSACTION_READ_COMMITTED;
int REPEATABLE_READS_MANDATORY = ChronicleAcidIsolation.TRANSACTION_REPEATABLE_READ;
int PHANTOM_READ_INTOLERANT = ChronicleAcidIsolation.TRANSACTION_SERIALIZABLE;
}
@@ -0,0 +1,436 @@
package net.openhft.chronicle.map.fromdocs.acid;
//import net.openhft.affinity.AffinitySupport;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.fromdocs.BondVOInterface;
import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
public class ChronicleAcidIsolationGovernor implements ChronicleAcidIsolation {
private ChronicleMap<String, Integer> transactionIsolationMap;
private ChronicleMap<String, BondVOInterface> compositeChronicleMap; //hacked in, not generic
private String priorCusip;
private Double aCoupon;
public synchronized void put(String cusip, BondVOInterface bond) throws Exception {
System.out.println(
" , @t="+System.currentTimeMillis()+
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
" inside chrAig.put('"+cusip+"'/"+aCoupon+") BEGIN"+
", "
);
ChronicleMap<String, BondVOInterface> cMap = this.getCompositeChronicleMap();
this.aCoupon = cMap.get(cusip).getCoupon();
bond.setCoupon(aCoupon);
cMap.put(cusip, bond);
System.out.println(
" , @t="+System.currentTimeMillis()+
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
" inside chrAig.put('"+cusip+"'/"+aCoupon+") DONE"+
", "
);
}
//here is where the drama happens
public synchronized BondVOInterface get(String cusip) throws Exception {
System.out.println(
" , @t="+System.currentTimeMillis()+
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
" inside chrAig.get('"+cusip+"') BEGIN"+
", "
);
BondVOInterface b = null;
String tx = Thread.currentThread().toString();
ChronicleMap<String,Integer> txMap = this.getTransactionIsolationMap();
if (txMap.size() > 1) { //other ACID transactions are active
if (txMap.get(tx) <= ChronicleAcidIsolation.DIRTY_READ_OPTIMISTIC) {
b = this.compositeChronicleMap.get(cusip);
} else if (txMap.get(tx) >= ChronicleAcidIsolation.DIRTY_READ_INTOLERANT) {
System.out.println(
", @t="+System.currentTimeMillis()+
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
" inside chrAig.get() WAITING"+
" ,"
);
this.wait();
System.out.println(
" , @t="+System.currentTimeMillis()+
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
" inside chrAig.get() RESUMING"+
", "
);
b = this.compositeChronicleMap.get(cusip);
}
} else {
b = this.compositeChronicleMap.get(cusip);
}
System.out.println(
" , @t="+System.currentTimeMillis()+
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
" inside chrAig.get() DONE"+
" ,"
);
return b;
}
public ChronicleMap<String, Integer> getTransactionIsolationMap() {
return transactionIsolationMap;
}
public void setTransactionIsolationMap(ChronicleMap<String, Integer> txMap) {
this.transactionIsolationMap = txMap;
}
public ChronicleMap<String, BondVOInterface> getCompositeChronicleMap() {
return compositeChronicleMap;
}
public void setCompositeChronicleMap(ChronicleMap<String, BondVOInterface> ccm) {
this.compositeChronicleMap = ccm;
}
@Override
public synchronized void setTransactionIsolation(int level) throws SQLException {
this.getTransactionIsolationMap().put(
Thread.currentThread().toString(),
level
);
}
@Override
public synchronized int getTransactionIsolation() throws SQLException {
int iL = this.getTransactionIsolationMap().get(Thread.currentThread());
return(iL);
}
@Override
public synchronized void commit() throws SQLException {
System.out.println(
" ,@t="+System.currentTimeMillis()+
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
" chrAig.commit() BEGIN "+
", "
);
this.getTransactionIsolationMap().remove(Thread.currentThread().toString());
System.out.println(
" , @t="+System.currentTimeMillis()+
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
" chrAig.commit() END "+
", "
);
this.notifyAll();
System.out.println(
", @t="+System.currentTimeMillis()+
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
" chrAig.commit() complete notifyAll() to waiting Tx Threads "+
","
);
}
@Override
public synchronized void rollback() throws SQLException {
BondVOInterface priorBond = this.getCompositeChronicleMap().get("369604101");
if (priorBond != null) {
priorBond.setCoupon(3.50);
this.getCompositeChronicleMap().put("369604101", priorBond);
}
System.out.println(
" , @t="+System.currentTimeMillis()+
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
" chrAig.rollback() BEGIN "+
", "
);
this.getTransactionIsolationMap().remove(Thread.currentThread().toString());
System.out.println(
" ,@t="+System.currentTimeMillis()+
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
" chrAig.rollback() COMPLETE coupon=3.50"+
", "
);
this.notifyAll();
System.out.println(
", @t="+System.currentTimeMillis()+
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
" chrAig.rollback() completed notifyAll() to waiting Tx Threads"+
", "
);
}
// rest of these java.sql.Connection methods remain unimplemented ...
// pedantics are too
// intense. In the real world, even in Captital Markets 'dirty read' intolerance
// is likely the only isolation level Chronicle would accommodate.
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
}
@Override
public boolean getAutoCommit() throws SQLException {
return false;
}
@Override
public void rollback(Savepoint savepoint) throws SQLException {
}
@Override
public void close() throws SQLException {
}
@Override
public Statement createStatement() throws SQLException {
return null;
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
return null;
}
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
return null;
}
@Override
public String nativeSQL(String sql) throws SQLException {
return null;
}
@Override
public boolean isClosed() throws SQLException {
return false;
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
return null;
}
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
}
@Override
public boolean isReadOnly() throws SQLException {
return false;
}
@Override
public void setCatalog(String catalog) throws SQLException {
}
@Override
public String getCatalog() throws SQLException {
return null;
}
@Override
public SQLWarning getWarnings() throws SQLException {
return null;
}
@Override
public void clearWarnings() throws SQLException {
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return null;
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return null;
}
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
return null;
}
@Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
}
@Override
public void setHoldability(int holdability) throws SQLException {
}
@Override
public int getHoldability() throws SQLException {
return 0;
}
@Override
public Savepoint setSavepoint() throws SQLException {
return null;
}
@Override
public Savepoint setSavepoint(String name) throws SQLException {
return null;
}
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return null;
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
return null;
}
@Override
public Clob createClob() throws SQLException {
return null;
}
@Override
public Blob createBlob() throws SQLException {
return null;
}
@Override
public NClob createNClob() throws SQLException {
return null;
}
@Override
public SQLXML createSQLXML() throws SQLException {
return null;
}
@Override
public boolean isValid(int timeout) throws SQLException {
return false;
}
@Override
public void setClientInfo(String name, String value) throws SQLClientInfoException {
}
@Override
public void setClientInfo(Properties properties) throws SQLClientInfoException {
}
@Override
public String getClientInfo(String name) throws SQLException {
return null;
}
@Override
public Properties getClientInfo() throws SQLException {
return null;
}
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
return null;
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
return null;
}
@Override
public void setSchema(String schema) throws SQLException {
}
@Override
public String getSchema() throws SQLException {
return null;
}
@Override
public void abort(Executor executor) throws SQLException {
}
@Override
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
}
@Override
public int getNetworkTimeout() throws SQLException {
return 0;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
}
@@ -1,75 +0,0 @@
package net.openhft.chronicle.map.fromdocs.acid;
import net.openhft.chronicle.map.acid.ChronicleAcidIsolation;
import net.openhft.chronicle.map.acid.ChronicleAcidIsolationGovernor;
import net.openhft.chronicle.map.acid.BondVOInterface;
import java.sql.SQLException;
import java.util.Scanner;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadOffender implements Runnable {
ChronicleAcidIsolationGovernor chrAig;
public ChronicleAcidIsolationGovernor getCraig() {
return chrAig;
}
public void setCraig(ChronicleAcidIsolationGovernor craig) {
this.chrAig = craig;
}
@Override
public void run() {
Scanner sc = new Scanner(System.in);
try {
BondVOInterface bond = newNativeReference(BondVOInterface.class);
chrAig.getCompositeChronicleMap().acquireUsing("369604101", bond);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender calling chrAig.put('369604101'/3.85) "
);
//sc.nextLine();
chrAig.setTransactionIsolation(ChronicleAcidIsolation.LOWEST_LATENCY);
bond.setCoupon(3.85);
chrAig.put("369604101", bond);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping 1 minute. "
);
Thread.sleep(60 * 1_000);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender calling chrAig.rollback() "
);
//sc.nextLine();
//chrAig.commit(); Normally, app would be coded like this. demo the ROLLBACK
chrAig.rollback(); //forced to accommodate demo to Peter
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender ROLLED BACK "
);
} catch (Exception throwables) {
try {
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender calling chrAig.rollback "
);
chrAig.rollback();
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender ROLLED BACK "
);
} catch (SQLException e) {
e.printStackTrace();
}
throwables.printStackTrace();
}
}
}
@@ -1,82 +0,0 @@
package net.openhft.chronicle.map.fromdocs.acid;
import com.sun.org.apache.xpath.internal.operations.Bool;
import net.openhft.affinity.AffinitySupport;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.chronicle.map.acid.ChronicleAcidIsolation;
import net.openhft.chronicle.map.acid.ChronicleAcidIsolationGovernor;
import net.openhft.chronicle.map.acid.BondVOInterface;
import net.openhft.chronicle.map.fromdocs.pingpong_latency.PingPongCASLeft;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import static net.openhft.chronicle.values.Values.newNativeReference;
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> operand =
DirtyReadTolerance.acquireChronicleMapOperand(
"C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
);
ChronicleMap<String, Integer> operator =
DirtyReadTolerance.acquireChronicleMapOperator(
"C:\\Users\\buddy\\dev\\shm\\OPERATOR_CHRONICLE_MAP"
);
ChronicleAcidIsolationGovernor chrAig = new ChronicleAcidIsolationGovernor();
chrAig.setCompositeChronicleMap(operand);
chrAig.setAutoCommit(Boolean.FALSE);
chrAig.setTransactionIsolationMap(operator);
System.out.println(",Established chrAig,");
DirtyReadOffender offender = new DirtyReadOffender();
offender.setCraig(chrAig);
DirtyReadVictim victim = new DirtyReadVictim(isoLevel);
victim.setCraig(chrAig);
(new Thread(offender)).start();
(new Thread(victim)).start();
}
static ChronicleMap<String, BondVOInterface> acquireChronicleMapOperand(String operand)
throws IOException {
// ensure thread ids are globally unique.
AffinitySupport.setThreadId();
return ChronicleMapBuilder.of(String.class, BondVOInterface.class)
.entries(16)
.averageKeySize("123456789".length())
.createPersistedTo(
new File(
operand
// "C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
)
);
//.create();
}
static ChronicleMap<String, Integer> acquireChronicleMapOperator(String operator)
throws IOException {
// ensure thread ids are globally unique.
AffinitySupport.setThreadId();
return ChronicleMapBuilder.of(String.class, Integer.class)
.entries(16)
.averageKeySize("123456789".length())
.createPersistedTo(
new File(
operator
// "C:\\Users\\buddy\\dev\\shm\\OPERATOR_CHRONICLE_MAP"
)
);
//.create();
}
}
@@ -1,87 +0,0 @@
package net.openhft.chronicle.map.fromdocs.acid;
import net.openhft.chronicle.map.acid.BondVOInterface;
import net.openhft.chronicle.map.acid.ChronicleAcidIsolation;
import net.openhft.chronicle.map.acid.ChronicleAcidIsolationGovernor;
import java.sql.SQLException;
import java.util.Scanner;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadVictim implements Runnable {
int isoLevel = ChronicleAcidIsolation.LOWEST_LATENCY;
DirtyReadVictim(String isoL) {
if (isoL.equals("DIRTY_READ_INTOLERANT"))
this.isoLevel = ChronicleAcidIsolation.DIRTY_READ_INTOLERANT;
else if (isoL.equals("DIRTY_READ_OPTIMISTIC"))
this.isoLevel = ChronicleAcidIsolation.DIRTY_READ_OPTIMISTIC;
}
ChronicleAcidIsolationGovernor chrAig;
public ChronicleAcidIsolationGovernor getCraig() {
return this.chrAig;
}
public void setCraig(ChronicleAcidIsolationGovernor craig) {
this.chrAig = craig;
}
@Override
public void run() {
Scanner sc= new Scanner(System.in);
try {
System.out.println(
" ,,@t="+System.currentTimeMillis()+
" DirtyReadVictim sleeping 10 seconds"
);
Thread.sleep(10_000);
BondVOInterface bond = newNativeReference(BondVOInterface.class);
chrAig.getCompositeChronicleMap().acquireUsing("369604101", bond);
System.out.println(
" ,,@t="+System.currentTimeMillis()+
" DirtyReadVictim calling chrAig.get('369604101')"
);
//sc.nextLine();
chrAig.setTransactionIsolation(this.isoLevel);
//chrAig.setTransactionIsolation(ChronicleAcidIsolation.DIRTY_READ_INTOLERANT);
//chrAig.setTransactionIsolation(ChronicleAcidIsolation.DIRTY_READ_OPTIMISTIC);
bond = chrAig.get("369604101");
Double coupon = bond.getCoupon();
System.out.println(
" ,,@t="+System.currentTimeMillis()+
" DirtyReadVictim got() coupon="+coupon+" "
);
System.out.println(
" ,,@t="+System.currentTimeMillis()+
" DirtyReadVictim calling chrAig.commmit()"
);
//sc.nextLine();
chrAig.commit();
System.out.println(
" ,,@t="+System.currentTimeMillis()+
" DirtyReadVictim COMMITTED"
);
} catch (Exception throwables) {
try {
System.out.println(
" ,,@t="+System.currentTimeMillis()+
" DirtyReadVictim calling chrAig.rollback()"
);
//sc.nextLine();
chrAig.rollback();
System.out.println(
" ,,@t="+System.currentTimeMillis()+
" DirtyReadVictim ROLLED BACK"
);
} catch (SQLException e) {
e.printStackTrace();
}
throwables.printStackTrace();
}
}
}
@@ -0,0 +1,105 @@
package net.openhft.chronicle.map.fromdocs.acid.exodus;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.fromdocs.BondVOInterface;
import java.util.Scanner;
import java.util.concurrent.locks.StampedLock;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadOffender implements Runnable {
private int isoLevel;
private ChronicleMap chm;
private StampedLock offHeapLock;
@Override
public void run() {
Scanner sc = new Scanner(System.in);
try {
BondVOInterface bond = newNativeReference(BondVOInterface.class);
this.chm.acquireUsing("369604101", bond);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping 10 seconds "
);
Thread.sleep(10 * 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 = this.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);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender coupon=["+
bond.getCoupon()+
"] written. "
);
} finally {
this.offHeapLock.unlockWrite(stamp);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender called offHeapLock.unlockWrite(stamp);"
);
}
/**
* ben.cotton@rutgers.edu
*
* END
*
*/
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
public ChronicleMap getCraig() {
return this.chm;
}
public void setCraig(ChronicleMap craig) {
this.chm = craig;
}
public int getIsoLevel() {
return isoLevel;
}
public void setIsoLevel(int isoLevel) {
this.isoLevel = isoLevel;
}
public void setStampedLock(StampedLock _sLock) {
this.offHeapLock = _sLock;
}
}
@@ -0,0 +1,52 @@
package net.openhft.chronicle.map.fromdocs.acid.exodus;
import net.openhft.affinity.AffinitySupport;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.chronicle.map.fromdocs.BondVOInterface;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.locks.StampedLock;
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(
"C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
);
System.out.println(",Established chm,");
StampedLock sLock = new StampedLock();
DirtyReadOffender offender = new DirtyReadOffender();
offender.setCraig(chm);
offender.setStampedLock(sLock);
DirtyReadVictim victim = new DirtyReadVictim(isoLevel);
victim.setCraig(chm);
victim.setStampedLock(sLock);
(new Thread(offender)).start();
(new Thread(victim)).start();
}
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())
.createPersistedTo(
new File(
operand
// "C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
)
);
//.create();
}
}
@@ -0,0 +1,115 @@
package net.openhft.chronicle.map.fromdocs.acid.exodus;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.fromdocs.BondVOInterface;
import net.openhft.chronicle.map.fromdocs.acid.ChronicleAcidIsolation;
import java.util.Scanner;
import java.util.concurrent.locks.StampedLock;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadVictim implements Runnable {
private int isoLevel = ChronicleAcidIsolation.LOWEST_LATENCY;
private ChronicleMap chm;
private StampedLock offHeapLock;
DirtyReadVictim(String isoL) {
if (isoL.equals("DIRTY_READ_INTOLERANT"))
this.isoLevel = ChronicleAcidIsolation.DIRTY_READ_INTOLERANT;
else if (isoL.equals("DIRTY_READ_OPTIMISTIC"))
this.isoLevel = ChronicleAcidIsolation.DIRTY_READ_OPTIMISTIC;
}
@Override
public void run() {
Scanner sc = new Scanner(System.in);
try {
/**
* ben.cotton@rutgers.edu START
*/
Double coupon = 0.00;
BondVOInterface bond = newNativeReference(BondVOInterface.class);
long stamp = 0;
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim CALLING offHeapLock.tryOptimisticRead()"
);
while ((stamp = this.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 60 seconds"
);
Thread.sleep( 60_000);
} finally {
if (this.offHeapLock.validate(stamp)) {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim OPTIMISTICALLY_READ coupon=" +
coupon + " "
);
} else {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim FAILED offHeapLock.validate(stamp) " +
" must apply PESSIMISTIC_POLICY (dirty read endured)" +
" coupon=[" + coupon + "] is *DIRTY*. "
);
}
}
/**
* 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();
}
}
public ChronicleMap getCraig() {
return this.chm;
}
public void setCraig(ChronicleMap craig) {
this.chm = craig;
}
public void setStampedLock(StampedLock _sLock) {
this.offHeapLock = _sLock;
}
}
@@ -0,0 +1,105 @@
package net.openhft.chronicle.map.fromdocs.acid.genesis;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.fromdocs.BondVOInterface;
import java.util.Scanner;
import java.util.concurrent.locks.StampedLock;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadOffender implements Runnable {
private int isoLevel;
private ChronicleMap chm;
private StampedLock offHeapLock;
@Override
public void run() {
Scanner sc = new Scanner(System.in);
try {
BondVOInterface bond = newNativeReference(BondVOInterface.class);
this.chm.acquireUsing("369604101", bond);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping 60 seconds "
);
Thread.sleep(60 * 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 = this.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);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender coupon=["+
bond.getCoupon()+
"] written. "
);
} finally {
this.offHeapLock.unlockWrite(stamp);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender called offHeapLock.unlockWrite(stamp);"
);
}
/**
* ben.cotton@rutgers.edu
*
* END
*
*/
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
public ChronicleMap getCraig() {
return this.chm;
}
public void setCraig(ChronicleMap craig) {
this.chm = craig;
}
public int getIsoLevel() {
return isoLevel;
}
public void setIsoLevel(int isoLevel) {
this.isoLevel = isoLevel;
}
public void setStampedLock(StampedLock _sLock) {
this.offHeapLock = _sLock;
}
}
@@ -0,0 +1,52 @@
package net.openhft.chronicle.map.fromdocs.acid.genesis;
import net.openhft.affinity.AffinitySupport;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.chronicle.map.fromdocs.BondVOInterface;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.locks.StampedLock;
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(
"C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
);
System.out.println(",Established chm,");
StampedLock sLock = new StampedLock();
DirtyReadOffender offender = new DirtyReadOffender();
offender.setCraig(chm);
offender.setStampedLock(sLock);
DirtyReadVictim victim = new DirtyReadVictim(isoLevel);
victim.setCraig(chm);
victim.setStampedLock(sLock);
(new Thread(offender)).start();
(new Thread(victim)).start();
}
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())
.createPersistedTo(
new File(
operand
// "C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
)
);
//.create();
}
}
@@ -0,0 +1,115 @@
package net.openhft.chronicle.map.fromdocs.acid.genesis;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.fromdocs.BondVOInterface;
import net.openhft.chronicle.map.fromdocs.acid.ChronicleAcidIsolation;
import java.util.Scanner;
import java.util.concurrent.locks.StampedLock;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadVictim implements Runnable {
private int isoLevel = ChronicleAcidIsolation.LOWEST_LATENCY;
private ChronicleMap chm;
private StampedLock offHeapLock;
DirtyReadVictim(String isoL) {
if (isoL.equals("DIRTY_READ_INTOLERANT"))
this.isoLevel = ChronicleAcidIsolation.DIRTY_READ_INTOLERANT;
else if (isoL.equals("DIRTY_READ_OPTIMISTIC"))
this.isoLevel = ChronicleAcidIsolation.DIRTY_READ_OPTIMISTIC;
}
@Override
public void run() {
Scanner sc = new Scanner(System.in);
try {
/**
* ben.cotton@rutgers.edu START
*/
Double coupon = 0.00;
BondVOInterface bond = newNativeReference(BondVOInterface.class);
long stamp = 0;
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim CALLING offHeapLock.tryOptimisticRead()"
);
while ((stamp = this.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 10 seconds"
);
Thread.sleep( 10_000);
} finally {
if (this.offHeapLock.validate(stamp)) {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim OPTIMISTICALLY_READ coupon=" +
coupon + " "
);
} else {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim FAILED offHeapLock.validate(stamp) " +
" must apply PESSIMISTIC_POLICY (dirty read endured)" +
" coupon=[" + coupon + "] is *DIRTY*. "
);
}
}
/**
* 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();
}
}
public ChronicleMap getCraig() {
return this.chm;
}
public void setCraig(ChronicleMap craig) {
this.chm = craig;
}
public void setStampedLock(StampedLock _sLock) {
this.offHeapLock = _sLock;
}
}
@@ -0,0 +1,6 @@
package net.openhft.chronicle.map.fromdocs.acid.revelations;
import java.util.concurrent.locks.StampedLock;
public class ChronicleStampedLock extends StampedLock {
}
@@ -0,0 +1,94 @@
package net.openhft.chronicle.map.fromdocs.acid.revelations;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.fromdocs.BondVOInterface;
import net.openhft.chronicle.map.fromdocs.acid.revelations.DirtyReadTolerance;
import java.util.Scanner;
import java.util.concurrent.locks.StampedLock;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadOffender {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
try {
String isoLevel = args[0];
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
"C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender established chm "
);
BondVOInterface bond = newNativeReference(BondVOInterface.class);
chm.acquireUsing("369604101", bond);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender sleeping 10 seconds "
);
Thread.sleep(10 * 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();"
);
ChronicleStampedLock offHeapLock = new ChronicleStampedLock();
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);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender coupon=["+
bond.getCoupon()+
"] written. "
);
} finally {
offHeapLock.unlockWrite(stamp);
System.out.println(
" @t=" + System.currentTimeMillis() +
" DirtyReadOffender called offHeapLock.unlockWrite(stamp);"
);
}
/**
* ben.cotton@rutgers.edu
*
* END
*
*/
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
}
@@ -0,0 +1,44 @@
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;
import net.openhft.chronicle.map.fromdocs.acid.exodus.DirtyReadOffender;
import net.openhft.chronicle.map.fromdocs.acid.exodus.DirtyReadVictim;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.locks.StampedLock;
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(
"C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
);
System.out.println(",Established chm,");
}
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())
.createPersistedTo(
new File(
operand
// "C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
)
);
//.create();
}
}
@@ -0,0 +1,85 @@
package net.openhft.chronicle.map.fromdocs.acid.revelations;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.fromdocs.BondVOInterface;
import net.openhft.chronicle.map.fromdocs.acid.ChronicleAcidIsolation;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class DirtyReadVictim {
public static void main(String args[]) {
try {
/**
* ben.cotton@rutgers.edu START
*/
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
"C:\\Users\\buddy\\dev\\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();
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 60 seconds"
);
Thread.sleep(60_000);
} finally {
if (offHeapLock.validate(stamp)) {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim OPTIMISTICALLY_READ coupon=" +
coupon + " "
);
} else {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim FAILED offHeapLock.validate(stamp) " +
" must apply PESSIMISTIC_POLICY (dirty read endured)" +
" coupon=[" + coupon + "] is *DIRTY*. "
);
}
}
/**
* 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,100 @@
package net.openhft.chronicle.map.fromdocs.acid.revelations;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.fromdocs.BondVOInterface;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static net.openhft.chronicle.values.Values.newNativeReference;
import static org.junit.Assert.*;
public class DirtyReadVictimTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void main() {
try {
/**
* ben.cotton@rutgers.edu START
*/
ChronicleMap<String, BondVOInterface> chm =
DirtyReadTolerance.offHeap(
"C:\\Users\\buddy\\dev\\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();
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 60 seconds"
);
Thread.sleep(60_000);
} finally {
if (offHeapLock.validate(stamp)) {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim OPTIMISTICALLY_READ coupon=" +
coupon + " "
);
Assert.assertFalse(Boolean.TRUE);
} else {
System.out.println(
" ,,@t=" + System.currentTimeMillis() +
" DirtyReadVictim FAILED offHeapLock.validate(stamp) " +
" must apply PESSIMISTIC_POLICY (dirty read endured)" +
" coupon=[" + coupon + "] is *DIRTY*. "
);
Assert.assertFalse(Boolean.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();
}
}
}
@@ -21,7 +21,6 @@ import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.chronicle.map.fromdocs.BondVOInterface;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
@@ -29,7 +28,7 @@ import static net.openhft.chronicle.values.Values.newNativeReference;
/* on an i7-4500 laptop.
PingPongLEFT: Timing 1 x off-heap operations on /dev/shm/RDR_DIM_Mock
PingPongLEFT: Timing 1 x off-heap operations on /dev/chm/RDR_DIM_Mock
#0: compareAndSwapCoupon() 50/90/99%tile was 41 / 52 / 132
#1: compareAndSwapCoupon() 50/90/99%tile was 40 / 56 / 119
#2: compareAndSwapCoupon() 50/90/99%tile was 39 / 48 / 54
@@ -46,17 +45,11 @@ public class PingPongCASLeft {
public static void main(String... ignored) throws IOException {
ChronicleMap<String, BondVOInterface> chm = PingPongCASLeft.acquireCHM();
playPingPong(chm, 4, 5, true, "PingPongCASLeft");
playPingPong(chm, 4, 5, true, "PingPongCASLEFT");
}
static void playPingPong(
ChronicleMap<String, BondVOInterface> chm,
double _coupon,
double _coupon2,
boolean setFirst,
final String desc
) {
static void playPingPong(ChronicleMap<String, BondVOInterface> chm, double _coupon,
double _coupon2, boolean setFirst, final String desc) {
BondVOInterface bond1 = newNativeReference(BondVOInterface.class);
BondVOInterface bond2 = newNativeReference(BondVOInterface.class);
BondVOInterface bond3 = newNativeReference(BondVOInterface.class);
@@ -66,7 +59,7 @@ public class PingPongCASLeft {
chm.acquireUsing("369604102", bond2);
chm.acquireUsing("369604103", bond3);
chm.acquireUsing("369604104", bond4);
System.out.printf("\n\n" + desc + ": Timing 1 x off-heap operations on /dev/shm/RDR_DIM_Mock\n");
System.out.printf("\n\n" + desc + ": Timing 1 x off-heap operations on /dev/chm/RDR_DIM_Mock\n");
if (setFirst) {
bond1.setCoupon(_coupon);
bond2.setCoupon(_coupon);
@@ -80,9 +73,9 @@ public class PingPongCASLeft {
for (int i = 0; i < runs; i++) {
long _start = System.nanoTime(); //
while (!bond1.compareAndSwapCoupon(_coupon, _coupon2)) ;
//while (!bond2.compareAndSwapCoupon(_coupon, _coupon2)) ;
//while (!bond3.compareAndSwapCoupon(_coupon, _coupon2)) ;
//while (!bond4.compareAndSwapCoupon(_coupon, _coupon2)) ;
while (!bond2.compareAndSwapCoupon(_coupon, _coupon2)) ;
while (!bond3.compareAndSwapCoupon(_coupon, _coupon2)) ;
while (!bond4.compareAndSwapCoupon(_coupon, _coupon2)) ;
timings[i] = (System.nanoTime() - _start - timeToCallNanoTime) / 4;
}
@@ -97,8 +90,6 @@ public class PingPongCASLeft {
AffinitySupport.setThreadId();
return ChronicleMapBuilder.of(String.class, BondVOInterface.class)
.entries(16)
.averageKeySize("369604101".length())
.createPersistedTo(new File("C:\\Users\\buddy\\dev\\shm\\RDR_DIM_MOCK"));
//.create();
.averageKeySize("369604101".length()).create();
}
}
@@ -27,6 +27,6 @@ public class PingPongCASRight {
public static void main(String... ignored) throws IOException {
ChronicleMap<String, BondVOInterface> chm = PingPongCASLeft.acquireCHM();
playPingPong(chm, 5, 4, false, "PingPongCASRight");
playPingPong(chm, 5, 4, false, "PingPongCASLEFT");
}
}
@@ -32,68 +32,68 @@ import static net.openhft.chronicle.algo.bytes.Access.checkedBytesStoreAccess;
import static net.openhft.chronicle.values.Values.newNativeReference;
public class PingPongLockLeft {
public static void main(String... ignored) throws IOException, InterruptedException {
ChronicleMap<String, BondVOInterface> chm = PingPongCASLeft.acquireCHM();
playPingPong(chm, 4, 5, true, "PingPongLockLEFT");
}
static void playPingPong(ChronicleMap<String, BondVOInterface> chm, double _coupon, double _coupon2, boolean setFirst, final String desc) throws InterruptedException {
BondVOInterface bond1 = newNativeReference(BondVOInterface.class);
BondVOInterface bond2 = newNativeReference(BondVOInterface.class);
BondVOInterface bond3 = newNativeReference(BondVOInterface.class);
BondVOInterface bond4 = newNativeReference(BondVOInterface.class);
chm.acquireUsing("369604101", bond1);
chm.acquireUsing("369604102", bond2);
chm.acquireUsing("369604103", bond3);
chm.acquireUsing("369604104", bond4);
System.out.printf("\n\n" + desc + ": Timing 1 x off-heap operations on " + chm.file() + "\n");
if (setFirst) {
bond1.setCoupon(_coupon);
bond2.setCoupon(_coupon);
bond3.setCoupon(_coupon);
bond4.setCoupon(_coupon);
}
int timeToCallNanoTime = 30;
int runs = 1000000;
long[] timings = new long[runs];
for (int j = 0; j < 10; j++) {
for (int i = 0; i < runs; i++) {
long _start = System.nanoTime();
toggleCoupon(bond1, _coupon, _coupon2);
toggleCoupon(bond2, _coupon, _coupon2);
toggleCoupon(bond3, _coupon, _coupon2);
toggleCoupon(bond4, _coupon, _coupon2);
timings[i] = (System.nanoTime() - _start - timeToCallNanoTime) / 4;
}
Arrays.sort(timings);
System.out.printf("#%d: lock,compare,set,unlock 50/90/99%%tile was %,d / %,d / %,d%n",
j, timings[runs / 2], timings[runs * 9 / 10], timings[runs * 99 / 100]);
}
}
private static void toggleCoupon(BondVOInterface bond, double _coupon, double _coupon2)
throws InterruptedException {
for (int i = 0; ; i++) {
AcquisitionStrategies.<ReadWriteLockingStrategy>spinLoopOrFail(5, TimeUnit.SECONDS)
.acquire(TryAcquireOperations.writeLock(),
VanillaReadWriteWithWaitsLockingStrategy.instance(),
checkedBytesStoreAccess(),
((Byteable) bond).bytesStore(), ((Byteable) bond).offset());
try {
if (bond.getCoupon() == _coupon) {
bond.setCoupon(_coupon2);
return;
}
if (i > 1000)
Thread.yield();
} finally {
VanillaReadWriteWithWaitsLockingStrategy.instance()
.writeUnlock(checkedBytesStoreAccess(),
((Byteable) bond).bytesStore(), ((Byteable) bond).offset());
}
}
}
// public static void main(String... ignored) throws IOException, InterruptedException {
// ChronicleMap<String, BondVOInterface> chm = PingPongCASLeft.acquireCHM();
//
// playPingPong(chm, 4, 5, true, "PingPongLockLEFT");
// }
//
// static void playPingPong(ChronicleMap<String, BondVOInterface> chm, double _coupon, double _coupon2, boolean setFirst, final String desc) throws InterruptedException {
// BondVOInterface bond1 = newNativeReference(BondVOInterface.class);
// BondVOInterface bond2 = newNativeReference(BondVOInterface.class);
// BondVOInterface bond3 = newNativeReference(BondVOInterface.class);
// BondVOInterface bond4 = newNativeReference(BondVOInterface.class);
//
// chm.acquireUsing("369604101", bond1);
// chm.acquireUsing("369604102", bond2);
// chm.acquireUsing("369604103", bond3);
// chm.acquireUsing("369604104", bond4);
// System.out.printf("\n\n" + desc + ": Timing 1 x off-heap operations on " + chm.file() + "\n");
// if (setFirst) {
// bond1.setCoupon(_coupon);
// bond2.setCoupon(_coupon);
// bond3.setCoupon(_coupon);
// bond4.setCoupon(_coupon);
// }
// int timeToCallNanoTime = 30;
// int runs = 1000000;
// long[] timings = new long[runs];
// for (int j = 0; j < 10; j++) {
// for (int i = 0; i < runs; i++) {
// long _start = System.nanoTime();
// toggleCoupon(bond1, _coupon, _coupon2);
// toggleCoupon(bond2, _coupon, _coupon2);
// toggleCoupon(bond3, _coupon, _coupon2);
// toggleCoupon(bond4, _coupon, _coupon2);
//
// timings[i] = (System.nanoTime() - _start - timeToCallNanoTime) / 4;
// }
// Arrays.sort(timings);
// System.out.printf("#%d: lock,compare,set,unlock 50/90/99%%tile was %,d / %,d / %,d%n",
// j, timings[runs / 2], timings[runs * 9 / 10], timings[runs * 99 / 100]);
// }
// }
//
// private static void toggleCoupon(BondVOInterface bond, double _coupon, double _coupon2)
// throws InterruptedException {
// for (int i = 0; ; i++) {
// AcquisitionStrategies.<ReadWriteLockingStrategy>spinLoopOrFail(5, TimeUnit.SECONDS)
// .acquire(TryAcquireOperations.writeLock(),
// VanillaReadWriteWithWaitsLockingStrategy.instance(),
// checkedBytesStoreAccess(),
// ((Byteable) bond).bytesStore(), ((Byteable) bond).offset());
// try {
// if (bond.getCoupon() == _coupon) {
// bond.setCoupon(_coupon2);
// return;
// }
// if (i > 1000)
// Thread.yield();
// } finally {
// VanillaReadWriteWithWaitsLockingStrategy.instance()
// .writeUnlock(checkedBytesStoreAccess(),
// ((Byteable) bond).bytesStore(), ((Byteable) bond).offset());
// }
// }
// }
}
@@ -25,6 +25,6 @@ public class PingPongLockRight {
public static void main(String... ignored) throws IOException, InterruptedException {
ChronicleMap<String, BondVOInterface> chm = PingPongCASLeft.acquireCHM();
PingPongLockLeft.playPingPong(chm, 5, 4, false, "PingPongLockRIGHT");
//PingPongLockLeft.playPingPong(chm, 5, 4, false, "PingPongLockRIGHT");
}
}
@@ -699,8 +699,7 @@ public class ChronicleMapTest extends JSR166TestCase {
}
Assert.fail();
}
}
}
}
}
@@ -104,8 +104,7 @@ public class MapFileManager extends JMXFileManager implements MapFileManagerMBea
map.createMappedStoreAndSegments(
new PersistedChronicleHashResources(file));
keyClass = map.keyType();
keyClass = map.keyType();
valueClass = map.valueType();
size = map.longSize();
name = map.name();
@@ -116,7 +115,7 @@ public class MapFileManager extends JMXFileManager implements MapFileManagerMBea
Jvm.warn().on(getClass(), "Unable to update", e);
header = e.toString();
} finally {
mf.release();
mf.releaseLast();
assert mf.refCount() == 0;
}
}
@@ -44,8 +44,9 @@ public class Builder {
This will only work if the user is an admin on windows.
*/
file.setWritable(true);//just in case relative path was used.
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.close();//allows closing the file access on windows. forcing to close access. Only works for admin-access.
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
//allows closing the file access on windows. forcing to close access. Only works for admin-access.
}
}
//file.delete(); //isnt guaranteed on windows.
@@ -18,6 +18,7 @@ package net.openhft.chronicle.set;
import net.openhft.chronicle.hash.ChronicleHash;
import net.openhft.chronicle.hash.ChronicleHashBuilder;
import net.openhft.chronicle.threads.NamedThreadFactory;
import org.junit.Test;
import java.io.File;
@@ -55,7 +56,8 @@ public class Issue24ChronicleSetTest {
@Test
public void issue24ChronicleSetTest() throws IOException {
ChronicleSet<String> set = initSet(String.class, 1_000_000, 30);
ExecutorService executor = Executors.newFixedThreadPool(5);
ExecutorService executor = Executors.newFixedThreadPool(5,
new NamedThreadFactory("test"));
for (int i = 0; i < 10; i++) {
Runnable worker = new WorkerThread(set);
executor.execute(worker);
@@ -62,7 +62,7 @@ public class DoubleArrayTest {
// expected
}
// free the memory.
da.bytesStore().release();
da.bytesStore().releaseLast();
}
@Test