Skip to content

Commit 32f6df0

Browse files
authored
Support passing request-id and unixmsec to server (#1573) (#1601)
Signed-off-by: yhmo <[email protected]>
1 parent ddf9ba7 commit 32f6df0

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

sdk-core/src/main/java/io/milvus/v2/client/ConnectConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public class ConnectConfig {
6161
private long idleTimeoutMs = TimeUnit.MILLISECONDS.convert(24, TimeUnit.HOURS);
6262

6363
private SSLContext sslContext;
64+
// clientRequestId maintains a map for different threads, each thread can assign a specific id.
65+
// the specific id is passed to the server, from the access log we can know which client calls the interface
66+
private ThreadLocal<String> clientRequestId;
6467

6568
public String getHost() {
6669
io.milvus.utils.URLParser urlParser = new io.milvus.utils.URLParser(this.uri);

sdk-core/src/main/java/io/milvus/v2/utils/ClientUtils.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020
package io.milvus.v2.utils;
2121

22-
import io.grpc.ManagedChannel;
23-
import io.grpc.ManagedChannelBuilder;
24-
import io.grpc.Metadata;
22+
import io.grpc.*;
2523
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
2624
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
2725
import io.grpc.netty.shaded.io.netty.handler.ssl.ApplicationProtocolConfig;
@@ -33,11 +31,7 @@
3331
import io.milvus.client.MilvusServiceClient;
3432
import io.milvus.grpc.*;
3533
import io.milvus.v2.client.ConnectConfig;
36-
import io.grpc.HttpConnectProxiedSocketAddress;
37-
import io.grpc.ProxiedSocketAddress;
38-
import io.grpc.ProxyDetector;
3934
import org.apache.commons.lang3.StringUtils;
40-
import org.jetbrains.annotations.NotNull;
4135
import org.slf4j.Logger;
4236
import org.slf4j.LoggerFactory;
4337

@@ -47,7 +41,9 @@
4741
import java.net.UnknownHostException;
4842
import java.nio.charset.StandardCharsets;
4943
import java.time.LocalDateTime;
44+
import java.util.ArrayList;
5045
import java.util.Base64;
46+
import java.util.List;
5147
import java.util.concurrent.TimeUnit;
5248
import java.net.InetSocketAddress;
5349
import java.net.SocketAddress;
@@ -66,6 +62,30 @@ public ManagedChannel getChannel(ConnectConfig connectConfig){
6662
metadata.put(Metadata.Key.of("dbname", Metadata.ASCII_STRING_MARSHALLER), connectConfig.getDbName());
6763
}
6864

65+
List<ClientInterceptor> clientInterceptors = new ArrayList<>();
66+
clientInterceptors.add(MetadataUtils.newAttachHeadersInterceptor(metadata));
67+
//client interceptor used to fetch client_request_id from threadlocal variable and set it for every grpc request
68+
clientInterceptors.add(new ClientInterceptor() {
69+
@Override
70+
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
71+
return new ForwardingClientCall
72+
.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
73+
@Override
74+
public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) {
75+
String currentMs = String.valueOf(System.currentTimeMillis());
76+
headers.put(Metadata.Key.of("client-request-unixmsec", Metadata.ASCII_STRING_MARSHALLER), currentMs);
77+
if(connectConfig.getClientRequestId() != null) {
78+
String clientID = connectConfig.getClientRequestId().get();
79+
if (!StringUtils.isEmpty(clientID)) {
80+
headers.put(Metadata.Key.of("client_request_id", Metadata.ASCII_STRING_MARSHALLER), clientID);
81+
}
82+
}
83+
super.start(responseListener, headers);
84+
}
85+
};
86+
}
87+
});
88+
6989
try {
7090
if (connectConfig.getSslContext() != null) {
7191
// sslContext from connect config
@@ -77,7 +97,7 @@ public ManagedChannel getChannel(ConnectConfig connectConfig){
7797
.keepAliveTimeout(connectConfig.getKeepAliveTimeoutMs(), TimeUnit.MILLISECONDS)
7898
.keepAliveWithoutCalls(connectConfig.isKeepAliveWithoutCalls())
7999
.idleTimeout(connectConfig.getIdleTimeoutMs(), TimeUnit.MILLISECONDS)
80-
.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata));
100+
.intercept(clientInterceptors);
81101

82102
if (StringUtils.isNotEmpty(connectConfig.getProxyAddress())) {
83103
configureProxy(builder, connectConfig.getProxyAddress());
@@ -104,7 +124,7 @@ public ManagedChannel getChannel(ConnectConfig connectConfig){
104124
.keepAliveTimeout(connectConfig.getKeepAliveTimeoutMs(), TimeUnit.MILLISECONDS)
105125
.keepAliveWithoutCalls(connectConfig.isKeepAliveWithoutCalls())
106126
.idleTimeout(connectConfig.getIdleTimeoutMs(), TimeUnit.MILLISECONDS)
107-
.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata));
127+
.intercept(clientInterceptors);
108128

109129
if (StringUtils.isNotEmpty(connectConfig.getProxyAddress())) {
110130
configureProxy(builder, connectConfig.getProxyAddress());
@@ -130,7 +150,7 @@ public ManagedChannel getChannel(ConnectConfig connectConfig){
130150
.keepAliveTimeout(connectConfig.getKeepAliveTimeoutMs(), TimeUnit.MILLISECONDS)
131151
.keepAliveWithoutCalls(connectConfig.isKeepAliveWithoutCalls())
132152
.idleTimeout(connectConfig.getIdleTimeoutMs(), TimeUnit.MILLISECONDS)
133-
.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata));
153+
.intercept(clientInterceptors);
134154

135155
if (StringUtils.isNotEmpty(connectConfig.getProxyAddress())) {
136156
configureProxy(builder, connectConfig.getProxyAddress());
@@ -152,7 +172,7 @@ public ManagedChannel getChannel(ConnectConfig connectConfig){
152172
.keepAliveTimeout(connectConfig.getKeepAliveTimeoutMs(), TimeUnit.MILLISECONDS)
153173
.keepAliveWithoutCalls(connectConfig.isKeepAliveWithoutCalls())
154174
.idleTimeout(connectConfig.getIdleTimeoutMs(), TimeUnit.MILLISECONDS)
155-
.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata));
175+
.intercept(clientInterceptors);
156176
if (StringUtils.isNotEmpty(connectConfig.getProxyAddress())) {
157177
configureProxy(builder, connectConfig.getProxyAddress());
158178
}

0 commit comments

Comments
 (0)