Skip to content

Instantly share code, notes, and snippets.

@jaymode
Created February 18, 2014 16:20
Show Gist options
  • Save jaymode/9074150 to your computer and use it in GitHub Desktop.
Save jaymode/9074150 to your computer and use it in GitHub Desktop.
Elasticsearch SearchContext and Reaper patch
diff --git a/src/main/java/org/elasticsearch/search/SearchService.java b/src/main/java/org/elasticsearch/search/SearchService.java
index 17374db..5bf46de 100644
--- a/src/main/java/org/elasticsearch/search/SearchService.java
+++ b/src/main/java/org/elasticsearch/search/SearchService.java
@@ -503,6 +503,9 @@ public class SearchService extends AbstractLifecycleComponent<SearchService> {
keepAlive = request.scroll().keepAlive().millis();
}
context.keepAlive(keepAlive);
+
+ // call accessed to prevent reaper seeing 0 as the last accessed time
+ context.accessed(threadPool.estimatedTimeInMillis());
} catch (Throwable e) {
context.release();
throw ExceptionsHelper.convertToRuntime(e);
@@ -833,10 +836,14 @@ public class SearchService extends AbstractLifecycleComponent<SearchService> {
public void run() {
long time = threadPool.estimatedTimeInMillis();
for (SearchContext context : activeContexts.values()) {
- if (context.lastAccessTime() == -1) { // its being processed or timeout is disabled
+ // Use the same value for both checks since lastAccessTime can
+ // be modified by another thread between checks!
+ long lastAccessTime = context.lastAccessTime();
+ if (lastAccessTime == -1l) { // its being processed or timeout is disabled
continue;
}
- if ((time - context.lastAccessTime() > context.keepAlive())) {
+ if ((time - lastAccessTime > context.keepAlive())) {
+ logger.debug("freeing search context {} time: {} lastAccessTime: {} keepAlive: {}", context.id(), time, lastAccessTime, context.keepAlive());
freeContext(context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment