Skip to content

Commit cab3a25

Browse files
committed
Polish OpenSergoClient: add client ID && request ID management and improve logs
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
1 parent ebf3d9d commit cab3a25

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

src/main/java/io/opensergo/OpenSergoClient.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.grpc.ManagedChannel;
1919
import io.grpc.ManagedChannelBuilder;
2020
import io.grpc.stub.StreamObserver;
21+
import io.opensergo.log.OpenSergoLogger;
2122
import io.opensergo.proto.transport.v1.OpenSergoUniversalTransportServiceGrpc;
2223
import io.opensergo.proto.transport.v1.SubscribeOpType;
2324
import io.opensergo.proto.transport.v1.SubscribeRequest;
@@ -27,6 +28,9 @@
2728
import io.opensergo.subscribe.SubscribeRegistry;
2829
import io.opensergo.subscribe.SubscribedConfigCache;
2930
import io.opensergo.util.AssertUtils;
31+
import io.opensergo.util.IdentifierUtils;
32+
33+
import java.util.concurrent.atomic.AtomicInteger;
3034

3135
/**
3236
* @author Eric Zhao
@@ -41,6 +45,8 @@ public class OpenSergoClient implements AutoCloseable {
4145
private final SubscribedConfigCache configCache;
4246
private final SubscribeRegistry subscribeRegistry;
4347

48+
private AtomicInteger reqId;
49+
4450
public OpenSergoClient(String host, int port) {
4551
this.channel = ManagedChannelBuilder.forAddress(host, port)
4652
// TODO: support TLS
@@ -49,6 +55,7 @@ public OpenSergoClient(String host, int port) {
4955
this.transportGrpcStub = OpenSergoUniversalTransportServiceGrpc.newStub(channel);
5056
this.configCache = new SubscribedConfigCache();
5157
this.subscribeRegistry = new SubscribeRegistry();
58+
this.reqId = new AtomicInteger(0);
5259
}
5360

5461
public void start() throws Exception {
@@ -71,7 +78,7 @@ public boolean unsubscribeConfig(SubscribeKey subscribeKey) {
7178

7279
if (requestAndResponseWriter == null) {
7380
// TODO: return status that indicates not ready
74-
return false;
81+
throw new IllegalStateException("gRPC stream is not ready");
7582
}
7683
SubscribeRequestTarget subTarget = SubscribeRequestTarget.newBuilder()
7784
.setNamespace(subscribeKey.getNamespace()).setApp(subscribeKey.getApp())
@@ -100,21 +107,25 @@ public boolean subscribeConfig(SubscribeKey subscribeKey, OpenSergoConfigSubscri
100107

101108
if (requestAndResponseWriter == null) {
102109
// TODO: return status that indicates not ready
103-
return false;
110+
throw new IllegalStateException("gRPC stream is not ready");
104111
}
105112
SubscribeRequestTarget subTarget = SubscribeRequestTarget.newBuilder()
106113
.setNamespace(subscribeKey.getNamespace()).setApp(subscribeKey.getApp())
107114
.addKinds(subscribeKey.getKind().getKindName())
108115
.build();
109116
SubscribeRequest request = SubscribeRequest.newBuilder()
117+
.setRequestId(String.valueOf(reqId.incrementAndGet()))
110118
.setTarget(subTarget).setOpType(SubscribeOpType.SUBSCRIBE)
119+
.setIdentifier(IdentifierUtils.generateIdentifier(System.identityHashCode(this)))
111120
.build();
112121
// Send SubscribeRequest
113122
requestAndResponseWriter.onNext(request);
114123

115124
// Register subscriber to local.
116125
if (subscriber != null) {
117126
subscribeRegistry.registerSubscriber(subscribeKey, subscriber);
127+
OpenSergoLogger.info("OpenSergo config subscriber registered, subscribeKey={}, subscriber={}",
128+
subscribeKey, subscriber);
118129
}
119130

120131
return true;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2022, OpenSergo Authors
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 io.opensergo.util;
17+
18+
import java.lang.management.ManagementFactory;
19+
import java.net.Inet4Address;
20+
import java.net.InetAddress;
21+
import java.net.NetworkInterface;
22+
import java.util.Enumeration;
23+
import java.util.concurrent.atomic.AtomicInteger;
24+
25+
import io.opensergo.log.OpenSergoLogger;
26+
27+
/**
28+
* @author xzd
29+
*/
30+
public class IdentifierUtils {
31+
32+
private static String ip;
33+
private static String hostName;
34+
private static int currentPid = -1;
35+
36+
private static final AtomicInteger counter = new AtomicInteger(0);
37+
38+
static {
39+
try {
40+
// Init the host information.
41+
resolveHost();
42+
} catch (Throwable e) {
43+
OpenSergoLogger.error("Failed to resolve local host", e);
44+
}
45+
}
46+
47+
private static void resolveHost() throws Exception {
48+
InetAddress addr = InetAddress.getLocalHost();
49+
hostName = addr.getHostName();
50+
ip = addr.getHostAddress();
51+
if (addr.isLoopbackAddress()) {
52+
// find the first IPv4 Address that not loopback
53+
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
54+
while (interfaces.hasMoreElements()) {
55+
NetworkInterface in = interfaces.nextElement();
56+
Enumeration<InetAddress> addrs = in.getInetAddresses();
57+
while (addrs.hasMoreElements()) {
58+
InetAddress address = addrs.nextElement();
59+
if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
60+
ip = address.getHostAddress();
61+
}
62+
}
63+
}
64+
}
65+
}
66+
67+
public static int getPid() {
68+
if (currentPid < 0) {
69+
resolvePid();
70+
}
71+
return currentPid;
72+
}
73+
74+
private static void resolvePid() {
75+
// Note: this will trigger local host resolve, which might be slow.
76+
String name = ManagementFactory.getRuntimeMXBean().getName();
77+
currentPid = Integer.parseInt(name.split("@")[0]);
78+
}
79+
80+
/**
81+
* Generate a unique identifier of an OpenSergo client.
82+
*
83+
* @return a unique identifier
84+
*/
85+
public static String generateIdentifier(long clientId) {
86+
// format: hostname-ip-pid-clientHash
87+
return String.format("%s-%s-%d-%d", hostName, ip, getPid(), clientId);
88+
}
89+
90+
}

0 commit comments

Comments
 (0)