Search in sources :

Example 21 with ClusterData

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

the class AdminTest method clusters.

@Test
void clusters() throws Exception {
    assertEquals(clusters.getClusters(), Lists.newArrayList());
    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(org.apache.pulsar.common.policies.data.ClusterData) AutoFailoverPolicyData(org.apache.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(org.apache.pulsar.common.policies.data.NamespaceIsolationData) RestException(org.apache.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 22 with ClusterData

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

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(org.apache.pulsar.common.policies.data.ClusterData) URI(java.net.URI) UriInfo(javax.ws.rs.core.UriInfo) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 23 with ClusterData

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

the class IncrementPartitionsTest 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 24 with ClusterData

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

the class PulsarWebResource method checkLocalOrGetPeerReplicationCluster.

protected static CompletableFuture<ClusterData> checkLocalOrGetPeerReplicationCluster(PulsarService pulsarService, NamespaceName namespace) {
    if (!namespace.isGlobal()) {
        return CompletableFuture.completedFuture(null);
    }
    final CompletableFuture<ClusterData> validationFuture = new CompletableFuture<>();
    final String localCluster = pulsarService.getConfiguration().getClusterName();
    final String path = AdminResource.path(POLICIES, namespace.toString());
    pulsarService.getConfigurationCache().policiesCache().getAsync(path).thenAccept(policiesResult -> {
        if (policiesResult.isPresent()) {
            Policies policies = policiesResult.get();
            if (policies.replication_clusters.isEmpty()) {
                String msg = String.format("Global namespace does not have any clusters configured : local_cluster=%s ns=%s", localCluster, namespace.toString());
                log.warn(msg);
                validationFuture.completeExceptionally(new RestException(Status.PRECONDITION_FAILED, msg));
            } else if (!policies.replication_clusters.contains(localCluster)) {
                ClusterData ownerPeerCluster = getOwnerFromPeerClusterList(pulsarService, policies.replication_clusters);
                if (ownerPeerCluster != null) {
                    // found a peer that own this namespace
                    validationFuture.complete(ownerPeerCluster);
                    return;
                }
                String msg = String.format("Global namespace missing local cluster name in replication list : local_cluster=%s ns=%s repl_clusters=%s", localCluster, namespace.toString(), policies.replication_clusters);
                log.warn(msg);
                validationFuture.completeExceptionally(new RestException(Status.PRECONDITION_FAILED, msg));
            } else {
                validationFuture.complete(null);
            }
        } else {
            String msg = String.format("Policies not found for %s namespace", namespace.toString());
            log.error(msg);
            validationFuture.completeExceptionally(new RestException(Status.NOT_FOUND, msg));
        }
    }).exceptionally(ex -> {
        String msg = String.format("Failed to validate global cluster configuration : cluster=%s ns=%s  emsg=%s", localCluster, namespace, ex.getMessage());
        log.error(msg);
        validationFuture.completeExceptionally(new RestException(ex));
        return null;
    });
    return validationFuture;
}
Also used : URL(java.net.URL) AdminResource(org.apache.pulsar.broker.admin.AdminResource) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) StringUtils(org.apache.commons.lang3.StringUtils) org.apache.pulsar.common.naming(org.apache.pulsar.common.naming) AuthenticationDataHttps(org.apache.pulsar.broker.authentication.AuthenticationDataHttps) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) HttpServletRequest(javax.servlet.http.HttpServletRequest) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) ZooKeeperCache.cacheTimeOutInSec(org.apache.pulsar.zookeeper.ZooKeeperCache.cacheTimeOutInSec) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) Status(javax.ws.rs.core.Response.Status) Splitter(com.google.common.base.Splitter) Context(javax.ws.rs.core.Context) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) KeeperException(org.apache.zookeeper.KeeperException) MalformedURLException(java.net.MalformedURLException) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) Range(com.google.common.collect.Range) Set(java.util.Set) PulsarService(org.apache.pulsar.broker.PulsarService) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) Sets(com.google.common.collect.Sets) Policies(org.apache.pulsar.common.policies.data.Policies) List(java.util.List) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) POLICIES(org.apache.pulsar.broker.cache.ConfigurationCacheService.POLICIES) Response(javax.ws.rs.core.Response) BoundType(com.google.common.collect.BoundType) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) ServletContext(javax.servlet.ServletContext) UriInfo(javax.ws.rs.core.UriInfo) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Joiner(com.google.common.base.Joiner) CompletableFuture(java.util.concurrent.CompletableFuture) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) Policies(org.apache.pulsar.common.policies.data.Policies)

Example 25 with ClusterData

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

the class ClusterDataTest method simple.

@Test
void simple() {
    String s1 = "http://broker.messaging.c1.example.com:8080";
    String s2 = "http://broker.messaging.c2.example.com:8080";
    String s3 = "https://broker.messaging.c1.example.com:4443";
    String s4 = "https://broker.messaging.c2.example.com:4443";
    ClusterData c = new ClusterData(s1);
    c.setServiceUrl(null);
    c.setServiceUrlTls(null);
    assertEquals(new ClusterData(s1), new ClusterData(s1));
    assertEquals(new ClusterData(s1).getServiceUrl(), s1);
    assertFalse(new ClusterData(s1).equals(new ClusterData()));
    assertFalse(new ClusterData(s1).equals(new ClusterData(s2)));
    assertFalse(new ClusterData(s1).equals(s1));
    assertEquals(new ClusterData(s1).hashCode(), new ClusterData(s1).hashCode());
    assertFalse(new ClusterData(s1).hashCode() == new ClusterData(s2).hashCode());
    assertFalse(new ClusterData(s1).hashCode() == c.hashCode());
    assertEquals(new ClusterData(s1, s3), new ClusterData(s1, s3));
    assertEquals(new ClusterData(s1, s3).getServiceUrl(), s1);
    assertEquals(new ClusterData(s1, s3).getServiceUrlTls(), s3);
    assertFalse(new ClusterData(s1, s3).equals(new ClusterData()));
    assertFalse(new ClusterData(s1, s3).equals(new ClusterData(s2, s4)));
    assertEquals(new ClusterData(s1, s3).hashCode(), new ClusterData(s1, s3).hashCode());
    assertFalse(new ClusterData(s1, s3).hashCode() == new ClusterData(s2, s4).hashCode());
    assertFalse(new ClusterData(s1, s3).hashCode() == new ClusterData(s1, s4).hashCode());
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) Test(org.testng.annotations.Test)

Aggregations

ClusterData (org.apache.pulsar.common.policies.data.ClusterData)53 PropertyAdmin (org.apache.pulsar.common.policies.data.PropertyAdmin)30 Test (org.testng.annotations.Test)23 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)13 BeforeMethod (org.testng.annotations.BeforeMethod)13 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)11 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)10 URL (java.net.URL)9 PulsarService (org.apache.pulsar.broker.PulsarService)9 RestException (org.apache.pulsar.broker.web.RestException)8 KeeperException (org.apache.zookeeper.KeeperException)8 IOException (java.io.IOException)7 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)7 URI (java.net.URI)6 AuthenticationTls (org.apache.pulsar.client.impl.auth.AuthenticationTls)6 Authentication (org.apache.pulsar.client.api.Authentication)5 Field (java.lang.reflect.Field)4 ExecutionException (java.util.concurrent.ExecutionException)4 AuthAction (org.apache.pulsar.common.policies.data.AuthAction)4 Policies (org.apache.pulsar.common.policies.data.Policies)4