use of org.apache.pulsar.zookeeper.ZookeeperBkClientFactoryImpl in project incubator-pulsar by apache.
the class ZooKeeperClientAspectJTest method testZkConnected.
@Test
public void testZkConnected() throws Exception {
OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
try {
ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl(executor);
CompletableFuture<ZooKeeper> zkFuture = zkf.create("127.0.0.1:" + LOCAL_ZOOKEEPER_PORT, SessionType.ReadWrite, (int) ZOOKEEPER_SESSION_TIMEOUT_MILLIS);
localZkc = zkFuture.get(ZOOKEEPER_SESSION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
assertTrue(localZkc.getState().isConnected());
assertNotEquals(localZkc.getState(), States.CONNECTEDREADONLY);
} finally {
if (localZkc != null) {
localZkc.close();
}
executor.shutdown();
}
}
use of org.apache.pulsar.zookeeper.ZookeeperBkClientFactoryImpl in project incubator-pulsar by apache.
the class ZooKeeperClientAspectJTest method testZkOpStatsMetrics.
/**
* Verifies that aspect-advice calculates the latency of of zk-operation and updates PulsarStats
*
* @throws Exception
*/
@Test(enabled = false, timeOut = 7000)
public void testZkOpStatsMetrics() throws Exception {
OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl(executor);
CompletableFuture<ZooKeeper> zkFuture = zkf.create("127.0.0.1:" + LOCAL_ZOOKEEPER_PORT, SessionType.ReadWrite, (int) ZOOKEEPER_SESSION_TIMEOUT_MILLIS);
localZkc = zkFuture.get(ZOOKEEPER_SESSION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
MockPulsar mockPulsar = new MockPulsar(localZkc);
mockPulsar.setup();
try {
PulsarClient pulsarClient = mockPulsar.getClient();
PulsarService pulsar = mockPulsar.getPulsar();
pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic1").create();
Metrics zkOpMetric = getMetric(pulsar, "zk_write_latency");
Assert.assertNotNull(zkOpMetric);
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_write_rate_s"));
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_write_time_95percentile_ms"));
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_write_time_99_99_percentile_ms"));
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_write_time_99_9_percentile_ms"));
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_write_time_99_percentile_ms"));
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_write_time_mean_ms"));
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_write_time_median_ms"));
zkOpMetric = getMetric(pulsar, "zk_read_latency");
Assert.assertNotNull(zkOpMetric);
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_read_rate_s"));
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_read_time_95percentile_ms"));
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_read_time_99_99_percentile_ms"));
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_read_time_99_9_percentile_ms"));
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_read_time_99_percentile_ms"));
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_read_time_mean_ms"));
Assert.assertTrue(zkOpMetric.getMetrics().containsKey("brk_zk_read_time_median_ms"));
CountDownLatch createLatch = new CountDownLatch(1);
CountDownLatch deleteLatch = new CountDownLatch(1);
CountDownLatch readLatch = new CountDownLatch(1);
CountDownLatch existLatch = new CountDownLatch(1);
localZkc.create("/createTest", "data".getBytes(), Acl, CreateMode.EPHEMERAL, (rc, path, ctx, name) -> {
createLatch.countDown();
}, "create");
localZkc.delete("/deleteTest", -1, (rc, path, ctx) -> {
deleteLatch.countDown();
}, "delete");
localZkc.exists("/createTest", null, (int rc, String path, Object ctx, Stat stat) -> {
existLatch.countDown();
}, null);
localZkc.getData("/createTest", null, (int rc, String path, Object ctx, byte[] data, Stat stat) -> {
readLatch.countDown();
}, null);
createLatch.await();
deleteLatch.await();
existLatch.await();
readLatch.await();
Thread.sleep(10);
BrokerService brokerService = pulsar.getBrokerService();
brokerService.updateRates();
List<Metrics> metrics = brokerService.getTopicMetrics();
AtomicDouble writeRate = new AtomicDouble();
AtomicDouble readRate = new AtomicDouble();
metrics.forEach(m -> {
if ("zk_write_latency".equalsIgnoreCase(m.getDimension("metric"))) {
writeRate.set((double) m.getMetrics().get("brk_zk_write_latency_rate_s"));
} else if ("zk_read_latency".equalsIgnoreCase(m.getDimension("metric"))) {
readRate.set((double) m.getMetrics().get("brk_zk_read_latency_rate_s"));
}
});
Assert.assertTrue(readRate.get() > 0);
Assert.assertTrue(writeRate.get() > 0);
} finally {
mockPulsar.cleanup();
if (localZkc != null) {
localZkc.close();
}
executor.shutdown();
}
}
use of org.apache.pulsar.zookeeper.ZookeeperBkClientFactoryImpl in project incubator-pulsar by apache.
the class CompactorTool method main.
public static void main(String[] args) throws Exception {
Arguments arguments = new Arguments();
JCommander jcommander = new JCommander(arguments);
jcommander.setProgramName("PulsarTopicCompactor");
// parse args by JCommander
jcommander.parse(args);
if (arguments.help) {
jcommander.usage();
System.exit(-1);
}
// init broker config
ServiceConfiguration brokerConfig;
if (isBlank(arguments.brokerConfigFile)) {
jcommander.usage();
throw new IllegalArgumentException("Need to specify a configuration file for broker");
} else {
brokerConfig = PulsarConfigurationLoader.create(arguments.brokerConfigFile, ServiceConfiguration.class);
}
String pulsarServiceUrl = PulsarService.brokerUrl(brokerConfig);
ClientConfiguration clientConfig = new ClientConfiguration();
if (isNotBlank(brokerConfig.getBrokerClientAuthenticationPlugin())) {
clientConfig.setAuthentication(brokerConfig.getBrokerClientAuthenticationPlugin(), brokerConfig.getBrokerClientAuthenticationParameters());
}
clientConfig.setUseTls(brokerConfig.isTlsEnabled());
clientConfig.setTlsAllowInsecureConnection(brokerConfig.isTlsAllowInsecureConnection());
clientConfig.setTlsTrustCertsFilePath(brokerConfig.getTlsCertificateFilePath());
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setNameFormat("compaction-%d").setDaemon(true).build());
OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
ZooKeeperClientFactory zkClientFactory = new ZookeeperBkClientFactoryImpl(executor);
ZooKeeper zk = zkClientFactory.create(brokerConfig.getZookeeperServers(), ZooKeeperClientFactory.SessionType.ReadWrite, (int) brokerConfig.getZooKeeperSessionTimeoutMillis()).get();
BookKeeperClientFactory bkClientFactory = new BookKeeperClientFactoryImpl();
BookKeeper bk = bkClientFactory.create(brokerConfig, zk);
try (PulsarClient pulsar = PulsarClient.create(pulsarServiceUrl, clientConfig)) {
Compactor compactor = new TwoPhaseCompactor(brokerConfig, pulsar, bk, scheduler);
long ledgerId = compactor.compact(arguments.topic).get();
log.info("Compaction of topic {} complete. Compacted to ledger {}", arguments.topic, ledgerId);
} finally {
bk.close();
bkClientFactory.close();
zk.close();
scheduler.shutdownNow();
executor.shutdown();
}
}
use of org.apache.pulsar.zookeeper.ZookeeperBkClientFactoryImpl in project incubator-pulsar by apache.
the class ZooKeeperClientAspectJTest method testZkClientAspectJTrigger.
/**
* Verifies that aspect-advice calculates the latency of of zk-operation
*
* @throws Exception
*/
@Test(enabled = false, timeOut = 7000)
void testZkClientAspectJTrigger() throws Exception {
OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl(executor);
CompletableFuture<ZooKeeper> zkFuture = zkf.create("127.0.0.1:" + LOCAL_ZOOKEEPER_PORT, SessionType.ReadWrite, (int) ZOOKEEPER_SESSION_TIMEOUT_MILLIS);
localZkc = zkFuture.get(ZOOKEEPER_SESSION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
try {
assertTrue(localZkc.getState().isConnected());
assertNotEquals(localZkc.getState(), States.CONNECTEDREADONLY);
final AtomicInteger writeCount = new AtomicInteger(0);
final AtomicInteger readCount = new AtomicInteger(0);
EventListner listener = new EventListner() {
@Override
public void recordLatency(EventType eventType, long latencyMiliSecond) {
if (eventType.equals(EventType.write)) {
writeCount.incrementAndGet();
} else if (eventType.equals(EventType.read)) {
readCount.incrementAndGet();
}
}
};
ClientCnxnAspect.addListener(listener);
CountDownLatch createLatch = new CountDownLatch(1);
CountDownLatch deleteLatch = new CountDownLatch(1);
CountDownLatch readLatch = new CountDownLatch(1);
CountDownLatch existLatch = new CountDownLatch(1);
localZkc.create("/createTest", "data".getBytes(), Acl, CreateMode.EPHEMERAL, (rc, path, ctx, name) -> {
createLatch.countDown();
}, "create");
localZkc.delete("/deleteTest", -1, (rc, path, ctx) -> {
deleteLatch.countDown();
}, "delete");
localZkc.exists("/createTest", null, (int rc, String path, Object ctx, Stat stat) -> {
existLatch.countDown();
}, null);
localZkc.getData("/createTest", null, (int rc, String path, Object ctx, byte[] data, Stat stat) -> {
readLatch.countDown();
}, null);
createLatch.await();
deleteLatch.await();
existLatch.await();
readLatch.await();
Thread.sleep(500);
Assert.assertEquals(writeCount.get(), 2);
Assert.assertEquals(readCount.get(), 2);
ClientCnxnAspect.removeListener(listener);
} finally {
if (localZkc != null) {
localZkc.close();
}
executor.shutdown();
}
}
Aggregations