Skip to content

Commit 51cf042

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 51cf042

6 files changed

+113
-129
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-20
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public interface StatisticsCollector {
2121
*
2222
* @return the current value after increment
2323
*/
24-
default <K> long incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
25-
return incrementLoadCount();
24+
default <K> void incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) {
25+
incrementLoadCount();
2626
}
2727

2828
/**
@@ -32,40 +32,36 @@ default <K> long incrementLoadCount(IncrementLoadCountStatisticsContext<K> conte
3232
* @return the current value after increment
3333
*/
3434
@Deprecated
35-
long incrementLoadCount();
35+
void incrementLoadCount();
3636

3737
/**
3838
* Called to increment the number of loads that resulted in an object deemed in error
3939
*
4040
* @param <K> the class of the key in the data loader
4141
* @param context the context containing metadata of the data loader invocation
4242
*
43-
* @return the current value after increment
4443
*/
45-
default <K> long incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
46-
return incrementLoadErrorCount();
44+
default <K> void incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) {
45+
incrementLoadErrorCount();
4746
}
4847

4948
/**
5049
* Called to increment the number of loads that resulted in an object deemed in error
5150
*
5251
* @deprecated use {@link #incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext)}
53-
* @return the current value after increment
5452
*/
5553
@Deprecated
56-
long incrementLoadErrorCount();
54+
void incrementLoadErrorCount();
5755

5856
/**
5957
* Called to increment the number of batch loads
6058
*
6159
* @param <K> the class of the key in the data loader
6260
* @param delta how much to add to the count
6361
* @param context the context containing metadata of the data loader invocation
64-
*
65-
* @return the current value after increment
6662
*/
67-
default <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
68-
return incrementBatchLoadCountBy(delta);
63+
default <K> void incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) {
64+
incrementBatchLoadCountBy(delta);
6965
}
7066

7167
/**
@@ -74,10 +70,9 @@ default <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountBy
7470
* @param delta how much to add to the count
7571
*
7672
* @deprecated use {@link #incrementBatchLoadCountBy(long, IncrementBatchLoadCountByStatisticsContext)}
77-
* @return the current value after increment
7873
*/
7974
@Deprecated
80-
long incrementBatchLoadCountBy(long delta);
75+
void incrementBatchLoadCountBy(long delta);
8176

8277
/**
8378
* Called to increment the number of batch loads exceptions
@@ -87,8 +82,8 @@ default <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountBy
8782
*
8883
* @return the current value after increment
8984
*/
90-
default <K> long incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
91-
return incrementBatchLoadExceptionCount();
85+
default <K> void incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) {
86+
incrementBatchLoadExceptionCount();
9287
}
9388

9489
/**
@@ -98,7 +93,7 @@ default <K> long incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCou
9893
* @return the current value after increment
9994
*/
10095
@Deprecated
101-
long incrementBatchLoadExceptionCount();
96+
void incrementBatchLoadExceptionCount();
10297

10398
/**
10499
* Called to increment the number of cache hits
@@ -108,8 +103,8 @@ default <K> long incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCou
108103
*
109104
* @return the current value after increment
110105
*/
111-
default <K> long incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
112-
return incrementCacheHitCount();
106+
default <K> void incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) {
107+
incrementCacheHitCount();
113108
}
114109

115110
/**
@@ -119,7 +114,7 @@ default <K> long incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<
119114
* @return the current value after increment
120115
*/
121116
@Deprecated
122-
long incrementCacheHitCount();
117+
void incrementCacheHitCount();
123118

124119
/**
125120
* @return the statistics that have been gathered to this point in time

0 commit comments

Comments
 (0)