Search in sources :

Example 16 with CreateMode

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

the class ZooKeeperUtils method getCreateMode.

/**
     * Pulls a createMode flag from the header keyed by
     * {@link ZooKeeperMessage#ZOOKEEPER_CREATE_MODE} in the given message and
     * attempts to parse a {@link CreateMode} from it.
     *
     * @param message the message that may contain a ZOOKEEPER_CREATE_MODE
     *            header.
     * @return the parsed {@link CreateMode} or null if the header was null or
     *         not a valid mode flag.
     */
public static CreateMode getCreateMode(Message message, CreateMode defaultMode) {
    CreateMode mode = null;
    try {
        mode = message.getHeader(ZooKeeperMessage.ZOOKEEPER_CREATE_MODE, CreateMode.class);
    } catch (Exception e) {
    }
    if (mode == null) {
        String modeHeader = message.getHeader(ZooKeeperMessage.ZOOKEEPER_CREATE_MODE, String.class);
        mode = getCreateModeFromString(modeHeader, defaultMode);
    }
    return mode == null ? defaultMode : mode;
}
Also used : CreateMode(org.apache.zookeeper.CreateMode)

Example 17 with CreateMode

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

the class ZooKeeperProducerTest method setUsingCreateModeFromHeader.

@Test
public void setUsingCreateModeFromHeader() throws Exception {
    client.createPersistent("/modes-test", "parent for modes");
    for (CreateMode mode : CreateMode.values()) {
        Exchange exchange = createExchangeWithBody(testPayload);
        exchange.getIn().setHeader(ZOOKEEPER_CREATE_MODE, mode);
        exchange.getIn().setHeader(ZOOKEEPER_NODE, "/modes-test/" + mode);
        exchange.setPattern(ExchangePattern.InOut);
        template.send("direct:node-from-header", exchange);
    }
    GetChildrenOperation listing = new GetChildrenOperation(getConnection(), "/modes-test");
    assertEquals(CreateMode.values().length, listing.get().getResult().size());
}
Also used : Exchange(org.apache.camel.Exchange) CreateMode(org.apache.zookeeper.CreateMode) GetChildrenOperation(org.apache.camel.component.zookeeper.operations.GetChildrenOperation) Test(org.junit.Test)

Example 18 with CreateMode

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

the class RegistryOperationsService method bind.

@Override
public void bind(String path, ServiceRecord record, int flags) throws IOException {
    Preconditions.checkArgument(record != null, "null record");
    validatePath(path);
    // validate the record before putting it
    RegistryTypeUtils.validateServiceRecord(path, record);
    LOG.info("Bound at {} : {}", path, record);
    CreateMode mode = CreateMode.PERSISTENT;
    byte[] bytes = serviceRecordMarshal.toBytes(record);
    zkSet(path, mode, bytes, getClientAcls(), ((flags & BindFlags.OVERWRITE) != 0));
}
Also used : CreateMode(org.apache.zookeeper.CreateMode)

Example 19 with CreateMode

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

the class ServiceDiscoveryImpl method internalRegisterService.

