Skip to content

Commit 32c5445

Browse files
committed
[grid]: Add a new session to SessionMap in Distributor
This has one major advantage: anything that extends Node now doesn't need to be aware of the SessionMap, though it still needs to know enough to fire the close event when the session is finally closed.
1 parent 2806d12 commit 32c5445

File tree

12 files changed

+133
-101
lines changed

12 files changed

+133
-101
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ public Executable configure(String... args) {
114114
Distributor distributor = new LocalDistributor(
115115
tracer,
116116
bus,
117-
clientFactory);
117+
clientFactory,
118+
sessions);
118119
Router router = new Router(tracer, clientFactory, sessions, distributor);
119120

120121
Server<?> server = new BaseServer<>(

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public Executable configure(String... args) {
119119
HttpClient.Factory clientFactory = HttpClient.Factory.createDefault();
120120

121121
SessionMap sessions = new LocalSessionMap(tracer, bus);
122-
Distributor distributor = new LocalDistributor(tracer, bus, clientFactory);
122+
Distributor distributor = new LocalDistributor(tracer, bus, clientFactory, sessions);
123123
Router router = new Router(tracer, clientFactory, sessions, distributor);
124124

125125
String hostName;
@@ -142,8 +142,7 @@ public Executable configure(String... args) {
142142
tracer,
143143
bus,
144144
clientFactory,
145-
localhost,
146-
sessions)
145+
localhost)
147146
.maximumConcurrentSessions(Runtime.getRuntime().availableProcessors() * 3);
148147
nodeFlags.configure(config, clientFactory, node);
149148

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public Executable configure(String... args) {
106106
Distributor distributor = new LocalDistributor(
107107
tracer,
108108
bus,
109-
HttpClient.Factory.createDefault());
109+
HttpClient.Factory.createDefault(),
110+
null);
110111

111112
BaseServerOptions serverOptions = new BaseServerOptions(config);
112113

java/server/src/org/openqa/selenium/grid/distributor/local/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ java_library(
1212
"//java/server/src/org/openqa/selenium/grid/web:web",
1313
"//java/server/src/org/openqa/selenium/grid/node:node",
1414
"//java/server/src/org/openqa/selenium/grid/node/remote:remote",
15+
"//java/server/src/org/openqa/selenium/grid/sessionmap:sessionmap",
1516
"//third_party/java/guava:guava",
1617
],
1718
visibility = [

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.openqa.selenium.grid.distributor.Distributor;
3434
import org.openqa.selenium.grid.distributor.DistributorStatus;
3535
import org.openqa.selenium.grid.node.Node;
36+
import org.openqa.selenium.grid.sessionmap.SessionMap;
3637
import org.openqa.selenium.json.Json;
3738
import org.openqa.selenium.json.JsonOutput;
3839
import org.openqa.selenium.remote.NewSessionPayload;
@@ -66,16 +67,19 @@ public class LocalDistributor extends Distributor {
6667
private final Set<Host> hosts = new HashSet<>();
6768
private final DistributedTracer tracer;
6869
private final EventBus bus;
70+
private final SessionMap sessions;
6971
private final Regularly hostChecker = new Regularly("distributor host checker");
7072
private final Map<UUID, Collection<Runnable>> allChecks = new ConcurrentHashMap<>();
7173

7274
public LocalDistributor(
7375
DistributedTracer tracer,
7476
EventBus bus,
75-
HttpClient.Factory httpClientFactory) {
77+
HttpClient.Factory httpClientFactory,
78+
SessionMap sessions) {
7679
super(tracer, httpClientFactory);
7780
this.tracer = Objects.requireNonNull(tracer);
7881
this.bus = Objects.requireNonNull(bus);
82+
this.sessions = Objects.requireNonNull(sessions);
7983
}
8084

8185
@Override
@@ -107,10 +111,14 @@ public Session newSession(NewSessionPayload payload) throws SessionNotCreatedExc
107111
writeLock.unlock();
108112
}
109113

110-
return selected
114+
Session session = selected
111115
.orElseThrow(
112116
() -> new SessionNotCreatedException("Unable to find provider for session: " + allCaps))
113117
.get();
118+
119+
sessions.add(session);
120+
121+
return session;
114122
}
115123

116124
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ public Executable configure(String... args) {
131131
tracer,
132132
bus,
133133
httpClientFactory,
134-
serverOptions.getExternalUri(),
135-
sessions);
134+
serverOptions.getExternalUri());
136135
nodeFlags.configure(config, httpClientFactory, builder);
137136
LocalNode node = builder.build();
138137

