Search in sources :

Example 66 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class GrpcBlockingStream method send.

/**
 * Sends a request. Will wait until the stream is ready before sending or timeout if the
 * given timeout is reached.
 *
 * @param request the request
 * @param timeoutMs maximum wait time before throwing a {@link DeadlineExceededException}
 * @throws IOException if any error occurs
 */
public void send(ReqT request, long timeoutMs) throws IOException {
    if (mClosed || mCanceled || mClosedFromRemote) {
        throw new CancelledException(formatErrorMessage("Failed to send request %s: stream is already closed or cancelled. clientClosed: %s " + "clientCancelled: %s serverClosed: %s", LogUtils.truncateMessageLineLength(request), mClosed, mCanceled, mClosedFromRemote));
    }
    try (LockResource lr = new LockResource(mLock)) {
        long startMs = System.currentTimeMillis();
        while (true) {
            checkError();
            if (mRequestObserver.isReady()) {
                break;
            }
            long waitedForMs = System.currentTimeMillis() - startMs;
            if (waitedForMs >= timeoutMs) {
                throw new DeadlineExceededException(formatErrorMessage("Timeout sending request %s after %dms. clientClosed: %s clientCancelled: %s " + "serverClosed: %s", LogUtils.truncateMessageLineLength(request), timeoutMs, mClosed, mCanceled, mClosedFromRemote));
            }
            try {
                // Wait for a minute max
                long awaitMs = Math.min(timeoutMs - waitedForMs, Constants.MINUTE_MS);
                if (!mReadyOrFailed.await(awaitMs, TimeUnit.MILLISECONDS)) {
                    // Log a warning before looping again
                    LOG.warn("Stream is not ready for client to send request, will wait again. totalWaitMs: {} " + "clientClosed: {} clientCancelled: {} serverClosed: {} description: {}", System.currentTimeMillis() - startMs, mClosed, mCanceled, mClosedFromRemote, mDescription);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new CancelledException(formatErrorMessage("Failed to send request %s: interrupted while waiting for server.", LogUtils.truncateMessageLineLength(request)), e);
            }
        }
    }
    mRequestObserver.onNext(request);
}
Also used : LockResource(alluxio.resource.LockResource) CancelledException(alluxio.exception.status.CancelledException) DeadlineExceededException(alluxio.exception.status.DeadlineExceededException)

Example 67 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class SharedGrpcDataReader method close.

@Override
public void close() throws IOException {
    if (mCachedDataReader.deRef() > 0) {
        return;
    }
    try (LockResource lockResource = new LockResource(getLock(mBlockId).writeLock())) {
        if (mCachedDataReader.getRefCount() == 0) {
            BLOCK_READERS.remove(mBlockId);
            mCachedDataReader.close();
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource)

Example 68 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class BufferCachingGrpcDataReader method readChunk.

/**
 * Reads a specific chunk from the block.
 *
 * @param index the chunk index
 * @return the chunk data if exists
 */
@Nullable
public DataBuffer readChunk(int index) throws IOException {
    if (index >= mDataBuffers.length) {
        return null;
    }
    if (index >= mBufferCount.get()) {
        try (LockResource r1 = new LockResource(mBufferLocks.writeLock())) {
            while (index >= mBufferCount.get()) {
                DataBuffer buffer = readChunk();
                mDataBuffers[mBufferCount.get()] = buffer;
                mBufferCount.incrementAndGet();
            }
        }
    }
    return mDataBuffers[index];
}
Also used : LockResource(alluxio.resource.LockResource) NioDataBuffer(alluxio.network.protocol.databuffer.NioDataBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer) Nullable(javax.annotation.Nullable)

Example 69 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class BufferedJournalApplier method catchup.

/**
 * Initiates catching up of the applier to a target sequence.
 * This method leaves the applier in suspended state.
 *
 * @param sequence target sequence
 * @return the future to track when applier reaches the target sequence
 */
public CatchupFuture catchup(long sequence) {
    try (LockResource stateLock = new LockResource(mStateLock)) {
        Preconditions.checkState(mSuspended, "Not suspended");
        Preconditions.checkState(!mResumeInProgress, "Resume in progress");
        Preconditions.checkState(mCatchupThread == null || !mCatchupThread.isAlive(), "Catch-up task in progress.");
        Preconditions.checkState(sequence >= 0, "Invalid negative sequence: %d", sequence);
        Preconditions.checkState(mLastAppliedSequence <= sequence, "Can't catchup to past. Current: %d, Requested: %d", mLastAppliedSequence, sequence);
        LOG.info("Catching up state machine to sequence: {}", sequence);
        // Complete the request if already at target sequence.
        if (mLastAppliedSequence == sequence) {
            return CatchupFuture.completed();
        }
        // Create an async task for catching up to target sequence.
        mCatchupThread = new RaftJournalCatchupThread(sequence);
        mCatchupThread.start();
        return new CatchupFuture(mCatchupThread);
    }
}
Also used : LockResource(alluxio.resource.LockResource) CatchupFuture(alluxio.master.journal.CatchupFuture)

Example 70 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class BufferedJournalApplier method processJournalEntry.

/**
 * Processes given journal entry for applying. Entry could be applied or buffered based on
 * buffer's state.
 *
 * @param journalEntry the journal entry
 */
public void processJournalEntry(Journal.JournalEntry journalEntry) {
    try (LockResource stateLock = new LockResource(mStateLock)) {
        if (mSuspended) {
            // New entry submissions are monitored by catch-up threads.
            synchronized (mSuspendBuffer) {
                mSuspendBuffer.offer(journalEntry);
                mSuspendBuffer.notifyAll();
            }
        } else {
            applyToMaster(journalEntry);
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource)

Aggregations

LockResource (alluxio.resource.LockResource)116 IOException (java.io.IOException)14 TempBlockMeta (alluxio.worker.block.meta.TempBlockMeta)13 HashMap (java.util.HashMap)12 BlockAlreadyExistsException (alluxio.exception.BlockAlreadyExistsException)11 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)11 Map (java.util.Map)11 AlluxioURI (alluxio.AlluxioURI)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)10 WorkerOutOfSpaceException (alluxio.exception.WorkerOutOfSpaceException)9 ArrayList (java.util.ArrayList)9 MasterWorkerInfo (alluxio.master.block.meta.MasterWorkerInfo)8 InvalidPathException (alluxio.exception.InvalidPathException)7 NotFoundException (alluxio.exception.status.NotFoundException)7 List (java.util.List)7 Lock (java.util.concurrent.locks.Lock)7 ConcurrentHashSet (alluxio.collections.ConcurrentHashSet)6 InvalidWorkerStateException (alluxio.exception.InvalidWorkerStateException)6 UnavailableException (alluxio.exception.status.UnavailableException)6