Skip to content
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

Moved remote index build client to a separate module #2588

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ task release(type: Copy, group: 'build') {
//****************************************************************************/
dependencies {
api "org.opensearch:opensearch:${opensearch_version}"
api project(":remote-index-build-client")
compileOnly "org.opensearch.plugin:opensearch-scripting-painless-spi:${versions.opensearch}"
api group: 'com.google.guava', name: 'failureaccess', version:'1.0.1'
api group: 'com.google.guava', name: 'guava', version:'32.1.3-jre'
Expand All @@ -321,9 +322,6 @@ dependencies {
api "net.java.dev.jna:jna-platform:5.13.0"
// OpenSearch core is using slf4j 1.7.36. Therefore, we cannot change the version here.
implementation 'org.slf4j:slf4j-api:1.7.36'
api "org.apache.httpcomponents.client5:httpclient5:${versions.httpclient5}"
api "org.apache.httpcomponents.core5:httpcore5:${versions.httpcore5}"
api "org.apache.httpcomponents.core5:httpcore5-h2:${versions.httpcore5}"
zipArchive group: 'org.opensearch.plugin', name:'opensearch-security', version: "${opensearch_build}"
}

Expand Down
404 changes: 404 additions & 0 deletions remote-index-build-client/LICENSE.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions remote-index-build-client/NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OpenSearch k-NN
Copyright OpenSearch Contributors
61 changes: 61 additions & 0 deletions remote-index-build-client/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

plugins {
id 'java'
id 'java-library'
id 'jacoco'
id "io.freefair.lombok"
id 'com.diffplug.spotless' version '6.25.0'
id 'opensearch.build'
}

repositories {
mavenLocal()
maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" }
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}

compileJava {
options.compilerArgs.addAll(["-processor", 'lombok.launch.AnnotationProcessorHider$AnnotationProcessor'])
}
compileTestJava {
options.compilerArgs.addAll(["-processor", 'lombok.launch.AnnotationProcessorHider$AnnotationProcessor'])
}

dependencies {
compileOnly group: 'org.opensearch', name: 'opensearch', version: "${opensearch_version}"
api group: 'commons-lang', name: 'commons-lang', version: '2.6'
api "org.apache.httpcomponents.client5:httpclient5:${versions.httpclient5}"
api "org.apache.httpcomponents.core5:httpcore5:${versions.httpcore5}"
api "org.apache.httpcomponents.core5:httpcore5-h2:${versions.httpcore5}"
testImplementation "org.opensearch.test:framework:${opensearch_version}"
}

check.dependsOn spotlessCheck

publishing {
publications {
pluginZip(MavenPublication) { publication ->
pom {
name = "opensearch-knn-remote-index-build-client"
description = 'Client for connecting to remote index build endpoint'
licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
developers {
developer {
name = "OpenSearch"
url = "https://github.com/opensearch-project/k-NN/remote-index-build-client"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.knn.index.remote;
package org.opensearch.remoteindexbuild.client;

import org.opensearch.remoteindexbuild.model.RemoteBuildRequest;
import org.opensearch.remoteindexbuild.model.RemoteBuildResponse;
import org.opensearch.remoteindexbuild.model.RemoteStatusResponse;

import java.io.IOException;

Expand All @@ -12,6 +16,9 @@
*/
public interface RemoteIndexClient {

String BUILD_ENDPOINT = "/_build";
String STATUS_ENDPOINT = "/_status";

/**
* Submit a build to the Remote Vector Build Service.
* @return RemoteBuildResponse from the server
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.remoteindexbuild.client;

public class RemoteIndexClientFactory {

// Default to HTTP client
public static RemoteIndexClient getRemoteIndexClient(final String endpoint) {
return new RemoteIndexHTTPClient(endpoint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.knn.index.remote;
package org.opensearch.remoteindexbuild.client;

import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang.StringUtils;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
Expand All @@ -16,18 +17,16 @@
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.common.settings.SecureString;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.knn.index.KNNSettings;
import org.opensearch.knn.index.codec.nativeindex.model.BuildIndexParams;
import org.opensearch.knn.index.codec.nativeindex.remote.RemoteIndexBuildStrategy;
import org.opensearch.knn.plugin.KNNPlugin;
import org.opensearch.remoteindexbuild.model.RemoteBuildRequest;
import org.opensearch.remoteindexbuild.model.RemoteBuildResponse;
import org.opensearch.remoteindexbuild.model.RemoteStatusResponse;

import java.io.Closeable;
import java.io.IOException;
Expand All @@ -37,9 +36,6 @@
import java.security.PrivilegedExceptionAction;

import static org.apache.hc.core5.http.HttpStatus.SC_OK;
import static org.opensearch.knn.index.KNNSettings.KNN_REMOTE_BUILD_CLIENT_PASSWORD_SETTING;
import static org.opensearch.knn.index.KNNSettings.KNN_REMOTE_BUILD_CLIENT_USERNAME_SETTING;
import static org.opensearch.knn.index.remote.KNNRemoteConstants.BUILD_ENDPOINT;

/**
* Class to handle all interactions with the remote vector build service.
Expand Down Expand Up @@ -71,11 +67,9 @@ protected static CloseableHttpClient getHttpClient() {

/**
* Creates the client, setting the endpoint per-instance so the same endpoint is used per-build operation
* (per call to {@link RemoteIndexBuildStrategy#buildAndWriteIndex(BuildIndexParams)})
*/
public RemoteIndexHTTPClient() {
String endpoint = KNNSettings.getRemoteBuildServiceEndpoint();
if (endpoint == null || endpoint.isEmpty()) {
public RemoteIndexHTTPClient(final String endpoint) {
if (StringUtils.isEmpty(endpoint)) {
throw new IllegalArgumentException("No endpoint set for RemoteIndexClient");
}
this.endpoint = endpoint;
Expand Down Expand Up @@ -147,14 +141,11 @@ private String toJson(ToXContentObject object) throws IOException {
}

/**
* Set the global auth header to use the refreshed secure settings.
* Called by {@link KNNPlugin#reload(Settings)} when the nodes reload API is called.
* @param settings Settings to use to get the credentials
*/
public static void reloadAuthHeader(Settings settings) {
SecureString username = KNN_REMOTE_BUILD_CLIENT_USERNAME_SETTING.get(settings);
SecureString password = KNN_REMOTE_BUILD_CLIENT_PASSWORD_SETTING.get(settings);

* Set the global auth header to use the refreshed secure settings.
* @param username username to use for authentication
* @param password password to use for authentication
*/
public static void reloadAuthHeader(final SecureString username, final SecureString password) {
if (password != null && !password.isEmpty()) {
if (username == null || username.isEmpty()) {
throw new IllegalArgumentException("Username must be set if password is set");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.knn.index.remote;
package org.opensearch.remoteindexbuild.client;

import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
import org.apache.hc.core5.http.ConnectionClosedException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.remoteindexbuild.constants;

// Public class to define the constants used by Remote Index Build in 2 or more classes.
public class KNNRemoteConstants {
// Build request keys
public static final String ALGORITHM = "algorithm";
public static final String ALGORITHM_PARAMETERS = "algorithm_parameters";
public static final String METHOD_PARAMETER_SPACE_TYPE = "space_type";
public static final String METHOD_PARAMETER_EF_SEARCH = "ef_search";
public static final String METHOD_PARAMETER_EF_CONSTRUCTION = "ef_construction";
public static final String METHOD_PARAMETER_M = "m";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.remoteindexbuild.model;

import lombok.Builder;
import lombok.Value;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;

import java.io.IOException;

/**
* Request object for sending build requests to the remote build service, encapsulating all the required parameters
* in a generic XContent format.
*/
@Value
@Builder
public class RemoteBuildRequest implements ToXContentObject {
private static final String DOC_COUNT = "doc_count";
private static final String TENANT_ID = "tenant_id";
private static final String DOC_ID_PATH = "doc_id_path";
private static final String DIMENSION = "dimension";
private static final String VECTOR_DATA_TYPE_FIELD = "data_type";
private static final String KNN_ENGINE = "engine";

private static final String VECTOR_PATH = "vector_path";
private static final String CONTAINER_NAME = "container_name";
private static final String REPOSITORY_TYPE = "repository_type";
private static final String INDEX_PARAMETERS = "index_parameters";

String repositoryType;
String containerName;
String vectorPath;
String docIdPath;
String tenantId;
int dimension;
int docCount;
String vectorDataType;
String engine;
RemoteIndexParameters indexParameters;

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(REPOSITORY_TYPE, repositoryType);
builder.field(CONTAINER_NAME, containerName);
builder.field(VECTOR_PATH, vectorPath);
builder.field(DOC_ID_PATH, docIdPath);
builder.field(TENANT_ID, tenantId);
builder.field(DIMENSION, dimension);
builder.field(DOC_COUNT, docCount);
builder.field(VECTOR_DATA_TYPE_FIELD, vectorDataType);
builder.field(KNN_ENGINE, engine);
builder.field(INDEX_PARAMETERS, indexParameters);
builder.endObject();
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.knn.index.remote;
package org.opensearch.remoteindexbuild.model;

import lombok.Builder;
import lombok.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.knn.index.remote;
package org.opensearch.remoteindexbuild.model;

import lombok.experimental.SuperBuilder;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.knn.common.KNNConstants;
import org.opensearch.remoteindexbuild.constants.KNNRemoteConstants;

import java.io.IOException;

import static org.opensearch.knn.index.remote.KNNRemoteConstants.ALGORITHM_PARAMETERS;

@SuperBuilder
public class RemoteFaissHNSWIndexParameters extends RemoteIndexParameters {

int m;
int efConstruction;
int efSearch;

@Override
void addAlgorithmParameters(XContentBuilder builder) throws IOException {
builder.startObject(ALGORITHM_PARAMETERS);
builder.field(KNNConstants.METHOD_PARAMETER_M, m);
builder.field(KNNConstants.METHOD_PARAMETER_EF_CONSTRUCTION, efConstruction);
builder.field(KNNConstants.METHOD_PARAMETER_EF_SEARCH, efSearch);
builder.startObject(KNNRemoteConstants.ALGORITHM_PARAMETERS);
builder.field(KNNRemoteConstants.METHOD_PARAMETER_M, m);
builder.field(KNNRemoteConstants.METHOD_PARAMETER_EF_CONSTRUCTION, efConstruction);
builder.field(KNNRemoteConstants.METHOD_PARAMETER_EF_SEARCH, efSearch);
builder.endObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.knn.index.remote;
package org.opensearch.remoteindexbuild.model;

import lombok.experimental.SuperBuilder;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.remoteindexbuild.constants.KNNRemoteConstants;

import java.io.IOException;

import static org.opensearch.knn.common.KNNConstants.METHOD_PARAMETER_SPACE_TYPE;
import static org.opensearch.knn.index.remote.KNNRemoteConstants.ALGORITHM;

@SuperBuilder
public abstract class RemoteIndexParameters implements ToXContentObject {

String spaceType;
String algorithm;

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(METHOD_PARAMETER_SPACE_TYPE, spaceType);
builder.field(ALGORITHM, algorithm);
builder.field(KNNRemoteConstants.METHOD_PARAMETER_SPACE_TYPE, spaceType);
builder.field(KNNRemoteConstants.ALGORITHM, algorithm);
addAlgorithmParameters(builder);
builder.endObject();
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.knn.index.remote;
package org.opensearch.remoteindexbuild.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.remoteindexbuild;

public class TestConstants {
public static final String TEST_BUCKET = "test-bucket";
public static final String TEST_CLUSTER = "test-cluster";
public static final String MOCK_JOB_ID_RESPONSE = "{\"job_id\": \"job-1739930402\"}";
public static final String MOCK_JOB_ID = "job-1739930402";
public static final String MOCK_BLOB_NAME = "blob";
public static final String MOCK_ENDPOINT = "https://mock-build-service.com";
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String L2_SPACE_TYPE = "l2";
public static final String HNSW_ALGORITHM = "hnsw";
}
Loading
Loading