java/server/src/org/openqa/selenium/grid/node/local/BUCK

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ java_library(
88
"//java/server/src/org/openqa/selenium/events:events",
99
"//java/server/src/org/openqa/selenium/grid/config:config",
1010
"//java/server/src/org/openqa/selenium/grid/node:node",
11-
"//java/server/src/org/openqa/selenium/grid/sessionmap:sessionmap",
1211
"//java/server/src/org/openqa/selenium/grid/web:web",
1312
"//third_party/java/beust:jcommander",
1413
"//third_party/java/guava:guava",

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.openqa.selenium.grid.data.Session;
3737
import org.openqa.selenium.grid.data.SessionClosedEvent;
3838
import org.openqa.selenium.grid.node.Node;
39-
import org.openqa.selenium.grid.sessionmap.SessionMap;
39+
import org.openqa.selenium.grid.web.CommandHandler;
4040
import org.openqa.selenium.remote.SessionId;
4141
import org.openqa.selenium.remote.http.HttpClient;
4242
import org.openqa.selenium.remote.http.HttpRequest;
@@ -154,7 +154,7 @@ public Optional<Session> newSession(Capabilities capabilities) {
154154

155155
// The session we return has to look like it came from the node, since we might be dealing
156156
// with a webdriver implementation that only accepts connections from localhost
157-
return Optional.of(new Session(session.getId(), externalUri, session.getCapabilities()));
157+
return Optional.of(createExternalSession(session, externalUri));
158158
}
159159
}
160160

@@ -183,7 +183,7 @@ public Session getSession(SessionId id) throws NoSuchSessionException {
183183

184184
span.addTag("session.capabilities", session.getCapabilities());
185185
span.addTag("session.uri", session.getUri());
186-
return new Session(session.getId(), externalUri, session.getCapabilities());
186+
return createExternalSession(session, externalUri);
187187
}
188188
}
189189

@@ -237,13 +237,20 @@ public void stop(SessionId id) throws NoSuchSessionException {
237237
}
238238
}
239239

