Search in sources :

Example 1 with JournalClosedException

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;
    }
}
Also used : JournalClosedException(alluxio.exception.JournalClosedException) Message(org.apache.ratis.protocol.Message) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JournalEntry(alluxio.proto.journal.Journal.JournalEntry) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with JournalClosedException

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);
                }
            }
        }
    }
}
Also used : JournalClosedException(alluxio.exception.JournalClosedException) Timer(com.codahale.metrics.Timer) Iterator(java.util.Iterator) IOException(java.io.IOException) JournalEntry(alluxio.proto.journal.Journal.JournalEntry)

Example 3 with JournalClosedException

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());
}
Also used : NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) JournalClosedException(alluxio.exception.JournalClosedException) UnavailableException(alluxio.exception.status.UnavailableException) AlluxioStatusException(alluxio.exception.status.AlluxioStatusException) TimeoutRetry(alluxio.retry.TimeoutRetry) IOException(java.io.IOException) RetryPolicy(alluxio.retry.RetryPolicy)

Example 4 with JournalClosedException

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();
    }
}
Also used : JournalClosedException(alluxio.exception.JournalClosedException) AlluxioStatusException(alluxio.exception.status.AlluxioStatusException) IOException(java.io.IOException)

Aggregations

JournalClosedException (alluxio.exception.JournalClosedException)4 IOException (java.io.IOException)4 AlluxioStatusException (alluxio.exception.status.AlluxioStatusException)2 JournalEntry (alluxio.proto.journal.Journal.JournalEntry)2 UnavailableException (alluxio.exception.status.UnavailableException)1 RetryPolicy (alluxio.retry.RetryPolicy)1 TimeoutRetry (alluxio.retry.TimeoutRetry)1 Timer (com.codahale.metrics.Timer)1 Iterator (java.util.Iterator)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Message (org.apache.ratis.protocol.Message)1 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)1 NotLeaderException (org.apache.ratis.protocol.exceptions.NotLeaderException)1