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