Skip to content

Commit 6942cc1

Browse files
committed
Breaking change for StatisticsCollector
Use `LongAdder` instead of `AtomicLong`, that is more suitable for high contention (see the javadoc).
1 parent 8dad2ac commit 6942cc1

7 files changed

+224
-138
lines changed

src/main/java/org/dataloader/stats/DelegatingStatisticsCollector.java

+20-20
Original file line numberDiff line numberDiff line change
@@ -26,63 +26,63 @@ public DelegatingStatisticsCollector(StatisticsCollector delegateCollector) {
2626
}
2727

2828
@Override
29-
public <K> long incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
29+
public <K> void incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
3030
delegateCollector.incrementLoadCount(context);
31-
return collector.incrementLoadCount(context);
31+
collector.incrementLoadCount(context);
3232
}
3333

3434
@Deprecated
3535
@Override
36-
public long incrementLoadCount() {
37-
return incrementLoadCount(null);
36+
public void incrementLoadCount() {
37+
incrementLoadCount(null);
3838
}
3939

4040
@Override
41-
public <K> long incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
41+
public <K> void incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
4242
delegateCollector.incrementLoadErrorCount(context);
43-
return collector.incrementLoadErrorCount(context);
43+
collector.incrementLoadErrorCount(context);
4444
}
4545

4646
@Deprecated
4747
@Override
48-
public long incrementLoadErrorCount() {
49-
return incrementLoadErrorCount(null);
48+
public void incrementLoadErrorCount() {
49+
incrementLoadErrorCount(null);
5050
}
5151

5252
@Override
53-
public <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
53+
public <K> void incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
5454
delegateCollector.incrementBatchLoadCountBy(delta, context);
55-
return collector.incrementBatchLoadCountBy(delta, context);
55+
collector.incrementBatchLoadCountBy(delta, context);
5656
}
5757

5858
@Deprecated
5959
@Override
60-
public long incrementBatchLoadCountBy(long delta) {
61-
return incrementBatchLoadCountBy(delta, null);
60+
public void incrementBatchLoadCountBy(long delta) {
61+
incrementBatchLoadCountBy(delta, null);
6262
}
6363

6464
@Override
65-
public <K> long incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
65+
public <K> void incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
6666
delegateCollector.incrementBatchLoadExceptionCount(context);
67-
return collector.incrementBatchLoadExceptionCount(context);
67+
collector.incrementBatchLoadExceptionCount(context);
6868
}
6969

7070
@Deprecated
7171
@Override
72-
public long incrementBatchLoadExceptionCount() {
73-
return incrementBatchLoadExceptionCount(null);
72+
public void incrementBatchLoadExceptionCount() {
73+
incrementBatchLoadExceptionCount(null);
7474
}
7575

7676
@Override
77-
public <K> long incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
77+
public <K> void incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
7878
delegateCollector.incrementCacheHitCount(context);
79-
return collector.incrementCacheHitCount(context);
79+
collector.incrementCacheHitCount(context);
8080
}
8181

