-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[WLM] Make resource usage value cacheable for NodeDuressTrackers #18649
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
Merged
jainankitk
merged 10 commits into
opensearch-project:main
from
kaushalmahi12:node_duress
Jul 1, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6913322
add CHANGELOG
kaushalmahi12 4167dff
make node duress cacheable
kaushalmahi12 7919411
add CHANGELOG
kaushalmahi12 a947183
remove duplicate changelog
kaushalmahi12 7d6e809
fix UTs
kaushalmahi12 3f7d996
move updateCache at resource level
kaushalmahi12 13ab487
move updateCache at resource level
kaushalmahi12 649a6a4
apply spotless check
kaushalmahi12 79322d0
fix UTs
kaushalmahi12 8b9721d
Merge branch 'main' into node_duress
jainankitk 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 hidden or 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
45 changes: 45 additions & 0 deletions
45
server/src/main/java/org/opensearch/common/util/TimeBasedExpiryTracker.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.common.util; | ||
|
|
||
| import java.util.function.BooleanSupplier; | ||
| import java.util.function.LongSupplier; | ||
|
|
||
| /** | ||
| * This class can be utilised to track the time based expiration events. | ||
| * Clients should be more cautious with the expiry time as this class is not completely thread safe. This is intentional | ||
| * as nanoSecond granularity level error for `lastAccessTimeInNanos` is tolerable and can be ignored. | ||
| * @opensearch.internal | ||
| */ | ||
| public class TimeBasedExpiryTracker implements BooleanSupplier { | ||
| private final LongSupplier nanoTimeSupplier; | ||
| private volatile long lastAccessTimeInNanos; | ||
| private final long expiryTimeInNanos; | ||
| private static final long ONE_SEC = 1000_000_000; | ||
|
|
||
| public TimeBasedExpiryTracker(LongSupplier nanoTimeSupplier) { | ||
| this(nanoTimeSupplier, ONE_SEC); | ||
| } | ||
|
|
||
| public TimeBasedExpiryTracker(LongSupplier nanoTimeSupplier, long expiryTimeInNanos) { | ||
| this.nanoTimeSupplier = nanoTimeSupplier; | ||
| this.lastAccessTimeInNanos = nanoTimeSupplier.getAsLong(); | ||
| this.expiryTimeInNanos = expiryTimeInNanos; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean getAsBoolean() { | ||
| final long currentTime = nanoTimeSupplier.getAsLong(); | ||
| final boolean isExpired = (currentTime - lastAccessTimeInNanos) > expiryTimeInNanos; | ||
| if (isExpired) { | ||
| lastAccessTimeInNanos = currentTime; | ||
| } | ||
| return isExpired; | ||
| } | ||
| } |
This file contains hidden or 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
47 changes: 47 additions & 0 deletions
47
server/src/test/java/org/opensearch/common/util/TimeBasedCacheExpiryTrackerTests.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.common.util; | ||
|
|
||
| import org.opensearch.test.OpenSearchTestCase; | ||
|
|
||
| import java.util.function.LongSupplier; | ||
|
|
||
| public class TimeBasedCacheExpiryTrackerTests extends OpenSearchTestCase { | ||
| TimeBasedExpiryTracker sut; | ||
| final long ONE_SEC = 1_000_000_000; | ||
|
|
||
| public void testExpiryEvent() { | ||
| TestTimeSupplier testTimeSupplier = new TestTimeSupplier(); | ||
| sut = new TimeBasedExpiryTracker(testTimeSupplier); | ||
|
|
||
| testTimeSupplier.advanceClockBy(2 * ONE_SEC); | ||
| assertTrue(sut.getAsBoolean()); | ||
| } | ||
|
|
||
| public void testNonExpiryEvent() { | ||
| TestTimeSupplier testTimeSupplier = new TestTimeSupplier(); | ||
| sut = new TimeBasedExpiryTracker(testTimeSupplier); | ||
|
|
||
| testTimeSupplier.advanceClockBy(ONE_SEC / 2); | ||
| assertFalse(sut.getAsBoolean()); | ||
| } | ||
|
|
||
| public static class TestTimeSupplier implements LongSupplier { | ||
| long currentTime = System.nanoTime(); | ||
|
|
||
| @Override | ||
| public long getAsLong() { | ||
| return currentTime; | ||
| } | ||
|
|
||
| public void advanceClockBy(long nanos) { | ||
| currentTime += nanos; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.