Search in sources :

Example 1 with PromiseCombiner

use of org.apache.hbase.thirdparty.io.netty.util.concurrent.PromiseCombiner in project hbase by apache.

the class SaslWrapHandler method flush.

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
    if (queue.isEmpty()) {
        return;
    }
    ByteBuf buf = null;
    try {
        ChannelPromise promise = ctx.newPromise();
        int readableBytes = queue.readableBytes();
        buf = queue.remove(readableBytes, promise);
        byte[] bytes = new byte[readableBytes];
        buf.readBytes(bytes);
        byte[] wrapperBytes = saslClient.wrap(bytes, 0, bytes.length);
        ChannelPromise lenPromise = ctx.newPromise();
        ctx.write(ctx.alloc().buffer(4).writeInt(wrapperBytes.length), lenPromise);
        ChannelPromise contentPromise = ctx.newPromise();
        ctx.write(Unpooled.wrappedBuffer(wrapperBytes), contentPromise);
        PromiseCombiner combiner = new PromiseCombiner();
        combiner.addAll(lenPromise, contentPromise);
        combiner.finish(promise);
        ctx.flush();
    } finally {
        if (buf != null) {
            ReferenceCountUtil.safeRelease(buf);
        }
    }
}
Also used : PromiseCombiner(org.apache.hbase.thirdparty.io.netty.util.concurrent.PromiseCombiner) ChannelPromise(org.apache.hbase.thirdparty.io.netty.channel.ChannelPromise) ByteBuf(org.apache.hbase.thirdparty.io.netty.buffer.ByteBuf)

Example 2 with PromiseCombiner

use of org.apache.hbase.thirdparty.io.netty.util.concurrent.PromiseCombiner in project hbase by apache.

the class CryptoAESWrapHandler method flush.

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
    if (queue.isEmpty()) {
        return;
    }
    ByteBuf buf = null;
    try {
        ChannelPromise promise = ctx.newPromise();
        int readableBytes = queue.readableBytes();
        buf = queue.remove(readableBytes, promise);
        byte[] bytes = new byte[readableBytes];
        buf.readBytes(bytes);
        byte[] wrapperBytes = cryptoAES.wrap(bytes, 0, bytes.length);
        ChannelPromise lenPromise = ctx.newPromise();
        ctx.write(ctx.alloc().buffer(4).writeInt(wrapperBytes.length), lenPromise);
        ChannelPromise contentPromise = ctx.newPromise();
        ctx.write(Unpooled.wrappedBuffer(wrapperBytes), contentPromise);
        PromiseCombiner combiner = new PromiseCombiner();
        combiner.addAll(lenPromise, contentPromise);
        combiner.finish(promise);
        ctx.flush();
    } finally {
        if (buf != null) {
            ReferenceCountUtil.safeRelease(buf);
        }
    }
}
Also used : PromiseCombiner(org.apache.hbase.thirdparty.io.netty.util.concurrent.PromiseCombiner) ChannelPromise(org.apache.hbase.thirdparty.io.netty.channel.ChannelPromise) ByteBuf(org.apache.hbase.thirdparty.io.netty.buffer.ByteBuf)

Example 3 with PromiseCombiner

use of org.apache.hbase.thirdparty.io.netty.util.concurrent.PromiseCombiner in project hbase by apache.

the class NettyRpcDuplexHandler method writeRequest.

private void writeRequest(ChannelHandlerContext ctx, Call call, ChannelPromise promise) throws IOException {
    id2Call.put(call.id, call);
    ByteBuf cellBlock = cellBlockBuilder.buildCellBlock(codec, compressor, call.cells, ctx.alloc());
    CellBlockMeta cellBlockMeta;
    if (cellBlock != null) {
        CellBlockMeta.Builder cellBlockMetaBuilder = CellBlockMeta.newBuilder();
        cellBlockMetaBuilder.setLength(cellBlock.writerIndex());
        cellBlockMeta = cellBlockMetaBuilder.build();
    } else {
        cellBlockMeta = null;
    }
    RequestHeader requestHeader = IPCUtil.buildRequestHeader(call, cellBlockMeta);
    int sizeWithoutCellBlock = IPCUtil.getTotalSizeWhenWrittenDelimited(requestHeader, call.param);
    int totalSize = cellBlock != null ? sizeWithoutCellBlock + cellBlock.writerIndex() : sizeWithoutCellBlock;
    ByteBuf buf = ctx.alloc().buffer(sizeWithoutCellBlock + 4);
    buf.writeInt(totalSize);
    try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf)) {
        requestHeader.writeDelimitedTo(bbos);
        if (call.param != null) {
            call.param.writeDelimitedTo(bbos);
        }
        if (cellBlock != null) {
            ChannelPromise withoutCellBlockPromise = ctx.newPromise();
            ctx.write(buf, withoutCellBlockPromise);
            ChannelPromise cellBlockPromise = ctx.newPromise();
            ctx.write(cellBlock, cellBlockPromise);
            PromiseCombiner combiner = new PromiseCombiner(ctx.executor());
            combiner.addAll((ChannelFuture) withoutCellBlockPromise, cellBlockPromise);
            combiner.finish(promise);
        } else {
            ctx.write(buf, promise);
        }
    }
}
Also used : PromiseCombiner(org.apache.hbase.thirdparty.io.netty.util.concurrent.PromiseCombiner) ByteBufOutputStream(org.apache.hbase.thirdparty.io.netty.buffer.ByteBufOutputStream) CellBlockMeta(org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.CellBlockMeta) RequestHeader(org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.RequestHeader) ChannelPromise(org.apache.hbase.thirdparty.io.netty.channel.ChannelPromise) ByteBuf(org.apache.hbase.thirdparty.io.netty.buffer.ByteBuf)

Aggregations

ByteBuf (org.apache.hbase.thirdparty.io.netty.buffer.ByteBuf)3 ChannelPromise (org.apache.hbase.thirdparty.io.netty.channel.ChannelPromise)3 PromiseCombiner (org.apache.hbase.thirdparty.io.netty.util.concurrent.PromiseCombiner)3 CellBlockMeta (org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.CellBlockMeta)1 RequestHeader (org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.RequestHeader)1 ByteBufOutputStream (org.apache.hbase.thirdparty.io.netty.buffer.ByteBufOutputStream)1