Search in sources :

Example 1 with PulsarResources

use of org.apache.pulsar.broker.resources.PulsarResources in project starlight-for-kafka by datastax.

the class KafkaServiceConfigurationTest method testAllowedNamespaces.

@Test
public void testAllowedNamespaces() throws Exception {
    final KafkaServiceConfiguration conf = new KafkaServiceConfiguration();
    assertEquals(conf.getKopAllowedNamespaces(), Collections.singletonList("${tenant}/default"));
    assertEquals(KafkaRequestHandler.expandAllowedNamespaces(conf.getKopAllowedNamespaces(), "my-tenant", null).get(), Collections.singletonList("my-tenant/default"));
    conf.setKafkaNamespace("my-ns");
    assertEquals(KafkaRequestHandler.expandAllowedNamespaces(conf.getKopAllowedNamespaces(), "my-tenant", null).get(), Collections.singletonList("my-tenant/my-ns"));
    conf.setKopAllowedNamespaces(Collections.singleton("my-tenant-2/my-ns-2"));
    assertEquals(KafkaRequestHandler.expandAllowedNamespaces(conf.getKopAllowedNamespaces(), "my-tenant", null).get(), Collections.singletonList("my-tenant-2/my-ns-2"));
    conf.setKopAllowedNamespaces(null);
    assertEquals(KafkaRequestHandler.expandAllowedNamespaces(conf.getKopAllowedNamespaces(), "my-tenant", null).get(), Collections.singletonList("my-tenant/my-ns"));
    conf.setKopAllowedNamespaces(Sets.newHashSet("my-tenant/my-ns-0", "my-tenant/my-ns-1"));
    assertEquals(KafkaRequestHandler.expandAllowedNamespaces(conf.getKopAllowedNamespaces(), "my-tenant", null).get(), Arrays.asList("my-tenant/my-ns-0", "my-tenant/my-ns-1"));
    conf.setKopAllowedNamespaces(Collections.singleton("${tenant}/*"));
    PulsarService pulsarService = mock(PulsarService.class);
    PulsarResources pulsarResources = mock(PulsarResources.class);
    NamespaceResources namespaceResources = mock(NamespaceResources.class);
    when(pulsarService.getPulsarResources()).thenReturn(pulsarResources);
    when(pulsarResources.getNamespaceResources()).thenReturn(namespaceResources);
    when(namespaceResources.getChildrenAsync(any(String.class))).thenReturn(CompletableFuture.completedFuture(Arrays.asList("one", "two")));
    assertEquals(KafkaRequestHandler.expandAllowedNamespaces(conf.getKopAllowedNamespaces(), "logged", pulsarService).get(), Arrays.asList("logged/one", "logged/two"));
}
Also used : NamespaceResources(org.apache.pulsar.broker.resources.NamespaceResources) PulsarService(org.apache.pulsar.broker.PulsarService) PulsarResources(org.apache.pulsar.broker.resources.PulsarResources) Test(org.testng.annotations.Test)

Example 2 with PulsarResources

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

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

the class PulsarInitialNamespaceSetup method doMain.

public static int doMain(String[] args) throws Exception {
    Arguments arguments = new Arguments();
    JCommander jcommander = new JCommander();
    try {
        jcommander.addObject(arguments);
        jcommander.parse(args);
        if (arguments.help) {
            jcommander.usage();
            return 0;
        }
        if (arguments.generateDocs) {
            CmdGenerateDocs cmd = new CmdGenerateDocs("pulsar");
            cmd.addCommand("initialize-namespace", arguments);
            cmd.run(null);
            return 0;
        }
    } catch (Exception e) {
        jcommander.usage();
        return 1;
    }
    if (arguments.configurationStore == null) {
        System.err.println("Configuration store address argument is required (--configuration-store)");
        jcommander.usage();
        return 1;
    }
    try (MetadataStore configStore = PulsarClusterMetadataSetup.initMetadataStore(arguments.configurationStore, arguments.zkSessionTimeoutMillis)) {
        PulsarResources pulsarResources = new PulsarResources(null, configStore);
        for (String namespace : arguments.namespaces) {
            NamespaceName namespaceName = null;
            try {
                namespaceName = NamespaceName.get(namespace);
            } catch (Exception e) {
                System.out.println("Invalid namespace name.");
                return 1;
            }
            // Create specified tenant
            PulsarClusterMetadataSetup.createTenantIfAbsent(pulsarResources, namespaceName.getTenant(), arguments.cluster);
            // Create specified namespace
            PulsarClusterMetadataSetup.createNamespaceIfAbsent(pulsarResources, namespaceName, arguments.cluster);
        }
    }
    System.out.println("Initial namespace setup success");
    return 0;
}
Also used : MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) PulsarResources(org.apache.pulsar.broker.resources.PulsarResources) JCommander(com.beust.jcommander.JCommander) CmdGenerateDocs(org.apache.pulsar.common.util.CmdGenerateDocs)

