Search in sources :

Example 1 with RequestFormatType

use of voldemort.client.protocol.RequestFormatType in project voldemort by voldemort.

the class AsyncRequestHandler method initRequestHandler.

/**
 * Returns true if the request should continue.
 *
 * @return
 */
private boolean initRequestHandler(SelectionKey selectionKey) {
    ByteBuffer inputBuffer = inputStream.getBuffer();
    int remaining = inputBuffer.remaining();
    // Don't have enough bytes to determine the protocol yet...
    if (remaining < 3)
        return true;
    byte[] protoBytes = { inputBuffer.get(0), inputBuffer.get(1), inputBuffer.get(2) };
    try {
        String proto = ByteUtils.getString(protoBytes, "UTF-8");
        inputBuffer.clear();
        RequestFormatType requestFormatType = RequestFormatType.fromCode(proto);
        requestHandler = requestHandlerFactory.getRequestHandler(requestFormatType);
        if (logger.isInfoEnabled())
            logger.info("Protocol negotiated for " + socketChannel.socket() + ": " + requestFormatType.getDisplayName());
        // The protocol negotiation is the first request, so respond by
        // sticking the bytes in the output buffer, signaling the Selector,
        // and returning false to denote no further processing is needed.
        outputStream.getBuffer().put(ByteUtils.getBytes("ok", "UTF-8"));
        prepForWrite(selectionKey);
        return false;
    } catch (IllegalArgumentException e) {
        // okay we got some nonsense. For backwards compatibility,
        // assume this is an old client who does not know how to negotiate
        RequestFormatType requestFormatType = RequestFormatType.VOLDEMORT_V0;
        requestHandler = requestHandlerFactory.getRequestHandler(requestFormatType);
        if (logger.isInfoEnabled())
            logger.info("No protocol proposal given for " + socketChannel.socket() + ", assuming " + requestFormatType.getDisplayName());
        return true;
    }
}
Also used : RequestFormatType(voldemort.client.protocol.RequestFormatType) ByteBuffer(java.nio.ByteBuffer)

Example 2 with RequestFormatType

use of voldemort.client.protocol.RequestFormatType in project voldemort by voldemort.

the class SocketServerSession method run.

public void run() {
    try {
        activeSessions.put(sessionId, this);
        DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream(), 64000));
        DataOutputStream outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream(), 64000));
        RequestFormatType protocol = negotiateProtocol(inputStream, outputStream);
        RequestHandler handler = handlerFactory.getRequestHandler(protocol);
        logger.info("Client " + socket.getRemoteSocketAddress() + " connected successfully with protocol " + protocol.getCode());
        while (!isInterrupted() && !socket.isClosed() && !isClosed) {
            StreamRequestHandler srh = handler.handleRequest(inputStream, outputStream);
            if (srh != null) {
                if (logger.isTraceEnabled())
                    logger.trace("Request is streaming");
                StreamRequestHandlerState srhs = null;
                try {
                    do {
                        if (logger.isTraceEnabled())
                            logger.trace("About to enter streaming request handler");
                        srhs = srh.handleRequest(inputStream, outputStream);
                        if (logger.isTraceEnabled())
                            logger.trace("Finished invocation of streaming request handler, result is " + srhs);
                    } while (srhs != StreamRequestHandlerState.COMPLETE);
                } catch (VoldemortException e) {
                    srh.handleError(outputStream, e);
                    outputStream.flush();
                    break;
                } finally {
                    srh.close(outputStream);
                }
            }
            outputStream.flush();
        }
        if (isInterrupted())
            logger.info(Thread.currentThread().getName() + " has been interrupted, closing session.");
    } catch (EOFException e) {
        logger.info("Client " + socket.getRemoteSocketAddress() + " disconnected.");
    } catch (IOException e) {
        // if this is an unexpected
        if (!isClosed)
            logger.error(e);
    } finally {
        try {
            if (!socket.isClosed())
                socket.close();
        } catch (Exception e) {
            logger.error("Error while closing socket", e);
        }
        // now remove ourselves from the set of active sessions
        this.activeSessions.remove(sessionId);
    }
}
Also used : StreamRequestHandler(voldemort.server.protocol.StreamRequestHandler) StreamRequestHandler(voldemort.server.protocol.StreamRequestHandler) RequestHandler(voldemort.server.protocol.RequestHandler) BufferedInputStream(java.io.BufferedInputStream) DataOutputStream(java.io.DataOutputStream) EOFException(java.io.EOFException) RequestFormatType(voldemort.client.protocol.RequestFormatType) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) BufferedOutputStream(java.io.BufferedOutputStream) VoldemortException(voldemort.VoldemortException) VoldemortException(voldemort.VoldemortException) IOException(java.io.IOException) EOFException(java.io.EOFException) StreamRequestHandlerState(voldemort.server.protocol.StreamRequestHandler.StreamRequestHandlerState)

