Search in sources :

Example 1 with BlockingReadHandler

use of org.jboss.netty.handler.queue.BlockingReadHandler in project graphdb by neo4j-attic.

the class Client method sendRequest.

protected <R> Response<R> sendRequest(RequestType<M> type, SlaveContext context, Serializer serializer, Deserializer<R> deserializer) {
    Triplet<Channel, ChannelBuffer, ByteBuffer> channelContext = null;
    try {
        // Send 'em over the wire
        channelContext = getChannel();
        Channel channel = channelContext.first();
        channelContext.second().clear();
        ChunkingChannelBuffer chunkingBuffer = new ChunkingChannelBuffer(channelContext.second(), channel, Protocol.MAX_FRAME_LENGTH);
        chunkingBuffer.writeByte(type.id());
        writeContext(type, context, chunkingBuffer);
        serializer.write(chunkingBuffer, channelContext.third());
        chunkingBuffer.done();
        // Read the response
        @SuppressWarnings("unchecked") BlockingReadHandler<ChannelBuffer> reader = (BlockingReadHandler<ChannelBuffer>) channel.getPipeline().get("blockingHandler");
        final Triplet<Channel, ChannelBuffer, ByteBuffer> finalChannelContext = channelContext;
        DechunkingChannelBuffer dechunkingBuffer = new DechunkingChannelBuffer(reader, DEFAULT_READ_RESPONSE_TIMEOUT_SECONDS) {

            @Override
            protected ChannelBuffer readNext() {
                ChannelBuffer result = super.readNext();
                if (result == null) {
                    channelPool.dispose(finalChannelContext);
                    throw new ComException("Channel has been closed");
                }
                return result;
            }
        };
        R response = deserializer.read(dechunkingBuffer, channelContext.third());
        StoreId storeId = readStoreId(dechunkingBuffer, channelContext.third());
        if (shouldCheckStoreId(type)) {
            assertCorrectStoreId(storeId);
        }
        TransactionStream txStreams = readTransactionStreams(dechunkingBuffer);
        return new Response<R>(response, storeId, txStreams);
    } catch (ClosedChannelException e) {
        channelPool.dispose(channelContext);
        throw new ComException(e);
    } catch (IOException e) {
        throw new ComException(e);
    } catch (InterruptedException e) {
        throw new ComException(e);
    } catch (Exception e) {
        throw new ComException(e);
    } finally {
        releaseChannel();
    }
}
Also used : BlockingReadHandler(org.jboss.netty.handler.queue.BlockingReadHandler) ClosedChannelException(java.nio.channels.ClosedChannelException) Channel(org.jboss.netty.channel.Channel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) StoreId(org.neo4j.kernel.impl.nioneo.store.StoreId)

Example 2 with BlockingReadHandler

use of org.jboss.netty.handler.queue.BlockingReadHandler in project neo4j by neo4j.

the class Client method getPipeline.

