You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by ti...@apache.org on 2022/12/09 13:57:23 UTC

[pulsar] branch master updated: [fix][broker] PulsarResourceDescription.compareTo should return zero on exactly match (#11580)

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

tison pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 0d707aec925 [fix][broker] PulsarResourceDescription.compareTo should return zero on exactly match (#11580)
0d707aec925 is described below

commit 0d707aec92595868192380758a46f8f02886128f
Author: Lei Zhiyuan <le...@gmail.com>
AuthorDate: Fri Dec 9 21:57:15 2022 +0800

    [fix][broker] PulsarResourceDescription.compareTo should return zero on exactly match (#11580)
---
 .../impl/PulsarResourceDescription.java            | 16 +++++++++-
 .../impl/PulsarResourceDescriptionTest.java        | 35 ++++++++++++++++++++++
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/PulsarResourceDescription.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/PulsarResourceDescription.java
index 3a1e5bce48e..1f87dac8ec0 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/PulsarResourceDescription.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/PulsarResourceDescription.java
@@ -37,7 +37,11 @@ public class PulsarResourceDescription extends ResourceDescription {
         if (o.getResourceUsage().size() > resourceUsageByName.size()) {
             return -1;
         }
-        // TODO need to return zero if two resourceDescription match exactly
+
+        if (exactlyEquals(o.getResourceUsage())) {
+            return 0;
+        }
+
         for (Map.Entry<String, ResourceUsage> entry : o.getResourceUsage().entrySet()) {
             // if we don't have any entry which is in other but not in our set, we fail
             String resourceName = entry.getKey();
@@ -55,6 +59,16 @@ public class PulsarResourceDescription extends ResourceDescription {
         return 1;
     }
 
+
+    private boolean exactlyEquals(Map<String, ResourceUsage> o) {
+        if (this.resourceUsageByName.size() != o.size()) {
+            return false;
+        }
+        return this.resourceUsageByName.entrySet().stream()
+                .allMatch(e -> e.getValue().equals(o.get(e.getKey())));
+    }
+
+
     @Override
     public void removeUsage(ResourceDescription rd) {
         // TODO Auto-generated method stub
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/impl/PulsarResourceDescriptionTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/impl/PulsarResourceDescriptionTest.java
new file mode 100644
index 00000000000..c39c5123110
--- /dev/null
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/impl/PulsarResourceDescriptionTest.java
@@ -0,0 +1,35 @@
+/*
+ * 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.loadbalance.impl;
+
+import static org.testng.Assert.assertEquals;
+import org.apache.pulsar.policies.data.loadbalancer.ResourceUsage;
+import org.testng.annotations.Test;
+
+@Test(groups = "broker")
+public class PulsarResourceDescriptionTest {
+    @Test
+    public void compareTo() {
+        PulsarResourceDescription one = new PulsarResourceDescription();
+        one.put("cpu", new ResourceUsage(0.1, 0.2));
+        PulsarResourceDescription two = new PulsarResourceDescription();
+        two.put("cpu", new ResourceUsage(0.1, 0.2));
+        assertEquals(0, one.compareTo(two));
+    }
+}