Search in sources :

Example 6 with LocalBrokerData

use of org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData in project incubator-pulsar by apache.

the class BrokerVersionFilterTest method initLoadData.

private LoadData initLoadData() {
    LocalBrokerData broker1Data = new LocalBrokerData();
    broker1Data.setBrokerVersionString("1.1.0-SNAPSHOT");
    LocalBrokerData broker2Data = new LocalBrokerData();
    broker2Data.setBrokerVersionString("1.1.0-SNAPSHOT");
    LocalBrokerData broker3Data = new LocalBrokerData();
    broker3Data.setBrokerVersionString("1.2.0-SNAPSHOT");
    LoadData loadData = new LoadData();
    loadData.getBrokerData().put("broker1", new BrokerData(broker1Data));
    loadData.getBrokerData().put("broker2", new BrokerData(broker2Data));
    loadData.getBrokerData().put("broker3", new BrokerData(broker3Data));
    return loadData;
}
Also used : LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData) LoadData(org.apache.pulsar.broker.loadbalance.LoadData) BrokerData(org.apache.pulsar.broker.BrokerData) LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData)

Example 7 with LocalBrokerData

use of org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData in project incubator-pulsar by apache.

the class ModularLoadManagerImplTest method testLoadShedding.

// Test that load shedding works
@Test
public void testLoadShedding() throws Exception {
    final NamespaceBundleStats stats1 = new NamespaceBundleStats();
    final NamespaceBundleStats stats2 = new NamespaceBundleStats();
    stats1.msgRateIn = 100;
    stats2.msgRateIn = 200;
    final Map<String, NamespaceBundleStats> statsMap = new ConcurrentHashMap<>();
    statsMap.put(mockBundleName(1), stats1);
    statsMap.put(mockBundleName(2), stats2);
    final LocalBrokerData localBrokerData = new LocalBrokerData();
    localBrokerData.update(new SystemResourceUsage(), statsMap);
    final Namespaces namespacesSpy1 = spy(pulsar1.getAdminClient().namespaces());
    AtomicReference<String> bundleReference = new AtomicReference<>();
    doAnswer(invocation -> {
        bundleReference.set(invocation.getArguments()[0].toString() + '/' + invocation.getArguments()[1]);
        return null;
    }).when(namespacesSpy1).unloadNamespaceBundle(Mockito.anyString(), Mockito.anyString());
    setField(pulsar1.getAdminClient(), "namespaces", namespacesSpy1);
    pulsar1.getConfiguration().setLoadBalancerEnabled(true);
    final LoadData loadData = (LoadData) getField(primaryLoadManager, "loadData");
    final Map<String, BrokerData> brokerDataMap = loadData.getBrokerData();
    final BrokerData brokerDataSpy1 = spy(brokerDataMap.get(primaryHost));
    when(brokerDataSpy1.getLocalData()).thenReturn(localBrokerData);
    brokerDataMap.put(primaryHost, brokerDataSpy1);
    // Need to update all the bundle data for the shedder to see the spy.
    primaryLoadManager.onUpdate(null, null, null);
    Thread.sleep(100);
    localBrokerData.setCpu(new ResourceUsage(80, 100));
    primaryLoadManager.doLoadShedding();
    // 80% is below overload threshold: verify nothing is unloaded.
    verify(namespacesSpy1, Mockito.times(0)).unloadNamespaceBundle(Mockito.anyString(), Mockito.anyString());
    localBrokerData.getCpu().usage = 90;
    primaryLoadManager.doLoadShedding();
    // Most expensive bundle will be unloaded.
    verify(namespacesSpy1, Mockito.times(1)).unloadNamespaceBundle(Mockito.anyString(), Mockito.anyString());
    assertEquals(bundleReference.get(), mockBundleName(2));
    primaryLoadManager.doLoadShedding();
    // Now less expensive bundle will be unloaded (normally other bundle would move off and nothing would be
    // unloaded, but this is not the case due to the spy's behavior).
    verify(namespacesSpy1, Mockito.times(2)).unloadNamespaceBundle(Mockito.anyString(), Mockito.anyString());
    assertEquals(bundleReference.get(), mockBundleName(1));
    primaryLoadManager.doLoadShedding();
    // Now both are in grace period: neither should be unloaded.
    verify(namespacesSpy1, Mockito.times(2)).unloadNamespaceBundle(Mockito.anyString(), Mockito.anyString());
}
Also used : Namespaces(org.apache.pulsar.client.admin.Namespaces) LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData) BrokerData(org.apache.pulsar.broker.BrokerData) LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData) SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage) ResourceUsage(org.apache.pulsar.policies.data.loadbalancer.ResourceUsage) SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage) AtomicReference(java.util.concurrent.atomic.AtomicReference) NamespaceBundleStats(org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test)

Example 8 with LocalBrokerData

use of org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData in project incubator-pulsar by apache.

the class ModularLoadManagerImplTest method testNeedBrokerDataUpdate.

