Search in sources :

Example 76 with PropertyAdmin

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

the class ModularLoadManagerImplTest method testNamespaceIsolationPoliciesForPrimaryAndSecondaryBrokers.

/**
 * It verifies namespace-isolation policies with primary and secondary brokers.
 *
 * usecase:
 *
 * <pre>
 *  1. Namespace: primary=broker1, secondary=broker2, shared=broker3, min_limit = 1
 *     a. available-brokers: broker1, broker2, broker3 => result: broker1
 *     b. available-brokers: broker2, broker3          => result: broker2
 *     c. available-brokers: broker3                   => result: NULL
 *  2. Namespace: primary=broker1, secondary=broker2, shared=broker3, min_limit = 2
 *     a. available-brokers: broker1, broker2, broker3 => result: broker1, broker2
 *     b. available-brokers: broker2, broker3          => result: broker2
 *     c. available-brokers: broker3                   => result: NULL
 * </pre>
 *
 * @throws Exception
 */
@Test
public void testNamespaceIsolationPoliciesForPrimaryAndSecondaryBrokers() throws Exception {
    final String property = "my-property";
    final String cluster = "use";
    final String namespace = "my-ns";
    final String broker1Address = pulsar1.getAdvertisedAddress() + "0";
    final String broker2Address = pulsar2.getAdvertisedAddress() + "1";
    final String sharedBroker = "broker3";
    admin1.clusters().createCluster(cluster, new ClusterData("http://" + pulsar1.getAdvertisedAddress()));
    admin1.properties().createProperty(property, new PropertyAdmin(Lists.newArrayList("appid1", "appid2"), Sets.newHashSet(cluster)));
    admin1.namespaces().createNamespace(property + "/" + cluster + "/" + namespace);
    // set a new policy
    String newPolicyJsonTemplate = "{\"namespaces\":[\"%s/%s/%s.*\"],\"primary\":[\"%s\"]," + "\"secondary\":[\"%s\"],\"auto_failover_policy\":{\"policy_type\":\"min_available\",\"parameters\":{\"min_limit\":%s,\"usage_threshold\":80}}}";
    String newPolicyJson = String.format(newPolicyJsonTemplate, property, cluster, namespace, broker1Address, broker2Address, 1);
    String newPolicyName = "my-ns-isolation-policies";
    ObjectMapper jsonMapper = ObjectMapperFactory.create();
    NamespaceIsolationData nsPolicyData = jsonMapper.readValue(newPolicyJson.getBytes(), NamespaceIsolationData.class);
    admin1.clusters().createNamespaceIsolationPolicy("use", newPolicyName, nsPolicyData);
    SimpleResourceAllocationPolicies simpleResourceAllocationPolicies = new SimpleResourceAllocationPolicies(pulsar1);
    ServiceUnitId serviceUnit = LoadBalancerTestingUtils.makeBundles(nsFactory, property, cluster, namespace, 1)[0];
    BrokerTopicLoadingPredicate brokerTopicLoadingPredicate = new BrokerTopicLoadingPredicate() {

        @Override
        public boolean isEnablePersistentTopics(String brokerUrl) {
            return true;
        }

        @Override
        public boolean isEnableNonPersistentTopics(String brokerUrl) {
            return true;
        }
    };
    // (1) now we have isolation policy : primary=broker1, secondary=broker2, minLimit=1
    // test1: shared=1, primary=1, secondary=1 => It should return 1 primary broker only
    Set<String> brokerCandidateCache = Sets.newHashSet();
    Set<String> availableBrokers = Sets.newHashSet(sharedBroker, broker1Address, broker2Address);
    LoadManagerShared.applyNamespacePolicies(serviceUnit, simpleResourceAllocationPolicies, brokerCandidateCache, availableBrokers, brokerTopicLoadingPredicate);
    assertEquals(brokerCandidateCache.size(), 1);
    assertTrue(brokerCandidateCache.contains(broker1Address));
    // test2: shared=1, primary=0, secondary=1 => It should return 1 secondary broker only
    brokerCandidateCache = Sets.newHashSet();
    availableBrokers = Sets.newHashSet(sharedBroker, broker2Address);
    LoadManagerShared.applyNamespacePolicies(serviceUnit, simpleResourceAllocationPolicies, brokerCandidateCache, availableBrokers, brokerTopicLoadingPredicate);
    assertEquals(brokerCandidateCache.size(), 1);
    assertTrue(brokerCandidateCache.contains(broker2Address));
    // test3: shared=1, primary=0, secondary=0 => It should return 0 broker
    brokerCandidateCache = Sets.newHashSet();
    availableBrokers = Sets.newHashSet(sharedBroker);
    LoadManagerShared.applyNamespacePolicies(serviceUnit, simpleResourceAllocationPolicies, brokerCandidateCache, availableBrokers, brokerTopicLoadingPredicate);
    assertEquals(brokerCandidateCache.size(), 0);
    // (2) now we will have isolation policy : primary=broker1, secondary=broker2, minLimit=2
    newPolicyJson = String.format(newPolicyJsonTemplate, property, cluster, namespace, broker1Address, broker2Address, 2);
    nsPolicyData = jsonMapper.readValue(newPolicyJson.getBytes(), NamespaceIsolationData.class);
    admin1.clusters().createNamespaceIsolationPolicy("use", newPolicyName, nsPolicyData);
    // test1: shared=1, primary=1, secondary=1 => It should return primary + secondary
    brokerCandidateCache = Sets.newHashSet();
    availableBrokers = Sets.newHashSet(sharedBroker, broker1Address, broker2Address);
    LoadManagerShared.applyNamespacePolicies(serviceUnit, simpleResourceAllocationPolicies, brokerCandidateCache, availableBrokers, brokerTopicLoadingPredicate);
    assertEquals(brokerCandidateCache.size(), 2);
    assertTrue(brokerCandidateCache.contains(broker1Address));
    assertTrue(brokerCandidateCache.contains(broker2Address));
    // test2: shared=1, secondary=1 => It should return secondary
    brokerCandidateCache = Sets.newHashSet();
    availableBrokers = Sets.newHashSet(sharedBroker, broker2Address);
    LoadManagerShared.applyNamespacePolicies(serviceUnit, simpleResourceAllocationPolicies, brokerCandidateCache, availableBrokers, brokerTopicLoadingPredicate);
    assertEquals(brokerCandidateCache.size(), 1);
    assertTrue(brokerCandidateCache.contains(broker2Address));
    // test3: shared=1, => It should return 0 broker
    brokerCandidateCache = Sets.newHashSet();
    availableBrokers = Sets.newHashSet(sharedBroker);
    LoadManagerShared.applyNamespacePolicies(serviceUnit, simpleResourceAllocationPolicies, brokerCandidateCache, availableBrokers, brokerTopicLoadingPredicate);
    assertEquals(brokerCandidateCache.size(), 0);
}
Also used : BrokerTopicLoadingPredicate(org.apache.pulsar.broker.loadbalance.impl.LoadManagerShared.BrokerTopicLoadingPredicate) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) SimpleResourceAllocationPolicies(org.apache.pulsar.broker.loadbalance.impl.SimpleResourceAllocationPolicies) NamespaceIsolationData(org.apache.pulsar.common.policies.data.NamespaceIsolationData) ServiceUnitId(org.apache.pulsar.common.naming.ServiceUnitId) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 77 with PropertyAdmin

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

