Search in sources :

Example 1 with CloseRequestException

use of org.apache.zookeeper.server.ServerCnxn.CloseRequestException 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)

Aggregations

IOException (java.io.IOException)1 BinaryInputArchive (org.apache.jute.BinaryInputArchive)1 ConnectRequest (org.apache.zookeeper.proto.ConnectRequest)1 CloseRequestException (org.apache.zookeeper.server.ServerCnxn.CloseRequestException)1 ReadOnlyZooKeeperServer (org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer)1