Skip to content

Commit

Permalink
[#9595] Refactor AsyncTrace
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jan 17, 2023
1 parent 1ffb662 commit e09141c
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 216 deletions.
Expand Up @@ -58,4 +58,15 @@ public static boolean hasScope(final Trace trace, final String scopeName) {
final TraceScope scope = trace.getScope(scopeName);
return scope != null;
}

public static boolean addScope(Trace trace, String scopeName) {
// add async scope.
final TraceScope oldScope = trace.addScope(scopeName);
if (oldScope != null) {
// delete corrupted trace.
// deleteAsyncTrace(trace);
return false;
}
return true;
}
}
Expand Up @@ -18,6 +18,7 @@

import com.navercorp.pinpoint.bootstrap.context.AsyncContext;
import com.navercorp.pinpoint.bootstrap.context.AsyncState;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;

/**
Expand All @@ -31,4 +32,5 @@ public interface AsyncContextFactory {

AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, boolean canSampled, AsyncState asyncState);

AsyncContext newDisableAsyncContext(LocalTraceRoot traceRoot);
}
@@ -0,0 +1,31 @@
package com.navercorp.pinpoint.profiler.context;

import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public final class AsyncScopeUtils {
private static final Logger logger = LogManager.getLogger(AsyncScopeUtils.class);

private AsyncScopeUtils() {
}

public static boolean nested(Trace trace, String scopeName) {
// add async scope.
final TraceScope oldScope = trace.addScope(scopeName);
if (oldScope != null) {
if (logger.isWarnEnabled()) {
logger.warn("Duplicated {} scope={}", trace.getClass().getSimpleName(), oldScope.getName());
}
// delete corrupted trace.
// deleteAsyncTrace(trace);
return true;
} else {
if (logger.isDebugEnabled()) {
logger.debug("start {} scope", trace.getClass().getSimpleName());
}
}
return false;
}
}
Expand Up @@ -17,6 +17,7 @@
package com.navercorp.pinpoint.profiler.context;

import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;


Expand All @@ -25,17 +26,8 @@
*/
public interface AsyncTraceContext {

// Reference<Trace> continueAsyncTraceObject(TraceRoot traceRoot, int asyncId, short asyncSequence);

Reference<Trace> continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled);

Trace newAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled);

Reference<Trace> currentRawTraceObject();

Reference<Trace> currentTraceObject();

void removeTraceObject();
Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId);

Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot);

}
Expand Up @@ -19,6 +19,7 @@
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.common.annotations.InterfaceAudience;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;