240+
private Session createExternalSession(SessionAndHandler other, URI externalUri) {
241+
return new HandledSession(
242+
other.getId(),
243+
externalUri,
244+
other.getCapabilities(),
245+
other.getHandler());
246+
}
247+
240248
private void killSession(Span span, SessionAndHandler session) {
241249
span.addTag("session.id", session.getId());
242250
span.addTag("session.capabilities", session.getCapabilities());
243251
span.addTag("session.uri", session.getUri());
244252

245253
currentSessions.invalidate(session.getId());
246-
session.stop();
247254
// Attempt to stop the session
248255
session.stop();
249256
bus.fire(new SessionClosedEvent(session.getId()));
@@ -289,9 +296,8 @@ public static Builder builder(
289296
DistributedTracer tracer,
290297
EventBus bus,
291298
HttpClient.Factory httpClientFactory,
292-
URI uri,
293-
SessionMap sessions) {
294-
return new Builder(tracer, bus, httpClientFactory, uri, sessions);
299+
URI uri) {
300+
return new Builder(tracer, bus, httpClientFactory, uri);
295301
}
296302

297303
public static class Builder {
@@ -300,7 +306,6 @@ public static class Builder {
300306
private final EventBus bus;
301307
private final HttpClient.Factory httpClientFactory;
302308
private final URI uri;
303-
private final SessionMap sessions;
304309
private final ImmutableList.Builder<SessionFactory> factories;
305310
private int maxCount = Runtime.getRuntime().availableProcessors() * 5;
306311
private Ticker ticker = Ticker.systemTicker();
@@ -311,21 +316,19 @@ public Builder(
311316
DistributedTracer tracer,
312317
EventBus bus,
313318
HttpClient.Factory httpClientFactory,
314-
URI uri,
315-
SessionMap sessions) {
319+
URI uri) {
316320
this.tracer = Objects.requireNonNull(tracer);
317321
this.bus = Objects.requireNonNull(bus);
318322
this.httpClientFactory = Objects.requireNonNull(httpClientFactory);
319323
this.uri = Objects.requireNonNull(uri);
320-
this.sessions = Objects.requireNonNull(sessions);
321324
this.factories = ImmutableList.builder();
322325
}
323326

324327
public Builder add(Capabilities stereotype, Function<Capabilities, Session> factory) {
325328
Objects.requireNonNull(stereotype, "Capabilities must be set.");
326329
Objects.requireNonNull(factory, "Session factory must be set.");
327330

328-
factories.add(new SessionFactory(httpClientFactory, sessions, stereotype, factory));
331+
factories.add(new SessionFactory(httpClientFactory, stereotype, factory));
329332

330333
return this;
331334
}
@@ -388,4 +391,18 @@ public Node build() {
388391
}
389392
}
390393

394+
class HandledSession extends Session implements CommandHandler {
395+
396+
private final CommandHandler handler;
397+
398+
public HandledSession(SessionId id, URI uri, Capabilities caps, CommandHandler handler) {
399+
super(id, uri, caps);
400+
this.handler = Objects.requireNonNull(handler);
401+
}
402+
403+
@Override
404+
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
405+
handler.execute(req, resp);
406+
}
407+
}
391408
}

java/server/src/org/openqa/selenium/grid/node/local/SessionFactory.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.openqa.selenium.Capabilities;
2323
import org.openqa.selenium.ImmutableCapabilities;
2424
import org.openqa.selenium.grid.data.Session;
25-
import org.openqa.selenium.grid.sessionmap.SessionMap;
2625
import org.openqa.selenium.grid.web.CommandHandler;
2726
import org.openqa.selenium.grid.web.ReverseProxyHandler;
2827
import org.openqa.selenium.remote.http.HttpClient;
@@ -38,18 +37,15 @@ class SessionFactory
3837
implements Predicate<Capabilities>, Function<Capabilities, Optional<SessionAndHandler>> {
3938

4039
private final HttpClient.Factory httpClientFactory;
41-
private final SessionMap sessions;
4240
private final Capabilities capabilities;
4341
private final Function<Capabilities, Session> generator;
4442
private volatile boolean available = true;
4543

4644
SessionFactory(
4745
HttpClient.Factory httpClientFactory,
48-
SessionMap sessions,
4946
Capabilities capabilities,
5047
Function<Capabilities, Session> generator) {
5148
this.httpClientFactory = Objects.requireNonNull(httpClientFactory);
52-
this.sessions = Objects.requireNonNull(sessions);
5349
this.capabilities = Objects.requireNonNull(ImmutableCapabilities.copyOf(capabilities));
5450
this.generator = Objects.requireNonNull(generator);
5551
}
@@ -88,7 +84,6 @@ public Optional<SessionAndHandler> apply(Capabilities capabilities) {
8884
this.available = true;
8985
return Optional.empty();
9086
}
91-
sessions.add(session);
9287

9388
CommandHandler handler;
9489
if (session instanceof CommandHandler) {
@@ -104,14 +99,10 @@ public Optional<SessionAndHandler> apply(Capabilities capabilities) {
10499

105100
String killUrl = "/session/" + session.getId();
106101
CommandHandler killingHandler = (req, res) -> {
102+
handler.execute(req, res);
107103
if (req.getMethod() == DELETE && killUrl.equals(req.getUri())) {
108-
try {
109-
sessions.remove(session.getId());
110-
} finally {
111-
available = true;
112-
}
104+
available = true;
113105
}
114-
handler.execute(req, res);
115106
};
116107

117108
return Optional.of(new SessionAndHandler(session, killingHandler));

0 commit comments

Comments
 (0)