Skip to content

Commit 01bf373

Browse files
pujaganidiemol
andauthored
[java] Deprecate max-threads flag. Add an alternate flag in the distributor for new session thread pool size. (#10995)
* [java] Make new session creation threadpool size configurable * [java] Deprecate max-threads flag Co-authored-by: Diego Molina <[email protected]>
1 parent 02b23e0 commit 01bf373

File tree

20 files changed

+207
-60
lines changed

20 files changed

+207
-60
lines changed

java/src/org/openqa/selenium/grid/commands/EventBusCommand.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.Set;
4646
import java.util.concurrent.CountDownLatch;
4747
import java.util.concurrent.TimeUnit;
48+
import java.util.logging.Level;
4849
import java.util.logging.Logger;
4950

5051
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
@@ -137,6 +138,13 @@ public Server<?> asServer(Config initialConfig) {
137138
protected void execute(Config config) {
138139
Require.nonNull("Config", config);
139140

141+
config.get("server", "max-threads")
142+
.ifPresent(value -> LOG.log(Level.WARNING,
143+
() ->
144+
"Support for max-threads flag is deprecated. " +
145+
"The intent of the flag is to set the thread pool size in the Distributor. " +
146+
"Please use newsession-threadpool-size flag instead."));
147+
140148
Server<?> server = asServer(config);
141149
server.start();
142150

java/src/org/openqa/selenium/grid/commands/Hub.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.net.URL;
6363
import java.util.Collections;
6464
import java.util.Set;
65+
import java.util.logging.Level;
6566
import java.util.logging.Logger;
6667

6768
import static java.net.HttpURLConnection.HTTP_OK;
@@ -163,7 +164,8 @@ protected Handlers createHandlers(Config config) {
163164
secret,
164165
distributorOptions.getHealthCheckInterval(),
165166
distributorOptions.shouldRejectUnsupportedCaps(),
166-
newSessionRequestOptions.getSessionRequestRetryInterval());
167+
newSessionRequestOptions.getSessionRequestRetryInterval(),
168+
distributorOptions.getNewSessionThreadPoolSize());
167169
handler.addHandler(distributor);
168170

169171
Router router = new Router(tracer, clientFactory, sessions, queue, distributor);
@@ -207,6 +209,13 @@ protected Handlers createHandlers(Config config) {
207209
protected void execute(Config config) {
208210
Require.nonNull("Config", config);
209211

212+
config.get("server", "max-threads")
213+
.ifPresent(value -> LOG.log(Level.WARNING,
214+
() ->
215+
"Support for max-threads flag is deprecated. " +
216+
"The intent of the flag is to set the thread pool size in the Distributor. " +
217+
"Please use newsession-threadpool-size flag instead."));
218+
210219
Server<?> server = asServer(config).start();
211220

212221
LOG.info(String.format("Started Selenium Hub %s: %s", getServerVersion(), server.getUrl()));

java/src/org/openqa/selenium/grid/commands/Standalone.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@
6565
import java.net.URI;
6666
import java.net.URL;
6767
import java.util.Collections;
68+
import java.util.Optional;
6869
import java.util.Set;
70+
import java.util.logging.Level;
6971
import java.util.logging.Logger;
7072

7173
import static java.net.HttpURLConnection.HTTP_OK;
@@ -162,7 +164,8 @@ protected Handlers createHandlers(Config config) {
162164
registrationSecret,
163165
distributorOptions.getHealthCheckInterval(),
164166
distributorOptions.shouldRejectUnsupportedCaps(),
165-
newSessionRequestOptions.getSessionRequestRetryInterval());
167+
newSessionRequestOptions.getSessionRequestRetryInterval(),
168+
distributorOptions.getNewSessionThreadPoolSize());
166169
combinedHandler.addHandler(distributor);
167170

168171
Routable router = new Router(tracer, clientFactory, sessions, queue, distributor)
@@ -232,6 +235,13 @@ protected Handlers createHandlers(Config config) {
232235
protected void execute(Config config) {
233236
Require.nonNull("Config", config);
234237

238+
config.get("server", "max-threads")
239+
.ifPresent(value -> LOG.log(Level.WARNING,
240+
() ->
241+
"Support for max-threads flag is deprecated. " +
242+
"The intent of the flag is to set the thread pool size in the Distributor. " +
243+
"Please use newsession-threadpool-size flag instead."));
244+
235245
Server<?> server = asServer(config).start();
236246

237247
LOG.info(String.format(

java/src/org/openqa/selenium/grid/distributor/config/DistributorFlags.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
3434
import static org.openqa.selenium.grid.distributor.config.DistributorOptions.DEFAULT_DISTRIBUTOR_IMPLEMENTATION;
3535
import static org.openqa.selenium.grid.distributor.config.DistributorOptions.DEFAULT_HEALTHCHECK_INTERVAL;
36+
import static org.openqa.selenium.grid.distributor.config.DistributorOptions.DEFAULT_NEWSESSION_THREADPOOL_SIZE;
3637
import static org.openqa.selenium.grid.distributor.config.DistributorOptions.DEFAULT_REJECT_UNSUPPORTED_CAPS;
3738
import static org.openqa.selenium.grid.distributor.config.DistributorOptions.DEFAULT_SLOT_MATCHER;
3839
import static org.openqa.selenium.grid.distributor.config.DistributorOptions.DEFAULT_SLOT_SELECTOR_IMPLEMENTATION;
@@ -92,6 +93,15 @@ public class DistributorFlags implements HasRoles {
9293
@ConfigValue(section = DISTRIBUTOR_SECTION, name = "reject-unsupported-caps", example = "true")
9394
private boolean rejectUnsupportedCaps = DEFAULT_REJECT_UNSUPPORTED_CAPS;
9495

96+
@Parameter(
97+
names = {"--newsession-threadpool-size"},
98+
description = "The Distributor uses a fixed-sized thread pool to create new sessions as it consumes new session requests from the queue."
99+
+ "This allows configuring the size of the thread pool. The default value is no. of available processors * 3. "
100+
+ "Note: If the no. of threads is way greater than the available processors it will not always increase the performance. "
101+
+ "A high number of threads causes more context switching which is an expensive operation. ")
102+
@ConfigValue(section = DISTRIBUTOR_SECTION, name = "newsession-threadpool-size", example = "4")
103+
public int newSessionThreadPoolSize = DEFAULT_NEWSESSION_THREADPOOL_SIZE;
104+
95105
@Override
96106
public Set<Role> getRoles() {
97107
return Collections.singleton(DISTRIBUTOR_ROLE);

java/src/org/openqa/selenium/grid/distributor/config/DistributorOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class DistributorOptions {
3838
static final String DEFAULT_SLOT_SELECTOR_IMPLEMENTATION =
3939
"org.openqa.selenium.grid.distributor.selector.DefaultSlotSelector";
4040
static final boolean DEFAULT_REJECT_UNSUPPORTED_CAPS = false;
41+
static final int DEFAULT_NEWSESSION_THREADPOOL_SIZE =
42+
Runtime.getRuntime().availableProcessors() * 3;
4143
private final Config config;
4244

4345
public DistributorOptions(Config config) {
@@ -117,6 +119,14 @@ public SlotSelector getSlotSelector() {
117119
DEFAULT_SLOT_SELECTOR_IMPLEMENTATION);
118120
}
119121

122+
public int getNewSessionThreadPoolSize() {
123+
// If the user sets 0 or less, we default to 1 to ensure Grid is running.
124+
return Math.max(
125+
config.getInt(DISTRIBUTOR_SECTION, "newsession-threadpool-size")
126+
.orElse(DEFAULT_NEWSESSION_THREADPOOL_SIZE),
127+
1);
128+
}
129+
120130
public boolean shouldRejectUnsupportedCaps() {
121131
return config.getBool(DISTRIBUTOR_SECTION,
122132
"reject-unsupported-caps").orElse(DEFAULT_REJECT_UNSUPPORTED_CAPS);

java/src/org/openqa/selenium/grid/distributor/httpd/DistributorServer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import java.util.Collections;
4040
import java.util.Set;
41+
import java.util.logging.Level;
4142
import java.util.logging.Logger;
4243

4344
import static java.net.HttpURLConnection.HTTP_OK;
@@ -116,6 +117,14 @@ protected Handlers createHandlers(Config config) {
116117
protected void execute(Config config) {
117118
Require.nonNull("Config", config);
118119

120+
config.get("server", "max-threads")
121+
.ifPresent(value -> LOG.log(Level.WARNING,
122+
() ->
123+
"Support for max-threads flag is deprecated. " +
124+
"The intent of the flag is to set the thread pool size in the Distributor. " +
125+
"Please use newsession-threadpool-size flag instead."));
126+
127+
119128
Server<?> server = asServer(config).start();
120129

121130
BuildInfo info = new BuildInfo();

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,8 @@ public class LocalDistributor extends Distributor implements Closeable {
168168
return thread;
169169
});
170170

171-
private final Executor sessionCreatorExecutor = Executors.newFixedThreadPool(
172-
Runtime.getRuntime().availableProcessors(),
173-
r -> {
174-
Thread thread = new Thread(r);
175-
thread.setName("Local Distributor - Session Creation");
176-
thread.setDaemon(true);
177-
return thread;
178-
}
179-
);
171+
private final Executor sessionCreatorExecutor;
172+
180173
private final NewSessionQueue sessionQueue;
181174

182175
private final boolean rejectUnsupportedCaps;
@@ -191,7 +184,8 @@ public LocalDistributor(
191184
Secret registrationSecret,
192185
Duration healthcheckInterval,
193186
boolean rejectUnsupportedCaps,
194-
Duration sessionRequestRetryInterval) {
187+
Duration sessionRequestRetryInterval,
188+
int newSessionThreadPoolSize) {
195189
super(tracer, clientFactory, registrationSecret);
196190
this.tracer = Require.nonNull("Tracer", tracer);
197191
this.bus = Require.nonNull("Event bus", bus);
@@ -217,6 +211,16 @@ public LocalDistributor(
217211
}
218212
}));
219213

214+
sessionCreatorExecutor = Executors.newFixedThreadPool(
215+
newSessionThreadPoolSize,
216+
r -> {
217+
Thread thread = new Thread(r);
218+
thread.setName("Local Distributor - Session Creation");
219+
thread.setDaemon(true);
220+
return thread;
221+
}
222+
);
223+
220224
NewSessionRunnable newSessionRunnable = new NewSessionRunnable();
221225
bus.addListener(NodeDrainComplete.listener(this::remove));
222226

@@ -262,7 +266,8 @@ public static Distributor create(Config config) {
262266
secretOptions.getRegistrationSecret(),
263267
distributorOptions.getHealthCheckInterval(),
264268
distributorOptions.shouldRejectUnsupportedCaps(),
265-
newSessionQueueOptions.getSessionRequestRetryInterval());
269+
newSessionQueueOptions.getSessionRequestRetryInterval(),
270+
distributorOptions.getNewSessionThreadPoolSize());
266271
}
267272

268273
@Override

java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.Set;
5959
import java.util.concurrent.Executors;
6060
import java.util.concurrent.atomic.AtomicBoolean;
61+
import java.util.logging.Level;
6162
import java.util.logging.Logger;
6263

6364
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
@@ -227,6 +228,13 @@ public NettyServer start() {
227228
protected void execute(Config config) {
228229
Require.nonNull("Config", config);
229230

231+
config.get("server", "max-threads")
232+
.ifPresent(value -> LOG.log(Level.WARNING,
233+
() ->
234+
"Support for max-threads flag is deprecated. " +
235+
"The intent of the flag is to set the thread pool size in the Distributor. " +
236+
"Please use newsession-threadpool-size flag instead."));
237+
230238
Runtime.getRuntime().addShutdownHook(shutdownHook);
231239
Server<?> server = asServer(config).start();
232240

java/src/org/openqa/selenium/grid/router/httpd/RouterServer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import java.time.Duration;
6262
import java.util.Collections;
6363
import java.util.Set;
64+
import java.util.logging.Level;
6465
import java.util.logging.Logger;
6566

6667
import static java.net.HttpURLConnection.HTTP_OK;
@@ -192,6 +193,14 @@ protected Handlers createHandlers(Config config) {
192193
protected void execute(Config config) {
193194
Require.nonNull("Config", config);
194195

196+
config.get("server", "max-threads")
197+
.ifPresent(value -> LOG.log(Level.WARNING,
198+
() ->
199+
"Support for max-threads flag is deprecated. " +
200+
"The intent of the flag is to set the thread pool size in the Distributor. " +
201+
"Please use newsession-threadpool-size flag instead."));
202+
203+
195204
Server<?> server = asServer(config).start();
196205

197206
LOG.info(String.format(

java/src/org/openqa/selenium/grid/sessionmap/httpd/SessionMapServer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import java.util.Collections;
3737
import java.util.Set;
38+
import java.util.logging.Level;
3839
import java.util.logging.Logger;
3940

4041
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
@@ -103,6 +104,14 @@ protected Handlers createHandlers(Config config) {
103104

104105
@Override
105106
protected void execute(Config config) {
107+
108+
config.get("server", "max-threads")
109+
.ifPresent(value -> LOG.log(Level.WARNING,
110+
() ->
111+
"Support for max-threads flag is deprecated. " +
112+
"The intent of the flag is to set the thread pool size in the Distributor. " +
113+
"Please use newsession-threadpool-size flag instead."));
114+
106115
Server<?> server = asServer(config);
107116
server.start();
108117

0 commit comments

Comments
 (0)