Search in sources :

Example 21 with ReplyHeader

use of org.apache.zookeeper.proto.ReplyHeader 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");
    // 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 {
                authReturn = ap.handleAuthentication(new ServerAuthenticationProvider.ServerObjs(this, cnxn), authPacket.getAuth());
            } catch (RuntimeException e) {
                LOG.warn("Caught runtime exception from AuthenticationProvider: " + scheme + " due to " + e);
                authReturn = KeeperException.Code.AUTHFAILED;
            }
        }
        if (authReturn == KeeperException.Code.OK) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Authentication succeeded for scheme: " + scheme);
            }
            LOG.info("auth success " + 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: " + scheme + " has " + 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) {
            Record rsp = processSasl(incomingBuffer, cnxn);
            ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.OK.intValue());
            // not sure about 3rd arg..what is it?
            cnxn.sendResponse(rh, rsp, "response");
            return;
        } else {
            Request si = new Request(cnxn, cnxn.getSessionId(), h.getXid(), h.getType(), incomingBuffer, cnxn.getAuthInfo());
            si.setOwner(ServerCnxn.me);
            // Always treat packet from the client as a possible
            // local request.
            setLocalSessionFlag(si);
            submitRequest(si);
        }
    }
    cnxn.incrOutstandingRequests(h);
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) InputStream(java.io.InputStream) ConnectRequest(org.apache.zookeeper.proto.ConnectRequest) GetSASLRequest(org.apache.zookeeper.proto.GetSASLRequest) ServerAuthenticationProvider(org.apache.zookeeper.server.auth.ServerAuthenticationProvider) Code(org.apache.zookeeper.KeeperException.Code) OpCode(org.apache.zookeeper.ZooDefs.OpCode) BinaryInputArchive(org.apache.jute.BinaryInputArchive) AuthPacket(org.apache.zookeeper.proto.AuthPacket) RequestHeader(org.apache.zookeeper.proto.RequestHeader) Record(org.apache.jute.Record)

Example 22 with ReplyHeader

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

the class ClientTest method testNonExistingOpCode.

/**
 * We create a perfectly valid 'exists' request, except that the opcode is wrong.
 * @return
 * @throws Exception
 */
@Test
public void testNonExistingOpCode() throws Exception {
    final CountDownLatch clientDisconnected = new CountDownLatch(1);
    Watcher watcher = new Watcher() {

        @Override
        public synchronized void process(WatchedEvent event) {
            if (event.getState() == KeeperState.Disconnected) {
                clientDisconnected.countDown();
            }
        }
    };
    TestableZooKeeper zk = new TestableZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher);
    final String path = "/m1";
    RequestHeader h = new RequestHeader();
    // This code does not exists
    h.setType(888);
    ExistsRequest request = new ExistsRequest();
    request.setPath(path);
    request.setWatch(false);
    ExistsResponse response = new ExistsResponse();
    ReplyHeader r = zk.submitRequest(h, request, response, null);
    Assert.assertEquals(r.getErr(), Code.UNIMPLEMENTED.intValue());
    // Sending a nonexisting opcode should cause the server to disconnect
    Assert.assertTrue("failed to disconnect", clientDisconnected.await(5000, TimeUnit.MILLISECONDS));
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ExistsRequest(org.apache.zookeeper.proto.ExistsRequest) ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) Watcher(org.apache.zookeeper.Watcher) RequestHeader(org.apache.zookeeper.proto.RequestHeader) CountDownLatch(java.util.concurrent.CountDownLatch) ExistsResponse(org.apache.zookeeper.proto.ExistsResponse) Test(org.junit.Test)

Example 23 with ReplyHeader

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

the class NettyServerCnxn method process.

@Override
public void process(WatchedEvent event) {
    ReplyHeader h = new ReplyHeader(-1, -1L, 0);
    if (LOG.isTraceEnabled()) {
        ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "Deliver event " + event + " to 0x" + Long.toHexString(this.sessionId) + " through " + this);
    }
    // Convert WatchedEvent to a type that can be sent over the wire
    WatcherEvent e = event.getWrapper();
    try {
        sendResponse(h, e, "notification");
    } catch (IOException e1) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Problem sending to " + getRemoteSocketAddress(), e1);
        }
        close();
    }
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) IOException(java.io.IOException) WatcherEvent(org.apache.zookeeper.proto.WatcherEvent)

Example 24 with ReplyHeader

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

the class UnimplementedRequestProcessor method processRequest.

public void processRequest(Request request) throws RequestProcessorException {
    KeeperException ke = new KeeperException.UnimplementedException();
    request.setException(ke);
    ReplyHeader rh = new ReplyHeader(request.cxid, request.zxid, ke.code().intValue());
    try {
        request.cnxn.sendResponse(rh, null, "response");
    } catch (IOException e) {
        throw new RequestProcessorException("Can't send the response", e);
    }
    request.cnxn.sendCloseSession();
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) IOException(java.io.IOException) KeeperException(org.apache.zookeeper.KeeperException)

Example 25 with ReplyHeader

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

the class ZooKeeper method removeWatches.

private void removeWatches(int opCode, String path, Watcher watcher, WatcherType watcherType, boolean local, VoidCallback cb, Object ctx) {
    PathUtils.validatePath(path);
    final String clientPath = path;
    final String serverPath = prependChroot(clientPath);
    WatchDeregistration wcb = new WatchDeregistration(clientPath, watcher, watcherType, local, watchManager);
    RequestHeader h = new RequestHeader();
    h.setType(opCode);
    Record request = getRemoveWatchesRequest(opCode, watcherType, serverPath);
    cnxn.queuePacket(h, new ReplyHeader(), request, null, cb, clientPath, serverPath, ctx, null, wcb);
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) RequestHeader(org.apache.zookeeper.proto.RequestHeader) Record(org.apache.jute.Record)

Aggregations

ReplyHeader (org.apache.zookeeper.proto.ReplyHeader)40 RequestHeader (org.apache.zookeeper.proto.RequestHeader)34 GetDataResponse (org.apache.zookeeper.proto.GetDataResponse)7 Record (org.apache.jute.Record)6 GetDataRequest (org.apache.zookeeper.proto.GetDataRequest)5 SetDataResponse (org.apache.zookeeper.proto.SetDataResponse)5 IOException (java.io.IOException)4 CreateResponse (org.apache.zookeeper.proto.CreateResponse)4 ExistsRequest (org.apache.zookeeper.proto.ExistsRequest)4 AuthPacket (org.apache.zookeeper.proto.AuthPacket)3 Create2Response (org.apache.zookeeper.proto.Create2Response)3 GetACLRequest (org.apache.zookeeper.proto.GetACLRequest)3 GetACLResponse (org.apache.zookeeper.proto.GetACLResponse)3 GetChildren2Request (org.apache.zookeeper.proto.GetChildren2Request)3 GetChildrenRequest (org.apache.zookeeper.proto.GetChildrenRequest)3 GetChildrenResponse (org.apache.zookeeper.proto.GetChildrenResponse)3 SetACLResponse (org.apache.zookeeper.proto.SetACLResponse)3 KeeperException (org.apache.zookeeper.KeeperException)2 Code (org.apache.zookeeper.KeeperException.Code)2 ErrorResult (org.apache.zookeeper.OpResult.ErrorResult)2