Search in sources :

Example 1 with TenantInfoImpl

use of org.apache.pulsar.common.policies.data.TenantInfoImpl in project pulsar by apache.

the class PulsarClusterMetadataSetup method createTenantIfAbsent.

static void createTenantIfAbsent(PulsarResources resources, String tenant, String cluster) throws IOException, InterruptedException, ExecutionException {
    TenantResources tenantResources = resources.getTenantResources();
    if (!tenantResources.tenantExists(tenant)) {
        TenantInfoImpl publicTenant = new TenantInfoImpl(Collections.emptySet(), Collections.singleton(cluster));
        tenantResources.createTenant(tenant, publicTenant);
    } else {
        // Update existing public tenant with new cluster
        tenantResources.updateTenantAsync(tenant, ti -> {
            ti.getAllowedClusters().add(cluster);
            return ti;
        }).get();
    }
}
Also used : TopicName(org.apache.pulsar.common.naming.TopicName) PulsarResources(org.apache.pulsar.broker.resources.PulsarResources) Parameter(com.beust.jcommander.Parameter) LoggerFactory(org.slf4j.LoggerFactory) MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) MetadataStoreExtended(org.apache.pulsar.metadata.api.extended.MetadataStoreExtended) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) MetadataStoreLifecycle(org.apache.pulsar.metadata.api.MetadataStoreLifecycle) MetadataStoreFactoryImpl(org.apache.pulsar.metadata.impl.MetadataStoreFactoryImpl) BookKeeperConstants(org.apache.bookkeeper.util.BookKeeperConstants) PoliciesUtil.getBundles(org.apache.pulsar.common.policies.data.PoliciesUtil.getBundles) BookieRackAffinityMapping(org.apache.pulsar.bookie.rackawareness.BookieRackAffinityMapping) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) PulsarMetadataBookieDriver(org.apache.pulsar.metadata.bookkeeper.PulsarMetadataBookieDriver) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) CmdGenerateDocs(org.apache.pulsar.common.util.CmdGenerateDocs) Logger(org.slf4j.Logger) WorkerUtils(org.apache.pulsar.functions.worker.WorkerUtils) JCommander(com.beust.jcommander.JCommander) PulsarMetadataClientDriver(org.apache.pulsar.metadata.bookkeeper.PulsarMetadataClientDriver) ZKMetadataStore(org.apache.pulsar.metadata.impl.ZKMetadataStore) IOException(java.io.IOException) MetadataStoreConfig(org.apache.pulsar.metadata.api.MetadataStoreConfig) ZkClusterInitializer(org.apache.bookkeeper.stream.storage.impl.cluster.ZkClusterInitializer) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) ExecutionException(java.util.concurrent.ExecutionException) ServiceURI(org.apache.bookkeeper.common.net.ServiceURI) Policies(org.apache.pulsar.common.policies.data.Policies) TenantResources(org.apache.pulsar.broker.resources.TenantResources) InternalConfigurationData(org.apache.pulsar.common.conf.InternalConfigurationData) Optional(java.util.Optional) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) NamespaceResources(org.apache.pulsar.broker.resources.NamespaceResources) ClusterInitializer(org.apache.bookkeeper.stream.storage.api.cluster.ClusterInitializer) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) Collections(java.util.Collections) TenantResources(org.apache.pulsar.broker.resources.TenantResources) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl)

Example 2 with TenantInfoImpl

use of org.apache.pulsar.common.policies.data.TenantInfoImpl in project pulsar by apache.

the class PulsarStandalone method createSampleNameSpace.

