From 0d0d98403433e870bbce44b62ce1f484d0da8207 Mon Sep 17 00:00:00 2001 From: wusheng Date: Fri, 17 Mar 2017 11:25:51 +0800 Subject: [PATCH] 1. Remove the primary ref. Only use ref to the parent segment. 2. Adjust globalTracerIds --- .../a/eye/skywalking/trace/TraceSegment.java | 80 +++++-------------- .../eye/skywalking/trace/TraceSegmentRef.java | 27 +++++++ .../src/main/proto/trace.proto | 7 +- .../skywalking/api/context/TracerContext.java | 14 +--- 4 files changed, 53 insertions(+), 75 deletions(-) diff --git a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegment.java b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegment.java index 02c7e2280..fade4273d 100644 --- a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegment.java +++ b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegment.java @@ -41,14 +41,8 @@ public class TraceSegment implements ISerializable { private long endTime; /** - * The primary ref of the parent trace segment. - * Use {@link TraceSegmentRef}, we can link this trace segment to the primary parent segment. - */ - private TraceSegmentRef primaryRef; - - /** - * The refs of other parent trace segments, except the primary one. - * For most RPC call, {@link #refs} stay in null, + * The refs of parent trace segments, except the primary one. + * For most RPC call, {@link #refs} contains only one element, * but if this segment is a start span of batch process, the segment faces multi parents, * at this moment, we use this {@link #refs} to link them. */ @@ -74,13 +68,13 @@ public class TraceSegment implements ISerializable { * element, because only one parent {@link TraceSegment} exists, but, in batch scenario, the num becomes greater * than 1, also meaning multi-parents {@link TraceSegment}. * - * The difference between relatedGlobalTraces and {@link #primaryRef}/{@link #refs} is: {@link - * #primaryRef}/{@link #refs} targets this {@link TraceSegment}'s direct parent, + * The difference between relatedGlobalTraces and {@link #refs} is: + * {@link #refs} targets this {@link TraceSegment}'s direct parent, * * and * * relatedGlobalTraces targets this {@link TraceSegment}'s related call chain, a call chain contains - * multi {@link TraceSegment}s, only using {@link #primaryRef}/{@link #refs} is enough for analysis and ui. + * multi {@link TraceSegment}s, only using {@link #refs} is not enough for analysis and ui. */ private LinkedList relatedGlobalTraces; @@ -108,48 +102,27 @@ public class TraceSegment implements ISerializable { /** * Establish the link between this segment and its parents. - * When {@param primaryOnly} is true; - * The first time, you {@link #ref(TraceSegmentRef)} to parent, it is affirmed as {@link #primaryRef}. - * And others are affirmed as {@link #refs}. - * - * @param refSegment {@link TraceSegmentRef} - * @param primaryOnly if true, set {@param refSegment} to {@link #primaryRef} only. - */ - public void ref(TraceSegmentRef refSegment, boolean primaryOnly) { - if (primaryOnly) { - if (primaryRef == null) { - primaryRef = refSegment; - } - } else { - if (primaryRef == null) { - primaryRef = refSegment; - } else { - if (refs == null) { - refs = new LinkedList(); - } - refs.add(refSegment); - } - } - } - - /** - * Set to {@link #primaryRef} only, - * based on {@link #ref(TraceSegmentRef, boolean)} * * @param refSegment {@link TraceSegmentRef} */ public void ref(TraceSegmentRef refSegment) { - ref(refSegment, true); + if(!refs.contains(refSegment)){ + refs.add(refSegment); + } } - public void buildRelation(List distributedTraceIds){ - if(distributedTraceIds == null || distributedTraceIds.size() == 0){ + public void relatedGlobalTraces(List distributedTraceIds) { + if (distributedTraceIds == null || distributedTraceIds.size() == 0) { return; } - if(relatedGlobalTraces.getFirst() instanceof NewDistributedTraceId){ + if (relatedGlobalTraces.getFirst() instanceof NewDistributedTraceId) { relatedGlobalTraces.removeFirst(); } - relatedGlobalTraces.addAll(distributedTraceIds); + for (DistributedTraceId distributedTraceId : distributedTraceIds) { + if(!relatedGlobalTraces.contains(distributedTraceId)){ + relatedGlobalTraces.add(distributedTraceId); + } + } } /** @@ -184,10 +157,6 @@ public class TraceSegment implements ISerializable { return endTime; } - public TraceSegmentRef getPrimaryRef() { - return primaryRef; - } - public List getRefs() { if (refs == null) { return null; @@ -207,13 +176,15 @@ public class TraceSegment implements ISerializable { return applicationCode; } - @Override - public String toString() { + @Override public String toString() { return "TraceSegment{" + "traceSegmentId='" + traceSegmentId + '\'' + + ", startTime=" + startTime + ", endTime=" + endTime + - ", primaryRef=" + primaryRef + - ", spans.size=" + spans.size() + + ", refs=" + refs + + ", spans=" + spans + + ", applicationCode='" + applicationCode + '\'' + + ", relatedGlobalTraces=" + relatedGlobalTraces + '}'; } @@ -224,9 +195,6 @@ public class TraceSegment implements ISerializable { segmentBuilder.setStartTime(startTime); segmentBuilder.setEndTime(endTime); segmentBuilder.setApplicationCode(applicationCode); - if (primaryRef != null) { - segmentBuilder.setPrimaryRef(primaryRef.serialize()); - } if (refs != null && refs.size() > 0) { for (TraceSegmentRef ref : refs) { segmentBuilder.addRefs(ref.serialize()); @@ -247,10 +215,6 @@ public class TraceSegment implements ISerializable { startTime = message.getStartTime(); endTime = message.getEndTime(); applicationCode = message.getApplicationCode(); - SegmentRefMessage messagePrimaryRef = message.getPrimaryRef(); - if (messagePrimaryRef != null) { - (primaryRef = new TraceSegmentRef()).deserialize(messagePrimaryRef); - } List refsList = message.getRefsList(); if (refsList != null && refsList.size() > 0) { this.refs = new LinkedList(); diff --git a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegmentRef.java b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegmentRef.java index 9cdcb2d07..7330b583c 100644 --- a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegmentRef.java +++ b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegmentRef.java @@ -97,4 +97,31 @@ public class TraceSegmentRef implements ISerializable { applicationCode = message.getApplicationCode(); peerHost = message.getPeerHost(); } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + + TraceSegmentRef ref = (TraceSegmentRef)o; + + if (spanId != ref.spanId) + return false; + if (traceSegmentId != null ? !traceSegmentId.equals(ref.traceSegmentId) : ref.traceSegmentId != null) + return false; + if (applicationCode != null ? !applicationCode.equals(ref.applicationCode) : ref.applicationCode != null) + return false; + return peerHost != null ? peerHost.equals(ref.peerHost) : ref.peerHost == null; + } + + @Override + public int hashCode() { + int result = traceSegmentId != null ? traceSegmentId.hashCode() : 0; + result = 31 * result + spanId; + result = 31 * result + (applicationCode != null ? applicationCode.hashCode() : 0); + result = 31 * result + (peerHost != null ? peerHost.hashCode() : 0); + return result; + } } diff --git a/skywalking-commons/skywalking-trace/src/main/proto/trace.proto b/skywalking-commons/skywalking-trace/src/main/proto/trace.proto index 56900dfac..0dad331cc 100644 --- a/skywalking-commons/skywalking-trace/src/main/proto/trace.proto +++ b/skywalking-commons/skywalking-trace/src/main/proto/trace.proto @@ -8,10 +8,9 @@ message SegmentMessage { int64 startTime = 2; int64 endTime = 3; string applicationCode = 4; - SegmentRefMessage primaryRef = 5; - repeated SegmentRefMessage refs = 6; - repeated string relatedTraceIds = 7; - repeated SpanMessage spans = 8; + repeated SegmentRefMessage refs = 5; + repeated string relatedTraceIds = 6; + repeated SpanMessage spans = 7; } message SegmentRefMessage { diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java index cc9684bc6..2eaeb38af 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java @@ -137,19 +137,7 @@ public final class TracerContext { public void extract(ContextCarrier carrier) { if(carrier.isValid()) { this.segment.ref(getRef(carrier)); - this.segment.buildRelation(carrier.getDistributedTraceIds()); - } - } - - /** - * Ref this {@link ContextCarrier} to this {@link TraceSegment}, and support multi-extract for supporting batch process, like MQ. - * - * @param carrier holds the snapshot, if get this {@link ContextCarrier} from remote, make sure {@link - * ContextCarrier#deserialize(String)} called. - */ - public void multiExtract(ContextCarrier carrier){ - if(carrier.isValid()) { - this.segment.ref(getRef(carrier), false); + this.segment.relatedGlobalTraces(carrier.getDistributedTraceIds()); } } -- GitLab