Skip to content

Commit 5e1b508

Browse files
AtomicLong for merge start time
1 parent a9f4823 commit 5e1b508

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.apache.lucene.store.IOContext;
2020
import org.apache.lucene.store.IndexOutput;
2121
import org.apache.lucene.store.RateLimitedIndexOutput;
22-
import org.apache.lucene.util.SetOnce;
2322
import org.elasticsearch.common.logging.Loggers;
2423
import org.elasticsearch.common.settings.Setting;
2524
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
@@ -231,15 +230,15 @@ public IndexOutput createOutput(String name, IOContext context) throws IOExcepti
231230

232231
final class MergeTask extends AbstractRunnable implements Comparable<MergeTask> {
233232
private final String name;
234-
private final SetOnce<Long> mergeStartTimeNS;
233+
private final AtomicLong mergeStartTimeNS;
235234
private final MergeSource mergeSource;
236235
private final OnGoingMerge onGoingMerge;
237236
private final MergeRateLimiter rateLimiter;
238237
private final boolean supportsIOThrottling;
239238

240239
MergeTask(MergeSource mergeSource, MergePolicy.OneMerge merge, boolean supportsIOThrottling, String name) {
241240
this.name = name;
242-
this.mergeStartTimeNS = new SetOnce<>();
241+
this.mergeStartTimeNS = new AtomicLong();
243242
this.mergeSource = mergeSource;
244243
this.onGoingMerge = new OnGoingMerge(merge);
245244
this.rateLimiter = new MergeRateLimiter(merge.getMergeProgress());
@@ -268,15 +267,14 @@ public void setIORateLimit(double mbPerSec) {
268267
}
269268

270269
public boolean isRunning() {
271-
return mergeStartTimeNS.get() != null;
270+
return mergeStartTimeNS.get() > 0L;
272271
}
273272

274273
@Override
275274
public void doRun() throws Exception {
276-
if (isRunning()) {
275+
if (!mergeStartTimeNS.compareAndSet(0L, System.nanoTime())) {
277276
throw new IllegalStateException("Cannot run the same merge task multiple times");
278277
}
279-
mergeStartTimeNS.set(System.nanoTime());
280278
try {
281279
beforeMerge(onGoingMerge);
282280
mergeTracking.mergeStarted(onGoingMerge);
@@ -351,7 +349,6 @@ public void onFailure(Exception e) {
351349
if (isRunning() == false) {
352350
throw new IllegalStateException("onFailure must only be invoked after doRun");
353351
}
354-
assert this.mergeStartTimeNS.get() != null : "onFailure should always be invoked after doRun";
355352
// most commonly the merge should've already be aborted by now,
356353
// plus the engine is probably going to be failed when any merge fails,
357354
// but keep this in case something believes calling `MergeTask#onFailure` is a sane way to abort a merge

0 commit comments

Comments
 (0)