-
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
EQL: Optimize string retention #66207
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,9 @@ | |
import org.elasticsearch.xpack.ql.util.ActionListeners; | ||
|
||
import java.util.Iterator; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.elasticsearch.action.ActionListener.wrap; | ||
import static org.elasticsearch.xpack.eql.execution.search.RuntimeUtils.searchHits; | ||
|
@@ -47,8 +49,23 @@ | |
*/ | ||
public class TumblingWindow implements Executable { | ||
|
||
private static final int CACHE_MAX_SIZE = 64; | ||
|
||
private final Logger log = LogManager.getLogger(TumblingWindow.class); | ||
|
||
/** | ||
* Simple cache for removing duplicate strings (such as index name or common keys). | ||
* Designed to be low-effort, non-concurrent (not needed) and thus optimistic in nature. | ||
* Thus it has a small, upper limit so that it doesn't require any cleaning up. | ||
*/ | ||
// start with the default size and allow growth until the max size | ||
private final Map<String, String> stringCache = new LinkedHashMap<>(16, 0.75f, true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One things to consider: Start with the CACHE_MAX_SIZE instead of the default size ( |
||
@Override | ||
protected boolean removeEldestEntry(Map.Entry<String, String> eldest) { | ||
return this.size() >= CACHE_MAX_SIZE; | ||
} | ||
}; | ||
|
||
private final QueryClient client; | ||
private final List<Criterion<BoxedQueryRequest>> criteria; | ||
private final Criterion<BoxedQueryRequest> until; | ||
|
@@ -522,6 +539,28 @@ private TimeValue timeTook() { | |
return new TimeValue(System.currentTimeMillis() - startTime); | ||
} | ||
|
||
private String cache(String string) { | ||
String value = stringCache.putIfAbsent(string, string); | ||
return value == null ? string : value; | ||
} | ||
|
||
private SequenceKey key(Object[] keys) { | ||
SequenceKey key; | ||
if (keys == null) { | ||
key = SequenceKey.NONE; | ||
} else { | ||
for (int i = 0; i < keys.length; i++) { | ||
Object o = keys[i]; | ||
if (o instanceof String) { | ||
keys[i] = cache((String) o); | ||
} | ||
} | ||
key = new SequenceKey(keys); | ||
} | ||
|
||
return key; | ||
} | ||
|
||
private static Ordinal headOrdinal(List<SearchHit> hits, Criterion<BoxedQueryRequest> criterion) { | ||
return criterion.ordinal(hits.get(0)); | ||
} | ||
|
@@ -565,9 +604,9 @@ public boolean hasNext() { | |
@Override | ||
public Tuple<KeyAndOrdinal, HitReference> next() { | ||
SearchHit hit = delegate.next(); | ||
SequenceKey k = criterion.key(hit); | ||
SequenceKey k = key(criterion.key(hit)); | ||
Ordinal o = criterion.ordinal(hit); | ||
return new Tuple<>(new KeyAndOrdinal(k, o), new HitReference(hit)); | ||
return new Tuple<>(new KeyAndOrdinal(k, o), new HitReference(cache(hit.getIndex()), hit.getId())); | ||
} | ||
}; | ||
}; | ||
|
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.
Since we have a
Cache
class in the common lib, which is more complex and supports concurrency, could we have a comment here that concurrency is not needed?