Search in sources :

Example 1 with OperationRejectedException

use of org.apache.bookkeeper.bookie.BookieException.OperationRejectedException in project bookkeeper by apache.

the class WriteEntryProcessor method processPacket.

@Override
protected void processPacket() {
    if (requestProcessor.bookie.isReadOnly()) {
        LOG.warn("BookieServer is running in readonly mode," + " so rejecting the request from the client!");
        sendResponse(BookieProtocol.EREADONLY, ResponseBuilder.buildErrorResponse(BookieProtocol.EREADONLY, request), requestProcessor.addRequestStats);
        request.release();
        request.recycle();
        return;
    }
    startTimeNanos = MathUtils.nowInNano();
    int rc = BookieProtocol.EOK;
    ByteBuf addData = request.getData();
    try {
        if (request.isRecoveryAdd()) {
            requestProcessor.bookie.recoveryAddEntry(addData, this, channel, request.getMasterKey());
        } else {
            requestProcessor.bookie.addEntry(addData, false, this, channel, request.getMasterKey());
        }
    } catch (OperationRejectedException e) {
        // unable to keep up with the write rate.
        if (LOG.isDebugEnabled()) {
            LOG.debug("Operation rejected while writing {}", request, e);
        }
        rc = BookieProtocol.EIO;
    } catch (IOException e) {
        LOG.error("Error writing {}", request, e);
        rc = BookieProtocol.EIO;
    } catch (BookieException.LedgerFencedException lfe) {
        LOG.error("Attempt to write to fenced ledger", lfe);
        rc = BookieProtocol.EFENCED;
    } catch (BookieException e) {
        LOG.error("Unauthorized access to ledger {}", request.getLedgerId(), e);
        rc = BookieProtocol.EUA;
    } catch (Throwable t) {
        LOG.error("Unexpected exception while writing {}@{} : {}", request.ledgerId, request.entryId, t.getMessage(), t);
        // some bad request which cause unexpected exception
        rc = BookieProtocol.EBADREQ;
    } finally {
        addData.release();
    }
    if (rc != BookieProtocol.EOK) {
        requestProcessor.addEntryStats.registerFailedEvent(MathUtils.elapsedNanos(startTimeNanos), TimeUnit.NANOSECONDS);
        sendResponse(rc, ResponseBuilder.buildErrorResponse(rc, request), requestProcessor.addRequestStats);
        request.recycle();
    }
}
Also used : OperationRejectedException(org.apache.bookkeeper.bookie.BookieException.OperationRejectedException) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) BookieException(org.apache.bookkeeper.bookie.BookieException)

Example 2 with OperationRejectedException

use of org.apache.bookkeeper.bookie.BookieException.OperationRejectedException 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)

Example 3 with OperationRejectedException

use of org.apache.bookkeeper.bookie.BookieException.OperationRejectedException in project bookkeeper by apache.

the class DbLedgerStorageWriteCacheTest method writeCacheFull.

@Test
public void writeCacheFull() throws Exception {
    storage.setMasterKey(4, "key".getBytes());
    assertEquals(false, storage.isFenced(4));
    assertEquals(true, storage.ledgerExists(4));
    assertEquals("key", new String(storage.readMasterKey(4)));
    // Add enough entries to fill the 1st write cache
    for (int i = 0; i < 5; i++) {
        ByteBuf entry = Unpooled.buffer(100 * 1024 + 2 * 8);
        // ledger id
        entry.writeLong(4);
        // entry id
        entry.writeLong(i);
        entry.writeZero(100 * 1024);
        storage.addEntry(entry);
    }
    for (int i = 0; i < 5; i++) {
        ByteBuf entry = Unpooled.buffer(100 * 1024 + 2 * 8);
        // ledger id
        entry.writeLong(4);
        // entry id
        entry.writeLong(5 + i);
        entry.writeZero(100 * 1024);
        storage.addEntry(entry);
    }
    // Next add should fail for cache full
    ByteBuf entry = Unpooled.buffer(100 * 1024 + 2 * 8);
    // ledger id
    entry.writeLong(4);
    // entry id
    entry.writeLong(22);
    entry.writeZero(100 * 1024);
    try {
        storage.addEntry(entry);
        fail("Should have thrown exception");
    } catch (OperationRejectedException e) {
    // Expected
    }
}
Also used : OperationRejectedException(org.apache.bookkeeper.bookie.BookieException.OperationRejectedException) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 4 with OperationRejectedException

use of org.apache.bookkeeper.bookie.BookieException.OperationRejectedException in project bookkeeper by apache.

the class DbLedgerStorage method triggerFlushAndAddEntry.

private void triggerFlushAndAddEntry(long ledgerId, long entryId, ByteBuf entry) throws IOException, BookieException {
    // cache, we don't need to trigger another flush
    if (!isFlushOngoing.get() && hasFlushBeenTriggered.compareAndSet(false, true)) {
        // Trigger an early flush in background
        log.info("Write cache is full, triggering flush");
        executor.execute(() -> {
            try {
                flush();
            } catch (IOException e) {
                log.error("Error during flush", e);
            }
        });
    }
    throttledWriteRequests.inc();
    long absoluteTimeoutNanos = System.nanoTime() + maxThrottleTimeNanos;
    while (System.nanoTime() < absoluteTimeoutNanos) {
        long stamp = writeCacheRotationLock.readLock();
        try {
            if (writeCache.put(ledgerId, entryId, entry)) {
                // We succeeded in putting the entry in write cache in the
                return;
            }
        } finally {
            writeCacheRotationLock.unlockRead(stamp);
        }
        // Wait some time and try again
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IOException("Interrupted when adding entry " + ledgerId + "@" + entryId);
        }
    }
    // Timeout expired and we weren't able to insert in write cache
    rejectedWriteRequests.inc();
    throw new OperationRejectedException();
}
Also used : OperationRejectedException(org.apache.bookkeeper.bookie.BookieException.OperationRejectedException) IOException(java.io.IOException)

Aggregations

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