8282
@Deprecated
8383
@Override
84-
public long incrementCacheHitCount() {
85-
return incrementCacheHitCount(null);
84+
public void incrementCacheHitCount() {
85+
incrementCacheHitCount(null);
8686
}
8787

8888
/**

src/main/java/org/dataloader/stats/NoOpStatisticsCollector.java

+16-20
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,54 @@ public class NoOpStatisticsCollector implements StatisticsCollector {
1414
private static final Statistics ZERO_STATS = new Statistics();
1515

1616
@Override
17-
public <K> long incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
18-
return 0;
17+
public <K> void incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
1918
}
2019

2120
@Deprecated
2221
@Override
23-
public long incrementLoadCount() {
24-
return incrementLoadCount(null);
22+
public void incrementLoadCount() {
23+
incrementLoadCount(null);
2524
}
2625

2726
@Override
28-
public <K> long incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
29-
return 0;
27+
public <K> void incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
3028
}
3129

3230
@Deprecated
3331
@Override
34-
public long incrementLoadErrorCount() {
35-
return incrementLoadErrorCount(null);
32+
public void incrementLoadErrorCount() {
33+
incrementLoadErrorCount(null);
3634
}
3735

3836
@Override
39-
public <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
40-
return 0;
37+
public <K> void incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
4138
}
4239

4340
@Deprecated
4441
@Override
45-
public long incrementBatchLoadCountBy(long delta) {
46-
return incrementBatchLoadCountBy(delta, null);
42+
public void incrementBatchLoadCountBy(long delta) {
43+
incrementBatchLoadCountBy(delta, null);
4744
}
4845

4946
@Override
50-
public <K> long incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
51-
return 0;
47+
public <K> void incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
48+
5249
}
5350

5451
@Deprecated
5552
@Override
56-
public long incrementBatchLoadExceptionCount() {
57-
return incrementBatchLoadExceptionCount(null);
53+
public void incrementBatchLoadExceptionCount() {
54+
incrementBatchLoadExceptionCount(null);
5855
}
5956

6057
@Override
61-
public <K> long incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
62-
return 0;
58+
public <K> void incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
6359
}
6460

6561
@Deprecated
6662
@Override
67-
public long incrementCacheHitCount() {
68-
return incrementCacheHitCount(null);
63+
public void incrementCacheHitCount() {
64+
incrementCacheHitCount(null);
6965
}
7066

7167
@Override

src/main/java/org/dataloader/stats/SimpleStatisticsCollector.java

+30-29
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import org.dataloader.stats.context.IncrementLoadCountStatisticsContext;
77
import org.dataloader.stats.context.IncrementLoadErrorCountStatisticsContext;
88

9-
import java.util.concurrent.atomic.AtomicLong;
9+
import java.util.concurrent.atomic.LongAdder;
1010

1111
/**
1212
* This simple collector uses {@link java.util.concurrent.atomic.AtomicLong}s to collect
@@ -15,72 +15,73 @@
1515
* @see org.dataloader.stats.StatisticsCollector
1616
*/
1717
public class SimpleStatisticsCollector implements StatisticsCollector {
18-
private final AtomicLong loadCount = new AtomicLong();
19-
private final AtomicLong batchInvokeCount = new AtomicLong();
20-
private final AtomicLong batchLoadCount = new AtomicLong();
21-
private final AtomicLong cacheHitCount = new AtomicLong();
22-
private final AtomicLong batchLoadExceptionCount = new AtomicLong();
23-
private final AtomicLong loadErrorCount = new AtomicLong();
18+
19+
private final LongAdder loadCount = new LongAdder();
20+
private final LongAdder batchInvokeCount = new LongAdder();
21+
private final LongAdder batchLoadCount = new LongAdder();
22+
private final LongAdder cacheHitCount = new LongAdder();
23+
private final LongAdder batchLoadExceptionCount = new LongAdder();
24+
private final LongAdder loadErrorCount = new LongAdder();
2425

2526
@Override
26-
public <K> long incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
27-
return loadCount.incrementAndGet();
27+
public <K> void incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
28+
loadCount.increment();
2829
}
2930

3031
@Deprecated
3132
@Override
32-
public long incrementLoadCount() {
33-
return incrementLoadCount(null);
33+
public void incrementLoadCount() {
34+
incrementLoadCount(null);
3435
}
3536

3637
@Override
37-
public <K> long incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
38-
return loadErrorCount.incrementAndGet();
38+
public <K> void incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
39+
loadErrorCount.increment();
3940
}
4041

4142
@Deprecated
4243
@Override
43-
public long incrementLoadErrorCount() {
44-
return incrementLoadErrorCount(null);
44+
public void incrementLoadErrorCount() {
45+
incrementLoadErrorCount(null);
4546
}
4647

4748
@Override
48-
public <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
49-
batchInvokeCount.incrementAndGet();
50-
return batchLoadCount.addAndGet(delta);
49+
public <K> void incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
50+
batchInvokeCount.increment();
51+
batchLoadCount.add(delta);
5152
}
5253

5354
@Deprecated
5455
@Override
55-
public long incrementBatchLoadCountBy(long delta) {
56-
return incrementBatchLoadCountBy(delta, null);
56+
public void incrementBatchLoadCountBy(long delta) {
57+
incrementBatchLoadCountBy(delta, null);
5758
}
5859

5960
@Override
60-
public <K> long incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
61-
return batchLoadExceptionCount.incrementAndGet();
61+
public <K> void incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
62+
batchLoadExceptionCount.increment();
6263
}
6364

6465
@Deprecated
6566
@Override
66-
public long incrementBatchLoadExceptionCount() {
67-
return incrementBatchLoadExceptionCount(null);
67+
public void incrementBatchLoadExceptionCount() {
68+
incrementBatchLoadExceptionCount(null);
6869
}
6970

7071
@Override
71-
public <K> long incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
72-
return cacheHitCount.incrementAndGet();
72+
public <K> void incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
73+
cacheHitCount.increment();
7374
}
7475

7576
@Deprecated
7677
@Override
77-
public long incrementCacheHitCount() {
78-
return incrementCacheHitCount(null);
78+
public void incrementCacheHitCount() {
79+
incrementCacheHitCount(null);
7980
}
8081

8182
@Override
8283
public Statistics getStatistics() {
83-
return new Statistics(loadCount.get(), loadErrorCount.get(), batchInvokeCount.get(), batchLoadCount.get(), batchLoadExceptionCount.get(), cacheHitCount.get());
84+
return new Statistics(loadCount.sum(), loadErrorCount.sum(), batchInvokeCount.sum(), batchLoadCount.sum(), batchLoadExceptionCount.sum(), cacheHitCount.sum());
8485
}
8586

8687
@Override

src/main/java/org/dataloader/stats/StatisticsCollector.java

+15-29
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,47 @@ public interface StatisticsCollector {
1818
*
1919
* @param <K> the class of the key in the data loader
2020
* @param context the context containing metadata of the data loader invocation
21-
*
22-
* @return the current value after increment
2321
*/
24-
default <K> long incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
25-
return incrementLoadCount();
22+
default <K> void incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
23+
incrementLoadCount();
2624
}
2725

2826
/**
2927
* Called to increment the number of loads
3028
*
3129
* @deprecated use {@link #incrementLoadCount(IncrementLoadCountStatisticsContext)}
32-
* @return the current value after increment
3330
*/
3431
@Deprecated
35-
long incrementLoadCount();
32+
void incrementLoadCount();
3633

3734
/**
3835
* Called to increment the number of loads that resulted in an object deemed in error
3936
*
4037
* @param <K> the class of the key in the data loader
4138
* @param context the context containing metadata of the data loader invocation
4239
*
43-
* @return the current value after increment
4440
*/
45-
default <K> long incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
46-
return incrementLoadErrorCount();
41+
default <K> void incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
42+
incrementLoadErrorCount();
4743
}
4844

4945
/**
5046
* Called to increment the number of loads that resulted in an object deemed in error
5147
*
5248
* @deprecated use {@link #incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext)}
53-
* @return the current value after increment
5449
*/
5550
@Deprecated
56-
long incrementLoadErrorCount();
51+
void incrementLoadErrorCount();
5752

5853
/**
5954
* Called to increment the number of batch loads
6055
*
6156
* @param <K> the class of the key in the data loader
6257
* @param delta how much to add to the count
6358
* @param context the context containing metadata of the data loader invocation
64-
*
65-
* @return the current value after increment
6659
*/
67-
default <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
68-
return incrementBatchLoadCountBy(delta);
60+
default <K> void incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
61+
incrementBatchLoadCountBy(delta);
6962
}
7063

