You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by cs...@apache.org on 2017/08/24 13:24:52 UTC

activemq git commit: AMQ-6798 - Clean up store usage object on Queue stop

Repository: activemq
Updated Branches:
  refs/heads/master c7291f1ec -> 35bd3ad93


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.


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

Branch: refs/heads/master
Commit: 35bd3ad938f5c378b17a34980d2f3480bf3bbfc3
Parents: c7291f1
Author: Christopher L. Shannon (cshannon) <ch...@gmail.com>
Authored: Thu Aug 24 09:23:17 2017 -0400
Committer: Christopher L. Shannon (cshannon) <ch...@gmail.com>
Committed: Thu Aug 24 09:24:36 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/35bd3ad9/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/35bd3ad9/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;
+    }
+}