Search in sources :

Example 6 with StatusCode

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

the class WriteLacProcessorV3 method getWriteLacResponse.

// Returns null if there is no exception thrown
private WriteLacResponse getWriteLacResponse() {
    final long startTimeNanos = MathUtils.nowInNano();
    WriteLacRequest writeLacRequest = request.getWriteLacRequest();
    long lac = writeLacRequest.getLac();
    long ledgerId = writeLacRequest.getLedgerId();
    final WriteLacResponse.Builder writeLacResponse = WriteLacResponse.newBuilder().setLedgerId(ledgerId);
    if (!isVersionCompatible()) {
        writeLacResponse.setStatus(StatusCode.EBADVERSION);
        return writeLacResponse.build();
    }
    if (requestProcessor.bookie.isReadOnly()) {
        logger.warn("BookieServer is running as readonly mode, so rejecting the request from the client!");
        writeLacResponse.setStatus(StatusCode.EREADONLY);
        return writeLacResponse.build();
    }
    StatusCode status = null;
    ByteBuffer lacToAdd = writeLacRequest.getBody().asReadOnlyByteBuffer();
    byte[] masterKey = writeLacRequest.getMasterKey().toByteArray();
    try {
        requestProcessor.bookie.setExplicitLac(Unpooled.wrappedBuffer(lacToAdd), channel, masterKey);
        status = StatusCode.EOK;
    } catch (IOException e) {
        logger.error("Error saving lac {} for ledger:{}", lac, ledgerId, e);
        status = StatusCode.EIO;
    } catch (BookieException e) {
        logger.error("Unauthorized access to ledger:{} while adding lac:{}", ledgerId, lac, e);
        status = StatusCode.EUA;
    } catch (Throwable t) {
        logger.error("Unexpected exception while writing lac {} for ledger:{}", lac, ledgerId, t);
        // some bad request which cause unexpected exception
        status = StatusCode.EBADREQ;
    }
    // dosn't return a response back to the caller.
    if (status.equals(StatusCode.EOK)) {
        requestProcessor.writeLacStats.registerSuccessfulEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
    } else {
        requestProcessor.writeLacStats.registerFailedEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
    }
    writeLacResponse.setStatus(status);
    return writeLacResponse.build();
}
Also used : WriteLacRequest(org.apache.bookkeeper.proto.BookkeeperProtocol.WriteLacRequest) WriteLacResponse(org.apache.bookkeeper.proto.BookkeeperProtocol.WriteLacResponse) IOException(java.io.IOException) StatusCode(org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode) ByteBuffer(java.nio.ByteBuffer) BookieException(org.apache.bookkeeper.bookie.BookieException)

Aggregations

StatusCode (org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode)6 IOException (java.io.IOException)4 ByteBuf (io.netty.buffer.ByteBuf)2 BookieException (org.apache.bookkeeper.bookie.BookieException)2 ByteBuffer (java.nio.ByteBuffer)1 Bookie (org.apache.bookkeeper.bookie.Bookie)1 OperationRejectedException (org.apache.bookkeeper.bookie.BookieException.OperationRejectedException)1 WriteFlag (org.apache.bookkeeper.client.api.WriteFlag)1 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)1 AddRequest (org.apache.bookkeeper.proto.BookkeeperProtocol.AddRequest)1 AddResponse (org.apache.bookkeeper.proto.BookkeeperProtocol.AddResponse)1 GetBookieInfoRequest (org.apache.bookkeeper.proto.BookkeeperProtocol.GetBookieInfoRequest)1 GetBookieInfoResponse (org.apache.bookkeeper.proto.BookkeeperProtocol.GetBookieInfoResponse)1 OperationType (org.apache.bookkeeper.proto.BookkeeperProtocol.OperationType)1 ReadLacRequest (org.apache.bookkeeper.proto.BookkeeperProtocol.ReadLacRequest)1 ReadLacResponse (org.apache.bookkeeper.proto.BookkeeperProtocol.ReadLacResponse)1 Response (org.apache.bookkeeper.proto.BookkeeperProtocol.Response)1 WriteLacRequest (org.apache.bookkeeper.proto.BookkeeperProtocol.WriteLacRequest)1 WriteLacResponse (org.apache.bookkeeper.proto.BookkeeperProtocol.WriteLacResponse)1