Search in sources :

Example 36 with RequestHeader

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

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

the class ClientCnxn method close.

/**
 * Close the connection, which includes; send session disconnect to the
 * server, shutdown the send/event threads.
 *
 * @throws IOException
 */
public void close() throws IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Closing client for session: 0x" + Long.toHexString(getSessionId()));
    }
    try {
        RequestHeader h = new RequestHeader();
        h.setType(ZooDefs.OpCode.closeSession);
        submitRequest(h, null, null, null);
    } catch (InterruptedException e) {
    // ignore, close the send/event threads
    } finally {
        disconnect();
    }
}
Also used : RequestHeader(org.apache.zookeeper.proto.RequestHeader)

Aggregations

RequestHeader (org.apache.zookeeper.proto.RequestHeader)37 ReplyHeader (org.apache.zookeeper.proto.ReplyHeader)34 GetDataResponse (org.apache.zookeeper.proto.GetDataResponse)6 Record (org.apache.jute.Record)5 GetDataRequest (org.apache.zookeeper.proto.GetDataRequest)4 SetDataResponse (org.apache.zookeeper.proto.SetDataResponse)4 AuthPacket (org.apache.zookeeper.proto.AuthPacket)3 CreateRequest (org.apache.zookeeper.proto.CreateRequest)3 CreateResponse (org.apache.zookeeper.proto.CreateResponse)3 ExistsRequest (org.apache.zookeeper.proto.ExistsRequest)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 ConnectRequest (org.apache.zookeeper.proto.ConnectRequest)2 Create2Response (org.apache.zookeeper.proto.Create2Response)2 DeleteRequest (org.apache.zookeeper.proto.DeleteRequest)2 GetACLRequest (org.apache.zookeeper.proto.GetACLRequest)2 GetACLResponse (org.apache.zookeeper.proto.GetACLResponse)2 GetChildren2Request (org.apache.zookeeper.proto.GetChildren2Request)2 GetChildren2Response (org.apache.zookeeper.proto.GetChildren2Response)2 GetChildrenRequest (org.apache.zookeeper.proto.GetChildrenRequest)2