use of org.apache.cassandra.utils.concurrent.UncheckedInterruptedException in project cassandra by apache.
the class Keyspace method applyInternal.
/**
* This method appends a row to the global CommitLog, then updates memtables and indexes.
*
* @param mutation the row to write. Must not be modified after calling apply, since commitlog append
* may happen concurrently, depending on the CL Executor type.
* @param makeDurable if true, don't return unless write has been made durable
* @param updateIndexes false to disable index updates (used by CollationController "defragmenting")
* @param isDroppable true if this should throw WriteTimeoutException if it does not acquire lock within write_request_timeout
* @param isDeferrable true if caller is not waiting for future to complete, so that future may be deferred
*/
private Future<?> applyInternal(final Mutation mutation, final boolean makeDurable, boolean updateIndexes, boolean isDroppable, boolean isDeferrable, Promise<?> future) {
if (TEST_FAIL_WRITES && metadata.name.equals(TEST_FAIL_WRITES_KS))
throw new RuntimeException("Testing write failures");
Lock[] locks = null;
boolean requiresViewUpdate = updateIndexes && viewManager.updatesAffectView(Collections.singleton(mutation), false);
if (requiresViewUpdate) {
mutation.viewLockAcquireStart.compareAndSet(0L, currentTimeMillis());
// the order of lock acquisition doesn't matter (from a deadlock perspective) because we only use tryLock()
Collection<TableId> tableIds = mutation.getTableIds();
Iterator<TableId> idIterator = tableIds.iterator();
locks = new Lock[tableIds.size()];
for (int i = 0; i < tableIds.size(); i++) {
TableId tableId = idIterator.next();
int lockKey = Objects.hash(mutation.key().getKey(), tableId);
while (true) {
Lock lock = null;
if (TEST_FAIL_MV_LOCKS_COUNT == 0)
lock = ViewManager.acquireLockFor(lockKey);
else
TEST_FAIL_MV_LOCKS_COUNT--;
if (lock == null) {
// throw WTE only if request is droppable
if (isDroppable && (approxTime.isAfter(mutation.approxCreatedAtNanos + DatabaseDescriptor.getWriteRpcTimeout(NANOSECONDS)))) {
for (int j = 0; j < i; j++) locks[j].unlock();
if (logger.isTraceEnabled())
logger.trace("Could not acquire lock for {} and table {}", ByteBufferUtil.bytesToHex(mutation.key().getKey()), columnFamilyStores.get(tableId).name);
Tracing.trace("Could not acquire MV lock");
if (future != null) {
future.tryFailure(new WriteTimeoutException(WriteType.VIEW, ConsistencyLevel.LOCAL_ONE, 0, 1));
return future;
} else
throw new WriteTimeoutException(WriteType.VIEW, ConsistencyLevel.LOCAL_ONE, 0, 1);
} else if (isDeferrable) {
for (int j = 0; j < i; j++) locks[j].unlock();
// This view update can't happen right now. so rather than keep this thread busy
// we will re-apply ourself to the queue and try again later
Stage.MUTATION.execute(() -> applyInternal(mutation, makeDurable, true, isDroppable, true, future));
return future;
} else {
// being blocked by waiting for futures which will never be processed as all workers are blocked
try {
// Wait a little bit before retrying to lock
Thread.sleep(10);
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e);
}
continue;
}
} else {
locks[i] = lock;
}
break;
}
}
long acquireTime = currentTimeMillis() - mutation.viewLockAcquireStart.get();
// Bulk non-droppable operations (e.g. commitlog replay, hint delivery) are not measured
if (isDroppable) {
for (TableId tableId : tableIds) columnFamilyStores.get(tableId).metric.viewLockAcquireTime.update(acquireTime, MILLISECONDS);
}
}
int nowInSec = FBUtilities.nowInSeconds();
try (WriteContext ctx = getWriteHandler().beginWrite(mutation, makeDurable)) {
for (PartitionUpdate upd : mutation.getPartitionUpdates()) {
ColumnFamilyStore cfs = columnFamilyStores.get(upd.metadata().id);
if (cfs == null) {
logger.error("Attempting to mutate non-existant table {} ({}.{})", upd.metadata().id, upd.metadata().keyspace, upd.metadata().name);
continue;
}
AtomicLong baseComplete = new AtomicLong(Long.MAX_VALUE);
if (requiresViewUpdate) {
try {
Tracing.trace("Creating materialized view mutations from base table replica");
viewManager.forTable(upd.metadata().id).pushViewReplicaUpdates(upd, makeDurable, baseComplete);
} catch (Throwable t) {
JVMStabilityInspector.inspectThrowable(t);
logger.error(String.format("Unknown exception caught while attempting to update MaterializedView! %s", upd.metadata().toString()), t);
throw t;
}
}
UpdateTransaction indexTransaction = updateIndexes ? cfs.indexManager.newUpdateTransaction(upd, ctx, nowInSec) : UpdateTransaction.NO_OP;
cfs.getWriteHandler().write(upd, ctx, indexTransaction);
if (requiresViewUpdate)
baseComplete.set(currentTimeMillis());
}
if (future != null) {
future.trySuccess(null);
}
return future;
} finally {
if (locks != null) {
for (Lock lock : locks) if (lock != null)
lock.unlock();
}
}
}
use of org.apache.cassandra.utils.concurrent.UncheckedInterruptedException in project cassandra by apache.
the class InterceptingExecutorFactory method factory.
<F extends ThreadFactory> F factory(String name, Object extraInfo, ThreadGroup threadGroup, UncaughtExceptionHandler uncaughtExceptionHandler, InterceptibleThreadFactory.MetaFactory<F> factory) {
if (uncaughtExceptionHandler == null)
uncaughtExceptionHandler = transferToInstance.apply((SerializableFunction<Supplier<Boolean>, UncaughtExceptionHandler>) (reportUnchecked) -> (thread, throwable) -> {
if (!(throwable instanceof UncheckedInterruptedException) || reportUnchecked.get())
JVMStabilityInspector.uncaughtException(thread, throwable);
}).apply(() -> !isClosed);
if (threadGroup == null)
threadGroup = this.threadGroup;
else if (!this.threadGroup.parentOf(threadGroup))
throw new IllegalArgumentException();
Runnable onTermination = transferToInstance.apply((SerializableRunnable) FastThreadLocal::removeAll);
LocalTime time = transferToInstance.apply((IIsolatedExecutor.SerializableCallable<LocalTime>) SimulatedTime.Global::current).call();
return factory.create(name, Thread.NORM_PRIORITY, classLoader, uncaughtExceptionHandler, threadGroup, onTermination, time, this, extraInfo);
}
use of org.apache.cassandra.utils.concurrent.UncheckedInterruptedException in project cassandra by apache.
the class BlockingPartitionRepair method awaitRepairsUntil.
/**
* Wait for the repair to complete util a future time
* If the {@param timeoutAt} is a past time, the method returns immediately with the repair result.
* @param timeoutAt, future time
* @param timeUnit, the time unit of the future time
* @return true if repair is done; otherwise, false.
*/
public boolean awaitRepairsUntil(long timeoutAt, TimeUnit timeUnit) {
long timeoutAtNanos = timeUnit.toNanos(timeoutAt);
long remaining = timeoutAtNanos - nanoTime();
try {
return latch.await(remaining, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e);
}
}
use of org.apache.cassandra.utils.concurrent.UncheckedInterruptedException in project cassandra by apache.
the class TruncateResponseHandler method get.
public void get() throws TimeoutException {
long timeoutNanos = getTruncateRpcTimeout(NANOSECONDS) - (nanoTime() - start);
boolean completedInTime;
try {
// TODO truncate needs a much longer timeout
completedInTime = condition.await(timeoutNanos, NANOSECONDS);
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e);
}
if (!completedInTime) {
throw new TimeoutException("Truncate timed out - received only " + responses.get() + " responses");
}
if (truncateFailingReplica != null) {
throw new TruncateException("Truncate failed on replica " + truncateFailingReplica);
}
}
use of org.apache.cassandra.utils.concurrent.UncheckedInterruptedException in project cassandra by apache.
the class CommitLog method stopUnsafe.
/**
* FOR TESTING PURPOSES
*/
@VisibleForTesting
public synchronized void stopUnsafe(boolean deleteSegments) {
if (!started)
return;
started = false;
executor.shutdown();
try {
executor.awaitTermination();
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e);
}
segmentManager.stopUnsafe(deleteSegments);
CommitLogSegment.resetReplayLimit();
if (DatabaseDescriptor.isCDCEnabled() && deleteSegments)
for (File f : new File(DatabaseDescriptor.getCDCLogLocation()).tryList()) f.delete();
}
Aggregations