You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by ss...@apache.org on 2013/07/26 12:15:52 UTC

[1/2] git commit: generalized string-based locking and used it also in ldcache

Updated Branches:
  refs/heads/develop d9e6f57a9 -> 69ce8cc63


generalized string-based locking and used it also in ldcache


Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/6625965b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/6625965b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/6625965b

Branch: refs/heads/develop
Commit: 6625965bf5f9df849b09c39bab97187ebc1d4c63
Parents: 3c4ccff
Author: Sebastian Schaffert <ss...@apache.org>
Authored: Fri Jul 26 12:15:29 2013 +0200
Committer: Sebastian Schaffert <ss...@apache.org>
Committed: Fri Jul 26 12:15:29 2013 +0200

----------------------------------------------------------------------
 .../marmotta/commons/locking/StringLocks.java   | 78 ++++++++++++++++++++
 .../marmotta/kiwi/sail/KiWiValueFactory.java    | 36 +++------
 .../marmotta/ldcache/services/LDCache.java      | 46 ++----------
 3 files changed, 94 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6625965b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/locking/StringLocks.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/locking/StringLocks.java b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/locking/StringLocks.java
new file mode 100644
index 0000000..c665e63
--- /dev/null
+++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/locking/StringLocks.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.marmotta.commons.locking;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.util.concurrent.Monitor;
+
+import java.util.HashSet;
+
+/**
+ * An implementation of dynamic name-based locks that allows more fine-grained locking methods based on a string name.
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+public class StringLocks {
+
+    private LoadingCache<String,Monitor> stringLocks;
+
+    // keeps strong references for locks that are in use
+    private HashSet<Monitor> activeLocks;
+
+    public StringLocks() {
+        stringLocks = CacheBuilder.newBuilder().weakValues().build(new LockCacheLoader());
+        activeLocks = new HashSet<>();
+    }
+
+
+    public void lock(String name) {
+        Monitor lock = stringLocks.getUnchecked(name);
+        lock.enter();
+        activeLocks.add(lock);
+    }
+
+    public void unlock(String name) {
+        Monitor lock = stringLocks.getUnchecked(name);
+        lock.leave();
+        if(lock.getOccupiedDepth() == 0) {
+            activeLocks.remove(lock);
+        }
+    }
+
+    public boolean tryLock(String name) {
+        Monitor lock = stringLocks.getUnchecked(name);
+        boolean result = lock.tryEnter();
+        if(result) {
+            activeLocks.add(lock);
+        }
+        return result;
+    }
+
+    /**
+     * A simple Guava cache loader implementation for generating object-based locks
+     */
+    private static class LockCacheLoader extends CacheLoader<Object,Monitor> {
+        @Override
+        public Monitor load(Object key) throws Exception {
+            return new Monitor();
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6625965b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java
index ca383a3..b3843b3 100644
--- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java
+++ b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiValueFactory.java
@@ -17,11 +17,9 @@
  */
 package org.apache.marmotta.kiwi.sail;
 
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
 import com.google.common.util.concurrent.Monitor;
 import org.apache.commons.lang3.LocaleUtils;
+import org.apache.marmotta.commons.locking.StringLocks;
 import org.apache.marmotta.commons.sesame.model.LiteralCommons;
 import org.apache.marmotta.commons.sesame.model.Namespaces;
 import org.apache.marmotta.commons.util.DateUtils;
@@ -69,8 +67,8 @@ public class KiWiValueFactory implements ValueFactory {
     private KiWiStore store;
 
 
-    private LoadingCache<String,Monitor> resourceLocks;
-    private LoadingCache<String,Monitor> literalLocks;
+    private StringLocks resourceLocks;
+    private StringLocks literalLocks;
 
     private String defaultContext;
 
@@ -99,8 +97,8 @@ public class KiWiValueFactory implements ValueFactory {
     };
 
     public KiWiValueFactory(KiWiStore store, String defaultContext) {
-        resourceLocks = CacheBuilder.newBuilder().weakValues().build(new LockCacheLoader());
-        literalLocks  = CacheBuilder.newBuilder().weakValues().build(new LockCacheLoader());
+        resourceLocks = new StringLocks();
+        literalLocks  = new StringLocks();
 
         anonIdGenerator = new Random();
         tripleRegistry  = store.tripleRegistry;
@@ -167,8 +165,7 @@ public class KiWiValueFactory implements ValueFactory {
     @Override
     public URI createURI(String uri) {
 
-        Monitor lock = resourceLocks.getUnchecked(uri);
-        lock.enter();
+        resourceLocks.lock(uri);
 
         try {
             KiWiUriResource result = batchUriLookup.get(uri);
@@ -219,7 +216,7 @@ public class KiWiValueFactory implements ValueFactory {
                 }
             }
         } finally {
-            lock.leave();
+            resourceLocks.unlock(uri);
         }
     }
 
@@ -250,8 +247,7 @@ public class KiWiValueFactory implements ValueFactory {
      */
     @Override
     public BNode createBNode(String nodeID) {
-        Monitor lock = resourceLocks.getUnchecked(nodeID);
-        lock.enter();
+        resourceLocks.lock(nodeID);
 
         try {
             KiWiAnonResource result = batchBNodeLookup.get(nodeID);
@@ -296,7 +292,7 @@ public class KiWiValueFactory implements ValueFactory {
                 }
             }
         } finally {
-            lock.leave();
+            resourceLocks.unlock(nodeID);
         }
     }
 
@@ -392,8 +388,7 @@ public class KiWiValueFactory implements ValueFactory {
         }
         String key = LiteralCommons.createCacheKey(value.toString(),locale,type);
 
-        Monitor lock = literalLocks.getUnchecked(key);
-        lock.enter();
+        literalLocks.lock(key);
 
         try {
             KiWiLiteral result = batchLiteralLookup.get(key);
@@ -512,7 +507,7 @@ public class KiWiValueFactory implements ValueFactory {
                 }
             }
         } finally {
-            lock.leave();
+            literalLocks.unlock(key);
         }
     }
 
@@ -803,14 +798,5 @@ public class KiWiValueFactory implements ValueFactory {
         }
     }
 
-    /**
-     * A simple Guava cache loader implementation for generating object-based locks
-     */
-    private static class LockCacheLoader extends CacheLoader<Object,Monitor> {
-        @Override
-        public Monitor load(Object key) throws Exception {
-            return new Monitor();
-        }
-    }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6625965b/libraries/ldcache/ldcache-core/src/main/java/org/apache/marmotta/ldcache/services/LDCache.java
----------------------------------------------------------------------
diff --git a/libraries/ldcache/ldcache-core/src/main/java/org/apache/marmotta/ldcache/services/LDCache.java b/libraries/ldcache/ldcache-core/src/main/java/org/apache/marmotta/ldcache/services/LDCache.java
index 4c5a894..9845579 100644
--- a/libraries/ldcache/ldcache-core/src/main/java/org/apache/marmotta/ldcache/services/LDCache.java
+++ b/libraries/ldcache/ldcache-core/src/main/java/org/apache/marmotta/ldcache/services/LDCache.java
@@ -18,6 +18,7 @@
 package org.apache.marmotta.ldcache.services;
 
 import info.aduna.iteration.CloseableIteration;
+import org.apache.marmotta.commons.locking.StringLocks;
 import org.apache.marmotta.ldcache.api.LDCachingBackend;
 import org.apache.marmotta.ldcache.api.LDCachingConnection;
 import org.apache.marmotta.ldcache.api.LDCachingService;
@@ -27,7 +28,6 @@ import org.apache.marmotta.ldclient.api.ldclient.LDClientService;
 import org.apache.marmotta.ldclient.exception.DataRetrievalException;
 import org.apache.marmotta.ldclient.model.ClientResponse;
 import org.apache.marmotta.ldclient.services.ldclient.LDClient;
-import org.openrdf.model.Resource;
 import org.openrdf.model.Statement;
 import org.openrdf.model.URI;
 import org.openrdf.repository.RepositoryConnection;
@@ -37,8 +37,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.Date;
-import java.util.HashMap;
-import java.util.concurrent.locks.ReentrantLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 /**
@@ -54,7 +52,7 @@ public class LDCache implements LDCachingService {
 
 
     // lock a resource while refreshing it so that not several threads trigger a refresh at the same time
-    private HashMap<Resource,ReentrantLock> resourceLocks;
+    private StringLocks resourceLocks;
 
     private LDClientService  ldclient;
 
@@ -73,7 +71,7 @@ public class LDCache implements LDCachingService {
     public LDCache(CacheConfiguration config, LDCachingBackend backend) {
         log.info("Linked Data Caching Service initialising ...");
 
-        this.resourceLocks = new HashMap<Resource, ReentrantLock>();
+        this.resourceLocks = new StringLocks();
         this.backend  = backend;
         this.ldclient = new LDClient(config.getClientConfiguration());
         this.config   = config;
@@ -207,7 +205,7 @@ public class LDCache implements LDCachingService {
      */
     @Override
     public void refreshResource(URI resource, boolean forceRefresh) {
-        final ReentrantLock lock = lockResource(resource);
+        resourceLocks.lock(resource.stringValue());
         try {
             LDCachingConnection cacheConnection = backend.getCacheConnection(resource.stringValue());
             CacheEntry entry = null;
@@ -321,7 +319,7 @@ public class LDCache implements LDCachingService {
         } catch (RepositoryException e) {
             log.error("repository exception while obtaining cache connection",e);
         } finally {
-            unlockResource(resource, lock);
+            resourceLocks.unlock(resource.stringValue());
         }
 
     }
@@ -418,38 +416,4 @@ public class LDCache implements LDCachingService {
     public LDClientService getLDClient() {
         return ldclient;
     }
-
-    private ReentrantLock lockResource(final URI resource) {
-        ReentrantLock lock;
-        synchronized (resourceLocks) {
-            lock = resourceLocks.get(resource);
-            if(lock == null) {
-                lock = new ReentrantLock();
-                resourceLocks.put(resource,lock);
-            }
-        }
-        lock.lock();
-        return lock;
-    }
-
-    private void unlockResource(final URI resource, final ReentrantLock lock) {
-        synchronized (resourceLocks) {
-            // lock = resourceLocks.get(resource);
-            if (lock != null) {
-                if (!lock.hasQueuedThreads()) {
-                    resourceLocks.remove(resource);
-                }
-            }
-        }
-        if (lock != null) {
-            try {
-                lock.unlock();
-            } catch (IllegalMonitorStateException e) {
-                log.error("Could not release lock for {} (Thread: {}, Lock: {})", resource, Thread.currentThread().getName(), lock.toString() );
-                throw e;
-            }
-
-        }
-    }
-
 }


[2/2] git commit: Merge remote-tracking branch 'origin/develop' into develop

Posted by ss...@apache.org.
Merge remote-tracking branch 'origin/develop' into develop


Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/69ce8cc6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/69ce8cc6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/69ce8cc6

Branch: refs/heads/develop
Commit: 69ce8cc633f6550a146c2bc1274b9fe583f50ad1
Parents: 6625965 d9e6f57
Author: Sebastian Schaffert <ss...@apache.org>
Authored: Fri Jul 26 12:15:38 2013 +0200
Committer: Sebastian Schaffert <ss...@apache.org>
Committed: Fri Jul 26 12:15:38 2013 +0200

----------------------------------------------------------------------
 .../services/task/TaskManagerServiceImpl.java   | 34 +++++++++++---------
 1 file changed, 18 insertions(+), 16 deletions(-)
----------------------------------------------------------------------