-
Notifications
You must be signed in to change notification settings - Fork 140
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
[BUG FIX] Add space type default and ef search parameter in warmup #276
Changes from all commits
ee9f532
7e4113c
769b263
56565e3
fd8bd5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -5,7 +5,6 @@ | |||||||
|
||||||||
package org.opensearch.knn.index; | ||||||||
|
||||||||
import com.google.common.collect.ImmutableMap; | ||||||||
import org.apache.lucene.index.FieldInfo; | ||||||||
import org.apache.logging.log4j.LogManager; | ||||||||
import org.apache.logging.log4j.Logger; | ||||||||
|
@@ -31,6 +30,7 @@ | |||||||
import java.util.stream.Collectors; | ||||||||
|
||||||||
import static org.opensearch.knn.common.KNNConstants.SPACE_TYPE; | ||||||||
import static org.opensearch.knn.index.IndexUtil.getParametersAtLoading; | ||||||||
import static org.opensearch.knn.index.codec.util.KNNCodecUtil.buildEngineFileName; | ||||||||
|
||||||||
/** | ||||||||
|
@@ -86,7 +86,7 @@ public void warmup() throws IOException { | |||||||
new NativeMemoryEntryContext.IndexEntryContext( | ||||||||
key, | ||||||||
NativeMemoryLoadStrategy.IndexLoadStrategy.getInstance(), | ||||||||
ImmutableMap.of(SPACE_TYPE, value.getValue()), | ||||||||
getParametersAtLoading(value, KNNEngine.getEngineNameFromPath(key), getIndexName()), | ||||||||
getIndexName() | ||||||||
), true); | ||||||||
} catch (ExecutionException ex) { | ||||||||
|
@@ -122,7 +122,10 @@ private Map<String, SpaceType> getEnginePaths(IndexReader indexReader, KNNEngine | |||||||
|
||||||||
for (FieldInfo fieldInfo : reader.getFieldInfos()) { | ||||||||
if (fieldInfo.attributes().containsKey(KNNVectorFieldMapper.KNN_FIELD)) { | ||||||||
SpaceType spaceType = SpaceType.getSpace(fieldInfo.attributes().get(SPACE_TYPE)); | ||||||||
// Space Type will not be present on ES versions 7.1 and 7.4 because the only available space type | ||||||||
// was L2. So, if Space Type is not present, just fall back to L2 | ||||||||
String spaceTypeName = fieldInfo.attributes().getOrDefault(SPACE_TYPE, SpaceType.L2.getValue()); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about to add a default value here to make more readable?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make default value a constant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense, I will update. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either way is fine, but i am leaning towards what you have :) since method itself gives the hint that it is get or Default :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I think I will just keep what I have. So, we do have SpaceType.DEFAULT, but in this case I do not want to use that because I know for sure that this should be L2. I will update with a comment. |
||||||||
SpaceType spaceType = SpaceType.getSpace(spaceTypeName); | ||||||||
String engineFileName = buildEngineFileName(reader.getSegmentInfo().info.name, | ||||||||
knnEngine.getLatestBuildVersion(), fieldInfo.name, fileExtension); | ||||||||
|
||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.knn.index; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.opensearch.cluster.ClusterState; | ||
import org.opensearch.cluster.metadata.IndexMetadata; | ||
import org.opensearch.cluster.metadata.Metadata; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.knn.KNNTestCase; | ||
import org.opensearch.knn.index.util.KNNEngine; | ||
|
||
import java.util.Map; | ||
|
||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.opensearch.knn.common.KNNConstants.HNSW_ALGO_EF_SEARCH; | ||
import static org.opensearch.knn.common.KNNConstants.SPACE_TYPE; | ||
import static org.opensearch.knn.index.IndexUtil.getParametersAtLoading; | ||
import static org.opensearch.knn.index.KNNSettings.KNN_ALGO_PARAM_EF_SEARCH; | ||
|
||
public class IndexUtilTests extends KNNTestCase { | ||
public void testGetLoadParameters() { | ||
// Test faiss to ensure that space type gets set properly | ||
SpaceType spaceType1 = SpaceType.COSINESIMIL; | ||
KNNEngine knnEngine1 = KNNEngine.FAISS; | ||
String indexName = "my-test-index"; | ||
|
||
Map<String, Object> loadParameters = getParametersAtLoading(spaceType1, knnEngine1, indexName); | ||
assertEquals(1, loadParameters.size()); | ||
assertEquals(spaceType1.getValue(), loadParameters.get(SPACE_TYPE)); | ||
|
||
// Test nmslib to ensure both space type and ef search are properly set | ||
SpaceType spaceType2 = SpaceType.L1; | ||
KNNEngine knnEngine2 = KNNEngine.NMSLIB; | ||
int efSearchValue = 413; | ||
|
||
// We use the constant for the setting here as opposed to the identifier of efSearch in nmslib jni | ||
Map<String, Object> indexSettings = ImmutableMap.of( | ||
KNN_ALGO_PARAM_EF_SEARCH, efSearchValue | ||
); | ||
|
||
// Because ef search comes from an index setting, we need to mock the long line of calls to get those | ||
// index settings | ||
Settings settings = Settings.builder().loadFromMap(indexSettings).build(); | ||
IndexMetadata indexMetadata = mock(IndexMetadata.class); | ||
when(indexMetadata.getSettings()).thenReturn(settings); | ||
Metadata metadata = mock(Metadata.class); | ||
when(metadata.index(anyString())).thenReturn(indexMetadata); | ||
ClusterState clusterState = mock(ClusterState.class); | ||
when(clusterState.getMetadata()).thenReturn(metadata); | ||
ClusterService clusterService = mock(ClusterService.class); | ||
when(clusterService.state()).thenReturn(clusterState); | ||
KNNSettings.state().setClusterService(clusterService); | ||
|
||
loadParameters = getParametersAtLoading(spaceType2, knnEngine2, indexName); | ||
assertEquals(2, loadParameters.size()); | ||
assertEquals(spaceType2.getValue(), loadParameters.get(SPACE_TYPE)); | ||
assertEquals(efSearchValue, loadParameters.get(HNSW_ALGO_EF_SEARCH)); | ||
} | ||
} |
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.
Are we going to mutate this map later? Seems that at the end we return mutable object from this method.
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.
or we can just return UnModifiableMap.
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.
Prefer the unmodifiable map approach. Will go with that.