Skip to content

Commit

Permalink
[grid] Moving SessionQueue config to a single class
Browse files Browse the repository at this point in the history
  • Loading branch information
diemol committed Sep 6, 2021
1 parent 1117eee commit cf684e4
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 174 deletions.
26 changes: 13 additions & 13 deletions java/src/org/openqa/selenium/grid/commands/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@

package org.openqa.selenium.grid.commands;

import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.EVENT_BUS_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.ROUTER_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_QUEUE_ROLE;
import static org.openqa.selenium.remote.http.Route.combine;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;

Expand Down Expand Up @@ -44,7 +53,7 @@
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap;
import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;
import org.openqa.selenium.grid.sessionqueue.config.SessionRequestOptions;
import org.openqa.selenium.grid.sessionqueue.config.NewSessionQueueOptions;
import org.openqa.selenium.grid.sessionqueue.local.LocalNewSessionQueue;
import org.openqa.selenium.grid.web.CombinedHandler;
import org.openqa.selenium.grid.web.GridUiRoute;
Expand All @@ -64,15 +73,6 @@
import java.util.Set;
import java.util.logging.Logger;

import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.EVENT_BUS_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.ROUTER_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_QUEUE_ROLE;
import static org.openqa.selenium.remote.http.Route.combine;

@AutoService(CliCommand.class)
public class Hub extends TemplateGridServerCommand {

Expand Down Expand Up @@ -144,13 +144,13 @@ protected Handlers createHandlers(Config config) {
networkOptions.getHttpClientFactory(tracer));

DistributorOptions distributorOptions = new DistributorOptions(config);
SessionRequestOptions sessionRequestOptions = new SessionRequestOptions(config);
NewSessionQueueOptions newSessionRequestOptions = new NewSessionQueueOptions(config);
NewSessionQueue queue = new LocalNewSessionQueue(
tracer,
bus,
distributorOptions.getSlotMatcher(),
sessionRequestOptions.getSessionRequestRetryInterval(),
sessionRequestOptions.getSessionRequestTimeout(),
newSessionRequestOptions.getSessionRequestRetryInterval(),
newSessionRequestOptions.getSessionRequestTimeout(),
secret);
handler.addHandler(queue);

Expand Down
26 changes: 13 additions & 13 deletions java/src/org/openqa/selenium/grid/commands/Standalone.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@

package org.openqa.selenium.grid.commands;

import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.NODE_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.ROUTER_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_QUEUE_ROLE;
import static org.openqa.selenium.remote.http.Route.combine;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;

Expand Down Expand Up @@ -46,7 +55,7 @@
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap;
import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;
import org.openqa.selenium.grid.sessionqueue.config.SessionRequestOptions;
import org.openqa.selenium.grid.sessionqueue.config.NewSessionQueueOptions;
import org.openqa.selenium.grid.sessionqueue.local.LocalNewSessionQueue;
import org.openqa.selenium.grid.web.CombinedHandler;
import org.openqa.selenium.grid.web.GridUiRoute;
Expand All @@ -67,15 +76,6 @@
import java.util.Set;
import java.util.logging.Logger;

import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.NODE_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.ROUTER_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_QUEUE_ROLE;
import static org.openqa.selenium.remote.http.Route.combine;

@AutoService(CliCommand.class)
public class Standalone extends TemplateGridServerCommand {

Expand Down Expand Up @@ -142,13 +142,13 @@ protected Handlers createHandlers(Config config) {
combinedHandler.addHandler(sessions);

DistributorOptions distributorOptions = new DistributorOptions(config);
SessionRequestOptions sessionRequestOptions = new SessionRequestOptions(config);
NewSessionQueueOptions newSessionRequestOptions = new NewSessionQueueOptions(config);
NewSessionQueue queue = new LocalNewSessionQueue(
tracer,
bus,
distributorOptions.getSlotMatcher(),
sessionRequestOptions.getSessionRequestRetryInterval(),
sessionRequestOptions.getSessionRequestTimeout(),
newSessionRequestOptions.getSessionRequestRetryInterval(),
newSessionRequestOptions.getSessionRequestTimeout(),
registrationSecret);
combinedHandler.addHandler(queue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package org.openqa.selenium.grid.sessionqueue.config;

import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_QUEUE_ROLE;
import static org.openqa.selenium.grid.sessionqueue.config.NewSessionQueueOptions.DEFAULT_REQUEST_TIMEOUT;
import static org.openqa.selenium.grid.sessionqueue.config.NewSessionQueueOptions.DEFAULT_RETRY_INTERVAL;
import static org.openqa.selenium.grid.sessionqueue.config.NewSessionQueueOptions.SESSION_QUEUE_SECTION;

import com.google.auto.service.AutoService;

Expand All @@ -31,11 +34,12 @@
import java.util.Collections;
import java.util.Set;

@SuppressWarnings("FieldMayBeFinal")
@AutoService(HasRoles.class)
public class NewSessionQueueFlags implements HasRoles {

@Parameter(
names = { "--sq", "--sessionqueue" },
names = {"--sq", "--sessionqueue"},
description = "Address of the session queue server.")
@ConfigValue(section = "sessionqueue", name = "host", example = "\"http://localhost:1237\"")
private URI sessionQueueServer;
Expand All @@ -52,6 +56,20 @@ public class NewSessionQueueFlags implements HasRoles {
@ConfigValue(section = "sessionqueue", name = "hostname", example = "\"localhost\"")
private String sessionQueueServerHost;

@Parameter(
names = {"--session-request-timeout"},
description = "Timeout in seconds. New incoming session request is added to the queue. "
+ "Requests sitting in the queue for longer than the configured time will timeout.")
@ConfigValue(section = SESSION_QUEUE_SECTION, name = "session-request-timeout", example = "5")
private int sessionRequestTimeout = DEFAULT_REQUEST_TIMEOUT;

@Parameter(
names = {"--session-retry-interval"},
description = "Retry interval in seconds. If all slots are busy, new session request " +
"will be retried after the given interval.")
@ConfigValue(section = SESSION_QUEUE_SECTION, name = "session-retry-interval", example = "5")
private int sessionRetryInterval = DEFAULT_RETRY_INTERVAL;

@Override
public Set<Role> getRoles() {
return Collections.singleton(SESSION_QUEUE_ROLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,29 @@

import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.ConfigException;
import org.openqa.selenium.grid.jmx.JMXHelper;
import org.openqa.selenium.grid.jmx.ManagedAttribute;
import org.openqa.selenium.grid.jmx.ManagedService;
import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;

import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.Optional;

@ManagedService(objectName = "org.seleniumhq.grid:type=Config,name=NewSessionQueueConfig",
description = "New session queue config")
public class NewSessionQueueOptions {

static final String SESSION_QUEUE_SECTION = "sessionqueue";
static final int DEFAULT_REQUEST_TIMEOUT = 300;
static final int DEFAULT_RETRY_INTERVAL = 5;

private final Config config;

public NewSessionQueueOptions(Config config) {
this.config = config;
new JMXHelper().register(this);
}

public URI getSessionQueueUri() {
Expand Down Expand Up @@ -77,7 +86,37 @@ public URI getSessionQueueUri() {
}
}

public Duration getSessionRequestTimeout() {
// If the user sets 0 or less, we default to 1s.
int timeout = Math.max(
config.getInt(SESSION_QUEUE_SECTION, "session-request-timeout")
.orElse(DEFAULT_REQUEST_TIMEOUT),
1);

return Duration.ofSeconds(timeout);
}

public Duration getSessionRequestRetryInterval() {
// If the user sets 0 or less, we default to 1s.
int interval = Math.max(
config.getInt(SESSION_QUEUE_SECTION, "session-retry-interval")
.orElse(DEFAULT_REQUEST_TIMEOUT),
1);
return Duration.ofSeconds(interval);
}

@ManagedAttribute(name = "RequestTimeoutSeconds")
public long getRequestTimeoutSeconds() {
return getSessionRequestTimeout().getSeconds();
}

@ManagedAttribute(name = "RetryIntervalSeconds")
public long getRetryIntervalSeconds() {
return getSessionRequestRetryInterval().getSeconds();
}

public NewSessionQueue getSessionQueue(String implementation) {
return config.getClass(SESSION_QUEUE_SECTION, "implementation", NewSessionQueue.class, implementation);
return config
.getClass(SESSION_QUEUE_SECTION, "implementation", NewSessionQueue.class, implementation);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit cf684e4

Please sign in to comment.