Skip to content

Commit

Permalink
[fix][broker] Fix broker LoadBalance uneffective (#15314)
Browse files Browse the repository at this point in the history
* 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 <mikemarsh17@gmail.com>
(cherry picked from commit 3711f7a)
  • Loading branch information
fu-turer authored and lhotari committed Apr 27, 2022
1 parent b125c61 commit 09819b2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
Expand Up @@ -27,17 +27,10 @@
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() {
Expand All @@ -57,6 +50,10 @@ public int compareTo(ResourceUsage o) {
}

public float percentUsage() {
return percentUsage;
float proportion = 0;
if (limit > 0) {
proportion = ((float) usage) / ((float) limit);
}
return proportion * 100;
}
}
@@ -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},\"msgThroughputIn\":0.0,\"msgThroughputOut\":0.0,\"msgRateIn\":0.0,\"msgRateOut\":0.0,\"lastUpdate\":1650886425227,\"lastStats\":{\"pulsar/pulsar/10.244.2.23:8080/0x00000000_0xffffffff\":{\"msgRateIn\":0.0,\"msgThroughputIn\":0.0,\"msgRateOut\":0.0,\"msgThroughputOut\":0.0,\"consumerCount\":0,\"producerCount\":0,\"topics\":1,\"cacheSize\":0}},\"numTopics\":1,\"numBundles\":1,\"numConsumers\":0,\"numProducers\":0,\"bundles\":[\"pulsar/pulsar/10.244.2.23:8080/0x00000000_0xffffffff\"],\"lastBundleGains\":[],\"lastBundleLosses\":[],\"brokerVersionString\":\"2.11.0-hw-0.0.4-SNAPSHOT\",\"protocols\":{},\"advertisedListeners\":{},\"bundleStats\":{\"pulsar/pulsar/10.244.2.23:8080/0x00000000_0xffffffff\":{\"msgRateIn\":0.0,\"msgThroughputIn\":0.0,\"msgRateOut\":0.0,\"msgThroughputOut\":0.0,\"consumerCount\":0,\"producerCount\":0,\"topics\":1,\"cacheSize\":0}},\"maxResourceUsage\":0.49645519256591797,\"loadReportType\":\"LocalBrokerData\"}";
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);
}
}

0 comments on commit 09819b2

Please sign in to comment.