Skip to content

Commit 493d91c

Browse files
committed
chore: enforce minimal server version
1 parent 03d711f commit 493d91c

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

src/main/java/io/weaviate/client6/v1/api/WeaviateClient.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.weaviate.client6.v1.api.rbac.users.WeaviateUsersClient;
1212
import io.weaviate.client6.v1.internal.ObjectBuilder;
1313
import io.weaviate.client6.v1.internal.TokenProvider;
14+
import io.weaviate.client6.v1.internal.VersionSupport;
1415
import io.weaviate.client6.v1.internal.grpc.DefaultGrpcTransport;
1516
import io.weaviate.client6.v1.internal.grpc.GrpcChannelOptions;
1617
import io.weaviate.client6.v1.internal.grpc.GrpcTransport;
@@ -37,7 +38,7 @@ public class WeaviateClient implements AutoCloseable {
3738

3839
/** Client for {@code /backups} endpoints for managing backups. */
3940
public final WeaviateBackupClient backup;
40-
41+
4142
/**
4243
* Client for {@code /authz/roles} endpoints for managing RBAC roles.
4344
*/
@@ -96,6 +97,10 @@ public WeaviateClient(Config config) {
9697
throw ex;
9798
}
9899

100+
if (!VersionSupport.isSupported(meta.version())) {
101+
throw new WeaviateUnsupportedVersionException(meta.version());
102+
}
103+
99104
if (meta.grpcMaxMessageSize() != null) {
100105
grpcOpt = grpcOpt.withMaxMessageSize(meta.grpcMaxMessageSize());
101106
}

src/main/java/io/weaviate/client6/v1/api/WeaviateClientAsync.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.weaviate.client6.v1.api.rbac.users.WeaviateUsersClientAsync;
1414
import io.weaviate.client6.v1.internal.ObjectBuilder;
1515
import io.weaviate.client6.v1.internal.TokenProvider;
16+
import io.weaviate.client6.v1.internal.VersionSupport;
1617
import io.weaviate.client6.v1.internal.grpc.DefaultGrpcTransport;
1718
import io.weaviate.client6.v1.internal.grpc.GrpcChannelOptions;
1819
import io.weaviate.client6.v1.internal.grpc.GrpcTransport;
@@ -99,6 +100,10 @@ public WeaviateClientAsync(Config config) {
99100
throw ex;
100101
}
101102

103+
if (!VersionSupport.isSupported(meta.version())) {
104+
throw new WeaviateUnsupportedVersionException(meta.version());
105+
}
106+
102107
if (meta.grpcMaxMessageSize() != null) {
103108
grpcOpt = grpcOpt.withMaxMessageSize(meta.grpcMaxMessageSize());
104109
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.weaviate.client6.v1.api;
2+
3+
import io.weaviate.client6.v1.internal.VersionSupport;
4+
5+
/**
6+
* This exception is thrown when the client refuses to talk to an unsupported
7+
* version of the server.
8+
*
9+
* @see VersionSupport#MINIMAL_SUPPORTED_VERSION.
10+
*/
11+
public class WeaviateUnsupportedVersionException extends WeaviateException {
12+
public WeaviateUnsupportedVersionException(String actual) {
13+
this(VersionSupport.MINIMAL_SUPPORTED_VERSION.toString(), actual);
14+
}
15+
16+
public WeaviateUnsupportedVersionException(String minimal, String actual) {
17+
super("Server version %s is not supported. Earliest supported version is %s.".formatted(actual, minimal));
18+
}
19+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.weaviate.client6.v1.internal;
2+
3+
import java.util.Arrays;
4+
5+
public final class VersionSupport {
6+
public static final SemanticVersion MINIMAL_SUPPORTED_VERSION = new SemanticVersion(1, 33);
7+
8+
/**
9+
* Returns true if the {@code version} is the same as or older than the
10+
* {@link VersionSupport#MINIMAL_SUPPORTED_VERSION}.
11+
*/
12+
public static boolean isSupported(String version) {
13+
var semver = SemanticVersion.of(version);
14+
return semver.compareTo(MINIMAL_SUPPORTED_VERSION) >= 0;
15+
}
16+
17+
public record SemanticVersion(int major, int minor, String patch) implements Comparable<SemanticVersion> {
18+
19+
public SemanticVersion(int major, int minor) {
20+
this(major, minor, null);
21+
}
22+
23+
/**
24+
* Parse semantic version from a formatted string, e.g.
25+
* {@code "(v)1.23.6-rc.1"}.
26+
*/
27+
public static SemanticVersion of(String version) {
28+
var parts = version.replaceFirst("v", "").split("\\.");
29+
var major = Integer.valueOf(parts[0].replaceAll("[^0-9]", ""));
30+
var minor = Integer.valueOf(parts[1].replaceAll("[^0-9]", ""));
31+
var patch = String.join(".", Arrays.stream(parts, 2, parts.length).toList());
32+
return new SemanticVersion(major, minor, patch);
33+
}
34+
35+
@Override
36+
public int compareTo(SemanticVersion that) {
37+
var this_v = Integer.valueOf("%d%d".formatted(this.major, this.minor));
38+
var that_v = Integer.valueOf("%d%d".formatted(that.major, that.minor));
39+
return this_v.compareTo(that_v);
40+
}
41+
42+
public String toString() {
43+
return String.join(".", String.valueOf(major), String.valueOf(minor), patch);
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)