use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.
the class BrokerServiceTest method testTlsEnabled.
@Test
public void testTlsEnabled() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/newTopic";
final String subName = "newSub";
ClientConfiguration clientConfig;
ConsumerConfiguration consumerConfig;
Consumer consumer;
conf.setAuthenticationEnabled(false);
conf.setTlsEnabled(true);
conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
restartBroker();
// Case 1: Access without TLS
PulsarClient pulsarClient = null;
try {
clientConfig = new ClientConfiguration();
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrl.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
} catch (Exception e) {
fail("should not fail");
} finally {
pulsarClient.close();
}
// Case 2: Access with TLS (Allow insecure TLS connection)
try {
clientConfig = new ClientConfiguration();
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
} catch (Exception e) {
fail("should not fail");
} finally {
pulsarClient.close();
}
// Case 3: Access with TLS (Disallow insecure TLS connection)
try {
clientConfig = new ClientConfiguration();
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(false);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
fail("should fail");
} catch (Exception e) {
assertTrue(e.getMessage().contains("General SSLEngine problem"));
} finally {
pulsarClient.close();
}
// Case 4: Access with TLS (Use trusted certificates)
try {
clientConfig = new ClientConfiguration();
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(false);
clientConfig.setTlsTrustCertsFilePath(TLS_SERVER_CERT_FILE_PATH);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
} catch (Exception e) {
fail("should not fail");
} finally {
pulsarClient.close();
}
}
use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.
the class BrokerServiceTest method testTlsAuthUseTrustCert.
@Test
public void testTlsAuthUseTrustCert() throws Exception {
final String topicName = "persistent://prop/usw/my-ns/newTopic";
final String subName = "newSub";
ClientConfiguration clientConfig;
ConsumerConfiguration consumerConfig;
Consumer consumer;
Authentication auth;
Set<String> providers = new HashSet<>();
providers.add("com.yahoo.pulsar.broker.authentication.AuthenticationProviderTls");
conf.setAuthenticationEnabled(true);
conf.setAuthenticationProviders(providers);
conf.setTlsEnabled(true);
conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
conf.setTlsAllowInsecureConnection(false);
conf.setTlsTrustCertsFilePath(TLS_CLIENT_CERT_FILE_PATH);
restartBroker();
Map<String, String> authParams = new HashMap<>();
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
PulsarClient pulsarClient = null;
// Case 1: Access without client certificate
try {
clientConfig = new ClientConfiguration();
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
fail("should fail");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Authentication required"));
} finally {
pulsarClient.close();
}
// Case 2: Access with client certificate
try {
auth = new AuthenticationTls();
auth.configure(authParams);
clientConfig = new ClientConfiguration();
clientConfig.setAuthentication(auth);
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
} catch (Exception e) {
fail("should not fail");
} finally {
pulsarClient.close();
}
}
use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.
the class BrokerServiceTest method testLookupThrottlingForClientByClient.
/**
* Verifies: client side throttling.
*
* @throws Exception
*/
@Test
public void testLookupThrottlingForClientByClient() throws Exception {
final String topicName = "persistent://prop/usw/my-ns/newTopic";
com.yahoo.pulsar.client.api.ClientConfiguration clientConf = new com.yahoo.pulsar.client.api.ClientConfiguration();
clientConf.setStatsInterval(0, TimeUnit.SECONDS);
clientConf.setConcurrentLookupRequest(0);
String lookupUrl = new URI("pulsar://localhost:" + BROKER_PORT).toString();
PulsarClient pulsarClient = PulsarClient.create(lookupUrl, clientConf);
try {
Consumer consumer = pulsarClient.subscribe(topicName, "mysub", new ConsumerConfiguration());
fail("It should fail as throttling should not receive any request");
} catch (com.yahoo.pulsar.client.api.PulsarClientException.TooManyLookupRequestException e) {
// ok as throttling set to 0
}
}
use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.
the class BrokerServiceThrottlingTest method testLookupThrottlingForClientByBroker0Permit.
/**
* Broker has maxConcurrentLookupRequest = 0 so, it rejects incoming lookup request and it cause consumer creation
* failure.
*
* @throws Exception
*/
@Test
public void testLookupThrottlingForClientByBroker0Permit() throws Exception {
// create configuration znode
ZkUtils.createFullPathOptimistic(mockZookKeeper, BROKER_SERVICE_CONFIGURATION_PATH, "{}".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// Now, znode is created: set the watch and listener on the znode
setWatchOnThrottlingZnode();
final String topicName = "persistent://prop/usw/my-ns/newTopic";
com.yahoo.pulsar.client.api.ClientConfiguration clientConf = new com.yahoo.pulsar.client.api.ClientConfiguration();
clientConf.setStatsInterval(0, TimeUnit.SECONDS);
String lookupUrl = new URI("pulsar://localhost:" + BROKER_PORT).toString();
PulsarClient pulsarClient = PulsarClient.create(lookupUrl, clientConf);
ConsumerConfiguration consumerConfig = new ConsumerConfiguration();
Consumer consumer = pulsarClient.subscribe(topicName, "mysub", consumerConfig);
consumer.close();
int newPermits = 0;
admin.brokers().updateDynamicConfiguration("maxConcurrentLookupRequest", Integer.toString(newPermits));
// wait config to be updated
for (int i = 0; i < 5; i++) {
if (pulsar.getConfiguration().getMaxConcurrentLookupRequest() != newPermits) {
Thread.sleep(100 + (i * 10));
} else {
break;
}
}
try {
consumer = pulsarClient.subscribe(topicName, "mysub", consumerConfig);
consumer.close();
fail("It should fail as throttling should not receive any request");
} catch (com.yahoo.pulsar.client.api.PulsarClientException.TooManyLookupRequestException e) {
// ok as throttling set to 0
}
}
use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.
the class BrokerServiceThrottlingTest method testLookupThrottlingForClientByBrokerInternalRetry.
/**
* This testcase make sure that once consumer lost connection with broker, it always reconnects with broker by
* retrying on throttling-error exception also.
*
* <pre>
* 1. all consumers get connected
* 2. broker restarts with maxConcurrentLookupRequest = 1
* 3. consumers reconnect and some get TooManyRequestException and again retries
* 4. eventually all consumers will successfully connect to broker
* </pre>
*
* @throws Exception
*/
@Test
public void testLookupThrottlingForClientByBrokerInternalRetry() throws Exception {
// create configuration znode
ZkUtils.createFullPathOptimistic(mockZookKeeper, BROKER_SERVICE_CONFIGURATION_PATH, "{}".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// Now, znode is created: set the watch and listener on the znode
setWatchOnThrottlingZnode();
final String topicName = "persistent://prop/usw/my-ns/newTopic";
com.yahoo.pulsar.client.api.ClientConfiguration clientConf = new com.yahoo.pulsar.client.api.ClientConfiguration();
clientConf.setStatsInterval(0, TimeUnit.SECONDS);
clientConf.setIoThreads(20);
clientConf.setConnectionsPerBroker(20);
String lookupUrl = new URI("pulsar://localhost:" + BROKER_PORT).toString();
PulsarClient pulsarClient = PulsarClient.create(lookupUrl, clientConf);
upsertLookupPermits(100);
ConsumerConfiguration consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Shared);
List<Consumer> consumers = Lists.newArrayList();
ExecutorService executor = Executors.newFixedThreadPool(10);
final int totalConsumers = 8;
CountDownLatch latch = new CountDownLatch(totalConsumers);
for (int i = 0; i < totalConsumers; i++) {
executor.execute(() -> {
try {
consumers.add(pulsarClient.subscribe(topicName, "mysub", consumerConfig));
} catch (PulsarClientException.TooManyLookupRequestException e) {
// ok
} catch (Exception e) {
fail("it shouldn't failed");
}
latch.countDown();
});
}
latch.await();
stopBroker();
conf.setMaxConcurrentLookupRequest(1);
startBroker();
// wait strategically for all consumers to reconnect
for (int i = 0; i < 5; i++) {
if (!areAllConsumersConnected(consumers)) {
Thread.sleep(1000 + (i * 500));
} else {
break;
}
}
int totalConnectedConsumers = 0;
for (int i = 0; i < consumers.size(); i++) {
if (((ConsumerImpl) consumers.get(i)).isConnected()) {
totalConnectedConsumers++;
}
consumers.get(i).close();
}
assertEquals(totalConnectedConsumers, totalConsumers);
pulsarClient.close();
}
Aggregations