Skip to content

Commit

Permalink
[pinpoint-apm#2584] Refactor AsyncTrace
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Oct 18, 2017
1 parent b687382 commit 0cbc282
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright 2017 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.profiler.context;

import com.navercorp.pinpoint.bootstrap.context.*;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;

public class AsyncChildTrace implements Trace {

private static final int BEGIN_STACKID = 1;

private final AsyncContextFactory asyncContextFactory;

private final TraceRoot traceRoot;
private final DefaultTrace trace;

private final int asyncId;
private final short asyncSequence;

public AsyncChildTrace(final AsyncContextFactory asyncContextFactory, final TraceRoot traceRoot, final DefaultTrace trace, final int asyncId, final short asyncSequence) {
this.asyncContextFactory = Assert.requireNonNull(asyncContextFactory, "asyncContextFactory must not be null");
this.traceRoot = Assert.requireNonNull(traceRoot, "traceRoot must not be null");
this.trace = Assert.requireNonNull(trace, "trace must not be null");
this.asyncId = asyncId;
this.asyncSequence = asyncSequence;

traceBlockBegin(BEGIN_STACKID);
}


@Override
public long getId() {
return traceRoot.getLocalTransactionId();
}

@Override
public long getStartTime() {
return this.traceRoot.getTraceStartTime();
}

@Override
public Thread getBindThread() {
return null;
}

@Override
public long getThreadId() {
return -1;
}

@Override
public TraceId getTraceId() {
return this.traceRoot.getTraceId();
}

@Override
public boolean canSampled() {
return trace.canSampled();
}

@Override
public boolean isRoot() {
return this.traceRoot.getTraceId().isRoot();
}

@Override
public SpanEventRecorder traceBlockBegin() {
final SpanEventRecorder recorder = trace.traceBlockBegin();
recorder.recordAsyncId(asyncId);
recorder.recordAsyncSequence(asyncSequence);
return recorder;
}

@Override
public SpanEventRecorder traceBlockBegin(int stackId) {
final SpanEventRecorder recorder = trace.traceBlockBegin(stackId);
recorder.recordAsyncId(asyncId);
recorder.recordAsyncSequence(asyncSequence);
return recorder;
}

@Override
public void traceBlockEnd() {
trace.traceBlockEnd();
}

@Override
public void traceBlockEnd(int stackId) {
trace.traceBlockEnd(stackId);
}

@Override
public boolean isAsync() {
return true;
}

@Override
public boolean isRootStack() {
return trace.getCallStackFrameId() == BEGIN_STACKID;
}

/**
* @deprecated Since 1.7.0 Use {@link SpanEventRecorder#recordNextAsyncContext()}
* This API will be removed in 1.8.0
*/
@Deprecated
@Override
public AsyncTraceId getAsyncTraceId() {
return asyncContextFactory.newAsyncTraceId(traceRoot);
}

@Override
public boolean isClosed() {
return this.trace.isClosed();
}

@Override
public void close() {
traceBlockEnd(BEGIN_STACKID);
trace.close();
}


@Override
public SpanRecorder getSpanRecorder() {
return trace.getSpanRecorder();
}

@Override
public SpanEventRecorder currentSpanEventRecorder() {
return trace.currentSpanEventRecorder();
}

@Override
public int getCallStackFrameId() {
return trace.getCallStackFrameId();
}

@Override
public TraceScope getScope(String name) {
return trace.getScope(name);
}

@Override
public TraceScope addScope(String name) {
return trace.addScope(name);
}

@Override
public String toString() {
return "AsyncChildTrace{" +
"traceRoot=" + traceRoot +
", trace=" + trace +
", asyncId=" + asyncId +
", asyncSequence=" + asyncSequence +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.slf4j.LoggerFactory;

public class AsyncTrace implements Trace {
private static final int BEGIN_STACKID = 1;

private static final Logger logger = LoggerFactory.getLogger(AsyncTrace.class.getName());
private static final boolean isDebug = logger.isDebugEnabled();
Expand All @@ -32,53 +31,26 @@ public class AsyncTrace implements Trace {

private final TraceRoot traceRoot;
private final DefaultTrace trace;
private final boolean entryPoint;

private int asyncId;
private short asyncSequence;
private AsyncState asyncState;
private final AsyncState asyncState;

public AsyncTrace(final AsyncContextFactory asyncContextFactory, final TraceRoot traceRoot, final DefaultTrace trace, final AsyncState asyncState) {
this.asyncContextFactory = Assert.requireNonNull(asyncContextFactory, "asyncContextFactory must not be null");
this.traceRoot = Assert.requireNonNull(traceRoot, "traceRoot must not be null");
this.trace = Assert.requireNonNull(trace, "trace must not be null");
this.asyncState = Assert.requireNonNull(asyncState, "asyncState must not be null");
this.entryPoint = true;
}

public AsyncTrace(final AsyncContextFactory asyncContextFactory, final TraceRoot traceRoot, final DefaultTrace trace, final int asyncId, final short asyncSequence) {
this.asyncContextFactory = Assert.requireNonNull(asyncContextFactory, "asyncContextFactory must not be null");
this.traceRoot = Assert.requireNonNull(traceRoot, "traceRoot must not be null");
this.trace = Assert.requireNonNull(trace, "trace must not be null");
this.asyncId = asyncId;
this.asyncSequence = asyncSequence;

this.asyncState = null;
this.entryPoint = false;

traceBlockBegin(BEGIN_STACKID);
}

public int getAsyncId() {
return asyncId;
}

@Override
public long getId() {
if (this.entryPoint) {
return traceRoot.getLocalTransactionId();
}
return traceRoot.getLocalTransactionId();

return -1;
}

@Override
public long getStartTime() {
if (this.entryPoint) {
return this.traceRoot.getTraceStartTime();
}

return 0;
return this.traceRoot.getTraceStartTime();
}

@Override
Expand All @@ -88,11 +60,8 @@ public Thread getBindThread() {

@Override
public long getThreadId() {
if (this.entryPoint) {
return this.traceRoot.getShared().getThreadId();
}
return this.traceRoot.getShared().getThreadId();

return -1;
}

@Override
Expand All @@ -112,24 +81,12 @@ public boolean isRoot() {

@Override
public SpanEventRecorder traceBlockBegin() {
final SpanEventRecorder recorder = trace.traceBlockBegin();
if (this.entryPoint) {
return recorder;
}
recorder.recordAsyncId(asyncId);
recorder.recordAsyncSequence(asyncSequence);
return recorder;
return trace.traceBlockBegin();
}

@Override
public SpanEventRecorder traceBlockBegin(int stackId) {
final SpanEventRecorder recorder = trace.traceBlockBegin(stackId);
if (this.entryPoint) {
return recorder;
}
recorder.recordAsyncId(asyncId);
recorder.recordAsyncSequence(asyncSequence);
return recorder;
return trace.traceBlockBegin(stackId);
}

@Override
Expand All @@ -144,18 +101,12 @@ public void traceBlockEnd(int stackId) {

@Override
public boolean isAsync() {
if (this.entryPoint) {
return false;
}
return true;
return false;
}

@Override
public boolean isRootStack() {
if (this.entryPoint) {
return this.trace.isRootStack();
}
return trace.getCallStackFrameId() == BEGIN_STACKID;
return this.trace.isRootStack();
}

/**
Expand All @@ -175,15 +126,6 @@ public boolean isClosed() {

@Override
public void close() {
if (this.entryPoint) {
closeOrFlush();
} else {
traceBlockEnd(BEGIN_STACKID);
trace.close();
}
}

private void closeOrFlush() {
final AsyncState asyncState = this.asyncState;
if (asyncState == null) {
return;
Expand Down Expand Up @@ -236,9 +178,6 @@ public String toString() {
return "AsyncTrace{" +
"traceRoot=" + traceRoot +
", trace=" + trace +
", entryPoint=" + entryPoint +
", asyncId=" + asyncId +
", asyncSequence=" + asyncSequence +
", asyncState=" + asyncState +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public Trace continueAsyncTraceObject(TraceRoot traceRoot, int asyncId, short as
// TODO AtomicIdGenerator.UNTRACKED_ID
final DefaultTrace trace = new DefaultTrace(span, callStack, asyncStorage, asyncContextFactory, samplingEnable, spanRecorder, wrappedSpanEventRecorder, ActiveTraceHandle.EMPTY_HANDLE);

final Trace asyncTrace = new AsyncTrace(asyncContextFactory, traceRoot, trace, asyncId, asyncSequence);
final Trace asyncTrace = new AsyncChildTrace(asyncContextFactory, traceRoot, trace, asyncId, asyncSequence);

return asyncTrace;
}
Expand Down

0 comments on commit 0cbc282

Please sign in to comment.