Search in sources :

Example 1 with TraceState

use of org.apache.cassandra.tracing.TraceState in project cassandra by apache.

the class RepairRunnable method runMayThrow.

protected void runMayThrow() throws Exception {
    final TraceState traceState;
    final UUID parentSession = UUIDGen.getTimeUUID();
    final String tag = "repair:" + cmd;
    final AtomicInteger progress = new AtomicInteger();
    // get valid column families, calculate neighbors, validation, prepare for repair + number of ranges to repair
    final int totalProgress = 4 + options.getRanges().size();
    String[] columnFamilies = options.getColumnFamilies().toArray(new String[options.getColumnFamilies().size()]);
    Iterable<ColumnFamilyStore> validColumnFamilies;
    try {
        validColumnFamilies = storageService.getValidColumnFamilies(false, false, keyspace, columnFamilies);
        progress.incrementAndGet();
    } catch (IllegalArgumentException e) {
        logger.error("Repair failed:", e);
        fireErrorAndComplete(tag, progress.get(), totalProgress, e.getMessage());
        return;
    }
    final long startTime = System.currentTimeMillis();
    String message = String.format("Starting repair command #%d (%s), repairing keyspace %s with %s", cmd, parentSession, keyspace, options);
    logger.info(message);
    if (options.isTraced()) {
        StringBuilder cfsb = new StringBuilder();
        for (ColumnFamilyStore cfs : validColumnFamilies) cfsb.append(", ").append(cfs.keyspace.getName()).append(".").append(cfs.name);
        UUID sessionId = Tracing.instance.newSession(Tracing.TraceType.REPAIR);
        traceState = Tracing.instance.begin("repair", ImmutableMap.of("keyspace", keyspace, "columnFamilies", cfsb.substring(2)));
        message = message + " tracing with " + sessionId;
        fireProgressEvent(tag, new ProgressEvent(ProgressEventType.START, 0, 100, message));
        Tracing.traceRepair(message);
        traceState.enableActivityNotification(tag);
        for (ProgressListener listener : listeners) traceState.addProgressListener(listener);
        Thread queryThread = createQueryThread(cmd, sessionId);
        queryThread.setName("RepairTracePolling");
        queryThread.start();
    } else {
        fireProgressEvent(tag, new ProgressEvent(ProgressEventType.START, 0, 100, message));
        traceState = null;
    }
    final Set<InetAddress> allNeighbors = new HashSet<>();
    List<Pair<Set<InetAddress>, ? extends Collection<Range<Token>>>> commonRanges = new ArrayList<>();
    //pre-calculate output of getLocalRanges and pass it to getNeighbors to increase performance and prevent
    //calculation multiple times
    Collection<Range<Token>> keyspaceLocalRanges = storageService.getLocalRanges(keyspace);
    try {
        for (Range<Token> range : options.getRanges()) {
            Set<InetAddress> neighbors = ActiveRepairService.getNeighbors(keyspace, keyspaceLocalRanges, range, options.getDataCenters(), options.getHosts());
            addRangeToNeighbors(commonRanges, range, neighbors);
            allNeighbors.addAll(neighbors);
        }
        progress.incrementAndGet();
    } catch (IllegalArgumentException e) {
        logger.error("Repair failed:", e);
        fireErrorAndComplete(tag, progress.get(), totalProgress, e.getMessage());
        return;
    }
    // Validate columnfamilies
    List<ColumnFamilyStore> columnFamilyStores = new ArrayList<>();
    try {
        Iterables.addAll(columnFamilyStores, validColumnFamilies);
        progress.incrementAndGet();
    } catch (IllegalArgumentException e) {
        fireErrorAndComplete(tag, progress.get(), totalProgress, e.getMessage());
        return;
    }
    String[] cfnames = new String[columnFamilyStores.size()];
    for (int i = 0; i < columnFamilyStores.size(); i++) {
        cfnames[i] = columnFamilyStores.get(i).name;
    }
    SystemDistributedKeyspace.startParentRepair(parentSession, keyspace, cfnames, options);
    long repairedAt;
    try {
        ActiveRepairService.instance.prepareForRepair(parentSession, FBUtilities.getBroadcastAddress(), allNeighbors, options, columnFamilyStores);
        repairedAt = ActiveRepairService.instance.getParentRepairSession(parentSession).getRepairedAt();
        progress.incrementAndGet();
    } catch (Throwable t) {
        SystemDistributedKeyspace.failParentRepair(parentSession, t);
        fireErrorAndComplete(tag, progress.get(), totalProgress, t.getMessage());
        return;
    }
    if (options.isIncremental()) {
        consistentRepair(parentSession, repairedAt, startTime, traceState, allNeighbors, commonRanges, cfnames);
    } else {
        normalRepair(parentSession, startTime, traceState, allNeighbors, commonRanges, cfnames);
    }
}
Also used : Token(org.apache.cassandra.dht.Token) ProgressEvent(org.apache.cassandra.utils.progress.ProgressEvent) Pair(org.apache.cassandra.utils.Pair) TraceState(org.apache.cassandra.tracing.TraceState) Range(org.apache.cassandra.dht.Range) ProgressListener(org.apache.cassandra.utils.progress.ProgressListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) InetAddress(java.net.InetAddress)

