Example usage for io.netty.util ReferenceCountUtil release

List of usage examples for io.netty.util ReferenceCountUtil release

Introduction

In this page you can find the example usage for io.netty.util ReferenceCountUtil release.

Prototype

public static boolean release(Object msg) 

Source Link

Document

Try to call ReferenceCounted#release() if the specified message implements ReferenceCounted .

Usage

From source file:org.apache.activemq.transport.amqp.client.AmqpConnection.java

License:Apache License

@Override
public void onData(final ByteBuf incoming) {

    // We need to retain until the serializer gets around to processing it.
    ReferenceCountUtil.retain(incoming);

    serializer.execute(new Runnable() {

        @Override/*w ww. ja va2  s  .  c  o  m*/
        public void run() {
            ByteBuffer source = incoming.nioBuffer();
            LOG.trace("Client Received from Broker {} bytes:", source.remaining());

            if (protonTransport.isClosed()) {
                LOG.debug("Ignoring incoming data because transport is closed");
                return;
            }

            do {
                ByteBuffer buffer = protonTransport.getInputBuffer();
                int limit = Math.min(buffer.remaining(), source.remaining());
                ByteBuffer duplicate = source.duplicate();
                duplicate.limit(source.position() + limit);
                buffer.put(duplicate);
                protonTransport.processInput();
                source.position(source.position() + limit);
            } while (source.hasRemaining());

            ReferenceCountUtil.release(incoming);

            // Process the state changes from the latest data and then answer back
            // any pending updates to the Broker.
            processUpdates();
            pumpToProtonTransport();
        }
    });
}

From source file:org.apache.bookkeeper.client.impl.LedgerEntryImpl.java

License:Apache License

public void setEntryBuf(ByteBuf buf) {
    ReferenceCountUtil.release(entryBuf);
    this.entryBuf = buf;
}

From source file:org.apache.bookkeeper.client.impl.LedgerEntryImpl.java

License:Apache License

private void recycle() {
    this.ledgerId = -1L;
    this.entryId = -1L;
    this.length = -1L;
    ReferenceCountUtil.release(entryBuf);
    this.entryBuf = null;
    recycleHandle.recycle(this);
}

From source file:org.apache.bookkeeper.client.PendingAddOp.java

License:Apache License

private void maybeRecycle() {
    /**/*from w  ww  .  j a  v a 2s . c  om*/
     * We have opportunity to recycle two objects here.
     * PendingAddOp#toSend and LedgerHandle#pendingAddOp
     *
     * A. LedgerHandle#pendingAddOp: This can be released after all 3 conditions are met.
     *    - After the callback is run
     *    - After safeRun finished by the executor
     *    - Write responses are returned from all bookies
     *      as BookieClient Holds a reference from the point the addEntry requests are sent.
     *
     * B. ByteBuf can be released after 2 of the conditions are met.
     *    - After the callback is run as we will not retry the write after callback
     *    - After safeRun finished by the executor
     * BookieClient takes and releases on this buffer immediately after sending the data.
     *
     * The object can only be recycled after the above conditions are met
     * otherwise we could end up recycling twice and all
     * joy that goes along with that.
     */
    if (hasRun && callbackTriggered) {
        ReferenceCountUtil.release(toSend);
        toSend = null;
    }
    // only recycle a pending add op after it has been run.
    if (hasRun && toSend == null && pendingWriteRequests == 0) {
        recyclePendAddOpObject();
    }
}

From source file:org.apache.bookkeeper.client.PendingAddOp.java

License:Apache License

private void recyclePendAddOpObject() {
    entryId = LedgerHandle.INVALID_ENTRY_ID;
    currentLedgerLength = -1;/*from   w w  w . jav a  2  s  . co  m*/
    if (payload != null) {
        ReferenceCountUtil.release(payload);
        payload = null;
    }
    cb = null;
    ctx = null;
    ensemble = null;
    ackSet.recycle();
    ackSet = null;
    lh = null;
    clientCtx = null;
    isRecoveryAdd = false;
    completed = false;
    pendingWriteRequests = 0;
    callbackTriggered = false;
    hasRun = false;
    allowFailFast = false;
    writeFlags = null;

    recyclerHandle.recycle(this);
}

From source file:org.apache.bookkeeper.proto.ProtocolBenchmark.java

License:Apache License

@Benchmark
public void testAddEntryV2() throws Exception {
    ByteBufList list = ByteBufList.get(entry.retainedSlice());
    BookieProtocol.AddRequest req = BookieProtocol.AddRequest.create(BookieProtocol.CURRENT_PROTOCOL_VERSION,
            ledgerId, entryId, flags, masterKey, list);
    Object res = this.reqEnDeV2.encode(req, ByteBufAllocator.DEFAULT);
    ReferenceCountUtil.release(res);
    ReferenceCountUtil.release(list);/*from   ww  w  .  j  a  va2 s .co m*/
}

