use of com.yahoo.pulsar.client.admin.PulsarAdminException in project pulsar by yahoo.
the class MessageIdTest method partitionedProducerSendAsync.
@Test(timeOut = 10000)
public void partitionedProducerSendAsync() throws PulsarClientException, PulsarAdminException {
// 1. Basic Config
String key = "partitionedProducerSendAsync";
final String topicName = "persistent://prop/cluster/namespace/topic-" + key;
final String subscriptionName = "my-subscription-" + key;
final String messagePredicate = "my-message-" + key + "-";
final int numberOfMessages = 30;
int numberOfPartitions = 3;
admin.persistentTopics().createPartitionedTopic(topicName, numberOfPartitions);
// 2. Create Producer
Producer producer = pulsarClient.createProducer(topicName);
// 3. Create Consumer
Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName);
// 4. Publish message and get message id
Set<MessageId> messageIds = new HashSet();
Set<Future<MessageId>> futures = new HashSet();
for (int i = 0; i < numberOfMessages; i++) {
String message = messagePredicate + i;
futures.add(producer.sendAsync(message.getBytes()));
}
futures.forEach(f -> {
try {
messageIds.add(f.get());
} catch (Exception e) {
Assert.fail("Failed to publish message, Exception: " + e.getMessage());
}
});
// 4. Check if message Ids are correct
log.info("Message IDs = " + messageIds);
Assert.assertEquals(messageIds.size(), numberOfMessages, "Not all messages published successfully");
for (int i = 0; i < numberOfMessages; i++) {
MessageId messageId = consumer.receive().getMessageId();
log.info("Message ID Received = " + messageId);
Assert.assertTrue(messageIds.remove(messageId), "Failed to receive Message");
}
log.info("Message IDs = " + messageIds);
Assert.assertEquals(messageIds.size(), 0, "Not all messages received successfully");
consumer.unsubscribe();
}
use of com.yahoo.pulsar.client.admin.PulsarAdminException in project pulsar by yahoo.
the class PulsarStandaloneStarter method start.
void start() throws Exception {
if (config == null) {
System.exit(1);
}
log.debug("--- setup PulsarStandaloneStarter ---");
if (!onlyBroker) {
// Start LocalBookKeeper
bkEnsemble = new LocalBookkeeperEnsemble(numOfBk, zkPort, bkPort, zkDir, bkDir, wipeData);
bkEnsemble.start();
}
if (noBroker) {
return;
}
// Start Broker
broker = new PulsarService(config);
broker.start();
// Create a sample namespace
URL webServiceUrl = new URL(String.format("http://%s:%d", config.getAdvertisedAddress(), config.getWebServicePort()));
String brokerServiceUrl = String.format("pulsar://%s:%d", config.getAdvertisedAddress(), config.getBrokerServicePort());
admin = new PulsarAdmin(webServiceUrl, config.getBrokerClientAuthenticationPlugin(), config.getBrokerClientAuthenticationParameters());
String property = "sample";
String cluster = config.getClusterName();
String namespace = property + "/" + cluster + "/ns1";
try {
ClusterData clusterData = new ClusterData(webServiceUrl.toString(), null, /* serviceUrlTls */
brokerServiceUrl, null);
if (!admin.clusters().getClusters().contains(cluster)) {
admin.clusters().createCluster(cluster, clusterData);
} else {
admin.clusters().updateCluster(cluster, clusterData);
}
if (!admin.properties().getProperties().contains(property)) {
admin.properties().createProperty(property, new PropertyAdmin(Lists.newArrayList(config.getSuperUserRoles()), Sets.newHashSet(cluster)));
}
if (!admin.namespaces().getNamespaces(property).contains(namespace)) {
admin.namespaces().createNamespace(namespace);
}
} catch (PulsarAdminException e) {
log.info(e.getMessage());
}
log.debug("--- setup completed ---");
}
use of com.yahoo.pulsar.client.admin.PulsarAdminException in project pulsar by yahoo.
the class AdminApiTest method clusterNamespaceIsolationPolicies.
@Test
public void clusterNamespaceIsolationPolicies() throws PulsarAdminException {
try {
// create
String policyName1 = "policy-1";
NamespaceIsolationData nsPolicyData1 = new NamespaceIsolationData();
nsPolicyData1.namespaces = new ArrayList<String>();
nsPolicyData1.namespaces.add("other/use/other.*");
nsPolicyData1.primary = new ArrayList<String>();
nsPolicyData1.primary.add("prod1-broker[4-6].messaging.use.example.com");
nsPolicyData1.secondary = new ArrayList<String>();
nsPolicyData1.secondary.add("prod1-broker.*.messaging.use.example.com");
nsPolicyData1.auto_failover_policy = new AutoFailoverPolicyData();
nsPolicyData1.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
nsPolicyData1.auto_failover_policy.parameters = new HashMap<String, String>();
nsPolicyData1.auto_failover_policy.parameters.put("min_limit", "1");
nsPolicyData1.auto_failover_policy.parameters.put("usage_threshold", "100");
admin.clusters().createNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
String policyName2 = "policy-2";
NamespaceIsolationData nsPolicyData2 = new NamespaceIsolationData();
nsPolicyData2.namespaces = new ArrayList<String>();
nsPolicyData2.namespaces.add("other/use/other.*");
nsPolicyData2.primary = new ArrayList<String>();
nsPolicyData2.primary.add("prod1-broker[4-6].messaging.use.example.com");
nsPolicyData2.secondary = new ArrayList<String>();
nsPolicyData2.secondary.add("prod1-broker.*.messaging.use.example.com");
nsPolicyData2.auto_failover_policy = new AutoFailoverPolicyData();
nsPolicyData2.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
nsPolicyData2.auto_failover_policy.parameters = new HashMap<String, String>();
nsPolicyData2.auto_failover_policy.parameters.put("min_limit", "1");
nsPolicyData2.auto_failover_policy.parameters.put("usage_threshold", "100");
admin.clusters().createNamespaceIsolationPolicy("use", policyName2, nsPolicyData2);
// verify create indirectly with get
Map<String, NamespaceIsolationData> policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
assertEquals(policiesMap.get(policyName1), nsPolicyData1);
assertEquals(policiesMap.get(policyName2), nsPolicyData2);
// verify update of primary
nsPolicyData1.primary.remove(0);
nsPolicyData1.primary.add("prod1-broker[1-2].messaging.use.example.com");
admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
// verify primary change
policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
assertEquals(policiesMap.get(policyName1), nsPolicyData1);
// verify update of secondary
nsPolicyData1.secondary.remove(0);
nsPolicyData1.secondary.add("prod1-broker[3-4].messaging.use.example.com");
admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
// verify secondary change
policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
assertEquals(policiesMap.get(policyName1), nsPolicyData1);
// verify update of failover policy limit
nsPolicyData1.auto_failover_policy.parameters.put("min_limit", "10");
admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
// verify min_limit change
policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
assertEquals(policiesMap.get(policyName1), nsPolicyData1);
// verify update of failover usage_threshold limit
nsPolicyData1.auto_failover_policy.parameters.put("usage_threshold", "80");
admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
// verify usage_threshold change
policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
assertEquals(policiesMap.get(policyName1), nsPolicyData1);
// verify single get
NamespaceIsolationData policy1Data = admin.clusters().getNamespaceIsolationPolicy("use", policyName1);
assertEquals(policy1Data, nsPolicyData1);
// verify creation of more than one policy
admin.clusters().createNamespaceIsolationPolicy("use", policyName2, nsPolicyData1);
try {
admin.clusters().getNamespaceIsolationPolicy("use", "no-such-policy");
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof NotFoundException);
}
// verify delete cluster failed
try {
admin.clusters().deleteCluster("use");
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof PreconditionFailedException);
}
// verify delete
admin.clusters().deleteNamespaceIsolationPolicy("use", policyName1);
admin.clusters().deleteNamespaceIsolationPolicy("use", policyName2);
try {
admin.clusters().getNamespaceIsolationPolicy("use", policyName1);
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof NotFoundException);
}
try {
admin.clusters().getNamespaceIsolationPolicy("use", policyName2);
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof NotFoundException);
}
try {
admin.clusters().getNamespaceIsolationPolicies("usc");
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof NotFoundException);
}
try {
admin.clusters().getNamespaceIsolationPolicy("usc", "no-such-cluster");
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof PreconditionFailedException);
}
try {
admin.clusters().createNamespaceIsolationPolicy("usc", "no-such-cluster", nsPolicyData1);
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof PreconditionFailedException);
}
try {
admin.clusters().updateNamespaceIsolationPolicy("usc", "no-such-cluster", policy1Data);
fail("should have raised exception");
} catch (PulsarAdminException e) {
assertTrue(e instanceof PreconditionFailedException);
}
} catch (PulsarAdminException e) {
LOG.warn("TEST FAILED [{}]", e.getMessage());
throw e;
}
}
use of com.yahoo.pulsar.client.admin.PulsarAdminException in project pulsar by yahoo.
the class AdminApiTest method testNamespaceUnloadBundle.
@Test
public void testNamespaceUnloadBundle() throws Exception {
assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
// Force to create a destination
publishMessagesOnPersistentTopic("persistent://prop-xyz/use/ns1/ds2", 0);
assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList("persistent://prop-xyz/use/ns1/ds2"));
// create consumer and subscription
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = pulsarClient.subscribe("persistent://prop-xyz/use/ns1/ds2", "my-sub", conf);
assertEquals(admin.persistentTopics().getSubscriptions("persistent://prop-xyz/use/ns1/ds2"), Lists.newArrayList("my-sub"));
// Create producer
Producer producer = pulsarClient.createProducer("persistent://prop-xyz/use/ns1/ds2");
for (int i = 0; i < 10; i++) {
String message = "message-" + i;
producer.send(message.getBytes());
}
consumer.close();
producer.close();
try {
admin.namespaces().unloadNamespaceBundle("prop-xyz/use/ns1", "0x00000000_0xffffffff");
} catch (Exception e) {
fail("Unload shouldn't have throw exception");
}
// check that no one owns the namespace
NamespaceBundle bundle = bundleFactory.getBundle(new NamespaceName("prop-xyz/use/ns1"), Range.range(0L, BoundType.CLOSED, 0xffffffffL, BoundType.CLOSED));
assertFalse(pulsar.getNamespaceService().isServiceUnitOwned(bundle));
assertFalse(otherPulsar.getNamespaceService().isServiceUnitOwned(bundle));
pulsarClient.shutdown();
LOG.info("--- RELOAD ---");
// Force reload of namespace and wait for topic to be ready
for (int i = 0; i < 30; i++) {
try {
admin.persistentTopics().getStats("persistent://prop-xyz/use/ns1/ds2");
break;
} catch (PulsarAdminException e) {
LOG.warn("Failed to get topic stats.. {}", e.getMessage());
Thread.sleep(1000);
}
}
admin.persistentTopics().deleteSubscription("persistent://prop-xyz/use/ns1/ds2", "my-sub");
admin.persistentTopics().delete("persistent://prop-xyz/use/ns1/ds2");
}
use of com.yahoo.pulsar.client.admin.PulsarAdminException in project pulsar by yahoo.
the class AdminApiTest method properties.
@Test(enabled = true)
public void properties() throws PulsarAdminException {
Set<String> allowedClusters = Sets.newHashSet("use");
PropertyAdmin propertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "role2"), allowedClusters);
admin.properties().updateProperty("prop-xyz", propertyAdmin);
assertEquals(admin.properties().getProperties(), Lists.newArrayList("prop-xyz"));
assertEquals(admin.properties().getPropertyAdmin("prop-xyz"), propertyAdmin);
PropertyAdmin newPropertyAdmin = new PropertyAdmin(Lists.newArrayList("role3", "role4"), allowedClusters);
admin.properties().updateProperty("prop-xyz", newPropertyAdmin);
assertEquals(admin.properties().getPropertyAdmin("prop-xyz"), newPropertyAdmin);
admin.namespaces().deleteNamespace("prop-xyz/use/ns1");
admin.properties().deleteProperty("prop-xyz");
assertEquals(admin.properties().getProperties(), Lists.newArrayList());
// Check name validation
try {
admin.properties().createProperty("prop-xyz&", propertyAdmin);
fail("should have failed");
} catch (PulsarAdminException e) {
assertTrue(e instanceof PreconditionFailedException);
}
}
Aggregations