Search in sources :

Example 1 with LoadManagerReport

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

the class LookupDataTest method testLoadReportSerialization.

@Test
public void testLoadReportSerialization() throws Exception {
    final String simpleLmBrokerUrl = "simple";
    final String simpleLmReportName = "simpleLoadManager";
    final String modularLmBrokerUrl = "modular";
    final SystemResourceUsage simpleLmSystemResourceUsage = new SystemResourceUsage();
    final ResourceUsage resource = new ResourceUsage();
    final double usage = 55.0;
    resource.usage = usage;
    simpleLmSystemResourceUsage.bandwidthIn = resource;
    LoadReport simpleReport = getSimpleLoadManagerLoadReport(simpleLmBrokerUrl, simpleLmReportName, simpleLmSystemResourceUsage);
    LocalBrokerData modularReport = getModularLoadManagerLoadReport(modularLmBrokerUrl, resource);
    LoadManagerReport simpleLoadReport = ObjectMapperFactory.getThreadLocal().readValue(ObjectMapperFactory.getThreadLocal().writeValueAsBytes(simpleReport), LoadManagerReport.class);
    LoadManagerReport modularLoadReport = ObjectMapperFactory.getThreadLocal().readValue(ObjectMapperFactory.getThreadLocal().writeValueAsBytes(modularReport), LoadManagerReport.class);
    assertEquals(simpleLoadReport.getWebServiceUrl(), simpleLmBrokerUrl);
    assertTrue(simpleLoadReport instanceof LoadReport);
    assertEquals(((LoadReport) simpleLoadReport).getName(), simpleLmReportName);
    assertEquals(((LoadReport) simpleLoadReport).getSystemResourceUsage().bandwidthIn.usage, usage);
    assertEquals(modularLoadReport.getWebServiceUrl(), modularLmBrokerUrl);
    assertTrue(modularLoadReport instanceof LocalBrokerData);
    assertEquals(((LocalBrokerData) modularLoadReport).getBandwidthIn().usage, usage);
}
Also used : LoadManagerReport(org.apache.pulsar.policies.data.loadbalancer.LoadManagerReport) LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData) ResourceUsage(org.apache.pulsar.policies.data.loadbalancer.ResourceUsage) SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage) LoadReport(org.apache.pulsar.policies.data.loadbalancer.LoadReport) SystemResourceUsage(org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage) Test(org.testng.annotations.Test)

Example 2 with LoadManagerReport

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

the class ZookeeperCacheLoaderTest method testZookeeperCacheLoader.

/**
 * Create znode for available broker in ZooKeeper and updates it again to verify ZooKeeper cache update
 *
 * @throws InterruptedException
 * @throws KeeperException
 * @throws IOException
 */
@Test
public void testZookeeperCacheLoader() throws InterruptedException, KeeperException, Exception {
    DiscoveryZooKeeperClientFactoryImpl.zk = mockZookKeeper;
    @SuppressWarnings("resource") ZookeeperCacheLoader zkLoader = new ZookeeperCacheLoader(new DiscoveryZooKeeperClientFactoryImpl(), "", 30_000);
    List<String> brokers = Lists.newArrayList("broker-1:15000", "broker-2:15000", "broker-3:15000");
    for (int i = 0; i < brokers.size(); i++) {
        try {
            LoadManagerReport report = i % 2 == 0 ? getSimpleLoadManagerLoadReport(brokers.get(i)) : getModularLoadManagerLoadReport(brokers.get(i));
            zkLoader.getLocalZkCache().getZooKeeper().create(LOADBALANCE_BROKERS_ROOT + "/" + brokers.get(i), ObjectMapperFactory.getThreadLocal().writeValueAsBytes(report), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (Exception e) {
            fail("failed while creating broker znodes");
        }
    }
    // strategically wait for cache to get sync
    for (int i = 0; i < 5; i++) {
        if (zkLoader.getAvailableBrokers().size() == 3 || i == 4) {
            break;
        }
        Thread.sleep(1000);
    }
    // 2. get available brokers from ZookeeperCacheLoader
    List<LoadManagerReport> list = zkLoader.getAvailableBrokers();
    // 3. verify retrieved broker list
    Set<String> cachedBrokers = list.stream().map(loadReport -> loadReport.getWebServiceUrl()).collect(Collectors.toSet());
    Assert.assertEquals(list.size(), brokers.size());
    Assert.assertTrue(brokers.containsAll(cachedBrokers));
    // 4.a add new broker
    final String newBroker = "broker-4:15000";
    LoadManagerReport report = getSimpleLoadManagerLoadReport(newBroker);
    zkLoader.getLocalZkCache().getZooKeeper().create(LOADBALANCE_BROKERS_ROOT + "/" + newBroker, ObjectMapperFactory.getThreadLocal().writeValueAsBytes(report), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    brokers.add(newBroker);
    // wait for 100 msec: to get cache updated
    Thread.sleep(100);
    // 4.b. get available brokers from ZookeeperCacheLoader
    list = zkLoader.getAvailableBrokers();
    // 4.c. verify retrieved broker list
    cachedBrokers = list.stream().map(loadReport -> loadReport.getWebServiceUrl()).collect(Collectors.toSet());
    Assert.assertEquals(list.size(), brokers.size());
    Assert.assertTrue(brokers.containsAll(cachedBrokers));
}
Also used : LoadManagerReport(org.apache.pulsar.policies.data.loadbalancer.LoadManagerReport) CreateMode(org.apache.zookeeper.CreateMode) LOADBALANCE_BROKERS_ROOT(org.apache.pulsar.discovery.service.web.ZookeeperCacheLoader.LOADBALANCE_BROKERS_ROOT) KeeperException(org.apache.zookeeper.KeeperException) LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData) Assert.fail(org.testng.Assert.fail) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) IOException(java.io.IOException) Test(org.testng.annotations.Test) LoadReport(org.apache.pulsar.policies.data.loadbalancer.LoadReport) AfterMethod(org.testng.annotations.AfterMethod) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) Lists(com.google.common.collect.Lists) ZooDefs(org.apache.zookeeper.ZooDefs) Assert(org.testng.Assert) LoadManagerReport(org.apache.pulsar.policies.data.loadbalancer.LoadManagerReport) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 3 with LoadManagerReport

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

