Skip to content

Commit ea8a9ce

Browse files
committed
Only update a feature's lastUsed time every ten seconds
1 parent 62c84a4 commit ea8a9ce

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/license/XPackLicenseState.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
*/
3434
public class XPackLicenseState {
3535

36+
/**
37+
* For features that are used at high frequency (e.g. per-search or per-document, etc.), we don't want to bother updating
38+
* the 'last used' timestamp more often than some minimum frequency.
39+
*/
40+
private static final long FEATURE_USAGE_MINIMUM_ELAPSED_TIME_MILLIS = TimeValue.timeValueSeconds(10).getMillis();
41+
3642
/** Messages for each feature which are printed when the license expires. */
3743
static final Map<String, String[]> EXPIRATION_MESSAGES;
3844
static {
@@ -447,7 +453,13 @@ public String statusDescription() {
447453

448454
void featureUsed(LicensedFeature feature) {
449455
checkExpiry();
450-
usage.put(new FeatureUsage(feature, null), epochMillisProvider.getAsLong());
456+
// if at least the minimum elapsed time has passed, then update the most recently used time for this feature
457+
final FeatureUsage feat = new FeatureUsage(feature, null);
458+
final long now = epochMillisProvider.getAsLong();
459+
final Long mostRecent = usage.get(feat);
460+
if (mostRecent == null || now - mostRecent >= FEATURE_USAGE_MINIMUM_ELAPSED_TIME_MILLIS) {
461+
usage.put(feat, now);
462+
}
451463
}
452464

453465
void enableUsageTracking(LicensedFeature feature, String contextName) {

x-pack/plugin/core/src/test/java/org/elasticsearch/license/XPackLicenseStateTests.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,18 @@ public void testLastUsedMomentaryFeature() {
239239
assertThat(usage.contextName(), nullValue());
240240
assertThat(lastUsed.get(usage), equalTo(100L));
241241

242-
currentTime.set(200);
242+
// updates to the last used timestamp only happen if at least ten seconds have passed since the last update (*for this feature*)
243+
currentTime.set(10099);
243244
goldFeature.check(licenseState);
244245
lastUsed = licenseState.getLastUsed();
245246
assertThat("feature.check updates usage", lastUsed.keySet(), containsInAnyOrder(usage));
246-
assertThat(lastUsed.get(usage), equalTo(200L));
247+
assertThat(lastUsed.get(usage), equalTo(100L));
248+
249+
currentTime.set(10100);
250+
goldFeature.check(licenseState);
251+
lastUsed = licenseState.getLastUsed();
252+
assertThat("feature.check updates usage", lastUsed.keySet(), containsInAnyOrder(usage));
253+
assertThat(lastUsed.get(usage), equalTo(10100L));
247254
}
248255

249256
public void testLastUsedMomentaryFeatureWithSameNameDifferentFamily() {
@@ -270,7 +277,7 @@ public void testLastUsedMomentaryFeatureWithSameNameDifferentFamily() {
270277
)
271278
);
272279

273-
currentTime.set(200);
280+
currentTime.set(10200);
274281
featureFamilyB.check(licenseState);
275282

276283
lastUsed = licenseState.getLastUsed();
@@ -283,7 +290,7 @@ public void testLastUsedMomentaryFeatureWithSameNameDifferentFamily() {
283290
actualFeatures,
284291
containsInAnyOrder(
285292
new FeatureInfoWithTimestamp("familyA", "goldFeature", 100L),
286-
new FeatureInfoWithTimestamp("familyB", "goldFeature", 200L)
293+
new FeatureInfoWithTimestamp("familyB", "goldFeature", 10200L)
287294
)
288295
);
289296
}

0 commit comments

Comments
 (0)