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/03/31 13:07:47 UTC

activemq git commit: AMQ-6642: Fix potential NPE on updateMessage

Repository: activemq
Updated Branches:
  refs/heads/master 4ef1fc74c -> 2731f04f1


AMQ-6642: Fix potential NPE on updateMessage

Fixing potential NPE when calling updateMessage on a Queue store in
KahaDB if subscription statistics are enabled.  Also reduced the
visibily from public to protected to subscription statistic related
methods that shouldn't be public and added null pointer checks there as
well.

https://issues.apache.org/jira/browse/AMQ-6642


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

Branch: refs/heads/master
Commit: 2731f04f1ca81312d08ffc21c0ceb09513165b7c
Parents: 4ef1fc7
Author: Christopher L. Shannon (cshannon) <ch...@gmail.com>
Authored: Fri Mar 31 09:06:01 2017 -0400
Committer: Christopher L. Shannon (cshannon) <ch...@gmail.com>
Committed: Fri Mar 31 09:06:01 2017 -0400

----------------------------------------------------------------------
 .../activemq/store/kahadb/MessageDatabase.java  | 49 +++++++++++---------
 .../store/kahadb/MessageDatabaseSizeTest.java   | 41 ++++++++++++++++
 2 files changed, 68 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq/blob/2731f04f/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java
----------------------------------------------------------------------
diff --git a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java
index ea53ad0..0611be7 100644
--- a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java
+++ b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MessageDatabase.java
@@ -1524,7 +1524,7 @@ public abstract class MessageDatabase extends ServiceSupport implements BrokerSe
                 decrementAndSubSizeToStoreStat(command.getDestination(), previousKeys.location.getSize());
 
                 //update all the subscription metrics
