Search in sources :

Example 6 with WriteTimeoutException

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);
    }
}
Also used : ReplicaPlan(org.apache.cassandra.locator.ReplicaPlan) ArrayList(java.util.ArrayList) UnavailableException(org.apache.cassandra.exceptions.UnavailableException) ConsistencyLevel(org.apache.cassandra.db.ConsistencyLevel) CasWriteTimeoutException(org.apache.cassandra.exceptions.CasWriteTimeoutException) WriteTimeoutException(org.apache.cassandra.exceptions.WriteTimeoutException) WriteFailureException(org.apache.cassandra.exceptions.WriteFailureException) BatchlogCleanup(org.apache.cassandra.service.BatchlogResponseHandler.BatchlogCleanup) Mutation(org.apache.cassandra.db.Mutation) CounterMutation(org.apache.cassandra.db.CounterMutation) IMutation(org.apache.cassandra.db.IMutation) UUID(java.util.UUID)

Example 7 with WriteTimeoutException

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();
    }
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) WriteTimeoutException(org.apache.cassandra.exceptions.WriteTimeoutException)

Example 8 with WriteTimeoutException

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));
        }
    }
}
Also used : WriteTimeoutException(org.apache.cassandra.exceptions.WriteTimeoutException) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) Lock(java.util.concurrent.locks.Lock)

Example 9 with WriteTimeoutException

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);
    }
}
Also used : WriteTimeoutException(org.apache.cassandra.exceptions.WriteTimeoutException) WriteFailureException(org.apache.cassandra.exceptions.WriteFailureException) UncheckedInterruptedException(org.apache.cassandra.utils.concurrent.UncheckedInterruptedException) UncheckedInterruptedException(org.apache.cassandra.utils.concurrent.UncheckedInterruptedException)

Example 10 with WriteTimeoutException

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);
}
Also used : ConsistencyLevel(org.apache.cassandra.db.ConsistencyLevel) CasWriteTimeoutException(org.apache.cassandra.exceptions.CasWriteTimeoutException) WriteTimeoutException(org.apache.cassandra.exceptions.WriteTimeoutException) ErrorMessage(org.apache.cassandra.transport.messages.ErrorMessage) CasWriteUnknownResultException(org.apache.cassandra.exceptions.CasWriteUnknownResultException) Test(org.junit.Test)

Aggregations

WriteTimeoutException (org.apache.cassandra.exceptions.WriteTimeoutException)18 CasWriteTimeoutException (org.apache.cassandra.exceptions.CasWriteTimeoutException)10 WriteFailureException (org.apache.cassandra.exceptions.WriteFailureException)7 UnavailableException (org.apache.cassandra.exceptions.UnavailableException)6 ConsistencyLevel (org.apache.cassandra.db.ConsistencyLevel)5 UncheckedInterruptedException (org.apache.cassandra.utils.concurrent.UncheckedInterruptedException)5 UUID (java.util.UUID)4 CasWriteUnknownResultException (org.apache.cassandra.exceptions.CasWriteUnknownResultException)4 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)4 OverloadedException (org.apache.cassandra.exceptions.OverloadedException)4 ReadAbortException (org.apache.cassandra.exceptions.ReadAbortException)4 ReadFailureException (org.apache.cassandra.exceptions.ReadFailureException)4 ReadTimeoutException (org.apache.cassandra.exceptions.ReadTimeoutException)4 Hint (org.apache.cassandra.hints.Hint)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 TimeoutException (java.util.concurrent.TimeoutException)3 Lock (java.util.concurrent.locks.Lock)3 CounterMutation (org.apache.cassandra.db.CounterMutation)3 IMutation (org.apache.cassandra.db.IMutation)3