From ab2e7740853bfc11cf410955e3132556febb0fad Mon Sep 17 00:00:00 2001 From: Viet Nguyen Duc Date: Tue, 4 Mar 2025 14:39:24 +0700 Subject: [PATCH 1/2] [grid] Node server graceful shutdown Signed-off-by: Viet Nguyen Duc --- java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java b/java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java index 87bba4e4be1f3..fbc11a13f36cc 100644 --- a/java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java +++ b/java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java @@ -50,7 +50,6 @@ import org.openqa.selenium.grid.config.Role; import org.openqa.selenium.grid.data.NodeAddedEvent; import org.openqa.selenium.grid.data.NodeDrainComplete; -import org.openqa.selenium.grid.data.NodeRemovedEvent; import org.openqa.selenium.grid.data.NodeStatusEvent; import org.openqa.selenium.grid.log.LoggingOptions; import org.openqa.selenium.grid.node.HealthCheck; @@ -77,8 +76,7 @@ public class NodeServer extends TemplateGridServerCommand { private final AtomicBoolean nodeRegistered = new AtomicBoolean(false); private Node node; private EventBus bus; - private final Thread shutdownHook = - new Thread(() -> bus.fire(new NodeRemovedEvent(node.getStatus()))); + private final Thread shutdownHook = new Thread(() -> node.drain()); @Override public String getName() { From 3283bff8568ec8a6be77544c899bbffecdcc968c Mon Sep 17 00:00:00 2001 From: Viet Nguyen Duc Date: Wed, 5 Mar 2025 04:23:12 +0700 Subject: [PATCH 2/2] Add traces in LocalSessionMap when consuming Node event Signed-off-by: Viet Nguyen Duc --- .../selenium/grid/node/httpd/NodeServer.java | 4 +- .../sessionmap/local/LocalSessionMap.java | 42 ++++++++++++++----- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java b/java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java index fbc11a13f36cc..87bba4e4be1f3 100644 --- a/java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java +++ b/java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java @@ -50,6 +50,7 @@ import org.openqa.selenium.grid.config.Role; import org.openqa.selenium.grid.data.NodeAddedEvent; import org.openqa.selenium.grid.data.NodeDrainComplete; +import org.openqa.selenium.grid.data.NodeRemovedEvent; import org.openqa.selenium.grid.data.NodeStatusEvent; import org.openqa.selenium.grid.log.LoggingOptions; import org.openqa.selenium.grid.node.HealthCheck; @@ -76,7 +77,8 @@ public class NodeServer extends TemplateGridServerCommand { private final AtomicBoolean nodeRegistered = new AtomicBoolean(false); private Node node; private EventBus bus; - private final Thread shutdownHook = new Thread(() -> node.drain()); + private final Thread shutdownHook = + new Thread(() -> bus.fire(new NodeRemovedEvent(node.getStatus()))); @Override public String getName() { diff --git a/java/src/org/openqa/selenium/grid/sessionmap/local/LocalSessionMap.java b/java/src/org/openqa/selenium/grid/sessionmap/local/LocalSessionMap.java index df62989b5bb4e..9cea2090bf473 100644 --- a/java/src/org/openqa/selenium/grid/sessionmap/local/LocalSessionMap.java +++ b/java/src/org/openqa/selenium/grid/sessionmap/local/LocalSessionMap.java @@ -59,23 +59,45 @@ public LocalSessionMap(Tracer tracer, EventBus bus) { bus.addListener( NodeRemovedEvent.listener( - nodeStatus -> + nodeStatus -> { + try (Span span = + tracer + .getCurrentContext() + .createSpan("local_sessionmap.remove_session_node_removed")) { + AttributeMap attributeMap = tracer.createAttributeMap(); + attributeMap.put(AttributeKey.LOGGER_CLASS.getKey(), getClass().getName()); + span.addEvent( + "Node removed event received. Removing sessions associated with the node", + attributeMap); nodeStatus.getSlots().stream() .filter(slot -> slot.getSession() != null) .map(slot -> slot.getSession().getId()) - .forEach(this::remove))); + .forEach(this::remove); + } + })); bus.addListener( NodeRestartedEvent.listener( previousNodeStatus -> { - List toRemove = - knownSessions.entrySet().stream() - .filter( - (e) -> e.getValue().getUri().equals(previousNodeStatus.getExternalUri())) - .map(Map.Entry::getKey) - .collect(Collectors.toList()); - - toRemove.forEach(this::remove); + try (Span span = + tracer + .getCurrentContext() + .createSpan("local_sessionmap.remove_session_node_restarted")) { + AttributeMap attributeMap = tracer.createAttributeMap(); + attributeMap.put(AttributeKey.LOGGER_CLASS.getKey(), getClass().getName()); + span.addEvent( + "Node restarted event received. Removing sessions associated with the node", + attributeMap); + List toRemove = + knownSessions.entrySet().stream() + .filter( + (e) -> + e.getValue().getUri().equals(previousNodeStatus.getExternalUri())) + .map(Map.Entry::getKey) + .collect(Collectors.toList()); + + toRemove.forEach(this::remove); + } })); }