Skip to content

Commit

Permalink
[fix][broker] Fix the wrong behaviour when set `overrideBrokerNicSpee…
Browse files Browse the repository at this point in the history
…dGbps` (#18818)
  • Loading branch information
mattisonchao authored Dec 8, 2022
1 parent da87e40 commit 3e9994e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.apache.pulsar.broker.loadbalance.LinuxInfoUtils.getTotalNicUsage;
import static org.apache.pulsar.broker.loadbalance.LinuxInfoUtils.isCGroupEnabled;
import static org.apache.pulsar.common.util.Runnables.catchingAndLoggingThrowables;
import com.google.common.annotations.VisibleForTesting;
import com.sun.management.OperatingSystemMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;
Expand Down Expand Up @@ -121,10 +122,12 @@ public void calculateBrokerHostUsage() {
this.usage = usage;
}

private double getTotalNicLimitWithConfiguration(List<String> nics) {
@VisibleForTesting
double getTotalNicLimitWithConfiguration(List<String> nics) {
// Use the override value as configured. Return the total max speed across all available NICs, converted
// from Gbps into Kbps
return overrideBrokerNicSpeedGbps.map(BitRateUnit.Gigabit::toKilobit)
.map(speed -> speed * nics.size())
.orElseGet(() -> getTotalNicLimit(nics, BitRateUnit.Kilobit));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 lombok.Cleanup;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class LinuxBrokerHostUsageImplTest {

@Test
public void checkOverrideBrokerNicSpeedGbps() {
@Cleanup("shutdown")
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
LinuxBrokerHostUsageImpl linuxBrokerHostUsage =
new LinuxBrokerHostUsageImpl(1, Optional.of(3.0), executorService);
List<String> nics = new ArrayList<>();
nics.add("1");
nics.add("2");
nics.add("3");
double totalLimit = linuxBrokerHostUsage.getTotalNicLimitWithConfiguration(nics);
Assert.assertEquals(totalLimit, 3.0 * 1000 * 1000 * 3);
}
}

0 comments on commit 3e9994e

Please sign in to comment.