Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
import org.apache.skywalking.apm.agent.core.conf.Config;
Expand All @@ -37,7 +37,6 @@
import org.apache.skywalking.apm.agent.core.context.trace.WithPeerInfo;
import org.apache.skywalking.apm.agent.core.dictionary.DictionaryManager;
import org.apache.skywalking.apm.agent.core.dictionary.DictionaryUtil;
import org.apache.skywalking.apm.agent.core.dictionary.PossibleFound;
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
import org.apache.skywalking.apm.agent.core.profile.ProfileTaskExecutionService;
Expand All @@ -63,12 +62,12 @@ public class TracingContext implements AbstractTracerContext {
private long lastWarningTimestamp = 0;

/**
* @see {@link ProfileTaskExecutionService}
* @see ProfileTaskExecutionService
*/
private static ProfileTaskExecutionService PROFILE_TASK_EXECUTION_SERVICE;

/**
* @see {@link SamplingService}
* @see SamplingService
*/
private static SamplingService SAMPLING_SERVICE;

Expand All @@ -82,7 +81,7 @@ public class TracingContext implements AbstractTracerContext {
* storage-structure. <p> I use {@link LinkedList#removeLast()}, {@link LinkedList#addLast(Object)} and {@link
* LinkedList#getLast()} instead of {@link #pop()}, {@link #push(AbstractSpan)}, {@link #peek()}
*/
private LinkedList<AbstractSpan> activeSpanStack = new LinkedList<AbstractSpan>();
private LinkedList<AbstractSpan> activeSpanStack = new LinkedList<>();

/**
* A counter for the next span.
Expand All @@ -92,7 +91,10 @@ public class TracingContext implements AbstractTracerContext {
/**
* The counter indicates
*/
private volatile AtomicInteger asyncSpanCounter;
@SuppressWarnings("unused") // updated by ASYNC_SPAN_COUNTER_UPDATER
private volatile int asyncSpanCounter;
private static final AtomicIntegerFieldUpdater<TracingContext> ASYNC_SPAN_COUNTER_UPDATER =
AtomicIntegerFieldUpdater.newUpdater(TracingContext.class, "asyncSpanCounter");
private volatile boolean isRunningInAsyncMode;
private volatile ReentrantLock asyncFinishLock;

Expand Down Expand Up @@ -170,7 +172,7 @@ public void inject(ContextCarrier carrier) {
entryApplicationInstanceId = ref.getEntryServiceInstanceId();
} else {
if (firstSpan.isEntry()) {
/**
/*
* Since 6.6.0, if first span is not entry span, then this is an internal segment(no RPC),
* rather than an endpoint.
*/
Expand All @@ -186,7 +188,7 @@ public void inject(ContextCarrier carrier) {
if (!StringUtil.isEmpty(operationName)) {
carrier.setEntryEndpointName(operationName);
} else {
/**
/*
* Since 6.6.0, if first span is not entry span, then this is an internal segment(no RPC),
* rather than an endpoint.
*/
Expand All @@ -200,7 +202,7 @@ public void inject(ContextCarrier carrier) {
if (firstSpan.isEntry() && !StringUtil.isEmpty(firstSpanOperationName)) {
carrier.setParentEndpointName(firstSpanOperationName);
} else {
/**
/*
* Since 6.6.0, if first span is not entry span, then this is an internal segment(no RPC),
* rather than an endpoint.
*/
Expand Down Expand Up @@ -257,7 +259,7 @@ public ContextSnapshot capture() {
entryOperationId = firstSpan.getOperationId();
entryOperationName = firstSpanOperationName;
} else {
/**
/*
* Since 6.6.0, if first span is not entry span, then this is an internal segment(no RPC),
* rather than an endpoint.
*/
Expand All @@ -271,7 +273,7 @@ public ContextSnapshot capture() {
if (!StringUtil.isEmpty(entryOperationName)) {
snapshot.setEntryOperationName(entryOperationName);
} else {
/**
/*
* Since 6.6.0, if first span is not entry span, then this is an internal segment(no RPC),
* rather than an endpoint.
*/
Expand All @@ -285,7 +287,7 @@ public ContextSnapshot capture() {
if (firstSpan.isEntry() && !StringUtil.isEmpty(firstSpanOperationName)) {
snapshot.setParentOperationName(firstSpanOperationName);
} else {
/**
/*
* Since 6.6.0, if first span is not entry span, then this is an internal segment(no RPC),
* rather than an endpoint.
*/
Expand Down Expand Up @@ -337,27 +339,13 @@ public AbstractSpan createEntrySpan(final String operationName) {
if (parentSpan != null && parentSpan.isEntry()) {
entrySpan = (AbstractTracingSpan)DictionaryManager.findEndpointSection()
.findOnly(segment.getServiceId(), operationName)
.doInCondition(new PossibleFound.FoundAndObtain() {
@Override public Object doProcess(int operationId) {
return parentSpan.setOperationId(operationId);
}
}, new PossibleFound.NotFoundAndObtain() {
@Override public Object doProcess() {
return parentSpan.setOperationName(operationName);
}
});
.doInCondition(parentSpan::setOperationId, () -> parentSpan.setOperationName(operationName));
return entrySpan.start();
} else {
entrySpan = (AbstractTracingSpan)DictionaryManager.findEndpointSection()
.findOnly(segment.getServiceId(), operationName)
.doInCondition(new PossibleFound.FoundAndObtain() {
@Override public Object doProcess(int operationId) {
return new EntrySpan(spanIdGenerator++, parentSpanId, operationId, owner);
}
}, new PossibleFound.NotFoundAndObtain() {
@Override public Object doProcess() {
return new EntrySpan(spanIdGenerator++, parentSpanId, operationName, owner);
}
.doInCondition(operationId -> new EntrySpan(spanIdGenerator++, parentSpanId, operationId, owner), () -> {
return new EntrySpan(spanIdGenerator++, parentSpanId, operationName, owner);
});
entrySpan.start();
return push(entrySpan);
Expand All @@ -378,7 +366,7 @@ public AbstractSpan createLocalSpan(final String operationName) {
}
AbstractSpan parentSpan = peek();
final int parentSpanId = parentSpan == null ? -1 : parentSpan.getSpanId();
/**
/*
* From v6.0.0-beta, local span doesn't do op name register.
* All op name register is related to entry and exit spans only.
*/
Expand Down Expand Up @@ -411,17 +399,9 @@ public AbstractSpan createExitSpan(final String operationName, final String remo
final int parentSpanId = parentSpan == null ? -1 : parentSpan.getSpanId();
exitSpan = (AbstractSpan)DictionaryManager.findNetworkAddressSection()
.find(remotePeer).doInCondition(
new PossibleFound.FoundAndObtain() {
@Override
public Object doProcess(final int peerId) {
return new ExitSpan(spanIdGenerator++, parentSpanId, operationName, peerId, owner);
}
},
new PossibleFound.NotFoundAndObtain() {
@Override
public Object doProcess() {
return new ExitSpan(spanIdGenerator++, parentSpanId, operationName, remotePeer, owner);
}
peerId -> new ExitSpan(spanIdGenerator++, parentSpanId, operationName, peerId, owner),
() -> {
return new ExitSpan(spanIdGenerator++, parentSpanId, operationName, remotePeer, owner);
});
push(exitSpan);
}
Expand Down Expand Up @@ -473,17 +453,17 @@ public boolean stopSpan(AbstractSpan span) {
synchronized (this) {
if (!isRunningInAsyncMode) {
asyncFinishLock = new ReentrantLock();
asyncSpanCounter = new AtomicInteger(0);
ASYNC_SPAN_COUNTER_UPDATER.set(this, 0);
isRunningInAsyncMode = true;
}
}
}
asyncSpanCounter.addAndGet(1);
ASYNC_SPAN_COUNTER_UPDATER.incrementAndGet(this);
return this;
}

@Override public void asyncStop(AsyncSpan span) {
asyncSpanCounter.addAndGet(-1);
ASYNC_SPAN_COUNTER_UPDATER.decrementAndGet(this);
finish();
}

Expand Down Expand Up @@ -513,13 +493,13 @@ private void finish() {
try {
boolean isFinishedInMainThread = activeSpanStack.isEmpty() && running;
if (isFinishedInMainThread) {
/**
/*
* Notify after tracing finished in the main thread.
*/
TracingThreadListenerManager.notifyFinish(this);
}

if (isFinishedInMainThread && (!isRunningInAsyncMode || asyncSpanCounter.get() == 0)) {
if (isFinishedInMainThread && (!isRunningInAsyncMode || asyncSpanCounter == 0)) {
TraceSegment finishedSegment = segment.finish(isLimitMechanismWorking());
/*
* Recheck the segment if the segment contains only one span.
Expand Down Expand Up @@ -558,7 +538,7 @@ private void finish() {
* when the <code>TracingContext</code> finished, and {@link #segment} is ready for further process.
*/
public static class ListenerManager {
private static List<TracingContextListener> LISTENERS = new LinkedList<TracingContextListener>();
private static List<TracingContextListener> LISTENERS = new LinkedList<>();

/**
* Add the given {@link TracingContextListener} to {@link #LISTENERS} list.
Expand All @@ -574,7 +554,7 @@ public static synchronized void add(TracingContextListener listener) {
* trigger {@link TracingContext.ListenerManager} to notify all {@link #LISTENERS} 's {@link
* TracingContextListener#afterFinished(TraceSegment)}
*
* @param finishedSegment
* @param finishedSegment the segment that has finished
*/
static void notifyFinish(TraceSegment finishedSegment) {
for (TracingContextListener listener : LISTENERS) {
Expand Down Expand Up @@ -622,7 +602,7 @@ private AbstractSpan pop() {
/**
* Add a new Span at the top of 'ActiveSpanStack'
*
* @param span
* @param span the {@code span} to push
*/
private AbstractSpan push(AbstractSpan span) {
activeSpanStack.addLast(span);
Expand Down