Search in sources :

Example 1 with ClusterData

use of com.yahoo.pulsar.common.policies.data.ClusterData in project pulsar by yahoo.

the class AdminTest method clusters.

@Test
void clusters() throws Exception {
    assertEquals(clusters.getClusters(), new ArrayList<String>());
    verify(clusters, never()).validateSuperUserAccess();
    clusters.createCluster("use", new ClusterData("http://broker.messaging.use.example.com"));
    verify(clusters, times(1)).validateSuperUserAccess();
    // ensure to read from ZooKeeper directly
    clusters.clustersListCache().clear();
    assertEquals(clusters.getClusters(), Lists.newArrayList("use"));
    // Check creating existing cluster
    try {
        clusters.createCluster("use", new ClusterData("http://broker.messaging.use.example.com"));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
    }
    // Check deleting non-existing cluster
    try {
        clusters.deleteCluster("usc");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    assertEquals(clusters.getCluster("use"), new ClusterData("http://broker.messaging.use.example.com"));
    verify(clusters, times(4)).validateSuperUserAccess();
    clusters.updateCluster("use", new ClusterData("http://new-broker.messaging.use.example.com"));
    verify(clusters, times(5)).validateSuperUserAccess();
    assertEquals(clusters.getCluster("use"), new ClusterData("http://new-broker.messaging.use.example.com"));
    verify(clusters, times(6)).validateSuperUserAccess();
    try {
        clusters.getNamespaceIsolationPolicies("use");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), 404);
    }
    NamespaceIsolationData policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("dummy/colo/ns");
    policyData.primary = new ArrayList<String>();
    policyData.primary.add("localhost" + ":" + BROKER_WEBSERVICE_PORT);
    policyData.secondary = new ArrayList<String>();
    policyData.auto_failover_policy = new AutoFailoverPolicyData();
    policyData.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
    policyData.auto_failover_policy.parameters = new HashMap<String, String>();
    policyData.auto_failover_policy.parameters.put("min_limit", "1");
    policyData.auto_failover_policy.parameters.put("usage_threshold", "90");
    clusters.setNamespaceIsolationPolicy("use", "policy1", policyData);
    clusters.getNamespaceIsolationPolicies("use");
    try {
        clusters.deleteCluster("use");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), 412);
    }
    clusters.deleteNamespaceIsolationPolicy("use", "policy1");
    assertTrue(clusters.getNamespaceIsolationPolicies("use").isEmpty());
    clusters.deleteCluster("use");
    verify(clusters, times(13)).validateSuperUserAccess();
    assertEquals(clusters.getClusters(), Lists.newArrayList());
    try {
        clusters.getCluster("use");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), 404);
    }
    try {
        clusters.updateCluster("use", new ClusterData());
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), 404);
    }
    try {
        clusters.getNamespaceIsolationPolicies("use");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), 404);
    }
    // Test zk failures
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    configurationCache.clustersListCache().clear();
    try {
        clusters.getClusters();
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        clusters.createCluster("test", new ClusterData("http://broker.messaging.test.example.com"));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        clusters.updateCluster("test", new ClusterData("http://broker.messaging.test.example.com"));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        clusters.getCluster("test");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failAfter(0, Code.SESSIONEXPIRED);
    try {
        clusters.deleteCluster("use");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failAfter(1, Code.SESSIONEXPIRED);
    try {
        clusters.deleteCluster("use");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    // Check name validations
    try {
        clusters.createCluster("bf@", new ClusterData("http://dummy.messaging.example.com"));
        fail("should have filed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
}
Also used : ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) AutoFailoverPolicyData(com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(com.yahoo.pulsar.common.policies.data.NamespaceIsolationData) RestException(com.yahoo.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 2 with ClusterData

use of com.yahoo.pulsar.common.policies.data.ClusterData in project pulsar by yahoo.

the class AdminTest method brokers.

@Test
void brokers() throws Exception {
    clusters.createCluster("use", new ClusterData("http://broker.messaging.use.example.com", "https://broker.messaging.use.example.com:4443"));
    URI requestUri = new URI("http://broker.messaging.use.example.com" + ":" + BROKER_WEBSERVICE_PORT + "/admin/brokers/use");
    UriInfo mockUri = mock(UriInfo.class);
    doReturn(requestUri).when(mockUri).getRequestUri();
    Field uriField = PulsarWebResource.class.getDeclaredField("uri");
    uriField.setAccessible(true);
    uriField.set(brokers, mockUri);
    Set<String> activeBrokers = brokers.getActiveBrokers("use");
    assertEquals(activeBrokers.size(), 1);
    assertEquals(activeBrokers, Sets.newHashSet(pulsar.getAdvertisedAddress() + ":" + BROKER_WEBSERVICE_PORT));
}
Also used : Field(java.lang.reflect.Field) ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) URI(java.net.URI) UriInfo(javax.ws.rs.core.UriInfo) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 3 with ClusterData

use of com.yahoo.pulsar.common.policies.data.ClusterData in project pulsar by yahoo.

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(com.yahoo.pulsar.common.policies.data.ClusterData) PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 4 with ClusterData

use of com.yahoo.pulsar.common.policies.data.ClusterData in project pulsar by yahoo.

the class AdminApiTest method setup.

@BeforeMethod
@Override
public void setup() throws Exception {
    conf.setLoadBalancerEnabled(true);
    super.internalSetup();
    bundleFactory = new NamespaceBundleFactory(pulsar, Hashing.crc32());
    // create otherbroker to test redirect on calls that need
    // namespace ownership
    ServiceConfiguration otherconfig = new ServiceConfiguration();
    otherconfig.setBrokerServicePort(SECONDARY_BROKER_PORT);
    otherconfig.setWebServicePort(SECONDARY_BROKER_WEBSERVICE_PORT);
    otherconfig.setLoadBalancerEnabled(false);
    otherconfig.setClusterName("test");
    otherPulsar = startBroker(otherconfig);
    otheradmin = new PulsarAdmin(new URL("http://127.0.0.1" + ":" + SECONDARY_BROKER_WEBSERVICE_PORT), (Authentication) null);
    // 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(com.yahoo.pulsar.common.policies.data.ClusterData) ServiceConfiguration(com.yahoo.pulsar.broker.ServiceConfiguration) PulsarAdmin(com.yahoo.pulsar.client.admin.PulsarAdmin) PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) Authentication(com.yahoo.pulsar.client.api.Authentication) NamespaceBundleFactory(com.yahoo.pulsar.common.naming.NamespaceBundleFactory) URL(java.net.URL) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 5 with ClusterData

use of com.yahoo.pulsar.common.policies.data.ClusterData in project pulsar by yahoo.

the class AdminApiTest method clusters.

@Test
public void clusters() throws Exception {
    admin.clusters().createCluster("usw", new ClusterData("http://broker.messaging.use.example.com" + ":" + BROKER_WEBSERVICE_PORT));
    assertEquals(admin.clusters().getClusters(), Lists.newArrayList("use", "usw"));
    assertEquals(admin.clusters().getCluster("use"), new ClusterData("http://127.0.0.1" + ":" + BROKER_WEBSERVICE_PORT));
    admin.clusters().updateCluster("usw", new ClusterData("http://new-broker.messaging.usw.example.com" + ":" + BROKER_WEBSERVICE_PORT));
    assertEquals(admin.clusters().getClusters(), Lists.newArrayList("use", "usw"));
    assertEquals(admin.clusters().getCluster("usw"), new ClusterData("http://new-broker.messaging.usw.example.com" + ":" + BROKER_WEBSERVICE_PORT));
    admin.clusters().updateCluster("usw", new ClusterData("http://new-broker.messaging.usw.example.com" + ":" + BROKER_WEBSERVICE_PORT, "https://new-broker.messaging.usw.example.com" + ":" + BROKER_WEBSERVICE_PORT_TLS));
    assertEquals(admin.clusters().getClusters(), Lists.newArrayList("use", "usw"));
    assertEquals(admin.clusters().getCluster("usw"), new ClusterData("http://new-broker.messaging.usw.example.com" + ":" + BROKER_WEBSERVICE_PORT, "https://new-broker.messaging.usw.example.com" + ":" + BROKER_WEBSERVICE_PORT_TLS));
    admin.clusters().deleteCluster("usw");
    Thread.sleep(300);
    assertEquals(admin.clusters().getClusters(), Lists.newArrayList("use"));
    admin.namespaces().deleteNamespace("prop-xyz/use/ns1");
    admin.clusters().deleteCluster("use");
    assertEquals(admin.clusters().getClusters(), Lists.newArrayList());
    // Check name validation
    try {
        admin.clusters().createCluster("bf!", new ClusterData("http://dummy.messaging.example.com"));
        fail("should have failed");
    } catch (PulsarAdminException e) {
        assertTrue(e instanceof PreconditionFailedException);
    }
}
Also used : ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) PreconditionFailedException(com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

ClusterData (com.yahoo.pulsar.common.policies.data.ClusterData)29 PropertyAdmin (com.yahoo.pulsar.common.policies.data.PropertyAdmin)15 Test (org.testng.annotations.Test)13 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)8 URL (java.net.URL)8 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)7 PulsarService (com.yahoo.pulsar.broker.PulsarService)6 ServiceConfiguration (com.yahoo.pulsar.broker.ServiceConfiguration)6 PulsarAdmin (com.yahoo.pulsar.client.admin.PulsarAdmin)6 URI (java.net.URI)5 RestException (com.yahoo.pulsar.broker.web.RestException)4 AuthenticationTls (com.yahoo.pulsar.client.impl.auth.AuthenticationTls)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 KeeperException (org.apache.zookeeper.KeeperException)4 PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)3 AuthorizationManager (com.yahoo.pulsar.broker.authorization.AuthorizationManager)3 Authentication (com.yahoo.pulsar.client.api.Authentication)3 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)3 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)3