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");
}
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);
}
}
}
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);
}
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);
}
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);
}
Aggregations