Search in sources :

Example 1 with AddResponse

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

the class WriteEntryProcessorV3 method safeRun.

@Override
public void safeRun() {
    AddResponse addResponse = getAddResponse();
    if (null != addResponse) {
        // This means there was an error and we should send this back.
        Response.Builder response = Response.newBuilder().setHeader(getHeader()).setStatus(addResponse.getStatus()).setAddResponse(addResponse);
        Response resp = response.build();
        sendResponse(addResponse.getStatus(), resp, requestProcessor.addRequestStats);
    }
}
Also used : Response(org.apache.bookkeeper.proto.BookkeeperProtocol.Response) AddResponse(org.apache.bookkeeper.proto.BookkeeperProtocol.AddResponse) AddResponse(org.apache.bookkeeper.proto.BookkeeperProtocol.AddResponse)

Example 2 with AddResponse

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

the class WriteEntryProcessorV3 method getAddResponse.

// Returns null if there is no exception thrown
private AddResponse getAddResponse() {
    final long startTimeNanos = MathUtils.nowInNano();
    AddRequest addRequest = request.getAddRequest();
    long ledgerId = addRequest.getLedgerId();
    long entryId = addRequest.getEntryId();
    final AddResponse.Builder addResponse = AddResponse.newBuilder().setLedgerId(ledgerId).setEntryId(entryId);
    if (!isVersionCompatible()) {
        addResponse.setStatus(StatusCode.EBADVERSION);
        return addResponse.build();
    }
    if (requestProcessor.bookie.isReadOnly()) {
        logger.warn("BookieServer is running as readonly mode, so rejecting the request from the client!");
        addResponse.setStatus(StatusCode.EREADONLY);
        return addResponse.build();
    }
    BookkeeperInternalCallbacks.WriteCallback wcb = new BookkeeperInternalCallbacks.WriteCallback() {

        @Override
        public void writeComplete(int rc, long ledgerId, long entryId, BookieSocketAddress addr, Object ctx) {
            if (BookieProtocol.EOK == rc) {
                requestProcessor.addEntryStats.registerSuccessfulEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
            } else {
                requestProcessor.addEntryStats.registerFailedEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
            }
            StatusCode status;
            switch(rc) {
                case BookieProtocol.EOK:
                    status = StatusCode.EOK;
                    break;
                case BookieProtocol.EIO:
                    status = StatusCode.EIO;
                    break;
                default:
                    status = StatusCode.EUA;
                    break;
            }
            addResponse.setStatus(status);
            Response.Builder response = Response.newBuilder().setHeader(getHeader()).setStatus(addResponse.getStatus()).setAddResponse(addResponse);
            Response resp = response.build();
            sendResponse(status, resp, requestProcessor.addRequestStats);
        }
    };
    final EnumSet<WriteFlag> writeFlags;
    if (addRequest.hasWriteFlags()) {
        writeFlags = WriteFlag.getWriteFlags(addRequest.getWriteFlags());
    } else {
        writeFlags = EnumSet.noneOf(WriteFlag.class);
    }
    final boolean ackBeforeSync = writeFlags.contains(WriteFlag.DEFERRED_SYNC);
    StatusCode status = null;
    byte[] masterKey = addRequest.getMasterKey().toByteArray();
    ByteBuf entryToAdd = Unpooled.wrappedBuffer(addRequest.getBody().asReadOnlyByteBuffer());
    try {
        if (RequestUtils.hasFlag(addRequest, AddRequest.Flag.RECOVERY_ADD)) {
            requestProcessor.bookie.recoveryAddEntry(entryToAdd, wcb, channel, masterKey);
        } else {
            requestProcessor.bookie.addEntry(entryToAdd, ackBeforeSync, wcb, channel, masterKey);
        }
        status = StatusCode.EOK;
    } catch (OperationRejectedException e) {
        // unable to keep up with the write rate.
        if (logger.isDebugEnabled()) {
            logger.debug("Operation rejected while writing {}", request, e);
        }
        status = StatusCode.EIO;
    } catch (IOException e) {
        logger.error("Error writing entry:{} to ledger:{}", entryId, ledgerId, e);
        status = StatusCode.EIO;
    } catch (BookieException.LedgerFencedException e) {
        logger.error("Ledger fenced while writing entry:{} to ledger:{}", entryId, ledgerId, e);
        status = StatusCode.EFENCED;
    } catch (BookieException e) {
        logger.error("Unauthorized access to ledger:{} while writing entry:{}", ledgerId, entryId, e);
        status = StatusCode.EUA;
    } catch (Throwable t) {
        logger.error("Unexpected exception while writing {}@{} : ", entryId, ledgerId, t);
        // some bad request which cause unexpected exception
        status = StatusCode.EBADREQ;
    }
    // doesn't return a response back to the caller.
    if (!status.equals(StatusCode.EOK)) {
        addResponse.setStatus(status);
        return addResponse.build();
    }
    return null;
}
Also used : IOException(java.io.IOException) AddResponse(org.apache.bookkeeper.proto.BookkeeperProtocol.AddResponse) ByteBuf(io.netty.buffer.ByteBuf) StatusCode(org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode) AddRequest(org.apache.bookkeeper.proto.BookkeeperProtocol.AddRequest) Response(org.apache.bookkeeper.proto.BookkeeperProtocol.Response) AddResponse(org.apache.bookkeeper.proto.BookkeeperProtocol.AddResponse) OperationRejectedException(org.apache.bookkeeper.bookie.BookieException.OperationRejectedException) WriteFlag(org.apache.bookkeeper.client.api.WriteFlag) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) BookieException(org.apache.bookkeeper.bookie.BookieException)

Aggregations

AddResponse (org.apache.bookkeeper.proto.BookkeeperProtocol.AddResponse)2 Response (org.apache.bookkeeper.proto.BookkeeperProtocol.Response)2 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 BookieException (org.apache.bookkeeper.bookie.BookieException)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 StatusCode (org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode)1