Skip to content

Commit 9f6c0fe

Browse files
authoredApr 2, 2025··
[grid] Expose register status via Node status response (#15448)
1 parent 98b1553 commit 9f6c0fe

File tree

9 files changed

+27
-15
lines changed

9 files changed

+27
-15
lines changed
 

Diff for: ‎java/src/org/openqa/selenium/grid/distributor/local/LocalDistributor.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,8 @@ private SlotId reserveSlot(RequestId requestId, Capabilities caps) {
704704
}
705705

706706
private boolean isNotSupported(Capabilities caps) {
707-
return getAvailableNodes().stream().noneMatch(node -> node.hasCapability(caps, slotMatcher));
707+
return getAvailableNodes().stream()
708+
.noneMatch(node -> node.hasCapability(caps, slotMatcher) && node.getAvailability() == UP);
708709
}
709710

710711
private boolean reserve(SlotId id) {
@@ -794,7 +795,7 @@ public void run() {
794795
// up starving a session request.
795796
Map<Capabilities, Long> stereotypes =
796797
getAvailableNodes().stream()
797-
.filter(NodeStatus::hasCapacity)
798+
.filter(node -> node.hasCapacity() && node.getAvailability() == UP)
798799
.flatMap(node -> node.getSlots().stream().map(Slot::getStereotype))
799800
.collect(
800801
Collectors.groupingBy(ImmutableCapabilities::copyOf, Collectors.counting()));

Diff for: ‎java/src/org/openqa/selenium/grid/distributor/selector/DefaultSlotSelector.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.openqa.selenium.grid.distributor.selector;
1919

2020
import static com.google.common.collect.ImmutableSet.toImmutableSet;
21+
import static org.openqa.selenium.grid.data.Availability.UP;
2122

2223
import com.google.common.annotations.VisibleForTesting;
2324
import java.util.Comparator;
@@ -48,7 +49,7 @@ public Set<SlotId> selectSlot(
4849
// Nodes).
4950
// After that, Nodes are ordered by their load, last session creation, and their id.
5051
return nodes.stream()
51-
.filter(node -> node.hasCapacity(capabilities, slotMatcher))
52+
.filter(node -> node.hasCapacity(capabilities, slotMatcher) && node.getAvailability() == UP)
5253
.sorted(
5354
Comparator.comparingLong(this::getNumberOfSupportedBrowsers)
5455
// Now sort by node which has the lowest load (natural ordering)

Diff for: ‎java/src/org/openqa/selenium/grid/node/Node.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Map;
3333
import java.util.ServiceLoader;
3434
import java.util.Set;
35+
import java.util.concurrent.atomic.AtomicBoolean;
3536
import java.util.logging.Logger;
3637
import java.util.stream.Collectors;
3738
import java.util.stream.StreamSupport;
@@ -130,7 +131,8 @@ public abstract class Node implements HasReadyState, Routable {
130131
private final URI uri;
131132
private final Duration sessionTimeout;
132133
private final Route routes;
133-
protected boolean draining;
134+
protected final AtomicBoolean draining = new AtomicBoolean(false);
135+
protected final AtomicBoolean registered = new AtomicBoolean(false);
134136

135137
protected Node(
136138
Tracer tracer, NodeId id, URI uri, Secret registrationSecret, Duration sessionTimeout) {
@@ -271,7 +273,15 @@ public Duration getSessionTimeout() {
271273
}
272274

273275
public boolean isDraining() {
274-
return draining;
276+
return draining.get();
277+
}
278+
279+
public boolean isRegistered() {
280+
return registered.get();
281+
}
282+
283+
public void register() {
284+
registered.set(true);
275285
}
276286

277287
public abstract void drain();

Diff for: ‎java/src/org/openqa/selenium/grid/node/StatusHandler.java

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
4747
status.hasCapacity(),
4848
"message",
4949
status.hasCapacity() ? "Ready" : "No free slots available",
50+
"registered",
51+
node.isRegistered(),
5052
"node",
5153
status));
5254

Diff for: ‎java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.util.Set;
3838
import java.util.concurrent.ExecutorService;
3939
import java.util.concurrent.Executors;
40-
import java.util.concurrent.atomic.AtomicBoolean;
4140
import java.util.logging.Level;
4241
import java.util.logging.Logger;
4342
import org.openqa.selenium.BuildInfo;
@@ -74,7 +73,6 @@
7473
public class NodeServer extends TemplateGridServerCommand {
7574

7675
private static final Logger LOG = Logger.getLogger(NodeServer.class.getName());
77-
private final AtomicBoolean nodeRegistered = new AtomicBoolean(false);
7876
private Node node;
7977
private EventBus bus;
8078
private final Thread shutdownHook =
@@ -130,7 +128,7 @@ protected Handlers createHandlers(Config config) {
130128

131129
HttpHandler readinessCheck =
132130
req -> {
133-
if (node.getStatus().hasCapacity()) {
131+
if (node.isReady() && node.getStatus().hasCapacity()) {
134132
return new HttpResponse()
135133
.setStatus(HTTP_OK)
136134
.setHeader("Content-Type", MediaType.PLAIN_TEXT_UTF_8.toString())
@@ -147,7 +145,7 @@ protected Handlers createHandlers(Config config) {
147145
NodeAddedEvent.listener(
148146
nodeId -> {
149147
if (node.getId().equals(nodeId)) {
150-
nodeRegistered.set(true);
148+
node.register();
151149
LOG.info("Node has been added");
152150
}
153151
}));
@@ -237,7 +235,7 @@ public NettyServer start() {
237235
Failsafe.with(registrationPolicy)
238236
.run(
239237
() -> {
240-
if (nodeRegistered.get()) {
238+
if (node.isRegistered()) {
241239
throw new InterruptedException("Stopping registration thread.");
242240
}
243241
HealthCheck.Result check = node.getHealthCheck().check();

Diff for: ‎java/src/org/openqa/selenium/grid/node/k8s/OneShotNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ public NodeStatus getStatus() {
415415
@Override
416416
public void drain() {
417417
events.fire(new NodeDrainStarted(getId()));
418-
draining = true;
418+
draining.set(true);
419419
}
420420

421421
@Override

Diff for: ‎java/src/org/openqa/selenium/grid/node/local/LocalNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ public void drain() {
10151015
AttributeMap attributeMap = tracer.createAttributeMap();
10161016
attributeMap.put(AttributeKey.LOGGER_CLASS.getKey(), getClass().getName());
10171017
bus.fire(new NodeDrainStarted(getId()));
1018-
draining = true;
1018+
draining.set(true);
10191019
// Ensure the pendingSessions counter will not be decremented by timed out sessions not
10201020
// included
10211021
// in the currentSessionCount and the NodeDrainComplete will be raised to early.

Diff for: ‎java/src/org/openqa/selenium/grid/node/remote/RemoteNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public void drain() {
295295
HttpResponse res = client.with(addSecret).execute(req);
296296

297297
if (res.getStatus() == HTTP_OK) {
298-
draining = true;
298+
draining.set(true);
299299
}
300300
}
301301

Diff for: ‎java/test/org/openqa/selenium/grid/router/StressTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void multipleSimultaneousSessions() throws Exception {
137137
executor);
138138
}
139139

140-
CompletableFuture.allOf(futures).get(4, MINUTES);
140+
CompletableFuture.allOf(futures).get(6, MINUTES);
141141
}
142142

143143
@Test
@@ -190,6 +190,6 @@ void multipleSimultaneousSessionsTimedOut() throws Exception {
190190
executor);
191191
}
192192

193-
CompletableFuture.allOf(futures).get(5, MINUTES);
193+
CompletableFuture.allOf(futures).get(6, MINUTES);
194194
}
195195
}

0 commit comments

Comments
 (0)
Please sign in to comment.