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"));
}
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();
}
}
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;
}
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");
}
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;
}
Aggregations