Example usage for io.netty.buffer ByteBuf retain

List of usage examples for io.netty.buffer ByteBuf retain

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf retain.

Prototype

@Override
    public abstract ByteBuf retain();

Source Link

Usage

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

License:Apache License

@Override
public void readEntryComplete(int rc, long ledgerId, long entryId, ByteBuf buffer, Object ctx) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("{} received response for (lid={}, eid={}) : {}", getClass().getName(), ledgerId, entryId,
                rc);//from   ww  w  . ja va 2s  .  c  o m
    }
    ReadLastConfirmedAndEntryContext rCtx = (ReadLastConfirmedAndEntryContext) ctx;
    BookieSocketAddress bookie = rCtx.getBookieAddress();
    numResponsesPending--;
    if (BKException.Code.OK == rc) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Received lastAddConfirmed (lac={}) from bookie({}) for (lid={}).",
                    rCtx.getLastAddConfirmed(), bookie, ledgerId);
        }

        if (rCtx.getLastAddConfirmed() > lastAddConfirmed) {
            lastAddConfirmed = rCtx.getLastAddConfirmed();
            lh.updateLastConfirmed(rCtx.getLastAddConfirmed(), 0L);
        }

        hasValidResponse = true;

        if (entryId != BookieProtocol.LAST_ADD_CONFIRMED) {
            buffer.retain();
            if (!requestComplete.get() && request.complete(rCtx.getBookieIndex(), bookie, buffer, entryId)) {
                // callback immediately
                if (rCtx.getLacUpdateTimestamp().isPresent()) {
                    long elapsedMicros = TimeUnit.MILLISECONDS
                            .toMicros(System.currentTimeMillis() - rCtx.getLacUpdateTimestamp().get());
                    elapsedMicros = Math.max(elapsedMicros, 0);
                    clientCtx.getClientStats().getReadLacAndEntryRespLogger()
                            .registerSuccessfulEvent(elapsedMicros, TimeUnit.MICROSECONDS);
                }

                // if the request has already completed, the buffer is not going to be used anymore, release it.
                if (!completeRequest()) {
                    buffer.release();
                }
                heardFromHostsBitSet.set(rCtx.getBookieIndex(), true);
            } else {
                buffer.release();
            }
        } else {
            emptyResponsesFromHostsBitSet.set(rCtx.getBookieIndex(), true);
            if (lastAddConfirmed > prevEntryId) {
                // received advanced lac
                completeRequest();
            } else if (emptyResponsesFromHostsBitSet.cardinality() >= numEmptyResponsesAllowed) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(
                            "Completed readLACAndEntry(lid = {}, previousEntryId = {}) "
                                    + "after received {} empty responses ('{}').",
                            ledgerId, prevEntryId, emptyResponsesFromHostsBitSet.cardinality(),
                            emptyResponsesFromHostsBitSet);
                }
                completeRequest();
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(
                            "Received empty response for readLACAndEntry(lid = {}, previousEntryId = {}) from"
                                    + " bookie {} @ {}, reattempting reading next bookie : lac = {}",
                            ledgerId, prevEntryId, rCtx.getBookieAddress(), rCtx.getBookieAddress(),
                            lastAddConfirmed);
                }
                request.logErrorAndReattemptRead(rCtx.getBookieIndex(), bookie, "Empty Response", rc);
            }
            return;
        }
    } else if (BKException.Code.UnauthorizedAccessException == rc && !requestComplete.get()) {
        submitCallback(rc);
        requestComplete.set(true);
    } else {
        request.logErrorAndReattemptRead(rCtx.getBookieIndex(), bookie, "Error: " + BKException.getMessage(rc),
                rc);
        return;
    }

    if (numResponsesPending <= 0) {
        completeRequest();
    }
}

From source file:org.apache.bookkeeper.clients.impl.kv.PByteBufSimpleTableImpl.java

License:Apache License

@Override
public CompletableFuture<RangeResult<ByteBuf, ByteBuf>> get(ByteBuf pKey, ByteBuf lKey,
        RangeOption<ByteBuf> option) {
    pKey.retain();
    lKey.retain();//  w  ww .j av  a2s.  c  o m
    if (null != option.endKey()) {
        option.endKey().retain();
    }
    return retryUtils
            .execute(() -> fromListenableFuture(
                    ClientCalls.futureUnaryCall(getChannel(pKey).newCall(getRangeMethod(), getCallOptions()),
                            KvUtils.newRangeRequest(lKey, option).setHeader(newRoutingHeader(pKey)).build())))
            .thenApply(response -> KvUtils.newRangeResult(response, resultFactory, kvFactory))
            .whenComplete((value, cause) -> {
                pKey.release();
                lKey.release();
                if (null != option.endKey()) {
                    option.endKey().release();
                }
            });
}

From source file:org.apache.bookkeeper.clients.impl.kv.PByteBufSimpleTableImpl.java

License:Apache License

@Override
public CompletableFuture<PutResult<ByteBuf, ByteBuf>> put(ByteBuf pKey, ByteBuf lKey, ByteBuf value,
        PutOption<ByteBuf> option) {
    pKey.retain();
    lKey.retain();//from ww  w .java 2  s.  co m
    value.retain();
    return retryUtils
            .execute(() -> fromListenableFuture(
                    ClientCalls.futureUnaryCall(getChannel(pKey).newCall(getPutMethod(), getCallOptions()),
                            KvUtils.newPutRequest(lKey, value, option).setHeader(newRoutingHeader(pKey))
                                    .build())))
            .thenApply(response -> KvUtils.newPutResult(response, resultFactory, kvFactory))
            .whenComplete((ignored, cause) -> {
                pKey.release();
                lKey.release();
                value.release();
            });
}

