Skip to content

Commit

Permalink
[grid] Update for node shutdown gracefully
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>
  • Loading branch information
VietND96 committed Oct 21, 2024
1 parent 6ea5651 commit d066ac7
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,30 @@

package org.openqa.selenium.grid.node;

import static org.openqa.selenium.remote.HttpSessionId.getSessionId;

import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.http.Routable;

class ForwardWebDriverCommand implements HttpHandler {
class ForwardWebDriverCommand implements HttpHandler, Routable {

private final Node node;

ForwardWebDriverCommand(Node node) {
this.node = Require.nonNull("Node", node);
}

@Override
public boolean matches(HttpRequest req) {
return getSessionId(req.getUri())
.map(id -> node.isSessionOwner(new SessionId(id)))
.orElse(false);
}

@Override
public HttpResponse execute(HttpRequest req) {
return node.executeWebDriverCommand(req);
Expand Down
9 changes: 1 addition & 8 deletions java/src/org/openqa/selenium/grid/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,7 @@ protected Node(
Json json = new Json();
routes =
combine(
// "getSessionId" is aggressive about finding session ids, so this needs to be the last
// route that is checked.
matching(
req ->
getSessionId(req.getUri())
.map(SessionId::new)
.map(this::isSessionOwner)
.orElse(false))
matching(req -> getSessionId(req.getUri()).map(SessionId::new).isPresent())
.to(() -> new ForwardWebDriverCommand(this))
.with(spanDecorator("node.forward_command")),
new CustomLocatorHandler(this, registrationSecret, customLocators),
Expand Down
17 changes: 13 additions & 4 deletions java/src/org/openqa/selenium/grid/node/local/LocalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,13 @@ protected LocalNode(
heartbeatPeriod.getSeconds(),
TimeUnit.SECONDS);

Runtime.getRuntime().addShutdownHook(new Thread(this::stopAllSessions));
Runtime.getRuntime()
.addShutdownHook(
new Thread(
() -> {
stopAllSessions();
drain();
}));
new JMXHelper().register(this);
}

Expand All @@ -316,7 +322,6 @@ private void stopTimedOutSession(RemovalNotification<SessionId, SessionSlot> not
}
// Attempt to stop the session
slot.stop();
this.sessionToDownloadsDir.invalidate(id);
// Decrement pending sessions if Node is draining
if (this.isDraining()) {
int done = pendingSessions.decrementAndGet();
Expand Down Expand Up @@ -473,8 +478,6 @@ public Either<WebDriverException, CreateSessionResponse> newSession(
sessionToDownloadsDir.put(session.getId(), uuidForSessionDownloads);
currentSessions.put(session.getId(), slotToUse);

checkSessionCount();

SessionId sessionId = session.getId();
Capabilities caps = session.getCapabilities();
SESSION_ID.accept(span, sessionId);
Expand Down Expand Up @@ -513,6 +516,8 @@ public Either<WebDriverException, CreateSessionResponse> newSession(
span.addEvent("Unable to create session with the driver", attributeMap);
return Either.left(possibleSession.left());
}
} finally {
checkSessionCount();
}
}

Expand Down Expand Up @@ -765,6 +770,10 @@ public HttpResponse uploadFile(HttpRequest req, SessionId id) {
public void stop(SessionId id) throws NoSuchSessionException {
Require.nonNull("Session ID", id);

if (sessionToDownloadsDir.getIfPresent(id) != null) {
sessionToDownloadsDir.invalidate(id);
}

SessionSlot slot = currentSessions.getIfPresent(id);
if (slot == null) {
throw new NoSuchSessionException("Cannot find session with id: " + id);
Expand Down
20 changes: 20 additions & 0 deletions java/test/org/openqa/selenium/grid/e2e/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "JUNIT5_DEPS", "java_selenium_test_suite")

java_selenium_test_suite(
name = "large-tests",
size = "large",
srcs = glob(["*.java"]),
browsers = [
"chrome",
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/chrome",
"//java/src/org/openqa/selenium/grid",
"//java/src/org/openqa/selenium/remote/http",
artifact("com.google.guava:guava"),
artifact("org.junit.jupiter:junit-jupiter-api"),
artifact("org.assertj:assertj-core"),
] + JUNIT5_DEPS,
)
51 changes: 51 additions & 0 deletions java/test/org/openqa/selenium/grid/e2e/SessionTimeoutTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.e2e;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.URI;
import org.assertj.core.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.grid.Bootstrap;
import org.openqa.selenium.remote.RemoteWebDriver;

public class SessionTimeoutTest {
@Test
void testSessionTimeout() throws Exception {
Assumptions.assumeThat(System.getProperty("webdriver.chrome.binary")).isNull();
Bootstrap.main(("hub --host 127.0.0.1 --port 4444").split(" "));
Bootstrap.main(
("node --host 127.0.0.1 --port 5555 --session-timeout 15 --selenium-manager true")
.split(" "));

var options = new ChromeOptions();
options.addArguments("--disable-search-engine-choice-screen");
options.addArguments("--headless=new");

WebDriver driver = new RemoteWebDriver(URI.create("http://localhost:4444").toURL(), options);
driver.get("http://localhost:4444");
Thread.sleep(15000);
NoSuchSessionException exception = assertThrows(NoSuchSessionException.class, driver::getTitle);
assertTrue(exception.getMessage().startsWith("Cannot find session with id:"));
}
}
44 changes: 41 additions & 3 deletions java/test/org/openqa/selenium/grid/node/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.remote.http.Contents.string;
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
Expand Down Expand Up @@ -102,7 +104,9 @@ class NodeTest {
private Tracer tracer;
private EventBus bus;
private LocalNode local;
private LocalNode local2;
private Node node;
private Node node2;
private ImmutableCapabilities stereotype;
private ImmutableCapabilities caps;
private URI uri;
Expand Down Expand Up @@ -150,6 +154,7 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
builder = builder.enableManagedDownloads(true).sessionTimeout(Duration.ofSeconds(1));
}
local = builder.build();
local2 = builder.build();

node =
new RemoteNode(
Expand All @@ -160,6 +165,16 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
registrationSecret,
local.getSessionTimeout(),
ImmutableSet.of(caps));

node2 =
new RemoteNode(
tracer,
new PassthroughHttpClient.Factory(local2),
new NodeId(UUID.randomUUID()),
uri,
registrationSecret,
local2.getSessionTimeout(),
ImmutableSet.of(caps));
}

@Test
Expand Down Expand Up @@ -371,13 +386,36 @@ void shouldOnlyRespondToWebDriverCommandsForSessionsTheNodeOwns() {
assertThatEither(response).isRight();
Session session = response.right().getSession();

Either<WebDriverException, CreateSessionResponse> response2 =
node2.newSession(createSessionRequest(caps));
assertThatEither(response2).isRight();
Session session2 = response2.right().getSession();

// Assert that should respond to commands for sessions Node 1 owns
HttpRequest req = new HttpRequest(POST, String.format("/session/%s/url", session.getId()));
assertThat(local.matches(req)).isTrue();
assertThat(node.matches(req)).isTrue();

req = new HttpRequest(POST, String.format("/session/%s/url", UUID.randomUUID()));
assertThat(local.matches(req)).isFalse();
assertThat(node.matches(req)).isFalse();
// Assert that should respond to commands for sessions Node 2 owns
HttpRequest req2 = new HttpRequest(POST, String.format("/session/%s/url", session2.getId()));
assertThat(local2.matches(req2)).isTrue();
assertThat(node2.matches(req2)).isTrue();

// Assert that should not respond to commands for sessions Node 1 does not own
NoSuchSessionException exception =
assertThrows(NoSuchSessionException.class, () -> node.execute(req2));
assertTrue(
exception
.getMessage()
.startsWith(String.format("Cannot find session with id: %s", session2.getId())));

// Assert that should not respond to commands for sessions Node 2 does not own
NoSuchSessionException exception2 =
assertThrows(NoSuchSessionException.class, () -> node2.execute(req));
assertTrue(
exception2
.getMessage()
.startsWith(String.format("Cannot find session with id: %s", session.getId())));
}

@Test
Expand Down

0 comments on commit d066ac7

Please sign in to comment.