Search in sources :

Example 21 with BinaryOutputArchive

use of org.apache.jute.BinaryOutputArchive in project zookeeper by apache.

the class PrepRequestProcessor method pRequest.

/**
     * This method will be called inside the ProcessRequestThread, which is a
     * singleton, so there will be a single thread calling this code.
     *
     * @param request
     */
protected void pRequest(Request request) throws RequestProcessorException {
    // LOG.info("Prep>>> cxid = " + request.cxid + " type = " +
    // request.type + " id = 0x" + Long.toHexString(request.sessionId));
    request.setHdr(null);
    request.setTxn(null);
    try {
        switch(request.type) {
            case OpCode.createContainer:
            case OpCode.create:
            case OpCode.create2:
                CreateRequest create2Request = new CreateRequest();
                pRequest2Txn(request.type, zks.getNextZxid(), request, create2Request, true);
                break;
            case OpCode.createTTL:
                CreateTTLRequest createTtlRequest = new CreateTTLRequest();
                pRequest2Txn(request.type, zks.getNextZxid(), request, createTtlRequest, true);
                break;
            case OpCode.deleteContainer:
            case OpCode.delete:
                DeleteRequest deleteRequest = new DeleteRequest();
                pRequest2Txn(request.type, zks.getNextZxid(), request, deleteRequest, true);
                break;
            case OpCode.setData:
                SetDataRequest setDataRequest = new SetDataRequest();
                pRequest2Txn(request.type, zks.getNextZxid(), request, setDataRequest, true);
                break;
            case OpCode.reconfig:
                ReconfigRequest reconfigRequest = new ReconfigRequest();
                ByteBufferInputStream.byteBuffer2Record(request.request, reconfigRequest);
                pRequest2Txn(request.type, zks.getNextZxid(), request, reconfigRequest, true);
                break;
            case OpCode.setACL:
                SetACLRequest setAclRequest = new SetACLRequest();
                pRequest2Txn(request.type, zks.getNextZxid(), request, setAclRequest, true);
                break;
            case OpCode.check:
                CheckVersionRequest checkRequest = new CheckVersionRequest();
                pRequest2Txn(request.type, zks.getNextZxid(), request, checkRequest, true);
                break;
            case OpCode.multi:
                MultiTransactionRecord multiRequest = new MultiTransactionRecord();
                try {
                    ByteBufferInputStream.byteBuffer2Record(request.request, multiRequest);
                } catch (IOException e) {
                    request.setHdr(new TxnHeader(request.sessionId, request.cxid, zks.getNextZxid(), Time.currentWallTime(), OpCode.multi));
                    throw e;
                }
                List<Txn> txns = new ArrayList<Txn>();
                //Each op in a multi-op must have the same zxid!
                long zxid = zks.getNextZxid();
                KeeperException ke = null;
                //Store off current pending change records in case we need to rollback
                Map<String, ChangeRecord> pendingChanges = getPendingChanges(multiRequest);
                for (Op op : multiRequest) {
                    Record subrequest = op.toRequestRecord();
                    int type;
                    Record txn;
                    /* If we've already failed one of the ops, don't bother
                     * trying the rest as we know it's going to fail and it
                     * would be confusing in the logfiles.
                     */
                    if (ke != null) {
                        type = OpCode.error;
                        txn = new ErrorTxn(Code.RUNTIMEINCONSISTENCY.intValue());
                    } else /* Prep the request and convert to a Txn */
                    {
                        try {
                            pRequest2Txn(op.getType(), zxid, request, subrequest, false);
                            type = request.getHdr().getType();
                            txn = request.getTxn();
                        } catch (KeeperException e) {
                            ke = e;
                            type = OpCode.error;
                            txn = new ErrorTxn(e.code().intValue());
                            LOG.info("Got user-level KeeperException when processing " + request.toString() + " aborting remaining multi ops." + " Error Path:" + e.getPath() + " Error:" + e.getMessage());
                            request.setException(e);
                            /* Rollback change records from failed multi-op */
                            rollbackPendingChanges(zxid, pendingChanges);
                        }
                    }
                    //FIXME: I don't want to have to serialize it here and then
                    //       immediately deserialize in next processor. But I'm
                    //       not sure how else to get the txn stored into our list.
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
                    txn.serialize(boa, "request");
                    ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
                    txns.add(new Txn(type, bb.array()));
                }
                request.setHdr(new TxnHeader(request.sessionId, request.cxid, zxid, Time.currentWallTime(), request.type));
                request.setTxn(new MultiTxn(txns));
                break;
            //create/close session don't require request record
            case OpCode.createSession:
            case OpCode.closeSession:
                if (!request.isLocalSession()) {
                    pRequest2Txn(request.type, zks.getNextZxid(), request, null, true);
                }
                break;
            //All the rest don't need to create a Txn - just verify session
            case OpCode.sync:
            case OpCode.exists:
            case OpCode.getData:
            case OpCode.getACL:
            case OpCode.getChildren:
            case OpCode.getChildren2:
            case OpCode.ping:
            case OpCode.setWatches:
            case OpCode.checkWatches:
            case OpCode.removeWatches:
                zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
                break;
            default:
                LOG.warn("unknown type " + request.type);
                break;
        }
    } catch (KeeperException e) {
        if (request.getHdr() != null) {
            request.getHdr().setType(OpCode.error);
            request.setTxn(new ErrorTxn(e.code().intValue()));
        }
        LOG.info("Got user-level KeeperException when processing " + request.toString() + " Error Path:" + e.getPath() + " Error:" + e.getMessage());
        request.setException(e);
    } catch (Exception e) {
        // log at error level as we are returning a marshalling
        // error to the user
        LOG.error("Failed to process " + request, e);
        StringBuilder sb = new StringBuilder();
        ByteBuffer bb = request.request;
        if (bb != null) {
            bb.rewind();
            while (bb.hasRemaining()) {
                sb.append(Integer.toHexString(bb.get() & 0xff));
            }
        } else {
            sb.append("request buffer is null");
        }
        LOG.error("Dumping request buffer: 0x" + sb.toString());
        if (request.getHdr() != null) {
            request.getHdr().setType(OpCode.error);
            request.setTxn(new ErrorTxn(Code.MARSHALLINGERROR.intValue()));
        }
    }
    request.zxid = zks.getZxid();
    nextProcessor.processRequest(request);
}
Also used : Op(org.apache.zookeeper.Op) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) CheckVersionRequest(org.apache.zookeeper.proto.CheckVersionRequest) MultiTxn(org.apache.zookeeper.txn.MultiTxn) CreateRequest(org.apache.zookeeper.proto.CreateRequest) ArrayList(java.util.ArrayList) SetDataTxn(org.apache.zookeeper.txn.SetDataTxn) CreateSessionTxn(org.apache.zookeeper.txn.CreateSessionTxn) CreateTxn(org.apache.zookeeper.txn.CreateTxn) CreateTTLTxn(org.apache.zookeeper.txn.CreateTTLTxn) DeleteTxn(org.apache.zookeeper.txn.DeleteTxn) CreateContainerTxn(org.apache.zookeeper.txn.CreateContainerTxn) SetACLTxn(org.apache.zookeeper.txn.SetACLTxn) CheckVersionTxn(org.apache.zookeeper.txn.CheckVersionTxn) Txn(org.apache.zookeeper.txn.Txn) MultiTxn(org.apache.zookeeper.txn.MultiTxn) ErrorTxn(org.apache.zookeeper.txn.ErrorTxn) ReconfigRequest(org.apache.zookeeper.proto.ReconfigRequest) CreateTTLRequest(org.apache.zookeeper.proto.CreateTTLRequest) Record(org.apache.jute.Record) ChangeRecord(org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord) MultiTransactionRecord(org.apache.zookeeper.MultiTransactionRecord) SetACLRequest(org.apache.zookeeper.proto.SetACLRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) XidRolloverException(org.apache.zookeeper.server.quorum.Leader.XidRolloverException) BadArgumentsException(org.apache.zookeeper.KeeperException.BadArgumentsException) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) MultiTransactionRecord(org.apache.zookeeper.MultiTransactionRecord) ErrorTxn(org.apache.zookeeper.txn.ErrorTxn) DeleteRequest(org.apache.zookeeper.proto.DeleteRequest) ChangeRecord(org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord) KeeperException(org.apache.zookeeper.KeeperException) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 22 with BinaryOutputArchive

