Search in sources :

Example 1 with TenantResources

use of org.apache.pulsar.broker.resources.TenantResources 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 TenantResources

use of org.apache.pulsar.broker.resources.TenantResources in project pulsar by apache.

the class ClusterMetadataSetupTest method testInitialNamespaceSetup.

@Test
public void testInitialNamespaceSetup() throws Exception {
    // missing arguments
    assertEquals(PulsarInitialNamespaceSetup.doMain(new String[] {}), 1);
    // invalid namespace
    assertEquals(PulsarInitialNamespaceSetup.doMain(new String[] { "--cluster", "testInitialNamespaceSetup-cluster", "--configuration-store", "127.0.0.1:" + localZkS.getZookeeperPort(), "a/b/c/d" }), 1);
    String[] args = { "--cluster", "testInitialNamespaceSetup-cluster", "--configuration-store", "127.0.0.1:" + localZkS.getZookeeperPort(), "test/a", "test/b", "test/c" };
    assertEquals(PulsarInitialNamespaceSetup.doMain(args), 0);
    try (MetadataStoreExtended store = MetadataStoreExtended.create("127.0.0.1:" + localZkS.getZookeeperPort(), MetadataStoreConfig.builder().build())) {
        TenantResources tenantResources = new TenantResources(store, PulsarResources.DEFAULT_OPERATION_TIMEOUT_SEC);
        List<String> namespaces = tenantResources.getListOfNamespaces("test");
        assertEquals(new HashSet<>(namespaces), new HashSet<>(Arrays.asList("test/a", "test/b", "test/c")));
    }
}
Also used : TenantResources(org.apache.pulsar.broker.resources.TenantResources) MetadataStoreExtended(org.apache.pulsar.metadata.api.extended.MetadataStoreExtended) Test(org.testng.annotations.Test) ZookeeperServerTest(org.apache.pulsar.zookeeper.ZookeeperServerTest)

Example 3 with TenantResources

use of org.apache.pulsar.broker.resources.TenantResources in project pulsar by yahoo.

the class PulsarStandalone method createNameSpace.

@VisibleForTesting
void createNameSpace(String cluster, String publicTenant, NamespaceName ns) throws Exception {
    ClusterResources cr = broker.getPulsarResources().getClusterResources();
    TenantResources tr = broker.getPulsarResources().getTenantResources();
    NamespaceResources nsr = broker.getPulsarResources().getNamespaceResources();
    if (!cr.clusterExists(cluster)) {
        cr.createCluster(cluster, ClusterData.builder().serviceUrl(broker.getWebServiceAddress()).serviceUrlTls(broker.getWebServiceAddressTls()).brokerServiceUrl(broker.getBrokerServiceUrl()).brokerServiceUrlTls(broker.getBrokerServiceUrlTls()).build());
    }
    if (!tr.tenantExists(publicTenant)) {
        tr.createTenant(publicTenant, TenantInfo.builder().adminRoles(Sets.newHashSet(config.getSuperUserRoles())).allowedClusters(Sets.newHashSet(cluster)).build());
    }
    if (!nsr.namespaceExists(ns)) {
        Policies nsp = new Policies();
        nsp.replication_clusters = Collections.singleton(config.getClusterName());
        nsr.createPolicies(ns, nsp);
    }
}
Also used : NamespaceResources(org.apache.pulsar.broker.resources.NamespaceResources) Policies(org.apache.pulsar.common.policies.data.Policies) ClusterResources(org.apache.pulsar.broker.resources.ClusterResources) TenantResources(org.apache.pulsar.broker.resources.TenantResources) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with TenantResources

use of org.apache.pulsar.broker.resources.TenantResources in project pulsar by yahoo.

the class PulsarStandaloneTest method testCreateNameSpace.

