Skip to content

Commit d6f2611

Browse files
authored
Merge branch 'trunk' into renovate/bazel-7.x
2 parents d7d62d2 + 9c9cd27 commit d6f2611

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

java/src/org/openqa/selenium/grid/distributor/GridModel.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,12 @@ public void release(SessionId id) {
400400
}
401401
}
402402

403-
public void reserve(NodeStatus status, Slot slot) {
403+
/**
404+
* A helper to reserve a slot of a node. The writeLock must be acquired outside to ensure the view
405+
* of the NodeStatus is the current state, otherwise concurrent calls to amend will work with an
406+
* outdated view of slots.
407+
*/
408+
private void reserve(NodeStatus status, Slot slot) {
404409
Instant now = Instant.now();
405410

406411
Slot reserved =
@@ -490,30 +495,29 @@ public void updateHealthCheckCount(NodeId id, Availability availability) {
490495
}
491496
}
492497

498+
/**
499+
* A helper to replace the availability and a slot of a node. The writeLock must be acquired
500+
* outside to ensure the view of the NodeStatus is the current state, otherwise concurrent calls
501+
* to amend will work with an outdated view of slots.
502+
*/
493503
private void amend(Availability availability, NodeStatus status, Slot slot) {
494504
Set<Slot> newSlots = new HashSet<>(status.getSlots());
495505
newSlots.removeIf(s -> s.getId().equals(slot.getId()));
496506
newSlots.add(slot);
497507

498508
NodeStatus node = getNode(status.getNodeId());
499509

500-
Lock writeLock = lock.writeLock();
501-
writeLock.lock();
502-
try {
503-
nodes.remove(node);
504-
nodes.add(
505-
new NodeStatus(
506-
status.getNodeId(),
507-
status.getExternalUri(),
508-
status.getMaxSessionCount(),
509-
newSlots,
510-
availability,
511-
status.getHeartbeatPeriod(),
512-
status.getSessionTimeout(),
513-
status.getVersion(),
514-
status.getOsInfo()));
515-
} finally {
516-
writeLock.unlock();
517-
}
510+
nodes.remove(node);
511+
nodes.add(
512+
new NodeStatus(
513+
status.getNodeId(),
514+
status.getExternalUri(),
515+
status.getMaxSessionCount(),
516+
newSlots,
517+
availability,
518+
status.getHeartbeatPeriod(),
519+
status.getSessionTimeout(),
520+
status.getVersion(),
521+
status.getOsInfo()));
518522
}
519523
}

java/src/org/openqa/selenium/grid/distributor/local/LocalDistributor.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ public LocalDistributor add(Node node) {
355355
// An exception occurs if Node heartbeat has started but the server is not ready.
356356
// Unhandled exception blocks the event-bus thread from processing any event henceforth.
357357
NodeStatus initialNodeStatus;
358+
Runnable healthCheck;
358359
try {
359360
initialNodeStatus = node.getStatus();
360361
if (initialNodeStatus.getAvailability() != UP) {
@@ -363,8 +364,17 @@ public LocalDistributor add(Node node) {
363364
// We do not need to add this Node for now.
364365
return this;
365366
}
366-
model.add(initialNodeStatus);
367-
nodes.put(node.getId(), node);
367+
// Extract the health check
368+
healthCheck = asRunnableHealthCheck(node);
369+
Lock writeLock = lock.writeLock();
370+
writeLock.lock();
371+
try {
372+
nodes.put(node.getId(), node);
373+
model.add(initialNodeStatus);
374+
allChecks.put(node.getId(), healthCheck);
375+
} finally {
376+
writeLock.unlock();
377+
}
368378
} catch (Exception e) {
369379
LOG.log(
370380
Debug.getDebugLogLevel(),
@@ -373,10 +383,6 @@ public LocalDistributor add(Node node) {
373383
return this;
374384
}
375385

376-
// Extract the health check
377-
Runnable healthCheck = asRunnableHealthCheck(node);
378-
allChecks.put(node.getId(), healthCheck);
379-
380386
updateNodeStatus(initialNodeStatus, healthCheck);
381387

382388
LOG.info(
@@ -415,7 +421,15 @@ private void updateNodeStatus(NodeStatus status, Runnable healthCheck) {
415421

416422
private Runnable runNodeHealthChecks() {
417423
return () -> {
418-
ImmutableMap<NodeId, Runnable> nodeHealthChecks = ImmutableMap.copyOf(allChecks);
424+
ImmutableMap<NodeId, Runnable> nodeHealthChecks;
425+
Lock readLock = this.lock.readLock();
426+
readLock.lock();
427+
try {
428+
nodeHealthChecks = ImmutableMap.copyOf(allChecks);
429+
} finally {
430+
readLock.unlock();
431+
}
432+
419433
for (Runnable nodeHealthCheck : nodeHealthChecks.values()) {
420434
GuardedRunnable.guard(nodeHealthCheck).run();
421435
}

java/src/org/openqa/selenium/grid/node/local/LocalNode.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,10 @@ public boolean isReady() {
377377
@VisibleForTesting
378378
@ManagedAttribute(name = "CurrentSessions")
379379
public int getCurrentSessionCount() {
380+
// we need the exact size, see javadoc of Cache.size
381+
long n = currentSessions.asMap().values().stream().count();
380382
// It seems wildly unlikely we'll overflow an int
381-
return Math.toIntExact(currentSessions.size());
383+
return Math.toIntExact(n);
382384
}
383385

384386
@VisibleForTesting
@@ -1005,6 +1007,9 @@ public HealthCheck getHealthCheck() {
10051007
public void drain() {
10061008
bus.fire(new NodeDrainStarted(getId()));
10071009
draining = true;
1010+
// Ensure the pendingSessions counter will not be decremented by timed out sessions not included
1011+
// in the currentSessionCount and the NodeDrainComplete will be raised to early.
1012+
currentSessions.cleanUp();
10081013
int currentSessionCount = getCurrentSessionCount();
10091014
if (currentSessionCount == 0) {
10101015
LOG.info("Firing node drain complete message");

0 commit comments

Comments
 (0)