You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2022/04/10 16:11:12 UTC

[pulsar] branch branch-2.10 updated: Ensure NamespaceEphemeralData has equals() operator (#15092)

This is an automated email from the ASF dual-hosted git repository.

mmerli pushed a commit to branch branch-2.10
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/branch-2.10 by this push:
     new 8d895cc7c2c Ensure NamespaceEphemeralData has equals() operator (#15092)
8d895cc7c2c is described below

commit 8d895cc7c2c76f1e1c111a34a1b8a1e6af2b0242
Author: Matteo Merli <mm...@apache.org>
AuthorDate: Sat Apr 9 01:40:19 2022 -0700

    Ensure NamespaceEphemeralData has equals() operator (#15092)
---
 .../broker/namespace/NamespaceEphemeralData.java   |  8 +--
 .../namespace/NamespaceEphemeralDataTest.java      | 62 ++++++++++++++++++++++
 .../data/loadbalancer/AdvertisedListener.java      | 14 +----
 3 files changed, 66 insertions(+), 18 deletions(-)

diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceEphemeralData.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceEphemeralData.java
index e6acbaf6582..2f1262f5bf2 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceEphemeralData.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceEphemeralData.java
@@ -22,16 +22,12 @@ import com.google.common.collect.Maps;
 import java.util.Collections;
 import java.util.Map;
 import javax.validation.constraints.NotNull;
-import lombok.EqualsAndHashCode;
-import lombok.Getter;
+import lombok.Data;
 import lombok.NoArgsConstructor;
-import lombok.ToString;
 import org.apache.pulsar.policies.data.loadbalancer.AdvertisedListener;
 
-@Getter
+@Data
 @NoArgsConstructor
-@EqualsAndHashCode
-@ToString
 public class NamespaceEphemeralData {
     private String nativeUrl;
     private String nativeUrlTls;
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/namespace/NamespaceEphemeralDataTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/namespace/NamespaceEphemeralDataTest.java
new file mode 100644
index 00000000000..1fe1ff94805
--- /dev/null
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/namespace/NamespaceEphemeralDataTest.java
@@ -0,0 +1,62 @@
+/**
+ * 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.pulsar.broker.namespace;
+
+import static org.testng.Assert.assertEquals;
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.pulsar.policies.data.loadbalancer.AdvertisedListener;
+import org.testng.annotations.Test;
+
+@Test(groups = "broker")
+public class NamespaceEphemeralDataTest {
+
+    private static void setFields(NamespaceEphemeralData ned) throws Exception {
+        ned.setNativeUrl("pulsar://localhost:6650");
+        ned.setNativeUrlTls("pulsar+ssl://localhost:6651");
+        ned.setHttpUrl("http://localhost:8080");
+        ned.setHttpUrlTls("https://localhost:8443");
+        ned.setDisabled(false);
+
+        Map<String, AdvertisedListener> advertisedListeners = new HashMap<>();
+        advertisedListeners.put("test-listener", AdvertisedListener.builder()
+                .brokerServiceUrl(new URI("pulsar://adv-addr:6650"))
+                .brokerServiceUrlTls(new URI("pulsar+ssl://adv-addr:6651"))
+                .build());
+        ned.setAdvertisedListeners(advertisedListeners);
+    }
+
+    /**
+     * We must ensure NamespaceEphemeralData respect the equals() properties because it's used as a resource lock,
+     * where equality is checked in the revalidation phase.
+     */
+    @Test
+    public void testEquals() throws Exception {
+        NamespaceEphemeralData ned1 = new NamespaceEphemeralData();
+        setFields(ned1);
+
+        NamespaceEphemeralData ned2 = new NamespaceEphemeralData();
+        setFields(ned2);
+
+        assertEquals(ned1.hashCode(), ned2.hashCode());
+        assertEquals(ned1, ned2);
+    }
+
+}
diff --git a/pulsar-common/src/main/java/org/apache/pulsar/policies/data/loadbalancer/AdvertisedListener.java b/pulsar-common/src/main/java/org/apache/pulsar/policies/data/loadbalancer/AdvertisedListener.java
index b73fdab4483..6caaafb9462 100644
--- a/pulsar-common/src/main/java/org/apache/pulsar/policies/data/loadbalancer/AdvertisedListener.java
+++ b/pulsar-common/src/main/java/org/apache/pulsar/policies/data/loadbalancer/AdvertisedListener.java
@@ -21,10 +21,8 @@ package org.apache.pulsar.policies.data.loadbalancer;
 import java.net.URI;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
-import lombok.Getter;
+import lombok.Data;
 import lombok.NoArgsConstructor;
-import lombok.Setter;
-import lombok.ToString;
 
 /**
  * The advertisedListener for broker with brokerServiceUrl and brokerServiceUrlTls.
@@ -32,27 +30,19 @@ import lombok.ToString;
 @Builder
 @NoArgsConstructor
 @AllArgsConstructor
-@ToString
+@Data
 public class AdvertisedListener {
     //
-    @Getter
-    @Setter
     // the broker service uri without ssl
     private URI brokerServiceUrl;
     //
-    @Getter
-    @Setter
     // the broker service uri with ssl
     private URI brokerServiceUrlTls;
 
     //
-    @Getter
-    @Setter
     // the broker service uri without ssl
     private URI brokerHttpUrl;
     //
-    @Getter
-    @Setter
     // the broker service uri with ssl
     private URI brokerHttpsUrl;