Search in sources :

Example 1 with StatusCode

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

the class GetBookieInfoProcessorV3 method getGetBookieInfoResponse.

private GetBookieInfoResponse getGetBookieInfoResponse() {
    long startTimeNanos = MathUtils.nowInNano();
    GetBookieInfoRequest getBookieInfoRequest = request.getGetBookieInfoRequest();
    long requested = getBookieInfoRequest.getRequested();
    GetBookieInfoResponse.Builder getBookieInfoResponse = GetBookieInfoResponse.newBuilder();
    if (!isVersionCompatible()) {
        getBookieInfoResponse.setStatus(StatusCode.EBADVERSION);
        requestProcessor.getBookieInfoStats.registerFailedEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
        return getBookieInfoResponse.build();
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Received new getBookieInfo request: {}", request);
    }
    StatusCode status = StatusCode.EOK;
    long freeDiskSpace = 0L, totalDiskSpace = 0L;
    try {
        if ((requested & GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE) != 0) {
            freeDiskSpace = requestProcessor.bookie.getTotalFreeSpace();
            getBookieInfoResponse.setFreeDiskSpace(freeDiskSpace);
        }
        if ((requested & GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE) != 0) {
            totalDiskSpace = requestProcessor.bookie.getTotalDiskSpace();
            getBookieInfoResponse.setTotalDiskCapacity(totalDiskSpace);
        }
        LOG.debug("FreeDiskSpace info is " + freeDiskSpace + " totalDiskSpace is: " + totalDiskSpace);
    } catch (IOException e) {
        status = StatusCode.EIO;
        LOG.error("IOException while getting  freespace/totalspace", e);
    }
    getBookieInfoResponse.setStatus(status);
    requestProcessor.getBookieInfoStats.registerSuccessfulEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
    return getBookieInfoResponse.build();
}
Also used : GetBookieInfoResponse(org.apache.bookkeeper.proto.BookkeeperProtocol.GetBookieInfoResponse) GetBookieInfoRequest(org.apache.bookkeeper.proto.BookkeeperProtocol.GetBookieInfoRequest) IOException(java.io.IOException) StatusCode(org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode)

Example 2 with StatusCode

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

the class PerChannelBookieClient method readV2Response.

private void readV2Response(final BookieProtocol.Response response) {
    OperationType operationType = getOperationType(response.getOpCode());
    StatusCode status = getStatusCodeFromErrorCode(response.errorCode);
    CompletionKey key = acquireV2Key(response.ledgerId, response.entryId, operationType);
    CompletionValue completionValue = completionObjects.remove(key);
    key.release();
    if (completionValue == null) {
        // If there's no completion object here, try in the multimap
        synchronized (this) {
            if (completionObjectsV2Conflicts.containsKey(key)) {
                completionValue = completionObjectsV2Conflicts.get(key).get(0);
                completionObjectsV2Conflicts.remove(key, completionValue);
            }
        }
    }
    if (null == completionValue) {
        // Unexpected response, so log it. The txnId should have been present.
        if (LOG.isDebugEnabled()) {
            LOG.debug("Unexpected response received from bookie : " + addr + " for type : " + operationType + " and ledger:entry : " + response.ledgerId + ":" + response.entryId);
        }
        response.release();
    } else {
        long orderingKey = completionValue.ledgerId;
        executor.executeOrdered(orderingKey, ReadV2ResponseCallback.create(completionValue, response.ledgerId, response.entryId, status, response));
    }
}
Also used : OperationType(org.apache.bookkeeper.proto.BookkeeperProtocol.OperationType) StatusCode(org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode)

Example 3 with StatusCode

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

the class ReadEntryProcessorV3 method getFenceResponse.

private void getFenceResponse(ReadResponse.Builder readResponse, ByteBuf entryBody, boolean fenceResult) {
    StatusCode status;
    if (!fenceResult) {
        status = StatusCode.EIO;
        registerFailedEvent(requestProcessor.fenceReadWaitStats, lastPhaseStartTime);
    } else {
        status = StatusCode.EOK;
        readResponse.setBody(ByteString.copyFrom(entryBody.nioBuffer()));
        registerSuccessfulEvent(requestProcessor.fenceReadWaitStats, lastPhaseStartTime);
    }
    if (null != entryBody) {
        ReferenceCountUtil.release(entryBody);
    }
    readResponse.setStatus(status);
}
Also used : StatusCode(org.apache.bookkeeper.proto.BookkeeperProtocol.StatusCode)

Example 4 with StatusCode

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

Example 5 with StatusCode

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

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