Search in sources :

Example 1 with ByteBufferBackedInputStream

use of voldemort.common.nio.ByteBufferBackedInputStream in project voldemort by voldemort.

the class AsyncRequestHandler method initializeStreams.

@Override
protected void initializeStreams(int socketBufferSize, CommBufferSizeStats commBufferStats) {
    ByteBufferContainer inputBufferContainer, outputBufferContainer;
    inputBufferContainer = new ByteBufferContainer(socketBufferSize, resizeThreshold, commBufferStats.getCommReadBufferSizeTracker());
    if (requestHandlerFactory.shareReadWriteBuffer()) {
        outputBufferContainer = inputBufferContainer;
    } else {
        outputBufferContainer = new ByteBufferContainer(socketBufferSize, resizeThreshold, commBufferStats.getCommWriteBufferSizeTracker());
    }
    this.inputStream = new ByteBufferBackedInputStream(inputBufferContainer);
    this.outputStream = new ByteBufferBackedOutputStream(outputBufferContainer);
}
Also used : ByteBufferBackedOutputStream(voldemort.common.nio.ByteBufferBackedOutputStream) ByteBufferContainer(voldemort.common.nio.ByteBufferContainer) ByteBufferBackedInputStream(voldemort.common.nio.ByteBufferBackedInputStream)

Example 2 with ByteBufferBackedInputStream

use of voldemort.common.nio.ByteBufferBackedInputStream in project voldemort by voldemort.

the class AdminServiceRequestHandler method isCompleteRequest.

/**
     * This method is used by non-blocking code to determine if the give buffer
     * represents a complete request. Because the non-blocking code can by
     * definition not just block waiting for more data, it's possible to get
     * partial reads, and this identifies that case.
     *
     * @param buffer Buffer to check; the buffer is reset to position 0 before
     *        calling this method and the caller must reset it after the call
     *        returns
     * @return True if the buffer holds a complete request, false otherwise
     */
@Override
public boolean isCompleteRequest(ByteBuffer buffer) {
    DataInputStream inputStream = new DataInputStream(new ByteBufferBackedInputStream(buffer));
    try {
        int dataSize = inputStream.readInt();
        if (logger.isTraceEnabled())
            logger.trace("In isCompleteRequest, dataSize: " + dataSize + ", buffer position: " + buffer.position());
        if (dataSize == -1)
            return true;
        // Here we skip over the data (without reading it in) and
        // move our position to just past it.
        buffer.position(buffer.position() + dataSize);
        return true;
    } catch (Exception e) {
        // throws an InvalidArgumentException.
        if (logger.isTraceEnabled())
            logger.trace("In isCompleteRequest, probable partial read occurred: " + e);
        return false;
    }
}
Also used : DataInputStream(java.io.DataInputStream) NoSuchCapabilityException(voldemort.store.NoSuchCapabilityException) ConfigurationException(voldemort.utils.ConfigurationException) ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) StoreOperationFailureException(voldemort.store.StoreOperationFailureException) VoldemortException(voldemort.VoldemortException) IOException(java.io.IOException) PersistenceFailureException(voldemort.store.PersistenceFailureException) StoreNotFoundException(voldemort.store.StoreNotFoundException) ByteBufferBackedInputStream(voldemort.common.nio.ByteBufferBackedInputStream)

Example 3 with ByteBufferBackedInputStream

use of voldemort.common.nio.ByteBufferBackedInputStream in project voldemort by voldemort.

the class VoldemortNativeRequestHandler method isCompleteRequest.

/**
     * This is pretty ugly. We end up mimicking the request logic here, so this
     * needs to stay in sync with handleRequest.
     */
