You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by im...@apache.org on 2015/01/21 15:12:40 UTC
stratos git commit: Switching off read write dead-lock detection
logic if lock monitor is disabled to save resources
Repository: stratos
Updated Branches:
refs/heads/master 547e211a9 -> 2f0b9040c
Switching off read write dead-lock detection logic if lock monitor is disabled to save resources
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/2f0b9040
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/2f0b9040
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/2f0b9040
Branch: refs/heads/master
Commit: 2f0b9040c57d0647afc5e2900bc92f71ea468cfd
Parents: 547e211
Author: Imesh Gunaratne <im...@apache.org>
Authored: Wed Jan 21 19:40:31 2015 +0530
Committer: Imesh Gunaratne <im...@apache.org>
Committed: Wed Jan 21 19:42:32 2015 +0530
----------------------------------------------------------------------
.../common/concurrent/locks/ReadWriteLock.java | 61 ++++++++++++-------
.../concurrent/locks/ReadWriteLockMonitor.java | 7 ++-
.../stratos/common/test/ReadWriteLockTest.java | 64 ++++++--------------
.../stratos/common/test/TestLogAppender.java | 52 ++++++++++++++++
4 files changed, 112 insertions(+), 72 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/stratos/blob/2f0b9040/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/concurrent/locks/ReadWriteLock.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/concurrent/locks/ReadWriteLock.java b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/concurrent/locks/ReadWriteLock.java
index 205b777..46330af 100644
--- a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/concurrent/locks/ReadWriteLock.java
+++ b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/concurrent/locks/ReadWriteLock.java
@@ -47,17 +47,21 @@ public class ReadWriteLock {
private final String name;
private final ReentrantReadWriteLock lock;
private final Map<Long, Map<LockType, LockMetadata>> threadToLockSetMap;
- private final boolean readWriteLockMonitorEnabled = Boolean.getBoolean("read.write.lock.monitor.enabled");
- private final int readWriteLockMonitorInterval = Integer.getInteger("read.write.lock.monitor.interval", 30000);
- private final int threadPoolSize = Integer.getInteger(READ_WRITE_LOCK_MONITOR_THREAD_POOL_SIZE_KEY, 10);
+ private boolean readWriteLockMonitorEnabled;
+ private int readWriteLockMonitorInterval;
+ private int threadPoolSize;
public ReadWriteLock(String name) {
this.name = name;
this.lock = new ReentrantReadWriteLock(true);
this.threadToLockSetMap = new ConcurrentHashMap<Long, Map<LockType, LockMetadata>>();
+ readWriteLockMonitorEnabled = Boolean.getBoolean("read.write.lock.monitor.enabled");
if (readWriteLockMonitorEnabled) {
// Schedule read write lock monitor
+ readWriteLockMonitorInterval = Integer.getInteger("read.write.lock.monitor.interval", 30000);
+ threadPoolSize = Integer.getInteger(READ_WRITE_LOCK_MONITOR_THREAD_POOL_SIZE_KEY, 10);
+
ScheduledExecutorService scheduledExecutorService = StratosThreadPool.getScheduledExecutorService(
READ_WRITE_LOCK_MONITOR_THREAD_POOL, threadPoolSize);
scheduledExecutorService.scheduleAtFixedRate(new ReadWriteLockMonitor(this),
@@ -100,22 +104,27 @@ public class ReadWriteLock {
getName(), currentThread.getId(), currentThread.getName()));
}
- // Check whether the thread has already taken a read lock before requesting a write lock
- Map<LockType, LockMetadata> lockTypeLongMap = getLockTypeLongMap(currentThread.getId());
- if (lockTypeLongMap.containsKey(LockType.Read)) {
- String message = String.format("System error, cannot acquire a write lock while having a " +
- "read lock on the same thread: [lock-name] %s [thread-id] %d [thread-name] %s",
- getName(), currentThread.getId(), currentThread.getName());
- InvalidLockRequestedException exception = new InvalidLockRequestedException(message);
- log.error(exception);
- throw exception;
+ if (readWriteLockMonitorEnabled) {
+ // Check whether the thread has already taken a read lock before requesting a write lock
+ Map<LockType, LockMetadata> lockTypeLongMap = getLockTypeLongMap(currentThread.getId());
+ if (lockTypeLongMap.containsKey(LockType.Read)) {
+ String message = String.format("System error, cannot acquire a write lock while having a " +
+ "read lock on the same thread: [lock-name] %s [thread-id] %d [thread-name] %s",
+ getName(), currentThread.getId(), currentThread.getName());
+ InvalidLockRequestedException exception = new InvalidLockRequestedException(message);
+ log.error(exception);
+ throw exception;
+ }
}
lock.writeLock().lock();
- LockMetadata lockMetadata = new LockMetadata(getName(), LockType.Write, currentThread.getId(),
- currentThread.getName(), currentThread.getStackTrace(), System.currentTimeMillis());
- lockTypeLongMap.put(lockMetadata.getLockType(), lockMetadata);
+ if (readWriteLockMonitorEnabled) {
+ LockMetadata lockMetadata = new LockMetadata(getName(), LockType.Write, currentThread.getId(),
+ currentThread.getName(), currentThread.getStackTrace(), System.currentTimeMillis());
+ Map<LockType, LockMetadata> lockTypeLongMap = getLockTypeLongMap(currentThread.getId());
+ lockTypeLongMap.put(lockMetadata.getLockType(), lockMetadata);
+ }
if (log.isDebugEnabled()) {
log.debug(String.format("Write lock acquired: [lock-name] %s [thread-id] %d [thread-name] %s",
@@ -135,8 +144,10 @@ public class ReadWriteLock {
lock.writeLock().unlock();
- Map<LockType, LockMetadata> lockTypeLongMap = getLockTypeLongMap(currentThread.getId());
- lockTypeLongMap.remove(LockType.Write);
+ if (readWriteLockMonitorEnabled) {
+ Map<LockType, LockMetadata> lockTypeLongMap = getLockTypeLongMap(currentThread.getId());
+ lockTypeLongMap.remove(LockType.Write);
+ }
if (log.isDebugEnabled()) {
log.debug(String.format("Write lock released: [lock-name] %s [thread-id] %d [thread-name] %s",
@@ -156,10 +167,12 @@ public class ReadWriteLock {
lock.readLock().lock();
- Map<LockType, LockMetadata> lockTypeLongMap = getLockTypeLongMap(currentThread.getId());
- LockMetadata lockMetadata = new LockMetadata(getName(), LockType.Read, currentThread.getId(),
- currentThread.getName(), currentThread.getStackTrace(), System.currentTimeMillis());
- lockTypeLongMap.put(lockMetadata.getLockType(), lockMetadata);
+ if (readWriteLockMonitorEnabled) {
+ Map<LockType, LockMetadata> lockTypeLongMap = getLockTypeLongMap(currentThread.getId());
+ LockMetadata lockMetadata = new LockMetadata(getName(), LockType.Read, currentThread.getId(),
+ currentThread.getName(), currentThread.getStackTrace(), System.currentTimeMillis());
+ lockTypeLongMap.put(lockMetadata.getLockType(), lockMetadata);
+ }
if (log.isDebugEnabled()) {
log.debug(String.format("Read lock acquired: [lock-name] %s [thread-id] %d [thread-name] %s",
@@ -179,8 +192,10 @@ public class ReadWriteLock {
lock.readLock().unlock();
- Map<LockType, LockMetadata> lockTypeLongMap = getLockTypeLongMap(currentThread.getId());
- lockTypeLongMap.remove(LockType.Read);
+ if (readWriteLockMonitorEnabled) {
+ Map<LockType, LockMetadata> lockTypeLongMap = getLockTypeLongMap(currentThread.getId());
+ lockTypeLongMap.remove(LockType.Read);
+ }
if (log.isDebugEnabled()) {
log.debug(String.format("Read lock released: [lock-name] %s [thread-id] %d [thread-name] %s",
http://git-wip-us.apache.org/repos/asf/stratos/blob/2f0b9040/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/concurrent/locks/ReadWriteLockMonitor.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/concurrent/locks/ReadWriteLockMonitor.java b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/concurrent/locks/ReadWriteLockMonitor.java
index bec1dbb..6190a33 100644
--- a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/concurrent/locks/ReadWriteLockMonitor.java
+++ b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/concurrent/locks/ReadWriteLockMonitor.java
@@ -32,10 +32,11 @@ class ReadWriteLockMonitor implements Runnable {
private static final Log log = LogFactory.getLog(ReadWriteLockMonitor.class);
- private static final long LOCK_TIMEOUT = Long.getLong("read.write.lock.timeout", 30000); // 30 seconds
+ private long lockTimeout;
private ReadWriteLock readWriteLock;
ReadWriteLockMonitor(ReadWriteLock readWriteLock) {
+ lockTimeout = Long.getLong("read.write.lock.timeout", 30000); // 30 seconds
this.readWriteLock = readWriteLock;
}
@@ -62,10 +63,10 @@ class ReadWriteLockMonitor implements Runnable {
}
private void checkTimeout(LockMetadata lockMetadata) {
- if ((System.currentTimeMillis() - lockMetadata.getCreatedTime()) > LOCK_TIMEOUT) {
+ if ((System.currentTimeMillis() - lockMetadata.getCreatedTime()) > lockTimeout) {
String message = String.format("System error, lock has not released for %d seconds: " +
"[lock-name] %s [lock-type] %s [thread-id] %d [thread-name] %s [stack-trace] \n%s",
- LOCK_TIMEOUT / (1000), lockMetadata.getLockName(), lockMetadata.getLockType(),
+ lockTimeout / (1000), lockMetadata.getLockName(), lockMetadata.getLockType(),
lockMetadata.getThreadId(), lockMetadata.getThreadName(), stackTraceToString(
lockMetadata.getStackTrace()));
LockNotReleasedException exception = new LockNotReleasedException();
http://git-wip-us.apache.org/repos/asf/stratos/blob/2f0b9040/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/ReadWriteLockTest.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/ReadWriteLockTest.java b/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/ReadWriteLockTest.java
index 2fa7ef5..9f5a8c9 100644
--- a/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/ReadWriteLockTest.java
+++ b/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/ReadWriteLockTest.java
@@ -19,14 +19,14 @@
package org.apache.stratos.common.test;
-import org.apache.log4j.AppenderSkeleton;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.log4j.Level;
import org.apache.log4j.Logger;
-import org.apache.log4j.spi.LoggingEvent;
import org.apache.stratos.common.concurrent.locks.ReadWriteLock;
import org.apache.stratos.common.exception.InvalidLockRequestedException;
import org.junit.Test;
-import java.util.ArrayList;
import java.util.List;
import static junit.framework.TestCase.assertTrue;
@@ -37,31 +37,14 @@ import static org.junit.Assert.assertNotNull;
*/
public class ReadWriteLockTest {
- public static class TestAppender extends AppenderSkeleton {
-
- public List<LoggingEvent> getEvents() {
- return events;
- }
-
- private List<LoggingEvent> events = new ArrayList<LoggingEvent>();
-
- public void close() {}
-
- public boolean requiresLayout() {
- return false;
- }
-
- @Override
- protected void append(LoggingEvent event) {
- events.add(event);
- }
- }
-
+ private static final Log log = LogFactory.getLog(ReadWriteLockTest.class);
@Test
public void testInvalidLock() {
Exception exception = null;
try {
+ System.setProperty("read.write.lock.monitor.enabled", "true");
+
ReadWriteLock lock = new ReadWriteLock("lock-test");
lock.acquireReadLock();
lock.acquireWriteLock();
@@ -74,35 +57,24 @@ public class ReadWriteLockTest {
@Test
public void testUnreleasedLocks() {
- final TestAppender testAppender = new TestAppender();
- Logger.getRootLogger().addAppender(testAppender);
+ final TestLogAppender testLogAppender = new TestLogAppender();
+ Logger.getRootLogger().addAppender(testLogAppender);
+ Logger.getRootLogger().setLevel(Level.DEBUG);
System.setProperty("read.write.lock.monitor.enabled", "true");
System.setProperty("read.write.lock.monitor.interval", "2000");
- System.setProperty("read.write.lock.timeout", "2000");
-
- Runnable r1 = new Runnable() {
- @Override
- public void run() {
- ReadWriteLock lock = new ReadWriteLock("unrelased-lock-test");
- lock.acquireReadLock();
- sleep(5000);
- List<LoggingEvent> events = testAppender.getEvents();
- assertTrue(containsMessage(events, "System error, lock has not released"));
- }
- };
- Thread thread = new Thread(r1);
- thread.start();
+ System.setProperty("read.write.lock.timeout", "1500");
- try {
- thread.join();
- } catch (InterruptedException ignore) {
- }
+ ReadWriteLock lock = new ReadWriteLock("unrelased-lock-test");
+ lock.acquireReadLock();
+ sleep(5000);
+ List<String> messages = testLogAppender.getMessages();
+ assertTrue(containsMessage(messages, "System error, lock has not released"));
}
- private boolean containsMessage(List<LoggingEvent> events, String message) {
- for(LoggingEvent event : events) {
- if((event.getMessage() != null) && (event.getMessage().toString().contains(message))) {
+ private boolean containsMessage(List<String> messages, String text) {
+ for (String message : messages) {
+ if ((message != null) && (message.contains(text))) {
return true;
}
}
http://git-wip-us.apache.org/repos/asf/stratos/blob/2f0b9040/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/TestLogAppender.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/TestLogAppender.java b/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/TestLogAppender.java
new file mode 100644
index 0000000..4d74826
--- /dev/null
+++ b/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/TestLogAppender.java
@@ -0,0 +1,52 @@
+/*
+ * 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.stratos.common.test;
+
+import org.apache.log4j.AppenderSkeleton;
+import org.apache.log4j.spi.LoggingEvent;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class TestLogAppender extends AppenderSkeleton {
+
+ private List<String> messages;
+
+ public TestLogAppender() {
+ messages = new ArrayList<String>();
+ }
+
+ public List<String> getMessages() {
+ return new ArrayList<String>(messages);
+ }
+
+ public void close() {}
+
+ public boolean requiresLayout() {
+ return false;
+ }
+
+ @Override
+ protected void append(LoggingEvent event) {
+ if(event.getMessage() != null) {
+ messages.add(event.getMessage().toString());
+ }
+ }
+}