11
11
import org .opensearch .common .StopWatch ;
12
12
import org .opensearch .common .annotation .ExperimentalApi ;
13
13
import org .opensearch .index .IndexSettings ;
14
+ import org .opensearch .knn .common .KNNConstants ;
14
15
import org .opensearch .knn .common .featureflags .KNNFeatureFlags ;
15
16
import org .opensearch .knn .index .KNNSettings ;
16
17
import org .opensearch .knn .index .codec .nativeindex .NativeIndexBuildStrategy ;
17
18
import org .opensearch .knn .index .codec .nativeindex .model .BuildIndexParams ;
19
+ import org .opensearch .knn .index .remote .RemoteBuildRequest ;
20
+ import org .opensearch .knn .index .remote .RemoteIndexClient ;
18
21
import org .opensearch .knn .index .vectorvalues .KNNVectorValues ;
19
22
import org .opensearch .repositories .RepositoriesService ;
20
23
import org .opensearch .repositories .Repository ;
21
24
import org .opensearch .repositories .RepositoryMissingException ;
22
25
import org .opensearch .repositories .blobstore .BlobStoreRepository ;
23
26
24
27
import java .io .IOException ;
28
+ import java .util .HashMap ;
29
+ import java .util .Map ;
25
30
import java .util .function .Supplier ;
26
31
27
32
import static org .opensearch .knn .index .KNNSettings .KNN_INDEX_REMOTE_VECTOR_BUILD_SETTING ;
28
33
import static org .opensearch .knn .index .KNNSettings .KNN_REMOTE_VECTOR_REPO_SETTING ;
34
+ import static org .opensearch .knn .index .KNNSettings .state ;
29
35
30
36
/**
31
37
* This class orchestrates building vector indices. It handles uploading data to a repository, submitting a remote
@@ -54,7 +60,7 @@ public RemoteIndexBuildStrategy(Supplier<RepositoriesService> repositoriesServic
54
60
* @return whether to use the remote build feature
55
61
*/
56
62
public static boolean shouldBuildIndexRemotely (IndexSettings indexSettings ) {
57
- String vectorRepo = KNNSettings . state ().getSettingValue (KNN_REMOTE_VECTOR_REPO_SETTING .getKey ());
63
+ String vectorRepo = state ().getSettingValue (KNN_REMOTE_VECTOR_REPO_SETTING .getKey ());
58
64
return KNNFeatureFlags .isKNNRemoteVectorBuildEnabled ()
59
65
&& indexSettings .getValue (KNN_INDEX_REMOTE_VECTOR_BUILD_SETTING )
60
66
&& vectorRepo != null
@@ -88,17 +94,18 @@ public void buildAndWriteIndex(BuildIndexParams indexInfo) throws IOException {
88
94
log .debug ("Repository write took {} ms for vector field [{}]" , time_in_millis , indexInfo .getFieldName ());
89
95
90
96
stopWatch = new StopWatch ().start ();
91
- submitVectorBuild ();
97
+ RemoteBuildRequest buildRequest = constructBuildRequest (indexInfo );
98
+ String jobId = RemoteIndexClient .getInstance ().submitVectorBuild (buildRequest );
92
99
time_in_millis = stopWatch .stop ().totalTime ().millis ();
93
100
log .debug ("Submit vector build took {} ms for vector field [{}]" , time_in_millis , indexInfo .getFieldName ());
94
101
95
102
stopWatch = new StopWatch ().start ();
96
- awaitVectorBuild ();
103
+ String indexPath = awaitVectorBuild (jobId );
97
104
time_in_millis = stopWatch .stop ().totalTime ().millis ();
98
105
log .debug ("Await vector build took {} ms for vector field [{}]" , time_in_millis , indexInfo .getFieldName ());
99
106
100
107
stopWatch = new StopWatch ().start ();
101
- readFromRepository ();
108
+ readFromRepository (indexPath );
102
109
time_in_millis = stopWatch .stop ().totalTime ().millis ();
103
110
log .debug ("Repository read took {} ms for vector field [{}]" , time_in_millis , indexInfo .getFieldName ());
104
111
} catch (Exception e ) {
@@ -145,6 +152,49 @@ private void writeToRepository(
145
152
throw new NotImplementedException ();
146
153
}
147
154
155
+ /**
156
+ * Construct the RemoteBuildRequest object for the index build request
157
+ * @return RemoteBuildRequest with parameters set
158
+ */
159
+ public RemoteBuildRequest constructBuildRequest (BuildIndexParams indexInfo ) throws IOException {
160
+ String repositoryType = getRepository ().getMetadata ().type ();
161
+ String containerName = switch (repositoryType ) {
162
+ case "s3" -> getRepository ().getMetadata ().settings ().get ("bucket" );
163
+ case "fs" -> getRepository ().getMetadata ().settings ().get ("location" );
164
+ default -> throw new IllegalStateException ("Unexpected value: " + repositoryType );
165
+ };
166
+ String vectorPath = null ; // blobName + VECTOR_BLOB_FILE_EXTENSION
167
+ String docIdPath = null ; // blobName + DOC_ID_FILE_EXTENSION
168
+ String tenantId = null ; // indexSettings.getSettings().get(ClusterName.CLUSTER_NAME_SETTING.getKey());
169
+ int dimension = 0 ; // TODO
170
+ int docCount = indexInfo .getTotalLiveDocs ();
171
+ String dataType = indexInfo .getVectorDataType ().getValue (); // TODO need to fetch encoder param to get fp16 vs fp32
172
+ String engine = indexInfo .getKnnEngine ().getName ();
173
+
174
+ String spaceType = indexInfo .getParameters ().get (KNNConstants .SPACE_TYPE ).toString ();
175
+
176
+ Map <String , Object > algorithmParams = new HashMap <>();
177
+ algorithmParams .put ("ef_construction" , 100 );
178
+ algorithmParams .put ("m" , 16 );
179
+
180
+ Map <String , Object > indexParameters = new HashMap <>();
181
+ indexParameters .put ("algorithm" , "hnsw" );
182
+ indexParameters .put ("algorithm_parameters" , algorithmParams );
183
+
184
+ return RemoteBuildRequest .builder ()
185
+ .repositoryType (repositoryType )
186
+ .containerName (containerName )
187
+ .vectorPath (vectorPath )
188
+ .docIdPath (docIdPath )
189
+ .tenantId (tenantId )
190
+ .dimension (dimension )
191
+ .docCount (docCount )
192
+ .dataType (dataType )
193
+ .engine (engine )
194
+ .indexParameters (indexParameters )
195
+ .build ();
196
+ }
197
+
148
198
/**
149
199
* Submit vector build request to remote vector build service
150
200
*
@@ -156,14 +206,14 @@ private void submitVectorBuild() {
156
206
/**
157
207
* Wait on remote vector build to complete
158
208
*/
159
- private void awaitVectorBuild () {
209
+ private String awaitVectorBuild (String jobId ) {
160
210
throw new NotImplementedException ();
161
211
}
162
212
163
213
/**
164
214
* Read constructed vector file from remote repository and write to IndexOutput
165
215
*/
166
- private void readFromRepository () {
216
+ private void readFromRepository (String indexPath ) {
167
217
throw new NotImplementedException ();
168
218
}
169
219
}
0 commit comments