-
Notifications
You must be signed in to change notification settings - Fork 25k
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
[INGEST] Interrupt the current thread if evaluation grok expressions take too long #31024
Merged
martijnvg
merged 6 commits into
elastic:master
from
martijnvg:ingest_hanging_grok_expressions
Jun 12, 2018
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
168d471
[INGEST] Interrupt the current thread if evaluation grok expressions …
martijnvg d90f38b
iter
martijnvg 281ca0e
changed defaults and added docs
martijnvg 92737df
iter2
martijnvg c21a408
rename method
martijnvg f9d20c3
Merge remote-tracking branch 'es/master' into ingest_hanging_grok_exp…
martijnvg 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
148 changes: 148 additions & 0 deletions
148
libs/grok/src/main/java/org/elasticsearch/grok/ThreadWatchdog.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,148 @@ | ||
/* | ||
* 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.grok; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.function.BiFunction; | ||
import java.util.function.LongSupplier; | ||
|
||
/** | ||
* Protects against long running operations that happen between the register and unregister invocations. | ||
* Threads that invoke {@link #register()}, but take too long to invoke the {@link #unregister()} method | ||
* will be interrupted. | ||
* | ||
* This is needed for Joni's {@link org.joni.Matcher#search(int, int, int)} method, because | ||
* it can end up spinning endlessly if the regular expression is too complex. Joni has checks | ||
* that for every 30k iterations it checks if the current thread is interrupted and if so | ||
* returns {@link org.joni.Matcher#INTERRUPTED}. | ||
*/ | ||
public interface ThreadWatchdog { | ||
|
||
/** | ||
* Registers the current thread and interrupts the current thread | ||
* if the takes too long for this thread to invoke {@link #unregister()}. | ||
*/ | ||
void register(); | ||
|
||
/** | ||
* @return The maximum allowed time in milliseconds for a thread to invoke {@link #unregister()} | ||
* after {@link #register()} has been invoked before this ThreadWatchDog starts to interrupting that thread. | ||
*/ | ||
long maxExecutionTimeInMillis(); | ||
|
||
/** | ||
* Unregisters the current thread and prevents it from being interrupted. | ||
*/ | ||
void unregister(); | ||
|
||
/** | ||
* Returns an implementation that checks for each fixed interval if there are threads that have invoked {@link #register()} | ||
* and not {@link #unregister()} and have been in this state for longer than the specified max execution interval and | ||
* then interrupts these threads. | ||
* | ||
* @param interval The fixed interval to check if there are threads to interrupt | ||
* @param maxExecutionTime The time a thread has the execute an operation. | ||
* @param relativeTimeSupplier A supplier that returns relative time | ||
* @param scheduler A scheduler that is able to execute a command for each fixed interval | ||
*/ | ||
static ThreadWatchdog newInstance(long interval, | ||
long maxExecutionTime, | ||
LongSupplier relativeTimeSupplier, | ||
BiFunction<Long, Runnable, ScheduledFuture<?>> scheduler) { | ||
return new Default(interval, maxExecutionTime, relativeTimeSupplier, scheduler); | ||
} | ||
|
||
/** | ||
* @return A noop implementation that does not interrupt threads and is useful for testing and pre-defined grok expressions. | ||
*/ | ||
static ThreadWatchdog noop() { | ||
return Noop.INSTANCE; | ||
} | ||
|
||
class Noop implements ThreadWatchdog { | ||
|
||
private static final Noop INSTANCE = new Noop(); | ||
|
||
private Noop() { | ||
} | ||
|
||
@Override | ||
public void register() { | ||
} | ||
|
||
@Override | ||
public long maxExecutionTimeInMillis() { | ||
return Long.MAX_VALUE; | ||
} | ||
|
||
@Override | ||
public void unregister() { | ||
} | ||
} | ||
|
||
class Default implements ThreadWatchdog { | ||
|
||
private final long interval; | ||
private final long maxExecutionTime; | ||
private final LongSupplier relativeTimeSupplier; | ||
private final BiFunction<Long, Runnable, ScheduledFuture<?>> scheduler; | ||
final ConcurrentHashMap<Thread, Long> registry = new ConcurrentHashMap<>(); | ||
|
||
private Default(long interval, | ||
long maxExecutionTime, | ||
LongSupplier relativeTimeSupplier, | ||
BiFunction<Long, Runnable, ScheduledFuture<?>> scheduler) { | ||
this.interval = interval; | ||
this.maxExecutionTime = maxExecutionTime; | ||
this.relativeTimeSupplier = relativeTimeSupplier; | ||
this.scheduler = scheduler; | ||
scheduler.apply(interval, this::interruptLongRunningExecutions); | ||
} | ||
|
||
public void register() { | ||
Long previousValue = registry.put(Thread.currentThread(), relativeTimeSupplier.getAsLong()); | ||
assert previousValue == null; | ||
} | ||
|
||
@Override | ||
public long maxExecutionTimeInMillis() { | ||
return maxExecutionTime; | ||
} | ||
|
||
public void unregister() { | ||
Long previousValue = registry.remove(Thread.currentThread()); | ||
assert previousValue != null; | ||
} | ||
|
||
private void interruptLongRunningExecutions() { | ||
final long currentRelativeTime = relativeTimeSupplier.getAsLong(); | ||
for (Map.Entry<Thread, Long> entry : registry.entrySet()) { | ||
if ((currentRelativeTime - entry.getValue()) > maxExecutionTime) { | ||
entry.getKey().interrupt(); | ||
// not removing the entry here, this happens in the unregister() method. | ||
} | ||
} | ||
scheduler.apply(interval, this::interruptLongRunningExecutions); | ||
} | ||
|
||
} | ||
|
||
} |
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.
I think this should not happen so we can rethrow this as an
AssertionError
?