Search in sources :

Example 1 with InvalidEnvelopedEntryException

use of org.apache.distributedlog.exceptions.InvalidEnvelopedEntryException in project bookkeeper by apache.

the class BKLogSegmentWriter method flushIfNeeded.

// Based on transmit buffer size, immediate flush, etc., should we flush the current
// packet now.
void flushIfNeeded() throws BKTransmitException, WriteException, InvalidEnvelopedEntryException, LockingException, FlushException {
    if (outstandingBytes > transmissionThreshold) {
        // If flush delay is disabled, flush immediately, else schedule appropriately.
        if (0 == minDelayBetweenImmediateFlushMs) {
            checkStateAndTransmit();
        } else {
            scheduleFlushWithDelayIfNeeded(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    checkStateAndTransmit();
                    return null;
                }
            }, transmitSchedFutureRefUpdater);
            // Timing here is not very important--the last flush failed and we should
            // indicate this to the caller. The next flush may succeed and unset the
            // scheduledFlushException in which case the next write will succeed (if the caller
            // hasn't already closed the writer).
            Exception exec = scheduledFlushExceptionUpdater.get(this);
            if (exec != null) {
                throw new FlushException("Last flush encountered an error while writing data to the backend", getLastTxId(), getLastTxIdAcknowledged(), exec);
            }
        }
    }
}
Also used : FlushException(org.apache.distributedlog.exceptions.FlushException) FlushException(org.apache.distributedlog.exceptions.FlushException) BKException(org.apache.bookkeeper.client.BKException) WriteException(org.apache.distributedlog.exceptions.WriteException) InvalidEnvelopedEntryException(org.apache.distributedlog.exceptions.InvalidEnvelopedEntryException) LockingException(org.apache.distributedlog.exceptions.LockingException) WriteCancelledException(org.apache.distributedlog.exceptions.WriteCancelledException) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException) IOException(java.io.IOException) TransactionIdOutOfOrderException(org.apache.distributedlog.exceptions.TransactionIdOutOfOrderException) LogRecordTooLongException(org.apache.distributedlog.exceptions.LogRecordTooLongException)

Example 2 with InvalidEnvelopedEntryException

use of org.apache.distributedlog.exceptions.InvalidEnvelopedEntryException in project bookkeeper by apache.

the class BKLogSegmentWriter method transmit.

/**
 * Transmit the current buffer to bookkeeper.
 * Synchronised at the class. #write() and #flush()
 * are never called at the same time.
 * NOTE: This method should only throw known exceptions so that we don't accidentally
 *       add new code that throws in an inappropriate place.
 *
 * @return a transmit future for caller to wait for transmit result if we transmit successfully,
 *         null if no data to transmit
 * @throws BKTransmitException if the segment writer is already in error state
 * @throws LockingException if the segment writer lost lock before transmit
 * @throws WriteException if failed to create the envelope for the data to transmit
 * @throws InvalidEnvelopedEntryException when built an invalid enveloped entry
 */
private CompletableFuture<Integer> transmit() throws BKTransmitException, LockingException, WriteException, InvalidEnvelopedEntryException {
    EntryBuffer recordSetToTransmit;
    transmitLock.lock();
    try {
        synchronized (this) {
            checkWriteLock();
            // stream has encountered an error and cannot be written to.
            if (!transmitResultUpdater.compareAndSet(this, BKException.Code.OK, BKException.Code.OK)) {
                LOG.error("Log Segment {} Trying to write to an errored stream; Error is {}", fullyQualifiedLogSegment, BKException.getMessage(transmitResultUpdater.get(this)));
                throw new BKTransmitException("Trying to write to an errored stream;" + " Error code : (" + transmitResultUpdater.get(this) + ") " + BKException.getMessage(transmitResultUpdater.get(this)), transmitResultUpdater.get(this));
            }
            if (recordSetWriter.getNumRecords() == 0) {
                // Control flushes always have at least the control record to flush
                transmitDataMisses.inc();
                return null;
            }
            recordSetToTransmit = recordSetWriter;
            recordSetWriter = newRecordSetWriter();
            outstandingBytes = 0;
            if (recordSetToTransmit.hasUserRecords()) {
                numBytes += recordSetToTransmit.getNumBytes();
                numFlushesSinceRestart++;
            }
        }
        ByteBuf toSend;
        try {
            toSend = recordSetToTransmit.getBuffer();
            FailpointUtils.checkFailPoint(FailpointUtils.FailPointName.FP_TransmitFailGetBuffer);
        } catch (IOException e) {
            if (e instanceof InvalidEnvelopedEntryException) {
                alertStatsLogger.raise("Invalid enveloped entry for segment {} : ", fullyQualifiedLogSegment, e);
            }
            LOG.error("Exception while enveloping entries for segment: {}", new Object[] { fullyQualifiedLogSegment }, e);
            // If a write fails here, we need to set the transmit result to an error so that
            // no future writes go through and violate ordering guarantees.
            transmitResultUpdater.set(this, BKException.Code.WriteException);
            if (e instanceof InvalidEnvelopedEntryException) {
                alertStatsLogger.raise("Invalid enveloped entry for segment {} : ", fullyQualifiedLogSegment, e);
                throw (InvalidEnvelopedEntryException) e;
            } else {
                throw new WriteException(streamName, "Envelope Error");
            }
        }
        synchronized (this) {
            // update the transmit timestamp
            lastTransmitNanos = MathUtils.nowInNano();
            BKTransmitPacket packet = new BKTransmitPacket(recordSetToTransmit);
            packetPrevious = packet;
            entryWriter.asyncAddEntry(toSend, this, packet);
            if (recordSetToTransmit.hasUserRecords()) {
                transmitDataSuccesses.inc();
            } else {
                transmitControlSuccesses.inc();
            }
            lastTransmit.reset().start();
            outstandingTransmitsUpdater.incrementAndGet(this);
            controlFlushNeeded = false;
            return packet.getTransmitFuture();
        }
    } finally {
        transmitLock.unlock();
    }
}
Also used : WriteException(org.apache.distributedlog.exceptions.WriteException) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) MutableObject(org.apache.commons.lang3.mutable.MutableObject) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) InvalidEnvelopedEntryException(org.apache.distributedlog.exceptions.InvalidEnvelopedEntryException)

Aggregations

IOException (java.io.IOException)2 BKTransmitException (org.apache.distributedlog.exceptions.BKTransmitException)2 InvalidEnvelopedEntryException (org.apache.distributedlog.exceptions.InvalidEnvelopedEntryException)2 WriteException (org.apache.distributedlog.exceptions.WriteException)2 ByteBuf (io.netty.buffer.ByteBuf)1 BKException (org.apache.bookkeeper.client.BKException)1 MutableObject (org.apache.commons.lang3.mutable.MutableObject)1 EndOfStreamException (org.apache.distributedlog.exceptions.EndOfStreamException)1 FlushException (org.apache.distributedlog.exceptions.FlushException)1 LockingException (org.apache.distributedlog.exceptions.LockingException)1 LogRecordTooLongException (org.apache.distributedlog.exceptions.LogRecordTooLongException)1 TransactionIdOutOfOrderException (org.apache.distributedlog.exceptions.TransactionIdOutOfOrderException)1 WriteCancelledException (org.apache.distributedlog.exceptions.WriteCancelledException)1