use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat in project storm-signals by ptgoetz.
the class AbstractSignalConnection method send.
public void send(String toPath, byte[] signal) throws Exception {
Stat stat = this.client.checkExists().forPath(toPath);
if (stat == null) {
String path = this.client.create().creatingParentsIfNeeded().forPath(toPath);
LOG.info("Created: " + path);
}
this.client.setData().forPath(toPath, signal);
}
use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat in project BRFS by zhangnianli.
the class FileCenter method main.
public static void main(String[] args) {
id = new Random().nextInt(10);
System.out.println("id = " + id);
System.out.println("hahahahha");
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.builder().namespace(ROOT).connectString(zk_address).retryPolicy(retryPolicy).build();
client.start();
try {
Stat stat = client.checkExists().forPath(DUPS);
System.out.println("stat =" + stat);
if (stat == null) {
System.out.println("create--" + client.create().forPath(DUPS));
}
ExecutorService pool = Executors.newFixedThreadPool(5);
PathChildrenCache pathCache = new PathChildrenCache(client, DUPS, true, false, pool);
pathCache.getListenable().addListener(new PathNodeListener());
pathCache.start();
// TreeCache cache = new TreeCache(client, DUPS);
// cache.getListenable().addListener(new TreeNodeListener(), pool);
// cache.start();
} catch (Exception e) {
e.printStackTrace();
}
synchronized (client) {
try {
client.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
client.close();
}
use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat in project BRFS by zhangnianli.
the class ZookeeperFileCoordinator method publish.
@Override
public boolean publish(FileNode node) {
String fileNodePath = ZKPaths.makePath(COORDINATOR_ROOT, COORDINATOR_FILENODES, node.getName());
Stat fileNodeStat = null;
try {
fileNodeStat = client.checkExists().forPath(fileNodePath);
} catch (Exception e) {
e.printStackTrace();
}
if (fileNodeStat != null) {
return false;
}
String result = null;
try {
result = client.create().forPath(fileNodePath, JsonUtils.toJsonBytes(node));
} catch (Exception e) {
e.printStackTrace();
}
if (result == null) {
return false;
}
return true;
}
use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat in project BRFS by zhangnianli.
the class DefaultStorageNameManager method createStorageName.
@Override
public StorageNameNode createStorageName(String storageName, int replicas, int ttl) {
if (exists(storageName)) {
return findStorageName(storageName);
}
StorageNameNode node = new StorageNameNode(storageName, StorageIdBuilder.createStorageId(), replicas, ttl);
String storageNamePath = ZKPaths.makePath(DEFAULT_STORAGE_NAME_ROOT, storageName);
String path = null;
try {
path = zkClient.create().forPath(storageNamePath, ProtoStuffUtils.serialize(node));
} catch (Exception e) {
e.printStackTrace();
}
if (path != null) {
return node;
}
Stat storagenNameStat = null;
try {
storagenNameStat = zkClient.checkExists().forPath(storageNamePath);
} catch (Exception e) {
e.printStackTrace();
}
if (storagenNameStat != null) {
byte[] idBytes = null;
try {
idBytes = zkClient.getData().forPath(storageNamePath);
} catch (Exception e) {
e.printStackTrace();
}
if (idBytes != null) {
return ProtoStuffUtils.deserialize(idBytes, StorageNameNode.class);
}
}
return null;
}
use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.data.Stat in project nifi by apache.
the class ZooKeeperMigrator method writeZooKeeper.
void writeZooKeeper(InputStream zkData, AuthMode authMode, byte[] authData, boolean ignoreSource, boolean useExistingACL) throws IOException, ExecutionException, InterruptedException {
// ensure that the chroot path exists
ZooKeeper zooKeeperRoot = getZooKeeper(Joiner.on(',').join(zooKeeperEndpointConfig.getServers()), authMode, authData);
ensureNodeExists(zooKeeperRoot, zooKeeperEndpointConfig.getPath(), CreateMode.PERSISTENT);
closeZooKeeper(zooKeeperRoot);
ZooKeeper zooKeeper = getZooKeeper(zooKeeperEndpointConfig.getConnectString(), authMode, authData);
JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(zkData)));
Gson gson = new GsonBuilder().create();
jsonReader.beginArray();
// determine source ZooKeeperEndpointConfig for this data
final ZooKeeperEndpointConfig sourceZooKeeperEndpointConfig = gson.fromJson(jsonReader, ZooKeeperEndpointConfig.class);
LOGGER.info("Source data was obtained from ZooKeeper: {}", sourceZooKeeperEndpointConfig);
Preconditions.checkArgument(!Strings.isNullOrEmpty(sourceZooKeeperEndpointConfig.getConnectString()) && !Strings.isNullOrEmpty(sourceZooKeeperEndpointConfig.getPath()) && sourceZooKeeperEndpointConfig.getServers() != null && sourceZooKeeperEndpointConfig.getServers().size() > 0, "Source ZooKeeper %s from %s is invalid", sourceZooKeeperEndpointConfig, zkData);
Preconditions.checkArgument(Collections.disjoint(zooKeeperEndpointConfig.getServers(), sourceZooKeeperEndpointConfig.getServers()) || !zooKeeperEndpointConfig.getPath().equals(sourceZooKeeperEndpointConfig.getPath()) || ignoreSource, "Source ZooKeeper config %s for the data provided can not contain the same server and path as the configured destination ZooKeeper config %s", sourceZooKeeperEndpointConfig, zooKeeperEndpointConfig);
// stream through each node read from the json input
final Stream<DataStatAclNode> stream = StreamSupport.stream(new Spliterators.AbstractSpliterator<DataStatAclNode>(0, 0) {
@Override
public boolean tryAdvance(Consumer<? super DataStatAclNode> action) {
try {
// stream each DataStatAclNode from configured json file
synchronized (jsonReader) {
if (jsonReader.hasNext()) {
action.accept(gson.fromJson(jsonReader, DataStatAclNode.class));
return true;
} else {
return false;
}
}
} catch (IOException e) {
throw new RuntimeException("unable to read nodes from json", e);
}
}
}, false);
final List<CompletableFuture<Stat>> writeFutures = stream.parallel().map(node -> {
/*
* create stage to determine the acls that should be applied to the node.
* this stage will be used to initialize the chain
*/
final CompletableFuture<List<ACL>> determineACLStage = CompletableFuture.supplyAsync(() -> determineACLs(node, authMode, useExistingACL));
/*
* create stage to apply acls to nodes and transform node to DataStatAclNode object
*/
final Function<List<ACL>, CompletableFuture<DataStatAclNode>> transformNodeStage = acls -> CompletableFuture.supplyAsync(() -> transformNode(node, acls));
/*
* create stage to ensure that nodes exist for the entire path of the zookeeper node, must be invoked after the transformNode stage to
* ensure that the node will exist after path migration
*/
final Function<DataStatAclNode, CompletionStage<String>> ensureNodeExistsStage = dataStatAclNode -> CompletableFuture.supplyAsync(() -> ensureNodeExists(zooKeeper, dataStatAclNode.getPath(), dataStatAclNode.getEphemeralOwner() == 0 ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL));
/*
* create stage that waits for both the transformNode and ensureNodeExists stages complete, and also provides that the given transformed node is
* available to the next stage
*/
final BiFunction<String, DataStatAclNode, DataStatAclNode> combineEnsureNodeAndTransferNodeStage = (u, dataStatAclNode) -> dataStatAclNode;
/*
* create stage to transmit the node to the destination zookeeper endpoint, must be invoked after the node has been transformed and its path
* has been created (or already exists) in the destination zookeeper
*/
final Function<DataStatAclNode, CompletionStage<Stat>> transmitNodeStage = dataStatNode -> CompletableFuture.supplyAsync(() -> transmitNode(zooKeeper, dataStatNode));
/*
* submit the stages chained together in the proper order to perform the processing on the given node
*/
final CompletableFuture<DataStatAclNode> dataStatAclNodeCompletableFuture = determineACLStage.thenCompose(transformNodeStage);
return dataStatAclNodeCompletableFuture.thenCompose(ensureNodeExistsStage).thenCombine(dataStatAclNodeCompletableFuture, combineEnsureNodeAndTransferNodeStage).thenCompose(transmitNodeStage);
}).collect(Collectors.toList());
CompletableFuture<Void> allWritesFuture = CompletableFuture.allOf(writeFutures.toArray(new CompletableFuture[writeFutures.size()]));
final CompletableFuture<List<Stat>> finishedWrites = allWritesFuture.thenApply(v -> writeFutures.stream().map(CompletableFuture::join).collect(Collectors.toList()));
final List<Stat> writesDone = finishedWrites.get();
if (LOGGER.isInfoEnabled()) {
final int writeCount = writesDone.size();
LOGGER.info("{} {} transferred to {}", writeCount, writeCount == 1 ? "node" : "nodes", zooKeeperEndpointConfig);
}
jsonReader.close();
closeZooKeeper(zooKeeper);
}
Aggregations