Search in sources :

Example 21 with CreateMode

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

the class PrepRequestProcessor method pRequest2Txn.

/**
 * This method will be called inside the ProcessRequestThread, which is a
 * singleton, so there will be a single thread calling this code.
 *
 * @param type
 * @param zxid
 * @param request
 * @param record
 */
@SuppressWarnings("unchecked")
protected void pRequest2Txn(int type, long zxid, Request request, Record record, boolean deserialize) throws KeeperException, IOException, RequestProcessorException {
    request.hdr = new TxnHeader(request.sessionId, request.cxid, zxid, zks.getTime(), type);
    switch(type) {
        case OpCode.create:
            zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
            CreateRequest createRequest = (CreateRequest) record;
            if (deserialize)
                ByteBufferInputStream.byteBuffer2Record(request.request, createRequest);
            String path = createRequest.getPath();
            int lastSlash = path.lastIndexOf('/');
            if (lastSlash == -1 || path.indexOf('\0') != -1 || failCreate) {
                LOG.info("Invalid path " + path + " with session 0x" + Long.toHexString(request.sessionId));
                throw new KeeperException.BadArgumentsException(path);
            }
            List<ACL> listACL = removeDuplicates(createRequest.getAcl());
            if (!fixupACL(request.authInfo, listACL)) {
                throw new KeeperException.InvalidACLException(path);
            }
            String parentPath = path.substring(0, lastSlash);
            ChangeRecord parentRecord = getRecordForPath(parentPath);
            checkACL(zks, parentRecord.acl, ZooDefs.Perms.CREATE, request.authInfo);
            int parentCVersion = parentRecord.stat.getCversion();
            CreateMode createMode = CreateMode.fromFlag(createRequest.getFlags());
            if (createMode.isSequential()) {
                path = path + String.format(Locale.ENGLISH, "%010d", parentCVersion);
            }
            validatePath(path, request.sessionId);
            try {
                if (getRecordForPath(path) != null) {
                    throw new KeeperException.NodeExistsException(path);
                }
            } catch (KeeperException.NoNodeException e) {
            // ignore this one
            }
            boolean ephemeralParent = parentRecord.stat.getEphemeralOwner() != 0;
            if (ephemeralParent) {
                throw new KeeperException.NoChildrenForEphemeralsException(path);
            }
            int newCversion = parentRecord.stat.getCversion() + 1;
            request.txn = new CreateTxn(path, createRequest.getData(), listACL, createMode.isEphemeral(), newCversion);
            StatPersisted s = new StatPersisted();
            if (createMode.isEphemeral()) {
                s.setEphemeralOwner(request.sessionId);
            }
            parentRecord = parentRecord.duplicate(request.hdr.getZxid());
            parentRecord.childCount++;
            parentRecord.stat.setCversion(newCversion);
            addChangeRecord(parentRecord);
            addChangeRecord(new ChangeRecord(request.hdr.getZxid(), path, s, 0, listACL));
            break;
        case OpCode.delete:
            zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
            DeleteRequest deleteRequest = (DeleteRequest) record;
            if (deserialize)
                ByteBufferInputStream.byteBuffer2Record(request.request, deleteRequest);
            path = deleteRequest.getPath();
            lastSlash = path.lastIndexOf('/');
            if (lastSlash == -1 || path.indexOf('\0') != -1 || zks.getZKDatabase().isSpecialPath(path)) {
                throw new KeeperException.BadArgumentsException(path);
            }
            parentPath = path.substring(0, lastSlash);
            parentRecord = getRecordForPath(parentPath);
            ChangeRecord nodeRecord = getRecordForPath(path);
            checkACL(zks, parentRecord.acl, ZooDefs.Perms.DELETE, request.authInfo);
            int version = deleteRequest.getVersion();
            if (version != -1 && nodeRecord.stat.getVersion() != version) {
                throw new KeeperException.BadVersionException(path);
            }
            if (nodeRecord.childCount > 0) {
                throw new KeeperException.NotEmptyException(path);
            }
            request.txn = new DeleteTxn(path);
            parentRecord = parentRecord.duplicate(request.hdr.getZxid());
            parentRecord.childCount--;
            addChangeRecord(parentRecord);
            addChangeRecord(new ChangeRecord(request.hdr.getZxid(), path, null, -1, null));
            break;
        case OpCode.setData:
            zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
            SetDataRequest setDataRequest = (SetDataRequest) record;
            if (deserialize)
                ByteBufferInputStream.byteBuffer2Record(request.request, setDataRequest);
            path = setDataRequest.getPath();
            validatePath(path, request.sessionId);
            nodeRecord = getRecordForPath(path);
            checkACL(zks, nodeRecord.acl, ZooDefs.Perms.WRITE, request.authInfo);
            version = setDataRequest.getVersion();
            int currentVersion = nodeRecord.stat.getVersion();
            if (version != -1 && version != currentVersion) {
                throw new KeeperException.BadVersionException(path);
            }
            version = currentVersion + 1;
            request.txn = new SetDataTxn(path, setDataRequest.getData(), version);
            nodeRecord = nodeRecord.duplicate(request.hdr.getZxid());
            nodeRecord.stat.setVersion(version);
            addChangeRecord(nodeRecord);
            break;
        case OpCode.setACL:
            zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
            SetACLRequest setAclRequest = (SetACLRequest) record;
            if (deserialize)
                ByteBufferInputStream.byteBuffer2Record(request.request, setAclRequest);
            path = setAclRequest.getPath();
            validatePath(path, request.sessionId);
            listACL = removeDuplicates(setAclRequest.getAcl());
            if (!fixupACL(request.authInfo, listACL)) {
                throw new KeeperException.InvalidACLException(path);
            }
            nodeRecord = getRecordForPath(path);
            checkACL(zks, nodeRecord.acl, ZooDefs.Perms.ADMIN, request.authInfo);
            version = setAclRequest.getVersion();
            currentVersion = nodeRecord.stat.getAversion();
            if (version != -1 && version != currentVersion) {
                throw new KeeperException.BadVersionException(path);
            }
            version = currentVersion + 1;
            request.txn = new SetACLTxn(path, listACL, version);
            nodeRecord = nodeRecord.duplicate(request.hdr.getZxid());
            nodeRecord.stat.setAversion(version);
            addChangeRecord(nodeRecord);
            break;
        case OpCode.createSession:
            request.request.rewind();
            int to = request.request.getInt();
            request.txn = new CreateSessionTxn(to);
            request.request.rewind();
            zks.sessionTracker.addSession(request.sessionId, to);
            zks.setOwner(request.sessionId, request.getOwner());
            break;
        case OpCode.closeSession:
            // We don't want to do this check since the session expiration thread
            // queues up this operation without being the session owner.
            // this request is the last of the session so it should be ok
            // zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
            HashSet<String> es = zks.getZKDatabase().getEphemerals(request.sessionId);
            synchronized (zks.outstandingChanges) {
                for (ChangeRecord c : zks.outstandingChanges) {
                    if (c.stat == null) {
                        // Doing a delete
                        es.remove(c.path);
                    } else if (c.stat.getEphemeralOwner() == request.sessionId) {
                        es.add(c.path);
                    }
                }
                for (String path2Delete : es) {
                    addChangeRecord(new ChangeRecord(request.hdr.getZxid(), path2Delete, null, 0, null));
                }
                zks.sessionTracker.setSessionClosing(request.sessionId);
            }
            LOG.info("Processed session termination for sessionid: 0x" + Long.toHexString(request.sessionId));
            break;
        case OpCode.check:
            zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
            CheckVersionRequest checkVersionRequest = (CheckVersionRequest) record;
            if (deserialize)
                ByteBufferInputStream.byteBuffer2Record(request.request, checkVersionRequest);
            path = checkVersionRequest.getPath();
            validatePath(path, request.sessionId);
            nodeRecord = getRecordForPath(path);
            checkACL(zks, nodeRecord.acl, ZooDefs.Perms.READ, request.authInfo);
            version = checkVersionRequest.getVersion();
            currentVersion = nodeRecord.stat.getVersion();
            if (version != -1 && version != currentVersion) {
                throw new KeeperException.BadVersionException(path);
            }
            version = currentVersion + 1;
            request.txn = new CheckVersionTxn(path, version);
            break;
    }
}
Also used : CheckVersionRequest(org.apache.zookeeper.proto.CheckVersionRequest) CreateRequest(org.apache.zookeeper.proto.CreateRequest) DeleteTxn(org.apache.zookeeper.txn.DeleteTxn) CreateSessionTxn(org.apache.zookeeper.txn.CreateSessionTxn) BadArgumentsException(org.apache.zookeeper.KeeperException.BadArgumentsException) SetACLRequest(org.apache.zookeeper.proto.SetACLRequest) CheckVersionTxn(org.apache.zookeeper.txn.CheckVersionTxn) ACL(org.apache.zookeeper.data.ACL) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) SetDataTxn(org.apache.zookeeper.txn.SetDataTxn) CreateTxn(org.apache.zookeeper.txn.CreateTxn) CreateMode(org.apache.zookeeper.CreateMode) SetACLTxn(org.apache.zookeeper.txn.SetACLTxn) StatPersisted(org.apache.zookeeper.data.StatPersisted) ChangeRecord(org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord) DeleteRequest(org.apache.zookeeper.proto.DeleteRequest) KeeperException(org.apache.zookeeper.KeeperException) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 22 with CreateMode

