Search in sources :

Example 1 with ReadLacRequest

use of org.apache.bookkeeper.proto.BookkeeperProtocol.ReadLacRequest in project bookkeeper by apache.

the class ReadLacProcessorV3 method getReadLacResponse.

// Returns null if there is no exception thrown
private ReadLacResponse getReadLacResponse() {
    final long startTimeNanos = MathUtils.nowInNano();
    ReadLacRequest readLacRequest = request.getReadLacRequest();
    long ledgerId = readLacRequest.getLedgerId();
    final ReadLacResponse.Builder readLacResponse = ReadLacResponse.newBuilder().setLedgerId(ledgerId);
    if (!isVersionCompatible()) {
        readLacResponse.setStatus(StatusCode.EBADVERSION);
        return readLacResponse.build();
    }
    logger.debug("Received ReadLac request: {}", request);
    StatusCode status = StatusCode.EOK;
    ByteBuf lastEntry = null;
    ByteBuf lac = null;
    try {
        lac = requestProcessor.bookie.getExplicitLac(ledgerId);
        if (lac != null) {
            readLacResponse.setLacBody(ByteString.copyFrom(lac.nioBuffer()));
        }
    } catch (Bookie.NoLedgerException e) {
        status = StatusCode.ENOLEDGER;
        logger.error("No ledger found while performing readLac from ledger: {}", ledgerId, e);
    } catch (IOException e) {
        status = StatusCode.EIO;
        logger.error("IOException while performing readLac from ledger: {}", ledgerId);
    } finally {
        ReferenceCountUtil.release(lac);
    }
    try {
        lastEntry = requestProcessor.bookie.readEntry(ledgerId, BookieProtocol.LAST_ADD_CONFIRMED);
        if (lastEntry != null) {
            readLacResponse.setLastEntryBody(ByteString.copyFrom(lastEntry.nioBuffer()));
        }
    } catch (Bookie.NoLedgerException e) {
        status = StatusCode.ENOLEDGER;
        logger.error("No ledger found while trying to read last entry: {}", ledgerId, e);
    } catch (IOException e) {
        status = StatusCode.EIO;
        logger.error("IOException while trying to read last entry: {}", ledgerId, e);
    } finally {
        ReferenceCountUtil.release(lastEntry);
    }
    if ((lac == null) && (lastEntry == null)) {
        status = StatusCode.ENOENTRY;
    }
    if (status == StatusCode.EOK) {
        requestProcessor.readLacStats.registerSuccessfulEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
    } else {
        requestProcessor.readLacStats.registerFailedEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
    }
    // Finally set the status and return
    readLacResponse.setStatus(status);
    return readLacResponse.build();
}
Also used : Bookie(org.apache.bookkeeper.bookie.Bookie) ReadLacResponse(org.apache.bookkeeper.proto.BookkeeperProtocol.ReadLacResponse) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) StatusCode(org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode) ReadLacRequest(org.apache.bookkeeper.proto.BookkeeperProtocol.ReadLacRequest)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 Bookie (org.apache.bookkeeper.bookie.Bookie)1 ReadLacRequest (org.apache.bookkeeper.proto.BookkeeperProtocol.ReadLacRequest)1 ReadLacResponse (org.apache.bookkeeper.proto.BookkeeperProtocol.ReadLacResponse)1 StatusCode (org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode)1