You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by lh...@apache.org on 2022/04/27 06:46:38 UTC
[pulsar] branch master updated: [fix][broker] Fix broker LoadBalance uneffective (#15314)
This is an automated email from the ASF dual-hosted git repository.
lhotari 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 3711f7a101b [fix][broker] Fix broker LoadBalance uneffective (#15314)
3711f7a101b is described below
commit 3711f7a101bd9e2f6c29c99def4e1a8b4e6dd069
Author: Tian Luo <fu...@outlook.com>
AuthorDate: Wed Apr 27 14:46:31 2022 +0800
[fix][broker] Fix broker LoadBalance uneffective (#15314)
* fix ResourceUsage percentUsage is always 0 when construct by json serialize
* Update pulsar-common/src/test/java/org/apache/pulsar/policies/data/loadbalancer/LocalBrokerDataTest.java
Co-authored-by: Michael Marshall <mi...@gmail.com>
---
.../policies/data/loadbalancer/ResourceUsage.java | 13 +++-----
.../data/loadbalancer/LocalBrokerDataTest.java | 36 ++++++++++++++++++++++
2 files changed, 41 insertions(+), 8 deletions(-)
diff --git a/pulsar-client-admin-api/src/main/java/org/apache/pulsar/policies/data/loadbalancer/ResourceUsage.java b/pulsar-client-admin-api/src/main/java/org/apache/pulsar/policies/data/loadbalancer/ResourceUsage.java
index 55093d3a320..28621e12757 100644
--- a/pulsar-client-admin-api/src/main/java/org/apache/pulsar/policies/data/loadbalancer/ResourceUsage.java
+++ b/pulsar-client-admin-api/src/main/java/org/apache/pulsar/policies/data/loadbalancer/ResourceUsage.java
@@ -27,17 +27,10 @@ import lombok.EqualsAndHashCode;
public class ResourceUsage {
public final double usage;
public final double limit;
- @EqualsAndHashCode.Exclude
- private final float percentUsage;
public ResourceUsage(double usage, double limit) {
this.usage = usage;
this.limit = limit;
- float proportion = 0;
- if (limit > 0) {
- proportion = ((float) usage) / ((float) limit);
- }
- percentUsage = proportion * 100;
}
public ResourceUsage() {
@@ -57,6 +50,10 @@ public class ResourceUsage {
}
public float percentUsage() {
- return percentUsage;
+ float proportion = 0;
+ if (limit > 0) {
+ proportion = ((float) usage) / ((float) limit);
+ }
+ return proportion * 100;
}
}
diff --git a/pulsar-common/src/test/java/org/apache/pulsar/policies/data/loadbalancer/LocalBrokerDataTest.java b/pulsar-common/src/test/java/org/apache/pulsar/policies/data/loadbalancer/LocalBrokerDataTest.java
new file mode 100644
index 00000000000..e1f7222133a
--- /dev/null
+++ b/pulsar-common/src/test/java/org/apache/pulsar/policies/data/loadbalancer/LocalBrokerDataTest.java
@@ -0,0 +1,36 @@
+/**
+ * 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.policies.data.loadbalancer;
+
+import com.google.gson.Gson;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class LocalBrokerDataTest {
+
+ @Test
+ public void testLocalBrokerDataDeserialization() {
+ String data = "{\"webServiceUrl\":\"http://10.244.2.23:8080\",\"webServiceUrlTls\":\"https://10.244.2.23:8081\",\"pulsarServiceUrlTls\":\"pulsar+ssl://10.244.2.23:6651\",\"persistentTopicsEnabled\":true,\"nonPersistentTopicsEnabled\":false,\"cpu\":{\"usage\":3.1577712104798255,\"limit\":100.0},\"memory\":{\"usage\":614.0,\"limit\":1228.0},\"directMemory\":{\"usage\":32.0,\"limit\":1228.0},\"bandwidthIn\":{\"usage\":0.0,\"limit\":0.0},\"bandwidthOut\":{\"usage\":0.0,\"limit\":0.0} [...]
+ Gson gson = new Gson();
+ LocalBrokerData localBrokerData = gson.fromJson(data, LocalBrokerData.class);
+ Assert.assertEquals(localBrokerData.getMemory().limit, 1228.0d, 0.0001f);
+ Assert.assertEquals(localBrokerData.getMemory().usage, 614.0d, 0.0001f);
+ Assert.assertEquals(localBrokerData.getMemory().percentUsage(), ((float) localBrokerData.getMemory().usage) / ((float) localBrokerData.getMemory().limit) * 100, 0.0001f);
+ }
+}