Search in sources :

Example 1 with ReadOnlyZooKeeperServer

use of org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer in project zookeeper by apache.

the class ZooKeeperServer method processConnectRequest.

@SuppressFBWarnings(value = "IS2_INCONSISTENT_SYNC", justification = "the value won't change after startup")
public void processConnectRequest(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOException, ClientCnxnLimitException {
    BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(incomingBuffer));
    ConnectRequest connReq = new ConnectRequest();
    connReq.deserialize(bia, "connect");
    LOG.debug("Session establishment request from client {} client's lastZxid is 0x{}", cnxn.getRemoteSocketAddress(), Long.toHexString(connReq.getLastZxidSeen()));
    long sessionId = connReq.getSessionId();
    int tokensNeeded = 1;
    if (connThrottle.isConnectionWeightEnabled()) {
        if (sessionId == 0) {
            if (localSessionEnabled) {
                tokensNeeded = connThrottle.getRequiredTokensForLocal();
            } else {
                tokensNeeded = connThrottle.getRequiredTokensForGlobal();
            }
        } else {
            tokensNeeded = connThrottle.getRequiredTokensForRenew();
        }
    }
    if (!connThrottle.checkLimit(tokensNeeded)) {
        throw new ClientCnxnLimitException();
    }
    ServerMetrics.getMetrics().CONNECTION_TOKEN_DEFICIT.add(connThrottle.getDeficit());
    ServerMetrics.getMetrics().CONNECTION_REQUEST_COUNT.add(1);
    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 {}; will be dropped if server is in r-o mode", cnxn.getRemoteSocketAddress());
    }
    if (!readOnly && this instanceof ReadOnlyZooKeeperServer) {
        String msg = "Refusing session request for not-read-only client " + cnxn.getRemoteSocketAddress();
        LOG.info(msg);
        throw new CloseRequestException(msg, ServerCnxn.DisconnectReason.NOT_READ_ONLY_CLIENT);
    }
    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, ServerCnxn.DisconnectReason.CLIENT_ZXID_AHEAD);
    }
    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();
    if (sessionId == 0) {
        long id = createSession(cnxn, passwd, sessionTimeout);
        LOG.debug("Client attempting to establish new session: session = 0x{}, zxid = 0x{}, timeout = {}, address = {}", Long.toHexString(id), Long.toHexString(connReq.getLastZxidSeen()), connReq.getTimeOut(), cnxn.getRemoteSocketAddress());
    } else {
        validateSession(cnxn, sessionId);
        LOG.debug("Client attempting to renew session: session = 0x{}, zxid = 0x{}, timeout = {}, address = {}", Long.toHexString(sessionId), Long.toHexString(connReq.getLastZxidSeen()), connReq.getTimeOut(), cnxn.getRemoteSocketAddress());
        if (serverCnxnFactory != null) {
            serverCnxnFactory.closeSession(sessionId, ServerCnxn.DisconnectReason.CLIENT_RECONNECT);
        }
        if (secureServerCnxnFactory != null) {
            secureServerCnxnFactory.closeSession(sessionId, ServerCnxn.DisconnectReason.CLIENT_RECONNECT);
        }
        cnxn.setSessionId(sessionId);
        reopenSession(cnxn, sessionId, passwd, sessionTimeout);
        ServerMetrics.getMetrics().CONNECTION_REVALIDATE_COUNT.add(1);
    }
}
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) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with ReadOnlyZooKeeperServer

use of org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer in project zookeeper by apache.

the class ZooKeeperServer method finishSessionInit.

