diff --git a/rusty-search/.gitignore b/rusty-search/.gitignore new file mode 100644 index 0000000000000..9f970225adb6a --- /dev/null +++ b/rusty-search/.gitignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/rusty-search/Cargo.lock b/rusty-search/Cargo.lock new file mode 100644 index 0000000000000..ad045385a7194 --- /dev/null +++ b/rusty-search/Cargo.lock @@ -0,0 +1,243 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bytes" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" + +[[package]] +name = "cesu8" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "combine" +version = "4.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35ed6e9d84f0b51a7f52daf1c7d71dd136fd7a3f41a8462b8cdb8c78d920fad4" +dependencies = [ + "bytes", + "memchr", +] + +[[package]] +name = "jni" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97" +dependencies = [ + "cesu8", + "cfg-if", + "combine", + "jni-sys", + "log", + "thiserror", + "walkdir", + "windows-sys", +] + +[[package]] +name = "jni-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[package]] +name = "memchr" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" + +[[package]] +name = "proc-macro2" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rust_library" +version = "0.1.0" +dependencies = [ + "jni", +] + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "syn" +version = "2.0.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "walkdir" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" diff --git a/rusty-search/Cargo.toml b/rusty-search/Cargo.toml new file mode 100644 index 0000000000000..a6ef67058c0c2 --- /dev/null +++ b/rusty-search/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "rust_library" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +jni = "0.21.1" + +[lib] +crate_type = ["cdylib"] \ No newline at end of file diff --git a/rusty-search/build.gradle b/rusty-search/build.gradle new file mode 100644 index 0000000000000..d1d7bcafdc459 --- /dev/null +++ b/rusty-search/build.gradle @@ -0,0 +1,9 @@ +plugins{ + id 'io.github.arc-blroth.cargo-wrapper' version '1.0.0' apply true +} + + +cargo { + outputs = ['': System.mapLibraryName('rust_library')] + cargoCommand="/opt/homebrew/bin/cargo" +} \ No newline at end of file diff --git a/rusty-search/src/lib.rs b/rusty-search/src/lib.rs new file mode 100644 index 0000000000000..8c8879b68cb6c --- /dev/null +++ b/rusty-search/src/lib.rs @@ -0,0 +1,6 @@ +use jni::JNIEnv; + +#[no_mangle] +pub extern "system" fn Java_org_opensearch_search_query_QueryPhase_doStuff(_env: JNIEnv) { + println!("Hello Rust!"); +} \ No newline at end of file diff --git a/server/build.gradle b/server/build.gradle index e36498bf1038b..c6c1b97dcedcf 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -58,6 +58,15 @@ sourceSets { } } +configurations { + // Declare a custom configuration to + // resolve the library from :native + backend { + canBeConsumed false + canBeResolved true + } +} + dependencies { api project(':libs:opensearch-common') @@ -131,6 +140,13 @@ dependencies { internalClusterTestImplementation(project(":test:framework")) { exclude group: 'org.opensearch', module: 'server' } + + backend(project(':rusty-search')) +} + +processResources { + // Copy the native library into the final jar + from(configurations.backend) } tasks.withType(JavaCompile).configureEach { diff --git a/server/src/main/java/org/opensearch/search/query/QueryPhase.java b/server/src/main/java/org/opensearch/search/query/QueryPhase.java index 8f98f0d9efbd4..0d0162a79d0cd 100644 --- a/server/src/main/java/org/opensearch/search/query/QueryPhase.java +++ b/server/src/main/java/org/opensearch/search/query/QueryPhase.java @@ -71,7 +71,12 @@ import org.opensearch.search.suggest.SuggestProcessor; import org.opensearch.threadpool.ThreadPool; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -101,6 +106,26 @@ public class QueryPhase { private final SuggestProcessor suggestProcessor; private final RescoreProcessor rescoreProcessor; + private native static void doStuff(); + + static { + try { + // Extract and load our native library + String nativeLibraryName = System.mapLibraryName("rust_library"); + File tempFile = File.createTempFile("extracted_", nativeLibraryName); + try ( + ReadableByteChannel src = Channels.newChannel(QueryPhase.class.getClassLoader().getResourceAsStream(nativeLibraryName)); + FileChannel dst = new FileOutputStream(tempFile).getChannel() + ) { + dst.transferFrom(src, 0, Long.MAX_VALUE); + } + System.load(tempFile.getAbsolutePath()); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + } + public QueryPhase() { this(DEFAULT_QUERY_PHASE_SEARCHER); } @@ -353,6 +378,7 @@ private static boolean searchWithCollector( } QuerySearchResult queryResult = searchContext.queryResult(); try { + doStuff(); searcher.search(query, queryCollector); } catch (EarlyTerminatingCollector.EarlyTerminationException e) { queryResult.terminatedEarly(true); diff --git a/settings.gradle b/settings.gradle index 24ab4a7a22237..7809e2a4304ce 100644 --- a/settings.gradle +++ b/settings.gradle @@ -91,7 +91,8 @@ List projects = [ 'test:fixtures:old-elasticsearch', 'test:fixtures:s3-fixture', 'test:logger-usage', - 'test:telemetry' + 'test:telemetry', + 'rusty-search' ] /**