Search in sources :

Example 11 with SetDataRequest

use of org.apache.zookeeper.proto.SetDataRequest 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 12 with SetDataRequest

use of org.apache.zookeeper.proto.SetDataRequest in project zookeeper by apache.

the class ZooKeeper method setData.

/**
 * Set the data for the node of the given path if such a node exists and the
 * given version matches the version of the node (if the given version is
 * -1, it matches any node's versions). Return the stat of the node.
 * <p>
 * This operation, if successful, will trigger all the watches on the node
 * of the given path left by getData calls.
 * <p>
 * A KeeperException with error code KeeperException.NoNode will be thrown
 * if no node with the given path exists.
 * <p>
 * A KeeperException with error code KeeperException.BadVersion will be
 * thrown if the given version does not match the node's version.
 * <p>
 * The maximum allowable size of the data array is 1 MB (1,048,576 bytes).
 * Arrays larger than this will cause a KeeperException to be thrown.
 *
 * @param path
 *                the path of the node
 * @param data
 *                the data to set
 * @param version
 *                the expected matching version
 * @return the state of the node
 * @throws InterruptedException If the server transaction is interrupted.
 * @throws KeeperException If the server signals an error with a non-zero error code.
 * @throws IllegalArgumentException if an invalid path is specified
 */
public Stat setData(final String path, byte[] data, int version) throws KeeperException, InterruptedException {
    final String clientPath = path;
    PathUtils.validatePath(clientPath);
    final String serverPath = prependChroot(clientPath);
    RequestHeader h = new RequestHeader();
    h.setType(ZooDefs.OpCode.setData);
    SetDataRequest request = new SetDataRequest();
    request.setPath(serverPath);
    request.setData(data);
    request.setVersion(version);
    SetDataResponse response = new SetDataResponse();
    ReplyHeader r = cnxn.submitRequest(h, request, response, null);
    if (r.getErr() != 0) {
        throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath);
    }
    return response.getStat();
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) RequestHeader(org.apache.zookeeper.proto.RequestHeader) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) SetDataResponse(org.apache.zookeeper.proto.SetDataResponse)

Example 13 with SetDataRequest

use of org.apache.zookeeper.proto.SetDataRequest in project zookeeper by apache.

the class ZooKeeper method setData.

/**
 * The asynchronous version of setData.
 *
 * @see #setData(String, byte[], int)
 */
public void setData(final String path, byte[] data, int version, StatCallback cb, Object ctx) {
    final String clientPath = path;
    PathUtils.validatePath(clientPath);
    final String serverPath = prependChroot(clientPath);
    RequestHeader h = new RequestHeader();
    h.setType(ZooDefs.OpCode.setData);
    SetDataRequest request = new SetDataRequest();
    request.setPath(serverPath);
    request.setData(data);
    request.setVersion(version);
    SetDataResponse response = new SetDataResponse();
    cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, null);
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) RequestHeader(org.apache.zookeeper.proto.RequestHeader) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) SetDataResponse(org.apache.zookeeper.proto.SetDataResponse)

Example 14 with SetDataRequest

use of org.apache.zookeeper.proto.SetDataRequest in project zookeeper by apache.

the class ToStringTest method testJuteToString.

/**
 * Verify jute - which we've had particular problems with in the past
 * wrt null fields
 */
@Test
public void testJuteToString() {
    SetDataRequest req = new SetDataRequest(null, null, 0);
    assertNotSame("ERROR", req.toString());
}
Also used : SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) Test(org.junit.jupiter.api.Test)

Example 15 with SetDataRequest

use of org.apache.zookeeper.proto.SetDataRequest in project hbase by apache.

the class RecoverableZooKeeper method prepareZKMulti.

/**
 * Convert Iterable of {@link org.apache.zookeeper.Op} we got into the ZooKeeper.Op
 * instances to actually pass to multi (need to do this in order to appendMetaData).
 */
private Iterable<Op> prepareZKMulti(Iterable<Op> ops) throws UnsupportedOperationException {
    if (ops == null) {
        return null;
    }
    List<Op> preparedOps = new LinkedList<>();
    for (Op op : ops) {
        if (op.getType() == ZooDefs.OpCode.create) {
            CreateRequest create = (CreateRequest) op.toRequestRecord();
            preparedOps.add(Op.create(create.getPath(), ZKMetadata.appendMetaData(id, create.getData()), create.getAcl(), create.getFlags()));
        } else if (op.getType() == ZooDefs.OpCode.delete) {
            // no need to appendMetaData for delete
            preparedOps.add(op);
        } else if (op.getType() == ZooDefs.OpCode.setData) {
            SetDataRequest setData = (SetDataRequest) op.toRequestRecord();
            preparedOps.add(Op.setData(setData.getPath(), ZKMetadata.appendMetaData(id, setData.getData()), setData.getVersion()));
        } else {
            throw new UnsupportedOperationException("Unexpected ZKOp type: " + op.getClass().getName());
        }
    }
    return preparedOps;
}
Also used : Op(org.apache.zookeeper.Op) CreateRequest(org.apache.zookeeper.proto.CreateRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) LinkedList(java.util.LinkedList)

Aggregations

SetDataRequest (org.apache.zookeeper.proto.SetDataRequest)16 CreateRequest (org.apache.zookeeper.proto.CreateRequest)10 DeleteRequest (org.apache.zookeeper.proto.DeleteRequest)8 SetACLRequest (org.apache.zookeeper.proto.SetACLRequest)7 CheckVersionRequest (org.apache.zookeeper.proto.CheckVersionRequest)6 IOException (java.io.IOException)5 KeeperException (org.apache.zookeeper.KeeperException)5 BadArgumentsException (org.apache.zookeeper.KeeperException.BadArgumentsException)5 ChangeRecord (org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord)5 CheckVersionTxn (org.apache.zookeeper.txn.CheckVersionTxn)5 CreateSessionTxn (org.apache.zookeeper.txn.CreateSessionTxn)5 DeleteTxn (org.apache.zookeeper.txn.DeleteTxn)5 SetACLTxn (org.apache.zookeeper.txn.SetACLTxn)5 SetDataTxn (org.apache.zookeeper.txn.SetDataTxn)5 TxnHeader (org.apache.zookeeper.txn.TxnHeader)5 ArrayList (java.util.ArrayList)4 Op (org.apache.zookeeper.Op)4 ReconfigRequest (org.apache.zookeeper.proto.ReconfigRequest)4 CreateTxn (org.apache.zookeeper.txn.CreateTxn)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3