Skip to content

Commit e292a09

Browse files
authored
[Java] Add libcuvs <-> cuvs-java version check (#1327)
This PR extends #1314 to add version check (version reported by libcuvs vs. cuvs-java build version) It replaces #1315 which was already reviewed and approved, but got closed before I could rebase it. Authors: - Lorenzo Dematté (https://github.com/ldematte) Approvers: - MithunR (https://github.com/mythrocks) URL: #1327
1 parent 607e9eb commit e292a09

4 files changed

Lines changed: 79 additions & 3 deletions

File tree

java/cuvs-java/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,12 @@
166166
<version>3.4.2</version>
167167
<configuration>
168168
<archive>
169+
<manifest>
170+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
171+
</manifest>
169172
<manifestEntries>
170173
<Multi-Release>true</Multi-Release>
171174
<addClasspath>true</addClasspath>
172-
<mainClass>
173-
com.nvidia.cuvs.examples.CagraExample</mainClass>
174175
</manifestEntries>
175176
</archive>
176177
</configuration>

java/cuvs-java/src/main/java/com/nvidia/cuvs/spi/CuVSServiceProvider.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ static CuVSProvider builtinProvider() {
5151
if (supportedJavaRuntime && supportedOs && supportedArchitecture) {
5252
try {
5353
var cls = Class.forName("com.nvidia.cuvs.spi.JDKProvider");
54-
var ctr = MethodHandles.lookup().findConstructor(cls, MethodType.methodType(void.class));
54+
var ctr =
55+
MethodHandles.lookup()
56+
.findStatic(cls, "create", MethodType.methodType(CuVSProvider.class));
5557
return (CuVSProvider) ctr.invoke();
58+
} catch (ProviderInitializationException e) {
59+
return new UnsupportedProvider("cannot create JDKProvider: " + e.getMessage());
5660
} catch (Throwable e) {
5761
throw new AssertionError(e);
5862
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2025, NVIDIA CORPORATION.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.nvidia.cuvs.spi;
17+
18+
class ProviderInitializationException extends Exception {
19+
ProviderInitializationException(String message, Throwable cause) {
20+
super(message, cause);
21+
}
22+
23+
public ProviderInitializationException(String message) {
24+
super(message);
25+
}
26+
}

java/cuvs-java/src/main/java22/com/nvidia/cuvs/spi/JDKProvider.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
package com.nvidia.cuvs.spi;
1717

1818
import static com.nvidia.cuvs.internal.common.Util.*;
19+
import static com.nvidia.cuvs.internal.panama.headers_h.cuvsVersionGet;
20+
import static com.nvidia.cuvs.internal.panama.headers_h.uint16_t;
1921

2022
import com.nvidia.cuvs.*;
2123
import com.nvidia.cuvs.internal.*;
2224
import com.nvidia.cuvs.internal.common.Util;
25+
import java.io.IOException;
26+
import java.lang.foreign.Arena;
2327
import java.lang.foreign.MemorySegment;
2428
import java.lang.invoke.MethodHandle;
2529
import java.lang.invoke.MethodHandles;
@@ -28,6 +32,8 @@
2832
import java.nio.file.Path;
2933
import java.util.Locale;
3034
import java.util.Objects;
35+
import java.util.jar.JarFile;
36+
import java.util.jar.Manifest;
3137

3238
final class JDKProvider implements CuVSProvider {
3339

@@ -37,6 +43,45 @@ final class JDKProvider implements CuVSProvider {
3743

3844
private static final MethodHandle createNativeDataset$mh = createNativeDatasetBuilder();
3945

46+
static CuVSProvider create() throws Throwable {
47+
var mavenVersion = readCuVSVersionFromManifest();
48+
49+
try (var localArena = Arena.ofConfined()) {
50+
var majorPtr = localArena.allocate(uint16_t);
51+
var minorPtr = localArena.allocate(uint16_t);
52+
var patchPtr = localArena.allocate(uint16_t);
53+
checkCuVSError(cuvsVersionGet(majorPtr, minorPtr, patchPtr), "cuvsVersionGet");
54+
var major = majorPtr.get(uint16_t, 0);
55+
var minor = minorPtr.get(uint16_t, 0);
56+
var patch = patchPtr.get(uint16_t, 0);
57+
58+
var cuvsVersionString = String.format(Locale.ROOT, "%02d.%02d.%d", major, minor, patch);
59+
if (mavenVersion != null && !cuvsVersionString.equals(mavenVersion)) {
60+
throw new ProviderInitializationException(
61+
String.format(
62+
Locale.ROOT,
63+
"libcuvs_c version mismatch: expected [%s], found [%s]",
64+
mavenVersion,
65+
cuvsVersionString));
66+
}
67+
}
68+
return new JDKProvider();
69+
}
70+
71+
/**
72+
* Read cuvs-java version from this Jar Manifest, or null if these are not available
73+
*/
74+
private static String readCuVSVersionFromManifest() {
75+
try (var jarFile =
76+
new JarFile(
77+
JDKProvider.class.getProtectionDomain().getCodeSource().getLocation().getPath())) {
78+
Manifest manifest = jarFile.getManifest();
79+
return manifest.getMainAttributes().getValue("Implementation-Version");
80+
} catch (IOException e) {
81+
return null;
82+
}
83+
}
84+
4085
static MethodHandle createNativeDatasetBuilder() {
4186
try {
4287
var lookup = MethodHandles.lookup();

0 commit comments

Comments
 (0)