Skip to content

Commit 66276c1

Browse files
Cleanup some AtomicLong usage (elastic#91132)
Cleaning up a couple spots where we were using atomics inefficiently: * removed our home-brew get and update in the countdown * removed needlessly capturing lambdas in update and gets
1 parent 4d27313 commit 66276c1

File tree

9 files changed

+17
-24
lines changed

9 files changed

+17
-24
lines changed

server/src/main/java/org/elasticsearch/cluster/coordination/LagDetector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private class NodeAppliedStateTracker {
142142
}
143143

144144
void increaseAppliedVersion(long appliedVersion) {
145-
long maxAppliedVersion = this.appliedVersion.updateAndGet(v -> Math.max(v, appliedVersion));
145+
long maxAppliedVersion = this.appliedVersion.accumulateAndGet(appliedVersion, Math::max);
146146
logger.trace("{} applied version {}, max now {}", this, appliedVersion, maxAppliedVersion);
147147
}
148148

server/src/main/java/org/elasticsearch/common/util/concurrent/CountDown.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,10 @@ public CountDown(int count) {
3535
*/
3636
public boolean countDown() {
3737
assert originalCount > 0;
38-
for (;;) {
39-
final int current = countDown.get();
38+
return countDown.getAndUpdate(current -> {
4039
assert current >= 0;
41-
if (current == 0) {
42-
return false;
43-
}
44-
if (countDown.compareAndSet(current, current - 1)) {
45-
return current == 1;
46-
}
47-
}
40+
return current == 0 ? 0 : current - 1;
41+
}) == 1;
4842
}
4943

5044
/**

server/src/main/java/org/elasticsearch/common/util/concurrent/PrioritizedThrottledTaskRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected void pollAndSpawn() {
7777

7878
// Each worker thread that runs a task, first needs to get a "free slot" in order to respect maxRunningTasks.
7979
private boolean incrementRunningTasks() {
80-
int preUpdateValue = runningTasks.getAndUpdate(v -> v < maxRunningTasks ? v + 1 : v);
80+
int preUpdateValue = runningTasks.getAndAccumulate(maxRunningTasks, (v, maxRunning) -> v < maxRunning ? v + 1 : v);
8181
assert preUpdateValue <= maxRunningTasks;
8282
return preUpdateValue < maxRunningTasks;
8383
}

server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,7 +2914,7 @@ public void afterRefresh(boolean didRefresh) {
29142914
}
29152915

29162916
void updateRefreshedCheckpoint(long checkpoint) {
2917-
refreshedCheckpoint.updateAndGet(curr -> Math.max(curr, checkpoint));
2917+
refreshedCheckpoint.accumulateAndGet(checkpoint, Math::max);
29182918
assert refreshedCheckpoint.get() >= checkpoint : refreshedCheckpoint.get() + " < " + checkpoint;
29192919
}
29202920
}
@@ -2931,9 +2931,9 @@ public final void updateMaxUnsafeAutoIdTimestamp(long newTimestamp) {
29312931

29322932
private void updateAutoIdTimestamp(long newTimestamp, boolean unsafe) {
29332933
assert newTimestamp >= -1 : "invalid timestamp [" + newTimestamp + "]";
2934-
maxSeenAutoIdTimestamp.updateAndGet(curr -> Math.max(curr, newTimestamp));
2934+
maxSeenAutoIdTimestamp.accumulateAndGet(newTimestamp, Math::max);
29352935
if (unsafe) {
2936-
maxUnsafeAutoIdTimestamp.updateAndGet(curr -> Math.max(curr, newTimestamp));
2936+
maxUnsafeAutoIdTimestamp.accumulateAndGet(newTimestamp, Math::max);
29372937
}
29382938
assert maxUnsafeAutoIdTimestamp.get() <= maxSeenAutoIdTimestamp.get();
29392939
}
@@ -2949,7 +2949,7 @@ public void advanceMaxSeqNoOfUpdatesOrDeletes(long maxSeqNoOfUpdatesOnPrimary) {
29492949
assert false : "max_seq_no_of_updates on primary is unassigned";
29502950
throw new IllegalArgumentException("max_seq_no_of_updates on primary is unassigned");
29512951
}
2952-
this.maxSeqNoOfUpdatesOrDeletes.updateAndGet(curr -> Math.max(curr, maxSeqNoOfUpdatesOnPrimary));
2952+
this.maxSeqNoOfUpdatesOrDeletes.accumulateAndGet(maxSeqNoOfUpdatesOnPrimary, Math::max);
29532953
}
29542954

29552955
private boolean assertMaxSeqNoOfUpdatesIsAdvanced(Term id, long seqNo, boolean allowDeleted, boolean relaxIfGapInSeqNo) {

server/src/main/java/org/elasticsearch/index/engine/LiveVersionMap.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ public VersionValue remove(BytesRef uid) {
8585
}
8686

8787
public void updateMinDeletedTimestamp(DeleteVersionValue delete) {
88-
long time = delete.time;
89-
minDeleteTimestamp.updateAndGet(prev -> Math.min(time, prev));
88+
minDeleteTimestamp.accumulateAndGet(delete.time, Math::min);
9089
}
9190

9291
}

server/src/main/java/org/elasticsearch/indices/recovery/RecoverySourceHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ protected void executeChunkRequest(OperationChunkRequest request, ActionListener
12061206
retentionLeases,
12071207
mappingVersion,
12081208
listener.delegateFailure((l, newCheckpoint) -> {
1209-
targetLocalCheckpoint.updateAndGet(curr -> SequenceNumbers.max(curr, newCheckpoint));
1209+
targetLocalCheckpoint.accumulateAndGet(newCheckpoint, SequenceNumbers::max);
12101210
l.onResponse(null);
12111211
})
12121212
);

server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ public void updateState(ClusterState state) {
676676
);
677677
}
678678
final long finalBestGen = Math.max(bestGenerationFromCS, metadata.generation());
679-
latestKnownRepoGen.updateAndGet(known -> Math.max(known, finalBestGen));
679+
latestKnownRepoGen.accumulateAndGet(finalBestGen, Math::max);
680680
} else {
681681
final long previousBest = latestKnownRepoGen.getAndSet(metadata.generation());
682682
if (previousBest != metadata.generation()) {
@@ -861,7 +861,7 @@ private RepositoryData safeRepositoryData(long repositoryStateId, Map<String, Bl
861861
final long genToLoad;
862862
final RepositoryData cached;
863863
if (bestEffortConsistency) {
864-
genToLoad = latestKnownRepoGen.updateAndGet(known -> Math.max(known, repositoryStateId));
864+
genToLoad = latestKnownRepoGen.accumulateAndGet(repositoryStateId, Math::max);
865865
cached = null;
866866
} else {
867867
genToLoad = latestKnownRepoGen.get();
@@ -1906,7 +1906,7 @@ private void doGetRepositoryData(ActionListener<RepositoryData> listener) {
19061906
);
19071907
return;
19081908
}
1909-
genToLoad = latestKnownRepoGen.updateAndGet(known -> Math.max(known, generation));
1909+
genToLoad = latestKnownRepoGen.accumulateAndGet(generation, Math::max);
19101910
if (genToLoad > generation) {
19111911
logger.info(
19121912
"Determined repository generation [{}] from repository contents but correct generation must be at " + "least [{}]",

server/src/main/java/org/elasticsearch/search/internal/ReaderContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public Engine.Searcher acquireSearcher(String source) {
111111
}
112112

113113
private void tryUpdateKeepAlive(long keepAlive) {
114-
this.keepAlive.updateAndGet(curr -> Math.max(curr, keepAlive));
114+
this.keepAlive.accumulateAndGet(keepAlive, Math::max);
115115
}
116116

117117
/**

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/store/IndexInputStats.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ public static class Counter {
250250
void add(final long value) {
251251
count.increment();
252252
total.add(value);
253-
min.updateAndGet(prev -> Math.min(prev, value));
254-
max.updateAndGet(prev -> Math.max(prev, value));
253+
min.accumulateAndGet(value, Math::min);
254+
max.accumulateAndGet(value, Math::max);
255255
}
256256

257257
public long count() {

0 commit comments

Comments
 (0)