the class SLAMonitoringTest method createProperty.

private void createProperty(PulsarAdmin pulsarAdmin) throws PulsarClientException, MalformedURLException, PulsarAdminException {
    ClusterData clusterData = new ClusterData();
    clusterData.setServiceUrl(pulsarAdmin.getServiceUrl().toString());
    pulsarAdmins[0].clusters().createCluster("my-cluster", clusterData);
    Set<String> allowedClusters = new HashSet<>();
    allowedClusters.add("my-cluster");
    PropertyAdmin adminConfig = new PropertyAdmin();
    adminConfig.setAllowedClusters(allowedClusters);
    List<String> adminRoles = new ArrayList<>();
    adminRoles.add("");
    adminConfig.setAdminRoles(adminRoles);
    pulsarAdmin.properties().createProperty("sla-monitor", adminConfig);
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 78 with PropertyAdmin

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

the class AdminApiTest method properties.

@Test(enabled = true)
public void properties() throws PulsarAdminException {
    Set<String> allowedClusters = Sets.newHashSet("use");
    PropertyAdmin propertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "role2"), allowedClusters);
    admin.properties().updateProperty("prop-xyz", propertyAdmin);
    assertEquals(admin.properties().getProperties(), Lists.newArrayList("prop-xyz"));
    assertEquals(admin.properties().getPropertyAdmin("prop-xyz"), propertyAdmin);
    PropertyAdmin newPropertyAdmin = new PropertyAdmin(Lists.newArrayList("role3", "role4"), allowedClusters);
    admin.properties().updateProperty("prop-xyz", newPropertyAdmin);
    assertEquals(admin.properties().getPropertyAdmin("prop-xyz"), newPropertyAdmin);
    admin.namespaces().deleteNamespace("prop-xyz/use/ns1");
    admin.properties().deleteProperty("prop-xyz");
    assertEquals(admin.properties().getProperties(), Lists.newArrayList());
    // Check name validation
    try {
        admin.properties().createProperty("prop-xyz&", propertyAdmin);
        fail("should have failed");
    } catch (PulsarAdminException e) {
        assertTrue(e instanceof PreconditionFailedException);
    }
}
Also used : PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 79 with PropertyAdmin

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

