Skip to content

Commit 2550130

Browse files
authored
Merge branch 'main' into feature/system-props
2 parents 311dc69 + 3bfe741 commit 2550130

4 files changed

Lines changed: 90 additions & 40 deletions

File tree

pom.xml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,8 @@
170170
</dependency>
171171
<dependency>
172172
<groupId>org.slf4j</groupId>
173-
<artifactId>slf4j-nop</artifactId>
173+
<artifactId>slf4j-api</artifactId>
174174
<version>${slf4j.version}</version>
175-
<scope>test</scope>
176175
</dependency>
177176
<dependency>
178177
<groupId>org.mock-server</groupId>
@@ -237,13 +236,11 @@
237236
<artifactId>maven-surefire-plugin</artifactId>
238237
<version>3.5.4</version>
239238
<configuration>
240-
<trimStackTrace>false</trimStackTrace>
241-
<parallel>classes</parallel>
242-
<forkCount>4</forkCount>
243-
<reuseForks>true</reuseForks>
244-
<threadCount>1</threadCount>
245-
<perCoreThreadCount>true</perCoreThreadCount>
246-
<runOrder>balanced</runOrder>
239+
<systemPropertyVariables>
240+
<java.util.logging.config.file>
241+
${project.basedir}/src/test/resources/logging.properties
242+
</java.util.logging.config.file>
243+
</systemPropertyVariables>
247244
</configuration>
248245
</plugin>
249246
<plugin>
@@ -546,5 +543,5 @@
546543
<artifactId>central-publishing-maven-plugin</artifactId>
547544
</plugin>
548545
</plugins>
549-
</build>
546+
</build>
550547
</project>

src/main/java/io/weaviate/client6/v1/internal/TransportOptions.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,20 @@ public TrustManagerFactory trustManagerFactory() {
6464
public Proxy proxy() {
6565
return this.proxy;
6666
}
67+
68+
/**
69+
* isWeaviateDomain returns true if the host matches weaviate.io,
70+
* semi.technology, or weaviate.cloud domain.
71+
*/
72+
public static boolean isWeaviateDomain(String host) {
73+
var lower = host.toLowerCase();
74+
return lower.contains("weaviate.io") ||
75+
lower.contains("semi.technology") ||
76+
lower.contains("weaviate.cloud");
77+
}
78+
79+
public static boolean isGoogleCloudDomain(String host) {
80+
var lower = host.toLowerCase();
81+
return lower.contains("gcp");
82+
}
6783
}

src/main/java/io/weaviate/client6/v1/internal/grpc/DefaultGrpcTransport.java

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package io.weaviate.client6.v1.internal.grpc;
22

3+
import static java.util.Objects.requireNonNull;
4+
5+
import java.util.OptionalInt;
6+
import java.util.concurrent.CompletableFuture;
7+
import java.util.concurrent.Executor;
8+
import java.util.concurrent.TimeUnit;
9+
10+
import javax.net.ssl.SSLException;
11+
312
import com.google.common.util.concurrent.FutureCallback;
413
import com.google.common.util.concurrent.Futures;
514
import com.google.common.util.concurrent.ListenableFuture;
@@ -11,11 +20,14 @@
1120
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext;
1221
import io.grpc.stub.AbstractStub;
1322
import io.grpc.stub.MetadataUtils;
23+
import io.grpc.stub.StreamObserver;
1424
import io.weaviate.client6.v1.api.WeaviateApiException;
1525
import io.weaviate.client6.v1.internal.Proxy;
1626
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateGrpc;
1727
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateGrpc.WeaviateBlockingStub;
1828
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateGrpc.WeaviateFutureStub;
29+
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoBatch.BatchStreamReply;
30+
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoBatch.BatchStreamRequest;
1931

2032
import javax.net.ssl.SSLException;
2133
import java.net.InetSocketAddress;
@@ -24,51 +36,48 @@
2436
import java.util.concurrent.TimeUnit;
2537

