Skip to content

Add context objects to StatisticsCollector methods #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/main/java/org/dataloader/DataLoaderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import org.dataloader.annotations.Internal;
import org.dataloader.impl.CompletableFutureKit;
import org.dataloader.stats.StatisticsCollector;
import org.dataloader.stats.context.IncrementBatchLoadCountByStatisticsContext;
import org.dataloader.stats.context.IncrementBatchLoadExceptionCountStatisticsContext;
import org.dataloader.stats.context.IncrementCacheHitCountStatisticsContext;
import org.dataloader.stats.context.IncrementLoadCountStatisticsContext;
import org.dataloader.stats.context.IncrementLoadErrorCountStatisticsContext;

import java.time.Clock;
import java.time.Instant;
Expand Down Expand Up @@ -105,7 +110,7 @@ Optional<CompletableFuture<V>> getIfPresent(K key) {
if (cachingEnabled) {
Object cacheKey = getCacheKey(nonNull(key));
if (futureCache.containsKey(cacheKey)) {
stats.incrementCacheHitCount();
stats.incrementCacheHitCount(new IncrementCacheHitCountStatisticsContext<>(key));
return Optional.of(futureCache.get(cacheKey));
}
}
Expand All @@ -132,7 +137,7 @@ CompletableFuture<V> load(K key, Object loadContext) {
boolean batchingEnabled = loaderOptions.batchingEnabled();
boolean cachingEnabled = loaderOptions.cachingEnabled();

stats.incrementLoadCount();
stats.incrementLoadCount(new IncrementLoadCountStatisticsContext<>(key, loadContext));

if (cachingEnabled) {
return loadFromCache(key, loadContext, batchingEnabled);
Expand Down Expand Up @@ -223,18 +228,20 @@ private CompletableFuture<List<V>> sliceIntoBatchesOfBatches(List<K> keys, List<

@SuppressWarnings("unchecked")
private CompletableFuture<List<V>> dispatchQueueBatch(List<K> keys, List<Object> callContexts, List<CompletableFuture<V>> queuedFutures) {
stats.incrementBatchLoadCountBy(keys.size());
stats.incrementBatchLoadCountBy(keys.size(), new IncrementBatchLoadCountByStatisticsContext<>(keys, callContexts));
CompletableFuture<List<V>> batchLoad = invokeLoader(keys, callContexts, loaderOptions.cachingEnabled());
return batchLoad
.thenApply(values -> {
assertResultSize(keys, values);

List<K> clearCacheKeys = new ArrayList<>();
for (int idx = 0; idx < queuedFutures.size(); idx++) {
K key = keys.get(idx);
V value = values.get(idx);
Object callContext = callContexts.get(idx);
CompletableFuture<V> future = queuedFutures.get(idx);
if (value instanceof Throwable) {
stats.incrementLoadErrorCount();
stats.incrementLoadErrorCount(new IncrementLoadErrorCountStatisticsContext<>(key, callContext));
future.completeExceptionally((Throwable) value);
clearCacheKeys.add(keys.get(idx));
} else if (value instanceof Try) {
Expand All @@ -244,7 +251,7 @@ private CompletableFuture<List<V>> dispatchQueueBatch(List<K> keys, List<Object>
if (tryValue.isSuccess()) {
future.complete(tryValue.get());
} else {
stats.incrementLoadErrorCount();
stats.incrementLoadErrorCount(new IncrementLoadErrorCountStatisticsContext<>(key, callContext));
future.completeExceptionally(tryValue.getThrowable());
clearCacheKeys.add(keys.get(idx));
}
Expand All @@ -255,7 +262,7 @@ private CompletableFuture<List<V>> dispatchQueueBatch(List<K> keys, List<Object>
possiblyClearCacheEntriesOnExceptions(clearCacheKeys);
return values;
}).exceptionally(ex -> {
stats.incrementBatchLoadExceptionCount();
stats.incrementBatchLoadExceptionCount(new IncrementBatchLoadExceptionCountStatisticsContext<>(keys, callContexts));
if (ex instanceof CompletionException) {
ex = ex.getCause();
}
Expand Down Expand Up @@ -294,7 +301,7 @@ private CompletableFuture<V> loadFromCache(K key, Object loadContext, boolean ba

if (futureCache.containsKey(cacheKey)) {
// We already have a promise for this key, no need to check value cache or queue up load
stats.incrementCacheHitCount();
stats.incrementCacheHitCount(new IncrementCacheHitCountStatisticsContext<>(key, loadContext));
return futureCache.get(cacheKey);
}

Expand All @@ -310,7 +317,7 @@ private CompletableFuture<V> queueOrInvokeLoader(K key, Object loadContext, bool
loaderQueue.add(new LoaderQueueEntry<>(key, loadCallFuture, loadContext));
return loadCallFuture;
} else {
stats.incrementBatchLoadCountBy(1);
stats.incrementBatchLoadCountBy(1, new IncrementBatchLoadCountByStatisticsContext<>(key, loadContext));
// immediate execution of batch function
return invokeLoaderImmediately(key, loadContext, cachingEnabled);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.dataloader.stats;

import org.dataloader.stats.context.IncrementBatchLoadCountByStatisticsContext;
import org.dataloader.stats.context.IncrementBatchLoadExceptionCountStatisticsContext;
import org.dataloader.stats.context.IncrementCacheHitCountStatisticsContext;
import org.dataloader.stats.context.IncrementLoadCountStatisticsContext;
import org.dataloader.stats.context.IncrementLoadErrorCountStatisticsContext;

import static org.dataloader.impl.Assertions.nonNull;

/**
Expand All @@ -20,33 +26,63 @@ public DelegatingStatisticsCollector(StatisticsCollector delegateCollector) {
}

@Override
public long incrementLoadCount() {
delegateCollector.incrementLoadCount();
return collector.incrementLoadCount();
public <K> long incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
delegateCollector.incrementLoadCount(context);
return collector.incrementLoadCount(context);
}

@Deprecated
@Override
public long incrementBatchLoadCountBy(long delta) {
delegateCollector.incrementBatchLoadCountBy(delta);
return collector.incrementBatchLoadCountBy(delta);
public long incrementLoadCount() {
return incrementLoadCount(null);
}

@Override
public long incrementCacheHitCount() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff is a bit clobbered because I decided to order the methods according to the interface.

delegateCollector.incrementCacheHitCount();
return collector.incrementCacheHitCount();
public <K> long incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
delegateCollector.incrementLoadErrorCount(context);
return collector.incrementLoadErrorCount(context);
}

@Deprecated
@Override
public long incrementLoadErrorCount() {
delegateCollector.incrementLoadErrorCount();
return collector.incrementLoadErrorCount();
return incrementLoadErrorCount(null);
}

@Override
public <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
delegateCollector.incrementBatchLoadCountBy(delta, context);
return collector.incrementBatchLoadCountBy(delta, context);
}

@Deprecated
@Override
public long incrementBatchLoadCountBy(long delta) {
return incrementBatchLoadCountBy(delta, null);
}

@Override
public <K> long incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
delegateCollector.incrementBatchLoadExceptionCount(context);
return collector.incrementBatchLoadExceptionCount(context);
}

@Deprecated
@Override
public long incrementBatchLoadExceptionCount() {
delegateCollector.incrementBatchLoadExceptionCount();
return collector.incrementBatchLoadExceptionCount();
return incrementBatchLoadExceptionCount(null);
}

@Override
public <K> long incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
delegateCollector.incrementCacheHitCount(context);
return collector.incrementCacheHitCount(context);
}

@Deprecated
@Override
public long incrementCacheHitCount() {
return incrementCacheHitCount(null);
}

/**
Expand Down
38 changes: 37 additions & 1 deletion src/main/java/org/dataloader/stats/NoOpStatisticsCollector.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,71 @@
package org.dataloader.stats;

import org.dataloader.stats.context.IncrementBatchLoadCountByStatisticsContext;
import org.dataloader.stats.context.IncrementBatchLoadExceptionCountStatisticsContext;
import org.dataloader.stats.context.IncrementCacheHitCountStatisticsContext;
import org.dataloader.stats.context.IncrementLoadCountStatisticsContext;
import org.dataloader.stats.context.IncrementLoadErrorCountStatisticsContext;

/**
* A statistics collector that does nothing
*/
public class NoOpStatisticsCollector implements StatisticsCollector {

private static final Statistics ZERO_STATS = new Statistics();

@Override
public <K> long incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
return 0;
}

@Deprecated
@Override
public long incrementLoadCount() {
return incrementLoadCount(null);
}

@Override
public <K> long incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
return 0;
}

@Deprecated
@Override
public long incrementLoadErrorCount() {
return incrementLoadErrorCount(null);
}

@Override
public <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
return 0;
}

@Deprecated
@Override
public long incrementBatchLoadCountBy(long delta) {
return incrementBatchLoadCountBy(delta, null);
}

@Override
public <K> long incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
return 0;
}

@Deprecated
@Override
public long incrementBatchLoadExceptionCount() {
return incrementBatchLoadExceptionCount(null);
}

@Override
public <K> long incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
return 0;
}

@Deprecated
@Override
public long incrementCacheHitCount() {
return 0;
return incrementCacheHitCount(null);
}

@Override
Expand Down
49 changes: 42 additions & 7 deletions src/main/java/org/dataloader/stats/SimpleStatisticsCollector.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.dataloader.stats;

import org.dataloader.stats.context.IncrementBatchLoadCountByStatisticsContext;
import org.dataloader.stats.context.IncrementBatchLoadExceptionCountStatisticsContext;
import org.dataloader.stats.context.IncrementCacheHitCountStatisticsContext;
import org.dataloader.stats.context.IncrementLoadCountStatisticsContext;
import org.dataloader.stats.context.IncrementLoadErrorCountStatisticsContext;

import java.util.concurrent.atomic.AtomicLong;

/**
Expand All @@ -17,30 +23,59 @@ public class SimpleStatisticsCollector implements StatisticsCollector {
private final AtomicLong loadErrorCount = new AtomicLong();

@Override
public long incrementLoadCount() {
public <K> long incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
return loadCount.incrementAndGet();
}

@Deprecated
@Override
public long incrementLoadCount() {
return incrementLoadCount(null);
}

@Override
public long incrementBatchLoadCountBy(long delta) {
public <K> long incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
return loadErrorCount.incrementAndGet();
}

@Deprecated
@Override
public long incrementLoadErrorCount() {
return incrementLoadErrorCount(null);
}

@Override
public <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
batchInvokeCount.incrementAndGet();
return batchLoadCount.addAndGet(delta);
}

@Deprecated
@Override
public long incrementCacheHitCount() {
return cacheHitCount.incrementAndGet();
public long incrementBatchLoadCountBy(long delta) {
return incrementBatchLoadCountBy(delta, null);
}

@Override
public long incrementLoadErrorCount() {
return loadErrorCount.incrementAndGet();
public <K> long incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
return batchLoadExceptionCount.incrementAndGet();
}

@Deprecated
@Override
public long incrementBatchLoadExceptionCount() {
return batchLoadExceptionCount.incrementAndGet();
return incrementBatchLoadExceptionCount(null);
}

@Override
public <K> long incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
return cacheHitCount.incrementAndGet();
}

@Deprecated
@Override
public long incrementCacheHitCount() {
return incrementCacheHitCount(null);
}

@Override
Expand Down
Loading