the class AdminApiTest2 method setup.

@BeforeMethod
@Override
public void setup() throws Exception {
    conf.setLoadBalancerEnabled(true);
    super.internalSetup();
    // create otherbroker to test redirect on calls that need
    // namespace ownership
    mockPulsarSetup = new MockedPulsarService(this.conf);
    mockPulsarSetup.setup();
    // Setup namespaces
    admin.clusters().createCluster("use", new ClusterData("http://127.0.0.1" + ":" + BROKER_WEBSERVICE_PORT));
    PropertyAdmin propertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "role2"), Sets.newHashSet("use"));
    admin.properties().createProperty("prop-xyz", propertyAdmin);
    admin.namespaces().createNamespace("prop-xyz/use/ns1");
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) MockedPulsarService(org.apache.pulsar.broker.admin.AdminApiTest.MockedPulsarService) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 80 with PropertyAdmin

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

the class BacklogQuotaManagerTest method setup.

@BeforeMethod
void setup() throws Exception {
    try {
        // start local bookie and zookeeper
        bkEnsemble = new LocalBookkeeperEnsemble(3, ZOOKEEPER_PORT, 5001);
        bkEnsemble.start();
        // start pulsar service
        config = new ServiceConfiguration();
        config.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT);
        config.setAdvertisedAddress("localhost");
        config.setWebServicePort(BROKER_WEBSERVICE_PORT);
        config.setClusterName("usc");
        config.setBrokerServicePort(BROKER_SERVICE_PORT);
        config.setAuthorizationEnabled(false);
        config.setAuthenticationEnabled(false);
        config.setBacklogQuotaCheckIntervalInSeconds(TIME_TO_CHECK_BACKLOG_QUOTA);
        config.setManagedLedgerMaxEntriesPerLedger(5);
        config.setManagedLedgerMinLedgerRolloverTimeMinutes(0);
        pulsar = new PulsarService(config);
        pulsar.start();
        adminUrl = new URL("http://127.0.0.1" + ":" + BROKER_WEBSERVICE_PORT);
        admin = new PulsarAdmin(adminUrl, (Authentication) null);
        admin.clusters().createCluster("usc", new ClusterData(adminUrl.toString()));
        admin.properties().createProperty("prop", new PropertyAdmin(Lists.newArrayList("appid1"), Sets.newHashSet("usc")));
        admin.namespaces().createNamespace("prop/usc/ns-quota");
        admin.namespaces().createNamespace("prop/usc/quotahold");
        admin.namespaces().createNamespace("prop/usc/quotaholdasync");
    } catch (Throwable t) {
        LOG.error("Error setting up broker test", t);
        Assert.fail("Broker test setup failed");
    }
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PulsarService(org.apache.pulsar.broker.PulsarService) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) Authentication(org.apache.pulsar.client.api.Authentication) LocalBookkeeperEnsemble(org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble) URL(java.net.URL) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

PropertyAdmin (org.apache.pulsar.common.policies.data.PropertyAdmin)83 Test (org.testng.annotations.Test)60 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)29 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)13 PulsarClient (org.apache.pulsar.client.api.PulsarClient)12 BeforeMethod (org.testng.annotations.BeforeMethod)12 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)11 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)9 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)9 AuthenticationTls (org.apache.pulsar.client.impl.auth.AuthenticationTls)8 HashSet (java.util.HashSet)6 URI (java.net.URI)5 URL (java.net.URL)5 Pattern (java.util.regex.Pattern)5 PulsarService (org.apache.pulsar.broker.PulsarService)5 RestException (org.apache.pulsar.broker.web.RestException)5 Authentication (org.apache.pulsar.client.api.Authentication)5 AuthAction (org.apache.pulsar.common.policies.data.AuthAction)5 KeeperException (org.apache.zookeeper.KeeperException)5 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)4