use of org.apache.jute.BinaryOutputArchive in project zookeeper by apache.

the class CommitProcessorConcurrencyTest method newRequest.

private Request newRequest(Record rec, int type, int sessionId, int xid) throws IOException {
    ByteArrayOutputStream boas = new ByteArrayOutputStream();
    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas);
    rec.serialize(boa, "request");
    ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray());
    return new Request(null, sessionId, xid, type, bb, new ArrayList<Id>());
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) CreateRequest(org.apache.zookeeper.proto.CreateRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) Request(org.apache.zookeeper.server.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Id(org.apache.zookeeper.data.Id) ByteBuffer(java.nio.ByteBuffer)

Example 23 with BinaryOutputArchive

use of org.apache.jute.BinaryOutputArchive in project zookeeper by apache.

the class SessionInvalidationTest method testCreateAfterCloseShouldFail.

/**
     * Test solution for ZOOKEEPER-1208. Verify that operations are not
     * accepted after a close session.
     * 
     * We're using our own marshalling here in order to force an operation
     * after the session is closed (ZooKeeper.class will not allow this). Also
     * by filling the pipe with operations it increases the likelyhood that
     * the server will process the create before FinalRequestProcessor
     * removes the session from the tracker.
     */
@Test
public void testCreateAfterCloseShouldFail() throws Exception {
    for (int i = 0; i < 10; i++) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
        // open a connection
        boa.writeInt(44, "len");
        ConnectRequest conReq = new ConnectRequest(0, 0, 30000, 0, new byte[16]);
        conReq.serialize(boa, "connect");
        // close connection
        boa.writeInt(8, "len");
        RequestHeader h = new RequestHeader(1, ZooDefs.OpCode.closeSession);
        h.serialize(boa, "header");
        // create ephemeral znode
        // We'll fill this in later
        boa.writeInt(52, "len");
        RequestHeader header = new RequestHeader(2, OpCode.create);
        header.serialize(boa, "header");
        CreateRequest createReq = new CreateRequest("/foo" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, 1);
        createReq.serialize(boa, "request");
        baos.close();
        System.out.println("Length:" + baos.toByteArray().length);
        String[] hp = hostPort.split(":");
        Socket sock = new Socket(hp[0], Integer.parseInt(hp[1]));
        InputStream resultStream = null;
        try {
            OutputStream outstream = sock.getOutputStream();
            byte[] data = baos.toByteArray();
            outstream.write(data);
            outstream.flush();
            resultStream = sock.getInputStream();
            byte[] b = new byte[10000];
            int len;
            while ((len = resultStream.read(b)) >= 0) {
                // got results
                System.out.println("gotlen:" + len);
            }
        } finally {
            if (resultStream != null) {
                resultStream.close();
            }
            sock.close();
        }
    }
    ZooKeeper zk = createClient();
    Assert.assertEquals(1, zk.getChildren("/", false).size());
    zk.close();
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) CreateRequest(org.apache.zookeeper.proto.CreateRequest) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConnectRequest(org.apache.zookeeper.proto.ConnectRequest) ZooKeeper(org.apache.zookeeper.ZooKeeper) RequestHeader(org.apache.zookeeper.proto.RequestHeader) Socket(java.net.Socket) Test(org.junit.Test)

