Search in sources :

Example 1 with WriteLacResponse

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

the class WriteLacProcessorV3 method safeRun.

@Override
public void safeRun() {
    WriteLacResponse writeLacResponse = getWriteLacResponse();
    if (null != writeLacResponse) {
        Response.Builder response = Response.newBuilder().setHeader(getHeader()).setStatus(writeLacResponse.getStatus()).setWriteLacResponse(writeLacResponse);
        Response resp = response.build();
        sendResponse(writeLacResponse.getStatus(), resp, requestProcessor.writeLacRequestStats);
    }
}
Also used : Response(org.apache.bookkeeper.proto.BookkeeperProtocol.Response) WriteLacResponse(org.apache.bookkeeper.proto.BookkeeperProtocol.WriteLacResponse) WriteLacResponse(org.apache.bookkeeper.proto.BookkeeperProtocol.WriteLacResponse)

Example 2 with WriteLacResponse

use of org.apache.bookkeeper.proto.BookkeeperProtocol.WriteLacResponse 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

WriteLacResponse (org.apache.bookkeeper.proto.BookkeeperProtocol.WriteLacResponse)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 BookieException (org.apache.bookkeeper.bookie.BookieException)1 Response (org.apache.bookkeeper.proto.BookkeeperProtocol.Response)1 StatusCode (org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode)1 WriteLacRequest (org.apache.bookkeeper.proto.BookkeeperProtocol.WriteLacRequest)1