Skip to content

Commit 7ffc972

Browse files
Adrian Coleadriancole
Adrian Cole
authored andcommitted
Raises CorrectForClockSkew coverage to 100% in preparation of js port
1 parent 2c08e2b commit 7ffc972

File tree

2 files changed

+145
-98
lines changed

2 files changed

+145
-98
lines changed

zipkin/src/main/java/zipkin/internal/CorrectForClockSkew.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.logging.Logger;
2323
import zipkin.Annotation;
2424
import zipkin.BinaryAnnotation;
25-
import zipkin.Constants;
2625
import zipkin.Endpoint;
2726
import zipkin.Span;
2827
import zipkin2.internal.Node;
@@ -41,7 +40,7 @@ static class ClockSkew {
4140
final Endpoint endpoint;
4241
final long skew;
4342

44-
public ClockSkew(Endpoint endpoint, long skew) {
43+
ClockSkew(Endpoint endpoint, long skew) {
4544
this.endpoint = endpoint;
4645
this.skew = skew;
4746
}
@@ -115,38 +114,39 @@ static void adjust(Node<Span> node, @Nullable ClockSkew skewFromParent) {
115114
// Is there any skew in the current span?
116115
ClockSkew skew = getClockSkew(node.value());
117116
if (skew != null) {
118-
// the current span's skew may be a different endpoint than skewFromParent, adjust again.
117+
// the current span's skew may be a different endpoint than its parent, so adjust again.
119118
node.value(adjustTimestamps(node.value(), skew));
120-
} else {
121-
if (skewFromParent != null && isLocalSpan(node.value())) {
122-
//Propagate skewFromParent to local spans
123-
skew = skewFromParent;
124-
}
119+
} else if (skewFromParent != null && isSingleHostSpan(node.value())) {
120+
// Assumes we are on the same host: propagate skew from our parent
121+
skew = skewFromParent;
125122
}
126123
// propagate skew to any children
127124
for (Node<Span> child : node.children()) {
128125
adjust(child, skew);
129126
}
130127
}
131128

132-
static boolean isLocalSpan(Span span) {
133-
Endpoint endPoint = null;
129+
static boolean isSingleHostSpan(Span span) {
130+
Endpoint endpoint = null;
134131
for (int i = 0, length = span.annotations.size(); i < length; i++) {
135132
Annotation annotation = span.annotations.get(i);
136-
if (endPoint == null) {
137-
endPoint = annotation.endpoint;
133+
if (endpoint == null) {
134+
endpoint = annotation.endpoint;
135+
continue;
138136
}
139-
if (endPoint != null && !endPoint.equals(annotation.endpoint)) {
140-
return false;
137+
if (!endpoint.equals(annotation.endpoint)) {
138+
return false; // there's a mix of endpoints in this span
141139
}
142140
}
143141
for (int i = 0, length = span.binaryAnnotations.size(); i < length; i++) {
144142
BinaryAnnotation binaryAnnotation = span.binaryAnnotations.get(i);
145-
if (endPoint == null) {
146-
endPoint = binaryAnnotation.endpoint;
143+
if (binaryAnnotation.type != BinaryAnnotation.Type.STRING) continue;
144+
if (endpoint == null) {
145+
endpoint = binaryAnnotation.endpoint;
146+
continue;
147147
}
148-
if (endPoint != null && !endPoint.equals(binaryAnnotation.endpoint)) {
149-
return false;
148+
if (!endpoint.equals(binaryAnnotation.endpoint)) {
149+
return false; // there's a mix of endpoints in this span
150150
}
151151
}
152152
return true;
@@ -179,7 +179,7 @@ static Span adjustTimestamps(Span span, ClockSkew skew) {
179179
for (int i = 0, length = span.binaryAnnotations.size(); i < length; i++) {
180180
BinaryAnnotation b = span.binaryAnnotations.get(i);
181181
if (b.endpoint == null) continue;
182-
if (b.key.equals(Constants.LOCAL_COMPONENT) && ipsMatch(skew.endpoint, b.endpoint)) {
182+
if (b.key.equals("lc") && ipsMatch(skew.endpoint, b.endpoint)) {
183183
return span.toBuilder().timestamp(spanTimestamp - skew.skew).build();
184184
}
185185
}
@@ -190,21 +190,19 @@ static boolean ipsMatch(Endpoint skew, Endpoint that) {
190190
if (skew.ipv6 != null && that.ipv6 != null) {
191191
if (Arrays.equals(skew.ipv6, that.ipv6)) return true;
192192
}
193-
if (skew.ipv4 != 0 && that.ipv4 != 0 ) {
194-
if (skew.ipv4 == that.ipv4) return true;
195-
}
196-
return false;
193+
if (skew.ipv4 == 0 && that.ipv4 == 0) return false;
194+
return skew.ipv4 == that.ipv4;
197195
}
198196

199197
/** Use client/server annotations to determine if there's clock skew. */
200198
@Nullable
201199
static ClockSkew getClockSkew(Span span) {
202200
Map<String, Annotation> annotations = asMap(span.annotations);
203201

204-
Annotation clientSend = annotations.get(Constants.CLIENT_SEND);
205-
Annotation clientRecv = annotations.get(Constants.CLIENT_RECV);
206-
Annotation serverRecv = annotations.get(Constants.SERVER_RECV);
207-
Annotation serverSend = annotations.get(Constants.SERVER_SEND);
202+
Annotation clientSend = annotations.get("cs");
203+
Annotation clientRecv = annotations.get("cr");
204+
Annotation serverRecv = annotations.get("sr");
205+
Annotation serverSend = annotations.get("ss");
208206

209207
boolean oneWay = false;
210208
if (clientSend == null || serverRecv == null) {
@@ -247,7 +245,6 @@ static ClockSkew getClockSkew(Span span) {
247245
}
248246
}
249247

250-
251248
return null;
252249
}
253250

0 commit comments

Comments
 (0)