private void createSampleNameSpace(ClusterData clusterData, String cluster) {
    // Create a sample namespace
    final String tenant = "sample";
    final String globalCluster = "global";
    final String namespace = tenant + "/ns1";
    try {
        List<String> clusters = admin.clusters().getClusters();
        if (!clusters.contains(cluster)) {
            admin.clusters().createCluster(cluster, clusterData);
        } else {
            admin.clusters().updateCluster(cluster, clusterData);
        }
        // Create marker for "global" cluster
        if (!clusters.contains(globalCluster)) {
            admin.clusters().createCluster(globalCluster, ClusterData.builder().build());
        }
        if (!admin.tenants().getTenants().contains(tenant)) {
            admin.tenants().createTenant(tenant, new TenantInfoImpl(Sets.newHashSet(config.getSuperUserRoles()), Sets.newHashSet(cluster)));
        }
        if (!admin.namespaces().getNamespaces(tenant).contains(namespace)) {
            admin.namespaces().createNamespace(namespace);
        }
    } catch (PulsarAdminException e) {
        log.warn(e.getMessage(), e);
    }
}
Also used : PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl)

Example 3 with TenantInfoImpl

use of org.apache.pulsar.common.policies.data.TenantInfoImpl in project pulsar by apache.

the class NamespacesTest method testSubscribeRate.

@Test
public void testSubscribeRate() throws Exception {
    SubscribeRate subscribeRate = new SubscribeRate(1, 5);
    String namespace = "my-tenants/my-namespace";
    admin.tenants().createTenant("my-tenants", new TenantInfoImpl(Sets.newHashSet(), Sets.newHashSet(testLocalCluster)));
    admin.namespaces().createNamespace(namespace, Sets.newHashSet(testLocalCluster));
    admin.namespaces().setSubscribeRate(namespace, subscribeRate);
    assertEquals(subscribeRate, admin.namespaces().getSubscribeRate(namespace));
    String topicName = "persistent://" + namespace + "/" + "subscribe-rate";
    admin.topics().createPartitionedTopic(topicName, 2);
    pulsar.getConfiguration().setAuthorizationEnabled(false);
    Consumer<?> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionType(SubscriptionType.Shared).subscriptionName("subscribe-rate").subscribe();
    assertTrue(consumer.isConnected());
    // Subscribe Rate Limiter is enabled, will limited by broker
    pulsarClient.updateServiceUrl(lookupUrl.toString());
    Awaitility.await().untilAsserted(() -> assertFalse(consumer.isConnected()));
    // Out of limit period
    pulsarClient.updateServiceUrl(lookupUrl.toString());
    Awaitility.await().pollDelay(Duration.ofSeconds(6)).untilAsserted(() -> assertTrue(consumer.isConnected()));
    // Disable Subscribe Rate Limiter
    subscribeRate = new SubscribeRate(0, 10);
    admin.namespaces().setSubscribeRate(namespace, subscribeRate);
    pulsarClient.updateServiceUrl(lookupUrl.toString());
    Awaitility.await().untilAsserted(() -> assertTrue(consumer.isConnected()));
    pulsar.getConfiguration().setAuthorizationEnabled(true);
    admin.topics().deletePartitionedTopic(topicName, true);
    admin.namespaces().deleteNamespace(namespace);
    admin.tenants().deleteTenant("my-tenants");
}
Also used : SubscribeRate(org.apache.pulsar.common.policies.data.SubscribeRate) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 4 with TenantInfoImpl

use of org.apache.pulsar.common.policies.data.TenantInfoImpl in project pulsar by apache.

the class NamespacesTest method setup.

