Search in sources :

Example 1 with WriteType

use of org.apache.cassandra.db.WriteType in project cassandra by apache.

the class StorageProxy method mutate.

/**
 * Use this method to have these Mutations applied
 * across all replicas. This method will take care
 * of the possibility of a replica being down and hint
 * the data across to some other replica.
 *
 * @param mutations the mutations to be applied across the replicas
 * @param consistencyLevel the consistency level for the operation
 * @param queryStartNanoTime the value of nanoTime() when the query started to be processed
 */
public static void mutate(List<? extends IMutation> mutations, ConsistencyLevel consistencyLevel, long queryStartNanoTime) throws UnavailableException, OverloadedException, WriteTimeoutException, WriteFailureException {
    Tracing.trace("Determining replicas for mutation");
    final String localDataCenter = DatabaseDescriptor.getEndpointSnitch().getLocalDatacenter();
    long startTime = nanoTime();
    List<AbstractWriteResponseHandler<IMutation>> responseHandlers = new ArrayList<>(mutations.size());
    WriteType plainWriteType = mutations.size() <= 1 ? WriteType.SIMPLE : WriteType.UNLOGGED_BATCH;
    try {
        for (IMutation mutation : mutations) {
            if (hasLocalMutation(mutation))
                writeMetrics.localRequests.mark();
            else
                writeMetrics.remoteRequests.mark();
            if (mutation instanceof CounterMutation)
                responseHandlers.add(mutateCounter((CounterMutation) mutation, localDataCenter, queryStartNanoTime));
            else
                responseHandlers.add(performWrite(mutation, consistencyLevel, localDataCenter, standardWritePerformer, null, plainWriteType, queryStartNanoTime));
        }
        // upgrade to full quorum any failed cheap quorums
        for (int i = 0; i < mutations.size(); ++i) {
            if (// at the moment, only non-counter writes support cheap quorums
            !(mutations.get(i) instanceof CounterMutation))
                responseHandlers.get(i).maybeTryAdditionalReplicas(mutations.get(i), standardWritePerformer, localDataCenter);
        }
        // wait for writes.  throws TimeoutException if necessary
        for (AbstractWriteResponseHandler<IMutation> responseHandler : responseHandlers) responseHandler.get();
    } catch (WriteTimeoutException | WriteFailureException ex) {
        if (consistencyLevel == ConsistencyLevel.ANY) {
            hintMutations(mutations);
        } else {
            if (ex instanceof WriteFailureException) {
                writeMetrics.failures.mark();
                writeMetricsForLevel(consistencyLevel).failures.mark();
                WriteFailureException fe = (WriteFailureException) ex;
                Tracing.trace("Write failure; received {} of {} required replies, failed {} requests", fe.received, fe.blockFor, fe.failureReasonByEndpoint.size());
            } else {
                writeMetrics.timeouts.mark();
                writeMetricsForLevel(consistencyLevel).timeouts.mark();
                WriteTimeoutException te = (WriteTimeoutException) ex;
                Tracing.trace("Write timeout; received {} of {} required replies", te.received, te.blockFor);
            }
            throw ex;
        }
    } catch (UnavailableException e) {
        writeMetrics.unavailables.mark();
        writeMetricsForLevel(consistencyLevel).unavailables.mark();
        Tracing.trace("Unavailable");
        throw e;
    } catch (OverloadedException e) {
        writeMetrics.unavailables.mark();
        writeMetricsForLevel(consistencyLevel).unavailables.mark();
        Tracing.trace("Overloaded");
        throw e;
    } finally {
        long latency = nanoTime() - startTime;
        writeMetrics.addNano(latency);
        writeMetricsForLevel(consistencyLevel).addNano(latency);
        updateCoordinatorWriteLatencyTableMetric(mutations, latency);
    }
}
Also used : IMutation(org.apache.cassandra.db.IMutation) WriteType(org.apache.cassandra.db.WriteType) ArrayList(java.util.ArrayList) UnavailableException(org.apache.cassandra.exceptions.UnavailableException) OverloadedException(org.apache.cassandra.exceptions.OverloadedException) Hint(org.apache.cassandra.hints.Hint) CounterMutation(org.apache.cassandra.db.CounterMutation) CasWriteTimeoutException(org.apache.cassandra.exceptions.CasWriteTimeoutException) WriteTimeoutException(org.apache.cassandra.exceptions.WriteTimeoutException) WriteFailureException(org.apache.cassandra.exceptions.WriteFailureException)

Example 2 with WriteType

use of org.apache.cassandra.db.WriteType in project cassandra by apache.

the class ErrorMessageTest method testV5WriteFailureSerDeser.

@Test
public void testV5WriteFailureSerDeser() {
    int receivedBlockFor = 3;
    ConsistencyLevel consistencyLevel = ConsistencyLevel.ALL;
    WriteType writeType = WriteType.SIMPLE;
    WriteFailureException wfe = new WriteFailureException(consistencyLevel, receivedBlockFor, receivedBlockFor, writeType, failureReasonMap2);
    ErrorMessage deserialized = encodeThenDecode(ErrorMessage.fromException(wfe), ProtocolVersion.V5);
    WriteFailureException deserializedWfe = (WriteFailureException) deserialized.error;
    assertEquals(failureReasonMap2, deserializedWfe.failureReasonByEndpoint);
    assertEquals(receivedBlockFor, deserializedWfe.received);
    assertEquals(receivedBlockFor, deserializedWfe.blockFor);
    assertEquals(consistencyLevel, deserializedWfe.consistency);
    assertEquals(writeType, deserializedWfe.writeType);
}
Also used : ConsistencyLevel(org.apache.cassandra.db.ConsistencyLevel) WriteFailureException(org.apache.cassandra.exceptions.WriteFailureException) WriteType(org.apache.cassandra.db.WriteType) ErrorMessage(org.apache.cassandra.transport.messages.ErrorMessage) Test(org.junit.Test)

Aggregations

WriteType (org.apache.cassandra.db.WriteType)2 WriteFailureException (org.apache.cassandra.exceptions.WriteFailureException)2 ArrayList (java.util.ArrayList)1 ConsistencyLevel (org.apache.cassandra.db.ConsistencyLevel)1 CounterMutation (org.apache.cassandra.db.CounterMutation)1 IMutation (org.apache.cassandra.db.IMutation)1 CasWriteTimeoutException (org.apache.cassandra.exceptions.CasWriteTimeoutException)1 OverloadedException (org.apache.cassandra.exceptions.OverloadedException)1 UnavailableException (org.apache.cassandra.exceptions.UnavailableException)1 WriteTimeoutException (org.apache.cassandra.exceptions.WriteTimeoutException)1 Hint (org.apache.cassandra.hints.Hint)1 ErrorMessage (org.apache.cassandra.transport.messages.ErrorMessage)1 Test (org.junit.Test)1