From source file:org.apache.bookkeeper.proto.ProtocolBenchmark.java

License:Apache License

@Benchmark
public void testAddEntryV3() throws Exception {
    // Build the request and calculate the total size to be included in the packet.
    BKPacketHeader.Builder headerBuilder = BKPacketHeader.newBuilder().setVersion(ProtocolVersion.VERSION_THREE)
            .setOperation(OperationType.ADD_ENTRY).setTxnId(0L);

    ByteBuf toSend = entry.slice();//from  www  .jav  a 2 s  .co  m
    byte[] toSendArray = new byte[toSend.readableBytes()];
    toSend.getBytes(toSend.readerIndex(), toSendArray);
    AddRequest.Builder addBuilder = AddRequest.newBuilder().setLedgerId(ledgerId).setEntryId(entryId)
            .setMasterKey(ByteString.copyFrom(masterKey)).setBody(ByteString.copyFrom(toSendArray))
            .setFlag(AddRequest.Flag.RECOVERY_ADD);

    Request request = Request.newBuilder().setHeader(headerBuilder).setAddRequest(addBuilder).build();

    Object res = this.reqEnDeV3.encode(request, ByteBufAllocator.DEFAULT);
    ReferenceCountUtil.release(res);
}

From source file:org.apache.bookkeeper.proto.ProtocolBenchmark.java

License:Apache License

@Benchmark
public void testAddEntryV3WithMdc() throws Exception {
    MDC.put("parent_id", "LetsPutSomeLongParentRequestIdHere");
    MDC.put("request_id", "LetsPutSomeLongRequestIdHere");
    // Build the request and calculate the total size to be included in the packet.
    BKPacketHeader.Builder headerBuilder = BKPacketHeader.newBuilder().setVersion(ProtocolVersion.VERSION_THREE)
            .setOperation(OperationType.ADD_ENTRY).setTxnId(0L);

    ByteBuf toSend = entry.slice();/*from  w  w  w .  j a v a2 s .  c  o m*/
    byte[] toSendArray = new byte[toSend.readableBytes()];
    toSend.getBytes(toSend.readerIndex(), toSendArray);
    AddRequest.Builder addBuilder = AddRequest.newBuilder().setLedgerId(ledgerId).setEntryId(entryId)
            .setMasterKey(ByteString.copyFrom(masterKey)).setBody(ByteString.copyFrom(toSendArray))
            .setFlag(AddRequest.Flag.RECOVERY_ADD);

    Request request = PerChannelBookieClient.appendRequestContext(Request.newBuilder()).setHeader(headerBuilder)
            .setAddRequest(addBuilder).build();

    Object res = this.reqEnDeV3.encode(request, ByteBufAllocator.DEFAULT);
    ReferenceCountUtil.release(res);
    MDC.clear();
}

From source file:org.apache.bookkeeper.proto.ProtocolBenchmark.java

License:Apache License

@Benchmark
public void testAddEntryV3WithExtraContextDataNoMdc() throws Exception {
    // Build the request and calculate the total size to be included in the packet.
    BKPacketHeader.Builder headerBuilder = BKPacketHeader.newBuilder().setVersion(ProtocolVersion.VERSION_THREE)
            .setOperation(OperationType.ADD_ENTRY).setTxnId(0L);

    ByteBuf toSend = entry.slice();//w ww . ja va2  s .  c o m
    byte[] toSendArray = new byte[toSend.readableBytes()];
    toSend.getBytes(toSend.readerIndex(), toSendArray);
    AddRequest.Builder addBuilder = AddRequest.newBuilder().setLedgerId(ledgerId).setEntryId(entryId)
            .setMasterKey(ByteString.copyFrom(masterKey)).setBody(ByteString.copyFrom(toSendArray))
            .setFlag(AddRequest.Flag.RECOVERY_ADD);

    Request request = appendRequestContextNoMdc(Request.newBuilder()).setHeader(headerBuilder)
            .setAddRequest(addBuilder).build();

    Object res = this.reqEnDeV3.encode(request, ByteBufAllocator.DEFAULT);
    ReferenceCountUtil.release(res);
}

From source file:org.apache.bookkeeper.proto.ReadEntryProcessor.java

License:Apache License

private void sendResponse(ByteBuf data, int errorCode, long startTimeNanos) {
    final RequestStats stats = requestProcessor.getRequestStats();
    final OpStatsLogger logger = stats.getReadEntryStats();
    BookieProtocol.Response response;
    if (errorCode == BookieProtocol.EOK) {
        logger.registerSuccessfulEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
        response = ResponseBuilder.buildReadResponse(data, request);
    } else {/*from   w w  w .java2 s. c  o  m*/
        if (data != null) {
            ReferenceCountUtil.release(data);
        }
        logger.registerFailedEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
        response = ResponseBuilder.buildErrorResponse(errorCode, request);
    }
    sendResponse(errorCode, response, stats.getReadRequestStats());
    recycle();
}