2638
public final class DefaultGrpcTransport implements GrpcTransport {
39+
/**
40+
* ListenableFuture callbacks are executed
41+
* in the same thread they are called from.
42+
*/
43+
private static final Executor FUTURE_CALLBACK_EXECUTOR = Runnable::run;
44+
45+
private final GrpcChannelOptions transportOptions;
2746
private final ManagedChannel channel;
2847

2948
private final WeaviateBlockingStub blockingStub;
3049
private final WeaviateFutureStub futureStub;
3150

32-
private final GrpcChannelOptions transportOptions;
33-
3451
private TokenCallCredentials callCredentials;
3552

3653
public DefaultGrpcTransport(GrpcChannelOptions transportOptions) {
37-
this.transportOptions = transportOptions;
38-
this.channel = buildChannel(transportOptions);
39-
40-
var blockingStub = WeaviateGrpc.newBlockingStub(channel)
41-
.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(transportOptions.headers()));
42-
43-
var futureStub = WeaviateGrpc.newFutureStub(channel)
44-
.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(transportOptions.headers()));
45-
46-
if (transportOptions.maxMessageSize() != null) {
47-
var max = transportOptions.maxMessageSize();
48-
blockingStub = blockingStub.withMaxInboundMessageSize(max).withMaxOutboundMessageSize(max);
49-
futureStub = futureStub.withMaxInboundMessageSize(max).withMaxOutboundMessageSize(max);
50-
}
54+
requireNonNull(transportOptions, "transportOptions is null");
5155

56+
this.transportOptions = transportOptions;
5257
if (transportOptions.tokenProvider() != null) {
5358
this.callCredentials = new TokenCallCredentials(transportOptions.tokenProvider());
54-
blockingStub = blockingStub.withCallCredentials(callCredentials);
55-
futureStub = futureStub.withCallCredentials(callCredentials);
5659
}
5760

58-
this.blockingStub = blockingStub;
59-
this.futureStub = futureStub;
61+
this.channel = buildChannel(transportOptions);
62+
this.blockingStub = configure(WeaviateGrpc.newBlockingStub(channel));
63+
this.futureStub = configure(WeaviateGrpc.newFutureStub(channel));
6064
}
6165

6266
private <StubT extends AbstractStub<StubT>> StubT applyTimeout(StubT stub, Rpc<?, ?, ?, ?> rpc) {
6367
if (transportOptions.timeout() == null) {
6468
return stub;
6569
}
66-
var timeout = rpc.isInsert()
70+
int timeout = rpc.isInsert()
6771
? transportOptions.timeout().insertSeconds()
6872
: transportOptions.timeout().querySeconds();
6973
return stub.withDeadlineAfter(timeout, TimeUnit.SECONDS);
7074
}
7175

76+
@Override
77+
public OptionalInt maxMessageSizeBytes() {
78+
return transportOptions.maxMessageSize();
79+
}
80+
7281
@Override
7382
public <RequestT, RequestM, ReplyM, ResponseT> ResponseT performRequest(RequestT request,
7483
Rpc<RequestT, RequestM, ResponseT, ReplyM> rpc) {
@@ -98,7 +107,9 @@ public <RequestT, RequestM, ReplyM, ResponseT> CompletableFuture<ResponseT> perf
98107
* reusing the thread in which the original future is completed.
99108
*/
100109
private static final <T> CompletableFuture<T> toCompletableFuture(ListenableFuture<T> listenable) {
101-
var completable = new CompletableFuture<T>();
110+
requireNonNull(listenable, "listenable is null");
111+
112+
CompletableFuture<T> completable = new CompletableFuture<>();
102113
Futures.addCallback(listenable, new FutureCallback<T>() {
103114

104115
@Override
@@ -115,13 +126,14 @@ public void onFailure(Throwable t) {
115126
completable.completeExceptionally(t);
116127
}
117128

118-
}, Runnable::run);
129+
}, FUTURE_CALLBACK_EXECUTOR);
119130
return completable;
120131
}
121132

