mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Stub for a Set of Tests to drive Chronicle's potential to accommodate highly pedantic isolation zealots within its ACID transactions capability. Top priority ... does Chronicle have the ambition to accommodate the dirty read intolerant?
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
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 DirtyReadIntolerant {
|
||||
|
||||
public static void main(String... ignored) throws IOException, SQLException {
|
||||
|
||||
ChronicleMap<String, BondVOInterface> operand =
|
||||
DirtyReadIntolerant.acquireChronicleMapOperand(
|
||||
"C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
|
||||
);
|
||||
ChronicleMap<Thread,Integer> operator =
|
||||
DirtyReadIntolerant.acquireChronicleMapOperator(
|
||||
"C:\\Users\\buddy\\dev\\shm\\OPERATOR_CHRONICLE_MAP"
|
||||
);
|
||||
ChronicleAcidIsolationGovernor craig = new ChronicleAcidIsolationGovernor();
|
||||
craig.setCompositeChronicleMap(operand);
|
||||
craig.setAutoCommit(Boolean.FALSE);
|
||||
craig.setTransactionIsolationMap(operator);
|
||||
|
||||
DirtyReadOffender offender = new DirtyReadOffender();
|
||||
offender.setCraig(craig);
|
||||
|
||||
DirtyReadVictim victim = new DirtyReadVictim();
|
||||
victim.setCraig(craig);
|
||||
|
||||
(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<Thread, Integer> acquireChronicleMapOperator(String operator)
|
||||
throws IOException {
|
||||
// ensure thread ids are globally unique.
|
||||
AffinitySupport.setThreadId();
|
||||
return ChronicleMapBuilder.of(Thread.class, Integer.class)
|
||||
.entries(16)
|
||||
.averageKeySize("123456789".length())
|
||||
.createPersistedTo(
|
||||
new File(
|
||||
operator
|
||||
// "C:\\Users\\buddy\\dev\\shm\\OPERATOR_CHRONICLE_MAP"
|
||||
)
|
||||
);
|
||||
//.create();
|
||||
}
|
||||
|
||||
// static void offendPrice(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);
|
||||
// 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 /dev/shm/RDR_DIM_Mock\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(); //
|
||||
// while (!bond1.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;
|
||||
// }
|
||||
// Arrays.sort(timings);
|
||||
// System.out.printf("#%d: compareAndSwapCoupon() 50/90/99%%tile was %,d / %,d / %,d%n",
|
||||
// j, timings[runs / 2], timings[runs * 9 / 10], timings[runs * 99 / 100]);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
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 static net.openhft.chronicle.values.Values.newNativeReference;
|
||||
|
||||
public class DirtyReadOffender implements Runnable {
|
||||
|
||||
ChronicleAcidIsolationGovernor craig;
|
||||
|
||||
public ChronicleAcidIsolationGovernor getCraig() {
|
||||
return craig;
|
||||
}
|
||||
|
||||
public void setCraig(ChronicleAcidIsolationGovernor craig) {
|
||||
this.craig = craig;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
BondVOInterface bond = newNativeReference(BondVOInterface.class);
|
||||
craig.getCompositeChronicleMap().acquireUsing("369604101", bond);
|
||||
craig.setTransactionIsolation(ChronicleAcidIsolation.LOWEST_LATENCY);
|
||||
bond.setCoupon(4.55);
|
||||
craig.put("369604101",bond);
|
||||
craig.commit();
|
||||
} catch (Exception throwables) {
|
||||
try {
|
||||
craig.rollback();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
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 static net.openhft.chronicle.values.Values.newNativeReference;
|
||||
|
||||
public class DirtyReadVictim implements Runnable {
|
||||
|
||||
ChronicleAcidIsolationGovernor craig;
|
||||
|
||||
public ChronicleAcidIsolationGovernor getCraig() {
|
||||
return craig;
|
||||
}
|
||||
|
||||
public void setCraig(ChronicleAcidIsolationGovernor craig) {
|
||||
this.craig = craig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
BondVOInterface bond = newNativeReference(BondVOInterface.class);
|
||||
craig.getCompositeChronicleMap().acquireUsing("369604101", bond);
|
||||
|
||||
craig.setTransactionIsolation(ChronicleAcidIsolation.DIRTY_READ_INTOLERANT); //start tx
|
||||
bond = craig.getCompositeChronicleMap().get("369604101");
|
||||
Double coupon = bond.getCoupon();
|
||||
craig.commit();
|
||||
} catch (Exception throwables) {
|
||||
try {
|
||||
craig.rollback();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+18
-9
@@ -21,6 +21,7 @@ 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;
|
||||
|
||||
@@ -28,7 +29,7 @@ import static net.openhft.chronicle.values.Values.newNativeReference;
|
||||
|
||||
/* on an i7-4500 laptop.
|
||||
|
||||
PingPongLEFT: Timing 1 x off-heap operations on /dev/chm/RDR_DIM_Mock
|
||||
PingPongLEFT: Timing 1 x off-heap operations on /dev/shm/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
|
||||
@@ -45,11 +46,17 @@ 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);
|
||||
@@ -59,7 +66,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/chm/RDR_DIM_Mock\n");
|
||||
System.out.printf("\n\n" + desc + ": Timing 1 x off-heap operations on /dev/shm/RDR_DIM_Mock\n");
|
||||
if (setFirst) {
|
||||
bond1.setCoupon(_coupon);
|
||||
bond2.setCoupon(_coupon);
|
||||
@@ -73,9 +80,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;
|
||||
}
|
||||
@@ -90,6 +97,8 @@ public class PingPongCASLeft {
|
||||
AffinitySupport.setThreadId();
|
||||
return ChronicleMapBuilder.of(String.class, BondVOInterface.class)
|
||||
.entries(16)
|
||||
.averageKeySize("369604101".length()).create();
|
||||
.averageKeySize("369604101".length())
|
||||
.createPersistedTo(new File("C:\\Users\\buddy\\dev\\shm\\RDR_DIM_MOCK"));
|
||||
//.create();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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, "PingPongCASLEFT");
|
||||
playPingPong(chm, 5, 4, false, "PingPongCASRight");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user