From source file:org.apache.bookkeeper.clients.impl.kv.PByteBufSimpleTableImpl.java

License:Apache License

@Override
public CompletableFuture<DeleteResult<ByteBuf, ByteBuf>> delete(ByteBuf pKey, ByteBuf lKey,
        DeleteOption<ByteBuf> option) {
    pKey.retain();
    lKey.retain();/*from  w  w  w.j a  v a  2s .co  m*/
    if (null != option.endKey()) {
        option.endKey().retain();
    }
    return retryUtils
            .execute(() -> fromListenableFuture(
                    ClientCalls.futureUnaryCall(getChannel(pKey).newCall(getDeleteMethod(), getCallOptions()),
                            KvUtils.newDeleteRequest(lKey, option).setHeader(newRoutingHeader(pKey)).build())))
            .thenApply(response -> KvUtils.newDeleteResult(response, resultFactory, kvFactory))
            .whenComplete((ignored, cause) -> {
                pKey.release();
                lKey.release();
                if (null != option.endKey()) {
                    option.endKey().release();
                }
            });
}

From source file:org.apache.bookkeeper.clients.impl.kv.PByteBufSimpleTableImpl.java

License:Apache License

@Override
public CompletableFuture<IncrementResult<ByteBuf, ByteBuf>> increment(ByteBuf pKey, ByteBuf lKey, long amount,
        IncrementOption<ByteBuf> option) {
    pKey.retain();
    lKey.retain();/*ww w . j a  v a2  s .  c  om*/
    return retryUtils
            .execute(() -> fromListenableFuture(ClientCalls.futureUnaryCall(
                    getChannel(pKey).newCall(getIncrementMethod(), getCallOptions()),
                    KvUtils.newIncrementRequest(lKey, amount, option).setHeader(newRoutingHeader(pKey))
                            .build())))
            .thenApply(response -> KvUtils.newIncrementResult(response, resultFactory, kvFactory))
            .whenComplete((ignored, cause) -> {
                pKey.release();
                lKey.release();
            });
}

From source file:org.apache.bookkeeper.clients.impl.kv.PByteBufTableRangeImpl.java

License:Apache License

@Override
public CompletableFuture<RangeResult<ByteBuf, ByteBuf>> get(ByteBuf pKey, ByteBuf lKey,
        RangeOption<ByteBuf> option) {
    pKey.retain();
    lKey.retain();/* w ww . j  a v  a 2s. c om*/
    if (null != option.endKey()) {
        option.endKey().retain();
    }
    return RangeRequestProcessor
            .of(KvUtils.newRangeRequest(lKey, option).setHeader(newRoutingHeader(pKey)).build(),
                    response -> KvUtils.newRangeResult(response, resultFactory, kvFactory), scChannel, executor,
                    backoffPolicy)
            .process().whenComplete((value, cause) -> {
                pKey.release();
                lKey.release();
                if (null != option.endKey()) {
                    option.endKey().release();
                }
            });
}

From source file:org.apache.bookkeeper.clients.impl.kv.PByteBufTableRangeImpl.java

License:Apache License

@Override
public CompletableFuture<PutResult<ByteBuf, ByteBuf>> put(ByteBuf pKey, ByteBuf lKey, ByteBuf value,
        PutOption<ByteBuf> option) {
    pKey.retain();
    lKey.retain();/* w ww  .j  a v a 2 s. com*/
    value.retain();
    return PutRequestProcessor
            .of(KvUtils.newPutRequest(lKey, value, option).setHeader(newRoutingHeader(pKey)).build(),
                    response -> KvUtils.newPutResult(response, resultFactory, kvFactory), scChannel, executor,
                    backoffPolicy)
            .process().whenComplete((ignored, cause) -> {
                pKey.release();
                lKey.release();
                value.release();
            });
}

From source file:org.apache.bookkeeper.clients.impl.kv.PByteBufTableRangeImpl.java

License:Apache License

@Override
public CompletableFuture<DeleteResult<ByteBuf, ByteBuf>> delete(ByteBuf pKey, ByteBuf lKey,
        DeleteOption<ByteBuf> option) {
    pKey.retain();
    lKey.retain();//w  w  w  .  ja  v a  2s . c  om
    if (null != option.endKey()) {
        option.endKey().retain();
    }
    return DeleteRequestProcessor
            .of(KvUtils.newDeleteRequest(lKey, option).setHeader(newRoutingHeader(pKey)).build(),
                    response -> KvUtils.newDeleteResult(response, resultFactory, kvFactory), scChannel,
                    executor, backoffPolicy)
            .process().whenComplete((ignored, cause) -> {
                pKey.release();
                lKey.release();
                if (null != option.endKey()) {
                    option.endKey().release();
                }
            });
}

From source file:org.apache.bookkeeper.clients.impl.kv.PByteBufTableRangeImpl.java

License:Apache License

@Override
public CompletableFuture<IncrementResult<ByteBuf, ByteBuf>> increment(ByteBuf pKey, ByteBuf lKey, long amount,
        IncrementOption<ByteBuf> option) {
    pKey.retain();
    lKey.retain();// w  ww .  j  a va  2 s  .com
    return IncrementRequestProcessor
            .of(KvUtils.newIncrementRequest(lKey, amount, option).setHeader(newRoutingHeader(pKey)).build(),
                    response -> KvUtils.newIncrementResult(response, resultFactory, kvFactory), scChannel,
                    executor, backoffPolicy)
            .process().whenComplete((ignored, cause) -> {
                pKey.release();
                lKey.release();
            });
}

From source file:org.apache.bookkeeper.common.router.ByteBufHashRouter.java

License:Apache License

@Override
ByteBuf getRoutingKeyData(ByteBuf key) {
    return key.retain();
}