From d3f3966f2ae9fb0c0c9f3822bd3a35ca4a3aa9bc Mon Sep 17 00:00:00 2001 From: cotton-ben Date: Sun, 3 May 2020 13:16:56 -0400 Subject: [PATCH] made output spreadsheet formattable --- .../acid/ChronicleAcidIsolationGovernor.java | 107 +++++++++--------- .../fromdocs/acid/DirtyReadIntolerant.java | 64 ++--------- .../map/fromdocs/acid/DirtyReadOffender.java | 21 ++-- .../map/fromdocs/acid/DirtyReadVictim.java | 22 ++-- 4 files changed, 95 insertions(+), 119 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/map/acid/ChronicleAcidIsolationGovernor.java b/src/main/java/net/openhft/chronicle/map/acid/ChronicleAcidIsolationGovernor.java index af671d2f0..ec5c4eba8 100644 --- a/src/main/java/net/openhft/chronicle/map/acid/ChronicleAcidIsolationGovernor.java +++ b/src/main/java/net/openhft/chronicle/map/acid/ChronicleAcidIsolationGovernor.java @@ -16,50 +16,55 @@ import java.util.concurrent.Executor; public class ChronicleAcidIsolationGovernor implements ChronicleAcidIsolation { - private ChronicleMap transactionIsolationMap; + private ChronicleMap transactionIsolationMap; private ChronicleMap 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 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 txMap = this.getTransactionIsolationMap(); + String tx = Thread.currentThread().toString(); + ChronicleMap 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 getTransactionIsolationMap() { + public ChronicleMap getTransactionIsolationMap() { return transactionIsolationMap; } - public void setTransactionIsolationMap(ChronicleMap transactionIsolationMap) { - this.transactionIsolationMap = transactionIsolationMap; + public void setTransactionIsolationMap(ChronicleMap txMap) { + this.transactionIsolationMap = txMap; } public ChronicleMap 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 ... diff --git a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/DirtyReadIntolerant.java b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/DirtyReadIntolerant.java index 2213fb87a..421589c5d 100644 --- a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/DirtyReadIntolerant.java +++ b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/DirtyReadIntolerant.java @@ -21,19 +21,21 @@ public class DirtyReadIntolerant { public static void main(String... ignored) throws Exception { + System.out.println("DirtyReadOffender,chrAig,DirtyReadVictim"); + ChronicleMap operand = DirtyReadIntolerant.acquireChronicleMapOperand( - "C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP" + "C:\\Users\\buddy\\dev\\shm\\OPERAND_CHRONICLE_MAP" ); - ChronicleMap operator = + ChronicleMap 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 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 acquireChronicleMapOperator(String operator) - throws IOException { + static ChronicleMap 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 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]); -// } -// } -} +} \ No newline at end of file diff --git a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/DirtyReadOffender.java b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/DirtyReadOffender.java index 92378c18d..1f1e81d3a 100644 --- a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/DirtyReadOffender.java +++ b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/DirtyReadOffender.java @@ -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 { diff --git a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/DirtyReadVictim.java b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/DirtyReadVictim.java index a2c4a6361..0fd68cc6c 100644 --- a/src/test/java/net/openhft/chronicle/map/fromdocs/acid/DirtyReadVictim.java +++ b/src/test/java/net/openhft/chronicle/map/fromdocs/acid/DirtyReadVictim.java @@ -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) {