122133
private static ManagedChannel buildChannel(GrpcChannelOptions transportOptions) {
123-
var channel = NettyChannelBuilder.forAddress(transportOptions.host(), transportOptions.port());
134+
requireNonNull(transportOptions, "transportOptions is null");
124135

136+
NettyChannelBuilder channel = NettyChannelBuilder.forAddress(transportOptions.host(), transportOptions.port());
125137
if (transportOptions.isSecure()) {
126138
channel.useTransportSecurity();
127139
} else {
@@ -163,15 +175,39 @@ private static ManagedChannel buildChannel(GrpcChannelOptions transportOptions)
163175
}
164176

165177
channel.intercept(MetadataUtils.newAttachHeadersInterceptor(transportOptions.headers()));
166-
167178
return channel.build();
168179
}
169180

181+
@Override
182+
public StreamObserver<BatchStreamRequest> createStream(StreamObserver<BatchStreamReply> recv) {
183+
return configure(WeaviateGrpc.newStub(channel)).batchStream(recv);
184+
}
185+
186+
/** Apply common configuration to a stub. */
187+
private <S extends AbstractStub<S>> S configure(S stub) {
188+
requireNonNull(stub, "stub is null");
189+
190+
stub = stub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(transportOptions.headers()));
191+
if (transportOptions.maxMessageSize().isPresent()) {
192+
int max = transportOptions.maxMessageSize().getAsInt();
193+
stub = stub.withMaxInboundMessageSize(max).withMaxOutboundMessageSize(max);
194+
}
195+
if (callCredentials != null) {
196+
stub = stub.withCallCredentials(callCredentials);
197+
}
198+
return stub;
199+
}
200+
170201
@Override
171202
public void close() throws Exception {
172203
channel.shutdown();
173204
if (callCredentials != null) {
174205
callCredentials.close();
175206
}
176207
}
208+
209+
@Override
210+
public String host() {
211+
return transportOptions.host();
212+
}
177213
}

src/main/java/io/weaviate/client6/v1/internal/grpc/GrpcChannelOptions.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.weaviate.client6.v1.internal.grpc;
22

33
import java.util.Map;
4+
import java.util.OptionalInt;
45

56
import javax.net.ssl.TrustManagerFactory;
67

@@ -11,25 +12,25 @@
1112
import io.weaviate.client6.v1.internal.TransportOptions;
1213

1314
public class GrpcChannelOptions extends TransportOptions<Metadata> {
14-
private final Integer maxMessageSize;
15+
private final OptionalInt maxMessageSize;
1516

1617
public GrpcChannelOptions(String scheme, String host, int port, Map<String, String> headers,
1718
TokenProvider tokenProvider, TrustManagerFactory tmf, Timeout timeout, Proxy proxy) {
1819
this(scheme, host, port, buildMetadata(headers), tokenProvider, tmf, null, timeout, proxy);
1920
}
2021

2122
private GrpcChannelOptions(String scheme, String host, int port, Metadata headers,
22-
TokenProvider tokenProvider, TrustManagerFactory tmf, Integer maxMessageSize, Timeout timeout, Proxy proxy) {
23+
TokenProvider tokenProvider, TrustManagerFactory tmf, OptionalInt maxMessageSize, Timeout timeout, Proxy proxy) {
2324
super(scheme, host, port, headers, tokenProvider, tmf, timeout, proxy);
2425
this.maxMessageSize = maxMessageSize;
2526
}
2627

2728
public GrpcChannelOptions withMaxMessageSize(int maxMessageSize) {
28-
return new GrpcChannelOptions(scheme, host, port, headers, tokenProvider, trustManagerFactory, maxMessageSize,
29-
timeout, proxy);
29+
return new GrpcChannelOptions(scheme, host, port, headers, tokenProvider, trustManagerFactory,
30+
OptionalInt.of(maxMessageSize), timeout, proxy);
3031
}
3132

32-
public Integer maxMessageSize() {
33+
public OptionalInt maxMessageSize() {
3334
return maxMessageSize;
3435
}
3536

0 commit comments

Comments
 (0)