-
Notifications
You must be signed in to change notification settings - Fork 127
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
Remove latestSettings cache from KNNSettings #727
Conversation
Codecov Report
@@ Coverage Diff @@
## main #727 +/- ##
============================================
+ Coverage 84.43% 84.60% +0.16%
+ Complexity 1072 1069 -3
============================================
Files 152 152
Lines 4356 4364 +8
Branches 389 390 +1
============================================
+ Hits 3678 3692 +14
+ Misses 498 492 -6
Partials 180 180
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ |
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.
please fix license header to 2-lines format
NativeMemoryCacheManager.getInstance().rebuildCache(); | ||
}); | ||
} | ||
ByteSizeValue maxCacheWeight = getSettingValue(KNN_MEMORY_CIRCUIT_BREAKER_LIMIT); |
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.
nit: can we change maxCacheWeight type to long, this way it's gonna be consistent with expiryTimeInMinutes setting. now we do have both primitive object and getter of complex. seems maxCacheWeight it's not used anywhere else in this method so ByteSizeValue type is not required
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.
Sure, will update.
executor.execute(() -> { | ||
cache.invalidateAll(); | ||
initialize(); | ||
if (cache != null) { |
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.
curious did we have an NPE with cache
var or it's more of being on a safe side?
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.
I dont think this can be reached. The only time the cache will be null will be if rebuildCache is called before the constructor, but I dont think this can happen. I think Ill remove this.
); | ||
} | ||
|
||
private void initialize(boolean isWeightLimited, long maxWeight, boolean isExpirationLimited, long expiryTimeInMin) { |
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.
nit: I suggest we use DTO instead of 4 arguments. It's will increase maintainability for future changes if we need to add/remove settings, especially as we can re-use DTO in multiple places, e.g. rebuildCache, tests etc.
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.
Sure, will update
public class KNNSettingsTests extends KNNTestCase { | ||
|
||
@SneakyThrows | ||
public void testGetSettingValueFromConfig() { |
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.
I assume that would fail without changes in rebuild cache? do we need to include other settings as well?
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.
Not sure I understand what you mean by "that would fail without changes in rebuild cache". Could you elaborate?
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.
I mean is this test, if it exists before your PR, fail without you code change? And second question is about settings, we're checking here only for KNNSettings.KNN_MEMORY_CIRCUIT_BREAKER_LIMIT, but we have other settings like KNNSettings.KNN_CACHE_ITEM_EXPIRY_ENABLED and KNNSettings.KNN_CACHE_ITEM_EXPIRY_TIME_MINUTES, do we need to have similar test for them as well?
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.
I mean is this test, if it exists before your PR, fail without you code change?
Yes, this was the failure before the fix:
2> REPRODUCE WITH: ./gradlew ':test' --tests "org.opensearch.knn.index.KNNSettingsTests.testGetSettingValueFromConfig" -Dtests.seed=2B1E0A6CFDBD324B -Dtests.security.manager=false -Dtests.locale=fr -Dtests.timezone=America/Paramaribo -Druntime.java=11
2> java.lang.AssertionError: expected:<8126464> but was:<13>
at __randomizedtesting.SeedInfo.seed([2B1E0A6CFDBD324B:4423BA039518D35]:0)
at org.junit.Assert.fail(Assert.java:89)
at org.junit.Assert.failNotEquals(Assert.java:835)
at org.junit.Assert.assertEquals(Assert.java:647)
at org.junit.Assert.assertEquals(Assert.java:633)
I think my actual and expected are mixed up, but Ill go ahead and fix this.
And second question is about settings, we're checking here only for KNNSettings.KNN_MEMORY_CIRCUIT_BREAKER_LIMIT, but we have other settings like KNNSettings.KNN_CACHE_ITEM_EXPIRY_ENABLED and KNNSettings.KNN_CACHE_ITEM_EXPIRY_TIME_MINUTES, do we need to have similar test for them as well?
I think it is probably okay to test just one. We group all of the dynamic cache settings here: https://github.com/opensearch-project/k-NN/blob/main/src/main/java/org/opensearch/knn/index/KNNSettings.java#L223 and then dont handle them uniquely from there, so I think we are okay.
The better explanation for this as per the deep-dive done was config(from .yml) gets set at the start, before any functions can be registered. Hence the .yml file config will not trigger the settings update, as these are updates. |
Removes the latestSettings cache from the KNNSettings class. latestSettings cache gets updated in a consumer when the particular settings are updated. KNNSettings.getSettingValue would pull from this cache and then fallback to the default if it is not present. However, in the case when the settings are set via opensearch.yml config file, the settings update consumers never get called, so the config values never get put in the cache. This leads to getSettingValue to always return the default instead of the value specified in the config file. To fix this, this change refactors getSettingValue to pull from the cluster settings and removes the latestSetting cache. However, because the dynamicCacheSettings have consumers that rebuild the NativeMemoryCacheManager cache when they are changed, the logic for passing the parameters to rebuild this cache had to change as well. Signed-off-by: John Mazanec <[email protected]>
Addresses review comments. Switches configuration of cache manager to use DTO. Signed-off-by: John Mazanec <[email protected]>
Signed-off-by: John Mazanec <[email protected]>
@navneet1v true, I updated the PR description to reflect this. |
@@ -30,6 +41,18 @@ public static void resetState() { | |||
knnCounter.set(0L); | |||
} | |||
|
|||
ClusterService clusterService = mock(ClusterService.class); |
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.
can we @mock annotation and mock this
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.
Sure, will update.
@Getter | ||
@Builder |
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.
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.
Makes sense. Will add.
Minor comments overall code looks good to me. |
Signed-off-by: John Mazanec <[email protected]>
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.
looks good, thank you!
Removes the latestSettings cache from the KNNSettings class. latestSettings cache gets updated in a consumer when the particular settings are updated. KNNSettings.getSettingValue would pull from this cache and then fallback to the default if it is not present. However, in the case when the settings are set via opensearch.yml config file, the settings update consumers never get called, so the config values never get put in the cache. This leads to getSettingValue to always return the default instead of the value specified in the config file. To fix this, this change refactors getSettingValue to pull from the cluster settings and removes the latestSetting cache. However, because the dynamicCacheSettings have consumers that rebuild the NativeMemoryCacheManager cache when they are changed, the logic for passing the parameters to rebuild this cache had to change as well. Also, switches configuration of cache manager to use DTO. Signed-off-by: John Mazanec <[email protected]> (cherry picked from commit 8e2ad45)
Description
Removes the latestSettings cache from the KNNSettings class. latestSettings cache gets updated in a consumer when the particular settings are updated.
KNNSettings.getSettingValue would pull from this cache and then fallback to the default if it is not present. However, in the case when the settings are set via opensearch.yml config file, the settings get set before any functions can be registered. So the config values never get put in the cache. This leads to getSettingValue to always return the default instead of the value specified in the config file.
To fix this, this change refactors getSettingValue to pull from the cluster settings and removes the latestSetting cache. However, because the dynamicCacheSettings have consumers that rebuild the NativeMemoryCacheManager cache when they are changed, the logic for passing the parameters to rebuild this cache had to change as well. Initially, the NativeMemoryCacheManager gets its parameters from the settings. However, because we are switching "getSettingValue" to get the values from the ClusterSettings, the new cluster settings will not yet be committed to the cluster state when the settings update consumer is called (it gets called in this chain starting here). To workaround this, this change refactors the cache to accept parameters as args.
In addition to this, I added a couple tests units tests and did some refactoring to get tests to pass.
Issues Resolved
#585
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.