Search in sources :

Example 1 with ConnectRequest

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

the class ZooKeeperServer method processConnectRequest.

public void processConnectRequest(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOException {
    BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(incomingBuffer));
    ConnectRequest connReq = new ConnectRequest();
    connReq.deserialize(bia, "connect");
    if (LOG.isDebugEnabled()) {
        LOG.debug("Session establishment request from client " + cnxn.getRemoteSocketAddress() + " client's lastZxid is 0x" + Long.toHexString(connReq.getLastZxidSeen()));
    }
    boolean readOnly = false;
    try {
        readOnly = bia.readBool("readOnly");
        cnxn.isOldClient = false;
    } catch (IOException e) {
        // this is ok -- just a packet from an old client which
        // doesn't contain readOnly field
        LOG.warn("Connection request from old client " + cnxn.getRemoteSocketAddress() + "; will be dropped if server is in r-o mode");
    }
    if (!readOnly && this instanceof ReadOnlyZooKeeperServer) {
        String msg = "Refusing session request for not-read-only client " + cnxn.getRemoteSocketAddress();
        LOG.info(msg);
        throw new CloseRequestException(msg);
    }
    if (connReq.getLastZxidSeen() > zkDb.dataTree.lastProcessedZxid) {
        String msg = "Refusing session request for client " + cnxn.getRemoteSocketAddress() + " as it has seen zxid 0x" + Long.toHexString(connReq.getLastZxidSeen()) + " our last zxid is 0x" + Long.toHexString(getZKDatabase().getDataTreeLastProcessedZxid()) + " client must try another server";
        LOG.info(msg);
        throw new CloseRequestException(msg);
    }
    int sessionTimeout = connReq.getTimeOut();
    byte[] passwd = connReq.getPasswd();
    int minSessionTimeout = getMinSessionTimeout();
    if (sessionTimeout < minSessionTimeout) {
        sessionTimeout = minSessionTimeout;
    }
    int maxSessionTimeout = getMaxSessionTimeout();
    if (sessionTimeout > maxSessionTimeout) {
        sessionTimeout = maxSessionTimeout;
    }
    cnxn.setSessionTimeout(sessionTimeout);
    // We don't want to receive any packets until we are sure that the
    // session is setup
    cnxn.disableRecv();
    long sessionId = connReq.getSessionId();
    if (sessionId == 0) {
        LOG.info("Client attempting to establish new session at " + cnxn.getRemoteSocketAddress());
        createSession(cnxn, passwd, sessionTimeout);
    } else {
        long clientSessionId = connReq.getSessionId();
        LOG.info("Client attempting to renew session 0x" + Long.toHexString(clientSessionId) + " at " + cnxn.getRemoteSocketAddress());
        if (serverCnxnFactory != null) {
            serverCnxnFactory.closeSession(sessionId);
        }
        if (secureServerCnxnFactory != null) {
            secureServerCnxnFactory.closeSession(sessionId);
        }
        cnxn.setSessionId(sessionId);
        reopenSession(cnxn, sessionId, passwd, sessionTimeout);
    }
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) ConnectRequest(org.apache.zookeeper.proto.ConnectRequest) CloseRequestException(org.apache.zookeeper.server.ServerCnxn.CloseRequestException) IOException(java.io.IOException) ReadOnlyZooKeeperServer(org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer)

Example 2 with ConnectRequest

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

the class WatchLeakTest method createConnRequest.

/**
     * Create a connection request
     *
     * @return a serialized connection request
     */
private ByteBuffer createConnRequest() {
    Random r = new Random(SESSION_ID ^ superSecret);
    byte[] p = new byte[16];
    r.nextBytes(p);
    ConnectRequest conReq = new ConnectRequest(0, 1L, 30000, SESSION_ID, p);
    MockPacket packet = new MockPacket(null, null, conReq, null, null, false);
    return packet.createAndReturnBB();
}
Also used : ConnectRequest(org.apache.zookeeper.proto.ConnectRequest) Random(java.util.Random) MockPacket(org.apache.zookeeper.MockPacket)

Example 3 with ConnectRequest

use of org.apache.zookeeper.proto.ConnectRequest 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)

Aggregations

ConnectRequest (org.apache.zookeeper.proto.ConnectRequest)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 Random (java.util.Random)1 BinaryInputArchive (org.apache.jute.BinaryInputArchive)1 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)1 MockPacket (org.apache.zookeeper.MockPacket)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 CreateRequest (org.apache.zookeeper.proto.CreateRequest)1 RequestHeader (org.apache.zookeeper.proto.RequestHeader)1 CloseRequestException (org.apache.zookeeper.server.ServerCnxn.CloseRequestException)1 ReadOnlyZooKeeperServer (org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer)1 Test (org.junit.Test)1