/** * @param ensembleProvider the ensemble provider * @param sessionTimeoutMs session timeout * @param connectionTimeoutMs connection timeout * @param watcher default watcher or null * @param retryPolicy the retry policy to use */ public CuratorZookeeperClient(EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher watcher, RetryPolicy retryPolicy) { this(new DefaultZookeeperFactory(), ensembleProvider, sessionTimeoutMs, connectionTimeoutMs, watcher, retryPolicy, false); }
@Override public String apply(String child) { return ZKPaths.makePath(path, child); } }
/** * Returns a view of this EnsurePath instance that does not make the last node. * i.e. if the path is "/a/b/c" only "/a/b" will be ensured * * @return view */ public EnsurePath excludingLast() { return new EnsurePath(path, helper, false); }
private String adjustPath(String path) throws Exception { if ( doProtected ) { ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(path); String name = getProtectedPrefix() + pathAndNode.getNode(); path = ZKPaths.makePath(pathAndNode.getPath(), name); } return path; } }
String fixForNamespace(String path) { if ( ensurePath != null ) { try { ensurePath.ensure(client.getZookeeperClient()); } catch ( Exception e ) { client.logError("Ensure path threw exception", e); } } return ZKPaths.fixForNamespace(namespace, path); }
/** * @param client the client * @param threadFactory thread factory to use or null for a default */ public ConnectionStateManager(CuratorFramework client, ThreadFactory threadFactory) { this.client = client; if ( threadFactory == null ) { threadFactory = ThreadUtils.newThreadFactory("ConnectionStateManager"); } service = Executors.newSingleThreadExecutor(threadFactory); }
/** * Make sure all the nodes in the path are created. NOTE: Unlike File.mkdirs(), Zookeeper doesn't distinguish * between directories and files. So, every node in the path is created. The data for each node is an empty blob * * @param zookeeper the client * @param path path to ensure * @throws InterruptedException thread interruption * @throws org.apache.zookeeper.KeeperException * Zookeeper errors */ public static void mkdirs(ZooKeeper zookeeper, String path) throws InterruptedException, KeeperException { mkdirs(zookeeper, path, true); }
private static ScheduledExecutorService newExecutorService() { return ThreadUtils.newSingleThreadScheduledExecutor("Reaper"); } }
private static ScheduledExecutorService newExecutorService() { return ThreadUtils.newFixedThreadScheduledPool(2, "ChildReaper"); }
private EnsurePath(String path, AtomicReference<Helper> helper, boolean makeLastNode) { this.path = path; this.makeLastNode = makeLastNode; this.helper = (helper != null) ? helper : new AtomicReference<Helper>(new InitialHelper()); }
/** * First time, synchronizes and makes sure all nodes in the path are created. Subsequent calls * with this instance are NOPs. * * @param client ZK client * @throws Exception ZK errors */ public void ensure(CuratorZookeeperClient client) throws Exception { Helper localHelper = helper.get(); localHelper.ensure(client, path, makeLastNode); }
/** * @param client curztor client * @param path the full path to the node to cache * @param dataIsCompressed if true, data in the path is compressed */ public NodeCache(CuratorFramework client, String path, boolean dataIsCompressed) { this.client = client; this.path = path; this.dataIsCompressed = dataIsCompressed; ensurePath = client.newNamespaceAwareEnsurePath(path).excludingLast(); }
@Override public String apply(String name) { return ZKPaths.makePath(basePath, name); } }
EnsurePath newNamespaceAwareEnsurePath(String path) { return new EnsurePath(fixForNamespace(path)); } }
private ThreadFactory getThreadFactory(CuratorFrameworkFactory.Builder builder) { ThreadFactory threadFactory = builder.getThreadFactory(); if ( threadFactory == null ) { threadFactory = ThreadUtils.newThreadFactory("CuratorFramework"); } return threadFactory; }
/** * * @param connectString list of servers to connect to * @param sessionTimeoutMs session timeout * @param connectionTimeoutMs connection timeout * @param watcher default watcher or null * @param retryPolicy the retry policy to use */ public CuratorZookeeperClient(String connectString, int sessionTimeoutMs, int connectionTimeoutMs, Watcher watcher, RetryPolicy retryPolicy) { this(new DefaultZookeeperFactory(), new FixedEnsembleProvider(connectString), sessionTimeoutMs, connectionTimeoutMs, watcher, retryPolicy, false); }
String makeItemPath() { return ZKPaths.makePath(queuePath, QUEUE_ITEM_NAME); }
/** * Apply the namespace to the given path * * @param namespace namespace (can be null) * @param path path * @return adjusted path */ public static String fixForNamespace(String namespace, String path) { if ( namespace != null ) { return makePath(namespace, path); } return path; }
String unfixForNamespace(String path) { if ( (namespace != null) && (path != null) ) { String namespacePath = ZKPaths.makePath(namespace, null); if ( path.startsWith(namespacePath) ) { path = (path.length() > namespacePath.length()) ? path.substring(namespacePath.length()) : "/"; } } return path; }
LockInternals(CuratorFramework client, LockInternalsDriver driver, String path, String lockName, int maxLeases) { this.driver = driver; this.lockName = lockName; this.maxLeases = maxLeases; PathUtils.validatePath(path); this.client = client; this.basePath = path; this.path = ZKPaths.makePath(path, lockName); }