-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reindex Throttle Checkpointing #46763
Merged
henningandersen
merged 5 commits into
elastic:reindex_v2
from
henningandersen:enhance_reindex_throttle_checkpointing
Sep 24, 2019
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
40d0ad3
Reindex Throttle Checkpointing
henningandersen 3b59d76
Merge remote-tracking branch 'origin/reindex_v2' into enhance_reindex…
henningandersen 16cdb40
Bad whitespace.
henningandersen 9b7eb2a
Preserve thread context
henningandersen eb88eab
Merge remote-tracking branch 'origin/reindex_v2' into enhance_reindex…
henningandersen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
modules/reindex/src/main/java/org/elasticsearch/index/reindex/ThrottlingConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.index.reindex; | ||
|
||
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
import org.elasticsearch.threadpool.Scheduler; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
|
||
import java.util.function.BiConsumer; | ||
import java.util.function.Consumer; | ||
import java.util.function.LongSupplier; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A throttling consumer that forwards input to an outbound consumer, but discarding any that arrive too quickly (but eventually sending the | ||
* last state after the minimumInterval). | ||
* The outbound consumer is only called by a single thread at a time. The outbound consumer should be non-blocking. | ||
* TODO: could be moved to more generic place for reuse? | ||
* @param <T> type of object passed through. | ||
*/ | ||
public class ThrottlingConsumer<T> implements Consumer<T> { | ||
private final ThreadPool threadPool; | ||
private final BiConsumer<T, Runnable> outbound; | ||
private final TimeValue minimumInterval; | ||
private final Object lock = new Object(); | ||
private final LongSupplier nanoTimeSource; | ||
|
||
// state protected by lock. | ||
private long lastWriteTimeNanos; | ||
private T value; | ||
private Scheduler.ScheduledCancellable scheduledWrite; | ||
private boolean outboundActive; | ||
private boolean closed; | ||
private Runnable onClosed; | ||
|
||
public ThrottlingConsumer(BiConsumer<T, Runnable> outbound, TimeValue minimumInterval, | ||
LongSupplier nanoTimeSource, ThreadPool threadPool) { | ||
Supplier<ThreadContext.StoredContext> restorableContext = threadPool.getThreadContext().newRestorableContext(false); | ||
this.outbound = (value, whenDone) -> { | ||
try (ThreadContext.StoredContext ignored = restorableContext.get()) { | ||
outbound.accept(value, whenDone); | ||
} | ||
}; | ||
this.minimumInterval = minimumInterval; | ||
this.threadPool = threadPool; | ||
this.nanoTimeSource = nanoTimeSource; | ||
this.lastWriteTimeNanos = nanoTimeSource.getAsLong(); | ||
} | ||
|
||
@Override | ||
public void accept(T newValue) { | ||
long now = nanoTimeSource.getAsLong(); | ||
synchronized (lock) { | ||
if (closed) { | ||
return; | ||
} | ||
this.value = newValue; | ||
if (scheduledWrite == null) { | ||
// schedule is non-blocking | ||
scheduledWrite = threadPool.schedule(this::onScheduleTimeout, getDelay(now), ThreadPool.Names.SAME); | ||
} | ||
} | ||
} | ||
|
||
private TimeValue getDelay(long now) { | ||
long nanos = lastWriteTimeNanos + minimumInterval.nanos() - now; | ||
return nanos < 0 ? TimeValue.ZERO : TimeValue.timeValueNanos(nanos); | ||
} | ||
|
||
private void onScheduleTimeout() { | ||
T value; | ||
long now = nanoTimeSource.getAsLong(); | ||
synchronized (lock) { | ||
if (closed) { | ||
return; | ||
} | ||
value = this.value; | ||
lastWriteTimeNanos = now; | ||
outboundActive = true; | ||
} | ||
|
||
|
||
outbound.accept(value, () -> { | ||
synchronized (this) { | ||
outboundActive = false; | ||
if (closed == false) { | ||
if (value != this.value) { | ||
scheduledWrite = threadPool.schedule(this::onScheduleTimeout, minimumInterval, | ||
ThreadPool.Names.SAME); | ||
} else { | ||
scheduledWrite = null; | ||
} | ||
} | ||
} | ||
|
||
// safe since onScheduleTimeout is only called single threaded | ||
if (onClosed != null) { | ||
onClosed.run(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Async close this. Any state submitted since last outbound call will be discarded (as well as any new inbound accept calls). | ||
* @param onClosed called when closed, which guarantees no more calls on outbound consumer. Must be non-blocking. | ||
*/ | ||
public void close(Runnable onClosed) { | ||
synchronized (lock) { | ||
assert closed == false : "multiple closes not supported"; | ||
closed = true; | ||
if (scheduledWrite != null) { | ||
if (outboundActive) { | ||
this.onClosed = onClosed; | ||
} else { | ||
scheduledWrite.cancel(); | ||
} | ||
scheduledWrite = null; | ||
} | ||
} | ||
if (this.onClosed == null) { | ||
onClosed.run(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
schedule
loses the thread context. Given that we are afterwards writing to an index (possibly running with security enabled), I wonder if that thread context is relevant.