use of org.apache.zookeeper.CreateMode in project BRFS by zhangnianli.

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);
            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 23 with CreateMode

use of org.apache.zookeeper.CreateMode in project zookeeper by apache.

the class PrepRequestProcessor method pRequest2TxnCreate.

private void pRequest2TxnCreate(int type, Request request, Record record, boolean deserialize) throws IOException, KeeperException {
    if (deserialize) {
        ByteBufferInputStream.byteBuffer2Record(request.request, record);
    }
    int flags;
    String path;
    List<ACL> acl;
    byte[] data;
    long ttl;
    if (type == OpCode.createTTL) {
        CreateTTLRequest createTtlRequest = (CreateTTLRequest) record;
        flags = createTtlRequest.getFlags();
        path = createTtlRequest.getPath();
        acl = createTtlRequest.getAcl();
        data = createTtlRequest.getData();
        ttl = createTtlRequest.getTtl();
    } else {
        CreateRequest createRequest = (CreateRequest) record;
        flags = createRequest.getFlags();
        path = createRequest.getPath();
        acl = createRequest.getAcl();
        data = createRequest.getData();
        ttl = -1;
    }
    CreateMode createMode = CreateMode.fromFlag(flags);
    validateCreateRequest(path, createMode, request, ttl);
    String parentPath = validatePathForCreate(path, request.sessionId);
    List<ACL> listACL = fixupACL(path, request.authInfo, acl);
    ChangeRecord parentRecord = getRecordForPath(parentPath);
    zks.checkACL(request.cnxn, parentRecord.acl, ZooDefs.Perms.CREATE, request.authInfo, path, listACL);
    int parentCVersion = parentRecord.stat.getCversion();
    if (createMode.isSequential()) {
        path = path + String.format(Locale.ENGLISH, "%010d", parentCVersion);
    }
    validatePath(path, request.sessionId);
    try {
        if (getRecordForPath(path) != null) {
            throw new KeeperException.NodeExistsException(path);
        }
    } catch (KeeperException.NoNodeException e) {
    // ignore this one
    }
    boolean ephemeralParent = EphemeralType.get(parentRecord.stat.getEphemeralOwner()) == EphemeralType.NORMAL;
    if (ephemeralParent) {
        throw new KeeperException.NoChildrenForEphemeralsException(path);
    }
    int newCversion = parentRecord.stat.getCversion() + 1;
    zks.checkQuota(path, null, data, OpCode.create);
    if (type == OpCode.createContainer) {
        request.setTxn(new CreateContainerTxn(path, data, listACL, newCversion));
    } else if (type == OpCode.createTTL) {
        request.setTxn(new CreateTTLTxn(path, data, listACL, newCversion, ttl));
    } else {
        request.setTxn(new CreateTxn(path, data, listACL, createMode.isEphemeral(), newCversion));
    }
    TxnHeader hdr = request.getHdr();
    long ephemeralOwner = 0;
    if (createMode.isContainer()) {
        ephemeralOwner = EphemeralType.CONTAINER_EPHEMERAL_OWNER;
    } else if (createMode.isTTL()) {
        ephemeralOwner = EphemeralType.TTL.toEphemeralOwner(ttl);
    } else if (createMode.isEphemeral()) {
        ephemeralOwner = request.sessionId;
    }
    StatPersisted s = DataTree.createStat(hdr.getZxid(), hdr.getTime(), ephemeralOwner);
    parentRecord = parentRecord.duplicate(request.getHdr().getZxid());
    parentRecord.childCount++;
    parentRecord.stat.setCversion(newCversion);
    parentRecord.stat.setPzxid(request.getHdr().getZxid());
    parentRecord.precalculatedDigest = precalculateDigest(DigestOpCode.UPDATE, parentPath, parentRecord.data, parentRecord.stat);
    addChangeRecord(parentRecord);
    ChangeRecord nodeRecord = new ChangeRecord(request.getHdr().getZxid(), path, s, 0, listACL);
    nodeRecord.data = data;
    nodeRecord.precalculatedDigest = precalculateDigest(DigestOpCode.ADD, path, nodeRecord.data, s);
    setTxnDigest(request, nodeRecord.precalculatedDigest);
    addChangeRecord(nodeRecord);
}
Also used : CreateContainerTxn(org.apache.zookeeper.txn.CreateContainerTxn) CreateRequest(org.apache.zookeeper.proto.CreateRequest) ACL(org.apache.zookeeper.data.ACL) CreateTTLRequest(org.apache.zookeeper.proto.CreateTTLRequest) CreateTTLTxn(org.apache.zookeeper.txn.CreateTTLTxn) CreateTxn(org.apache.zookeeper.txn.CreateTxn) CreateMode(org.apache.zookeeper.CreateMode) StatPersisted(org.apache.zookeeper.data.StatPersisted) ChangeRecord(org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord) KeeperException(org.apache.zookeeper.KeeperException) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 24 with CreateMode