@Override
@BeforeMethod
public void setup() throws Exception {
    resetConfig();
    conf.setClusterName(testLocalCluster);
    super.internalSetup();
    namespaces = spy(Namespaces.class);
    namespaces.setServletContext(new MockServletContext());
    namespaces.setPulsar(pulsar);
    doReturn(false).when(namespaces).isRequestHttps();
    doReturn("test").when(namespaces).clientAppId();
    doReturn(null).when(namespaces).originalPrincipal();
    doReturn(null).when(namespaces).clientAuthData();
    doReturn(Sets.newTreeSet(Lists.newArrayList("use", "usw", "usc", "global"))).when(namespaces).clusters();
    admin.clusters().createCluster("use", ClusterData.builder().serviceUrl("http://broker-use.com:8080").build());
    admin.clusters().createCluster("usw", ClusterData.builder().serviceUrl("http://broker-usw.com:8080").build());
    admin.clusters().createCluster("usc", ClusterData.builder().serviceUrl("http://broker-usc.com:8080").build());
    admin.tenants().createTenant(this.testTenant, new TenantInfoImpl(Sets.newHashSet("role1", "role2"), Sets.newHashSet("use", "usc", "usw")));
    admin.tenants().createTenant(this.testOtherTenant, new TenantInfoImpl(Sets.newHashSet("role3", "role4"), Sets.newHashSet("use", "usc", "usw")));
    createTestNamespaces(this.testLocalNamespaces, BundlesData.builder().build());
    createGlobalTestNamespaces(this.testTenant, this.testGlobalNamespaces.get(0).getLocalName(), BundlesData.builder().build());
    doThrow(new RestException(Status.UNAUTHORIZED, "unauthorized")).when(namespaces).validateTenantOperation(this.testOtherTenant, null);
    doThrow(new RestException(Status.UNAUTHORIZED, "unauthorized")).when(namespaces).validateNamespacePolicyOperation(NamespaceName.get("other-tenant/use/test-namespace-1"), PolicyName.PERSISTENCE, PolicyOperation.WRITE);
    doThrow(new RestException(Status.UNAUTHORIZED, "unauthorized")).when(namespaces).validateNamespacePolicyOperation(NamespaceName.get("other-tenant/use/test-namespace-1"), PolicyName.RETENTION, PolicyOperation.WRITE);
    nsSvc = pulsar.getNamespaceService();
}
Also used : Namespaces(org.apache.pulsar.broker.admin.v1.Namespaces) RestException(org.apache.pulsar.broker.web.RestException) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 5 with TenantInfoImpl

use of org.apache.pulsar.common.policies.data.TenantInfoImpl in project pulsar by apache.

the class PersistentTopicsTest method testGetLastMessageId.

@Test
public void testGetLastMessageId() throws Exception {
    TenantInfoImpl tenantInfo = new TenantInfoImpl(Sets.newHashSet("role1", "role2"), Sets.newHashSet("test"));
    admin.tenants().createTenant("prop-xyz", tenantInfo);
    admin.namespaces().createNamespace("prop-xyz/ns1", Sets.newHashSet("test"));
    final String topicName = "persistent://prop-xyz/ns1/testGetLastMessageId";
    admin.topics().createNonPartitionedTopic(topicName);
    @Cleanup Producer<byte[]> batchProducer = pulsarClient.newProducer().topic(topicName).enableBatching(true).batchingMaxMessages(100).batchingMaxPublishDelay(2, TimeUnit.SECONDS).create();
    admin.topics().createSubscription(topicName, "test", MessageId.earliest);
    CompletableFuture<MessageId> completableFuture = new CompletableFuture<>();
    for (int i = 0; i < 10; i++) {
        completableFuture = batchProducer.sendAsync("test".getBytes());
    }
    completableFuture.get();
    Assert.assertEquals(((BatchMessageIdImpl) admin.topics().getLastMessageId(topicName)).getBatchIndex(), 9);
    @Cleanup Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).enableBatching(false).create();
    producer.send("test".getBytes());
    Assert.assertTrue(admin.topics().getLastMessageId(topicName) instanceof MessageIdImpl);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Cleanup(lombok.Cleanup) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

TenantInfoImpl (org.apache.pulsar.common.policies.data.TenantInfoImpl)690 Test (org.testng.annotations.Test)458 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)211 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)146 BeforeMethod (org.testng.annotations.BeforeMethod)144 Cleanup (lombok.Cleanup)139 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)136 PulsarClient (org.apache.pulsar.client.api.PulsarClient)91 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)72 HashSet (java.util.HashSet)55 HashMap (java.util.HashMap)49 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)48 CompletableFuture (java.util.concurrent.CompletableFuture)41 List (java.util.List)38 PulsarService (org.apache.pulsar.broker.PulsarService)37 AuthAction (org.apache.pulsar.common.policies.data.AuthAction)36 ArrayList (java.util.ArrayList)33 WebTarget (javax.ws.rs.client.WebTarget)33 Policies (org.apache.pulsar.common.policies.data.Policies)33 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)31