@Override public byte[] bytes() { if ( bytes == null ) bytes = L.uuidAsBytes(mostSignificantBits, leastSignificantBits) ; return bytes ; }
@Override public String name() { if ( name == null ) name = L.uuidToString(mostSignificantBits, leastSignificantBits) ; return name ; }
private static void write(String filename, long value) { try { L.writeStringAsUTF8(filename, Long.toString(value)) ; } catch (IOException ex) {} catch (NumberFormatException ex) {} }
@Test public void txn_promote_thread_writer_1() { Transaction txn1 = txnMgr.begin(TxnType.READ_PROMOTE) ; L.syncOtherThread(()->{ Transaction txn2 = txnMgr.begin(TxnType.WRITE) ; txn2.commit(); txn2.end() ; }) ; boolean b = txn1.promote() ; assertFalse(b) ; txn1.end() ; checkClear() ; }
/** Run synchronously but on another thread. */ public static void syncOtherThread(Runnable r) { runCallable(()->{ r.run(); return null ; }) ; }
@Test public void txn_coord_disable_writers_2() { txnMgr.blockWriters(); Transaction txn = L.syncCallThread(()->txnMgr.begin(TxnType.WRITE, false)) ; assertNull(txn) ; txnMgr.enableWriters(); Transaction txn2 = L.syncCallThread(()-> { Transaction txn1 = txnMgr.begin(TxnType.WRITE, false); assertNotNull(txn1); txn1.abort(); txn1.end(); return txn1; }) ; assertNotNull(txn2) ; }
/** Run inside a Lock */ public static <V> V callWithLock(Lock lock, Supplier<V> r) { return callWithBeforeAfter(r, ()->lock.lock(), ()->lock.unlock()) ; }
/** Run inside a Lock */ public static void withLock(Lock lock, Runnable r) { withBeforeAfter(r, ()->lock.lock(), ()->lock.unlock()) ; }
public static void quorumServer(String confFile) { // No join. L.async(() -> QuorumPeerMain.main(new String[] {confFile}) ); }
@Override public byte[] bytes() { if ( bytes == null ) bytes = L.uuidAsBytes(mostSignificantBits, leastSignificantBits) ; return bytes ; }
private static void write(String filename, long value) { try { L.writeStringAsUTF8(filename, Long.toString(value)) ; } catch (IOException ex) {} catch (NumberFormatException ex) {} }
@Test public void txn_promote_thread_writer_1() { Transaction txn1 = txnMgr.begin(TxnType.READ_PROMOTE) ; L.syncOtherThread(()->{ Transaction txn2 = txnMgr.begin(TxnType.WRITE) ; txn2.commit(); txn2.end() ; }) ; boolean b = txn1.promote() ; assertFalse(b) ; txn1.end() ; checkClear() ; }
@Override public String name() { if ( name == null ) name = L.uuidToString(mostSignificantBits, leastSignificantBits) ; return name ; }
/** Run synchronously but on another thread. */ public static <T> T syncCallThread(Supplier<T> r) { return runCallable(() -> { T t = r.get() ; return t ; }) ; }
@Test public void txn_coord_disable_writers_2() { txnMgr.blockWriters(); Transaction txn = L.syncCallThread(()->txnMgr.begin(TxnType.WRITE, false)) ; assertNull(txn) ; txnMgr.enableWriters(); Transaction txn2 = L.syncCallThread(()-> { Transaction txn1 = txnMgr.begin(TxnType.WRITE, false); assertNotNull(txn1); txn1.abort(); txn1.end(); return txn1; }) ; assertNotNull(txn2) ; }
/** A UUID string to bytes */ public static byte[] uuidAsBytes(String str) { return uuidAsBytes(UUID.fromString(str)) ; }
/** Write a string to a file as UTF-8. The file is closed after the operation. * @param filename * @param content String to be written * @throws IOException */ public static void writeStringAsUTF8(String filename, String content) throws IOException { try ( OutputStream out = IO.openOutputFileEx(filename) ) { writeStringAsUTF8(out, content) ; out.flush() ; } }
@Test public void txn_promote_thread_writer_2() { Transaction txn1 = txnMgr.begin(TxnType.READ_PROMOTE) ; L.syncOtherThread(()->{ Transaction txn2 = txnMgr.begin(TxnType.WRITE) ; txn2.abort(); txn2.end() ; }) ; boolean b = txn1.promote() ; assertTrue(b) ; // Now a writer. txn1.commit(); txn1.end() ; checkClear() ; }
/** * Java UUID to bytes (most significant first) */ public static byte[] uuidAsBytes(UUID uuid) { return uuidAsBytes(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()) ; }
/** * Jena UUID to bytes (most significant first) */ public static byte[] uuidAsBytes(JenaUUID uuid) { return uuidAsBytes(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()) ; }