use of alluxio.exception.JournalClosedException in project alluxio by Alluxio.
the class RaftJournalWriter method flush.
@Override
public void flush() throws IOException, JournalClosedException {
if (mClosed) {
throw new JournalClosedException("Cannot flush. Journal writer has been closed");
}
if (mJournalEntryBuilder != null) {
long flushSN = mNextSequenceNumberToWrite.get() - 1;
try {
// It is ok to submit the same entries multiple times because we de-duplicate by sequence
// number when applying them. This could happen if submit fails and we re-submit the same
// entry on retry.
JournalEntry entry = mJournalEntryBuilder.build();
Message message = RaftJournalSystem.toRaftMessage(entry);
mLastSubmittedSequenceNumber.set(flushSN);
LOG.trace("Flushing entry {} ({})", entry, message);
RaftClientReply reply = mClient.sendAsync(message, TimeDuration.valueOf(mWriteTimeoutMs, TimeUnit.MILLISECONDS)).get(mWriteTimeoutMs, TimeUnit.MILLISECONDS);
mLastCommittedSequenceNumber.set(flushSN);
if (reply.getException() != null) {
throw reply.getException();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException(e);
} catch (ExecutionException e) {
throw new IOException(e.getCause());
} catch (TimeoutException e) {
throw new IOException(String.format("Timed out after waiting %s milliseconds for journal entries to be processed", mWriteTimeoutMs), e);
}
mJournalEntryBuilder = null;
}
}
use of alluxio.exception.JournalClosedException in project alluxio by Alluxio.
the class AsyncJournalWriter method doFlush.
/**
* A dedicated thread that goes over outstanding queue items and writes/flushes them. Other
* threads can track progress by submitting tickets via ::flush() call.
*/
private void doFlush() {
// Runs the loop until ::stop() is called.
while (!mStopFlushing) {
/**
* Stand still unless;
* - queue has items
* - permit is given by:
* - clients
* -::stop()
*/
while (mQueue.isEmpty() && !mStopFlushing) {
try {
// queued entries proactively.
if (mFlushSemaphore.tryAcquire(mFlushBatchTimeNs, TimeUnit.NANOSECONDS)) {
break;
}
} catch (InterruptedException ie) {
break;
}
}
try {
long startTime = System.nanoTime();
// Write pending entries to journal.
while (!mQueue.isEmpty()) {
// Get, but do not remove, the head entry.
JournalEntry entry = mQueue.peek();
if (entry == null) {
// No more entries in the queue. Break write session.
break;
}
mJournalWriter.write(entry);
JournalUtils.sinkAppend(mJournalSinks, entry);
// Remove the head entry, after the entry was successfully written.
mQueue.poll();
mWriteCounter++;
if (((System.nanoTime() - startTime) >= mFlushBatchTimeNs) && !mStopFlushing) {
// infinite while-loop.
break;
}
}
// Either written new entries or previous flush had been failed.
if (mFlushCounter.get() < mWriteCounter) {
try (Timer.Context ctx = MetricsSystem.timer(MetricKey.MASTER_JOURNAL_FLUSH_TIMER.getName()).time()) {
mJournalWriter.flush();
}
JournalUtils.sinkFlush(mJournalSinks);
mFlushCounter.set(mWriteCounter);
}
// Notify tickets that have been served to wake up.
Iterator<FlushTicket> ticketIterator = mTicketSet.iterator();
while (ticketIterator.hasNext()) {
FlushTicket ticket = ticketIterator.next();
if (ticket.getTargetCounter() <= mFlushCounter.get()) {
ticket.setCompleted();
ticketIterator.remove();
}
}
} catch (IOException | JournalClosedException exc) {
// Add the error logging here since the actual flush error may be overwritten
// by the future meaningless ratis.protocol.AlreadyClosedException
SAMPLING_LOG.warn("Failed to flush journal entry: " + exc.getMessage(), exc);
Metrics.JOURNAL_FLUSH_FAILURE.inc();
// Release only tickets that have been flushed. Fail the rest.
Iterator<FlushTicket> ticketIterator = mTicketSet.iterator();
while (ticketIterator.hasNext()) {
FlushTicket ticket = ticketIterator.next();
ticketIterator.remove();
if (ticket.getTargetCounter() <= mFlushCounter.get()) {
ticket.setCompleted();
} else {
ticket.setError(exc);
}
}
}
}
}
use of alluxio.exception.JournalClosedException in project alluxio by Alluxio.
the class MasterJournalContext method waitForJournalFlush.
/**
* Waits for the flush counter to be flushed to the journal. If the counter is
* {@link #INVALID_FLUSH_COUNTER}, this is a noop.
*/
private void waitForJournalFlush() throws UnavailableException {
if (mFlushCounter == INVALID_FLUSH_COUNTER) {
// Check this before the precondition.
return;
}
RetryPolicy retry = new TimeoutRetry(FLUSH_RETRY_TIMEOUT_MS, FLUSH_RETRY_INTERVAL_MS);
while (retry.attempt()) {
try {
mAsyncJournalWriter.flush(mFlushCounter);
return;
} catch (NotLeaderException | JournalClosedException e) {
throw new UnavailableException(String.format("Failed to complete request: %s", e.getMessage()), e);
} catch (AlluxioStatusException e) {
// written already
if (e.getStatus().equals(Status.CANCELLED)) {
LOG.warn("Journal flush interrupted because the RPC was cancelled. ", e);
} else {
LOG.warn("Journal flush failed. retrying...", e);
}
} catch (IOException e) {
if (e instanceof AlluxioStatusException && ((AlluxioStatusException) e).getStatusCode() == Status.Code.CANCELLED) {
throw new UnavailableException(String.format("Failed to complete request: %s", e.getMessage()), e);
}
LOG.warn("Journal flush failed. retrying...", e);
} catch (Throwable e) {
ProcessUtils.fatalError(LOG, e, "Journal flush failed");
}
}
ProcessUtils.fatalError(LOG, "Journal flush failed after %d attempts", retry.getAttemptCount());
}
use of alluxio.exception.JournalClosedException in project alluxio by Alluxio.
the class AsyncJournalWriter method flush.
/**
* Submits a ticket to flush thread and waits until ticket is served.
*
* If the specified counter is already flushed, this is essentially a no-op.
*
* @param targetCounter the counter to flush
*/
public void flush(final long targetCounter) throws IOException, JournalClosedException {
// Return if flushed.
if (targetCounter <= mFlushCounter.get()) {
return;
}
// Submit the ticket for flush thread to process.
FlushTicket ticket = new FlushTicket(targetCounter);
mTicketSet.add(ticket);
try {
// Give a permit for flush thread to run.
mFlushSemaphore.release();
// Wait on the ticket until completed.
ticket.waitCompleted();
} catch (InterruptedException ie) {
// Interpret interruption as cancellation.
throw new AlluxioStatusException(Status.CANCELLED.withCause(ie));
} catch (Throwable e) {
// Filter, journal specific exception codes.
if (e instanceof IOException) {
throw (IOException) e;
}
if (e instanceof JournalClosedException) {
throw (JournalClosedException) e;
}
// Not expected. throw internal error.
throw new AlluxioStatusException(Status.INTERNAL.withCause(e));
} finally {
/*
* Client can only try to reacquire the permit it has given
* because the permit may or may not have been used by the flush thread.
*/
mFlushSemaphore.tryAcquire();
}
}
Aggregations