Java Code Examples for org.apache.bookkeeper.client.BKException#getMessage()

The following examples show how to use org.apache.bookkeeper.client.BKException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BookKeeperEditLogOutputStream.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@Override
public void flushAndSync(boolean durable) throws IOException {
  assert(syncLatch != null);
  try {
    syncLatch.await();
  } catch (InterruptedException ie) {
    throw new IOException("Interrupted waiting on latch", ie);
  }
  if (transmitResult.get() != BKException.Code.OK) {
    throw new IOException("Failed to write to bookkeeper; Error is ("
                          + transmitResult.get() + ") "
                          + BKException.getMessage(transmitResult.get()));
  }

  syncLatch = null;
  // wait for whatever we wait on
}
 
Example 2
Source File: BookKeeperEditLogOutputStream.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Transmit the current buffer to bookkeeper.
 * Synchronised at the FSEditLog level. #write() and #setReadyToFlush()
 * are never called at the same time.
 */
private void transmit() throws IOException {
  if (!transmitResult.compareAndSet(BKException.Code.OK,
                                   BKException.Code.OK)) {
    throw new IOException("Trying to write to an errored stream;"
        + " Error code : (" + transmitResult.get()
        + ") " + BKException.getMessage(transmitResult.get()));
  }
  if (bufCurrent.getLength() > 0) {
    byte[] entry = Arrays.copyOf(bufCurrent.getData(),
                                 bufCurrent.getLength());
    lh.asyncAddEntry(entry, this, null);
    bufCurrent.reset();
    outstandingRequests.incrementAndGet();
  }
}
 
Example 3
Source File: BookKeeperEditLogOutputStream.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Override
public void flushAndSync(boolean durable) throws IOException {
  assert(syncLatch != null);
  try {
    syncLatch.await();
  } catch (InterruptedException ie) {
    throw new IOException("Interrupted waiting on latch", ie);
  }
  if (transmitResult.get() != BKException.Code.OK) {
    throw new IOException("Failed to write to bookkeeper; Error is ("
                          + transmitResult.get() + ") "
                          + BKException.getMessage(transmitResult.get()));
  }

  syncLatch = null;
  // wait for whatever we wait on
}
 
Example 4
Source File: BookKeeperEditLogOutputStream.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Transmit the current buffer to bookkeeper.
 * Synchronised at the FSEditLog level. #write() and #setReadyToFlush()
 * are never called at the same time.
 */
private void transmit() throws IOException {
  if (!transmitResult.compareAndSet(BKException.Code.OK,
                                   BKException.Code.OK)) {
    throw new IOException("Trying to write to an errored stream;"
        + " Error code : (" + transmitResult.get()
        + ") " + BKException.getMessage(transmitResult.get()));
  }
  if (bufCurrent.getLength() > 0) {
    byte[] entry = Arrays.copyOf(bufCurrent.getData(),
                                 bufCurrent.getLength());
    lh.asyncAddEntry(entry, this, null);
    bufCurrent.reset();
    outstandingRequests.incrementAndGet();
  }
}
 
Example 5
Source File: DistributedLogInputFormat.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
public List<InputSplit> getSplits(JobContext jobContext)
        throws IOException, InterruptedException {
    List<LogSegmentMetadata> segments = dlm.getLogSegments();
    List<InputSplit> inputSplits = Lists.newArrayListWithCapacity(segments.size());
    BookKeeper bk = namespace.getReaderBKC().get();
    LedgerManager lm = BookKeeperAccessor.getLedgerManager(bk);
    final AtomicInteger rcHolder = new AtomicInteger(0);
    final AtomicReference<LedgerMetadata> metadataHolder = new AtomicReference<LedgerMetadata>(null);
    for (LogSegmentMetadata segment : segments) {
        final CountDownLatch latch = new CountDownLatch(1);
        lm.readLedgerMetadata(segment.getLedgerId(),
                new BookkeeperInternalCallbacks.GenericCallback<LedgerMetadata>() {
            @Override
            public void operationComplete(int rc, LedgerMetadata ledgerMetadata) {
                metadataHolder.set(ledgerMetadata);
                rcHolder.set(rc);
                latch.countDown();
            }
        });
        latch.await();
        if (BKException.Code.OK != rcHolder.get()) {
            throw new IOException("Faild to get log segment metadata for " + segment + " : "
                    + BKException.getMessage(rcHolder.get()));
        }
        inputSplits.add(new LogSegmentSplit(segment, metadataHolder.get()));
    }
    return inputSplits;
}
 
