Search in sources :

Example 11 with CreateMode

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

the class ZkBaseDataAccessor method doSet.

/**
 * sync set
 */
public AccessResult doSet(String path, T record, int expectVersion, int options) {
    AccessResult result = new AccessResult();
    CreateMode mode = AccessOption.getMode(options);
    if (mode == null) {
        LOG.error("Invalid set mode. options: " + options);
        result._retCode = RetCode.ERROR;
        return result;
    }
    boolean retry;
    do {
        retry = false;
        try {
            Stat stat = _zkClient.writeDataGetStat(path, record, expectVersion);
            DataTree.copyStat(stat, result._stat);
        } catch (ZkNoNodeException e) {
            // node not exists, try create if expectedVersion == -1; in this case, stat will not be set
            if (expectVersion != -1) {
                LOG.error("Could not create node if expectVersion != -1, was " + expectVersion);
                result._retCode = RetCode.ERROR;
                return result;
            }
            try {
                // may create recursively
                AccessResult res = doCreate(path, record, options);
                result._pathCreated.addAll(res._pathCreated);
                RetCode rc = res._retCode;
                switch(rc) {
                    case OK:
                        // not set stat if node is created (instead of set)
                        break;
                    case NODE_EXISTS:
                        retry = true;
                        break;
                    default:
                        LOG.error("Fail to set path by creating: " + path);
                        result._retCode = RetCode.ERROR;
                        return result;
                }
            } catch (Exception e1) {
                LOG.error("Exception while setting path by creating: " + path, e);
                result._retCode = RetCode.ERROR;
                return result;
            }
        } catch (ZkBadVersionException e) {
            LOG.debug("Exception while setting path: " + path, e);
            throw e;
        } catch (Exception e) {
            LOG.error("Exception while setting path: " + path, e);
            result._retCode = RetCode.ERROR;
            return result;
        }
    } while (retry);
    result._retCode = RetCode.OK;
    return result;
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) CreateMode(org.apache.zookeeper.CreateMode) ZkBadVersionException(org.I0Itec.zkclient.exception.ZkBadVersionException) ZkException(org.I0Itec.zkclient.exception.ZkException) HelixMetaDataAccessException(org.apache.helix.api.exceptions.HelixMetaDataAccessException) HelixException(org.apache.helix.HelixException) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) ZkBadVersionException(org.I0Itec.zkclient.exception.ZkBadVersionException)

Example 12 with CreateMode

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

the class ZkBaseDataAccessor method createChildren.

/**
 * async create
 * TODO: rename to create
 */
@Override
public boolean[] createChildren(List<String> paths, List<T> records, int options) {
    boolean[] success = new boolean[paths.size()];
    CreateMode mode = AccessOption.getMode(options);
    if (mode == null) {
        LOG.error("Invalid async create mode. options: " + options);
        return success;
    }
    boolean[] needCreate = new boolean[paths.size()];
    Arrays.fill(needCreate, true);
    List<List<String>> pathsCreated = new ArrayList<List<String>>(Collections.<List<String>>nCopies(paths.size(), null));
    long startT = System.nanoTime();
    try {
        CreateCallbackHandler[] cbList = create(paths, records, needCreate, pathsCreated, options);
        for (int i = 0; i < cbList.length; i++) {
            CreateCallbackHandler cb = cbList[i];
            success[i] = (Code.get(cb.getRc()) == Code.OK);
        }
        return success;
    } finally {
        long endT = System.nanoTime();
        if (LOG.isTraceEnabled()) {
            LOG.trace("create_async, size: " + paths.size() + ", paths: " + paths.get(0) + ",... time: " + (endT - startT) + " ns");
        }
    }
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CreateCallbackHandler(org.apache.helix.manager.zk.ZkAsyncCallbacks.CreateCallbackHandler)

Example 13 with CreateMode

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

the class ZKLogMetadataForWriter method createMissingMetadata.

static void createMissingMetadata(final ZooKeeper zk, final String logRootPath, final List<Versioned<byte[]>> metadatas, final List<ACL> acl, final boolean ownAllocator, final boolean createIfNotExists, final Promise<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
    if (pathExists(metadatas.get(MetadataIndex.LOG_ROOT_PARENT))) {
        pathsToCreate.add(null);
    } else {
        String logRootParentPath = new File(logRootPath).getParent();
        pathsToCreate.add(DistributedLogConstants.EMPTY_BYTES);
        zkOps.add(Op.create(logRootParentPath, DistributedLogConstants.EMPTY_BYTES, acl, createMode));
    }
    // log root path
    if (pathExists(metadatas.get(MetadataIndex.LOG_ROOT))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(DistributedLogConstants.EMPTY_BYTES);
        zkOps.add(Op.create(logRootPath, DistributedLogConstants.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(DistributedLogConstants.EMPTY_BYTES);
        zkOps.add(Op.create(logRootPath + LOCK_PATH, DistributedLogConstants.EMPTY_BYTES, acl, createMode));
    }
    // read lock path
    if (pathExists(metadatas.get(MetadataIndex.READ_LOCK))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(DistributedLogConstants.EMPTY_BYTES);
        zkOps.add(Op.create(logRootPath + READ_LOCK_PATH, DistributedLogConstants.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(DistributedLogConstants.EMPTY_BYTES);
            zkOps.add(Op.create(logRootPath + ALLOCATION_PATH, DistributedLogConstants.EMPTY_BYTES, acl, createMode));
        }
    }
    if (zkOps.isEmpty()) {
        // nothing missed
        promise.setValue(metadatas);
        return;
    }
    if (!createIfNotExists) {
        promise.setException(new LogNotFoundException("Log " + logRootPath + " not found"));
        return;
    }
    zk.multi(zkOps, new AsyncCallback.MultiCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, List<OpResult> resultList) {
            if (KeeperException.Code.OK.intValue() == rc) {
                List<Versioned<byte[]>> finalMetadatas = Lists.newArrayListWithExpectedSize(metadatas.size());
                for (int i = 0; i < pathsToCreate.size(); i++) {
                    byte[] dataCreated = pathsToCreate.get(i);
                    if (null == dataCreated) {
                        finalMetadatas.add(metadatas.get(i));
                    } else {
                        finalMetadatas.add(new Versioned<byte[]>(dataCreated, new ZkVersion(0)));
                    }
                }
                promise.setValue(finalMetadatas);
            } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                promise.setException(new LogExistsException("Someone just created log " + logRootPath));
            } else {
                if (LOG.isDebugEnabled()) {
                    StringBuilder builder = new StringBuilder();
                    for (OpResult result : resultList) {
                        if (result instanceof OpResult.ErrorResult) {
                            OpResult.ErrorResult errorResult = (OpResult.ErrorResult) result;
                            builder.append(errorResult.getErr()).append(",");
                        } else {
                            builder.append(0).append(",");
                        }
                    }
                    String resultCodeList = builder.substring(0, builder.length() - 1);
                    LOG.debug("Failed to create log, full rc list = {}", resultCodeList);
                }
                promise.setException(new ZKException("Failed to create log " + logRootPath, KeeperException.Code.get(rc)));
            }
        }
    }, null);
}
Also used : Op(org.apache.zookeeper.Op) LogExistsException(com.twitter.distributedlog.exceptions.LogExistsException) Versioned(org.apache.bookkeeper.versioning.Versioned) AsyncCallback(org.apache.zookeeper.AsyncCallback) ZKException(com.twitter.distributedlog.exceptions.ZKException) CreateMode(org.apache.zookeeper.CreateMode) OpResult(org.apache.zookeeper.OpResult) List(java.util.List) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException) File(java.io.File) ZkVersion(org.apache.bookkeeper.meta.ZkVersion)