use of org.apache.zookeeper.CreateMode in project zookeeper by apache.

the class CreateCommand method exec.

@Override
public boolean exec() throws CliException {
    boolean hasE = cl.hasOption("e");
    boolean hasS = cl.hasOption("s");
    boolean hasC = cl.hasOption("c");
    boolean hasT = cl.hasOption("t");
    if (hasC && (hasE || hasS)) {
        throw new MalformedCommandException("-c cannot be combined with -s or -e. Containers cannot be ephemeral or sequential.");
    }
    long ttl;
    try {
        ttl = hasT ? Long.parseLong(cl.getOptionValue("t")) : 0;
    } catch (NumberFormatException e) {
        throw new MalformedCommandException("-t argument must be a long value");
    }
    if (hasT && hasE) {
        throw new MalformedCommandException("TTLs cannot be used with Ephemeral znodes");
    }
    if (hasT && hasC) {
        throw new MalformedCommandException("TTLs cannot be used with Container znodes");
    }
    CreateMode flags;
    if (hasE && hasS) {
        flags = CreateMode.EPHEMERAL_SEQUENTIAL;
    } else if (hasE) {
        flags = CreateMode.EPHEMERAL;
    } else if (hasS) {
        flags = hasT ? CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL : CreateMode.PERSISTENT_SEQUENTIAL;
    } else if (hasC) {
        flags = CreateMode.CONTAINER;
    } else {
        flags = hasT ? CreateMode.PERSISTENT_WITH_TTL : CreateMode.PERSISTENT;
    }
    if (hasT) {
        try {
            EphemeralType.TTL.toEphemeralOwner(ttl);
        } catch (IllegalArgumentException e) {
            throw new MalformedCommandException(e.getMessage());
        }
    }
    String path = args[1];
    byte[] data = null;
    if (args.length > 2) {
        data = args[2].getBytes(UTF_8);
    }
    List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
    if (args.length > 3) {
        acl = AclParser.parse(args[3]);
    }
    try {
        String newPath = hasT ? zk.create(path, data, acl, flags, new Stat(), ttl) : zk.create(path, data, acl, flags);
        err.println("Created " + newPath);
    } catch (IllegalArgumentException ex) {
        throw new MalformedPathException(ex.getMessage());
    } catch (KeeperException.EphemeralOnLocalSessionException e) {
        err.println("Unable to create ephemeral node on a local session");
        throw new CliWrapperException(e);
    } catch (KeeperException.InvalidACLException ex) {
        err.println(ex.getMessage());
        throw new CliWrapperException(ex);
    } catch (KeeperException | InterruptedException ex) {
        throw new CliWrapperException(ex);
    }
    return true;
}
Also used : ACL(org.apache.zookeeper.data.ACL) Stat(org.apache.zookeeper.data.Stat) CreateMode(org.apache.zookeeper.CreateMode) KeeperException(org.apache.zookeeper.KeeperException)

