Skip to content
9 changes: 6 additions & 3 deletions java/src/org/openqa/selenium/grid/data/NodeStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,20 @@ public static NodeStatus fromJson(JsonInput input) {
}

public boolean hasCapability(Capabilities caps, SlotMatcher slotMatcher) {
return slots.stream().anyMatch(slot -> slot.isSupporting(caps, slotMatcher));
return this.getAvailability() == Availability.UP
&& slots.stream().anyMatch(slot -> slot.isSupporting(caps, slotMatcher));
}

public boolean hasCapacity() {
return slots.stream().filter(slot -> slot.getSession() != null).count() < maxSessionCount;
return this.getAvailability() == Availability.UP
&& slots.stream().filter(slot -> slot.getSession() != null).count() < maxSessionCount;
}

// Check if the Node's max session limit is not exceeded and has a free slot that supports the
// capability.
public boolean hasCapacity(Capabilities caps, SlotMatcher slotMatcher) {
return slots.stream().filter(slot -> slot.getSession() != null).count() < maxSessionCount
return this.getAvailability() == Availability.UP
&& slots.stream().filter(slot -> slot.getSession() != null).count() < maxSessionCount
&& slots.stream()
.anyMatch(slot -> slot.getSession() == null && slot.isSupporting(caps, slotMatcher));
}
Expand Down
14 changes: 12 additions & 2 deletions java/src/org/openqa/selenium/grid/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -130,7 +131,8 @@ public abstract class Node implements HasReadyState, Routable {
private final URI uri;
private final Duration sessionTimeout;
private final Route routes;
protected boolean draining;
protected final AtomicBoolean draining = new AtomicBoolean(false);
protected final AtomicBoolean registered = new AtomicBoolean(false);

protected Node(
Tracer tracer, NodeId id, URI uri, Secret registrationSecret, Duration sessionTimeout) {
Expand Down Expand Up @@ -271,7 +273,15 @@ public Duration getSessionTimeout() {
}

public boolean isDraining() {
return draining;
return draining.get();
}

public boolean isRegistered() {
return registered.get();
}

public void register() {
registered.set(true);
}

public abstract void drain();
Expand Down
2 changes: 2 additions & 0 deletions java/src/org/openqa/selenium/grid/node/StatusHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
status.hasCapacity(),
"message",
status.hasCapacity() ? "Ready" : "No free slots available",
"registered",
node.isRegistered(),
"node",
status));

Expand Down
8 changes: 3 additions & 5 deletions java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.BuildInfo;
Expand Down Expand Up @@ -74,7 +73,6 @@
public class NodeServer extends TemplateGridServerCommand {

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

HttpHandler readinessCheck =
req -> {
if (node.getStatus().hasCapacity()) {
if (node.isReady() && node.getStatus().hasCapacity()) {
return new HttpResponse()
.setStatus(HTTP_OK)
.setHeader("Content-Type", MediaType.PLAIN_TEXT_UTF_8.toString())
Expand All @@ -147,7 +145,7 @@ protected Handlers createHandlers(Config config) {
NodeAddedEvent.listener(
nodeId -> {
if (node.getId().equals(nodeId)) {
nodeRegistered.set(true);
node.register();
LOG.info("Node has been added");
}
}));
Expand Down Expand Up @@ -237,7 +235,7 @@ public NettyServer start() {
Failsafe.with(registrationPolicy)
.run(
() -> {
if (nodeRegistered.get()) {
if (node.isRegistered()) {
throw new InterruptedException("Stopping registration thread.");
}
HealthCheck.Result check = node.getHealthCheck().check();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public NodeStatus getStatus() {
@Override
public void drain() {
events.fire(new NodeDrainStarted(getId()));
draining = true;
draining.set(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ public void drain() {
AttributeMap attributeMap = tracer.createAttributeMap();
attributeMap.put(AttributeKey.LOGGER_CLASS.getKey(), getClass().getName());
bus.fire(new NodeDrainStarted(getId()));
draining = true;
draining.set(true);
// Ensure the pendingSessions counter will not be decremented by timed out sessions not
// included
// in the currentSessionCount and the NodeDrainComplete will be raised to early.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public void drain() {
HttpResponse res = client.with(addSecret).execute(req);

if (res.getStatus() == HTTP_OK) {
draining = true;
draining.set(true);
}
}

Expand Down
Loading