30
30
import io .opensergo .util .AssertUtils ;
31
31
import io .opensergo .util .IdentifierUtils ;
32
32
33
+ import java .util .Optional ;
34
+ import java .util .concurrent .TimeUnit ;
33
35
import java .util .concurrent .atomic .AtomicInteger ;
34
36
35
37
/**
@@ -46,6 +48,7 @@ public class OpenSergoClient implements AutoCloseable {
46
48
private final SubscribeRegistry subscribeRegistry ;
47
49
48
50
private AtomicInteger reqId ;
51
+ protected volatile OpensergoClientStatus status ;
49
52
50
53
public OpenSergoClient (String host , int port ) {
51
54
this .channel = ManagedChannelBuilder .forAddress (host , port )
@@ -56,17 +59,68 @@ public OpenSergoClient(String host, int port) {
56
59
this .configCache = new SubscribedConfigCache ();
57
60
this .subscribeRegistry = new SubscribeRegistry ();
58
61
this .reqId = new AtomicInteger (0 );
62
+ status = OpensergoClientStatus .INITIAL ;
63
+ }
64
+
65
+ public void registerSubscribeInfo (OpensergoClientSubscribeInfo subscribeInfo ) {
66
+ // Register subscriber to local.
67
+ if (Optional .of (subscribeInfo .getSubscriberList ()).isPresent () && subscribeInfo .getSubscriberList ().size () > 0 ) {
68
+ subscribeInfo .getSubscriberList ().forEach (subscriber -> {
69
+ this .subscribeRegistry .registerSubscriber (subscribeInfo .getSubscribeKey (), subscriber );
70
+ OpenSergoLogger .info ("OpenSergo subscribeinfo registered, subscribeKey={}, subscriber={}" , subscribeInfo .getSubscribeKey (), subscriber );
71
+
72
+ if (requestAndResponseWriter != null && this .status == OpensergoClientStatus .STARTED ) {
73
+ this .subscribeConfig (subscribeInfo .getSubscribeKey ());
74
+ }
75
+ });
76
+ }
59
77
}
60
78
61
79
public void start () throws Exception {
80
+ OpenSergoLogger .info ("OpensergoClient is starting..." );
81
+
82
+ if (status == OpensergoClientStatus .INITIAL ) {
83
+ OpenSergoLogger .info ("open keepavlive thread" );
84
+ new Thread (this ::keepAlive ).start ();
85
+ }
86
+
87
+ status = OpensergoClientStatus .STARTING ;
88
+
62
89
this .requestAndResponseWriter = transportGrpcStub .withWaitForReady ()
63
- .subscribeConfig (new OpenSergoSubscribeClientObserver (configCache , subscribeRegistry ));
90
+ .subscribeConfig (new OpenSergoSubscribeClientObserver (this ));
91
+
92
+ OpenSergoLogger .info ("begin to subscribe config-data..." );
93
+ this .subscribeRegistry .getSubscriberKeysAll ().forEach (subscribeKey -> {
94
+ this .subscribeConfig (subscribeKey );
95
+ });
96
+
97
+ OpenSergoLogger .info ("openSergoClient is started" );
98
+ status = OpensergoClientStatus .STARTED ;
99
+ }
100
+
101
+ private void keepAlive () {
102
+ try {
103
+ if (status != OpensergoClientStatus .STARTING
104
+ && status != OpensergoClientStatus .STARTED
105
+ && status != OpensergoClientStatus .SHUTDOWN ) {
106
+ OpenSergoLogger .info ("try to restart openSergoClient..." );
107
+ this .start ();
108
+ }
109
+ Thread .sleep (TimeUnit .SECONDS .toMillis (10 ));
110
+ if ( status != OpensergoClientStatus .SHUTDOWN ) {
111
+ keepAlive ();
112
+ }
113
+ } catch (Exception e ) {
114
+ e .printStackTrace ();
115
+ }
64
116
}
65
117
66
118
@ Override
67
119
public void close () throws Exception {
68
120
requestAndResponseWriter .onCompleted ();
69
121
122
+ status = OpensergoClientStatus .SHUTDOWN ;
123
+
70
124
// gracefully drain the requests, then close the connection
71
125
channel .shutdown ();
72
126
}
@@ -77,8 +131,8 @@ public boolean unsubscribeConfig(SubscribeKey subscribeKey) {
77
131
AssertUtils .assertNotNull (subscribeKey .getKind (), "kind cannot be null" );
78
132
79
133
if (requestAndResponseWriter == null ) {
80
- // TODO: return status that indicates not ready
81
- throw new IllegalStateException ( "gRPC stream is not ready" ) ;
134
+ OpenSergoLogger . error ( "Fatal error occurred on OpenSergo gRPC ClientObserver" , new IllegalStateException ( "gRPC stream is not ready" ));
135
+ status = OpensergoClientStatus . INTERRUPTED ;
82
136
}
83
137
SubscribeRequestTarget subTarget = SubscribeRequestTarget .newBuilder ()
84
138
.setNamespace (subscribeKey .getNamespace ()).setApp (subscribeKey .getApp ())
@@ -106,8 +160,8 @@ public boolean subscribeConfig(SubscribeKey subscribeKey, OpenSergoConfigSubscri
106
160
AssertUtils .assertNotNull (subscribeKey .getKind (), "kind cannot be null" );
107
161
108
162
if (requestAndResponseWriter == null ) {
109
- // TODO: return status that indicates not ready
110
- throw new IllegalStateException ( "gRPC stream is not ready" ) ;
163
+ OpenSergoLogger . error ( "Fatal error occurred on OpenSergo gRPC ClientObserver" , new IllegalStateException ( "gRPC stream is not ready" ));
164
+ status = OpensergoClientStatus . INTERRUPTED ;
111
165
}
112
166
SubscribeRequestTarget subTarget = SubscribeRequestTarget .newBuilder ()
113
167
.setNamespace (subscribeKey .getNamespace ()).setApp (subscribeKey .getApp ())
@@ -121,18 +175,15 @@ public boolean subscribeConfig(SubscribeKey subscribeKey, OpenSergoConfigSubscri
121
175
// Send SubscribeRequest
122
176
requestAndResponseWriter .onNext (request );
123
177
124
- // Register subscriber to local.
125
- if (subscriber != null ) {
126
- subscribeRegistry .registerSubscriber (subscribeKey , subscriber );
127
- OpenSergoLogger .info ("OpenSergo config subscriber registered, subscribeKey={}, subscriber={}" ,
128
- subscribeKey , subscriber );
129
- }
130
-
131
178
return true ;
132
179
}
133
180
134
181
public SubscribedConfigCache getConfigCache () {
135
182
return configCache ;
136
183
}
137
184
185
+ public SubscribeRegistry getSubscribeRegistry () {
186
+ return subscribeRegistry ;
187
+ }
188
+
138
189
}
0 commit comments