Search in sources :

Example 11 with ResourceQuota

use of org.apache.pulsar.common.policies.data.ResourceQuota in project incubator-pulsar by apache.

the class SimpleLoadManagerImplTest method testPrimary.

@Test(enabled = true)
public void testPrimary() throws Exception {
    createNamespacePolicies(pulsar1);
    LoadManager loadManager = new SimpleLoadManagerImpl(pulsar1);
    PulsarResourceDescription rd = new PulsarResourceDescription();
    rd.put("memory", new ResourceUsage(1024, 4096));
    rd.put("cpu", new ResourceUsage(10, 100));
    rd.put("bandwidthIn", new ResourceUsage(250 * 1024, 1024 * 1024));
    rd.put("bandwidthOut", new ResourceUsage(550 * 1024, 1024 * 1024));
    ResourceUnit ru1 = new SimpleResourceUnit("http://" + pulsar1.getAdvertisedAddress() + ":" + pulsar1.getConfiguration().getWebServicePort(), rd);
    Set<ResourceUnit> rus = new HashSet<ResourceUnit>();
    rus.add(ru1);
    LoadRanker lr = new ResourceAvailabilityRanker();
    // inject the load report and rankings
    Map<ResourceUnit, org.apache.pulsar.policies.data.loadbalancer.LoadReport> loadReports = new HashMap<>();
    org.apache.pulsar.policies.data.loadbalancer.LoadReport loadReport = new org.apache.pulsar.policies.data.loadbalancer.LoadReport();
    loadReport.setSystemResourceUsage(new SystemResourceUsage());
    loadReports.put(ru1, loadReport);
    setObjectField(SimpleLoadManagerImpl.class, loadManager, "currentLoadReports", loadReports);
    ResourceUnitRanking ranking = new ResourceUnitRanking(loadReport.getSystemResourceUsage(), new HashSet<String>(), new ResourceQuota(), new HashSet<String>(), new ResourceQuota());
    Map<ResourceUnit, ResourceUnitRanking> rankings = new HashMap<>();
    rankings.put(ru1, ranking);
    setObjectField(SimpleLoadManagerImpl.class, loadManager, "resourceUnitRankings", rankings);
    AtomicReference<Map<Long, Set<ResourceUnit>>> sortedRankingsInstance = new AtomicReference<>(Maps.newTreeMap());
    sortedRankingsInstance.get().put(lr.getRank(rd), rus);
    setObjectField(SimpleLoadManagerImpl.class, loadManager, "sortedRankings", sortedRankingsInstance);
    ResourceUnit found = ((SimpleLoadManagerImpl) loadManager).getLeastLoaded(NamespaceName.get("pulsar/use/primary-ns.10")).get();
    // broker is not active so found should be null
    assertNotEquals(found, null, "did not find a broker when expected one to be found");
}
Also used : PulsarResourceDescription(org.apache.pulsar.broker.loadbalance.impl.PulsarResourceDescription) HashMap(java.util.HashMap) ResourceAvailabilityRanker(org.apache.pulsar.broker.loadbalance.impl.ResourceAvailabilityRanker) SimpleResourceUnit(org.apache.pulsar.broker.loadbalance.impl.SimpleResourceUnit) SimpleLoadManagerImpl(org.apache.pulsar.broker.loadbalance.impl.SimpleLoadManagerImpl) ResourceQuota(org.apache.pulsar.common.policies.data.ResourceQuota) ResourceUnitRanking(org.apache.pulsar.policies.data.loadbalancer.ResourceUnitRanking) HashSet(java.util.HashSet) 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) SimpleResourceUnit(org.apache.pulsar.broker.loadbalance.impl.SimpleResourceUnit) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 12 with ResourceQuota

use of org.apache.pulsar.common.policies.data.ResourceQuota in project incubator-pulsar by apache.

the class SimpleLoadManagerImplTest method testEvenBundleDistribution.