Example 4 with PulsarResources

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

the class PulsarTransactionCoordinatorMetadataSetup method main.

public static void main(String[] args) throws Exception {
    Arguments arguments = new Arguments();
    JCommander jcommander = new JCommander();
    try {
        jcommander.addObject(arguments);
        jcommander.parse(args);
        if (arguments.help) {
            jcommander.usage();
            return;
        }
        if (arguments.generateDocs) {
            CmdGenerateDocs cmd = new CmdGenerateDocs("pulsar");
            cmd.addCommand("initialize-transaction-coordinator-metadata", arguments);
            cmd.run(null);
            return;
        }
    } catch (Exception e) {
        jcommander.usage();
        throw e;
    }
    if (arguments.configurationStore == null) {
        System.err.println("Configuration store address argument is required (--configuration-store)");
        jcommander.usage();
        System.exit(1);
    }
    if (arguments.numTransactionCoordinators <= 0) {
        System.err.println("Number of transaction coordinators must greater than 0");
        System.exit(1);
    }
    try (MetadataStoreExtended configStore = PulsarClusterMetadataSetup.initMetadataStore(arguments.configurationStore, arguments.zkSessionTimeoutMillis)) {
        PulsarResources pulsarResources = new PulsarResources(null, configStore);
        // Create system tenant
        PulsarClusterMetadataSetup.createTenantIfAbsent(pulsarResources, NamespaceName.SYSTEM_NAMESPACE.getTenant(), arguments.cluster);
        // Create system namespace
        PulsarClusterMetadataSetup.createNamespaceIfAbsent(pulsarResources, NamespaceName.SYSTEM_NAMESPACE, arguments.cluster);
        // Create transaction coordinator assign partitioned topic
        PulsarClusterMetadataSetup.createPartitionedTopic(configStore, TopicName.TRANSACTION_COORDINATOR_ASSIGN, arguments.numTransactionCoordinators);
    }
    System.out.println("Transaction coordinator metadata setup success");
}
Also used : PulsarResources(org.apache.pulsar.broker.resources.PulsarResources) JCommander(com.beust.jcommander.JCommander) CmdGenerateDocs(org.apache.pulsar.common.util.CmdGenerateDocs) MetadataStoreExtended(org.apache.pulsar.metadata.api.extended.MetadataStoreExtended)

Example 5 with PulsarResources

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

the class Worker method getAuthorizationService.

private AuthorizationService getAuthorizationService() throws PulsarServerException {
    if (this.workerConfig.isAuthorizationEnabled()) {
        log.info("starting configuration cache service");
        try {
            configMetadataStore = PulsarResources.createMetadataStore(workerConfig.getConfigurationMetadataStoreUrl(), (int) workerConfig.getMetadataStoreSessionTimeoutMillis());
        } catch (IOException e) {
            throw new PulsarServerException(e);
        }
        pulsarResources = new PulsarResources(null, configMetadataStore);
        return new AuthorizationService(getServiceConfiguration(), this.pulsarResources);
    }
    return null;
}
Also used : PulsarServerException(org.apache.pulsar.broker.PulsarServerException) PulsarResources(org.apache.pulsar.broker.resources.PulsarResources) AuthorizationService(org.apache.pulsar.broker.authorization.AuthorizationService) IOException(java.io.IOException)

Aggregations

PulsarResources (org.apache.pulsar.broker.resources.PulsarResources)70 ZKMetadataStore (org.apache.pulsar.metadata.impl.ZKMetadataStore)32 NamespaceResources (org.apache.pulsar.broker.resources.NamespaceResources)31 BeforeMethod (org.testng.annotations.BeforeMethod)24 PulsarService (org.apache.pulsar.broker.PulsarService)23 IOException (java.io.IOException)18 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)18 MetadataStoreException (org.apache.pulsar.metadata.api.MetadataStoreException)18 JCommander (com.beust.jcommander.JCommander)15 CmdGenerateDocs (org.apache.pulsar.common.util.CmdGenerateDocs)15 MetadataStoreExtended (org.apache.pulsar.metadata.api.extended.MetadataStoreExtended)15 AuthenticationService (org.apache.pulsar.broker.authentication.AuthenticationService)14 Policies (org.apache.pulsar.common.policies.data.Policies)13 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)12 ExecutionException (java.util.concurrent.ExecutionException)12 AuthorizationService (org.apache.pulsar.broker.authorization.AuthorizationService)12 NamespaceService (org.apache.pulsar.broker.namespace.NamespaceService)12 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)12 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)11 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)10