You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by yu...@apache.org on 2017/02/10 09:55:32 UTC

incubator-rocketmq git commit: [ROCKETMQ-74] Fix DataVersion equals defect, closes apache/incubator-rocketmq#50

Repository: incubator-rocketmq
Updated Branches:
  refs/heads/master 44ef40ab3 -> 767775838


[ROCKETMQ-74] Fix DataVersion equals defect, closes apache/incubator-rocketmq#50


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

Branch: refs/heads/master
Commit: 767775838fbbf556e6ebc899f4ccab7f148d7aae
Parents: 44ef40a
Author: lizhanhui <li...@gmail.com>
Authored: Fri Feb 10 17:55:04 2017 +0800
Committer: yukon <yu...@apache.org>
Committed: Fri Feb 10 17:55:04 2017 +0800

----------------------------------------------------------------------
 .../org/apache/rocketmq/common/DataVersion.java | 30 ++++++---
 .../apache/rocketmq/common/DataVersionTest.java | 70 ++++++++++++++++++++
 .../processor/DefaultRequestProcessor.java      |  4 +-
 3 files changed, 91 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/76777583/common/src/main/java/org/apache/rocketmq/common/DataVersion.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/rocketmq/common/DataVersion.java b/common/src/main/java/org/apache/rocketmq/common/DataVersion.java
index 0f42e3f..e47a9b3 100644
--- a/common/src/main/java/org/apache/rocketmq/common/DataVersion.java
+++ b/common/src/main/java/org/apache/rocketmq/common/DataVersion.java
@@ -20,25 +20,25 @@ import java.util.concurrent.atomic.AtomicLong;
 import org.apache.rocketmq.remoting.protocol.RemotingSerializable;
 
 public class DataVersion extends RemotingSerializable {
-    private long timestatmp = System.currentTimeMillis();
+    private long timestamp = System.currentTimeMillis();
     private AtomicLong counter = new AtomicLong(0);
 
     public void assignNewOne(final DataVersion dataVersion) {
-        this.timestatmp = dataVersion.timestatmp;
+        this.timestamp = dataVersion.timestamp;
         this.counter.set(dataVersion.counter.get());
     }
 
     public void nextVersion() {
-        this.timestatmp = System.currentTimeMillis();
+        this.timestamp = System.currentTimeMillis();
         this.counter.incrementAndGet();
     }
 
-    public long getTimestatmp() {
-        return timestatmp;
+    public long getTimestamp() {
+        return timestamp;
     }
 
-    public void setTimestatmp(long timestatmp) {
-        this.timestatmp = timestatmp;
+    public void setTimestamp(long timestamp) {
+        this.timestamp = timestamp;
     }
 
     public AtomicLong getCounter() {
@@ -58,16 +58,24 @@ public class DataVersion extends RemotingSerializable {
 
         final DataVersion that = (DataVersion) o;
 
-        if (timestatmp != that.timestatmp)
+        if (timestamp != that.timestamp) {
             return false;
-        return counter != null ? counter.equals(that.counter) : that.counter == null;
+        }
 
+        if (counter != null && that.counter != null) {
+            return counter.longValue() == that.counter.longValue();
+        }
+
+        return (null == counter) && (null == that.counter);
     }
 
     @Override
     public int hashCode() {
-        int result = (int) (timestatmp ^ (timestatmp >>> 32));
-        result = 31 * result + (counter != null ? counter.hashCode() : 0);
+        int result = (int) (timestamp ^ (timestamp >>> 32));
+        if (null != counter) {
+            long l = counter.get();
+            result = 31 * result + (int)(l ^ (l >>> 32));
+        }
         return result;
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/76777583/common/src/test/java/org/apache/rocketmq/common/DataVersionTest.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/rocketmq/common/DataVersionTest.java b/common/src/test/java/org/apache/rocketmq/common/DataVersionTest.java
new file mode 100644
index 0000000..f4d14e5
--- /dev/null
+++ b/common/src/test/java/org/apache/rocketmq/common/DataVersionTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.rocketmq.common;
+
+import java.util.concurrent.atomic.AtomicLong;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DataVersionTest {
+
+    @Test
+    public void testEquals() {
+        DataVersion dataVersion = new DataVersion();
+        DataVersion other = new DataVersion();
+        other.setTimestamp(dataVersion.getTimestamp());
+        Assert.assertTrue(dataVersion.equals(other));
+    }
+
+    @Test
+    public void testEquals_falseWhenCounterDifferent() {
+        DataVersion dataVersion = new DataVersion();
+        DataVersion other = new DataVersion();
+        other.setCounter(new AtomicLong(1L));
+        other.setTimestamp(dataVersion.getTimestamp());
+        Assert.assertFalse(dataVersion.equals(other));
+    }
+
+    @Test
+    public void testEquals_falseWhenCounterDifferent2() {
+        DataVersion dataVersion = new DataVersion();
+        DataVersion other = new DataVersion();
+        other.setCounter(null);
+        other.setTimestamp(dataVersion.getTimestamp());
+        Assert.assertFalse(dataVersion.equals(other));
+    }
+
+    @Test
+    public void testEquals_falseWhenCounterDifferent3() {
+        DataVersion dataVersion = new DataVersion();
+        dataVersion.setCounter(null);
+        DataVersion other = new DataVersion();
+        other.setTimestamp(dataVersion.getTimestamp());
+        Assert.assertFalse(dataVersion.equals(other));
+    }
+
+    @Test
+    public void testEquals_trueWhenCountersBothNull() {
+        DataVersion dataVersion = new DataVersion();
+        dataVersion.setCounter(null);
+        DataVersion other = new DataVersion();
+        other.setCounter(null);
+        other.setTimestamp(dataVersion.getTimestamp());
+        Assert.assertTrue(dataVersion.equals(other));
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/76777583/namesrv/src/main/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessor.java
----------------------------------------------------------------------
diff --git a/namesrv/src/main/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessor.java b/namesrv/src/main/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessor.java
index b6db7e0..9647684 100644
--- a/namesrv/src/main/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessor.java
+++ b/namesrv/src/main/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessor.java
@@ -191,7 +191,7 @@ public class DefaultRequestProcessor implements NettyRequestProcessor {
             registerBrokerBody = RegisterBrokerBody.decode(request.getBody(), RegisterBrokerBody.class);
         } else {
             registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setCounter(new AtomicLong(0));
-            registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setTimestatmp(0);
+            registerBrokerBody.getTopicConfigSerializeWrapper().getDataVersion().setTimestamp(0);
         }
 
         RegisterBrokerResult result = this.namesrvController.getRouteInfoManager().registerBroker(
@@ -227,7 +227,7 @@ public class DefaultRequestProcessor implements NettyRequestProcessor {
         } else {
             topicConfigWrapper = new TopicConfigSerializeWrapper();
             topicConfigWrapper.getDataVersion().setCounter(new AtomicLong(0));
-            topicConfigWrapper.getDataVersion().setTimestatmp(0);
+            topicConfigWrapper.getDataVersion().setTimestamp(0);
         }
 
         RegisterBrokerResult result = this.namesrvController.getRouteInfoManager().registerBroker(