Search in sources :

Example 1 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project lucene-solr by apache.

the class SolrZkClient method makePath.

/**
   * Creates the path in ZooKeeper, creating each node as necessary.
   *
   * e.g. If <code>path=/solr/group/node</code> and none of the nodes, solr,
   * group, node exist, each will be created.
   * 
   * skipPathParts will force the call to fail if the first skipPathParts do not exist already.
   *
   * Note: retryOnConnLoss is only respected for the final node - nodes
   * before that are always retried on connection loss.
   */
public void makePath(String path, byte[] data, CreateMode createMode, Watcher watcher, boolean failOnExists, boolean retryOnConnLoss, int skipPathParts) throws KeeperException, InterruptedException {
    log.debug("makePath: {}", path);
    boolean retry = true;
    if (path.startsWith("/")) {
        path = path.substring(1, path.length());
    }
    String[] paths = path.split("/");
    StringBuilder sbPath = new StringBuilder();
    for (int i = 0; i < paths.length; i++) {
        String pathPiece = paths[i];
        sbPath.append("/" + pathPiece);
        if (i < skipPathParts) {
            continue;
        }
        byte[] bytes = null;
        final String currentPath = sbPath.toString();
        Object exists = exists(currentPath, watcher, retryOnConnLoss);
        if (exists == null || ((i == paths.length - 1) && failOnExists)) {
            CreateMode mode = CreateMode.PERSISTENT;
            if (i == paths.length - 1) {
                mode = createMode;
                bytes = data;
                if (!retryOnConnLoss)
                    retry = false;
            }
            try {
                if (retry) {
                    final CreateMode finalMode = mode;
                    final byte[] finalBytes = bytes;
                    zkCmdExecutor.retryOperation(new ZkOperation() {

                        @Override
                        public Object execute() throws KeeperException, InterruptedException {
                            keeper.create(currentPath, finalBytes, zkACLProvider.getACLsToAdd(currentPath), finalMode);
                            return null;
                        }
                    });
                } else {
                    keeper.create(currentPath, bytes, zkACLProvider.getACLsToAdd(currentPath), mode);
                }
            } catch (NodeExistsException e) {
                if (!failOnExists) {
                    // TODO: version ? for now, don't worry about race
                    setData(currentPath, data, -1, retryOnConnLoss);
                    // set new watch
                    exists(currentPath, watcher, retryOnConnLoss);
                    return;
                }
                // ignore unless it's the last node in the path
                if (i == paths.length - 1) {
                    throw e;
                }
            }
            if (i == paths.length - 1) {
                // set new watch
                exists(currentPath, watcher, retryOnConnLoss);
            }
        } else if (i == paths.length - 1) {
            // TODO: version ? for now, don't worry about race
            setData(currentPath, data, -1, retryOnConnLoss);
            // set new watch
            exists(currentPath, watcher, retryOnConnLoss);
        }
    }
}
Also used : NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) CreateMode(org.apache.zookeeper.CreateMode) KeeperException(org.apache.zookeeper.KeeperException)

Example 2 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project flink by apache.

the class ZooKeeperStateHandleStoreITCase method testAddWithCreateMode.

/**
	 * Tests that {@link CreateMode} is respected.
	 */
