2424import java .util .Map ;
2525import java .util .Objects ;
2626import java .util .Set ;
27+ import java .util .UUID ;
2728import java .util .concurrent .BlockingQueue ;
2829import java .util .concurrent .CountDownLatch ;
2930import java .util .concurrent .LinkedBlockingQueue ;
@@ -75,7 +76,7 @@ public final class OpenCliWsReverseAgentClient implements AutoCloseable {
7576
7677 private volatile boolean shutdownRequested ;
7778
78- private final AtomicReference <WebSocketClient > activeClient = new AtomicReference <>();
79+ private final AtomicReference <WorkerSession > activeClient = new AtomicReference <>();
7980
8081 private Thread runnerThread ;
8182 private boolean stopping ;
@@ -136,7 +137,7 @@ public synchronized void start() {
136137 */
137138 public void stop () {
138139 long deadline = System .nanoTime () + CLOSE_WAIT_NANOS ;
139- WebSocketClient client ;
140+ WorkerSession client ;
140141 Thread runner ;
141142 synchronized (this ) {
142143 if (shutdownRequested || collectPool .isShutdown ()) {
@@ -171,7 +172,7 @@ public void stop() {
171172 @ Override
172173 public void close () {
173174 long deadline = System .nanoTime () + CLOSE_WAIT_NANOS ;
174- WebSocketClient client ;
175+ WorkerSession client ;
175176 Thread runner ;
176177 synchronized (this ) {
177178 if (collectPool .isShutdown ()) {
@@ -198,10 +199,10 @@ public void close() {
198199 }
199200
200201 // The caller holds this worker's monitor while invalidating admission and the queue.
201- private WebSocketClient stopAdmission () {
202+ private WorkerSession stopAdmission () {
202203 shutdownRequested = true ;
203204 admissionEpoch ++;
204- WebSocketClient client = activeClient .getAndSet (null );
205+ WorkerSession client = activeClient .getAndSet (null );
205206 List <Runnable > pending = new ArrayList <>();
206207 collectPool .getQueue ().drainTo (pending );
207208 for (Runnable waiting : pending ) {
@@ -230,16 +231,17 @@ private void runLoop() {
230231 int attempt = 0 ;
231232 while (!shutdownRequested ) {
232233 attempt ++;
234+ UUID correlationId = UUID .randomUUID ();
233235 try {
234- runOneSession ();
236+ runOneSession (correlationId );
235237 attempt = 0 ;
236238 } catch (InterruptedException e ) {
237239 Thread .currentThread ().interrupt ();
238240 log .info ("opencli ws agent runner interrupted" );
239241 break ;
240242 } catch (Exception e ) {
241243 if (!shutdownRequested ) {
242- log .warn ("opencli ws session failed attempt={}" , attempt );
244+ log .warn ("opencli ws session failed correlationId={} attempt={}" , correlationId , attempt );
243245 }
244246 }
245247 if (shutdownRequested ) {
@@ -260,15 +262,15 @@ private void runLoop() {
260262 log .info ("opencli ws agent runner stopped" );
261263 }
262264
263- private void runOneSession () throws Exception {
265+ private void runOneSession (UUID correlationId ) throws Exception {
264266 validateConnectionProperties ();
265267 URI endpoint = URI .create (OpenCliCenterWsUrls .toAgentWebSocketUrl (
266268 connectionProperties .getCentralApiBaseUrl (), connectionProperties .getWebSocketPath ()));
267269 // The default TLS client verifies the endpoint hostname. Plain WS has session-local identity.
268270 Object centerIdentity = "wss" .equalsIgnoreCase (endpoint .getScheme ()) ? endpoint : new Object ();
269271
270- log .info ("opencli ws connecting" );
271- WorkerSession client = new WorkerSession (endpoint , centerIdentity );
272+ log .info ("opencli ws connecting correlationId={}" , correlationId );
273+ WorkerSession client = new WorkerSession (endpoint , centerIdentity , correlationId );
272274 if (!publishSession (client )) {
273275 return ;
274276 }
@@ -299,14 +301,16 @@ private enum SessionPhase {
299301
300302 private final class WorkerSession extends WebSocketClient {
301303 private final Object centerIdentity ;
304+ private final UUID correlationId ;
302305 private final AtomicReference <SessionPhase > phase = new AtomicReference <>(SessionPhase .CONNECTING );
303306 private final CountDownLatch registered = new CountDownLatch (1 );
304307 private final CountDownLatch sessionClosed = new CountDownLatch (1 );
305308 private final AtomicReference <Throwable > handshakeFailure = new AtomicReference <>();
306309
307- WorkerSession (URI endpoint , Object centerIdentity ) {
310+ WorkerSession (URI endpoint , Object centerIdentity , UUID correlationId ) {
308311 super (endpoint , new Draft_6455 (Collections .emptyList (), maxInboundMessageBytes ));
309312 this .centerIdentity = centerIdentity ;
313+ this .correlationId = correlationId ;
310314 }
311315
312316 @ Override
@@ -328,7 +332,7 @@ public void onMessage(String message) {
328332 try {
329333 handleTextMessage (this , message );
330334 } catch (Exception e ) {
331- log .warn ("handle ws message failed" );
335+ log .warn ("handle ws message failed correlationId={}" , correlationId );
332336 }
333337 }
334338
@@ -339,7 +343,7 @@ public void onClose(int code, String reason, boolean remote) {
339343
340344 @ Override
341345 public void onError (Exception ex ) {
342- log .warn ("WebSocket error" );
346+ log .warn ("WebSocket error correlationId={}" , correlationId );
343347 retire (ex );
344348 }
345349
@@ -364,23 +368,23 @@ void retire(Throwable cause) {
364368 }
365369 }
366370
367- private synchronized boolean publishSession (WebSocketClient client ) {
371+ private synchronized boolean publishSession (WorkerSession client ) {
368372 if (shutdownRequested ) {
369373 return false ;
370374 }
371375 activeClient .set (client );
372376 return true ;
373377 }
374378
375- private boolean isCurrentSession (WebSocketClient client ) {
379+ private boolean isCurrentSession (WorkerSession client ) {
376380 return !shutdownRequested && activeClient .get () == client ;
377381 }
378382
379- private static void closeSession (WebSocketClient client ) {
383+ private static void closeSession (WorkerSession client ) {
380384 try {
381385 client .close ();
382386 } catch (Exception e ) {
383- log .debug ("ws session close failed" );
387+ log .debug ("ws session close failed correlationId={}" , client . correlationId );
384388 }
385389 }
386390
@@ -525,7 +529,7 @@ public void run() {
525529 ObjectNode response = null ;
526530 try {
527531 if (mayStartCollect (epoch )) {
528- response = executeCollect (key .requestId (), message , cancellation );
532+ response = executeCollect (key .requestId (), message , cancellation , socket . correlationId );
529533 }
530534 } finally {
531535 finishCollect (this , response );
@@ -536,7 +540,8 @@ public void run() {
536540 }
537541 }
538542
539- private ObjectNode executeCollect (String requestId , JsonNode msg , OpenCliCancellationToken cancellation ) {
543+ private ObjectNode executeCollect (String requestId , JsonNode msg , OpenCliCancellationToken cancellation ,
544+ UUID correlationId ) {
540545 try {
541546 String site = msg .path ("site" ).asText ("" );
542547 String command = msg .path ("command" ).asText ("" );
@@ -558,7 +563,7 @@ private ObjectNode executeCollect(String requestId, JsonNode msg, OpenCliCancell
558563 ? partial .getStderr () : e .getMessage ();
559564 return result (requestId , false , Collections .emptyList (), err );
560565 } catch (Exception e ) {
561- log .warn ("collect execution failed" );
566+ log .warn ("collect execution failed correlationId={}" , correlationId );
562567 return result (requestId , false , Collections .emptyList (), "collect execution failed" );
563568 }
564569 }
@@ -632,7 +637,7 @@ private void sendResult(WorkerSession ws, ObjectNode result) {
632637 try {
633638 resultSender .accept (ws , MAPPER .writeValueAsString (result ));
634639 } catch (Exception e ) {
635- log .error ("failed to send WS result" );
640+ log .error ("failed to send WS result correlationId={}" , ws . correlationId );
636641 ws .retire (e );
637642 closeSession (ws );
638643 }
0 commit comments