Search in sources :

Example 1 with BinaryInputArchive

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

the class LoadFromLogTest method testPad.

/**
     * Simulates ZOOKEEPER-1069 and verifies that flush() before padLogFile
     * fixes it.
     */
@Test
public void testPad() throws Exception {
    File tmpDir = ClientBase.createTmpDir();
    FileTxnLog txnLog = new FileTxnLog(tmpDir);
    TxnHeader txnHeader = new TxnHeader(0xabcd, 0x123, 0x123, Time.currentElapsedTime(), OpCode.create);
    Record txn = new CreateTxn("/Test", new byte[0], null, false, 1);
    txnLog.append(txnHeader, txn);
    FileInputStream in = new FileInputStream(tmpDir.getPath() + "/log." + Long.toHexString(txnHeader.getZxid()));
    BinaryInputArchive ia = BinaryInputArchive.getArchive(in);
    FileHeader header = new FileHeader();
    header.deserialize(ia, "fileheader");
    LOG.info("Received magic : " + header.getMagic() + " Expected : " + FileTxnLog.TXNLOG_MAGIC);
    Assert.assertTrue("Missing magic number ", header.getMagic() == FileTxnLog.TXNLOG_MAGIC);
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) CreateTxn(org.apache.zookeeper.txn.CreateTxn) FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) Record(org.apache.jute.Record) File(java.io.File) FileHeader(org.apache.zookeeper.server.persistence.FileHeader) FileInputStream(java.io.FileInputStream) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.Test)

Example 2 with BinaryInputArchive

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

the class ByteBufferInputStream method byteBuffer2Record.

public static void byteBuffer2Record(ByteBuffer bb, Record record) throws IOException {
    BinaryInputArchive ia;
    ia = BinaryInputArchive.getArchive(new ByteBufferInputStream(bb));
    record.deserialize(ia, "request");
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive)

Example 3 with BinaryInputArchive

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

the class MultiResponseTest method codeDecode.

private MultiResponse codeDecode(MultiResponse request) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
    request.serialize(boa, "result");
    baos.close();
    ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
    bb.rewind();
    BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(bb));
    MultiResponse decodedRequest = new MultiResponse();
    decodedRequest.deserialize(bia, "result");
    return decodedRequest;
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ByteBufferInputStream(org.apache.zookeeper.server.ByteBufferInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer)

Example 4 with BinaryInputArchive

use of org.apache.jute.BinaryInputArchive 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 5 with BinaryInputArchive

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

the class ZooKeeperServer method processPacket.

public void processPacket(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOException {
    // We have the request, now process and setup for next
    InputStream bais = new ByteBufferInputStream(incomingBuffer);
    BinaryInputArchive bia = BinaryInputArchive.getArchive(bais);
    RequestHeader h = new RequestHeader();
    h.deserialize(bia, "header");
    // Need to increase the outstanding request count first, otherwise
    // there might be a race condition that it enabled recv after
    // processing request and then disabled when check throttling.
    // 
    // Be aware that we're actually checking the global outstanding
    // request before this request.
    // 
    // It's fine if the IOException thrown before we decrease the count
    // in cnxn, since it will close the cnxn anyway.
    cnxn.incrOutstandingAndCheckThrottle(h);
    // Through the magic of byte buffers, txn will not be
    // pointing
    // to the start of the txn
    incomingBuffer = incomingBuffer.slice();
    if (h.getType() == OpCode.auth) {
        LOG.info("got auth packet {}", cnxn.getRemoteSocketAddress());
        AuthPacket authPacket = new AuthPacket();
        ByteBufferInputStream.byteBuffer2Record(incomingBuffer, authPacket);
        String scheme = authPacket.getScheme();
        ServerAuthenticationProvider ap = ProviderRegistry.getServerProvider(scheme);
        Code authReturn = KeeperException.Code.AUTHFAILED;
        if (ap != null) {
            try {
                // handleAuthentication may close the connection, to allow the client to choose
                // a different server to connect to.
                authReturn = ap.handleAuthentication(new ServerAuthenticationProvider.ServerObjs(this, cnxn), authPacket.getAuth());
            } catch (RuntimeException e) {
                LOG.warn("Caught runtime exception from AuthenticationProvider: {}", scheme, e);
                authReturn = KeeperException.Code.AUTHFAILED;
            }
        }
        if (authReturn == KeeperException.Code.OK) {
            LOG.info("Session 0x{}: auth success for scheme {} and address {}", Long.toHexString(cnxn.getSessionId()), scheme, cnxn.getRemoteSocketAddress());
            ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.OK.intValue());
            cnxn.sendResponse(rh, null, null);
        } else {
            if (ap == null) {
                LOG.warn("No authentication provider for scheme: {} has {}", scheme, ProviderRegistry.listProviders());
            } else {
                LOG.warn("Authentication failed for scheme: {}", scheme);
            }
            // send a response...
            ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.AUTHFAILED.intValue());
            cnxn.sendResponse(rh, null, null);
            // ... and close connection
            cnxn.sendBuffer(ServerCnxnFactory.closeConn);
            cnxn.disableRecv();
        }
        return;
    } else if (h.getType() == OpCode.sasl) {
        processSasl(incomingBuffer, cnxn, h);
    } else {
        if (!authHelper.enforceAuthentication(cnxn, h.getXid())) {
            // Already sent response to user about failure and closed the session, lets return
            return;
        } else {
            Request si = new Request(cnxn, cnxn.getSessionId(), h.getXid(), h.getType(), incomingBuffer, cnxn.getAuthInfo());
            int length = incomingBuffer.limit();
            if (isLargeRequest(length)) {
                // checkRequestSize will throw IOException if request is rejected
                checkRequestSizeWhenMessageReceived(length);
                si.setLargeRequestSize(length);
            }
            si.setOwner(ServerCnxn.me);
            submitRequest(si);
        }
    }
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) InputStream(java.io.InputStream) AuthPacket(org.apache.zookeeper.proto.AuthPacket) ConnectRequest(org.apache.zookeeper.proto.ConnectRequest) DeleteRequest(org.apache.zookeeper.proto.DeleteRequest) CreateRequest(org.apache.zookeeper.proto.CreateRequest) SetACLRequest(org.apache.zookeeper.proto.SetACLRequest) GetSASLRequest(org.apache.zookeeper.proto.GetSASLRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) RequestHeader(org.apache.zookeeper.proto.RequestHeader) ServerAuthenticationProvider(org.apache.zookeeper.server.auth.ServerAuthenticationProvider) Code(org.apache.zookeeper.KeeperException.Code) OpCode(org.apache.zookeeper.ZooDefs.OpCode)

Aggregations

BinaryInputArchive (org.apache.jute.BinaryInputArchive)25 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)7 FileHeader (org.apache.zookeeper.server.persistence.FileHeader)7 Test (org.junit.jupiter.api.Test)7 IOException (java.io.IOException)6 FileInputStream (java.io.FileInputStream)5 Record (org.apache.jute.Record)5 ByteBufferInputStream (org.apache.zookeeper.server.ByteBufferInputStream)5 TxnHeader (org.apache.zookeeper.txn.TxnHeader)5 ByteBuffer (java.nio.ByteBuffer)4 EOFException (java.io.EOFException)3 File (java.io.File)3 Adler32 (java.util.zip.Adler32)3 Checksum (java.util.zip.Checksum)3 Timeout (org.junit.jupiter.api.Timeout)3 ACL (org.apache.zookeeper.data.ACL)2 ConnectRequest (org.apache.zookeeper.proto.ConnectRequest)2 ReplyHeader (org.apache.zookeeper.proto.ReplyHeader)2