@VisibleForTesting
protected void internalRegisterService(ServiceInstance<T> service) throws Exception {
    byte[] bytes = serializer.serialize(service);
    String path = pathForInstance(service.getName(), service.getId());
    final int MAX_TRIES = 2;
    boolean isDone = false;
    for (int i = 0; !isDone && (i < MAX_TRIES); ++i) {
        try {
            CreateMode mode;
            switch(service.getServiceType()) {
                case DYNAMIC:
                    mode = CreateMode.EPHEMERAL;
                    break;
                case DYNAMIC_SEQUENTIAL:
                    mode = CreateMode.EPHEMERAL_SEQUENTIAL;
                    break;
                default:
                    mode = CreateMode.PERSISTENT;
                    break;
            }
            client.create().creatingParentContainersIfNeeded().withMode(mode).forPath(path, bytes);
            client.setData().forPath(pathForName(service.getName()), serviceDefinitionSerializer.serialize(service.getPayload()));
            isDone = true;
        } catch (KeeperException.NodeExistsException e) {
            // must delete then re-create so that watchers fire
            client.delete().forPath(path);
        }
    }
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) KeeperException(org.apache.zookeeper.KeeperException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 20 with CreateMode

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

the class ZKLogStreamMetadataStore method createMissingMetadata.

static void createMissingMetadata(final ZooKeeper zk, final String basePath, final String logRootPath, final List<Versioned<byte[]>> metadatas, final List<ACL> acl, final boolean ownAllocator, final boolean createIfNotExists, final CompletableFuture<List<Versioned<byte[]>>> promise) {
    final List<byte[]> pathsToCreate = Lists.newArrayListWithExpectedSize(metadatas.size());
    final List<Op> zkOps = Lists.newArrayListWithExpectedSize(metadatas.size());
    CreateMode createMode = CreateMode.PERSISTENT;
    // log root parent path
    String logRootParentPath = Utils.getParent(logRootPath);
    if (pathExists(metadatas.get(MetadataIndex.LOG_ROOT_PARENT))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(EMPTY_BYTES);
        zkOps.add(Op.create(logRootParentPath, EMPTY_BYTES, acl, createMode));
    }
    // log root path
    if (pathExists(metadatas.get(MetadataIndex.LOG_ROOT))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(EMPTY_BYTES);
        zkOps.add(Op.create(logRootPath, EMPTY_BYTES, acl, createMode));
    }
    // max id
    if (pathExists(metadatas.get(MetadataIndex.MAX_TXID))) {
        pathsToCreate.add(null);
    } else {
        byte[] zeroTxnIdData = DLUtils.serializeTransactionId(0L);
        pathsToCreate.add(zeroTxnIdData);
        zkOps.add(Op.create(logRootPath + MAX_TXID_PATH, zeroTxnIdData, acl, createMode));
    }
    // version
    if (pathExists(metadatas.get(MetadataIndex.VERSION))) {
        pathsToCreate.add(null);
    } else {
        byte[] versionData = intToBytes(LAYOUT_VERSION);
        pathsToCreate.add(versionData);
        zkOps.add(Op.create(logRootPath + VERSION_PATH, versionData, acl, createMode));
    }
    // lock path
    if (pathExists(metadatas.get(MetadataIndex.LOCK))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(EMPTY_BYTES);
        zkOps.add(Op.create(logRootPath + LOCK_PATH, EMPTY_BYTES, acl, createMode));
    }
    // read lock path
    if (pathExists(metadatas.get(MetadataIndex.READ_LOCK))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(EMPTY_BYTES);
        zkOps.add(Op.create(logRootPath + READ_LOCK_PATH, EMPTY_BYTES, acl, createMode));
    }
    // log segments path
    if (pathExists(metadatas.get(MetadataIndex.LOGSEGMENTS))) {
        pathsToCreate.add(null);
    } else {
        byte[] logSegmentsData = DLUtils.serializeLogSegmentSequenceNumber(DistributedLogConstants.UNASSIGNED_LOGSEGMENT_SEQNO);
        pathsToCreate.add(logSegmentsData);
        zkOps.add(Op.create(logRootPath + LOGSEGMENTS_PATH, logSegmentsData, acl, createMode));
    }
    // allocation path
    if (ownAllocator) {
        if (pathExists(metadatas.get(MetadataIndex.ALLOCATION))) {
            pathsToCreate.add(null);
        } else {
            pathsToCreate.add(EMPTY_BYTES);
            zkOps.add(Op.create(logRootPath + ALLOCATION_PATH, EMPTY_BYTES, acl, createMode));
        }
    }
    if (zkOps.isEmpty()) {
        // nothing missed
        promise.complete(metadatas);
        return;
    }
    if (!createIfNotExists) {
        promise.completeExceptionally(new LogNotFoundException("Log " + logRootPath + " not found"));
        return;
    }
    getMissingPaths(zk, basePath, Utils.getParent(logRootParentPath)).whenComplete(new FutureEventListener<List<String>>() {

        @Override
        public void onSuccess(List<String> paths) {
            for (String path : paths) {
                pathsToCreate.add(EMPTY_BYTES);
                zkOps.add(0, Op.create(path, EMPTY_BYTES, acl, createMode));
            }
            executeCreateMissingPathTxn(zk, zkOps, pathsToCreate, metadatas, logRootPath, promise);
        }

        @Override
        public void onFailure(Throwable cause) {
            promise.completeExceptionally(cause);
            return;
        }
    });
}
Also used : Op(org.apache.zookeeper.Op) CreateMode(org.apache.zookeeper.CreateMode) List(java.util.List) LinkedList(java.util.LinkedList) LogNotFoundException(org.apache.distributedlog.exceptions.LogNotFoundException)

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