Skip to content

Commit 292b283

Browse files
committed
Instrumentation support for dataloader - updated ScheduledDataLoaderRegistry
1 parent 3a8adcd commit 292b283

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

src/main/java/org/dataloader/DataLoaderRegistry.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,45 @@
1919
/**
2020
* This allows data loaders to be registered together into a single place, so
2121
* they can be dispatched as one. It also allows you to retrieve data loaders by
22-
* name from a central place
22+
* name from a central place.
23+
* <p>
24+
* Notes on {@link DataLoaderInstrumentation} : A {@link DataLoaderRegistry} can have an instrumentation
25+
* associated with it. As each {@link DataLoader} is added to the registry, the {@link DataLoaderInstrumentation}
26+
* of the registry is applied to that {@link DataLoader}.
27+
* <p>
28+
* The {@link DataLoader} is changed and hence the object in the registry is not the
29+
* same one as was originally registered. So you MUST get access to the {@link DataLoader} via {@link DataLoaderRegistry#getDataLoader(String)} methods
30+
* and not use the original {@link DataLoader} object.
31+
* <p>
32+
* If the {@link DataLoader} has no {@link DataLoaderInstrumentation} then the registry one is added to it. If it does have one already
33+
* then a {@link ChainedDataLoaderInstrumentation} is created with the registry {@link DataLoaderInstrumentation} in it first and then any other
34+
* {@link DataLoaderInstrumentation}s added after that.
2335
*/
2436
@PublicApi
2537
public class DataLoaderRegistry {
26-
protected final Map<String, DataLoader<?, ?>> dataLoaders = new ConcurrentHashMap<>();
38+
protected final Map<String, DataLoader<?, ?>> dataLoaders;
2739
protected final DataLoaderInstrumentation instrumentation;
2840

2941

3042
public DataLoaderRegistry() {
31-
instrumentation = null;
43+
this(new ConcurrentHashMap<>(),null);
3244
}
3345

3446
private DataLoaderRegistry(Builder builder) {
35-
instrument(builder.instrumentation, builder.dataLoaders);
36-
this.instrumentation = builder.instrumentation;
47+
this(builder.dataLoaders,builder.instrumentation);
3748
}
3849

39-
private void instrument(DataLoaderInstrumentation registryInstrumentation, Map<String, DataLoader<?, ?>> incomingDataLoaders) {
40-
this.dataLoaders.putAll(incomingDataLoaders);
50+
protected DataLoaderRegistry(Map<String, DataLoader<?, ?>> dataLoaders, DataLoaderInstrumentation instrumentation ) {
51+
this.dataLoaders = instrumentDLs(dataLoaders, instrumentation);
52+
this.instrumentation = instrumentation;
53+
}
54+
55+
private Map<String, DataLoader<?, ?>> instrumentDLs(Map<String, DataLoader<?, ?>> incomingDataLoaders, DataLoaderInstrumentation registryInstrumentation) {
56+
Map<String, DataLoader<?, ?>> dataLoaders = new ConcurrentHashMap<>(incomingDataLoaders);
4157
if (registryInstrumentation != null) {
42-
this.dataLoaders.replaceAll((k, existingDL) -> instrumentDL(registryInstrumentation, existingDL));
58+
dataLoaders.replaceAll((k, existingDL) -> instrumentDL(registryInstrumentation, existingDL));
4359
}
60+
return dataLoaders;
4461
}
4562

4663
/**

src/main/java/org/dataloader/registries/ScheduledDataLoaderRegistry.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.dataloader.DataLoader;
44
import org.dataloader.DataLoaderRegistry;
55
import org.dataloader.annotations.ExperimentalApi;
6+
import org.dataloader.instrumentation.DataLoaderInstrumentation;
67

78
import java.time.Duration;
89
import java.util.LinkedHashMap;
@@ -64,8 +65,7 @@ public class ScheduledDataLoaderRegistry extends DataLoaderRegistry implements A
6465
private volatile boolean closed;
6566

6667
private ScheduledDataLoaderRegistry(Builder builder) {
67-
super();
68-
this.dataLoaders.putAll(builder.dataLoaders);
68+
super(builder.dataLoaders, builder.instrumentation);
6969
this.scheduledExecutorService = builder.scheduledExecutorService;
7070
this.defaultExecutorUsed = builder.defaultExecutorUsed;
7171
this.schedule = builder.schedule;
@@ -271,6 +271,8 @@ public static class Builder {
271271
private boolean defaultExecutorUsed = false;
272272
private Duration schedule = Duration.ofMillis(10);
273273
private boolean tickerMode = false;
274+
private DataLoaderInstrumentation instrumentation;
275+
274276

275277
/**
276278
* If you provide a {@link ScheduledExecutorService} then it will NOT be shutdown when
@@ -363,6 +365,11 @@ public Builder tickerMode(boolean tickerMode) {
363365
return this;
364366
}
365367

368+
public Builder instrumentation(DataLoaderInstrumentation instrumentation) {
369+
this.instrumentation = instrumentation;
370+
return this;
371+
}
372+
366373
/**
367374
* @return the newly built {@link ScheduledDataLoaderRegistry}
368375
*/

src/test/java/org/dataloader/instrumentation/ChainedDataLoaderInstrumentationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import static org.hamcrest.MatcherAssert.assertThat;
1919
import static org.hamcrest.Matchers.equalTo;
2020

21-
class ChainedDataLoaderInstrumentationTest {
21+
public class ChainedDataLoaderInstrumentationTest {
2222

2323
CapturingInstrumentation capturingA;
2424
CapturingInstrumentation capturingB;

src/test/java/org/dataloader/instrumentation/DataLoaderInstrumentationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import static org.hamcrest.Matchers.greaterThan;
2323
import static org.hamcrest.Matchers.is;
2424

25-
class DataLoaderInstrumentationTest {
25+
public class DataLoaderInstrumentationTest {
2626

2727
BatchLoader<String, String> snoozingBatchLoader = keys -> CompletableFuture.supplyAsync(() -> {
2828
TestKit.snooze(100);

src/test/java/org/dataloader/instrumentation/DataLoaderRegistryInstrumentationTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.dataloader.DataLoaderRegistry;
55
import org.dataloader.fixtures.TestKit;
66
import org.dataloader.fixtures.parameterized.TestDataLoaderFactory;
7+
import org.dataloader.registries.ScheduledDataLoaderRegistry;
78
import org.junit.jupiter.api.BeforeEach;
89
import org.junit.jupiter.api.Test;
910
import org.junit.jupiter.params.ParameterizedTest;
@@ -18,7 +19,7 @@
1819
import static org.hamcrest.Matchers.equalTo;
1920
import static org.hamcrest.Matchers.instanceOf;
2021

21-
class DataLoaderRegistryInstrumentationTest {
22+
public class DataLoaderRegistryInstrumentationTest {
2223
DataLoader<String, String> dlX;
2324
DataLoader<String, String> dlY;
2425
DataLoader<String, String> dlZ;
@@ -177,6 +178,29 @@ void chainedInstrumentationsWillBeCombined() {
177178
assertThat(instrumentations, equalTo(List.of(instrA, instrB)));
178179
}
179180

181+
@SuppressWarnings("resource")
182+
@Test
183+
void canInstrumentScheduledRegistryViaBuilder() {
184+
185+
assertThat(dlX.getOptions().getInstrumentation(), equalTo(DataLoaderInstrumentationHelper.NOOP_INSTRUMENTATION));
186+
187+
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
188+
.instrumentation(chainedInstrA)
189+
.register("X", dlX)
190+
.register("Y", dlY)
191+
.register("Z", dlZ)
192+
.build();
193+
194+
assertThat(registry.getInstrumentation(), equalTo(chainedInstrA));
195+
196+
for (String key : List.of("X", "Y", "Z")) {
197+
DataLoaderInstrumentation instrumentation = registry.getDataLoader(key).getOptions().getInstrumentation();
198+
assertThat(instrumentation, instanceOf(ChainedDataLoaderInstrumentation.class));
199+
List<DataLoaderInstrumentation> instrumentations = ((ChainedDataLoaderInstrumentation) instrumentation).getInstrumentations();
200+
assertThat(instrumentations, equalTo(List.of(instrA)));
201+
}
202+
}
203+
180204
@ParameterizedTest
181205
@MethodSource("org.dataloader.fixtures.parameterized.TestDataLoaderFactories#get")
182206
public void endToEndIntegrationTest(TestDataLoaderFactory factory) {
@@ -200,6 +224,5 @@ public void endToEndIntegrationTest(TestDataLoaderFactory factory) {
200224
assertThat(instrA.methods, equalTo(List.of("A_beginDispatch",
201225
"A_beginBatchLoader", "A_beginBatchLoader_onDispatched", "A_beginBatchLoader_onCompleted",
202226
"A_beginDispatch_onDispatched", "A_beginDispatch_onCompleted")));
203-
204227
}
205228
}

0 commit comments

Comments
 (0)