Example 6
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public static ManagedLedgerException createManagedLedgerException(int bkErrorCode) {
    if (bkErrorCode == BKException.Code.TooManyRequestsException) {
        return new TooManyRequestsException("Too many request error from bookies");
    } else if (isBkErrorNotRecoverable(bkErrorCode)) {
        return new NonRecoverableLedgerException(BKException.getMessage(bkErrorCode));
    } else {
        return new ManagedLedgerException(BKException.getMessage(bkErrorCode));
    }
}
 
Example 7
Source File: BKLogSegmentWriter.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private synchronized CompletableFuture<DLSN> writeUserRecord(LogRecord record) throws IOException {
    if (null != closeFuture) {
        throw new WriteException(fullyQualifiedLogSegment,
                BKException.getMessage(BKException.Code.LedgerClosedException));
    }

    if (BKException.Code.OK != transmitResult.get()) {
        // Failfast if the stream already encountered error with safe retry on the client
        throw new WriteException(fullyQualifiedLogSegment, BKException.getMessage(transmitResult.get()));
    }

    if (streamEnded) {
        throw new EndOfStreamException("Writing to a stream after it has been marked as completed");
    }

    if ((record.getTransactionId() < 0)
            || (record.getTransactionId() == DistributedLogConstants.MAX_TXID)) {
        throw new TransactionIdOutOfOrderException(record.getTransactionId());
    }

    // Inject write delay if configured to do so
    writeDelayInjector.inject();

    // Will check write rate limits and throw if exceeded.
    writeLimiter.acquire();
    pendingWrites.inc();

    // The count represents the number of user records up to the
    // current record
    // Increment the record count only when writing a user log record
    // Internally generated log records don't increment the count
    // writeInternal will always set a count regardless of whether it was
    // incremented or not.
    CompletableFuture<DLSN> future = null;
    try {
        // increment the position for the record to write
        // if the record is failed to write, it would be decremented.
        positionWithinLogSegment++;
        int numRecords = 1;
        if (record.isRecordSet()) {
            numRecords = LogRecordSet.numRecords(record);
        }
        future = writeInternal(record);
        // after the record (record set) is written, the position should be
        // moved for {numRecords}, but since we already moved the record by 1
        // so advance the position for other {numRecords - 1}.
        positionWithinLogSegment += (numRecords - 1);
    } catch (IOException ex) {
        writeLimiter.release();
        pendingWrites.dec();
        positionWithinLogSegment--;
        throw ex;
    }

    // Track outstanding requests and return the future.
    return FutureUtils.ensure(future, () -> {
        pendingWrites.dec();
        writeLimiter.release();
    });
}
 
Example 8
Source File: BKLogSegmentWriter.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
            // If transmitResult is anything other than BKException.Code.OK, it means that the
            // stream has encountered an error and cannot be written to.
            if (!transmitResult.compareAndSet(BKException.Code.OK,
                                              BKException.Code.OK)) {
                LOG.error("Log Segment {} Trying to write to an errored stream; Error is {}",
                          fullyQualifiedLogSegment,
                          BKException.getMessage(transmitResult.get()));
                throw new BKTransmitException("Trying to write to an errored stream;"
                                                      + " Error code : (" + transmitResult.get() + ") "
                        + BKException.getMessage(transmitResult.get()), transmitResult.get());
            }

            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.
            transmitResult.set(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();
            outstandingTransmits.incrementAndGet();
            controlFlushNeeded = false;
            return packet.getTransmitFuture();
        }
    } finally {
        transmitLock.unlock();
    }
}
 
