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 2015/12/14 12:35:54 UTC
[2/3] jena git commit: JENA-1085: change commitLock to systemLock.
Use fixed code pattern.
JENA-1085: change commitLock to systemLock. Use fixed code pattern.
* Reorganise code to use the same pattern for operations
* Always call indexes with transaction lifecycle operations
* Rename commitLock as systemLock
* Use ReentrantLock, not ReentrantReadWriteLock
Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/393d9714
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/393d9714
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/393d9714
Branch: refs/heads/master
Commit: 393d9714df62e9da4d999c5c87aaee7a83d41a59
Parents: e5dc2f8
Author: Andy Seaborne <an...@seaborne.org>
Authored: Sat Dec 12 22:03:54 2015 +0000
Committer: Andy Seaborne <an...@seaborne.org>
Committed: Sat Dec 12 22:03:54 2015 +0000
----------------------------------------------------------------------
.../sparql/core/mem/DatasetGraphInMemory.java | 125 ++++++++++++-------
1 file changed, 81 insertions(+), 44 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jena/blob/393d9714/jena-arq/src/main/java/org/apache/jena/sparql/core/mem/DatasetGraphInMemory.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/core/mem/DatasetGraphInMemory.java b/jena-arq/src/main/java/org/apache/jena/sparql/core/mem/DatasetGraphInMemory.java
index e5585b4..b46fef0 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/core/mem/DatasetGraphInMemory.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/core/mem/DatasetGraphInMemory.java
@@ -26,7 +26,7 @@ import static org.apache.jena.sparql.core.Quad.isUnionGraph;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Iterator;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.concurrent.locks.ReentrantLock ;
import java.util.function.Consumer;
import java.util.function.Supplier;
@@ -37,11 +37,7 @@ import org.apache.jena.query.ReadWrite;
import org.apache.jena.shared.Lock;
import org.apache.jena.shared.LockMRPlusSW;
import org.apache.jena.sparql.JenaTransactionException;
-import org.apache.jena.sparql.core.DatasetGraph;
-import org.apache.jena.sparql.core.DatasetGraphTriplesQuads;
-import org.apache.jena.sparql.core.DatasetPrefixStorage;
-import org.apache.jena.sparql.core.Quad;
-import org.apache.jena.sparql.core.Transactional;
+import org.apache.jena.sparql.core.* ;
import org.slf4j.Logger;
/**
@@ -55,21 +51,15 @@ public class DatasetGraphInMemory extends DatasetGraphTriplesQuads implements Tr
private final DatasetPrefixStorage prefixes = new DatasetPrefixStorageInMemory();
+ /** This lock imposes the multiple-reader and single-writer policy of transactions */
private final Lock writeLock = new LockMRPlusSW();
- private Lock writeLock() {
- return writeLock;
- }
-
- private final ReentrantReadWriteLock commitLock = new ReentrantReadWriteLock(true);
-
/**
- * Commits must be atomic, and because a thread that is committing alters the various indexes one after another, we
- * lock out {@link #begin(ReadWrite)} while {@link #commit()} is executing.
+ * Transaction lifecycle operations must be atomi, especially begin and commit where
+ * the global state of the indexes is read or written one after another.
+ * and because a thread that is committing alters the various indexes.
*/
- private ReentrantReadWriteLock commitLock() {
- return commitLock;
- }
+ private final ReentrantLock systemLock = new ReentrantLock(true);
private final ThreadLocal<Boolean> isInTransaction = withInitial(() -> false);
@@ -125,57 +115,104 @@ public class DatasetGraphInMemory extends DatasetGraphTriplesQuads implements Tr
@Override
public void begin(final ReadWrite readWrite) {
- if (isInTransaction()) throw new JenaTransactionException("Transactions cannot be nested!");
- transactionType(readWrite);
- isInTransaction(true);
- writeLock().enterCriticalSection(readWrite.equals(READ)); // get the dataset write lock, if needed.
- commitLock().readLock().lock(); // if a commit is proceeding, wait so that we see a coherent index state
- try {
+ if (isInTransaction())
+ throw new JenaTransactionException("Transactions cannot be nested!");
+ startTransaction(readWrite) ;
+ _begin(readWrite) ;
+ }
+
+ private void _begin(ReadWrite readWrite) {
+ withLock(systemLock, () ->{
quadsIndex().begin(readWrite);
defaultGraph().begin(readWrite);
- } finally {
- commitLock().readLock().unlock();
- }
+ }) ;
+ }
+
+ /** Called transaction start code at most once per transaction. */
+ private void startTransaction(ReadWrite mode) {
+ writeLock.enterCriticalSection(mode.equals(READ)); // get the dataset write lock, if needed.
+ transactionType(mode);
+ isInTransaction(true);
}
+ /** Called transaction ending code at most once per transaction. */
+ private void finishTransaction() {
+ isInTransaction.remove();
+ transactionType.remove();
+ writeLock.leaveCriticalSection();
+ }
+
@Override
public void commit() {
- if (!isInTransaction()) throw new JenaTransactionException("Tried to commit outside a transaction!");
- commitLock().writeLock().lock();
- try {
+ if (!isInTransaction())
+ throw new JenaTransactionException("Tried to commit outside a transaction!");
+ if (transactionType().equals(WRITE))
+ _commit();
+ finishTransaction();
+ }
+
+ private void _commit() {
+ withLock(systemLock, () -> {
quadsIndex().commit();
defaultGraph().commit();
- } finally {
- commitLock().writeLock().unlock();
- }
- isInTransaction.remove();
- writeLock().leaveCriticalSection();
+ quadsIndex().end();
+ defaultGraph().end();
+ } ) ;
}
-
+
@Override
public void abort() {
- if (!isInTransaction()) throw new JenaTransactionException("Tried to abort outside a transaction!");
- end();
+ if (!isInTransaction())
+ throw new JenaTransactionException("Tried to abort outside a transaction!");
+ if (transactionType().equals(WRITE))
+ _abort();
+ finishTransaction();
}
+ private void _abort() {
+ withLock(systemLock, () -> {
+ quadsIndex().abort();
+ defaultGraph().abort();
+ quadsIndex().end();
+ defaultGraph().end();
+ } ) ;
+ }
+
@Override
public void close() {
- if (isInTransaction()) abort();
+ if (isInTransaction())
+ abort();
}
@Override
public void end() {
if (isInTransaction()) {
- if (transactionType().equals(WRITE))
- log.warn("end() called for WRITE transaction without commit or abort having been called");
+ if (transactionType().equals(WRITE)) {
+ log.warn("end() called for WRITE transaction without commit or abort having been called causing a forced abort");
+ // _abort does _end actions inside the lock.
+ _abort() ;
+ } else {
+ _end() ;
+ }
+ finishTransaction();
+ }
+ }
+
+ private void _end() {
+ withLock(systemLock, () -> {
quadsIndex().end();
defaultGraph().end();
- isInTransaction.remove();
- transactionType.remove();
- writeLock().leaveCriticalSection();
+ } ) ;
+ }
+
+ private static void withLock(java.util.concurrent.locks.Lock lock, Runnable action) {
+ lock.lock();
+ try { action.run(); }
+ finally {
+ lock.unlock();
}
}
-
+
private <T> Iterator<T> access(final Supplier<Iterator<T>> source) {
if (!isInTransaction()) {
begin(READ);