Example 3 with RequestFormatType

use of voldemort.client.protocol.RequestFormatType in project voldemort by voldemort.

the class SocketServerSession method negotiateProtocol.

private RequestFormatType negotiateProtocol(InputStream input, OutputStream output) throws IOException {
    input.mark(3);
    byte[] protoBytes = new byte[3];
    ByteUtils.read(input, protoBytes);
    RequestFormatType requestFormat;
    try {
        String proto = ByteUtils.getString(protoBytes, "UTF-8");
        requestFormat = RequestFormatType.fromCode(proto);
        output.write(ByteUtils.getBytes("ok", "UTF-8"));
        output.flush();
    } catch (IllegalArgumentException e) {
        // okay we got some nonsense. For backwards compatibility,
        // assume this is an old client who does not know how to negotiate
        requestFormat = RequestFormatType.VOLDEMORT_V0;
        // reset input stream so we don't interfere with request format
        input.reset();
        logger.info("No protocol proposal given, assuming " + RequestFormatType.VOLDEMORT_V0.getDisplayName());
    }
    return requestFormat;
}
Also used : RequestFormatType(voldemort.client.protocol.RequestFormatType)

Example 4 with RequestFormatType

use of voldemort.client.protocol.RequestFormatType in project voldemort by voldemort.

the class SocketPoolTest method testVariousProtocols.

@Test
public void testVariousProtocols() throws Exception {
    for (RequestFormatType type : RequestFormatType.values()) {
        SocketDestination dest = new SocketDestination("localhost", port, type);
        SocketAndStreams sas = pool.checkout(dest);
        assertEquals(type, sas.getRequestFormatType());
    }
}
Also used : SocketAndStreams(voldemort.client.protocol.admin.SocketAndStreams) SocketDestination(voldemort.store.socket.SocketDestination) RequestFormatType(voldemort.client.protocol.RequestFormatType) Test(org.junit.Test)

Example 5 with RequestFormatType

use of voldemort.client.protocol.RequestFormatType in project voldemort by voldemort.

the class VoldemortNativeSocketStoreTest method configs.

@Parameters
public static Collection<Object[]> configs() {
    RequestFormatType[] types = new RequestFormatType[] { RequestFormatType.VOLDEMORT_V1, RequestFormatType.VOLDEMORT_V2, RequestFormatType.VOLDEMORT_V3 };
    List<Object[]> options = new ArrayList<Object[]>();
    boolean[] nioOptions = { true, false };
    for (RequestFormatType type : types) {
        for (boolean nio : nioOptions) {
            options.add(new Object[] { type, nio });
        }
    }
    return options;
}
Also used : ArrayList(java.util.ArrayList) RequestFormatType(voldemort.client.protocol.RequestFormatType) Parameters(org.junit.runners.Parameterized.Parameters)

Aggregations

RequestFormatType (voldemort.client.protocol.RequestFormatType)5 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 Parameters (org.junit.runners.Parameterized.Parameters)1 VoldemortException (voldemort.VoldemortException)1 SocketAndStreams (voldemort.client.protocol.admin.SocketAndStreams)1 RequestHandler (voldemort.server.protocol.RequestHandler)1 StreamRequestHandler (voldemort.server.protocol.StreamRequestHandler)1 StreamRequestHandlerState (voldemort.server.protocol.StreamRequestHandler.StreamRequestHandlerState)1 SocketDestination (voldemort.store.socket.SocketDestination)1