@Override
public boolean isCompleteRequest(final ByteBuffer buffer) throws VoldemortException {
    DataInputStream inputStream = new DataInputStream(new ByteBufferBackedInputStream(buffer));
    try {
        byte opCode = inputStream.readByte();
        // Store Name
        inputStream.readUTF();
        // Store routing type
        getRoutingType(inputStream);
        switch(opCode) {
            case VoldemortOpCode.GET_VERSION_OP_CODE:
                if (!GetVersionRequestHandler.isCompleteRequest(inputStream, buffer))
                    return false;
                break;
            case VoldemortOpCode.GET_OP_CODE:
                if (!GetRequestHandler.isCompleteRequest(inputStream, buffer, protocolVersion))
                    return false;
                break;
            case VoldemortOpCode.GET_ALL_OP_CODE:
                if (!GetAllRequestHandler.isCompleteRequest(inputStream, buffer, protocolVersion))
                    return false;
                break;
            case VoldemortOpCode.PUT_OP_CODE:
                {
                    if (!PutRequestHandler.isCompleteRequest(inputStream, buffer, protocolVersion))
                        return false;
                    break;
                }
            case VoldemortOpCode.DELETE_OP_CODE:
                {
                    if (!DeleteRequestHandler.isCompleteRequest(inputStream, buffer))
                        return false;
                    break;
                }
            default:
                throw new VoldemortException(" Unrecognized Voldemort OpCode " + opCode);
        }
        // data, there is something wrong.
        if (buffer.hasRemaining()) {
            logger.info("Probably a client bug, Discarding additional bytes in isCompleteRequest. Opcode: " + opCode + ", remaining bytes: " + buffer.remaining());
        }
        return true;
    } catch (IOException e) {
        // throws an InvalidArgumentException.
        if (logger.isDebugEnabled())
            logger.debug("Probable partial read occurred causing exception", e);
        return false;
    }
}
Also used : IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) VoldemortException(voldemort.VoldemortException) ByteBufferBackedInputStream(voldemort.common.nio.ByteBufferBackedInputStream)

Example 4 with ByteBufferBackedInputStream

use of voldemort.common.nio.ByteBufferBackedInputStream in project voldemort by voldemort.

the class VoldemortNativeClientRequestFormat method isCompleteGetResponse.

public boolean isCompleteGetResponse(ByteBuffer buffer) {
    try {
        DataInputStream inputStream = new DataInputStream(new ByteBufferBackedInputStream(buffer));
        checkException(inputStream);
        if (!skipResults(inputStream, buffer)) {
            return false;
        }
    } catch (VoldemortException e) {
    } catch (IOException e) {
        return false;
    }
    return !buffer.hasRemaining();
}
Also used : IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) VoldemortException(voldemort.VoldemortException) ByteBufferBackedInputStream(voldemort.common.nio.ByteBufferBackedInputStream)

Example 5 with ByteBufferBackedInputStream

use of voldemort.common.nio.ByteBufferBackedInputStream in project voldemort by voldemort.

the class VoldemortNativeClientRequestFormat method isCompleteGetAllResponse.

@Override
public boolean isCompleteGetAllResponse(ByteBuffer buffer) {
    try {
        DataInputStream inputStream = new DataInputStream(new ByteBufferBackedInputStream(buffer));
        checkException(inputStream);
        int numResults = inputStream.readInt();
        for (int i = 0; i < numResults; i++) {
            int keySize = inputStream.readInt();
            if (!ByteUtils.skipByteArray(buffer, keySize)) {
                return false;
            }
            if (!skipResults(inputStream, buffer)) {
                return false;
            }
        }
    } catch (VoldemortException e) {
    } catch (IOException e) {
        return false;
    }
    return !buffer.hasRemaining();
}
Also used : IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) VoldemortException(voldemort.VoldemortException) ByteBufferBackedInputStream(voldemort.common.nio.ByteBufferBackedInputStream)

Aggregations

ByteBufferBackedInputStream (voldemort.common.nio.ByteBufferBackedInputStream)6 DataInputStream (java.io.DataInputStream)4 IOException (java.io.IOException)4 VoldemortException (voldemort.VoldemortException)4 ByteBufferBackedOutputStream (voldemort.common.nio.ByteBufferBackedOutputStream)2 ByteBufferContainer (voldemort.common.nio.ByteBufferContainer)2 NoSuchCapabilityException (voldemort.store.NoSuchCapabilityException)1 PersistenceFailureException (voldemort.store.PersistenceFailureException)1 StoreNotFoundException (voldemort.store.StoreNotFoundException)1 StoreOperationFailureException (voldemort.store.StoreOperationFailureException)1 ConfigurationException (voldemort.utils.ConfigurationException)1 ObsoleteVersionException (voldemort.versioning.ObsoleteVersionException)1