Skip to content

Commit ef71789

Browse files
authored
[grid] Update session slot to return Either for error propagation. Fix http response. (#9270)
* [grid] Update http response content to a format recognised by the client end * [grid] Update session slot to return Either for error propagation
1 parent 3862ad0 commit ef71789

File tree

3 files changed

+43
-35
lines changed

3 files changed

+43
-35
lines changed

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

+26-27
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public Either<WebDriverException, CreateSessionResponse> newSession(CreateSessio
303303

304304
// Identify possible slots to use as quickly as possible to enable concurrent session starting
305305
SessionSlot slotToUse = null;
306-
synchronized(factories) {
306+
synchronized (factories) {
307307
for (SessionSlot factory : factories) {
308308
if (!factory.isAvailable() || !factory.test(sessionRequest.getCapabilities())) {
309309
continue;
@@ -323,36 +323,35 @@ public Either<WebDriverException, CreateSessionResponse> newSession(CreateSessio
323323
new RetrySessionRequestException("No slot matched the requested capabilities. All slots are busy."));
324324
}
325325

326-
Optional<ActiveSession> possibleSession = slotToUse.apply(sessionRequest);
327-
328-
if (!possibleSession.isPresent()) {
326+
Either<WebDriverException, ActiveSession> possibleSession = slotToUse.apply(sessionRequest);
327+
328+
if (possibleSession.isRight()) {
329+
ActiveSession session = possibleSession.right();
330+
currentSessions.put(session.getId(), slotToUse);
331+
332+
SessionId sessionId = session.getId();
333+
Capabilities caps = session.getCapabilities();
334+
SESSION_ID.accept(span, sessionId);
335+
CAPABILITIES.accept(span, caps);
336+
String downstream = session.getDownstreamDialect().toString();
337+
String upstream = session.getUpstreamDialect().toString();
338+
String sessionUri = session.getUri().toString();
339+
span.setAttribute(AttributeKey.DOWNSTREAM_DIALECT.getKey(), downstream);
340+
span.setAttribute(AttributeKey.UPSTREAM_DIALECT.getKey(), upstream);
341+
span.setAttribute(AttributeKey.SESSION_URI.getKey(), sessionUri);
342+
343+
// The session we return has to look like it came from the node, since we might be dealing
344+
// with a webdriver implementation that only accepts connections from localhost
345+
Session externalSession = createExternalSession(session, externalUri, slotToUse.isSupportingCdp());
346+
return Either.right(new CreateSessionResponse(
347+
externalSession,
348+
getEncoder(session.getDownstreamDialect()).apply(externalSession)));
349+
} else {
329350
slotToUse.release();
330351
span.setAttribute("error", true);
331-
span.setStatus(Status.NOT_FOUND);
332352
span.addEvent("Unable to create session with the driver", attributeMap);
333-
return Either.left(new SessionNotCreatedException("Unable to create session with the driver"));
353+
return Either.left(possibleSession.left());
334354
}
335-
336-
ActiveSession session = possibleSession.get();
337-
currentSessions.put(session.getId(), slotToUse);
338-
339-
SessionId sessionId = session.getId();
340-
Capabilities caps = session.getCapabilities();
341-
SESSION_ID.accept(span, sessionId);
342-
CAPABILITIES.accept(span, caps);
343-
String downstream = session.getDownstreamDialect().toString();
344-
String upstream = session.getUpstreamDialect().toString();
345-
String sessionUri = session.getUri().toString();
346-
span.setAttribute(AttributeKey.DOWNSTREAM_DIALECT.getKey(), downstream);
347-
span.setAttribute(AttributeKey.UPSTREAM_DIALECT.getKey(), upstream);
348-
span.setAttribute(AttributeKey.SESSION_URI.getKey(), sessionUri);
349-
350-
// The session we return has to look like it came from the node, since we might be dealing
351-
// with a webdriver implementation that only accepts connections from localhost
352-
Session externalSession = createExternalSession(session, externalUri, slotToUse.isSupportingCdp());
353-
return Either.right(new CreateSessionResponse(
354-
externalSession,
355-
getEncoder(session.getDownstreamDialect()).apply(externalSession)));
356355
}
357356
}
358357

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

+16-7
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020
import org.openqa.selenium.Capabilities;
2121
import org.openqa.selenium.ImmutableCapabilities;
2222
import org.openqa.selenium.NoSuchSessionException;
23+
import org.openqa.selenium.RetrySessionRequestException;
24+
import org.openqa.selenium.SessionNotCreatedException;
25+
import org.openqa.selenium.WebDriverException;
2326
import org.openqa.selenium.WebDriverInfo;
2427
import org.openqa.selenium.events.EventBus;
2528
import org.openqa.selenium.grid.data.CreateSessionRequest;
2629
import org.openqa.selenium.grid.data.SessionClosedEvent;
2730
import org.openqa.selenium.grid.node.ActiveSession;
2831
import org.openqa.selenium.grid.node.SessionFactory;
32+
import org.openqa.selenium.internal.Either;
2933
import org.openqa.selenium.internal.Require;
3034
import org.openqa.selenium.remote.SessionId;
3135
import org.openqa.selenium.remote.http.HttpHandler;
@@ -45,7 +49,7 @@
4549

4650
public class SessionSlot implements
4751
HttpHandler,
48-
Function<CreateSessionRequest, Optional<ActiveSession>>,
52+
Function<CreateSessionRequest, Either<WebDriverException, ActiveSession>>,
4953
Predicate<Capabilities> {
5054

5155
private static final Logger LOG = Logger.getLogger(SessionSlot.class.getName());
@@ -126,22 +130,27 @@ public boolean test(Capabilities capabilities) {
126130
}
127131

128132
@Override
129-
public Optional<ActiveSession> apply(CreateSessionRequest sessionRequest) {
133+
public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sessionRequest) {
130134
if (currentSession != null) {
131-
return Optional.empty();
135+
return Either.left(new RetrySessionRequestException("Slot is busy. Try another slot."));
132136
}
133137

134138
if (!test(sessionRequest.getCapabilities())) {
135-
return Optional.empty();
139+
return Either.left(new SessionNotCreatedException("New session request capabilities does match the stereotype."));
136140
}
137141

138142
try {
139143
Optional<ActiveSession> possibleSession = factory.apply(sessionRequest);
140-
possibleSession.ifPresent(session -> currentSession = session);
141-
return possibleSession;
144+
if(possibleSession.isPresent()) {
145+
ActiveSession session = possibleSession.get();
146+
currentSession = session;
147+
return Either.right(session);
148+
} else {
149+
return Either.left(new SessionNotCreatedException("Error while creating session with the driver service."));
150+
}
142151
} catch (Exception e) {
143152
LOG.log(Level.WARNING, "Unable to create session", e);
144-
return Optional.empty();
153+
return Either.left(new SessionNotCreatedException(e.getMessage()));
145154
}
146155
}
147156

java/server/src/org/openqa/selenium/grid/sessionqueue/GetNewSessionResponse.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public HttpResponse add(HttpRequest request) {
143143
private HttpResponse internalErrorResponse(String message) {
144144
return new HttpResponse()
145145
.setStatus(HTTP_INTERNAL_ERROR)
146-
.setContent(asJson(singletonMap("message", message)));
146+
.setContent(asJson(singletonMap("value", singletonMap("message", message))));
147147
}
148148

149149
private void removeRequest(RequestId id) {

0 commit comments

Comments
 (0)