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);
}
}
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);
}
}
}
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);
}
}
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);
}
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);
}
Aggregations