@Test
public void testAddWithCreateMode() throws Exception {
    LongStateStorage longStateStorage = new LongStateStorage();
    ZooKeeperStateHandleStore<Long> store = new ZooKeeperStateHandleStore<Long>(ZooKeeper.getClient(), longStateStorage, Executors.directExecutor());
    // Config
    Long state = 3457347234L;
    CreateMode[] modes = CreateMode.values();
    for (int i = 0; i < modes.length; i++) {
        CreateMode mode = modes[i];
        state += i;
        String pathInZooKeeper = "/testAddWithCreateMode" + mode.name();
        // Test
        store.add(pathInZooKeeper, state, mode);
        if (mode.isSequential()) {
            // Figure out the sequential ID
            List<String> paths = ZooKeeper.getClient().getChildren().forPath("/");
            for (String p : paths) {
                if (p.startsWith("testAddWithCreateMode" + mode.name())) {
                    pathInZooKeeper = "/" + p;
                    break;
                }
            }
        }
        // Verify
        // State handle created
        assertEquals(i + 1, store.getAll().size());
        assertEquals(state, longStateStorage.getStateHandles().get(i).retrieveState());
        // Path created
        Stat stat = ZooKeeper.getClient().checkExists().forPath(pathInZooKeeper);
        assertNotNull(stat);
        // Is ephemeral or persistent
        if (mode.isEphemeral()) {
            assertTrue(stat.getEphemeralOwner() != 0);
        } else {
            assertEquals(0, stat.getEphemeralOwner());
        }
        // Data is equal
        @SuppressWarnings("unchecked") Long actual = ((RetrievableStateHandle<Long>) InstantiationUtil.deserializeObject(ZooKeeper.getClient().getData().forPath(pathInZooKeeper), ClassLoader.getSystemClassLoader())).retrieveState();
        assertEquals(state, actual);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) CreateMode(org.apache.zookeeper.CreateMode) RetrievableStateHandle(org.apache.flink.runtime.state.RetrievableStateHandle) Test(org.junit.Test)

Example 3 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project fabric8 by jboss-fuse.

the class CreateAction method doExecute.

@Override
protected void doExecute(CuratorFramework curator) throws Exception {
    List<ACL> acls = acl == null ? ZooDefs.Ids.OPEN_ACL_UNSAFE : parseACLs(acl);
    CreateMode mode;
    if (ephemeral && sequential) {
        mode = CreateMode.EPHEMERAL_SEQUENTIAL;
    } else if (ephemeral) {
        mode = CreateMode.EPHEMERAL;
    } else if (sequential) {
        mode = CreateMode.PERSISTENT_SEQUENTIAL;
    } else {
        mode = CreateMode.PERSISTENT;
    }
    String nodeData = data;
    if (importUrl) {
        nodeData = loadUrl(new URL(data));
    }
    try {
        CreateBuilder createBuilder = curator.create();
        if (recursive) {
            createBuilder.creatingParentsIfNeeded();
        }
        BackgroundPathAndBytesable<String> create = createBuilder.withMode(mode).withACL(acls);
        if (nodeData == null) {
            create.forPath(path);
        } else {
            create.forPath(path, nodeData.getBytes());
        }
    } catch (KeeperException.NodeExistsException e) {
        if (overwrite) {
            curator.setData().forPath(path, nodeData.getBytes());
        } else {
            throw e;
        }
    }
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ACL(org.apache.zookeeper.data.ACL) URL(java.net.URL) KeeperException(org.apache.zookeeper.KeeperException)

Example 4 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project tesb-rt-se by Talend.

the class RegisterEndpointProviderTest method createEndpointStatus.

private void createEndpointStatus(String endpointPath, boolean persistent) throws KeeperException, InterruptedException {
    String endpointStatusPath = endpointPath + "/" + STATUS_NODE;
    CreateMode mode = persistent ? PERSISTENT : EPHEMERAL;
    createNode(endpointStatusPath, mode);
}
Also used : CreateMode(org.apache.zookeeper.CreateMode)

Example 5 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project tesb-rt-se by Talend.

the class EndpointNodeImpl method setLive.

public void setLive(boolean persistent) throws ServiceLocatorException, InterruptedException {
    CreateMode mode = persistent ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
    NodePath endpointStatusNodePath = child(LIVE);
    zkBackend.ensurePathExists(endpointStatusNodePath, mode);
    // the old expiration time is not valid after re-registering the endpoint
    zkBackend.ensurePathDeleted(child(TIMETOLIVE), false);
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) NodePath(org.talend.esb.servicelocator.client.internal.NodePath)

Aggregations

CreateMode (org.apache.zookeeper.CreateMode)35 KeeperException (org.apache.zookeeper.KeeperException)8 HelixException (org.apache.helix.HelixException)7 Stat (org.apache.zookeeper.data.Stat)7 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Op (org.apache.zookeeper.Op)4 ACL (org.apache.zookeeper.data.ACL)4 DataUpdater (org.I0Itec.zkclient.DataUpdater)3 ZkBadVersionException (org.I0Itec.zkclient.exception.ZkBadVersionException)3 ZkException (org.I0Itec.zkclient.exception.ZkException)3 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)3 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)3 ZNRecord (org.apache.helix.ZNRecord)3 HelixMetaDataAccessException (org.apache.helix.api.exceptions.HelixMetaDataAccessException)3 CreateCallbackHandler (org.apache.helix.manager.zk.ZkAsyncCallbacks.CreateCallbackHandler)3 Code (org.apache.zookeeper.KeeperException.Code)3 CreateRequest (org.apache.zookeeper.proto.CreateRequest)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 URI (java.net.URI)2