-
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
Cache the creation of parsers within DateProcessor
#92880
Changes from 4 commits
37d1239
0426778
b8281f7
d3eb4cd
c004397
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 92880 | ||
summary: Cache the creation of parsers within DateProcessor | ||
area: Ingest Node | ||
type: enhancement | ||
issues: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,10 @@ | |
package org.elasticsearch.ingest.common; | ||
|
||
import org.elasticsearch.ExceptionsHelper; | ||
import org.elasticsearch.common.settings.SettingsException; | ||
import org.elasticsearch.common.time.DateFormatter; | ||
import org.elasticsearch.common.util.LocaleUtils; | ||
import org.elasticsearch.common.util.concurrent.ConcurrentCollections; | ||
import org.elasticsearch.core.Nullable; | ||
import org.elasticsearch.ingest.AbstractProcessor; | ||
import org.elasticsearch.ingest.ConfigurationUtils; | ||
|
@@ -19,18 +21,32 @@ | |
import org.elasticsearch.script.ScriptService; | ||
import org.elasticsearch.script.TemplateScript; | ||
|
||
import java.lang.ref.SoftReference; | ||
import java.time.ZoneId; | ||
import java.time.ZoneOffset; | ||
import java.time.ZonedDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
public final class DateProcessor extends AbstractProcessor { | ||
|
||
public static final String TYPE = "date"; | ||
private static final String CACHE_CAPACITY_SETTING = "es.ingest.date_processor.cache_capacity"; | ||
private static final Cache CACHE; | ||
|
||
static { | ||
var cacheSizeStr = System.getProperty(CACHE_CAPACITY_SETTING, "256"); | ||
try { | ||
CACHE = new Cache(Integer.parseInt(cacheSizeStr)); | ||
} catch (NumberFormatException e) { | ||
throw new SettingsException("{} must be a valid number but was [{}]", CACHE_CAPACITY_SETTING, cacheSizeStr); | ||
} | ||
} | ||
static final String DEFAULT_TARGET_FIELD = "@timestamp"; | ||
static final String DEFAULT_OUTPUT_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; | ||
|
||
|
@@ -72,9 +88,17 @@ public final class DateProcessor extends AbstractProcessor { | |
this.targetField = targetField; | ||
this.formats = formats; | ||
this.dateParsers = new ArrayList<>(this.formats.size()); | ||
|
||
for (String format : formats) { | ||
DateFormat dateFormat = DateFormat.fromString(format); | ||
dateParsers.add((params) -> dateFormat.getFunction(format, newDateTimeZone(params), newLocale(params))); | ||
dateParsers.add((params) -> { | ||
var documentZoneId = newDateTimeZone(params); | ||
var documentLocale = newLocale(params); | ||
return CACHE.getOrCompute( | ||
new Cache.Key(format, documentZoneId, documentLocale), | ||
() -> dateFormat.getFunction(format, documentZoneId, documentLocale) | ||
); | ||
}); | ||
} | ||
this.outputFormat = outputFormat; | ||
formatter = DateFormatter.forPattern(this.outputFormat); | ||
|
@@ -198,4 +222,34 @@ public DateProcessor create( | |
); | ||
} | ||
} | ||
|
||
static final class Cache { | ||
private final ConcurrentMap<Key, SoftReference<Function<String, ZonedDateTime>>> map; | ||
private final int capacity; | ||
|
||
Cache(int capacity) { | ||
if (capacity <= 0) { | ||
throw new IllegalArgumentException("cache capacity must be a value greater than 0 but was " + capacity); | ||
} | ||
this.capacity = capacity; | ||
this.map = ConcurrentCollections.newConcurrentMapWithAggressiveConcurrency(this.capacity); | ||
} | ||
|
||
Function<String, ZonedDateTime> getOrCompute(Key key, Supplier<Function<String, ZonedDateTime>> supplier) { | ||
Function<String, ZonedDateTime> fn; | ||
var element = map.get(key); | ||
// element exist and wasn't GCed | ||
if (element != null && (fn = element.get()) != null) { | ||
return fn; | ||
} | ||
if (map.size() >= capacity) { | ||
map.clear(); | ||
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. This is kind of surprising. Whenever the cache is full, we empty it and start over? Is this to avoid the complexity of a concurrent LRU cache? Or is the idea that we'll rarely see more than 256 formats in practice, so this is just for safety and will rarely be hit? 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. Yes! this decision was made mainly because we want to avoid the complexity of LRU (or any eviction policy) within this code path. The decision to make the size 256 was primarily based on experiments with different benchmarks. 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. I think it's worth adding a one line comment in the code above the clear to explain its purpose. |
||
} | ||
fn = supplier.get(); | ||
map.put(key, new SoftReference<>(fn)); | ||
return fn; | ||
} | ||
|
||
record Key(String format, ZoneId zoneId, Locale locale) {} | ||
} | ||
} |
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.
What's the reason for doing this as a system property instead of a Settings thing (and related, are we documenting this setting)?
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.
We don't expect users will have to change this value unless they see a big performance effect on their pipelines. In the opposite case, we wanted to provide an easy way to change the value if we receive an SDH (with the obvious memory consumption trade-off). I inspired by this other PR#80902 by @joegallo
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.
+1,
git grep 'System.getProperty("es' | grep -vi test/
shows a good number of examples of this as the pattern for non-customer-facing internal knobs that we don't expect to need to twiddle (but that we're making possible to twiddle as a favor to our future selves).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.
OK, just wanted to make sure it was intentional.