Skip to content

Commit

Permalink
Update bwc test to update index setting
Browse files Browse the repository at this point in the history
Signed-off-by: Vijayan Balasubramanian <balasvij@amazon.com>
  • Loading branch information
VijayanB committed Oct 24, 2024
1 parent 7d87c52 commit 36d9d7f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Infrastructure
### Documentation
### Maintenance
* Select index settings based on cluster version[2236](https://github.com/opensearch-project/k-NN/pull/2236)
### Refactoring
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

package org.opensearch.knn.bwc;

import org.opensearch.common.settings.Settings;

import java.util.Collections;

import static org.opensearch.knn.TestUtils.NODES_BWC_CLUSTER;

public class ClearCacheIT extends AbstractRestartUpgradeTestCase {
Expand All @@ -20,7 +23,11 @@ public class ClearCacheIT extends AbstractRestartUpgradeTestCase {
public void testClearCache() throws Exception {
waitForClusterHealthGreen(NODES_BWC_CLUSTER);
if (isRunningAgainstOldCluster()) {
createKnnIndex(testIndex, getKNNDefaultIndexSettings(), createKnnIndexMapping(TEST_FIELD, DIMENSIONS));
// if approximate threshold is supported, set value to 0, to build graph always
Settings indexSettings = isApproximateThresholdSupported(getBWCVersion())
? buildKNNIndexSettings(0)
: getKNNDefaultIndexSettings();
createKnnIndex(testIndex, indexSettings, createKnnIndexMapping(TEST_FIELD, DIMENSIONS));
addKNNDocs(testIndex, TEST_FIELD, DIMENSIONS, docId, NUM_DOCS);
queryCnt = NUM_DOCS;
int graphCount = getTotalGraphsInCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ public void testKNNIndexCustomLegacyFieldMapping() throws Exception {
// When the cluster is in old version, create a KNN index with custom legacy field mapping settings
// and add documents into that index
if (isRunningAgainstOldCluster()) {
createKnnIndex(
testIndex,
createKNNIndexCustomLegacyFieldMappingSettings(
SpaceType.LINF,
KNN_ALGO_PARAM_M_MIN_VALUE,
KNN_ALGO_PARAM_EF_CONSTRUCTION_MIN_VALUE
),
createKnnIndexMapping(TEST_FIELD, DIMENSIONS)
Settings.Builder indexMappingSettings = createKNNIndexCustomLegacyFieldMappingIndexSettingsBuilder(
SpaceType.LINF,
KNN_ALGO_PARAM_M_MIN_VALUE,
KNN_ALGO_PARAM_EF_CONSTRUCTION_MIN_VALUE
);
if (isApproximateThresholdSupported(getBWCVersion())) {
indexMappingSettings.put(KNNSettings.INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD, 0);
}
createKnnIndex(testIndex, indexMappingSettings.build(), createKnnIndexMapping(TEST_FIELD, DIMENSIONS));
addKNNDocs(testIndex, TEST_FIELD, DIMENSIONS, DOC_ID, NUM_DOCS);
} else {
validateKNNIndexingOnUpgrade(NUM_DOCS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public void testKNNWarmupDefaultLegacyFieldMapping() throws Exception {
waitForClusterHealthGreen(NODES_BWC_CLUSTER);

if (isRunningAgainstOldCluster()) {
createKnnIndex(testIndex, getKNNDefaultIndexSettings(), createKnnIndexMapping(TEST_FIELD, DIMENSIONS));
Settings indexSettings = isApproximateThresholdSupported(getBWCVersion())
? buildKNNIndexSettings(0)
: getKNNDefaultIndexSettings();
createKnnIndex(testIndex, indexSettings, createKnnIndexMapping(TEST_FIELD, DIMENSIONS));
addKNNDocs(testIndex, TEST_FIELD, DIMENSIONS, DOC_ID, NUM_DOCS);
} else {
// update index setting to allow build graph always since we test graph count that are loaded into memory
Expand All @@ -48,13 +51,16 @@ public void testKNNWarmupCustomLegacyFieldMapping() throws Exception {
// When the cluster is in old version, create a KNN index with custom legacy field mapping settings
// and add documents into that index
if (isRunningAgainstOldCluster()) {
Settings indexMappingSettings = createKNNIndexCustomLegacyFieldMappingSettings(
Settings.Builder indexMappingSettings = createKNNIndexCustomLegacyFieldMappingIndexSettingsBuilder(
SpaceType.LINF,
KNN_ALGO_PARAM_M_MIN_VALUE,
KNN_ALGO_PARAM_EF_CONSTRUCTION_MIN_VALUE
);
if (isApproximateThresholdSupported(getBWCVersion())) {
indexMappingSettings.put(KNNSettings.INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD, 0);
}
String indexMapping = createKnnIndexMapping(TEST_FIELD, DIMENSIONS);
createKnnIndex(testIndex, indexMappingSettings, indexMapping);
createKnnIndex(testIndex, indexMappingSettings.build(), indexMapping);
addKNNDocs(testIndex, TEST_FIELD, DIMENSIONS, DOC_ID, NUM_DOCS);
} else {
validateKNNWarmupOnUpgrade();
Expand All @@ -65,7 +71,10 @@ public void testKNNWarmupCustomLegacyFieldMapping() throws Exception {
// space_type : "l2", engine : "nmslib", m : 16, ef_construction : 512
public void testKNNWarmupDefaultMethodFieldMapping() throws Exception {
if (isRunningAgainstOldCluster()) {
createKnnIndex(testIndex, getKNNDefaultIndexSettings(), createKNNIndexMethodFieldMapping(TEST_FIELD, DIMENSIONS));
Settings indexSettings = isApproximateThresholdSupported(getBWCVersion())
? buildKNNIndexSettings(0)
: getKNNDefaultIndexSettings();
createKnnIndex(testIndex, indexSettings, createKNNIndexMethodFieldMapping(TEST_FIELD, DIMENSIONS));
addKNNDocs(testIndex, TEST_FIELD, DIMENSIONS, DOC_ID, NUM_DOCS);
} else {
// update index setting to allow build graph always since we test graph count that are loaded into memory
Expand All @@ -78,9 +87,12 @@ public void testKNNWarmupDefaultMethodFieldMapping() throws Exception {
// space_type : "innerproduct", engine : "faiss", m : 50, ef_construction : 1024
public void testKNNWarmupCustomMethodFieldMapping() throws Exception {
if (isRunningAgainstOldCluster()) {
Settings indexSettings = isApproximateThresholdSupported(getBWCVersion())
? buildKNNIndexSettings(0)
: getKNNDefaultIndexSettings();
createKnnIndex(
testIndex,
getKNNDefaultIndexSettings(),
indexSettings,
createKNNIndexCustomMethodFieldMapping(TEST_FIELD, DIMENSIONS, SpaceType.INNER_PRODUCT, FAISS_NAME, M, EF_CONSTRUCTION)
);
addKNNDocs(testIndex, TEST_FIELD, DIMENSIONS, DOC_ID, NUM_DOCS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package org.opensearch.knn.bwc;

import org.opensearch.common.settings.Settings;

import java.util.Collections;

import static org.opensearch.knn.TestUtils.NODES_BWC_CLUSTER;
Expand All @@ -22,7 +24,10 @@ public void testClearCache() throws Exception {
waitForClusterHealthGreen(NODES_BWC_CLUSTER);
switch (getClusterType()) {
case OLD:
createKnnIndex(testIndex, getKNNDefaultIndexSettings(), createKnnIndexMapping(TEST_FIELD, DIMENSIONS));
Settings indexSettings = isApproximateThresholdSupported(getBWCVersion())
? buildKNNIndexSettings(0)
: getKNNDefaultIndexSettings();
createKnnIndex(testIndex, indexSettings, createKnnIndexMapping(TEST_FIELD, DIMENSIONS));
int docIdOld = 0;
addKNNDocs(testIndex, TEST_FIELD, DIMENSIONS, docIdOld, NUM_DOCS);
int graphCount = getTotalGraphsInCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public void testKNNWarmup() throws Exception {
waitForClusterHealthGreen(NODES_BWC_CLUSTER);
switch (getClusterType()) {
case OLD:
createKnnIndex(testIndex, getKNNDefaultIndexSettings(), createKnnIndexMapping(TEST_FIELD, DIMENSIONS));
Settings indexSettings = isApproximateThresholdSupported(getBWCVersion())
? buildKNNIndexSettings(0)
: getKNNDefaultIndexSettings();
createKnnIndex(testIndex, indexSettings, createKnnIndexMapping(TEST_FIELD, DIMENSIONS));
addKNNDocs(testIndex, TEST_FIELD, DIMENSIONS, 0, NUM_DOCS);
break;
case MIXED:
Expand Down
28 changes: 25 additions & 3 deletions src/testFixtures/java/org/opensearch/knn/KNNRestTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.commons.lang.StringUtils;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.net.URIBuilder;
import org.opensearch.Version;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.core.xcontent.DeprecationHandler;
Expand Down Expand Up @@ -64,6 +65,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -1345,15 +1347,22 @@ public void validateKNNSearch(String testIndex, String testField, int dimension,
}
}

protected Settings createKNNIndexCustomLegacyFieldMappingSettings(SpaceType spaceType, Integer m, Integer ef_construction) {
protected Settings.Builder createKNNIndexCustomLegacyFieldMappingIndexSettingsBuilder(
SpaceType spaceType,
Integer m,
Integer ef_construction
) {
return Settings.builder()
.put(NUMBER_OF_SHARDS, 1)
.put(NUMBER_OF_REPLICAS, 0)
.put(INDEX_KNN, true)
.put(KNNSettings.KNN_SPACE_TYPE, spaceType.getValue())
.put(KNNSettings.KNN_ALGO_PARAM_M, m)
.put(KNNSettings.KNN_ALGO_PARAM_EF_CONSTRUCTION, ef_construction)
.build();
.put(KNNSettings.KNN_ALGO_PARAM_EF_CONSTRUCTION, ef_construction);
}

protected Settings createKNNIndexCustomLegacyFieldMappingIndexSettings(SpaceType spaceType, Integer m, Integer ef_construction) {
return createKNNIndexCustomLegacyFieldMappingIndexSettingsBuilder(spaceType, m, ef_construction).build();
}

public String createKNNIndexMethodFieldMapping(String fieldName, Integer dimensions) throws IOException {
Expand Down Expand Up @@ -1859,4 +1868,17 @@ protected XContentBuilder buildSearchQuery(String fieldName, int k, float[] vect
builder.endObject().endObject().endObject().endObject();
return builder;
}

// approximate threshold parameter is only supported on or after V_2_18_0
protected boolean isApproximateThresholdSupported(final Optional<String> bwcVersion) {
if (bwcVersion.isEmpty()) {
return false;
}
String versionString = bwcVersion.get();
if (versionString.endsWith("-SNAPSHOT")) {
versionString = versionString.substring(0, versionString.length() - 9);
}
final Version version = Version.fromString(versionString);
return version.onOrAfter(Version.V_2_18_0);
}
}

0 comments on commit 36d9d7f

Please # to comment.