You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2018/05/11 11:45:17 UTC
[3/6] jena git commit: JENA-1542: Add JenaLifecycle elements to an
existing TransactionManager
JENA-1542: Add JenaLifecycle elements to an existing TransactionManager
Clearup the JenaLifecycle phases:
* add a post-commit step
* rename the write-back steps
Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/06f713d9
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/06f713d9
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/06f713d9
Branch: refs/heads/master
Commit: 06f713d9d11b15bb55858ca778d31496da89daec
Parents: f684479
Author: Andy Seaborne <an...@apache.org>
Authored: Fri May 4 17:05:53 2018 +0100
Committer: Andy Seaborne <an...@apache.org>
Committed: Fri May 4 17:05:53 2018 +0100
----------------------------------------------------------------------
.../jena/tdb/transaction/BlockMgrJournal.java | 20 ++++----
.../jena/tdb/transaction/ObjectFileTrans.java | 27 ++++++----
.../tdb/transaction/ObjectFileTransComplex.java | 28 +++++-----
.../jena/tdb/transaction/Transaction.java | 54 ++++++++++++++------
.../tdb/transaction/TransactionLifecycle.java | 31 +++++++----
.../tdb/transaction/TransactionManager.java | 50 +++++-------------
.../AbstractTestObjectFileTrans.java | 14 ++---
.../AbstractTestObjectFileTransComplex.java | 14 ++---
8 files changed, 129 insertions(+), 109 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jena/blob/06f713d9/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/BlockMgrJournal.java
----------------------------------------------------------------------
diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/BlockMgrJournal.java b/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/BlockMgrJournal.java
index 5b02e30..1e01c92 100644
--- a/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/BlockMgrJournal.java
+++ b/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/BlockMgrJournal.java
@@ -93,14 +93,23 @@ public class BlockMgrJournal implements BlockMgr, TransactionLifecycle
writeJournalEntry(blk) ;
this.active = false ;
}
+
+ @Override
+ public void committed(Transaction txn)
+ {}
@Override
- public void commitEnact(Transaction txn)
- {
+ public void enactCommitted(Transaction txn) {
// No-op : this is done by playing the master journal.
}
@Override
+ public void clearupCommitted(Transaction txn) {
+ // Persistent state is in the system journal.
+ clear(txn) ;
+ }
+
+ @Override
public void abort(Transaction txn)
{
checkActive() ;
@@ -108,13 +117,6 @@ public class BlockMgrJournal implements BlockMgr, TransactionLifecycle
// Do clearup of in-memory structures in clearup().
}
- @Override
- public void commitClearup(Transaction txn)
- {
- // Persistent state is in the system journal.
- clear(txn) ;
- }
-
/** Set, or reset, this BlockMgr.
*/
private void reset(Transaction txn, FileRef fileRef, BlockMgr underlyingBlockMgr)
http://git-wip-us.apache.org/repos/asf/jena/blob/06f713d9/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/ObjectFileTrans.java
----------------------------------------------------------------------
diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/ObjectFileTrans.java b/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/ObjectFileTrans.java
index 13d60b4..04cbaf3 100644
--- a/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/ObjectFileTrans.java
+++ b/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/ObjectFileTrans.java
@@ -21,17 +21,21 @@ package org.apache.jena.tdb.transaction;
import org.apache.jena.tdb.base.objectfile.ObjectFile ;
import org.apache.jena.tdb.base.objectfile.ObjectFileWrapper ;
-/** Add transactionality control to an ObjectFile.
- * ObjectFiles are "append only" so with a single writer environment,
- * we just need to manage a reset on abort.
- * A crash in a transaction will accumulate some junk in the file.
- * This is now a tradeoff of speed and space.
+/**
+ * Add transactionality control to an ObjectFile. ObjectFiles are "append only" so with a
+ * single writer environment, we just need to manage a reset on abort. A crash in a
+ * transaction will accumulate some junk in the file. This is now a tradeoff of speed and
+ * space.
*
- * Speed : append to the original file directly and tolerate junk.
+ * Speed : append to the original file directly and tolerate junk. This class.
*
- * Space : use a journal file and write to main file on commit.
+ * Space : use a journal file and write to main file on commit. {@link ObjectFileTransComplex}
*
- * @see ObjectFileTransComplex
+ * {@link ObjectFileTransComplex} has an auxilliary file that it writes to, then copies to
+ * the main file on "commit". This avoids the possibility of junk from a failed
+ * transaction on a crash but costs extra writes.
+ *
+ * The normal choice is this class.
*/
class ObjectFileTrans extends ObjectFileWrapper implements TransactionLifecycle {
ObjectFileTrans(Transaction txn /*unused*/, ObjectFile other) {
@@ -57,8 +61,11 @@ class ObjectFileTrans extends ObjectFileWrapper implements TransactionLifecycle
}
@Override
- public void commitEnact(Transaction txn) { }
+ public void committed(Transaction txn) { }
+
+ @Override
+ public void enactCommitted(Transaction txn) { }
@Override
- public void commitClearup(Transaction txn) {}
+ public void clearupCommitted(Transaction txn) {}
}
http://git-wip-us.apache.org/repos/asf/jena/blob/06f713d9/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/ObjectFileTransComplex.java
----------------------------------------------------------------------
diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/ObjectFileTransComplex.java b/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/ObjectFileTransComplex.java
index adf88e8..1683820 100644
--- a/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/ObjectFileTransComplex.java
+++ b/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/ObjectFileTransComplex.java
@@ -34,10 +34,8 @@ import org.apache.jena.tdb.base.objectfile.ObjectFile ;
*
* To flush, it writes the journal to the base file.
*
- * It is relatively untested.
- *
- * Compare to {@link ObjectFileTrans} uses the fact that object files are append only so
- * writes directly and resets on abort.
+ * It is relatively untested. Thisis not the normal implementation of a transactional
+ * ObjectFile.
*
* @see ObjectFileTrans
*/
@@ -81,9 +79,13 @@ public class ObjectFileTransComplex implements ObjectFile, TransactionLifecycle
throw new TDBTransactionException("Not in a transaction for a commit to happen") ;
transObjects.sync() ;
}
-
+
+ @Override
+ public void committed(Transaction txn)
+ {}
+
@Override
- public void commitEnact(Transaction txn)
+ public void enactCommitted(Transaction txn)
{
if ( ! inTransaction )
throw new TDBTransactionException("Not in a transaction for a commit to happen") ;
@@ -93,18 +95,18 @@ public class ObjectFileTransComplex implements ObjectFile, TransactionLifecycle
}
@Override
- public void abort(Transaction txn)
- {
- transObjects.reposition(0) ;
- }
-
- @Override
- public void commitClearup(Transaction txn)
+ public void clearupCommitted(Transaction txn)
{
transObjects.truncate(0) ;
passthrough = true ;
}
+ @Override
+ public void abort(Transaction txn)
+ {
+ transObjects.reposition(0) ;
+ }
+
/** Copy from the temporary file to the real file */
private void append()
{
http://git-wip-us.apache.org/repos/asf/jena/blob/06f713d9/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/Transaction.java
----------------------------------------------------------------------
diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/Transaction.java b/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/Transaction.java
index 9e79a00..4d07836 100644
--- a/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/Transaction.java
+++ b/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/Transaction.java
@@ -21,6 +21,7 @@ import java.io.IOException ;
import java.util.ArrayList ;
import java.util.Iterator ;
import java.util.List ;
+import java.util.function.Consumer;
import org.apache.jena.atlas.logging.Log ;
import org.apache.jena.query.ReadWrite ;
@@ -43,6 +44,7 @@ public class Transaction
private final List<ObjectFileTrans> objectFileTrans = new ArrayList<>() ;
private final List<BlockMgrJournal> blkMgrs = new ArrayList<>() ;
+ private final List<TransactionLifecycle> others = new ArrayList<>() ;
// The dataset this is a transaction over - may be a commited, pending dataset.
private final DatasetGraphTDB basedsg ;
private final long version ;
@@ -147,6 +149,16 @@ public class Transaction
SystemTDB.errlog.warn("Exception during 'commit' : transaction status not known (but not a partial commit): ",ex) ;
throw new TDBTransactionException("Exception at commit point", ex) ;
}
+
+ try {
+ committed() ;
+ } catch (RuntimeException ex) {
+ if ( isIOException(ex) )
+ SystemTDB.errlog.warn("IOException during 'committed'"+ex.getMessage()) ;
+ else
+ SystemTDB.errlog.warn("Exception during 'committed': "+ex.getMessage(), ex) ;
+ throw new TDBTransactionException("Exception during 'committed' - transaction did commit", ex) ;
+ }
outcome = TxnOutcome.W_COMMITED ;
break ;
}
@@ -173,13 +185,20 @@ public class Transaction
}
return false ;
}
+
+ /*package*/ void forAllComponents(Consumer<TransactionLifecycle> action) {
+ objectFileTrans.forEach(action);
+ blkMgrs.forEach(action);
+ others.forEach(action);
+ }
private void prepare() {
state = TxnState.PREPARING ;
- for ( TransactionLifecycle x : objectFileTrans )
- x.commitPrepare(this) ;
- for ( TransactionLifecycle x : blkMgrs )
- x.commitPrepare(this) ;
+ forAllComponents(x->x.commitPrepare(this));
+ }
+
+ private void committed() {
+ forAllComponents(x->x.committed(this));
}
public void abort() {
@@ -193,11 +212,7 @@ public class Transaction
if ( state != TxnState.ACTIVE )
throw new TDBTransactionException("Transaction has already committed or aborted") ;
try {
- // Clearup.
- for ( TransactionLifecycle x : objectFileTrans )
- x.abort(this) ;
- for ( TransactionLifecycle x : blkMgrs )
- x.abort(this) ;
+ forAllComponents(x->x.abort(this)) ;
}
catch (RuntimeException ex) {
if ( isIOException(ex) )
@@ -317,13 +332,16 @@ public class Transaction
}
}
- /** Return the list of items registered for the transaction lifecycle */
- public List<TransactionLifecycle> lifecycleComponents() {
- List<TransactionLifecycle> x = new ArrayList<>() ;
- x.addAll(objectFileTrans) ;
- x.addAll(blkMgrs) ;
- return x ;
- }
+// /** Return the list of items registered for the transaction lifecycle */
+// public List<TransactionLifecycle> lifecycleComponents() {
+// List<TransactionLifecycle> x = new ArrayList<>() ;
+// x.addAll(objectFileTrans) ;
+// x.addAll(blkMgrs) ;
+// x.addAll(others);
+// return x ;
+// }
+
+ // For development and tracking, keep these as separate lists.
/*package*/ void addComponent(ObjectFileTrans oft) {
objectFileTrans.add(oft);
@@ -332,6 +350,10 @@ public class Transaction
/*package*/ void addComponent(BlockMgrJournal blkMgr) {
blkMgrs.add(blkMgr) ;
}
+
+ /*package*/ void addAdditionaComponent(TransactionLifecycle tlc) {
+ others.add(tlc) ;
+ }
public DatasetGraphTDB getBaseDataset() {
return basedsg ;
http://git-wip-us.apache.org/repos/asf/jena/blob/06f713d9/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/TransactionLifecycle.java
----------------------------------------------------------------------
diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/TransactionLifecycle.java b/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/TransactionLifecycle.java
index 4911b06..384a46d 100644
--- a/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/TransactionLifecycle.java
+++ b/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/TransactionLifecycle.java
@@ -23,22 +23,33 @@ package org.apache.jena.tdb.transaction;
*/
public interface TransactionLifecycle
{
- // begin - commitPrepare - commitEnact - clearup
- // begin - abort - clearup
+ // begin - commitPrepare commit, then later. commitEnact - clearup
+ // begin - abort
// May be reused via a new call to begin - see impl for if that's possible.
/** Start an update transaction */
public void begin(Transaction txn) ;
- /** End of active phase - will not be making these changes*/
- public void abort(Transaction txn) ;
+ /** End of active phase - will not be making these changes. */
+ public void abort(Transaction txn);
- /** End of active phase. Make changes safe; do not update the base data. */
- public void commitPrepare(Transaction txn) ;
+ /**
+ * Prepare to commit; end of active phase.
+ * Make changes safe; do not update the base data.
+ */
+ public void commitPrepare(Transaction txn);
- /** Update the base data */
- public void commitEnact(Transaction txn) ;
+ /**
+ * The commit has happened
+ */
+ public void committed(Transaction txn) ;
+
+ /** Update the base data - called during journal flush. */
+ public void enactCommitted(Transaction txn) ;
- /** All done - transaction committed and incorporated in the base dataset - can now tidy up */
- public void commitClearup(Transaction txn) ;
+ /**
+ * All done - transaction committed and incorporated in the base dataset - can now
+ * tidy up - called during journal flush if commiting.
+ */
+ public void clearupCommitted(Transaction txn) ;
}
http://git-wip-us.apache.org/repos/asf/jena/blob/06f713d9/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/TransactionManager.java
----------------------------------------------------------------------
diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/TransactionManager.java b/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/TransactionManager.java
index 448c99c..3a3e5e9 100644
--- a/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/TransactionManager.java
+++ b/jena-tdb/src/main/java/org/apache/jena/tdb/transaction/TransactionManager.java
@@ -108,6 +108,9 @@ public class TransactionManager
List<Transaction> commitedAwaitingFlush = new ArrayList<>() ;
static AtomicLong transactionId = new AtomicLong(1) ;
+
+ private List<TransactionLifecycle> additionalComponents = new ArrayList<>();
+
// The version of the data starting at 1. The contract is that this is
// never the same for two version (it may not be monotonic increasing).
// Currently, it is sequentially increasing.
@@ -317,27 +320,14 @@ public class TransactionManager
processDelayedReplayQueue(null) ;
journal.close() ;
}
+
+ /** Add a {@link TransactionLifecycle} to transactions createed from this point onwards. */
+ public void addAdditionComponent(TransactionLifecycle txnLifecycle) {
+ additionalComponents.add(txnLifecycle);
+ }
public /*for testing only*/ static final boolean DEBUG = false ;
- // TIM
-// @Override
-// public void begin(TxnMode txnMode) {
-// if (isInTransaction())
-// throw new JenaTransactionException("Transactions cannot be nested!");
-// transactionMode.set(txnMode);
-// ReadWrite initial = txnMode.equals(TxnMode.WRITE) ? WRITE : READ;
-// _begin(initial);
-// }
-//
-// @Override
-// public void begin(final ReadWrite readWrite) {
-// if (isInTransaction())
-// throw new JenaTransactionException("Transactions cannot be nested!");
-// transactionMode.set(TxnMode.convert(readWrite));
-// _begin(readWrite) ;
-// }
-
public DatasetGraphTxn begin(TxnType txnType, String label) {
return beginInternal(txnType, txnType, label);
}
@@ -469,19 +459,7 @@ public class TransactionManager
ReadWrite mode = initialMode(txnType);
DatasetGraphTxn dsgTxn = createDSGTxn(dsg, txn, mode);
txn.setActiveDataset(dsgTxn) ;
-
- // Empty for READ ; only WRITE transactions have components that need notifying.
- // Promote is a WRITE transaction starting.
- List<TransactionLifecycle> components = dsgTxn.getTransaction().lifecycleComponents() ;
-
- if ( mode == ReadWrite.READ ) {
- // ---- Consistency check. View caching does not reset components.
- if ( components.size() != 0 )
- log.warn("read transaction, non-empty lifecycleComponents list") ;
- }
-
- for ( TransactionLifecycle component : components )
- component.begin(dsgTxn.getTransaction()) ;
+ dsgTxn.getTransaction().forAllComponents(component->component.begin(dsgTxn.getTransaction())) ;
return dsgTxn ;
}
@@ -502,6 +480,8 @@ public class TransactionManager
if ( originalTxnType == null )
originalTxnType = txnType;
Transaction txn = new Transaction(dsg, version.get(), txnType, initialMode(txnType), transactionId.getAndIncrement(), originalTxnType, label, this) ;
+ // Add the extras.
+ additionalComponents.forEach(txn::addAdditionaComponent);
return txn ;
}
@@ -735,12 +715,8 @@ public class TransactionManager
/** The stage in a commit after committing - make the changes permanent in the base data */
private void enactTransaction(Transaction transaction)
{
- // Really, really do it!
- for ( TransactionLifecycle x : transaction.lifecycleComponents() )
- {
- x.commitEnact(transaction) ;
- x.commitClearup(transaction) ;
- }
+ transaction.forAllComponents(x->x.enactCommitted(transaction));
+ transaction.forAllComponents(x->x.clearupCommitted(transaction));
transaction.signalEnacted() ;
}
http://git-wip-us.apache.org/repos/asf/jena/blob/06f713d9/jena-tdb/src/test/java/org/apache/jena/tdb/transaction/AbstractTestObjectFileTrans.java
----------------------------------------------------------------------
diff --git a/jena-tdb/src/test/java/org/apache/jena/tdb/transaction/AbstractTestObjectFileTrans.java b/jena-tdb/src/test/java/org/apache/jena/tdb/transaction/AbstractTestObjectFileTrans.java
index 5309391..e26a0f6 100644
--- a/jena-tdb/src/test/java/org/apache/jena/tdb/transaction/AbstractTestObjectFileTrans.java
+++ b/jena-tdb/src/test/java/org/apache/jena/tdb/transaction/AbstractTestObjectFileTrans.java
@@ -108,9 +108,9 @@ public abstract class AbstractTestObjectFileTrans extends BaseTest
file.begin(txn) ;
//contains(file2) ;
file.commitPrepare(txn) ;
- file.commitEnact(txn) ;
+ file.enactCommitted(txn) ;
contains(file1, "ABC") ;
- file.commitClearup(txn) ;
+ file.clearupCommitted(txn) ;
}
@Test public void objFileTrans_03()
@@ -120,9 +120,9 @@ public abstract class AbstractTestObjectFileTrans extends BaseTest
file.begin(txn) ;
write(file, "X") ;
file.commitPrepare(txn) ;
- file.commitEnact(txn) ;
+ file.enactCommitted(txn) ;
contains(file1, "ABC", "X") ;
- file.commitClearup(txn) ;
+ file.clearupCommitted(txn) ;
}
@Test public void objFileTrans_04()
@@ -132,9 +132,9 @@ public abstract class AbstractTestObjectFileTrans extends BaseTest
file.begin(txn) ;
write(file, "ABCDEFGHIJKLMNOPQRSTUVWXYZ") ;
file.commitPrepare(txn) ;
- file.commitEnact(txn) ;
+ file.enactCommitted(txn) ;
contains(file1, "ABC", "ABC", "ABCDEFGHIJKLMNOPQRSTUVWXYZ") ;
- file.commitClearup(txn) ;
+ file.clearupCommitted(txn) ;
}
@Test public void objFileTrans_05()
@@ -145,7 +145,7 @@ public abstract class AbstractTestObjectFileTrans extends BaseTest
write(file, "ABCDEF") ;
file.abort(txn) ;
contains(file1, "ABC") ;
- file.commitClearup(txn) ;
+ file.clearupCommitted(txn) ;
}
@Test public void objFileTrans_06()
http://git-wip-us.apache.org/repos/asf/jena/blob/06f713d9/jena-tdb/src/test/java/org/apache/jena/tdb/transaction/AbstractTestObjectFileTransComplex.java
----------------------------------------------------------------------
diff --git a/jena-tdb/src/test/java/org/apache/jena/tdb/transaction/AbstractTestObjectFileTransComplex.java b/jena-tdb/src/test/java/org/apache/jena/tdb/transaction/AbstractTestObjectFileTransComplex.java
index 5cecb5e..ae6808c 100644
--- a/jena-tdb/src/test/java/org/apache/jena/tdb/transaction/AbstractTestObjectFileTransComplex.java
+++ b/jena-tdb/src/test/java/org/apache/jena/tdb/transaction/AbstractTestObjectFileTransComplex.java
@@ -107,9 +107,9 @@ public abstract class AbstractTestObjectFileTransComplex extends BaseTest
// Test empty.
contains(file2) ;
file.commitPrepare(txn) ;
- file.commitEnact(txn) ;
+ file.enactCommitted(txn) ;
contains(file1, "ABC") ;
- file.commitClearup(txn) ;
+ file.clearupCommitted(txn) ;
}
@Test public void objFileTrans_03()
@@ -119,9 +119,9 @@ public abstract class AbstractTestObjectFileTransComplex extends BaseTest
file.begin(txn) ;
write(file, "X") ;
file.commitPrepare(txn) ;
- file.commitEnact(txn) ;
+ file.enactCommitted(txn) ;
contains(file1, "ABC", "X") ;
- file.commitClearup(txn) ;
+ file.clearupCommitted(txn) ;
}
@Test public void objFileTrans_04()
@@ -131,9 +131,9 @@ public abstract class AbstractTestObjectFileTransComplex extends BaseTest
file.begin(txn) ;
write(file, "ABCDEFGHIJKLMNOPQRSTUVWXYZ") ;
file.commitPrepare(txn) ;
- file.commitEnact(txn) ;
+ file.enactCommitted(txn) ;
contains(file1, "ABC", "ABC", "ABCDEFGHIJKLMNOPQRSTUVWXYZ") ;
- file.commitClearup(txn) ;
+ file.clearupCommitted(txn) ;
}
@Test public void objFileTrans_05()
@@ -144,7 +144,7 @@ public abstract class AbstractTestObjectFileTransComplex extends BaseTest
write(file, "ABCDEF") ;
file.abort(txn) ;
contains(file1, "ABC") ;
- file.commitClearup(txn) ;
+ file.clearupCommitted(txn) ;
}
@Test public void objFileTrans_06()