Repository: activemq
Updated Branches:
  refs/heads/activemq-5.15.x 8e9f80e7d -> eca72dc81


AMQ-6798 - Clean up store usage object on Queue stop

When queues are stopped the StoreUsage object needs to be stopped so it
will be removed from the parent StoreUsage.  This allows the object to
be garbage collected and prevents a memory leak.

(cherry picked from commit 35bd3ad938f5c378b17a34980d2f3480bf3bbfc3)


Project: http://git-wip-us.apache.org/repos/asf/activemq/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/eca72dc8
Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/eca72dc8
Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/eca72dc8

Branch: refs/heads/activemq-5.15.x
Commit: eca72dc81ad8113e31fadf161580bf5e2b93e27a
Parents: 8e9f80e
Author: Christopher L. Shannon (cshannon) <christopher.l.shan...@gmail.com>
Authored: Thu Aug 24 09:23:17 2017 -0400
Committer: Christopher L. Shannon (cshannon) <christopher.l.shan...@gmail.com>
Committed: Thu Aug 24 09:25:13 2017 -0400

----------------------------------------------------------------------
 .../apache/activemq/broker/region/Queue.java    |   3 +
 .../QueueMemoryAndStoreUsageCleanupTest.java    | 103 +++++++++++++++++++
 2 files changed, 106 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq/blob/eca72dc8/activemq-broker/src/main/java/org/apache/activemq/broker/region/Queue.java
----------------------------------------------------------------------
diff --git 
a/activemq-broker/src/main/java/org/apache/activemq/broker/region/Queue.java 
b/activemq-broker/src/main/java/org/apache/activemq/broker/region/Queue.java
index 612aeeb..49dd5a2 100644
--- a/activemq-broker/src/main/java/org/apache/activemq/broker/region/Queue.java
+++ b/activemq-broker/src/main/java/org/apache/activemq/broker/region/Queue.java
@@ -1026,6 +1026,9 @@ public class Queue extends BaseDestination implements 
Task, UsageListener, Index
             if (memoryUsage != null) {
                 memoryUsage.stop();
             }
+            if (systemUsage.getStoreUsage() != null) {
+                systemUsage.getStoreUsage().stop();
+            }
             if (store != null) {
                 store.stop();
             }

http://git-wip-us.apache.org/repos/asf/activemq/blob/eca72dc8/activemq-unit-tests/src/test/java/org/apache/activemq/usage/QueueMemoryAndStoreUsageCleanupTest.java
----------------------------------------------------------------------
diff --git 
a/activemq-unit-tests/src/test/java/org/apache/activemq/usage/QueueMemoryAndStoreUsageCleanupTest.java
 
b/activemq-unit-tests/src/test/java/org/apache/activemq/usage/QueueMemoryAndStoreUsageCleanupTest.java
new file mode 100644
index 0000000..3259f40
--- /dev/null
+++ 
b/activemq-unit-tests/src/test/java/org/apache/activemq/usage/QueueMemoryAndStoreUsageCleanupTest.java
@@ -0,0 +1,103 @@
+/**
+ * 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.usage;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.lang.reflect.Field;
+import java.util.List;
+
+import org.apache.activemq.broker.BrokerService;
+import org.apache.activemq.broker.region.Destination;
+import org.apache.activemq.command.ActiveMQDestination;
+import org.apache.activemq.command.ActiveMQQueue;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Test for AMQ-6798
+ */
+public class QueueMemoryAndStoreUsageCleanupTest {
+    protected static final Logger LOG = LoggerFactory
+            .getLogger(QueueMemoryAndStoreUsageCleanupTest.class);
+
+    @Rule
+    public TemporaryFolder dataFileDir = new TemporaryFolder(new 
File("target"));
+    private BrokerService broker;
+    private SystemUsage systemUsage;
+
+    @Before
+    public void setUpBroker() throws Exception {
+        broker = new BrokerService();
+        broker.setPersistent(true);
+        broker.setDataDirectoryFile(dataFileDir.getRoot());
+        broker.setDeleteAllMessagesOnStartup(true);
+        systemUsage = broker.getSystemUsage();
+        startBroker();
+    }
+
+    protected void startBroker() throws Exception {
+        broker.start();
+        broker.waitUntilStarted();
+    }
+
+    @After
+    public void stopBroker() throws Exception {
+        broker.stop();
+        broker.waitUntilStopped();
+    }
+
+    @Test(timeout=30000)
+    public void testQueueMemoryAndStoreUsageCleanup() throws Exception {
+        Field childrenField = Usage.class.getDeclaredField("children");
+        childrenField.setAccessible(true);
+        List<?> memoryUsageChildren = (List<?>) 
childrenField.get(systemUsage.getMemoryUsage());
+        List<?> storeUsageChildren = (List<?>) 
childrenField.get(systemUsage.getStoreUsage());
+
+        Destination queue1 = addDestination(new ActiveMQQueue("queue1"));
+        Destination queue2 = addDestination(new ActiveMQQueue("queue2"));
+        Destination queue3 = addDestination(new ActiveMQQueue("queue3"));
+        Destination queue4 = addDestination(new ActiveMQQueue("queue4"));
+
+        int beforeStopMemoryChildren = memoryUsageChildren.size();
+        int beforeStopStoreChildren = storeUsageChildren.size();
+
+        queue1.stop();
+        queue2.stop();
+        queue3.stop();
+        queue4.stop();
+
+        //Make sure each memory usage and store usage object that was created 
for every queue
+        //has been cleaned up
+        assertEquals(beforeStopMemoryChildren - 4, memoryUsageChildren.size());
+        assertEquals(beforeStopStoreChildren - 4, storeUsageChildren.size());
+    }
+
+    private Destination addDestination(ActiveMQDestination destination) throws 
Exception {
+        Destination dest = 
broker.getBroker().addDestination(broker.getAdminConnectionContext(),
+                destination, false);
+        dest.start();
+        return dest;
+    }
+}

Reply via email to