2323import java .util .Map ;
2424import java .util .Objects ;
2525import java .util .Set ;
26+ import java .util .UUID ;
2627import java .util .concurrent .BlockingQueue ;
2728import java .util .concurrent .CountDownLatch ;
2829import java .util .concurrent .LinkedBlockingQueue ;
@@ -74,7 +75,7 @@ public final class OpenCliWsReverseAgentClient implements AutoCloseable {
7475
7576 private volatile boolean shutdownRequested ;
7677
77- private final AtomicReference <WebSocketClient > activeClient = new AtomicReference <>();
78+ private final AtomicReference <WorkerSession > activeClient = new AtomicReference <>();
7879
7980 private Thread runnerThread ;
8081 private boolean stopping ;
@@ -135,7 +136,7 @@ public synchronized void start() {
135136 */
136137 public void stop () {
137138 long deadline = System .nanoTime () + CLOSE_WAIT_NANOS ;
138- WebSocketClient client ;
139+ WorkerSession client ;
139140 Thread runner ;
140141 synchronized (this ) {
141142 if (shutdownRequested || collectPool .isShutdown ()) {
@@ -170,7 +171,7 @@ public void stop() {
170171 @ Override
171172 public void close () {
172173 long deadline = System .nanoTime () + CLOSE_WAIT_NANOS ;
173- WebSocketClient client ;
174+ WorkerSession client ;
174175 Thread runner ;
175176 synchronized (this ) {
176177 if (collectPool .isShutdown ()) {
@@ -197,10 +198,10 @@ public void close() {
197198 }
198199
199200 // The caller holds this worker's monitor while invalidating admission and the queue.
200- private WebSocketClient stopAdmission () {
201+ private WorkerSession stopAdmission () {
201202 shutdownRequested = true ;
202203 admissionEpoch ++;
203- WebSocketClient client = activeClient .getAndSet (null );
204+ WorkerSession client = activeClient .getAndSet (null );
204205 List <Runnable > pending = new ArrayList <>();
205206 collectPool .getQueue ().drainTo (pending );
206207 for (Runnable waiting : pending ) {
@@ -229,16 +230,17 @@ private void runLoop() {
229230 int attempt = 0 ;
230231 while (!shutdownRequested ) {
231232 attempt ++;
233+ UUID correlationId = UUID .randomUUID ();
232234 try {
233- runOneSession ();
235+ runOneSession (correlationId );
234236 attempt = 0 ;
235237 } catch (InterruptedException e ) {
236238 Thread .currentThread ().interrupt ();
237239 log .info ("opencli ws agent runner interrupted" );
238240 break ;
239241 } catch (Exception e ) {
240242 if (!shutdownRequested ) {
241- log .warn ("opencli ws session failed attempt={}" , attempt );
243+ log .warn ("opencli ws session failed correlationId={} attempt={}" , correlationId , attempt );
242244 }
243245 }
244246 if (shutdownRequested ) {
@@ -259,15 +261,15 @@ private void runLoop() {
259261 log .info ("opencli ws agent runner stopped" );
260262 }
261263
262- private void runOneSession () throws Exception {
264+ private void runOneSession (UUID correlationId ) throws Exception {
263265 validateConnectionProperties ();
264266 URI endpoint = URI .create (OpenCliCenterWsUrls .toAgentWebSocketUrl (
265267 connectionProperties .getCentralApiBaseUrl (), connectionProperties .getWebSocketPath ()));
266268 // The default TLS client verifies the endpoint hostname. Plain WS has session-local identity.
267269 Object centerIdentity = "wss" .equalsIgnoreCase (endpoint .getScheme ()) ? endpoint : new Object ();
268270
269- log .info ("opencli ws connecting" );
270- WorkerSession client = new WorkerSession (endpoint , centerIdentity );
271+ log .info ("opencli ws connecting correlationId={}" , correlationId );
272+ WorkerSession client = new WorkerSession (endpoint , centerIdentity , correlationId );
271273 if (!publishSession (client )) {
272274 return ;
273275 }
@@ -298,14 +300,16 @@ private enum SessionPhase {
298300
299301 private final class WorkerSession extends WebSocketClient {
300302 private final Object centerIdentity ;
303+ private final UUID correlationId ;
301304 private final AtomicReference <SessionPhase > phase = new AtomicReference <>(SessionPhase .CONNECTING );
302305 private final CountDownLatch registered = new CountDownLatch (1 );
303306 private final CountDownLatch sessionClosed = new CountDownLatch (1 );
304307 private final AtomicReference <Throwable > handshakeFailure = new AtomicReference <>();
305308
306- WorkerSession (URI endpoint , Object centerIdentity ) {
309+ WorkerSession (URI endpoint , Object centerIdentity , UUID correlationId ) {
307310 super (endpoint , new Draft_6455 (Collections .emptyList (), maxInboundMessageBytes ));
308311 this .centerIdentity = centerIdentity ;
312+ this .correlationId = correlationId ;
309313 }
310314
311315 @ Override
@@ -327,7 +331,7 @@ public void onMessage(String message) {
327331 try {
328332 handleTextMessage (this , message );
329333 } catch (Exception e ) {
330- log .warn ("handle ws message failed" );
334+ log .warn ("handle ws message failed correlationId={}" , correlationId );
331335 }
332336 }
333337
@@ -338,7 +342,7 @@ public void onClose(int code, String reason, boolean remote) {
338342
339343 @ Override
340344 public void onError (Exception ex ) {
341- log .warn ("WebSocket error" );
345+ log .warn ("WebSocket error correlationId={}" , correlationId );
342346 retire (ex );
343347 }
344348
@@ -363,23 +367,23 @@ void retire(Throwable cause) {
363367 }
364368 }
365369
366- private synchronized boolean publishSession (WebSocketClient client ) {
370+ private synchronized boolean publishSession (WorkerSession client ) {
367371 if (shutdownRequested ) {
368372 return false ;
369373 }
370374 activeClient .set (client );
371375 return true ;
372376 }
373377
374- private boolean isCurrentSession (WebSocketClient client ) {
378+ private boolean isCurrentSession (WorkerSession client ) {
375379 return !shutdownRequested && activeClient .get () == client ;
376380 }
377381
378- private static void closeSession (WebSocketClient client ) {
382+ private static void closeSession (WorkerSession client ) {
379383 try {
380384 client .close ();
381385 } catch (Exception e ) {
382- log .debug ("ws session close failed" );
386+ log .debug ("ws session close failed correlationId={}" , client . correlationId );
383387 }
384388 }
385389
@@ -524,7 +528,7 @@ public void run() {
524528 ObjectNode response = null ;
525529 try {
526530 if (mayStartCollect (epoch )) {
527- response = executeCollect (key .requestId (), message , cancellation );
531+ response = executeCollect (key .requestId (), message , cancellation , socket . correlationId );
528532 }
529533 } finally {
530534 finishCollect (this , response );
@@ -535,7 +539,8 @@ public void run() {
535539 }
536540 }
537541
538- private ObjectNode executeCollect (String requestId , JsonNode msg , OpenCliCancellationToken cancellation ) {
542+ private ObjectNode executeCollect (String requestId , JsonNode msg , OpenCliCancellationToken cancellation ,
543+ UUID correlationId ) {
539544 try {
540545 String site = msg .path ("site" ).asText ("" );
541546 String command = msg .path ("command" ).asText ("" );
@@ -557,7 +562,7 @@ private ObjectNode executeCollect(String requestId, JsonNode msg, OpenCliCancell
557562 ? partial .getStderr () : e .getMessage ();
558563 return result (requestId , false , Collections .emptyList (), err );
559564 } catch (Exception e ) {
560- log .warn ("collect execution failed" );
565+ log .warn ("collect execution failed correlationId={}" , correlationId );
561566 return result (requestId , false , Collections .emptyList (), "collect execution failed" );
562567 }
563568 }
@@ -629,7 +634,7 @@ private void sendResult(WorkerSession ws, ObjectNode result) {
629634 try {
630635 resultSender .accept (ws , MAPPER .writeValueAsString (result ));
631636 } catch (Exception e ) {
632- log .error ("failed to send WS result" );
637+ log .error ("failed to send WS result correlationId={}" , ws . correlationId );
633638 ws .retire (e );
634639 closeSession (ws );
635640 }
0 commit comments