// Test that ModularLoadManagerImpl will determine that writing local data to ZooKeeper is necessary if certain
// metrics change by a percentage threshold.
@Test
public void testNeedBrokerDataUpdate() throws Exception {
    final LocalBrokerData lastData = new LocalBrokerData();
    final LocalBrokerData currentData = new LocalBrokerData();
    final ServiceConfiguration conf = pulsar1.getConfiguration();
    // Set this manually in case the default changes.
    conf.setLoadBalancerReportUpdateThresholdPercentage(5);
    // Easier to test using an uninitialized ModularLoadManagerImpl.
    final ModularLoadManagerImpl loadManager = new ModularLoadManagerImpl();
    setField(loadManager, "lastData", lastData);
    setField(loadManager, "localData", currentData);
    setField(loadManager, "conf", conf);
    Supplier<Boolean> needUpdate = () -> {
        try {
            return (Boolean) invokeSimpleMethod(loadManager, "needBrokerDataUpdate");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };
    lastData.setMsgRateIn(100);
    currentData.setMsgRateIn(104);
    // 4% difference: shouldn't trigger an update.
    assert (!needUpdate.get());
    currentData.setMsgRateIn(105.1);
    // 5% difference: should trigger an update (exactly 5% is flaky due to precision).
    assert (needUpdate.get());
    // Do similar tests for lower values.
    currentData.setMsgRateIn(94);
    assert (needUpdate.get());
    currentData.setMsgRateIn(95.1);
    assert (!needUpdate.get());
    // 0 to non-zero should always trigger an update.
    lastData.setMsgRateIn(0);
    currentData.setMsgRateIn(1e-8);
    assert (needUpdate.get());
    // non-zero to zero should trigger an update as long as the threshold is less than 100.
    lastData.setMsgRateIn(1e-8);
    currentData.setMsgRateIn(0);
    assert (needUpdate.get());
    // zero to zero should never trigger an update.
    currentData.setMsgRateIn(0);
    lastData.setMsgRateIn(0);
    assert (!needUpdate.get());
    // Minimally test other absolute values to ensure they are included.
    lastData.getCpu().usage = 100;
    lastData.getCpu().limit = 1000;
    currentData.getCpu().usage = 106;
    currentData.getCpu().limit = 1000;
    assert (!needUpdate.get());
    // Minimally test other absolute values to ensure they are included.
    lastData.getCpu().usage = 100;
    lastData.getCpu().limit = 1000;
    currentData.getCpu().usage = 206;
    currentData.getCpu().limit = 1000;
    assert (needUpdate.get());
    lastData.setCpu(new ResourceUsage());
    currentData.setCpu(new ResourceUsage());
    lastData.setMsgThroughputIn(100);
    currentData.setMsgThroughputIn(106);
    assert (needUpdate.get());
    currentData.setMsgThroughputIn(100);
    lastData.setNumBundles(100);
    currentData.setNumBundles(106);
    assert (needUpdate.get());
    currentData.setNumBundles(100);
    assert (!needUpdate.get());
}
Also used : LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage) ResourceUsage(org.apache.pulsar.policies.data.loadbalancer.ResourceUsage) ModularLoadManagerImpl(org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerImpl) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) Test(org.testng.annotations.Test)

Example 9 with LocalBrokerData

use of org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData in project incubator-pulsar by apache.

the class AdminTest method brokerStats.

@Test
void brokerStats() throws Exception {
    doReturn("client-id").when(brokerStats).clientAppId();
    Collection<Metrics> metrics = brokerStats.getMetrics();
    assertNotNull(metrics);
    LocalBrokerData loadReport = (LocalBrokerData) brokerStats.getLoadReport();
    assertNotNull(loadReport);
    assertNotNull(loadReport.getCpu());
    Collection<Metrics> mBeans = brokerStats.getMBeans();
    assertTrue(!mBeans.isEmpty());
    AllocatorStats allocatorStats = brokerStats.getAllocatorStats("default");
    assertNotNull(allocatorStats);
    Map<String, Map<String, PendingBookieOpsStats>> bookieOpsStats = brokerStats.getPendingBookieOpsStats();
    assertTrue(bookieOpsStats.isEmpty());
    StreamingOutput topic = brokerStats.getTopics2();
    assertNotNull(topic);
    try {
        brokerStats.getBrokerResourceAvailability("prop", "use", "ns2");
        fail("should have failed as ModularLoadManager doesn't support it");
    } catch (RestException re) {
    // Ok
    }
}
Also used : Metrics(org.apache.pulsar.common.stats.Metrics) LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData) AllocatorStats(org.apache.pulsar.common.stats.AllocatorStats) RestException(org.apache.pulsar.broker.web.RestException) StreamingOutput(javax.ws.rs.core.StreamingOutput) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 10 with LocalBrokerData

use of org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData in project incubator-pulsar by apache.

the class LookupDataTest method getModularLoadManagerLoadReport.

private LocalBrokerData getModularLoadManagerLoadReport(String brokerUrl, ResourceUsage bandwidthIn) {
    LocalBrokerData report = new LocalBrokerData(brokerUrl, null, null, null);
    report.setBandwidthIn(bandwidthIn);
    return report;
}
Also used : LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData)

Aggregations

LocalBrokerData (org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData)14 BrokerData (org.apache.pulsar.broker.BrokerData)6 Test (org.testng.annotations.Test)5 Map (java.util.Map)4 TimeAverageBrokerData (org.apache.pulsar.broker.TimeAverageBrokerData)4 ResourceUsage (org.apache.pulsar.policies.data.loadbalancer.ResourceUsage)4 SystemResourceUsage (org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage)4 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)3 LoadReport (org.apache.pulsar.policies.data.loadbalancer.LoadReport)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)2 TimeAverageMessageData (org.apache.pulsar.broker.TimeAverageMessageData)2 BrokerFilterException (org.apache.pulsar.broker.loadbalance.BrokerFilterException)2 ModularLoadManagerImpl (org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerImpl)2 NamespaceBundleStats (org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats)2 KeeperException (org.apache.zookeeper.KeeperException)2 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)2 URI (java.net.URI)1