Example 2 with TraceState

use of org.apache.cassandra.tracing.TraceState in project cassandra by apache.

the class OutboundTcpConnection method writeConnected.

private void writeConnected(QueuedMessage qm, boolean flush) {
    try {
        byte[] sessionBytes = qm.message.parameters.get(Tracing.TRACE_HEADER);
        if (sessionBytes != null) {
            UUID sessionId = UUIDGen.getUUID(ByteBuffer.wrap(sessionBytes));
            TraceState state = Tracing.instance.get(sessionId);
            String message = String.format("Sending %s message to %s message size %d bytes", qm.message.verb, poolReference.endPoint(), qm.message.serializedSize(targetVersion) + PROTOCOL_MAGIC_ID_TIMESTAMP_SIZE);
            // session may have already finished; see CASSANDRA-5668
            if (state == null) {
                byte[] traceTypeBytes = qm.message.parameters.get(Tracing.TRACE_TYPE);
                Tracing.TraceType traceType = traceTypeBytes == null ? Tracing.TraceType.QUERY : Tracing.TraceType.deserialize(traceTypeBytes[0]);
                Tracing.instance.trace(ByteBuffer.wrap(sessionBytes), message, traceType.getTTL());
            } else {
                state.trace(message);
                if (qm.message.verb == MessagingService.Verb.REQUEST_RESPONSE)
                    Tracing.instance.doneWithNonLocalSession(state);
            }
        }
        long timestampMillis = NanoTimeToCurrentTimeMillis.convert(qm.timestampNanos);
        writeInternal(qm.message, qm.id, timestampMillis);
        completed++;
        if (flush)
            out.flush();
    } catch (Throwable e) {
        JVMStabilityInspector.inspectThrowable(e);
        disconnect();
        if (e instanceof IOException || e.getCause() instanceof IOException) {
            logger.debug("Error writing to {}", poolReference.endPoint(), e);
            // See CASSANDRA-5393 and CASSANDRA-12192.
            if (qm.shouldRetry()) {
                try {
                    backlog.put(new RetriedQueuedMessage(qm));
                } catch (InterruptedException e1) {
                    throw new AssertionError(e1);
                }
            }
        } else {
            // Non IO exceptions are likely a programming error so let's not silence them
            logger.error("error writing to {}", poolReference.endPoint(), e);
        }
    }
}
Also used : TraceState(org.apache.cassandra.tracing.TraceState) Tracing(org.apache.cassandra.tracing.Tracing) IOException(java.io.IOException)

Example 3 with TraceState

use of org.apache.cassandra.tracing.TraceState in project cassandra by apache.

the class DebuggableThreadPoolExecutorTest method withTracing.

