mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
made output spreadsheet formattable
This commit is contained in:
@@ -16,50 +16,55 @@ import java.util.concurrent.Executor;
|
||||
|
||||
public class ChronicleAcidIsolationGovernor implements ChronicleAcidIsolation {
|
||||
|
||||
private ChronicleMap<Thread, Integer> transactionIsolationMap;
|
||||
private ChronicleMap<String, Integer> transactionIsolationMap;
|
||||
private ChronicleMap<String, BondVOInterface> compositeChronicleMap; //hacked in, not generic
|
||||
private String priorCusip;
|
||||
private Double priorCoupon;
|
||||
private Double aCoupon;
|
||||
|
||||
|
||||
public synchronized void put(String cusip, BondVOInterface bond) throws Exception {
|
||||
|
||||
ChronicleMap<String,BondVOInterface> cMap = this.getCompositeChronicleMap();
|
||||
this.priorCusip = cusip;
|
||||
this.priorCoupon = cMap.get(cusip).getCoupon();
|
||||
this.aCoupon = cMap.get(cusip).getCoupon();
|
||||
|
||||
bond.setCoupon(priorCoupon + 0.001);
|
||||
this.getCompositeChronicleMap().put(cusip, bond);
|
||||
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()+
|
||||
" m.get()"+
|
||||
"---------- "
|
||||
" ,---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
|
||||
" inside chrAig.get('"+cusip+"') BEGIN"+
|
||||
"----------, "
|
||||
);
|
||||
BondVOInterface b = null;
|
||||
Thread tx = Thread.currentThread();
|
||||
ChronicleMap<Thread,Integer> txMap = this.getTransactionIsolationMap();
|
||||
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()+
|
||||
" m.get() WAITING"+
|
||||
"---------- "
|
||||
", ---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
|
||||
" inside chrAig.get() WAITING"+
|
||||
"---------- ,"
|
||||
);
|
||||
this.wait();
|
||||
System.out.println(
|
||||
" ---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread()+
|
||||
" m.get() RESUMING"+
|
||||
"---------- "
|
||||
" ,---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
|
||||
" inside chrAig.get() RESUMING"+
|
||||
"----------, "
|
||||
);
|
||||
b = this.compositeChronicleMap.get(cusip);
|
||||
}
|
||||
@@ -67,21 +72,21 @@ public class ChronicleAcidIsolationGovernor implements ChronicleAcidIsolation {
|
||||
b = this.compositeChronicleMap.get(cusip);
|
||||
}
|
||||
System.out.println(
|
||||
" ---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread()+
|
||||
" m.get() DONE"+
|
||||
"---------- "
|
||||
" ,---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
|
||||
" inside chrAig.get() DONE"+
|
||||
"---------- ,"
|
||||
);
|
||||
return b;
|
||||
}
|
||||
|
||||
public ChronicleMap<Thread, Integer> getTransactionIsolationMap() {
|
||||
public ChronicleMap<String, Integer> getTransactionIsolationMap() {
|
||||
|
||||
return transactionIsolationMap;
|
||||
}
|
||||
|
||||
public void setTransactionIsolationMap(ChronicleMap<Thread, Integer> transactionIsolationMap) {
|
||||
this.transactionIsolationMap = transactionIsolationMap;
|
||||
public void setTransactionIsolationMap(ChronicleMap<String, Integer> txMap) {
|
||||
this.transactionIsolationMap = txMap;
|
||||
}
|
||||
|
||||
public ChronicleMap<String, BondVOInterface> getCompositeChronicleMap() {
|
||||
@@ -95,16 +100,16 @@ public class ChronicleAcidIsolationGovernor implements ChronicleAcidIsolation {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTransactionIsolation(int level) throws SQLException {
|
||||
public synchronized void setTransactionIsolation(int level) throws SQLException {
|
||||
|
||||
this.getTransactionIsolationMap().put(
|
||||
Thread.currentThread(),
|
||||
Thread.currentThread().toString(),
|
||||
level
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTransactionIsolation() throws SQLException {
|
||||
public synchronized int getTransactionIsolation() throws SQLException {
|
||||
|
||||
int iL = this.getTransactionIsolationMap().get(Thread.currentThread());
|
||||
return(iL);
|
||||
@@ -113,24 +118,24 @@ public class ChronicleAcidIsolationGovernor implements ChronicleAcidIsolation {
|
||||
@Override
|
||||
public synchronized void commit() throws SQLException {
|
||||
System.out.println(
|
||||
" ---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread()+
|
||||
" ,---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
|
||||
" chrAig.commit() BEGIN "+
|
||||
"---------- "
|
||||
"----------, "
|
||||
);
|
||||
this.getTransactionIsolationMap().remove(Thread.currentThread());
|
||||
this.getTransactionIsolationMap().remove(Thread.currentThread().toString());
|
||||
System.out.println(
|
||||
" ---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread()+
|
||||
" ,---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
|
||||
" chrAig.commit() END "+
|
||||
"---------- "
|
||||
"----------, "
|
||||
);
|
||||
this.notifyAll();
|
||||
System.out.println(
|
||||
" ---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread()+
|
||||
", ---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
|
||||
" chrAig.commit() complete notifyAll() to waiting Tx Threads "+
|
||||
"---------- "
|
||||
"---------- ,"
|
||||
);
|
||||
|
||||
}
|
||||
@@ -139,28 +144,28 @@ public class ChronicleAcidIsolationGovernor implements ChronicleAcidIsolation {
|
||||
public synchronized void rollback() throws SQLException {
|
||||
BondVOInterface priorBond = this.getCompositeChronicleMap().get("369604101");
|
||||
if (priorBond != null) {
|
||||
priorBond.setCoupon(priorCoupon);
|
||||
priorBond.setCoupon(3.50);
|
||||
this.getCompositeChronicleMap().put("369604101", priorBond);
|
||||
}
|
||||
System.out.println(
|
||||
" ---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread()+
|
||||
" ,---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
|
||||
" chrAig.rollback() BEGIN "+
|
||||
"---------- "
|
||||
"----------, "
|
||||
);
|
||||
this.getTransactionIsolationMap().remove(Thread.currentThread());
|
||||
this.getTransactionIsolationMap().remove(Thread.currentThread().toString());
|
||||
System.out.println(
|
||||
" ---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread()+
|
||||
" ,---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread().toString().replaceAll(",",".")+
|
||||
" chrAig.rollback() COMPLETE "+
|
||||
"---------- "
|
||||
"----------, "
|
||||
);
|
||||
this.notifyAll();
|
||||
System.out.println(
|
||||
" ---------- @t="+System.currentTimeMillis()+
|
||||
" Tx="+Thread.currentThread()+
|
||||
", ---------- @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 ...
|
||||
|
||||
@@ -21,19 +21,21 @@ public class DirtyReadIntolerant {
|
||||
|
||||
public static void main(String... ignored) throws Exception {
|
||||
|
||||
System.out.println("DirtyReadOffender,chrAig,DirtyReadVictim");
|
||||
|
||||
ChronicleMap<String, BondVOInterface> operand =
|
||||
DirtyReadIntolerant.acquireChronicleMapOperand(
|
||||
"C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
|
||||
"C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
|
||||
);
|
||||
ChronicleMap<Thread,Integer> operator =
|
||||
ChronicleMap<String, Integer> operator =
|
||||
DirtyReadIntolerant.acquireChronicleMapOperator(
|
||||
"C:\\Users\\buddy\\dev\\shm\\OPERATOR_CHRONICLE_MAP"
|
||||
"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");
|
||||
System.out.println(",Established chrAig,");
|
||||
|
||||
DirtyReadOffender offender = new DirtyReadOffender();
|
||||
offender.setCraig(chrAig);
|
||||
@@ -47,10 +49,8 @@ public class DirtyReadIntolerant {
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
static ChronicleMap<String, BondVOInterface> acquireChronicleMapOperand(String operand)
|
||||
throws IOException {
|
||||
throws IOException {
|
||||
// ensure thread ids are globally unique.
|
||||
AffinitySupport.setThreadId();
|
||||
return ChronicleMapBuilder.of(String.class, BondVOInterface.class)
|
||||
@@ -59,17 +59,17 @@ public class DirtyReadIntolerant {
|
||||
.createPersistedTo(
|
||||
new File(
|
||||
operand
|
||||
// "C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
|
||||
// "C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP"
|
||||
)
|
||||
);
|
||||
//.create();
|
||||
}
|
||||
|
||||
static ChronicleMap<Thread, Integer> acquireChronicleMapOperator(String operator)
|
||||
throws IOException {
|
||||
static ChronicleMap<String, Integer> acquireChronicleMapOperator(String operator)
|
||||
throws IOException {
|
||||
// ensure thread ids are globally unique.
|
||||
AffinitySupport.setThreadId();
|
||||
return ChronicleMapBuilder.of(Thread.class, Integer.class)
|
||||
return ChronicleMapBuilder.of(String.class, Integer.class)
|
||||
.entries(16)
|
||||
.averageKeySize("123456789".length())
|
||||
.createPersistedTo(
|
||||
@@ -80,44 +80,4 @@ public class DirtyReadIntolerant {
|
||||
);
|
||||
//.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]);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -37,15 +37,22 @@ public class DirtyReadOffender implements Runnable {
|
||||
chrAig.setTransactionIsolation(ChronicleAcidIsolation.LOWEST_LATENCY);
|
||||
bond.setCoupon(4.55);
|
||||
chrAig.put("369604101", bond);
|
||||
System.out.println(
|
||||
" @t=" + System.currentTimeMillis() +
|
||||
" DirtyReadOffender calling chrAig.commit() ---------- "
|
||||
);
|
||||
sc.nextLine();
|
||||
chrAig.commit();
|
||||
|
||||
System.out.println(
|
||||
" @t=" + System.currentTimeMillis() +
|
||||
" DirtyReadOffender COMMITTED ---------- "
|
||||
" DirtyReadOffender sleeping 1 minute. ---------- "
|
||||
);
|
||||
Thread.sleep(60*1_000);
|
||||
System.out.println(
|
||||
" @t=" + System.currentTimeMillis() +
|
||||
" DirtyReadOffender calling chrAig.rollback() ---------- "
|
||||
);
|
||||
//sc.nextLine();
|
||||
//chrAig.commit();
|
||||
chrAig.rollback();
|
||||
System.out.println(
|
||||
" @t=" + System.currentTimeMillis() +
|
||||
" DirtyReadOffender ROLLED BACK ---------- "
|
||||
);
|
||||
} catch (Exception throwables) {
|
||||
try {
|
||||
|
||||
@@ -28,37 +28,41 @@ public class DirtyReadVictim implements Runnable {
|
||||
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 m.get('369604101')"
|
||||
" ,,---------- @t="+System.currentTimeMillis()+
|
||||
" DirtyReadVictim calling chrAig.get('369604101')"
|
||||
);
|
||||
//sc.nextLine();
|
||||
chrAig.setTransactionIsolation(ChronicleAcidIsolation.DIRTY_READ_INTOLERANT);
|
||||
bond = chrAig.getCompositeChronicleMap().get("369604101");
|
||||
bond = chrAig.get("369604101");
|
||||
Double coupon = bond.getCoupon();
|
||||
System.out.println(
|
||||
" ---------- @t="+System.currentTimeMillis()+
|
||||
" DirtyReadVictim calling chrAig.commmit()"
|
||||
" ,,---------- @t="+System.currentTimeMillis()+
|
||||
" DirtyReadVictim got() coupon="+coupon+" calling chrAig.commmit()"
|
||||
);
|
||||
//sc.nextLine();
|
||||
chrAig.commit();
|
||||
System.out.println(
|
||||
" ---------- @t="+System.currentTimeMillis()+
|
||||
" ,,---------- @t="+System.currentTimeMillis()+
|
||||
" DirtyReadVictim COMMITTED"
|
||||
);
|
||||
} catch (Exception throwables) {
|
||||
try {
|
||||
System.out.println(
|
||||
" ---------- @t="+System.currentTimeMillis()+
|
||||
" ,,---------- @t="+System.currentTimeMillis()+
|
||||
" DirtyReadVictim calling chrAig.rollback()"
|
||||
);
|
||||
sc.nextLine();
|
||||
//sc.nextLine();
|
||||
chrAig.rollback();
|
||||
System.out.println(
|
||||
" ---------- @t="+System.currentTimeMillis()+
|
||||
" ,,---------- @t="+System.currentTimeMillis()+
|
||||
" DirtyReadVictim ROLLED BACK"
|
||||
);
|
||||
} catch (SQLException e) {
|
||||
|
||||
Reference in New Issue
Block a user