the class ServerConnection method sendLookupResponse.

private void sendLookupResponse(long requestId) {
    try {
        LoadManagerReport availableBroker = service.getDiscoveryProvider().nextBroker();
        ctx.writeAndFlush(Commands.newLookupResponse(availableBroker.getPulsarServiceUrl(), availableBroker.getPulsarServiceUrlTls(), false, Redirect, requestId, false));
    } catch (PulsarServerException e) {
        LOG.warn("[{}] Failed to get next active broker {}", remoteAddress, e.getMessage(), e);
        ctx.writeAndFlush(Commands.newLookupErrorResponse(ServerError.ServiceNotReady, e.getMessage(), requestId));
    }
}
Also used : LoadManagerReport(org.apache.pulsar.policies.data.loadbalancer.LoadManagerReport) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 4 with LoadManagerReport

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

the class DiscoveryServiceServlet method redirect.

private void redirect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        LoadManagerReport broker = nextBroker();
        URI brokerURI;
        if (request.getScheme().equals("http")) {
            // Use normal HTTP url
            brokerURI = new URI(broker.getWebServiceUrl());
        } else {
            brokerURI = new URI(broker.getWebServiceUrlTls());
        }
        StringBuilder location = new StringBuilder();
        location.append(brokerURI.getScheme()).append("://").append(brokerURI.getHost()).append(':').append(brokerURI.getPort()).append(request.getRequestURI());
        if (request.getQueryString() != null) {
            location.append('?').append(request.getQueryString());
        }
        if (log.isDebugEnabled()) {
            log.info("Redirecting to {}", location);
        }
        response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
        response.setHeader("Location", location.toString());
    } catch (URISyntaxException e) {
        log.warn("No broker found in zookeeper {}", e.getMessage(), e);
        throw new RestException(Status.SERVICE_UNAVAILABLE, "Broker is not available");
    }
}
Also used : LoadManagerReport(org.apache.pulsar.policies.data.loadbalancer.LoadManagerReport) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 5 with LoadManagerReport

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

the class LoadReportNetworkLimit method checkLoadReportNicSpeed.

@Test
public void checkLoadReportNicSpeed() throws Exception {
    // Since we have overridden the NIC speed in the configuration, the load report for the broker should always
    LoadManagerReport report = admin.brokerStats().getLoadReport();
    if (SystemUtils.IS_OS_LINUX) {
        assertEquals(report.getBandwidthIn().limit, 5.4 * 1024 * 1024);
        assertEquals(report.getBandwidthOut().limit, 5.4 * 1024 * 1024);
    } else {
        // On non-Linux system we don't report the network usage
        assertEquals(report.getBandwidthIn().limit, -1.0);
        assertEquals(report.getBandwidthOut().limit, -1.0);
    }
}
Also used : LoadManagerReport(org.apache.pulsar.policies.data.loadbalancer.LoadManagerReport) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest) Test(org.testng.annotations.Test)

Aggregations

LoadManagerReport (org.apache.pulsar.policies.data.loadbalancer.LoadManagerReport)5 Test (org.testng.annotations.Test)3 LoadReport (org.apache.pulsar.policies.data.loadbalancer.LoadReport)2 LocalBrokerData (org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData)2 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)1 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)1 ObjectMapperFactory (org.apache.pulsar.common.util.ObjectMapperFactory)1 LOADBALANCE_BROKERS_ROOT (org.apache.pulsar.discovery.service.web.ZookeeperCacheLoader.LOADBALANCE_BROKERS_ROOT)1 ResourceUsage (org.apache.pulsar.policies.data.loadbalancer.ResourceUsage)1 SystemResourceUsage (org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage)1 CreateMode (org.apache.zookeeper.CreateMode)1 KeeperException (org.apache.zookeeper.KeeperException)1