@Test
public void testCreateNameSpace() throws Exception {
    final String cluster = "cluster1";
    final String tenant = "tenant1";
    final NamespaceName ns = NamespaceName.get(tenant, "ns1");
    ClusterResources cr = mock(ClusterResources.class);
    when(cr.clusterExists(cluster)).thenReturn(false).thenReturn(true);
    doNothing().when(cr).createCluster(eq(cluster), any());
    TenantResources tr = mock(TenantResources.class);
    when(tr.tenantExists(tenant)).thenReturn(false).thenReturn(true);
    doNothing().when(tr).createTenant(eq(tenant), any());
    NamespaceResources nsr = mock(NamespaceResources.class);
    when(nsr.namespaceExists(ns)).thenReturn(false).thenReturn(true);
    doNothing().when(nsr).createPolicies(eq(ns), any());
    PulsarResources resources = mock(PulsarResources.class);
    when(resources.getClusterResources()).thenReturn(cr);
    when(resources.getTenantResources()).thenReturn(tr);
    when(resources.getNamespaceResources()).thenReturn(nsr);
    PulsarService broker = mock(PulsarService.class);
    when(broker.getPulsarResources()).thenReturn(resources);
    when(broker.getWebServiceAddress()).thenReturn("pulsar://localhost:8080");
    when(broker.getWebServiceAddressTls()).thenReturn(null);
    when(broker.getBrokerServiceUrl()).thenReturn("pulsar://localhost:6650");
    when(broker.getBrokerServiceUrlTls()).thenReturn(null);
    ServiceConfiguration config = new ServiceConfiguration();
    config.setClusterName(cluster);
    PulsarStandalone standalone = new PulsarStandalone();
    standalone.setBroker(broker);
    standalone.setConfig(config);
    standalone.createNameSpace(cluster, tenant, ns);
    standalone.createNameSpace(cluster, tenant, ns);
    verify(cr, times(1)).createCluster(eq(cluster), any());
    verify(tr, times(1)).createTenant(eq(tenant), any());
    verify(nsr, times(1)).createPolicies(eq(ns), any());
}
Also used : NamespaceResources(org.apache.pulsar.broker.resources.NamespaceResources) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) PulsarService(org.apache.pulsar.broker.PulsarService) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PulsarResources(org.apache.pulsar.broker.resources.PulsarResources) ClusterResources(org.apache.pulsar.broker.resources.ClusterResources) TenantResources(org.apache.pulsar.broker.resources.TenantResources) Test(org.testng.annotations.Test)

Example 5 with TenantResources

use of org.apache.pulsar.broker.resources.TenantResources in project pulsar by yahoo.

the class ClusterMetadataSetupTest method testInitialNamespaceSetup.

@Test
public void testInitialNamespaceSetup() throws Exception {
    // missing arguments
    assertEquals(PulsarInitialNamespaceSetup.doMain(new String[] {}), 1);
    // invalid namespace
    assertEquals(PulsarInitialNamespaceSetup.doMain(new String[] { "--cluster", "testInitialNamespaceSetup-cluster", "--configuration-store", "127.0.0.1:" + localZkS.getZookeeperPort(), "a/b/c/d" }), 1);
    String[] args = { "--cluster", "testInitialNamespaceSetup-cluster", "--configuration-store", "127.0.0.1:" + localZkS.getZookeeperPort(), "test/a", "test/b", "test/c" };
    assertEquals(PulsarInitialNamespaceSetup.doMain(args), 0);
    try (MetadataStoreExtended store = MetadataStoreExtended.create("127.0.0.1:" + localZkS.getZookeeperPort(), MetadataStoreConfig.builder().build())) {
        TenantResources tenantResources = new TenantResources(store, PulsarResources.DEFAULT_OPERATION_TIMEOUT_SEC);
        List<String> namespaces = tenantResources.getListOfNamespaces("test");
        assertEquals(new HashSet<>(namespaces), new HashSet<>(Arrays.asList("test/a", "test/b", "test/c")));
    }
}
Also used : TenantResources(org.apache.pulsar.broker.resources.TenantResources) MetadataStoreExtended(org.apache.pulsar.metadata.api.extended.MetadataStoreExtended) Test(org.testng.annotations.Test) ZookeeperServerTest(org.apache.pulsar.zookeeper.ZookeeperServerTest)

Aggregations

TenantResources (org.apache.pulsar.broker.resources.TenantResources)10 NamespaceResources (org.apache.pulsar.broker.resources.NamespaceResources)7 MetadataStoreExtended (org.apache.pulsar.metadata.api.extended.MetadataStoreExtended)6 PulsarResources (org.apache.pulsar.broker.resources.PulsarResources)5 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)5 Policies (org.apache.pulsar.common.policies.data.Policies)5 Test (org.testng.annotations.Test)5 ClusterResources (org.apache.pulsar.broker.resources.ClusterResources)4 JCommander (com.beust.jcommander.JCommander)3 Parameter (com.beust.jcommander.Parameter)3 IOException (java.io.IOException)3 Collections (java.util.Collections)3 Optional (java.util.Optional)3 ExecutionException (java.util.concurrent.ExecutionException)3 BookKeeperAdmin (org.apache.bookkeeper.client.BookKeeperAdmin)3 ServiceURI (org.apache.bookkeeper.common.net.ServiceURI)3 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)3 ClusterInitializer (org.apache.bookkeeper.stream.storage.api.cluster.ClusterInitializer)3 ZkClusterInitializer (org.apache.bookkeeper.stream.storage.impl.cluster.ZkClusterInitializer)3 BookKeeperConstants (org.apache.bookkeeper.util.BookKeeperConstants)3