Skip to content

Commit 063beeb

Browse files
committed
Use LongAdder while capturing QueryPlanOptimizerStats
LongAdder accumulates stripped values during read, rather than making it visible to other threads on write.
1 parent be3486c commit 063beeb

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

core/trino-main/src/main/java/io/trino/execution/querystats/QueryPlanOptimizerStats.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import com.google.errorprone.annotations.ThreadSafe;
1717
import io.trino.spi.eventlistener.QueryPlanOptimizerStatistics;
1818

19-
import java.util.concurrent.atomic.AtomicLong;
19+
import java.util.concurrent.atomic.LongAdder;
2020

2121
import static com.google.common.base.Preconditions.checkArgument;
2222
import static java.util.Objects.requireNonNull;
@@ -25,10 +25,10 @@
2525
public class QueryPlanOptimizerStats
2626
{
2727
private final String rule;
28-
private final AtomicLong invocations = new AtomicLong();
29-
private final AtomicLong applied = new AtomicLong();
30-
private final AtomicLong totalTime = new AtomicLong();
31-
private final AtomicLong failures = new AtomicLong();
28+
private final LongAdder invocations = new LongAdder();
29+
private final LongAdder applied = new LongAdder();
30+
private final LongAdder totalTime = new LongAdder();
31+
private final LongAdder failures = new LongAdder();
3232

3333
public QueryPlanOptimizerStats(String rule)
3434
{
@@ -38,16 +38,16 @@ public QueryPlanOptimizerStats(String rule)
3838
public void record(long nanos, boolean applied)
3939
{
4040
if (applied) {
41-
this.applied.incrementAndGet();
41+
this.applied.increment();
4242
}
4343

44-
invocations.incrementAndGet();
45-
totalTime.addAndGet(nanos);
44+
invocations.increment();
45+
totalTime.add(nanos);
4646
}
4747

4848
public void recordFailure()
4949
{
50-
failures.incrementAndGet();
50+
failures.increment();
5151
}
5252

5353
public String getRule()
@@ -57,37 +57,37 @@ public String getRule()
5757

5858
public long getInvocations()
5959
{
60-
return invocations.get();
60+
return invocations.longValue();
6161
}
6262

6363
public long getApplied()
6464
{
65-
return applied.get();
65+
return applied.longValue();
6666
}
6767

6868
public long getFailures()
6969
{
70-
return failures.get();
70+
return failures.longValue();
7171
}
7272

7373
public long getTotalTime()
7474
{
75-
return totalTime.get();
75+
return totalTime.longValue();
7676
}
7777

7878
public QueryPlanOptimizerStatistics snapshot()
7979
{
80-
return new QueryPlanOptimizerStatistics(rule, invocations.get(), applied.get(), totalTime.get(), failures.get());
80+
return new QueryPlanOptimizerStatistics(rule, invocations.longValue(), applied.longValue(), totalTime.longValue(), failures.longValue());
8181
}
8282

8383
public QueryPlanOptimizerStats merge(QueryPlanOptimizerStats other)
8484
{
8585
checkArgument(rule.equals(other.getRule()), "Cannot merge stats for different rules: %s and %s", rule, other.getRule());
8686

87-
invocations.addAndGet(other.getInvocations());
88-
applied.addAndGet(other.getApplied());
89-
failures.addAndGet(other.getFailures());
90-
totalTime.addAndGet(other.getTotalTime());
87+
invocations.add(other.getInvocations());
88+
applied.add(other.getApplied());
89+
failures.add(other.getFailures());
90+
totalTime.add(other.getTotalTime());
9191

9292
return this;
9393
}

0 commit comments

Comments
 (0)