Skip to content

Commit ac2bff4

Browse files
committed
Refactoring opentracing out of core code, moving context unit tests into their own test
1 parent b24cd30 commit ac2bff4

File tree

11 files changed

+557
-315
lines changed

11 files changed

+557
-315
lines changed

src/main/java/com/uber/cadence/context/OpenTracingContextPropagator.java

+43-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@
3131
import java.util.HashMap;
3232
import java.util.Iterator;
3333
import java.util.Map;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
3436
import org.slf4j.MDC;
3537

3638
/** Support for OpenTracing spans */
3739
public class OpenTracingContextPropagator implements ContextPropagator {
3840

41+
private static final Logger log = LoggerFactory.getLogger(OpenTracingContextPropagator.class);
42+
3943
private static ThreadLocal<SpanContext> currentOpenTracingSpanContext = new ThreadLocal<>();
4044
private static ThreadLocal<Span> currentOpenTracingSpan = new ThreadLocal<>();
4145
private static ThreadLocal<Scope> currentOpenTracingScope = new ThreadLocal<>();
@@ -59,8 +63,10 @@ public String getName() {
5963
public Map<String, byte[]> serializeContext(Object context) {
6064
Map<String, byte[]> serializedContext = new HashMap<>();
6165
Map<String, String> contextMap = (Map<String, String>) context;
62-
for (Map.Entry<String, String> entry : contextMap.entrySet()) {
63-
serializedContext.put(entry.getKey(), entry.getValue().getBytes(Charset.defaultCharset()));
66+
if (contextMap != null) {
67+
for (Map.Entry<String, String> entry : contextMap.entrySet()) {
68+
serializedContext.put(entry.getKey(), entry.getValue().getBytes(Charset.defaultCharset()));
69+
}
6470
}
6571
return serializedContext;
6672
}
@@ -76,11 +82,14 @@ public Object deserializeContext(Map<String, byte[]> context) {
7682

7783
@Override
7884
public Object getCurrentContext() {
85+
log.debug("Getting current context");
7986
Tracer currentTracer = GlobalTracer.get();
8087
Span currentSpan = currentTracer.scopeManager().activeSpan();
8188
if (currentSpan != null) {
8289
HashMapTextMap contextTextMap = new HashMapTextMap();
8390
currentTracer.inject(currentSpan.context(), Format.Builtin.TEXT_MAP, contextTextMap);
91+
log.debug(
92+
"Retrieving current span data as current context: " + contextTextMap.getBackingMap());
8493
return contextTextMap.getBackingMap();
8594
} else {
8695
return null;
@@ -89,9 +98,11 @@ public Object getCurrentContext() {
8998

9099
@Override
91100
public void setCurrentContext(Object context) {
101+
log.debug("Setting current context");
92102
Tracer currentTracer = GlobalTracer.get();
93103
Map<String, String> contextAsMap = (Map<String, String>) context;
94104
if (contextAsMap != null) {
105+
log.debug("setting current context to " + contextAsMap);
95106
HashMapTextMap contextTextMap = new HashMapTextMap(contextAsMap);
96107
setCurrentOpenTracingSpanContext(
97108
currentTracer.extract(Format.Builtin.TEXT_MAP, contextTextMap));
@@ -100,6 +111,7 @@ public void setCurrentContext(Object context) {
100111

101112
@Override
102113
public void setUp() {
114+
log.debug("Starting a new opentracing span");
103115
Tracer openTracingTracer = GlobalTracer.get();
104116
Tracer.SpanBuilder builder =
105117
openTracingTracer
@@ -111,6 +123,7 @@ public void setUp() {
111123
}
112124

113125
Span span = builder.start();
126+
log.debug("New span: " + span);
114127
openTracingTracer.activateSpan(span);
115128
currentOpenTracingSpan.set(span);
116129
Scope scope = openTracingTracer.activateSpan(span);
@@ -134,8 +147,36 @@ public void onError(Throwable t) {
134147
public void finish(boolean successful) {
135148
Scope currentScope = currentOpenTracingScope.get();
136149
Span currentSpan = currentOpenTracingSpan.get();
150+
151+
log.debug("Closing currently open span " + currentSpan.context().toSpanId());
137152
currentScope.close();
138153
currentSpan.finish();
154+
currentOpenTracingScope.remove();
155+
currentOpenTracingSpan.remove();
156+
currentOpenTracingSpanContext.remove();
157+
}
158+
159+
/** Just check for other instances of the same class */
160+
@Override
161+
public boolean equals(Object obj) {
162+
if (obj == null) {
163+
return false;
164+
}
165+
166+
if (this == obj) {
167+
return true;
168+
}
169+
170+
if (this.getClass().equals(obj.getClass())) {
171+
return true;
172+
}
173+
174+
return false;
175+
}
176+
177+
@Override
178+
public int hashCode() {
179+
return this.getClass().hashCode();
139180
}
140181

141182
private class HashMapTextMap implements TextMap {

src/main/java/com/uber/cadence/internal/context/ContextThreadLocal.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static void onErrorContextPropagators(Throwable t) {
106106
}
107107

108108
/**
109-
* Calls {@link ContextPropagator#finish(boolean))} for each propagator
109+
* Calls {@link ContextPropagator#finish(boolean)} for each propagator
110110
*
111111
* @param successful True if the workflow/activity completed without unhandled exception, false
112112
* otherwise

src/main/java/com/uber/cadence/internal/replay/WorkflowContext.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.HashMap;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.stream.Collectors;
2627

2728
final class WorkflowContext {
2829

@@ -166,7 +167,17 @@ Map<String, Object> getPropagatedContexts() {
166167

167168
Map<String, Object> contextData = new HashMap<>();
168169
for (ContextPropagator propagator : contextPropagators) {
169-
contextData.put(propagator.getName(), propagator.deserializeContext(headerData));
170+
// Only send the context propagator the fields that belong to them
171+
// Change the map from MyPropagator:foo -> bar to foo -> bar
172+
Map<String, byte[]> filteredData =
173+
headerData
174+
.entrySet()
175+
.stream()
176+
.filter(e -> e.getKey().startsWith(propagator.getName()))
177+
.collect(
178+
Collectors.toMap(
179+
e -> e.getKey().substring(e.getKey().indexOf(":") + 1), Map.Entry::getValue));
180+
contextData.put(propagator.getName(), propagator.deserializeContext(filteredData));
170181
}
171182

172183
return contextData;

src/main/java/com/uber/cadence/internal/sync/SyncDecisionContext.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import java.util.function.Consumer;
7373
import java.util.function.Function;
7474
import java.util.function.Supplier;
75+
import java.util.stream.Collectors;
7576
import org.slf4j.Logger;
7677
import org.slf4j.LoggerFactory;
7778

@@ -449,7 +450,18 @@ private Map<String, byte[]> extractContextsAndConvertToBytes(
449450
}
450451
Map<String, byte[]> result = new HashMap<>();
451452
for (ContextPropagator propagator : contextPropagators) {
452-
result.putAll(propagator.serializeContext(propagator.getCurrentContext()));
453+
// Get the serialized context from the propagator
454+
Map<String, byte[]> serializedContext =
455+
propagator.serializeContext(propagator.getCurrentContext());
456+
// Namespace each entry in case of overlaps, so foo -> bar becomes MyPropagator:foo -> bar
457+
Map<String, byte[]> namespacedSerializedContext =
458+
serializedContext
459+
.entrySet()
460+
.stream()
461+
.collect(
462+
Collectors.toMap(
463+
e -> propagator.getName() + ":" + e.getKey(), Map.Entry::getValue));
464+
result.putAll(namespacedSerializedContext);
453465
}
454466
return result;
455467
}

src/main/java/com/uber/cadence/internal/sync/WorkflowStubImpl.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.concurrent.TimeUnit;
5959
import java.util.concurrent.TimeoutException;
6060
import java.util.concurrent.atomic.AtomicReference;
61+
import java.util.stream.Collectors;
6162

6263
class WorkflowStubImpl implements WorkflowStub {
6364

@@ -188,7 +189,18 @@ private Map<String, byte[]> extractContextsAndConvertToBytes(
188189
}
189190
Map<String, byte[]> result = new HashMap<>();
190191
for (ContextPropagator propagator : contextPropagators) {
191-
result.putAll(propagator.serializeContext(propagator.getCurrentContext()));
192+
// Get the serialized context from the propagator
193+
Map<String, byte[]> serializedContext =
194+
propagator.serializeContext(propagator.getCurrentContext());
195+
// Namespace each entry in case of overlaps, so foo -> bar becomes MyPropagator:foo -> bar
196+
Map<String, byte[]> namespacedSerializedContext =
197+
serializedContext
198+
.entrySet()
199+
.stream()
200+
.collect(
201+
Collectors.toMap(
202+
k -> propagator.getName() + ":" + k.getKey(), Map.Entry::getValue));
203+
result.putAll(namespacedSerializedContext);
192204
}
193205
return result;
194206
}

src/main/java/com/uber/cadence/internal/worker/ActivityWorker.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.util.Objects;
5151
import java.util.concurrent.CancellationException;
5252
import java.util.concurrent.TimeUnit;
53+
import java.util.stream.Collectors;
5354
import org.apache.thrift.TException;
5455
import org.slf4j.MDC;
5556

@@ -252,7 +253,18 @@ void propagateContext(PollForActivityTaskResponse response) {
252253
});
253254

254255
for (ContextPropagator propagator : options.getContextPropagators()) {
255-
propagator.setCurrentContext(propagator.deserializeContext(headerData));
256+
// Only send the context propagator the fields that belong to them
257+
// Change the map from MyPropagator:foo -> bar to foo -> bar
258+
Map<String, byte[]> filteredData =
259+
headerData
260+
.entrySet()
261+
.stream()
262+
.filter(e -> e.getKey().startsWith(propagator.getName()))
263+
.collect(
264+
Collectors.toMap(
265+
e -> e.getKey().substring(e.getKey().indexOf(":") + 1),
266+
Map.Entry::getValue));
267+
propagator.setCurrentContext(propagator.deserializeContext(filteredData));
256268
}
257269
}
258270

src/main/java/com/uber/cadence/internal/worker/LocalActivityWorker.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.uber.cadence.MarkerRecordedEventAttributes;
2323
import com.uber.cadence.PollForActivityTaskResponse;
2424
import com.uber.cadence.common.RetryOptions;
25+
import com.uber.cadence.context.ContextPropagator;
2526
import com.uber.cadence.internal.common.LocalActivityMarkerData;
2627
import com.uber.cadence.internal.metrics.MetricsTag;
2728
import com.uber.cadence.internal.metrics.MetricsType;
@@ -38,6 +39,7 @@
3839
import java.util.function.BiFunction;
3940
import java.util.function.Consumer;
4041
import java.util.function.LongSupplier;
42+
import java.util.stream.Collectors;
4143

4244
public final class LocalActivityWorker implements SuspendableWorker {
4345

@@ -271,9 +273,18 @@ private void propagateContext(ExecuteLocalActivityParameters params) {
271273
}
272274

273275
private void restoreContext(Map<String, byte[]> context) {
274-
options
275-
.getContextPropagators()
276-
.forEach(
277-
propagator -> propagator.setCurrentContext(propagator.deserializeContext(context)));
276+
for (ContextPropagator propagator : options.getContextPropagators()) {
277+
// Only send the context propagator the fields that belong to them
278+
// Change the map from MyPropagator:foo -> bar to foo -> bar
279+
Map<String, byte[]> filteredData =
280+
context
281+
.entrySet()
282+
.stream()
283+
.filter(e -> e.getKey().startsWith(propagator.getName()))
284+
.collect(
285+
Collectors.toMap(
286+
e -> e.getKey().substring(e.getKey().indexOf(":") + 1), Map.Entry::getValue));
287+
propagator.setCurrentContext(propagator.deserializeContext(filteredData));
288+
}
278289
}
279290
}

src/main/java/com/uber/cadence/worker/Worker.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -958,8 +958,11 @@ private FactoryOptions(
958958
this.contextPropagators = new ArrayList<>();
959959
}
960960

961-
// Add the OpenTracing propagator
962-
this.contextPropagators.add(new OpenTracingContextPropagator());
961+
// Add the OpenTracing propagator if it's not already there
962+
OpenTracingContextPropagator openTracing = new OpenTracingContextPropagator();
963+
if (!this.contextPropagators.contains(openTracing)) {
964+
this.contextPropagators.add(openTracing);
965+
}
963966
}
964967
}
965968
}

0 commit comments

Comments
 (0)