Skip to content

Commit

Permalink
Preloads .vec and .vex files
Browse files Browse the repository at this point in the history
LuceneFlatVectorReader uses IOContext.Random to open the read. IOContext.Random
indicates the kernel to not read ahead the pages on to physical memory.
This causes an increase in merge time due to increase of read ops at
runtime.

The preload settings signals the kernal to preload the files when the
reader is opened

Signed-off-by: Tejas Shah <[email protected]>
  • Loading branch information
shatejas committed Oct 4, 2024
1 parent 6771842 commit d44fa53
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.opensearch.common.ValidationException;
import org.opensearch.knn.index.SpaceType;

import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -137,6 +136,6 @@ KNNLibraryIndexingContext getKNNLibraryIndexingContext(
* @return list of file extensions that will be read/write with mmap
*/
default List<String> mmapFileExtensions() {
return Collections.emptyList();
return List.of("vec", "vex");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.opensearch.knn.index.engine.MethodResolver;
import org.opensearch.knn.index.engine.ResolvedMethodContext;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

Expand Down Expand Up @@ -89,11 +88,6 @@ public Float scoreToRadialThreshold(Float score, SpaceType spaceType) {
return score;
}

@Override
public List<String> mmapFileExtensions() {
return List.of("vec", "vex");
}

@Override
public ResolvedMethodContext resolveMethod(
KNNMethodContext knnMethodContext,
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/opensearch/knn/plugin/KNNPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.opensearch.index.engine.EngineFactory;
import org.opensearch.indices.SystemIndexDescriptor;
import org.opensearch.knn.index.KNNCircuitBreaker;
import org.opensearch.knn.index.engine.KNNEngine;
import org.opensearch.knn.plugin.search.KNNConcurrentSearchRequestDecider;
import org.opensearch.knn.index.util.KNNClusterUtil;
import org.opensearch.knn.index.query.KNNQueryBuilder;
Expand Down Expand Up @@ -110,6 +111,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static java.util.Collections.singletonList;
import static org.opensearch.knn.common.KNNConstants.KNN_THREAD_POOL_PREFIX;
Expand Down Expand Up @@ -352,6 +354,20 @@ public Collection<SystemIndexDescriptor> getSystemIndexDescriptors(Settings sett
return ImmutableList.of(new SystemIndexDescriptor(MODEL_INDEX_NAME, "Index for storing models used for k-NN indices"));
}

/**
* Plugin can provide additional node settings, that includes new settings or overrides for existing one from core.
*
* @return settings that are set by plugin
*/
@Override
public Settings additionalSettings() {
final List<String> mmapFileExtensions = Arrays.stream(KNNEngine.values())
.flatMap(engine -> engine.mmapFileExtensions().stream())
.collect(Collectors.toList());

return Settings.builder().putList(IndexModule.INDEX_STORE_PRE_LOAD_SETTING.getKey(), mmapFileExtensions).build();
}

@Override
public Optional<ConcurrentSearchRequestDecider.Factory> getConcurrentSearchRequestDeciderFactory() {
return Optional.of(new KNNConcurrentSearchRequestDecider.Factory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

Expand Down Expand Up @@ -367,4 +368,12 @@ public void testMethodAsMapBuilder() throws IOException {
assertEquals(expectedKNNMethodContext.getVectorValidator(), actualKNNLibraryIndexingContext.getVectorValidator());
}

public void testMmapFileExtensions() {
final List<String> mMapExtensions = Faiss.INSTANCE.mmapFileExtensions();
assertNotNull(mMapExtensions);
final List<String> expectedSettings = List.of("vex", "vec");
assertTrue(expectedSettings.containsAll(mMapExtensions));
assertTrue(mMapExtensions.containsAll(expectedSettings));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.knn.index.engine.nmslib;

import org.opensearch.knn.KNNTestCase;
import org.opensearch.knn.index.engine.faiss.Faiss;

import java.util.List;

public class NMSLibTests extends KNNTestCase {

public void testMmapFileExtensions() {
final List<String> mmapExtensions = Faiss.INSTANCE.mmapFileExtensions();
assertNotNull(mmapExtensions);
final List<String> expectedSettings = List.of("vex", "vec");
assertTrue(expectedSettings.containsAll(mmapExtensions));
assertTrue(mmapExtensions.containsAll(expectedSettings));
}
}
23 changes: 23 additions & 0 deletions src/test/java/org/opensearch/knn/plugin/KNNPluginTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.knn.plugin;

import org.opensearch.common.settings.Settings;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class KNNPluginTests extends OpenSearchTestCase {

public void testKNNPlugin_additionalSettings() throws IOException {
try (KNNPlugin knnPlugin = new KNNPlugin()) {
Settings additionalSettings = knnPlugin.additionalSettings();
assertEquals(Set.of("vec", "vex"), new HashSet<>(additionalSettings.getAsList("index.store.preload")));
}
}
}

0 comments on commit d44fa53

Please sign in to comment.