public void finishSessionInit(ServerCnxn cnxn, boolean valid) {
    // register with JMX
    try {
        if (valid) {
            if (serverCnxnFactory != null && serverCnxnFactory.cnxns.contains(cnxn)) {
                serverCnxnFactory.registerConnection(cnxn);
            } else if (secureServerCnxnFactory != null && secureServerCnxnFactory.cnxns.contains(cnxn)) {
                secureServerCnxnFactory.registerConnection(cnxn);
            }
        }
    } catch (Exception e) {
        LOG.warn("Failed to register with JMX", e);
    }
    try {
        ConnectResponse rsp = new ConnectResponse(0, valid ? cnxn.getSessionTimeout() : 0, // send 0 if session is no
        valid ? cnxn.getSessionId() : 0, // longer valid
        valid ? generatePasswd(cnxn.getSessionId()) : new byte[16]);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryOutputArchive bos = BinaryOutputArchive.getArchive(baos);
        bos.writeInt(-1, "len");
        rsp.serialize(bos, "connect");
        if (!cnxn.isOldClient) {
            bos.writeBool(this instanceof ReadOnlyZooKeeperServer, "readOnly");
        }
        baos.close();
        ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
        bb.putInt(bb.remaining() - 4).rewind();
        cnxn.sendBuffer(bb);
        if (valid) {
            LOG.debug("Established session 0x{} with negotiated timeout {} for client {}", Long.toHexString(cnxn.getSessionId()), cnxn.getSessionTimeout(), cnxn.getRemoteSocketAddress());
            cnxn.enableRecv();
        } else {
            LOG.info("Invalid session 0x{} for client {}, probably expired", Long.toHexString(cnxn.getSessionId()), cnxn.getRemoteSocketAddress());
            cnxn.sendBuffer(ServerCnxnFactory.closeConn);
        }
    } catch (Exception e) {
        LOG.warn("Exception while establishing session, closing", e);
        cnxn.close(ServerCnxn.DisconnectReason.IO_EXCEPTION_IN_SESSION_INIT);
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ConnectResponse(org.apache.zookeeper.proto.ConnectResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) SaslException(javax.security.sasl.SaslException) RequestProcessorException(org.apache.zookeeper.server.RequestProcessor.RequestProcessorException) SessionExpiredException(org.apache.zookeeper.KeeperException.SessionExpiredException) CloseRequestException(org.apache.zookeeper.server.ServerCnxn.CloseRequestException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) ReadOnlyZooKeeperServer(org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer)

Example 3 with ReadOnlyZooKeeperServer

use of org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer in project zookeeper by apache.

the class StatCommand method commandRun.

@Override
public void commandRun() {
    if (!isZKServerRunning()) {
        pw.println(ZK_NOT_SERVING);
    } else {
        pw.print("Zookeeper version: ");
        pw.println(Version.getFullVersion());
        if (zkServer instanceof ReadOnlyZooKeeperServer) {
            pw.println("READ-ONLY mode; serving only read-only clients");
        }
        if (len == FourLetterCommands.statCmd) {
            LOG.info("Stat command output");
            pw.println("Clients:");
            for (ServerCnxn c : factory.getConnections()) {
                c.dumpConnectionInfo(pw, true);
                pw.println();
            }
            pw.println();
        }
        ServerStats serverStats = zkServer.serverStats();
        pw.print(serverStats.toString());
        pw.print("Node count: ");
        pw.println(zkServer.getZKDatabase().getNodeCount());
        if (serverStats.getServerState().equals("leader")) {
            Leader leader = ((LeaderZooKeeperServer) zkServer).getLeader();
            BufferStats proposalStats = leader.getProposalStats();
            pw.printf("Proposal sizes last/min/max: %s%n", proposalStats.toString());
        }
    }
}
Also used : ServerCnxn(org.apache.zookeeper.server.ServerCnxn) Leader(org.apache.zookeeper.server.quorum.Leader) ServerStats(org.apache.zookeeper.server.ServerStats) BufferStats(org.apache.zookeeper.server.quorum.BufferStats) ReadOnlyZooKeeperServer(org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer) LeaderZooKeeperServer(org.apache.zookeeper.server.quorum.LeaderZooKeeperServer)

Aggregations

ReadOnlyZooKeeperServer (org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer)3 IOException (java.io.IOException)2 CloseRequestException (org.apache.zookeeper.server.ServerCnxn.CloseRequestException)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 SaslException (javax.security.sasl.SaslException)1 BinaryInputArchive (org.apache.jute.BinaryInputArchive)1 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)1 KeeperException (org.apache.zookeeper.KeeperException)1 SessionExpiredException (org.apache.zookeeper.KeeperException.SessionExpiredException)1 ConnectRequest (org.apache.zookeeper.proto.ConnectRequest)1 ConnectResponse (org.apache.zookeeper.proto.ConnectResponse)1 RequestProcessorException (org.apache.zookeeper.server.RequestProcessor.RequestProcessorException)1 ServerCnxn (org.apache.zookeeper.server.ServerCnxn)1 ServerStats (org.apache.zookeeper.server.ServerStats)1 BufferStats (org.apache.zookeeper.server.quorum.BufferStats)1 Leader (org.apache.zookeeper.server.quorum.Leader)1 LeaderZooKeeperServer (org.apache.zookeeper.server.quorum.LeaderZooKeeperServer)1