Example 24 with BinaryOutputArchive

use of org.apache.jute.BinaryOutputArchive in project zookeeper by apache.

the class TxnLogProposalIterator method next.

/**
     * Proposal returned by this iterator has request part set to null, since
     * it is not used for follower sync-up.
     */
@Override
public Proposal next() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
    Proposal p = new Proposal();
    try {
        TxnHeader hdr = itr.getHeader();
        Record txn = itr.getTxn();
        hdr.serialize(boa, "hdr");
        if (txn != null) {
            txn.serialize(boa, "txn");
        }
        baos.close();
        QuorumPacket pp = new QuorumPacket(Leader.PROPOSAL, itr.getHeader().getZxid(), baos.toByteArray(), null);
        p.packet = pp;
        p.request = null;
        // This is the only place that can throw IO exception
        hasNext = itr.next();
    } catch (IOException e) {
        LOG.error("Unable to read txnlog from disk", e);
        hasNext = false;
    }
    return p;
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) QuorumPacket(org.apache.zookeeper.server.quorum.QuorumPacket) Record(org.apache.jute.Record) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Proposal(org.apache.zookeeper.server.quorum.Leader.Proposal) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Aggregations

BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 ByteBuffer (java.nio.ByteBuffer)11 IOException (java.io.IOException)8 Test (org.junit.Test)7 BinaryInputArchive (org.apache.jute.BinaryInputArchive)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 CreateRequest (org.apache.zookeeper.proto.CreateRequest)5 Record (org.apache.jute.Record)4 Id (org.apache.zookeeper.data.Id)4 TxnHeader (org.apache.zookeeper.txn.TxnHeader)4 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 ACL (org.apache.zookeeper.data.ACL)3 SetDataRequest (org.apache.zookeeper.proto.SetDataRequest)3 Request (org.apache.zookeeper.server.Request)3 CreateTxn (org.apache.zookeeper.txn.CreateTxn)3 Socket (java.net.Socket)2 ArrayList (java.util.ArrayList)2 KeeperException (org.apache.zookeeper.KeeperException)2 Stat (org.apache.zookeeper.data.Stat)2