7164
/**
@@ -74,52 +67,45 @@ default <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountBy
7467
* @param delta how much to add to the count
7568
*
7669
* @deprecated use {@link #incrementBatchLoadCountBy(long, IncrementBatchLoadCountByStatisticsContext)}
77-
* @return the current value after increment
7870
*/
7971
@Deprecated
80-
long incrementBatchLoadCountBy(long delta);
72+
void incrementBatchLoadCountBy(long delta);
8173

8274
/**
8375
* Called to increment the number of batch loads exceptions
8476
*
8577
* @param <K> the class of the key in the data loader
8678
* @param context the context containing metadata of the data loader invocation
87-
*
88-
* @return the current value after increment
8979
*/
90-
default <K> long incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
91-
return incrementBatchLoadExceptionCount();
80+
default <K> void incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
81+
incrementBatchLoadExceptionCount();
9282
}
9383

9484
/**
9585
* Called to increment the number of batch loads exceptions
9686
*
9787
* @deprecated use {@link #incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext)}
98-
* @return the current value after increment
9988
*/
10089
@Deprecated
101-
long incrementBatchLoadExceptionCount();
90+
void incrementBatchLoadExceptionCount();
10291

10392
/**
10493
* Called to increment the number of cache hits
10594
*
10695
* @param <K> the class of the key in the data loader
10796
* @param context the context containing metadata of the data loader invocation
108-
*
109-
* @return the current value after increment
11097
*/
111-
default <K> long incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
112-
return incrementCacheHitCount();
98+
default <K> void incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
99+
incrementCacheHitCount();
113100
}
114101

115102
/**
116103
* Called to increment the number of cache hits
117104
*
118105
* @deprecated use {@link #incrementCacheHitCount(IncrementCacheHitCountStatisticsContext)}
119-
* @return the current value after increment
120106
*/
121107
@Deprecated
122-
long incrementCacheHitCount();
108+
void incrementCacheHitCount();
123109

124110
/**
125111
* @return the statistics that have been gathered to this point in time

0 commit comments

Comments
 (0)