@Override public void delete(final String path, final int version) throws KeeperException { assertClusterIdFlagTrue(); final String namespace = emptyToNull(client.getNamespace()); final String namespacedPath = ZKPaths.fixForNamespace(namespace, path); try { client.getZookeeperClient().getZooKeeper().delete(namespacedPath, version); } catch (Exception e) { throwIfInstanceOf(e, KeeperException.class); throw new RuntimeException(e); } }
nullNsFw.newNamespaceAwareEnsurePath("/" + zkClient.getNamespace()); try { ensureNs.ensure(nullNsFw.getZookeeperClient());
@Override public String getNamespace() { return inner.getNamespace(); }
public static CuratorFramework usingChildNamespace(CuratorFramework curator, String... children) { String path = curator.getNamespace(); for (String child : children) { path = path.length() > 0 ? ZKPaths.makePath(path, child) : child; } // Curator does not allow namespaces to start with a leading '/' if (path.startsWith("/")) { path = path.substring(1); } return curator.usingNamespace(path); } }
/** * Creates a new instance of the BookKeeperLogFactory class. * * @param config The configuration to use for all instances created. * @param zkClient ZooKeeper Client to use. * @param executor An executor to use for async operations. */ public BookKeeperLogFactory(BookKeeperConfig config, CuratorFramework zkClient, ScheduledExecutorService executor) { this.config = Preconditions.checkNotNull(config, "config"); this.executor = Preconditions.checkNotNull(executor, "executor"); this.namespace = zkClient.getNamespace(); this.zkClient = Preconditions.checkNotNull(zkClient, "zkClient") .usingNamespace(this.namespace + this.config.getZkMetadataPath()); this.bookKeeper = new AtomicReference<>(); }
/** * Performs a blocking sync operation. Returns true if the sync completed normally, false if it timed out or * was interrupted. */ public static boolean synchronousSync(CuratorFramework curator, Duration timeout) { try { // Curator sync() is always a background operation. Use a latch to block until it finishes. final CountDownLatch latch = new CountDownLatch(1); curator.sync().inBackground(new BackgroundCallback() { @Override public void processResult(CuratorFramework curator, CuratorEvent event) throws Exception { if (event.getType() == CuratorEventType.SYNC) { latch.countDown(); } } }).forPath(curator.getNamespace().isEmpty() ? "/" : ""); // Wait for sync to complete. return latch.await(timeout.toMillis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { return false; } catch (Exception e) { throw Throwables.propagate(e); } } }
/** * Performs a blocking sync operation. Returns true if the sync completed normally, false if it timed out or * was interrupted. */ public static boolean synchronousSync(CuratorFramework curator, Duration timeout) { try { // Curator sync() is always a background operation. Use a latch to block until it finishes. final CountDownLatch latch = new CountDownLatch(1); curator.sync().inBackground(new BackgroundCallback() { @Override public void processResult(CuratorFramework curator, CuratorEvent event) throws Exception { if (event.getType() == CuratorEventType.SYNC) { latch.countDown(); } } }).forPath(curator.getNamespace().isEmpty() ? "/" : ""); // Wait for sync to complete. return latch.await(timeout.toMillis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { return false; } catch (Exception e) { throw Throwables.propagate(e); } } }
/** * Loads the metadata for the current log, as stored in ZooKeeper. * * @return A new LogMetadata object with the desired information, or null if no such node exists. * @throws DataLogInitializationException If an Exception (other than NoNodeException) occurred. */ @VisibleForTesting LogMetadata loadMetadata() throws DataLogInitializationException { try { Stat storingStatIn = new Stat(); byte[] serializedMetadata = this.zkClient.getData().storingStatIn(storingStatIn).forPath(this.logNodePath); LogMetadata result = LogMetadata.SERIALIZER.deserialize(serializedMetadata); result.withUpdateVersion(storingStatIn.getVersion()); return result; } catch (KeeperException.NoNodeException nne) { // Node does not exist: this is the first time we are accessing this log. log.warn("{}: No ZNode found for path '{}{}'. This is OK if this is the first time accessing this log.", this.traceObjectId, this.zkClient.getNamespace(), this.logNodePath); return null; } catch (Exception ex) { throw new DataLogInitializationException(String.format("Unable to load ZNode contents for path '%s%s'.", this.zkClient.getNamespace(), this.logNodePath), ex); } }
/** * Checks that the {@link CuratorFramework} instance is started and that the configured root * namespace exists. * * @return {@link Result#unhealthy(String)} if the {@link CuratorFramework} is not started or * the configured root namespace does not exist; otherwise, {@link Result#healthy()}. * @throws Exception if an error occurs checking the health of the ZooKeeper ensemble. */ @Override protected Result check() throws Exception { final String namespace = framework.getNamespace(); if (framework.getState() != CuratorFrameworkState.STARTED) { return Result.unhealthy("Client not started"); } else if (framework.checkExists().forPath(namespace.isEmpty() ? "/" : "") == null) { return Result.unhealthy("Root for namespace does not exist"); } return Result.healthy(); } }
final String namespace = emptyToNull(client.getNamespace()); final String namespacedPath = ZKPaths.fixForNamespace(namespace, path);
@Override public void delete(final String path, final int version) throws KeeperException { assertClusterIdFlagTrue(); final String namespace = emptyToNull(client.getNamespace()); final String namespacedPath = ZKPaths.fixForNamespace(namespace, path); try { client.getZookeeperClient().getZooKeeper().delete(namespacedPath, version); } catch (Exception e) { propagateIfInstanceOf(e, KeeperException.class); throw propagate(e); } }
String recursiveDeletePath = ZKPaths.makePath(curatorClient.getNamespace(), path); ZKUtil.deleteRecursive(curatorClient.getZookeeperClient().getZooKeeper(), recursiveDeletePath); } catch (Exception e) {
String.format("Unable to acquire exclusive write lock for log (path = '%s%s').", this.zkClient.getNamespace(), this.logNodePath), keeperEx); } catch (Exception generalEx) { String.format("Unable to update ZNode for path '%s%s'.", this.zkClient.getNamespace(), this.logNodePath), generalEx);
private void checkAndSetACLs() throws Exception { if (zkSecure && !aclChecked) { // If znodes were previously created without security enabled, and now it is, we need to go through all existing znodes // and set the ACLs for them. This is done just once at the startup // We can't get the namespace znode through curator; have to go through zk client startCuratorFramework(); String namespace = "/" + curatorFramework.getNamespace(); if (curatorFramework.getZookeeperClient().getZooKeeper().exists(namespace, null) != null) { List<ACL> acls = curatorFramework.getZookeeperClient().getZooKeeper().getACL(namespace, new Stat()); if (acls.isEmpty() || !acls.get(0).getId().getScheme().equals("sasl")) { LOGGER.info("'sasl' ACLs not set; setting..."); List<String> children = curatorFramework.getZookeeperClient().getZooKeeper().getChildren(namespace, null); for (String child : children) { checkAndSetACLs("/" + child); } curatorFramework.getZookeeperClient().getZooKeeper().setACL(namespace, saslACL, -1); } } aclChecked = true; } }
private void checkAndSetACLs() throws Exception { if (Services.get().getConf().getBoolean(ZK_SECURE, false)) { // If znodes were previously created without security enabled, and now it is, we need to go through all existing znodes // and set the ACLs for them // We can't get the namespace znode through curator; have to go through zk client String namespace = "/" + client.getNamespace(); if (client.getZookeeperClient().getZooKeeper().exists(namespace, null) != null) { List<ACL> acls = client.getZookeeperClient().getZooKeeper().getACL(namespace, new Stat()); if (!acls.get(0).getId().getScheme().equals("sasl")) { log.info("'sasl' ACLs not set; setting..."); List<String> children = client.getZookeeperClient().getZooKeeper().getChildren(namespace, null); for (String child : children) { checkAndSetACLs("/" + child); } client.getZookeeperClient().getZooKeeper().setACL(namespace, saslACL, -1); } } } }
private void checkAndSetACLs() throws Exception { if (Services.get().getConf().getBoolean(ZK_SECURE, false)) { // If znodes were previously created without security enabled, and now it is, we need to go through all existing znodes // and set the ACLs for them // We can't get the namespace znode through curator; have to go through zk client String namespace = "/" + client.getNamespace(); if (client.getZookeeperClient().getZooKeeper().exists(namespace, null) != null) { List<ACL> acls = client.getZookeeperClient().getZooKeeper().getACL(namespace, new Stat()); if (!acls.get(0).getId().getScheme().equals("sasl")) { log.info("'sasl' ACLs not set; setting..."); List<String> children = client.getZookeeperClient().getZooKeeper().getChildren(namespace, null); for (String child : children) { checkAndSetACLs("/" + child); } client.getZookeeperClient().getZooKeeper().setACL(namespace, saslACL, -1); } } } }
/** * Make directories if necessary * @param dir the dir * @return return true if mkdirs successfully, or throw ZkException */ public Boolean mkdirs(String dir){ try { EnsurePath clientAppPathExist = new EnsurePath("/" + client.getNamespace() + slash(dir)); clientAppPathExist.ensure(client.getZookeeperClient()); return Boolean.TRUE; } catch (Exception e) { handleConnectionLoss(e); throw new ZkException(e); } }
nullNsFw.newNamespaceAwareEnsurePath("/" + zkClient.getNamespace()); try { ensureNs.ensure(nullNsFw.getZookeeperClient());
nullNsFw.newNamespaceAwareEnsurePath("/" + zkClient.getNamespace()); try { ensureNs.ensure(nullNsFw.getZookeeperClient());
/** * Tests the BookKeeperLogFactory and its initialization. */ @Test public void testFactoryInitialize() { BookKeeperConfig bkConfig = BookKeeperConfig .builder() .with(BookKeeperConfig.ZK_ADDRESS, "localhost:" + BK_PORT.get()) .with(BookKeeperConfig.BK_LEDGER_MAX_SIZE, WRITE_MAX_LENGTH * 10) // Very frequent rollovers. .with(BookKeeperConfig.ZK_METADATA_PATH, this.zkClient.get().getNamespace()) .build(); @Cleanup val factory = new BookKeeperLogFactory(bkConfig, this.zkClient.get(), executorService()); AssertExtensions.assertThrows("", factory::initialize, ex -> ex instanceof DataLogNotAvailableException && ex.getCause() instanceof BKException.ZKException ); }