Skip to content

Commit

Permalink
[grid] Fixing Node drain when session expires
Browse files Browse the repository at this point in the history
Helps with SeleniumHQ#9845
  • Loading branch information
diemol authored and elgatov committed Jun 27, 2022
1 parent 927f2a7 commit f3e1500
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions java/src/org/openqa/selenium/grid/node/local/LocalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,32 +161,41 @@ private LocalNode(
String.format("%s is %s", uri, status.getAvailability()));
} : healthCheck;

this.tempFileSystems = CacheBuilder.newBuilder()
.expireAfterAccess(sessionTimeout)
.ticker(ticker)
.removalListener((RemovalListener<SessionId, TemporaryFilesystem>) notification -> {
TemporaryFilesystem tempFS = notification.getValue();
tempFS.deleteTemporaryFiles();
tempFS.deleteBaseDir();
})
.build();

this.currentSessions = CacheBuilder.newBuilder()
.expireAfterAccess(sessionTimeout)
.ticker(ticker)
.removalListener((RemovalListener<SessionId, SessionSlot>) notification -> {
if (notification.getKey() != null && notification.getValue() != null) {
// Attempt to stop the session
SessionSlot slot = notification.getValue();
if (!slot.isAvailable()) {
slot.stop();
SessionId sessionId = notification.getKey();
slot.stop();
// Invalidate temp file system
this.tempFileSystems.invalidate(sessionId);
// Decrement pending sessions if Node is draining
if (this.isDraining()) {
int done = pendingSessions.decrementAndGet();
if (done <= 0) {
LOG.info("Node draining complete!");
bus.fire(new NodeDrainComplete(this.getId()));
}
}
} else {
LOG.log(Debug.getDebugLogLevel(), "Received stop session notification with null values");
}
})
.build();

this.tempFileSystems = CacheBuilder.newBuilder()
.expireAfterAccess(sessionTimeout)
.ticker(ticker)
.removalListener((RemovalListener<SessionId, TemporaryFilesystem>) notification -> {
TemporaryFilesystem tempFS = notification.getValue();
tempFS.deleteTemporaryFiles();
tempFS.deleteBaseDir();
})
.build();

ScheduledExecutorService sessionCleanupNodeService =
Executors.newSingleThreadScheduledExecutor(
r -> {
Expand Down Expand Up @@ -486,16 +495,6 @@ public void stop(SessionId id) throws NoSuchSessionException {
}

currentSessions.invalidate(id);
tempFileSystems.invalidate(id);

// Decrement pending sessions if Node is draining
if (this.isDraining()) {
int done = pendingSessions.decrementAndGet();
if (done <= 0) {
LOG.info("Node draining complete!");
bus.fire(new NodeDrainComplete(this.getId()));
}
}
}

private void stopAllSessions() {
Expand Down

0 comments on commit f3e1500

Please sign in to comment.