19package org.sleuthkit.autopsy.coordinationservice;
21import java.io.IOException;
22import java.util.Collection;
23import java.util.Iterator;
26import java.util.concurrent.ConcurrentHashMap;
27import java.util.concurrent.TimeUnit;
28import javax.annotation.concurrent.GuardedBy;
29import javax.annotation.concurrent.ThreadSafe;
30import org.apache.curator.RetryPolicy;
31import org.apache.curator.framework.CuratorFramework;
32import org.apache.curator.framework.CuratorFrameworkFactory;
33import org.apache.curator.framework.recipes.locks.InterProcessMutex;
34import org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock;
35import org.apache.curator.retry.ExponentialBackoffRetry;
36import org.apache.curator.utils.ZKPaths;
37import org.apache.zookeeper.CreateMode;
38import org.apache.zookeeper.KeeperException;
39import org.apache.zookeeper.KeeperException.NoNodeException;
40import org.apache.zookeeper.ZooDefs;
41import org.openide.util.Lookup;
42import org.sleuthkit.autopsy.coordinationservice.utils.CoordinationServiceUtils;
43import org.sleuthkit.autopsy.core.UserPreferences;
57 @GuardedBy(
"CoordinationService.class")
60 @GuardedBy(
"categoryNodeToPath")
76 Iterator<? extends CoordinationServiceNamespace> it = providers.iterator();
78 rootNode = it.next().getNamespaceRoot();
86 }
catch (InterruptedException ex) {
93 Thread.currentThread().interrupt();
114 if (hostName.isEmpty() || port.isEmpty()) {
118 port = Integer.toString(portInt);
129 RetryPolicy retryPolicy =
new ExponentialBackoffRetry(1000, 3);
130 String connectString = hostName +
":" + port;
137 String rootNode = rootNodeName;
139 if (!rootNode.startsWith(
"/")) {
140 rootNode =
"/" + rootNode;
144 String nodePath = rootNode +
"/" + node.getDisplayName();
146 curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).forPath(nodePath);
147 }
catch (KeeperException ex) {
148 if (ex.code() != KeeperException.Code.NODEEXISTS) {
151 }
catch (Exception ex) {
170 while(fullNodePath.endsWith(
"/")) {
171 fullNodePath = fullNodePath.substring(0, fullNodePath.length() - 1);
176 ZKPaths.mkdirs(
curator.getZookeeperClient().getZooKeeper(), fullNodePath);
178 }
catch (Exception ex) {
204 String fullNodePath =
"";
208 InterProcessReadWriteLock lock =
new InterProcessReadWriteLock(
curator, fullNodePath);
209 if (lock.writeLock().acquire(timeOut, timeUnit)) {
210 return new Lock(nodePath, lock.writeLock());
214 }
catch (Exception ex) {
215 if (ex instanceof InterruptedException) {
216 throw (InterruptedException) ex;
240 String fullNodePath =
"";
244 InterProcessReadWriteLock lock =
new InterProcessReadWriteLock(
curator, fullNodePath);
245 if (!lock.writeLock().acquire(0, TimeUnit.SECONDS)) {
248 return new Lock(nodePath, lock.writeLock());
249 }
catch (Exception ex) {
275 String fullNodePath =
"";
279 InterProcessReadWriteLock lock =
new InterProcessReadWriteLock(
curator, fullNodePath);
280 if (lock.readLock().acquire(timeOut, timeUnit)) {
281 return new Lock(nodePath, lock.readLock());
285 }
catch (Exception ex) {
286 if (ex instanceof InterruptedException) {
287 throw (InterruptedException) ex;
311 String fullNodePath =
"";
315 InterProcessReadWriteLock lock =
new InterProcessReadWriteLock(
curator, fullNodePath);
316 if (!lock.readLock().acquire(0, TimeUnit.SECONDS)) {
319 return new Lock(nodePath, lock.readLock());
320 }
catch (Exception ex) {
340 String fullNodePath =
"";
346 return curator.getData().forPath(fullNodePath);
347 }
catch (NoNodeException ex) {
349 }
catch (Exception ex) {
350 if (ex instanceof InterruptedException) {
351 throw (InterruptedException) ex;
373 curator.setData().forPath(fullNodePath, data);
374 }
catch (Exception ex) {
375 if (ex instanceof InterruptedException) {
376 throw (InterruptedException) ex;
398 curator.delete().forPath(fullNodePath);
399 }
catch (Exception ex) {
400 if (ex instanceof InterruptedException) {
401 throw (InterruptedException) ex;
425 }
catch (Exception ex) {
426 if (ex instanceof InterruptedException) {
427 throw (InterruptedException) ex;
444 if (nodePath.startsWith(
"/")) {
463 super(message, cause);
472 public static class Lock implements AutoCloseable {
483 this.interProcessLock = lock;
492 this.interProcessLock.release();
493 }
catch (Exception ex) {
static final long serialVersionUID
CoordinationServiceException(String message, Throwable cause)
CoordinationServiceException(String message)
Lock(String nodePath, InterProcessMutex lock)
final InterProcessMutex interProcessLock
Lock tryGetExclusiveLock(CategoryNode category, String nodePath, int timeOut, TimeUnit timeUnit)
static final int PORT_OFFSET
static final int CONNECTION_TIMEOUT_MILLISECONDS
static final int SESSION_TIMEOUT_MILLISECONDS
void setNodeData(CategoryNode category, String nodePath, byte[] data)
Lock tryGetSharedLock(CategoryNode category, String nodePath, int timeOut, TimeUnit timeUnit)
CoordinationService(String rootNodeName)
String getFullyQualifiedNodePath(CategoryNode category, String nodePath)
Lock tryGetExclusiveLock(CategoryNode category, String nodePath)
void deleteNode(CategoryNode category, String nodePath)
static synchronized CoordinationService getInstance()
final Map< String, String > categoryNodeToPath
final CuratorFramework curator
static CoordinationService instance
List< String > getNodeList(CategoryNode category)
byte[] getNodeData(CategoryNode category, String nodePath)
static final String DEFAULT_NAMESPACE_ROOT
Lock tryGetSharedLock(CategoryNode category, String nodePath)
String upsertNodePath(CategoryNode category, String nodePath)
static boolean isZooKeeperAccessible(String hostName, String port)
static String getIndexingServerPort()
static String getZkServerHost()
static String getIndexingServerHost()
static String getZkServerPort()
CategoryNode(String displayName)