Repository: activemq Updated Branches: refs/heads/master c705523cd -> 89c75ca28
AMQ-5709 Reworking Logging on Locker https://issues.apache.org/jira/browse/AMQ-5709 This is about changing how the Log is done in case of not being able to lock (log only once) Project: http://git-wip-us.apache.org/repos/asf/activemq/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/89c75ca2 Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/89c75ca2 Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/89c75ca2 Branch: refs/heads/master Commit: 89c75ca28aae1e6a5b62f159350d29a5f7782e71 Parents: c705523 Author: Clebert Suconic <[email protected]> Authored: Tue Apr 7 15:48:42 2015 -0400 Committer: Clebert Suconic <[email protected]> Committed: Tue Apr 7 17:24:48 2015 -0400 ---------------------------------------------------------------------- .../apache/activemq/store/SharedFileLocker.java | 29 +++-- .../activemq/store/SharedFileLockerTest.java | 109 +++++++++++++++++++ 2 files changed, 129 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq/blob/89c75ca2/activemq-broker/src/main/java/org/apache/activemq/store/SharedFileLocker.java ---------------------------------------------------------------------- diff --git a/activemq-broker/src/main/java/org/apache/activemq/store/SharedFileLocker.java b/activemq-broker/src/main/java/org/apache/activemq/store/SharedFileLocker.java index e14eb03..a5432aa 100644 --- a/activemq-broker/src/main/java/org/apache/activemq/store/SharedFileLocker.java +++ b/activemq-broker/src/main/java/org/apache/activemq/store/SharedFileLocker.java @@ -16,15 +16,15 @@ */ package org.apache.activemq.store; +import java.io.File; +import java.io.IOException; + import org.apache.activemq.broker.AbstractLocker; import org.apache.activemq.util.LockFile; import org.apache.activemq.util.ServiceStopper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.IOException; - /** * Represents an exclusive lock on a database to avoid multiple brokers running * against the same logical database. @@ -48,19 +48,30 @@ public class SharedFileLocker extends AbstractLocker { if (failIfLocked) { lockFile.lock(); } else { + // Print a warning only once + boolean warned = false; boolean locked = false; while ((!isStopped()) && (!isStopping())) { try { lockFile.lock(); locked = keepAlive(); + LOG.info("locked " + locked); break; } catch (IOException e) { - LOG.info("Database " - + lockFileName - + " is locked... waiting " - + (lockAcquireSleepInterval / 1000) - + " seconds for the database to be unlocked. Reason: " - + e); + if (!warned) + { + LOG.info("Database " + + lockFileName + + " is locked by another server. This broker is now in slave mode waiting a lock to be acquired"); + warned = true; + } + + LOG.debug("Database " + + lockFileName + + " is locked... waiting " + + (lockAcquireSleepInterval / 1000) + + " seconds for the database to be unlocked. Reason: " + + e); try { Thread.sleep(lockAcquireSleepInterval); } catch (InterruptedException e1) { http://git-wip-us.apache.org/repos/asf/activemq/blob/89c75ca2/activemq-unit-tests/src/test/java/org/apache/activemq/store/SharedFileLockerTest.java ---------------------------------------------------------------------- diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/store/SharedFileLockerTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/store/SharedFileLockerTest.java new file mode 100644 index 0000000..d8700c0 --- /dev/null +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/store/SharedFileLockerTest.java @@ -0,0 +1,109 @@ +/** + * 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.activemq.store; + +import java.io.File; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class SharedFileLockerTest +{ + @Rule + public TemporaryFolder testFolder = new TemporaryFolder(); + + @Test + public void testLock() throws Exception + { + final AtomicInteger errors = new AtomicInteger(0); + + Thread thread = null; + + SharedFileLocker locker1 = new SharedFileLocker(); + locker1.setDirectory(testFolder.getRoot()); + + final SharedFileLocker locker2 = new SharedFileLocker(); + locker2.setLockAcquireSleepInterval(1); + locker2.setDirectory(testFolder.getRoot()); + + + try + { + locker1.doStart(); + + Assert.assertTrue(locker1.keepAlive()); + + Thread.sleep(10); + + thread = new Thread("Locker Thread") + { + public void run() + { + try + { + locker2.doStart(); + } + catch (Throwable e) + { + errors.incrementAndGet(); + } + } + }; + + thread.start(); + + // Waiting some small time here, you shouldn't see many messages + Thread.sleep(100); + + Assert.assertTrue(thread.isAlive()); + + locker1.stop(); + + // 10 seconds here is an eternity, but it should only take milliseconds + thread.join(5000); + + long timeout = System.currentTimeMillis() + 5000; + + while (timeout > System.currentTimeMillis() && !locker2.keepAlive()) + { + Thread.sleep(1); + } + + Assert.assertTrue(locker2.keepAlive()); + + locker2.stop(); + + } + finally + { + // to make sure we won't leak threads if the test ever failed for any reason + thread.join(1000); + if (thread.isAlive()) + { + thread.interrupt(); + } + + File lockFile = new File(testFolder.getRoot(), "lock"); + lockFile.delete(); + } + + } +}
