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);
}
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();
}
}
}
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];
}
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);
}
}
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);
}
}
}
Aggregations