Skip to content

Commit

Permalink
Give SearchContext a toString
Browse files Browse the repository at this point in the history
and move the string capturing to capture time.
  • Loading branch information
nik9000 committed Apr 11, 2016
1 parent ac94e5f commit 525ce40
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,16 @@ public enum Lifetime {

public abstract QueryShardContext getQueryShardContext();

@Override
public String toString() {
StringBuilder result = new StringBuilder().append(shardTarget());
if (searchType() != SearchType.DEFAULT) {
result.append("searchType=[").append(searchType()).append("]");
}
if (scrollContext() != null) {
result.append("scroll=[").append(scrollContext().scroll.keepAlive()).append("]");
}
result.append(" query=[").append(query()).append("]");
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.search;

import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.cache.recycler.PageCacheRecycler;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Inject;
Expand Down Expand Up @@ -62,30 +61,18 @@ public void onModule(SearchModule module) {
public static void assertNoInFlightContext() {
final Map<SearchContext, Throwable> copy = new HashMap<>(ACTIVE_SEARCH_CONTEXTS);
if (copy.isEmpty() == false) {
Map.Entry<SearchContext, Throwable> firstOpen = copy.entrySet().iterator().next();
SearchContext context = firstOpen.getKey();
StringBuilder message = new StringBuilder().append(context.shardTarget());
if (context.searchType() != SearchType.DEFAULT) {
message.append("searchType=[").append(context.searchType()).append("]");
}
if (context.scrollContext() != null) {
message.append("scroll=[").append(context.scrollContext().scroll.keepAlive()).append("]");
}
message.append(" query=[").append(context.query()).append("]");
RuntimeException cause = new RuntimeException(message.toString());
cause.setStackTrace(firstOpen.getValue().getStackTrace());
throw new AssertionError(
"There are still " + copy.size()
+ " in-flight contexts. The first one's creation site is listed as the cause of this exception.",
cause);
"There are still [" + copy.size()
+ "] in-flight contexts. The first one's creation site is listed as the cause of this exception.",
copy.values().iterator().next());
}
}

/**
* Add an active search context to the list of tracked contexts. Package private for testing.
*/
static void addActiveContext(SearchContext context) {
ACTIVE_SEARCH_CONTEXTS.put(context, new RuntimeException());
ACTIVE_SEARCH_CONTEXTS.put(context, new RuntimeException(context.toString()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Query query() {
MockSearchService.addActiveContext(s);
try {
Throwable e = expectThrows(AssertionError.class, () -> MockSearchService.assertNoInFlightContext());
assertEquals("There are still 1 in-flight contexts. The first one's creation site is listed as the cause of this exception.",
assertEquals("There are still [1] in-flight contexts. The first one's creation site is listed as the cause of this exception.",
e.getMessage());
e = e.getCause();
// The next line with throw an exception if the date looks wrong
Expand Down

0 comments on commit 525ce40

Please sign in to comment.