private static void withTracing(Runnable fn) {
    TraceState state = Tracing.instance.get();
    try {
        Tracing.instance.set(new TraceStateImpl(InetAddressAndPort.getByAddress(InetAddresses.forString("127.0.0.1")), UUID.randomUUID(), Tracing.TraceType.NONE));
        fn.run();
    } finally {
        Tracing.instance.set(state);
    }
}
Also used : TraceState(org.apache.cassandra.tracing.TraceState) TraceStateImpl(org.apache.cassandra.tracing.TraceStateImpl)

Example 4 with TraceState

use of org.apache.cassandra.tracing.TraceState in project cassandra by apache.

the class RepairRunnable method complete.

private void complete(String msg) {
    long durationMillis = currentTimeMillis() - creationTimeMillis;
    if (msg == null) {
        String duration = DurationFormatUtils.formatDurationWords(durationMillis, true, true);
        msg = String.format("Repair command #%d finished in %s", cmd, duration);
    }
    fireProgressEvent(new ProgressEvent(ProgressEventType.COMPLETE, progressCounter.get(), totalProgress, msg));
    logger.info(options.getPreviewKind().logPrefix(parentSession) + msg);
    ActiveRepairService.instance.removeParentRepairSession(parentSession);
    TraceState localState = traceState;
    if (options.isTraced() && localState != null) {
        for (ProgressListener listener : listeners) localState.removeProgressListener(listener);
        // Because ExecutorPlus#afterExecute and this callback
        // run in a nondeterministic order (within the same thread), the
        // TraceState may have been nulled out at this point. The TraceState
        // should be traceState, so just set it without bothering to check if it
        // actually was nulled out.
        Tracing.instance.set(localState);
        Tracing.traceRepair(msg);
        Tracing.instance.stopSession();
    }
    Keyspace.open(keyspace).metric.repairTime.update(durationMillis, TimeUnit.MILLISECONDS);
}
Also used : TraceState(org.apache.cassandra.tracing.TraceState) ProgressListener(org.apache.cassandra.utils.progress.ProgressListener) ProgressEvent(org.apache.cassandra.utils.progress.ProgressEvent)

Example 5 with TraceState

use of org.apache.cassandra.tracing.TraceState in project cassandra by apache.

the class ExecutorLocals method create.

/**
     * This creates a new ExecutorLocals object based on what is already set.
     *
     * @return an ExecutorLocals object which has the trace state and client warn state captured if either has been set,
     *         or null if both are unset. The null result short-circuits logic in
     *         {@link AbstractLocalAwareExecutorService#newTaskFor(Runnable, Object, ExecutorLocals)}, preventing
     *         unnecessarily calling {@link ExecutorLocals#set(ExecutorLocals)}.
     */
public static ExecutorLocals create() {
    TraceState traceState = tracing.get();
    ClientWarn.State clientWarnState = clientWarn.get();
    if (traceState == null && clientWarnState == null)
        return null;
    else
        return new ExecutorLocals(traceState, clientWarnState);
}
Also used : TraceState(org.apache.cassandra.tracing.TraceState) ClientWarn(org.apache.cassandra.service.ClientWarn)

Aggregations

TraceState (org.apache.cassandra.tracing.TraceState)11 ProgressListener (org.apache.cassandra.utils.progress.ProgressListener)3 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)2 ClientWarn (org.apache.cassandra.service.ClientWarn)2 ProgressEvent (org.apache.cassandra.utils.progress.ProgressEvent)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 UUID (java.util.UUID)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 LocalAwareExecutorService (org.apache.cassandra.concurrent.LocalAwareExecutorService)1 Range (org.apache.cassandra.dht.Range)1 Token (org.apache.cassandra.dht.Token)1 Header (org.apache.cassandra.net.Message.Header)1 TraceStateImpl (org.apache.cassandra.tracing.TraceStateImpl)1 Tracing (org.apache.cassandra.tracing.Tracing)1 Pair (org.apache.cassandra.utils.Pair)1