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 <[email protected]>
  • Loading branch information
VijayanB committed Oct 24, 2024
1 parent 7d87c52 commit 0135d8b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
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 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
24 changes: 21 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,13 @@ 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;
}
final Version version = Version.fromString(bwcVersion.get());
return version.onOrAfter(Version.V_2_18_0);
}
}

0 comments on commit 0135d8b

Please sign in to comment.