From 3069351bde456564ea059d988cd4f739e825d636 Mon Sep 17 00:00:00 2001 From: Jack Mazanec Date: Tue, 8 Sep 2020 10:28:02 -0700 Subject: [PATCH] Fix casting issue with cache expiration during cache initialization (#215) --- .../knn/index/KNNIndexCache.java | 4 +++- .../knn/index/KNNESSettingsTestIT.java | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/knn/index/KNNIndexCache.java b/src/main/java/com/amazon/opendistroforelasticsearch/knn/index/KNNIndexCache.java index f51a1293..961af6bc 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/knn/index/KNNIndexCache.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/knn/index/KNNIndexCache.java @@ -25,6 +25,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.watcher.FileChangesListener; import org.elasticsearch.watcher.FileWatcher; import org.elasticsearch.watcher.ResourceWatcherService; @@ -99,7 +100,8 @@ private void initCache() { /** * If the hnsw index is not accessed for knn.cache.item.expiry.minutes, it would be garbage collected. */ - long expiryTime = KNNSettings.state().getSettingValue(KNNSettings.KNN_CACHE_ITEM_EXPIRY_TIME_MINUTES); + long expiryTime = ((TimeValue) KNNSettings.state() + .getSettingValue(KNNSettings.KNN_CACHE_ITEM_EXPIRY_TIME_MINUTES)).getMinutes(); cacheBuilder.expireAfterAccess(expiryTime, TimeUnit.MINUTES); } diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/knn/index/KNNESSettingsTestIT.java b/src/test/java/com/amazon/opendistroforelasticsearch/knn/index/KNNESSettingsTestIT.java index 9279c926..1d5eca2b 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/knn/index/KNNESSettingsTestIT.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/knn/index/KNNESSettingsTestIT.java @@ -79,6 +79,26 @@ public void testQueriesPluginDisabled() throws Exception { searchKNNIndex(INDEX_NAME, new KNNQueryBuilder(FIELD_NAME, qvector, 1), 1); } + public void testItemRemovedFromCache_expiration() throws Exception { + createKnnIndex(INDEX_NAME, createKnnIndexMapping(FIELD_NAME, 2)); + updateClusterSettings(KNNSettings.KNN_CACHE_ITEM_EXPIRY_ENABLED, true); + updateClusterSettings(KNNSettings.KNN_CACHE_ITEM_EXPIRY_TIME_MINUTES, "1m"); + + Float[] vector = {6.0f, 6.0f}; + addKnnDoc(INDEX_NAME, "1", FIELD_NAME, vector); + + float[] qvector = {1.0f, 2.0f}; + Response response = searchKNNIndex(INDEX_NAME, new KNNQueryBuilder(FIELD_NAME, qvector, 1), 1); + assertEquals("knn query failed", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); + assertEquals(1, getTotalGraphsInCache()); + + Thread.sleep(65 * 1000); + + assertEquals(0, getTotalGraphsInCache()); + + updateClusterSettings(KNNSettings.KNN_CACHE_ITEM_EXPIRY_ENABLED, false); + } + public void testCreateIndexWithInvalidSpaceType() throws IOException { String invalidSpaceType = "bar"; Settings invalidSettings = Settings.builder()