/**
Expand All @@ -34,7 +35,9 @@ public interface BaseTraceFactory {
@InterfaceAudience.LimitedPrivate("vert.x")
Trace continueAsyncTraceObject(TraceId traceId);

Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled);
Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId);

Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot);

Trace newTraceObject();

Expand Down
Expand Up @@ -19,12 +19,11 @@
import com.navercorp.pinpoint.bootstrap.context.AsyncContext;
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Objects;

Expand All @@ -38,21 +37,23 @@ public class DefaultAsyncContext implements AsyncContext {

private final TraceRoot traceRoot;
private final AsyncId asyncId;
private final boolean canSampled;

private final AsyncTraceContext asyncTraceContext;
private final Binder<Trace> binder;

private final int asyncMethodApiId;


public DefaultAsyncContext(AsyncTraceContext asyncTraceContext, TraceRoot traceRoot, AsyncId asyncId, int asyncMethodApiId, boolean canSampled) {
public DefaultAsyncContext(AsyncTraceContext asyncTraceContext,
Binder<Trace> binder,
TraceRoot traceRoot,
AsyncId asyncId, int asyncMethodApiId) {
this.asyncTraceContext = Objects.requireNonNull(asyncTraceContext, "asyncTraceContext");
this.binder = Objects.requireNonNull(binder, "binder");
this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot");
this.asyncId = Objects.requireNonNull(asyncId, "asyncId");


this.asyncMethodApiId = asyncMethodApiId;
this.canSampled = canSampled;
}


Expand All @@ -63,7 +64,7 @@ public TraceRoot getTraceRoot() {
@Override
public Trace continueAsyncTraceObject() {

final Reference<Trace> reference = asyncTraceContext.currentRawTraceObject();
final Reference<Trace> reference = binder.get();
final Trace nestedTrace = reference.get();
if (nestedTrace != null) {
// return Nested Trace Object?
Expand All @@ -80,42 +81,26 @@ private Trace newAsyncContextTrace(Reference<Trace> reference) {
// final int asyncId = this.asyncId.getAsyncId();
// final short asyncSequence = this.asyncId.nextAsyncSequence();
final LocalAsyncId localAsyncId = this.asyncId.nextLocalAsyncId();
final Trace asyncTrace = asyncTraceContext.newAsyncContextTraceObject(traceRoot, localAsyncId, canSampled);

final Trace asyncTrace = asyncTraceContext.continueAsyncContextTraceObject(traceRoot, localAsyncId);

bind(reference, asyncTrace);

if (logger.isDebugEnabled()) {
logger.debug("asyncTraceContext.continueAsyncTraceObject() AsyncTrace:{}", asyncTrace);
logger.debug("asyncTraceContext.continuAsyncTraceObject(e) AsyncTrace:{}", asyncTrace);
}

// add async scope.
final TraceScope oldScope = asyncTrace.addScope(ASYNC_TRACE_SCOPE);
if (oldScope != null) {
if (logger.isWarnEnabled()) {
logger.warn("Duplicated async trace scope={}.", oldScope.getName());
}
// delete corrupted trace.
// deleteAsyncTrace(trace);
if (AsyncScopeUtils.nested(asyncTrace, ASYNC_TRACE_SCOPE)) {
return null;
} else {
if (logger.isDebugEnabled()) {
logger.debug("start async trace scope");
}
}

// first block.
final SpanEventRecorder recorder = asyncTrace.currentSpanEventRecorder();

if (recorder != null) {
recorder.recordServiceType(ServiceType.ASYNC);
recorder.recordApiId(asyncMethodApiId);
}

if (asyncTrace.canSampled()) {
return asyncTrace;
}
return null;
return asyncTrace;
}

private void bind(Reference<Trace> reference, Trace asyncTrace) {
Expand All @@ -127,14 +112,21 @@ private void bind(Reference<Trace> reference, Trace asyncTrace) {

@Override
public Trace currentAsyncTraceObject() {
final Reference<Trace> reference = asyncTraceContext.currentTraceObject();
return reference.get();
final Reference<Trace> reference = binder.get();
final Trace trace = reference.get();
if (trace == null) {
return null;
}
if (trace.canSampled()) {
return trace;
}
return null;
}


@Override
public void close() {
asyncTraceContext.removeTraceObject();
binder.remove();
}

@Override
Expand Down
Expand Up @@ -19,7 +19,9 @@
import com.navercorp.pinpoint.bootstrap.context.AsyncContext;
import com.navercorp.pinpoint.bootstrap.context.AsyncState;
import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.profiler.context.id.AsyncIdGenerator;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import com.navercorp.pinpoint.profiler.context.method.PredefinedMethodDescriptorRegistry;

Expand All @@ -31,13 +33,18 @@
public class DefaultAsyncContextFactory implements AsyncContextFactory {

private final AsyncTraceContext asyncTraceContext;
private final Binder<Trace> binder;
private final AsyncIdGenerator asyncIdGenerator;
private final PredefinedMethodDescriptorRegistry predefinedMethodDescriptorRegistry;
private final int asyncMethodApiId;

public DefaultAsyncContextFactory(AsyncTraceContext asyncTraceContext, AsyncIdGenerator asyncIdGenerator, PredefinedMethodDescriptorRegistry predefinedMethodDescriptorRegistry) {
public DefaultAsyncContextFactory(AsyncTraceContext asyncTraceContext,
Binder<Trace> binder,
AsyncIdGenerator asyncIdGenerator,
PredefinedMethodDescriptorRegistry predefinedMethodDescriptorRegistry) {
this.asyncTraceContext = Objects.requireNonNull(asyncTraceContext, "traceFactoryProvider");
this.asyncIdGenerator = Objects.requireNonNull(asyncIdGenerator, "asyncIdGenerator");
this.binder = Objects.requireNonNull(binder, "binder");

this.predefinedMethodDescriptorRegistry = Objects.requireNonNull(predefinedMethodDescriptorRegistry, "predefinedMethodDescriptorRegistry");

Expand All @@ -59,7 +66,11 @@ public AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, boolea
Objects.requireNonNull(traceRoot, "traceRoot");
Objects.requireNonNull(asyncId, "asyncId");

return new DefaultAsyncContext(asyncTraceContext, traceRoot, asyncId, this.asyncMethodApiId, canSampled);
if (canSampled) {
return new DefaultAsyncContext(asyncTraceContext, binder, traceRoot, asyncId, this.asyncMethodApiId);
} else {
return newDisableAsyncContext(traceRoot);
}
}

@Override
Expand All @@ -68,8 +79,18 @@ public AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, boolea
Objects.requireNonNull(asyncId, "asyncId");
Objects.requireNonNull(asyncState, "asyncState");

return new StatefulAsyncContext(asyncTraceContext, traceRoot, asyncId, asyncMethodApiId, asyncState, canSampled);
if (canSampled) {
return new StatefulAsyncContext(asyncTraceContext, binder, traceRoot, asyncId, asyncMethodApiId, asyncState);
} else {
// TODO
return new StatefulDisableAsyncContext(asyncTraceContext, binder, traceRoot, asyncState);
}

}

@Override
public AsyncContext newDisableAsyncContext(LocalTraceRoot traceRoot) {
return new DisableAsyncContext(asyncTraceContext, binder, traceRoot);
}

}
Expand Up @@ -18,87 +18,34 @@

import com.google.inject.Provider;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import java.util.Objects;
import com.navercorp.pinpoint.exception.PinpointException;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.util.Objects;


/**
* @author Woonduk Kang(emeroad)
*/
public class DefaultAsyncTraceContext implements AsyncTraceContext {

private final Logger logger = LogManager.getLogger(this.getClass());

private static final Reference<Trace> EMPTY = DefaultReference.emptyReference();

private final Provider<BaseTraceFactory> baseTraceFactoryProvider;
private final Binder<Trace> binder;

public DefaultAsyncTraceContext(Provider<BaseTraceFactory> baseTraceFactoryProvider, Binder<Trace> binder) {
public DefaultAsyncTraceContext(Provider<BaseTraceFactory> baseTraceFactoryProvider) {
this.baseTraceFactoryProvider = Objects.requireNonNull(baseTraceFactoryProvider, "baseTraceFactoryProvider");
this.binder = Objects.requireNonNull(binder, "binder");
}

@Override
public Reference<Trace> continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled) {
final Reference<Trace> reference = checkAndGet();

final Trace trace = newAsyncContextTraceObject(traceRoot, localAsyncId, canSampled);

bind(reference, trace);
return reference;
}

@Override
public Trace newAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled) {
public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId) {
final BaseTraceFactory baseTraceFactory = baseTraceFactoryProvider.get();
return baseTraceFactory.continueAsyncContextTraceObject(traceRoot, localAsyncId, canSampled);
return baseTraceFactory.continueAsyncContextTraceObject(traceRoot, localAsyncId);
}


@Override
public Reference<Trace> currentRawTraceObject() {
return binder.get();
}

@Override
public Reference<Trace> currentTraceObject() {
final Reference<Trace> reference = binder.get();
final Trace trace = reference.get();
if (trace == null) {
return EMPTY;
}
if (trace.canSampled()) {
return reference;
}
return EMPTY;
}


@Override
public void removeTraceObject() {
binder.remove();
}


private Reference<Trace> checkAndGet() {
final Reference<Trace> reference = this.binder.get();
final Trace old = reference.get();
if (old != null) {
final PinpointException exception = new PinpointException("already Trace Object exist.");
if (logger.isWarnEnabled()) {
logger.warn("beforeTrace:{}", old, exception);
}
throw exception;
}
return reference;
public Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot) {
final BaseTraceFactory baseTraceFactory = baseTraceFactoryProvider.get();
return baseTraceFactory.continueDisableAsyncContextTraceObject(traceRoot);
}

private void bind(Reference<Trace> reference, Trace trace) {
reference.set(trace);
}

}

0 comments on commit e09141c

Please sign in to comment.