-                if (enableSubscriptionStatistics && location.getSize() != previousKeys.location.getSize()) {
+                if (enableSubscriptionStatistics && sd.ackPositions != null && location.getSize() != previousKeys.location.getSize()) {
                     Iterator<Entry<String, SequenceSet>> iter = sd.ackPositions.iterator(tx);
                     while (iter.hasNext()) {
                         Entry<String, SequenceSet> e = iter.next();
@@ -2970,33 +2970,38 @@ public abstract class MessageDatabase extends ServiceSupport implements BrokerSe
         return sd.subscriptionAcks.get(tx, subscriptionKey);
     }
 
-    public long getStoredMessageCount(Transaction tx, StoredDestination sd, String subscriptionKey) throws IOException {
-        SequenceSet messageSequences = sd.ackPositions.get(tx, subscriptionKey);
-        if (messageSequences != null) {
-            long result = messageSequences.rangeSize();
-            // if there's anything in the range the last value is always the nextMessage marker, so remove 1.
-            return result > 0 ? result - 1 : 0;
+    protected long getStoredMessageCount(Transaction tx, StoredDestination sd, String subscriptionKey) throws IOException {
+        if (sd.ackPositions != null) {
+            SequenceSet messageSequences = sd.ackPositions.get(tx, subscriptionKey);
+            if (messageSequences != null) {
+                long result = messageSequences.rangeSize();
+                // if there's anything in the range the last value is always the nextMessage marker, so remove 1.
+                return result > 0 ? result - 1 : 0;
+            }
         }
 
         return 0;
     }
 
-    public long getStoredMessageSize(Transaction tx, StoredDestination sd, String subscriptionKey) throws IOException {
-        //grab the messages attached to this subscription
-        SequenceSet messageSequences = sd.ackPositions.get(tx, subscriptionKey);
-
+    protected long getStoredMessageSize(Transaction tx, StoredDestination sd, String subscriptionKey) throws IOException {
         long locationSize = 0;
-        if (messageSequences != null) {
-            Sequence head = messageSequences.getHead();
-            if (head != null) {
-                //get an iterator over the order index starting at the first unacked message
-                //and go over each message to add up the size
-                Iterator<Entry<Long, MessageKeys>> iterator = sd.orderIndex.iterator(tx,
-                        new MessageOrderCursor(head.getFirst()));
-
-                while (iterator.hasNext()) {
-                    Entry<Long, MessageKeys> entry = iterator.next();
-                    locationSize += entry.getValue().location.getSize();
+
+        if (sd.ackPositions != null) {
+            //grab the messages attached to this subscription
+            SequenceSet messageSequences = sd.ackPositions.get(tx, subscriptionKey);
+
+            if (messageSequences != null) {
+                Sequence head = messageSequences.getHead();
+                if (head != null) {
+                    //get an iterator over the order index starting at the first unacked message
+                    //and go over each message to add up the size
+                    Iterator<Entry<Long, MessageKeys>> iterator = sd.orderIndex.iterator(tx,
+                            new MessageOrderCursor(head.getFirst()));
+
+                    while (iterator.hasNext()) {
+                        Entry<Long, MessageKeys> entry = iterator.next();
+                        locationSize += entry.getValue().location.getSize();
+                    }
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/activemq/blob/2731f04f/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/MessageDatabaseSizeTest.java
----------------------------------------------------------------------
diff --git a/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/MessageDatabaseSizeTest.java b/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/MessageDatabaseSizeTest.java
index 357dc5f..4deb1e0 100644
--- a/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/MessageDatabaseSizeTest.java
+++ b/activemq-kahadb-store/src/test/java/org/apache/activemq/store/kahadb/MessageDatabaseSizeTest.java
@@ -17,9 +17,12 @@
 package org.apache.activemq.store.kahadb;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
 
 import org.apache.activemq.broker.BrokerService;
 import org.apache.activemq.command.ActiveMQQueue;
@@ -36,13 +39,27 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@RunWith(Parameterized.class)
 public class MessageDatabaseSizeTest {
 
     private static final Logger LOG = LoggerFactory.getLogger(MessageDatabaseSizeTest.class);
 
+    @Parameters(name = "subStatsEnabled={0}")
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][] {
+                // Subscription stats on
+                {true},
+                // Subscription stats off
+                {false}
+        });
+    }
+
     @Rule
     public TemporaryFolder dataDir = new TemporaryFolder(new File("target"));
     private final String payload = new String(new byte[1024]);
@@ -50,6 +67,13 @@ public class MessageDatabaseSizeTest {
     private BrokerService broker = null;
     private final ActiveMQQueue destination = new ActiveMQQueue("Test");
     private KahaDBPersistenceAdapter adapter;
+    private boolean subStatsEnabled;
+
+    public MessageDatabaseSizeTest(boolean subStatsEnabled) {
+        super();
+        this.subStatsEnabled = subStatsEnabled;
+    }
+
 
     protected void startBroker() throws Exception {
         broker = new BrokerService();
@@ -58,6 +82,7 @@ public class MessageDatabaseSizeTest {
         broker.setUseJmx(true);
         broker.setDataDirectory(dataDir.getRoot().getAbsolutePath());
         adapter = (KahaDBPersistenceAdapter) broker.getPersistenceAdapter();
+        adapter.setEnableSubscriptionStatistics(subStatsEnabled);
         broker.start();
         LOG.info("Starting broker..");
     }
@@ -101,6 +126,22 @@ public class MessageDatabaseSizeTest {
         assertEquals(existingSize, messageStore.getMessageSize());
     }
 
+    @Test
+    public void testUpdateMessageSameLocationDifferentSize() throws Exception {
+        final KahaDBStore store = adapter.getStore();
+        MessageId messageId = new MessageId("111:222:333");
+        ActiveMQTextMessage textMessage = getMessage(new MessageId("111:222:333"));
+
+        //Add a single message and update once so we can compare the size consistently
+        MessageStore messageStore = store.createQueueMessageStore(destination);
+        messageStore.addMessage(broker.getAdminConnectionContext(), textMessage);
+        textMessage.setText("new size of message");
+        messageStore.updateMessage(textMessage);
+
+        assertNotNull(findMessageLocation(messageId.toString(), store.convert(destination)));
+
+    }
+
     /**
      * Test that when updating an existing message to a different location in the
      * journal that the index size doesn't change