Skip to content

Commit c92575c

Browse files
authored
Limit frequency of feature last-used time updates (#133004) (#133170)
1 parent ca3e8e2 commit c92575c

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

docs/changelog/133004.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 133004
2+
summary: Limit frequency of feature last-used time updates
3+
area: License
4+
type: bug
5+
issues: []

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,13 @@ public String statusDescription() {
447447

448448
void featureUsed(LicensedFeature feature) {
449449
checkExpiry();
450-
usage.put(new FeatureUsage(feature, null), epochMillisProvider.getAsLong());
450+
final long now = epochMillisProvider.getAsLong();
451+
final FeatureUsage feat = new FeatureUsage(feature, null);
452+
final Long mostRecent = usage.get(feat);
453+
// only update if needed, to prevent ConcurrentHashMap lock-contention on writes
454+
if (mostRecent == null || now > mostRecent) {
455+
usage.put(feat, now);
456+
}
451457
}
452458

453459
void enableUsageTracking(LicensedFeature feature, String contextName) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ public void testLastUsedMomentaryFeature() {
244244
lastUsed = licenseState.getLastUsed();
245245
assertThat("feature.check updates usage", lastUsed.keySet(), containsInAnyOrder(usage));
246246
assertThat(lastUsed.get(usage), equalTo(200L));
247+
248+
// updates to the last used timestamp only happen if the time has increased
249+
currentTime.set(199);
250+
goldFeature.check(licenseState);
251+
lastUsed = licenseState.getLastUsed();
252+
assertThat("feature.check updates usage", lastUsed.keySet(), containsInAnyOrder(usage));
253+
assertThat(lastUsed.get(usage), equalTo(200L));
247254
}
248255

249256
public void testLastUsedMomentaryFeatureWithSameNameDifferentFamily() {

0 commit comments

Comments
 (0)