Example 14 with CreateMode

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

the class QuorumZooKeeperServer method checkUpgradeSession.

public Request checkUpgradeSession(Request request) throws IOException, KeeperException {
    if (request.isThrottled()) {
        return null;
    }
    // So will not be called concurrently by two threads.
    if ((request.type != OpCode.create && request.type != OpCode.create2 && request.type != OpCode.multi) || !upgradeableSessionTracker.isLocalSession(request.sessionId)) {
        return null;
    }
    if (OpCode.multi == request.type) {
        MultiOperationRecord multiTransactionRecord = new MultiOperationRecord();
        request.request.rewind();
        ByteBufferInputStream.byteBuffer2Record(request.request, multiTransactionRecord);
        request.request.rewind();
        boolean containsEphemeralCreate = false;
        for (Op op : multiTransactionRecord) {
            if (op.getType() == OpCode.create || op.getType() == OpCode.create2) {
                CreateRequest createRequest = (CreateRequest) op.toRequestRecord();
                CreateMode createMode = CreateMode.fromFlag(createRequest.getFlags());
                if (createMode.isEphemeral()) {
                    containsEphemeralCreate = true;
                    break;
                }
            }
        }
        if (!containsEphemeralCreate) {
            return null;
        }
    } else {
        CreateRequest createRequest = new CreateRequest();
        request.request.rewind();
        ByteBufferInputStream.byteBuffer2Record(request.request, createRequest);
        request.request.rewind();
        CreateMode createMode = CreateMode.fromFlag(createRequest.getFlags());
        if (!createMode.isEphemeral()) {
            return null;
        }
    }
    // Uh oh.  We need to upgrade before we can proceed.
    if (!self.isLocalSessionsUpgradingEnabled()) {
        throw new KeeperException.EphemeralOnLocalSessionException();
    }
    return makeUpgradeRequest(request.sessionId);
}
Also used : Op(org.apache.zookeeper.Op) MultiOperationRecord(org.apache.zookeeper.MultiOperationRecord) CreateMode(org.apache.zookeeper.CreateMode) CreateRequest(org.apache.zookeeper.proto.CreateRequest)

Example 15 with CreateMode

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

the class ZooKeeperProducer method createNode.

private OperationResult<String> createNode(ProductionContext ctx) throws Exception {
    CreateOperation create = new CreateOperation(ctx.connection, ctx.node);
    create.setPermissions(getAclListFromMessage(ctx.exchange.getIn()));
    CreateMode mode = null;
    String modeString = configuration.getCreateMode();
    if (modeString != null) {
        try {
            mode = getCreateModeFromString(modeString, CreateMode.EPHEMERAL);
        } catch (Exception e) {
        }
    } else {
        mode = getCreateMode(ctx.exchange.getIn(), CreateMode.EPHEMERAL);
    }
    create.setCreateMode(mode == null ? CreateMode.EPHEMERAL : mode);
    create.setData(ctx.payload);
    return create.get();
}
Also used : CreateOperation(org.apache.camel.component.zookeeper.operations.CreateOperation) CreateMode(org.apache.zookeeper.CreateMode) ZooKeeperUtils.getCreateMode(org.apache.camel.component.zookeeper.ZooKeeperUtils.getCreateMode) ZooKeeperUtils.getCreateModeFromString(org.apache.camel.component.zookeeper.ZooKeeperUtils.getCreateModeFromString)

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