// Test that bundles belonging to the same namespace are evenly distributed.
@Test
public void testEvenBundleDistribution() throws Exception {
    final NamespaceBundle[] bundles = LoadBalancerTestingUtils.makeBundles(pulsar1.getNamespaceService().getNamespaceBundleFactory(), "pulsar", "use", "test", 16);
    final ResourceQuota quota = new ResourceQuota();
    final String quotaZPath = String.format("%s/%s/%s", ResourceQuotaCache.RESOURCE_QUOTA_ROOT, "namespace", bundles[0]);
    // Create high message rate quota for the first bundle to make it unlikely to be a coincidence of even
    // distribution.
    ZkUtils.createFullPathOptimistic(pulsar1.getZkClient(), quotaZPath, ObjectMapperFactory.getThreadLocal().writeValueAsBytes(quota), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    int numAssignedToPrimary = 0;
    int numAssignedToSecondary = 0;
    pulsar1.getConfiguration().setLoadBalancerPlacementStrategy(SimpleLoadManagerImpl.LOADBALANCER_STRATEGY_LLS);
    final SimpleLoadManagerImpl loadManager = (SimpleLoadManagerImpl) pulsar1.getLoadManager().get();
    for (final NamespaceBundle bundle : bundles) {
        if (loadManager.getLeastLoaded(bundle).get().getResourceId().equals(primaryHost)) {
            ++numAssignedToPrimary;
        } else {
            ++numAssignedToSecondary;
        }
        // Check that number of assigned bundles are equivalent when an even number have been assigned.
        if ((numAssignedToPrimary + numAssignedToSecondary) % 2 == 0) {
            assert (numAssignedToPrimary == numAssignedToSecondary);
        }
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) ResourceQuota(org.apache.pulsar.common.policies.data.ResourceQuota) SimpleLoadManagerImpl(org.apache.pulsar.broker.loadbalance.impl.SimpleLoadManagerImpl) Test(org.testng.annotations.Test)

Example 13 with ResourceQuota

use of org.apache.pulsar.common.policies.data.ResourceQuota in project incubator-pulsar by apache.

the class AdminTest method resourceQuotas.

@Test
void resourceQuotas() throws Exception {
    // get Default Resource Quota
    ResourceQuota quota = resourceQuotas.getDefaultResourceQuota();
    assertNotNull(quota);
    assertTrue(quota.getBandwidthIn() > 0);
    // set Default Resource Quota
    double defaultBandwidth = 1000;
    quota.setBandwidthIn(defaultBandwidth);
    quota.setBandwidthOut(defaultBandwidth);
    resourceQuotas.setDefaultResourceQuota(quota);
    assertTrue(resourceQuotas.getDefaultResourceQuota().getBandwidthIn() == defaultBandwidth);
    assertTrue(resourceQuotas.getDefaultResourceQuota().getBandwidthOut() == defaultBandwidth);
    String property = "prop-xyz";
    String cluster = "use";
    String namespace = "ns";
    String bundleRange = "0x00000000_0xffffffff";
    Policies policies = new Policies();
    doReturn(policies).when(resourceQuotas).getNamespacePolicies(NamespaceName.get(property, cluster, namespace));
    doReturn("client-id").when(resourceQuotas).clientAppId();
    try {
        resourceQuotas.setNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange, quota);
        fail();
    } catch (Exception e) {
    // OK : should fail without creating policies
    }
    try {
        resourceQuotas.removeNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange);
        fail();
    } catch (Exception e) {
    // OK : should fail without creating policies
    }
    // create policies
    PropertyAdmin admin = new PropertyAdmin();
    admin.getAllowedClusters().add(cluster);
    mockZookKeeper.create(PulsarWebResource.path(POLICIES, property), ObjectMapperFactory.getThreadLocal().writeValueAsBytes(admin), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // customized bandwidth for this namespace
    double customizeBandwidth = 3000;
    quota.setBandwidthIn(customizeBandwidth);
    quota.setBandwidthOut(customizeBandwidth);
    // set and get Resource Quota
    resourceQuotas.setNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange, quota);
    ResourceQuota bundleQuota = resourceQuotas.getNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange);
    assertEquals(quota, bundleQuota);
    // remove quota which sets to default quota
    resourceQuotas.removeNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange);
    bundleQuota = resourceQuotas.getNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange);
    assertTrue(bundleQuota.getBandwidthIn() == defaultBandwidth);
    assertTrue(bundleQuota.getBandwidthOut() == defaultBandwidth);
}
Also used : Policies(org.apache.pulsar.common.policies.data.Policies) ResourceQuota(org.apache.pulsar.common.policies.data.ResourceQuota) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) RestException(org.apache.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 14 with ResourceQuota

use of org.apache.pulsar.common.policies.data.ResourceQuota in project incubator-pulsar by apache.

the class ResourceQuotaCacheTest method testGetSetDefaultQuota.

@Test
public void testGetSetDefaultQuota() throws Exception {
    ResourceQuotaCache cache = new ResourceQuotaCache(zkCache);
    ResourceQuota quota1 = ResourceQuotaCache.getInitialQuotaValue();
    ResourceQuota quota2 = new ResourceQuota();
    quota2.setMsgRateIn(10);
    quota2.setMsgRateOut(20);
    quota2.setBandwidthIn(10000);
    quota2.setBandwidthOut(20000);
    quota2.setMemory(100);
    quota2.setDynamic(false);
    assertEquals(cache.getDefaultQuota(), quota1);
    cache.setDefaultQuota(quota2);
    assertEquals(cache.getDefaultQuota(), quota2);
}
Also used : ResourceQuota(org.apache.pulsar.common.policies.data.ResourceQuota) Test(org.testng.annotations.Test)

Example 15 with ResourceQuota

use of org.apache.pulsar.common.policies.data.ResourceQuota in project incubator-pulsar by apache.

the class ResourceQuotaTest method testResourceQuotaDefault.

@Test
public void testResourceQuotaDefault() {
    ResourceQuota quota = new ResourceQuota();
    Assert.assertEquals(quota.getMsgRateIn(), 0.0);
    Assert.assertEquals(quota.getMsgRateOut(), 0.0);
    Assert.assertEquals(quota.getBandwidthIn(), 0.0);
    Assert.assertEquals(quota.getBandwidthOut(), 0.0);
    Assert.assertEquals(quota.getMemory(), 0.0);
    Assert.assertEquals(quota.getDynamic(), true);
    Assert.assertFalse(quota.isValid());
    quota.setMsgRateIn(10);
    quota.setMsgRateOut(20);
    quota.setBandwidthIn(10000);
    quota.setBandwidthOut(20000);
    quota.setMemory(100);
    quota.setDynamic(false);
    Assert.assertEquals(quota.getMsgRateIn(), 10.0);
    Assert.assertEquals(quota.getMsgRateOut(), 20.0);
    Assert.assertEquals(quota.getBandwidthIn(), 10000.0);
    Assert.assertEquals(quota.getBandwidthOut(), 20000.0);
    Assert.assertEquals(quota.getMemory(), 100.0);
    Assert.assertEquals(quota.getDynamic(), false);
}
Also used : ResourceQuota(org.apache.pulsar.common.policies.data.ResourceQuota) Test(org.testng.annotations.Test)

Aggregations

ResourceQuota (org.apache.pulsar.common.policies.data.ResourceQuota)24 Test (org.testng.annotations.Test)10 HashMap (java.util.HashMap)9 Map (java.util.Map)9 TreeMap (java.util.TreeMap)6 KeeperException (org.apache.zookeeper.KeeperException)6 LoadReport (org.apache.pulsar.policies.data.loadbalancer.LoadReport)5 SystemResourceUsage (org.apache.pulsar.policies.data.loadbalancer.SystemResourceUsage)5 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)4 ResourceUnit (org.apache.pulsar.broker.loadbalance.ResourceUnit)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 BundleData (org.apache.pulsar.broker.BundleData)3 NamespaceBundleStats (org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats)3 ResourceUnitRanking (org.apache.pulsar.policies.data.loadbalancer.ResourceUnitRanking)3 ParameterException (com.beust.jcommander.ParameterException)2 Set (java.util.Set)2 Future (java.util.concurrent.Future)2