Example 25 with CreateMode

use of org.apache.zookeeper.CreateMode in project zookeeper by apache.

the class CreateModeTest method testBasicCreateMode.

@Test
public void testBasicCreateMode() {
    CreateMode cm = CreateMode.PERSISTENT;
    assertEquals(cm.toFlag(), 0);
    assertFalse(cm.isEphemeral());
    assertFalse(cm.isSequential());
    assertFalse(cm.isContainer());
    cm = CreateMode.EPHEMERAL;
    assertEquals(cm.toFlag(), 1);
    assertTrue(cm.isEphemeral());
    assertFalse(cm.isSequential());
    assertFalse(cm.isContainer());
    cm = CreateMode.PERSISTENT_SEQUENTIAL;
    assertEquals(cm.toFlag(), 2);
    assertFalse(cm.isEphemeral());
    assertTrue(cm.isSequential());
    assertFalse(cm.isContainer());
    cm = CreateMode.EPHEMERAL_SEQUENTIAL;
    assertEquals(cm.toFlag(), 3);
    assertTrue(cm.isEphemeral());
    assertTrue(cm.isSequential());
    assertFalse(cm.isContainer());
    cm = CreateMode.CONTAINER;
    assertEquals(cm.toFlag(), 4);
    assertFalse(cm.isEphemeral());
    assertFalse(cm.isSequential());
    assertTrue(cm.isContainer());
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) Test(org.junit.jupiter.api.Test)

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