Search in sources :

Example 76 with Stat

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);
}
Also used : Stat(org.apache.zookeeper.data.Stat)

Example 77 with Stat

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();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Stat(org.apache.zookeeper.data.Stat) Random(java.util.Random) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ExecutorService(java.util.concurrent.ExecutorService) RetryPolicy(org.apache.curator.RetryPolicy)

Example 78 with Stat

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;
}
Also used : Stat(org.apache.zookeeper.data.Stat)

Example 79 with Stat

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;
}
Also used : Stat(org.apache.zookeeper.data.Stat)

Example 80 with Stat

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);
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) Spliterators(java.util.Spliterators) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) ACL(org.apache.zookeeper.data.ACL) CompletableFuture(java.util.concurrent.CompletableFuture) Stat(org.apache.zookeeper.data.Stat) JsonParser(com.google.gson.JsonParser) Function(java.util.function.Function) GsonBuilder(com.google.gson.GsonBuilder) JsonReader(com.google.gson.stream.JsonReader) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) Gson(com.google.gson.Gson) OutputStreamWriter(java.io.OutputStreamWriter) StreamSupport(java.util.stream.StreamSupport) Splitter(com.google.common.base.Splitter) JsonWriter(com.google.gson.stream.JsonWriter) ZooKeeper(org.apache.zookeeper.ZooKeeper) OutputStream(java.io.OutputStream) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Watcher(org.apache.zookeeper.Watcher) BufferedWriter(java.io.BufferedWriter) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) ZooDefs(org.apache.zookeeper.ZooDefs) Preconditions(com.google.common.base.Preconditions) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) Joiner(com.google.common.base.Joiner) InputStream(java.io.InputStream) Gson(com.google.gson.Gson) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) CompletableFuture(java.util.concurrent.CompletableFuture) Stat(org.apache.zookeeper.data.Stat) JsonReader(com.google.gson.stream.JsonReader) ArrayList(java.util.ArrayList) List(java.util.List) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) ACL(org.apache.zookeeper.data.ACL) IOException(java.io.IOException) Spliterators(java.util.Spliterators) ZooKeeper(org.apache.zookeeper.ZooKeeper) BiFunction(java.util.function.BiFunction) BufferedReader(java.io.BufferedReader)

Aggregations

Stat (org.apache.zookeeper.data.Stat)799 KeeperException (org.apache.zookeeper.KeeperException)266 Test (org.junit.Test)124 IOException (java.io.IOException)120 ZooKeeper (org.apache.zookeeper.ZooKeeper)88 ArrayList (java.util.ArrayList)67 Test (org.testng.annotations.Test)58 Test (org.junit.jupiter.api.Test)53 Watcher (org.apache.zookeeper.Watcher)49 AsyncCallback (org.apache.zookeeper.AsyncCallback)48 ACL (org.apache.zookeeper.data.ACL)47 List (java.util.List)43 CountDownLatch (java.util.concurrent.CountDownLatch)43 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)39 WatchedEvent (org.apache.zookeeper.WatchedEvent)38 CuratorFramework (org.apache.curator.framework.CuratorFramework)37 Map (java.util.Map)34 HashMap (java.util.HashMap)32 WebApplicationException (javax.ws.rs.WebApplicationException)29 ExecutionException (java.util.concurrent.ExecutionException)27