Example 9
Source File: BKLogSegmentWriter.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
synchronized private Future<DLSN> writeUserRecord(LogRecord record) throws IOException {
    if (null != closeFuture) {
        throw new WriteException(fullyQualifiedLogSegment, BKException.getMessage(BKException.Code.LedgerClosedException));
    }

    if (BKException.Code.OK != transmitResult.get()) {
        // Failfast if the stream already encountered error with safe retry on the client
        throw new WriteException(fullyQualifiedLogSegment, BKException.getMessage(transmitResult.get()));
    }

    if (streamEnded) {
        throw new EndOfStreamException("Writing to a stream after it has been marked as completed");
    }

    if ((record.getTransactionId() < 0) ||
        (record.getTransactionId() == DistributedLogConstants.MAX_TXID)) {
        throw new TransactionIdOutOfOrderException(record.getTransactionId());
    }

    // Inject write delay if configured to do so
    writeDelayInjector.inject();

    // Will check write rate limits and throw if exceeded.
    writeLimiter.acquire();
    pendingWrites.inc();

    // The count represents the number of user records up to the
    // current record
    // Increment the record count only when writing a user log record
    // Internally generated log records don't increment the count
    // writeInternal will always set a count regardless of whether it was
    // incremented or not.
    Future<DLSN> future = null;
    try {
        // increment the position for the record to write
        // if the record is failed to write, it would be decremented.
        positionWithinLogSegment++;
        int numRecords = 1;
        if (record.isRecordSet()) {
            numRecords = LogRecordSet.numRecords(record);
        }
        future = writeInternal(record);
        // after the record (record set) is written, the position should be
        // moved for {numRecords}, but since we already moved the record by 1
        // so advance the position for other {numRecords - 1}.
        positionWithinLogSegment += (numRecords - 1);
    } catch (IOException ex) {
        writeLimiter.release();
        pendingWrites.dec();
        positionWithinLogSegment--;
        throw ex;
    }

    // Track outstanding requests and return the future.
    return future.ensure(new Function0<BoxedUnit>() {
        public BoxedUnit apply() {
            pendingWrites.dec();
            writeLimiter.release();
            return null;
        }
    });
}
 
Example 10
Source File: BKLogSegmentWriter.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
/**
 * Transmit the current buffer to bookkeeper.
 * Synchronised at the class. #write() and #setReadyToFlush()
 * 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 Future<Integer> transmit()
    throws BKTransmitException, LockingException, WriteException, InvalidEnvelopedEntryException {
    EntryBuffer recordSetToTransmit;
    transmitLock.lock();
    try {
        synchronized (this) {
            checkWriteLock();
            // If transmitResult is anything other than BKException.Code.OK, it means that the
            // stream has encountered an error and cannot be written to.
            if (!transmitResult.compareAndSet(BKException.Code.OK,
                                              BKException.Code.OK)) {
                LOG.error("Log Segment {} Trying to write to an errored stream; Error is {}",
                          fullyQualifiedLogSegment,
                          BKException.getMessage(transmitResult.get()));
                throw new BKTransmitException("Trying to write to an errored stream;"
                                                      + " Error code : (" + transmitResult.get()
                                                      + ") " + BKException.getMessage(transmitResult.get()), transmitResult.get());
            }

            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++;
            }
        }

        Buffer 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.
            transmitResult.set(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) {
            BKTransmitPacket packet = new BKTransmitPacket(recordSetToTransmit);
            packetPrevious = packet;
            entryWriter.asyncAddEntry(toSend.getData(), 0, toSend.size(),
                                      this, packet);

            if (recordSetToTransmit.hasUserRecords()) {
                transmitDataSuccesses.inc();
            } else {
                transmitControlSuccesses.inc();
            }

            lastTransmit.reset().start();
            outstandingTransmits.incrementAndGet();
            controlFlushNeeded = false;
            return packet.getTransmitFuture();
        }
    } finally {
        transmitLock.unlock();
    }
}
 
Example 11
Source File: Utils.java    From distributedlog with Apache License 2.0 2 votes vote down vote up
/**
 * Create transmit exception from transmit result.
 *
 * @param transmitResult
 *          transmit result (basically bk exception code)
 * @return transmit exception
 */
public static BKTransmitException transmitException(int transmitResult) {
    return new BKTransmitException("Failed to write to bookkeeper; Error is ("
        + transmitResult + ") "
        + BKException.getMessage(transmitResult), transmitResult);
}
 
Example 12
Source File: FutureUtils.java    From distributedlog with Apache License 2.0 2 votes vote down vote up
/**
 * Create transmit exception from transmit result.
 *
 * @param transmitResult
 *          transmit result (basically bk exception code)
 * @return transmit exception
 */
public static BKTransmitException transmitException(int transmitResult) {
    return new BKTransmitException("Failed to write to bookkeeper; Error is ("
        + transmitResult + ") "
        + BKException.getMessage(transmitResult), transmitResult);
}
 
Example 13
Source File: FutureUtils.java    From distributedlog with Apache License 2.0 2 votes vote down vote up
/**
 * Create transmit exception from transmit result.
 *
 * @param transmitResult
 *          transmit result (basically bk exception code)
 * @return transmit exception
 */
public static BKTransmitException transmitException(int transmitResult) {
    return new BKTransmitException("Failed to write to bookkeeper; Error is ("
        + transmitResult + ") "
        + BKException.getMessage(transmitResult), transmitResult);
}