use of org.apache.cassandra.exceptions.WriteTimeoutException in project cassandra by apache.
the class StorageProxy method mutateAtomically.
/**
* See mutate. Adds additional steps before and after writing a batch.
* Before writing the batch (but after doing availability check against the FD for the row replicas):
* write the entire batch to a batchlog elsewhere in the cluster.
* After: remove the batchlog entry (after writing hints for the batch rows, if necessary).
*
* @param mutations the Mutations to be applied across the replicas
* @param consistency_level the consistency level for the operation
* @param requireQuorumForRemove at least a quorum of nodes will see update before deleting batchlog
* @param queryStartNanoTime the value of nanoTime() when the query started to be processed
*/
public static void mutateAtomically(Collection<Mutation> mutations, ConsistencyLevel consistency_level, boolean requireQuorumForRemove, long queryStartNanoTime) throws UnavailableException, OverloadedException, WriteTimeoutException {
Tracing.trace("Determining replicas for atomic batch");
long startTime = nanoTime();
List<WriteResponseHandlerWrapper> wrappers = new ArrayList<>(mutations.size());
if (mutations.stream().anyMatch(mutation -> Keyspace.open(mutation.getKeyspaceName()).getReplicationStrategy().hasTransientReplicas()))
throw new AssertionError("Logged batches are unsupported with transient replication");
try {
// If we are requiring quorum nodes for removal, we upgrade consistency level to QUORUM unless we already
// require ALL, or EACH_QUORUM. This is so that *at least* QUORUM nodes see the update.
ConsistencyLevel batchConsistencyLevel = requireQuorumForRemove ? ConsistencyLevel.QUORUM : consistency_level;
switch(consistency_level) {
case ALL:
case EACH_QUORUM:
batchConsistencyLevel = consistency_level;
}
ReplicaPlan.ForTokenWrite replicaPlan = ReplicaPlans.forBatchlogWrite(batchConsistencyLevel == ConsistencyLevel.ANY);
final UUID batchUUID = UUIDGen.getTimeUUID();
BatchlogCleanup cleanup = new BatchlogCleanup(mutations.size(), () -> asyncRemoveFromBatchlog(replicaPlan, batchUUID));
// add a handler for each mutation - includes checking availability, but doesn't initiate any writes, yet
for (Mutation mutation : mutations) {
if (hasLocalMutation(mutation))
writeMetrics.localRequests.mark();
else
writeMetrics.remoteRequests.mark();
WriteResponseHandlerWrapper wrapper = wrapBatchResponseHandler(mutation, consistency_level, batchConsistencyLevel, WriteType.BATCH, cleanup, queryStartNanoTime);
// exit early if we can't fulfill the CL at this time.
wrappers.add(wrapper);
}
// write to the batchlog
syncWriteToBatchlog(mutations, replicaPlan, batchUUID, queryStartNanoTime);
// now actually perform the writes and wait for them to complete
syncWriteBatchedMutations(wrappers, Stage.MUTATION);
} catch (UnavailableException e) {
writeMetrics.unavailables.mark();
writeMetricsForLevel(consistency_level).unavailables.mark();
Tracing.trace("Unavailable");
throw e;
} catch (WriteTimeoutException e) {
writeMetrics.timeouts.mark();
writeMetricsForLevel(consistency_level).timeouts.mark();
Tracing.trace("Write timeout; received {} of {} required replies", e.received, e.blockFor);
throw e;
} catch (WriteFailureException e) {
writeMetrics.failures.mark();
writeMetricsForLevel(consistency_level).failures.mark();
Tracing.trace("Write failure; received {} of {} required replies", e.received, e.blockFor);
throw e;
} finally {
long latency = nanoTime() - startTime;
writeMetrics.addNano(latency);
writeMetricsForLevel(consistency_level).addNano(latency);
updateCoordinatorWriteLatencyTableMetric(mutations, latency);
}
}
use of org.apache.cassandra.exceptions.WriteTimeoutException in project cassandra by apache.
the class MutationVerbHandler method doVerb.
public void doVerb(Message<Mutation> message) {
// Check if there were any forwarding headers in this message
InetAddressAndPort from = message.respondTo();
InetAddressAndPort respondToAddress;
if (from == null) {
respondToAddress = message.from();
ForwardingInfo forwardTo = message.forwardTo();
if (forwardTo != null)
forwardToLocalNodes(message, forwardTo);
} else {
respondToAddress = from;
}
try {
message.payload.applyFuture().addCallback(o -> respond(message, respondToAddress), wto -> failed());
} catch (WriteTimeoutException wto) {
failed();
}
}
use of org.apache.cassandra.exceptions.WriteTimeoutException in project cassandra by apache.
the class CounterMutation method grabCounterLocks.
private void grabCounterLocks(Keyspace keyspace, List<Lock> locks) throws WriteTimeoutException {
long startTime = nanoTime();
AbstractReplicationStrategy replicationStrategy = keyspace.getReplicationStrategy();
for (Lock lock : LOCKS.bulkGet(getCounterLockKeys())) {
long timeout = getTimeout(NANOSECONDS) - (nanoTime() - startTime);
try {
if (!lock.tryLock(timeout, NANOSECONDS))
throw new WriteTimeoutException(WriteType.COUNTER, consistency(), 0, consistency().blockFor(replicationStrategy));
locks.add(lock);
} catch (InterruptedException e) {
throw new WriteTimeoutException(WriteType.COUNTER, consistency(), 0, consistency().blockFor(replicationStrategy));
}
}
}
use of org.apache.cassandra.exceptions.WriteTimeoutException in project cassandra by apache.
the class AbstractWriteResponseHandler method get.
public void get() throws WriteTimeoutException, WriteFailureException {
long timeoutNanos = currentTimeoutNanos();
boolean success;
try {
success = condition.await(timeoutNanos, NANOSECONDS);
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e);
}
if (!success) {
int blockedFor = blockFor();
int acks = ackCount();
// avoid sending confusing info to the user (see CASSANDRA-6491).
if (acks >= blockedFor)
acks = blockedFor - 1;
throw new WriteTimeoutException(writeType, replicaPlan.consistencyLevel(), acks, blockedFor);
}
if (blockFor() + failures > candidateReplicaCount()) {
throw new WriteFailureException(replicaPlan.consistencyLevel(), ackCount(), blockFor(), writeType, failureReasonByEndpoint);
}
}
use of org.apache.cassandra.exceptions.WriteTimeoutException in project cassandra by apache.
the class ErrorMessageTest method testV4CasWriteResultUnknownSerDeser.
@Test
public void testV4CasWriteResultUnknownSerDeser() {
int receivedBlockFor = 3;
ConsistencyLevel consistencyLevel = ConsistencyLevel.SERIAL;
CasWriteUnknownResultException ex = new CasWriteUnknownResultException(consistencyLevel, receivedBlockFor, receivedBlockFor);
ErrorMessage deserialized = encodeThenDecode(ErrorMessage.fromException(ex), ProtocolVersion.V4);
assertTrue(deserialized.error instanceof WriteTimeoutException);
assertFalse(deserialized.error instanceof CasWriteUnknownResultException);
WriteTimeoutException deserializedEx = (WriteTimeoutException) deserialized.error;
assertEquals(consistencyLevel, deserializedEx.consistency);
assertEquals(receivedBlockFor, deserializedEx.received);
assertEquals(receivedBlockFor, deserializedEx.blockFor);
}
Aggregations