-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Unit and Integration tests for Clear Cache API
Signed-off-by: Naveen Tatikonda <navtat@amazon.com>
- Loading branch information
1 parent
e697382
commit 9174a9f
Showing
5 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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
85 changes: 85 additions & 0 deletions
85
src/test/java/org/opensearch/knn/plugin/action/RestClearCacheHandlerIT.java
This file contains 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,85 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.plugin.action; | ||
|
||
import org.junit.Test; | ||
import org.opensearch.client.Request; | ||
import org.opensearch.client.ResponseException; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.knn.KNNRestTestCase; | ||
import org.opensearch.knn.plugin.KNNPlugin; | ||
import org.opensearch.rest.RestRequest; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
import static org.opensearch.knn.common.KNNConstants.CLEAR_CACHE; | ||
|
||
/** | ||
* Integration tests to validate ClearCache API | ||
*/ | ||
|
||
public class RestClearCacheHandlerIT extends KNNRestTestCase { | ||
private static final String TEST_FIELD = "test-field"; | ||
private static final int DIMENSIONS = 2; | ||
|
||
// @Test(expected = ResponseException.class) | ||
public void testNonExistentIndex() throws IOException { | ||
String nonExistentIndex = "non-existent-index"; | ||
|
||
String restURI = String.join("/", KNNPlugin.KNN_BASE_URI, CLEAR_CACHE, nonExistentIndex); | ||
Request request = new Request(RestRequest.Method.GET.name(), restURI); | ||
|
||
ResponseException ex = expectThrows(ResponseException.class, () -> client().performRequest(request)); | ||
assertTrue(ex.getMessage().contains(nonExistentIndex)); | ||
} | ||
|
||
@Test(expected = ResponseException.class) | ||
public void testNotKnnIndex() throws IOException { | ||
String notKNNIndex = "not-KNN-index"; | ||
createIndex(notKNNIndex, Settings.EMPTY); | ||
|
||
String restURI = String.join("/", KNNPlugin.KNN_BASE_URI, CLEAR_CACHE, notKNNIndex); | ||
Request request = new Request(RestRequest.Method.GET.name(), restURI); | ||
|
||
ResponseException ex = expectThrows(ResponseException.class, () -> client().performRequest(request)); | ||
assertTrue(ex.getMessage().contains(notKNNIndex)); | ||
} | ||
|
||
public void testClearCacheSingleIndex() throws Exception { | ||
String testIndex = getTestName().toLowerCase(); | ||
int graphCountBefore = getTotalGraphsInCache(); | ||
createKnnIndex(testIndex, getKNNDefaultIndexSettings(), createKnnIndexMapping(TEST_FIELD, DIMENSIONS)); | ||
addKnnDoc(testIndex, String.valueOf(randomInt()), TEST_FIELD, new Float[] { randomFloat(), randomFloat() }); | ||
|
||
knnWarmup(Collections.singletonList(testIndex)); | ||
|
||
assertEquals(graphCountBefore + 1, getTotalGraphsInCache()); | ||
|
||
clearCache(Collections.singletonList(testIndex)); | ||
assertEquals(graphCountBefore, getTotalGraphsInCache()); | ||
} | ||
|
||
public void testClearCacheMultipleIndices() throws Exception { | ||
String testIndex1 = getTestName().toLowerCase(); | ||
String testIndex2 = getTestName().toLowerCase() + 1; | ||
int graphCountBefore = getTotalGraphsInCache(); | ||
|
||
createKnnIndex(testIndex1, getKNNDefaultIndexSettings(), createKnnIndexMapping(TEST_FIELD, DIMENSIONS)); | ||
addKnnDoc(testIndex1, String.valueOf(randomInt()), TEST_FIELD, new Float[] { randomFloat(), randomFloat() }); | ||
|
||
createKnnIndex(testIndex2, getKNNDefaultIndexSettings(), createKnnIndexMapping(TEST_FIELD, DIMENSIONS)); | ||
addKnnDoc(testIndex2, String.valueOf(randomInt()), TEST_FIELD, new Float[] { randomFloat(), randomFloat() }); | ||
|
||
knnWarmup(Arrays.asList(testIndex1, testIndex2)); | ||
|
||
assertEquals(graphCountBefore + 2, getTotalGraphsInCache()); | ||
|
||
clearCache(Arrays.asList(testIndex1, testIndex2)); | ||
assertEquals(graphCountBefore, getTotalGraphsInCache()); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
src/test/java/org/opensearch/knn/plugin/transport/ClearCacheTransportActionTests.java
This file contains 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,116 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.plugin.transport; | ||
|
||
import org.opensearch.cluster.ClusterName; | ||
import org.opensearch.cluster.ClusterState; | ||
import org.opensearch.cluster.block.ClusterBlockException; | ||
import org.opensearch.cluster.block.ClusterBlockLevel; | ||
import org.opensearch.cluster.routing.ShardRouting; | ||
import org.opensearch.cluster.routing.ShardsIterator; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.index.IndexService; | ||
import org.opensearch.knn.KNNSingleNodeTestCase; | ||
import org.opensearch.knn.index.memory.NativeMemoryCacheManager; | ||
|
||
import java.io.IOException; | ||
import java.util.EnumSet; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ClearCacheTransportActionTests extends KNNSingleNodeTestCase { | ||
private static final String TEST_FIELD = "test-field"; | ||
private static final int DIMENSIONS = 2; | ||
|
||
public void testShardOperation() throws IOException, ExecutionException, InterruptedException { | ||
String testIndex = getTestName().toLowerCase(); | ||
KNNWarmupRequest knnWarmupRequest = new KNNWarmupRequest(testIndex); | ||
KNNWarmupTransportAction knnWarmupTransportAction = node().injector().getInstance(KNNWarmupTransportAction.class); | ||
assertEquals(0, NativeMemoryCacheManager.getInstance().getIndicesCacheStats().size()); | ||
|
||
IndexService indexService = createKNNIndex(testIndex); | ||
createKnnIndexMapping(testIndex, TEST_FIELD, DIMENSIONS); | ||
addKnnDoc(testIndex, String.valueOf(randomInt()), TEST_FIELD, new Float[] { randomFloat(), randomFloat() }); | ||
ShardRouting shardRouting = indexService.iterator().next().routingEntry(); | ||
|
||
knnWarmupTransportAction.shardOperation(knnWarmupRequest, shardRouting); | ||
assertEquals(1, NativeMemoryCacheManager.getInstance().getIndicesCacheStats().size()); | ||
|
||
ClearCacheRequest clearCacheRequest = new ClearCacheRequest(testIndex); | ||
ClearCacheTransportAction clearCacheTransportAction = node().injector().getInstance(ClearCacheTransportAction.class); | ||
clearCacheTransportAction.shardOperation(clearCacheRequest, shardRouting); | ||
assertEquals(0, NativeMemoryCacheManager.getInstance().getIndicesCacheStats().size()); | ||
} | ||
|
||
public void testShards() throws InterruptedException, ExecutionException, IOException { | ||
String testIndex = getTestName().toLowerCase(); | ||
ClusterService clusterService = node().injector().getInstance(ClusterService.class); | ||
ClearCacheTransportAction clearCacheTransportAction = node().injector().getInstance(ClearCacheTransportAction.class); | ||
ClearCacheRequest clearCacheRequest = new ClearCacheRequest(testIndex); | ||
|
||
createKNNIndex(testIndex); | ||
createKnnIndexMapping(testIndex, TEST_FIELD, DIMENSIONS); | ||
addKnnDoc(testIndex, String.valueOf(randomInt()), TEST_FIELD, new Float[] { randomFloat(), randomFloat() }); | ||
|
||
ShardsIterator shardsIterator = clearCacheTransportAction.shards( | ||
clusterService.state(), | ||
clearCacheRequest, | ||
new String[] { testIndex } | ||
); | ||
assertEquals(1, shardsIterator.size()); | ||
} | ||
|
||
public void testCheckGlobalBlock_throwsClusterBlockException() { | ||
String testIndex = getTestName().toLowerCase(); | ||
String description = "testing metadata block"; | ||
ClusterService clusterService = mock(ClusterService.class); | ||
addGlobalClusterBlock(clusterService, description, EnumSet.of(ClusterBlockLevel.METADATA_READ)); | ||
ClearCacheTransportAction clearCacheTransportAction = node().injector().getInstance(ClearCacheTransportAction.class); | ||
ClearCacheRequest clearCacheRequest = new ClearCacheRequest(testIndex); | ||
ClusterBlockException ex = clearCacheTransportAction.checkGlobalBlock(clusterService.state(), clearCacheRequest); | ||
assertTrue(ex.getMessage().contains(description)); | ||
} | ||
|
||
public void testCheckGlobalBlock_notThrowsClusterBlockException() { | ||
String testIndex = getTestName().toLowerCase(); | ||
ClusterService clusterService = mock(ClusterService.class); | ||
ClearCacheTransportAction clearCacheTransportAction = node().injector().getInstance(ClearCacheTransportAction.class); | ||
ClearCacheRequest clearCacheRequest = new ClearCacheRequest(testIndex); | ||
ClusterState state = ClusterState.builder(ClusterName.DEFAULT).build(); | ||
when(clusterService.state()).thenReturn(state); | ||
assertNull(clearCacheTransportAction.checkGlobalBlock(clusterService.state(), clearCacheRequest)); | ||
} | ||
|
||
public void testCheckRequestBlock_throwsClusterBlockException() { | ||
String testIndex = getTestName().toLowerCase(); | ||
String description = "testing index metadata block"; | ||
ClusterService clusterService = mock(ClusterService.class); | ||
addIndexClusterBlock(clusterService, description, EnumSet.of(ClusterBlockLevel.METADATA_READ), testIndex); | ||
|
||
ClearCacheTransportAction clearCacheTransportAction = node().injector().getInstance(ClearCacheTransportAction.class); | ||
ClearCacheRequest clearCacheRequest = new ClearCacheRequest(testIndex); | ||
ClusterBlockException ex = clearCacheTransportAction.checkRequestBlock( | ||
clusterService.state(), | ||
clearCacheRequest, | ||
new String[] { testIndex } | ||
); | ||
assertTrue(ex.getMessage().contains(testIndex)); | ||
assertTrue(ex.getMessage().contains(description)); | ||
|
||
} | ||
|
||
public void testCheckRequestBlock_notThrowsClusterBlockException() { | ||
String testIndex = getTestName().toLowerCase(); | ||
ClusterService clusterService = mock(ClusterService.class); | ||
ClearCacheTransportAction clearCacheTransportAction = node().injector().getInstance(ClearCacheTransportAction.class); | ||
ClearCacheRequest clearCacheRequest = new ClearCacheRequest(testIndex); | ||
ClusterState state = ClusterState.builder(ClusterName.DEFAULT).build(); | ||
when(clusterService.state()).thenReturn(state); | ||
assertNull(clearCacheTransportAction.checkRequestBlock(clusterService.state(), clearCacheRequest, new String[] { testIndex })); | ||
} | ||
} |
This file contains 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