@Override
public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = Channels.pipeline();
    pipeline.addLast(MONITORING_CHANNEL_HANDLER_NAME, new MonitorChannelHandler(byteCounterMonitor));
    addLengthFieldPipes(pipeline, frameLength);
    BlockingReadHandler<ChannelBuffer> reader = new BlockingReadHandler<>(new ArrayBlockingQueue<>(100, false));
    pipeline.addLast(BLOCKING_CHANNEL_HANDLER_NAME, reader);
    return pipeline;
}
Also used : BlockingReadHandler(org.jboss.netty.handler.queue.BlockingReadHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 3 with BlockingReadHandler

use of org.jboss.netty.handler.queue.BlockingReadHandler in project graphdb by neo4j-attic.

the class Client method getPipeline.

public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = Channels.pipeline();
    addLengthFieldPipes(pipeline);
    BlockingReadHandler<ChannelBuffer> reader = new BlockingReadHandler<ChannelBuffer>(new ArrayBlockingQueue<ChannelEvent>(3, false));
    pipeline.addLast("blockingHandler", reader);
    return pipeline;
}
Also used : BlockingReadHandler(org.jboss.netty.handler.queue.BlockingReadHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ChannelEvent(org.jboss.netty.channel.ChannelEvent) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 4 with BlockingReadHandler

use of org.jboss.netty.handler.queue.BlockingReadHandler in project neo4j by neo4j.

the class Protocol method deserializeResponse.

public <PAYLOAD> Response<PAYLOAD> deserializeResponse(BlockingReadHandler<ChannelBuffer> reader, ByteBuffer input, long timeout, Deserializer<PAYLOAD> payloadDeserializer, ResourceReleaser channelReleaser, final LogEntryReader<ReadableClosablePositionAwareChannel> entryReader) throws IOException {
    final DechunkingChannelBuffer dechunkingBuffer = new DechunkingChannelBuffer(reader, timeout, internalProtocolVersion, applicationProtocolVersion);
    PAYLOAD response = payloadDeserializer.read(dechunkingBuffer, input);
    StoreId storeId = readStoreId(dechunkingBuffer, input);
    // Response type is what previously was a byte saying how many data sources there were in the
    // coming transaction stream response. For backwards compatibility we keep it as a byte and we introduce
    // the transaction obligation response type as -1
    byte responseType = dechunkingBuffer.readByte();
    if (responseType == TransactionObligationResponse.RESPONSE_TYPE) {
        // It is a transaction obligation response
        long obligationTxId = dechunkingBuffer.readLong();
        return new TransactionObligationResponse<>(response, storeId, obligationTxId, channelReleaser);
    }
    // It's a transaction stream in this response
    TransactionStream transactions = visitor -> {
        NetworkReadableClosableChannel channel = new NetworkReadableClosableChannel(dechunkingBuffer);
        try (PhysicalTransactionCursor<ReadableClosablePositionAwareChannel> cursor = new PhysicalTransactionCursor<>(channel, entryReader)) {
            while (cursor.next() && !visitor.visit(cursor.get())) {
            }
        }
    };
    return new TransactionStreamResponse<>(response, storeId, transactions, channelReleaser);
}
Also used : StoreId(org.neo4j.kernel.impl.store.StoreId) RecordFormat(org.neo4j.kernel.impl.store.format.RecordFormat) LengthFieldBasedFrameDecoder(org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder) NeoStoreDataSource(org.neo4j.kernel.NeoStoreDataSource) TransactionRepresentation(org.neo4j.kernel.impl.transaction.TransactionRepresentation) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) LogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.LogEntryReader) BlockingReadHandler(org.jboss.netty.handler.queue.BlockingReadHandler) StoreWriter(org.neo4j.com.storecopy.StoreWriter) IOException(java.io.IOException) ReadableClosablePositionAwareChannel(org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel) Channel(org.jboss.netty.channel.Channel) ByteBuffer(java.nio.ByteBuffer) StorageCommand(org.neo4j.storageengine.api.StorageCommand) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) List(java.util.List) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) LinkedList(java.util.LinkedList) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) PhysicalTransactionCursor(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionCursor) LengthFieldPrepender(org.jboss.netty.handler.codec.frame.LengthFieldPrepender) PhysicalTransactionCursor(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionCursor) StoreId(org.neo4j.kernel.impl.store.StoreId)

Aggregations

ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)4 BlockingReadHandler (org.jboss.netty.handler.queue.BlockingReadHandler)4 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)3 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Channel (org.jboss.netty.channel.Channel)2 ClosedChannelException (java.nio.channels.ClosedChannelException)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ChannelEvent (org.jboss.netty.channel.ChannelEvent)1 LengthFieldBasedFrameDecoder (org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder)1 LengthFieldPrepender (org.jboss.netty.handler.codec.frame.LengthFieldPrepender)1 StoreWriter (org.neo4j.com.storecopy.StoreWriter)1 NeoStoreDataSource (org.neo4j.kernel.NeoStoreDataSource)1 StoreId (org.neo4j.kernel.impl.nioneo.store.StoreId)1 StoreId (org.neo4j.kernel.impl.store.StoreId)1 RecordFormat (org.neo4j.kernel.impl.store.format.RecordFormat)1 TransactionRepresentation (org.neo4j.kernel.impl.transaction.TransactionRepresentation)1 PhysicalTransactionCursor (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionCursor)1 PhysicalTransactionRepresentation (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)1