use of com.yahoo.pulsar.client.admin.PulsarAdminException.ConflictException in project pulsar by yahoo.
the class PulsarBrokerStatsClientTest method testServiceException.
@Test
public void testServiceException() throws Exception {
URL url = new URL("http://localhost:15000");
PulsarAdmin admin = new PulsarAdmin(url, (Authentication) null);
BrokerStatsImpl client = (BrokerStatsImpl) spy(admin.brokerStats());
try {
client.getLoadReport();
} catch (PulsarAdminException e) {
// Ok
}
try {
client.getPendingBookieOpsStats();
} catch (PulsarAdminException e) {
// Ok
}
try {
client.getBrokerResourceAvailability("prop", "cluster", "ns");
} catch (PulsarAdminException e) {
// Ok
}
assertTrue(client.getApiException(new ClientErrorException(403)) instanceof NotAuthorizedException);
assertTrue(client.getApiException(new ClientErrorException(404)) instanceof NotFoundException);
assertTrue(client.getApiException(new ClientErrorException(409)) instanceof ConflictException);
assertTrue(client.getApiException(new ClientErrorException(412)) instanceof PreconditionFailedException);
assertTrue(client.getApiException(new ClientErrorException(400)) instanceof PulsarAdminException);
assertTrue(client.getApiException(new ServerErrorException(500)) instanceof ServerSideErrorException);
assertTrue(client.getApiException(new ServerErrorException(503)) instanceof PulsarAdminException);
log.info("Client: ", client);
admin.close();
}
use of com.yahoo.pulsar.client.admin.PulsarAdminException.ConflictException in project pulsar by yahoo.
the class AdminApiTest method partitionedTopics.
@Test(dataProvider = "topicName")
public void partitionedTopics(String topicName) throws Exception {
final String partitionedTopicName = "persistent://prop-xyz/use/ns1/" + topicName;
admin.persistentTopics().createPartitionedTopic(partitionedTopicName, 4);
assertEquals(admin.persistentTopics().getPartitionedTopicMetadata(partitionedTopicName).partitions, 4);
// check if the virtual topic doesn't get created
List<String> destinations = admin.persistentTopics().getList("prop-xyz/use/ns1");
assertEquals(destinations.size(), 0);
assertEquals(admin.persistentTopics().getPartitionedTopicMetadata("persistent://prop-xyz/use/ns1/ds2").partitions, 0);
// create consumer and subscription
URL pulsarUrl = new URL("http://127.0.0.1" + ":" + BROKER_WEBSERVICE_PORT);
ClientConfiguration clientConf = new ClientConfiguration();
clientConf.setStatsInterval(0, TimeUnit.SECONDS);
PulsarClient client = PulsarClient.create(pulsarUrl.toString(), clientConf);
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = client.subscribe(partitionedTopicName, "my-sub", conf);
assertEquals(admin.persistentTopics().getSubscriptions(partitionedTopicName), Lists.newArrayList("my-sub"));
Consumer consumer1 = client.subscribe(partitionedTopicName, "my-sub-1", conf);
assertEquals(Sets.newHashSet(admin.persistentTopics().getSubscriptions(partitionedTopicName)), Sets.newHashSet("my-sub", "my-sub-1"));
consumer1.close();
admin.persistentTopics().deleteSubscription(partitionedTopicName, "my-sub-1");
assertEquals(admin.persistentTopics().getSubscriptions(partitionedTopicName), Lists.newArrayList("my-sub"));
ProducerConfiguration prodConf = new ProducerConfiguration();
prodConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
Producer producer = client.createProducer(partitionedTopicName, prodConf);
for (int i = 0; i < 10; i++) {
String message = "message-" + i;
producer.send(message.getBytes());
}
assertEquals(Sets.newHashSet(admin.persistentTopics().getList("prop-xyz/use/ns1")), Sets.newHashSet(partitionedTopicName + "-partition-0", partitionedTopicName + "-partition-1", partitionedTopicName + "-partition-2", partitionedTopicName + "-partition-3"));
// test cumulative stats for partitioned topic
PartitionedTopicStats topicStats = admin.persistentTopics().getPartitionedStats(partitionedTopicName, false);
assertEquals(topicStats.subscriptions.keySet(), Sets.newTreeSet(Lists.newArrayList("my-sub")));
assertEquals(topicStats.subscriptions.get("my-sub").consumers.size(), 1);
assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 10);
assertEquals(topicStats.publishers.size(), 1);
assertEquals(topicStats.partitions, Maps.newHashMap());
// test per partition stats for partitioned topic
topicStats = admin.persistentTopics().getPartitionedStats(partitionedTopicName, true);
assertEquals(topicStats.metadata.partitions, 4);
assertEquals(topicStats.partitions.keySet(), Sets.newHashSet(partitionedTopicName + "-partition-0", partitionedTopicName + "-partition-1", partitionedTopicName + "-partition-2", partitionedTopicName + "-partition-3"));
PersistentTopicStats partitionStats = topicStats.partitions.get(partitionedTopicName + "-partition-0");
assertEquals(partitionStats.publishers.size(), 1);
assertEquals(partitionStats.subscriptions.get("my-sub").consumers.size(), 1);
assertEquals(partitionStats.subscriptions.get("my-sub").msgBacklog, 3, 1);
try {
admin.persistentTopics().skipMessages(partitionedTopicName, "my-sub", 5);
fail("skip messages for partitioned topics should fail");
} catch (Exception e) {
// ok
}
admin.persistentTopics().skipAllMessages(partitionedTopicName, "my-sub");
topicStats = admin.persistentTopics().getPartitionedStats(partitionedTopicName, false);
assertEquals(topicStats.subscriptions.get("my-sub").msgBacklog, 0);
producer.close();
consumer.close();
admin.persistentTopics().deleteSubscription(partitionedTopicName, "my-sub");
assertEquals(admin.persistentTopics().getSubscriptions(partitionedTopicName), Lists.newArrayList());
try {
admin.persistentTopics().createPartitionedTopic(partitionedTopicName, 32);
fail("Should have failed as the partitioned topic already exists");
} catch (ConflictException ce) {
}
producer = client.createProducer(partitionedTopicName);
destinations = admin.persistentTopics().getList("prop-xyz/use/ns1");
assertEquals(destinations.size(), 4);
try {
admin.persistentTopics().deletePartitionedTopic(partitionedTopicName);
fail("The topic is busy");
} catch (PreconditionFailedException pfe) {
// ok
}
producer.close();
client.close();
admin.persistentTopics().deletePartitionedTopic(partitionedTopicName);
assertEquals(admin.persistentTopics().getPartitionedTopicMetadata(partitionedTopicName).partitions, 0);
admin.persistentTopics().createPartitionedTopic(partitionedTopicName, 32);
assertEquals(admin.persistentTopics().getPartitionedTopicMetadata(partitionedTopicName).partitions, 32);
try {
admin.persistentTopics().deletePartitionedTopic("persistent://prop-xyz/use/ns1/ds2");
fail("Should have failed as the partitioned topic was not created");
} catch (NotFoundException nfe) {
}
admin.persistentTopics().deletePartitionedTopic(partitionedTopicName);
// delete a partitioned topic in a global namespace
admin.persistentTopics().createPartitionedTopic(partitionedTopicName, 4);
admin.persistentTopics().deletePartitionedTopic(partitionedTopicName);
}
use of com.yahoo.pulsar.client.admin.PulsarAdminException.ConflictException in project pulsar by yahoo.
the class WebServiceTest method setupEnv.
private void setupEnv(boolean enableFilter, String minApiVersion, boolean allowUnversionedClients, boolean enableTls, boolean enableAuth, boolean allowInsecure) throws Exception {
Set<String> providers = new HashSet<>();
providers.add("com.yahoo.pulsar.broker.authentication.AuthenticationProviderTls");
Set<String> roles = new HashSet<>();
roles.add("client");
ServiceConfiguration config = new ServiceConfiguration();
config.setWebServicePort(BROKER_WEBSERVICE_PORT);
config.setWebServicePortTls(BROKER_WEBSERVICE_PORT_TLS);
config.setClientLibraryVersionCheckEnabled(enableFilter);
config.setAuthenticationEnabled(enableAuth);
config.setAuthenticationProviders(providers);
config.setAuthorizationEnabled(false);
config.setClientLibraryVersionCheckAllowUnversioned(allowUnversionedClients);
config.setSuperUserRoles(roles);
config.setTlsEnabled(enableTls);
config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
config.setTlsAllowInsecureConnection(allowInsecure);
config.setTlsTrustCertsFilePath(allowInsecure ? "" : TLS_CLIENT_CERT_FILE_PATH);
config.setClusterName("local");
// TLS certificate expects localhost
config.setAdvertisedAddress("localhost");
pulsar = spy(new PulsarService(config));
doReturn(new MockedZooKeeperClientFactoryImpl()).when(pulsar).getZooKeeperClientFactory();
pulsar.start();
try {
pulsar.getZkClient().delete("/minApiVersion", -1);
} catch (Exception ex) {
}
pulsar.getZkClient().create("/minApiVersion", minApiVersion.getBytes(), null, CreateMode.PERSISTENT);
String serviceUrl = BROKER_URL_BASE;
ClientConfiguration clientConfig = new ClientConfiguration();
if (enableTls && enableAuth) {
serviceUrl = BROKER_URL_BASE_TLS;
Map<String, String> authParams = new HashMap<>();
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
Authentication auth = new AuthenticationTls();
auth.configure(authParams);
clientConfig.setAuthentication(auth);
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
}
PulsarAdmin pulsarAdmin = new PulsarAdmin(new URL(serviceUrl), clientConfig);
try {
pulsarAdmin.clusters().createCluster(config.getClusterName(), new ClusterData(pulsar.getWebServiceAddress()));
} catch (ConflictException ce) {
// This is OK.
} finally {
pulsarAdmin.close();
}
}
Aggregations