use of org.apache.zookeeper.ZooKeeper in project incubator-pulsar by apache.
the class LoadSimulationController method handleCopy.
// Handle the command line arguments associated with the copy command.
private void handleCopy(final ShellArguments arguments) throws Exception {
final List<String> commandArguments = arguments.commandArguments;
// Copy accepts 3 application arguments: Tenant name, source ZooKeeper and target ZooKeeper connect strings.
if (checkAppArgs(commandArguments.size() - 1, 3)) {
final String tenantName = commandArguments.get(1);
final String sourceZKConnectString = commandArguments.get(2);
final String targetZKConnectString = commandArguments.get(3);
final ZooKeeper sourceZKClient = new ZooKeeper(sourceZKConnectString, 5000, null);
final ZooKeeper targetZKClient = new ZooKeeper(targetZKConnectString, 5000, null);
// Make a map for each thread to speed up the ZooKeeper writing process.
final Map<String, ResourceQuota>[] threadLocalMaps = new Map[clients.length];
for (int i = 0; i < clients.length; ++i) {
threadLocalMaps[i] = new HashMap<>();
}
getResourceQuotas(QUOTA_ROOT, sourceZKClient, threadLocalMaps);
final List<Future> futures = new ArrayList<>(clients.length);
int i = 0;
log.info("Copying...");
for (final Map<String, ResourceQuota> bundleToQuota : threadLocalMaps) {
final int j = i;
futures.add(threadPool.submit(() -> {
for (final Map.Entry<String, ResourceQuota> entry : bundleToQuota.entrySet()) {
final String bundle = entry.getKey();
final ResourceQuota quota = entry.getValue();
// Simulation will send messages in and out at about the same rate, so just make the rate the
// average of in and out.
final int tenantStart = QUOTA_ROOT.length() + 1;
final int clusterStart = bundle.indexOf('/', tenantStart) + 1;
final String sourceTenant = bundle.substring(tenantStart, clusterStart - 1);
final int namespaceStart = bundle.indexOf('/', clusterStart) + 1;
final String sourceCluster = bundle.substring(clusterStart, namespaceStart - 1);
final String namespace = bundle.substring(namespaceStart, bundle.lastIndexOf('/'));
final String keyRangeString = bundle.substring(bundle.lastIndexOf('/') + 1);
// To prevent duplicate node issues for same namespace names in different clusters/tenants.
final String manglePrefix = String.format("%s-%s-%s", sourceCluster, sourceTenant, keyRangeString);
final String mangledNamespace = String.format("%s-%s", manglePrefix, namespace);
final BundleData bundleData = initializeBundleData(quota, arguments);
final String oldAPITargetPath = String.format("/loadbalance/resource-quota/namespace/%s/%s/%s/0x00000000_0xffffffff", tenantName, cluster, mangledNamespace);
final String newAPITargetPath = String.format("/loadbalance/bundle-data/%s/%s/%s/0x00000000_0xffffffff", tenantName, cluster, mangledNamespace);
try {
ZkUtils.createFullPathOptimistic(targetZKClient, oldAPITargetPath, ObjectMapperFactory.getThreadLocal().writeValueAsBytes(quota), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException.NodeExistsException e) {
// Ignore already created nodes.
} catch (Exception e) {
throw new RuntimeException(e);
}
// Put the bundle data in the new ZooKeeper.
try {
ZkUtils.createFullPathOptimistic(targetZKClient, newAPITargetPath, bundleData.getJsonBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException.NodeExistsException e) {
// Ignore already created nodes.
} catch (Exception e) {
throw new RuntimeException(e);
}
try {
trade(arguments, makeTopic(tenantName, mangledNamespace, "t"), j);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}));
++i;
}
for (final Future future : futures) {
future.get();
}
sourceZKClient.close();
targetZKClient.close();
}
}
use of org.apache.zookeeper.ZooKeeper in project incubator-pulsar by apache.
the class GlobalZooKeeperCache method start.
public void start() throws IOException {
CompletableFuture<ZooKeeper> zkFuture = zlClientFactory.create(globalZkConnect, SessionType.AllowReadOnly, zkSessionTimeoutMillis);
// Initial session creation with global ZK must work
try {
ZooKeeper newSession = zkFuture.get(10, TimeUnit.SECONDS);
// Register self as a watcher to receive notification when session expires and trigger a new session to be
// created
newSession.register(this);
zkSession.set(newSession);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
LOG.error("Failed to establish global zookeeper session: {}", e.getMessage(), e);
throw new IOException(e);
}
}
use of org.apache.zookeeper.ZooKeeper in project incubator-pulsar by apache.
the class LocalBookkeeperEnsemble method initializeZookeper.
private void initializeZookeper() throws IOException {
LOG.info("Instantiate ZK Client");
// initialize the zk client with values
try {
ZKConnectionWatcher zkConnectionWatcher = new ZKConnectionWatcher();
zkc = new ZooKeeper(HOSTPORT, zkSessionTimeOut, zkConnectionWatcher);
zkConnectionWatcher.waitForConnection();
if (zkc.exists("/ledgers", false) == null) {
zkc.create("/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
if (zkc.exists("/ledgers/available", false) == null) {
zkc.create("/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
if (zkc.exists("/ledgers/available/readonly", false) == null) {
zkc.create("/ledgers/available/readonly", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
// No need to create an entry for each requested bookie anymore as the
// BookieServers will register themselves with ZooKeeper on startup.
} catch (KeeperException e) {
LOG.error("Exception while creating znodes", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.error("Interrupted while creating znodes", e);
}
}
use of org.apache.zookeeper.ZooKeeper in project incubator-pulsar by apache.
the class LocalZooKeeperConnectionService method start.
public void start(ShutdownService shutdownService) throws IOException {
// Connect to local ZK
CompletableFuture<ZooKeeper> zkFuture = zkClientFactory.create(zkConnect, SessionType.ReadWrite, (int) zkSessionTimeoutMillis);
try {
localZooKeeper = zkFuture.get(zkSessionTimeoutMillis, TimeUnit.MILLISECONDS);
localZooKeeperSessionWatcher = new ZooKeeperSessionWatcher(localZooKeeper, zkSessionTimeoutMillis, shutdownService);
localZooKeeperSessionWatcher.start();
localZooKeeper.register(localZooKeeperSessionWatcher);
} catch (Exception e) {
throw new IOException("Failed to establish session with local ZK", e);
}
}
use of org.apache.zookeeper.ZooKeeper in project incubator-pulsar by apache.
the class ZookeeperClientFactoryImplTest method testZKCreationRW.
@Test
void testZKCreationRW() throws Exception {
ZooKeeperClientFactory zkf = new ZookeeperClientFactoryImpl();
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);
localZkc.close();
}
Aggregations