28
28
import org .openqa .selenium .ImmutableCapabilities ;
29
29
import org .openqa .selenium .NoSuchSessionException ;
30
30
import org .openqa .selenium .PersistentCapabilities ;
31
+ import org .openqa .selenium .RetrySessionRequestException ;
32
+ import org .openqa .selenium .SessionNotCreatedException ;
31
33
import org .openqa .selenium .WebDriverException ;
32
34
import org .openqa .selenium .concurrent .Regularly ;
33
35
import org .openqa .selenium .events .EventBus ;
42
44
import org .openqa .selenium .grid .data .SessionClosedEvent ;
43
45
import org .openqa .selenium .grid .data .Slot ;
44
46
import org .openqa .selenium .grid .data .SlotId ;
47
+ import org .openqa .selenium .grid .jmx .JMXHelper ;
48
+ import org .openqa .selenium .grid .jmx .ManagedAttribute ;
49
+ import org .openqa .selenium .grid .jmx .ManagedService ;
45
50
import org .openqa .selenium .grid .node .ActiveSession ;
46
51
import org .openqa .selenium .grid .node .HealthCheck ;
47
52
import org .openqa .selenium .grid .node .Node ;
48
53
import org .openqa .selenium .grid .node .SessionFactory ;
49
54
import org .openqa .selenium .grid .security .Secret ;
55
+ import org .openqa .selenium .internal .Either ;
50
56
import org .openqa .selenium .internal .Require ;
51
57
import org .openqa .selenium .io .TemporaryFilesystem ;
52
58
import org .openqa .selenium .io .Zip ;
53
59
import org .openqa .selenium .json .Json ;
54
60
import org .openqa .selenium .remote .SessionId ;
55
61
import org .openqa .selenium .remote .http .HttpRequest ;
56
62
import org .openqa .selenium .remote .http .HttpResponse ;
57
- import org .openqa .selenium .grid .jmx .JMXHelper ;
58
- import org .openqa .selenium .grid .jmx .ManagedAttribute ;
59
- import org .openqa .selenium .grid .jmx .ManagedService ;
60
63
import org .openqa .selenium .remote .tracing .AttributeKey ;
61
64
import org .openqa .selenium .remote .tracing .EventAttribute ;
62
65
import org .openqa .selenium .remote .tracing .EventAttributeValue ;
@@ -252,17 +255,27 @@ public boolean isSupporting(Capabilities capabilities) {
252
255
253
256
@ Override
254
257
public Optional <CreateSessionResponse > newSession (CreateSessionRequest sessionRequest ) {
258
+ Either <WebDriverException , CreateSessionResponse > result = createNewSession (sessionRequest );
259
+
260
+ if (result .isRight ()) {
261
+ return Optional .of (result .right ());
262
+ } else {
263
+ return Optional .empty ();
264
+ }
265
+ }
266
+
267
+ public Either <WebDriverException , CreateSessionResponse > createNewSession (CreateSessionRequest sessionRequest ) {
255
268
Require .nonNull ("Session request" , sessionRequest );
256
269
257
270
try (Span span = tracer .getCurrentContext ().createSpan ("node.new_session" )) {
258
271
Map <String , EventAttributeValue > attributeMap = new HashMap <>();
259
272
attributeMap
260
- .put (AttributeKey .LOGGER_CLASS .getKey (), EventAttribute .setValue (getClass ().getName ()));
261
- LOG .fine ("Creating new session using span: " + span );
273
+ .put (AttributeKey .LOGGER_CLASS .getKey (), EventAttribute .setValue (getClass ().getName ()));
262
274
attributeMap .put ("session.request.capabilities" ,
263
- EventAttribute .setValue (sessionRequest .getCapabilities ().toString ()));
275
+ EventAttribute .setValue (sessionRequest .getCapabilities ().toString ()));
264
276
attributeMap .put ("session.request.downstreamdialect" ,
265
- EventAttribute .setValue (sessionRequest .getDownstreamDialects ().toString ()));
277
+ EventAttribute .setValue (sessionRequest .getDownstreamDialects ().toString ()));
278
+
266
279
int currentSessionCount = getCurrentSessionCount ();
267
280
span .setAttribute ("current.session.count" , currentSessionCount );
268
281
attributeMap .put ("current.session.count" , EventAttribute .setValue (currentSessionCount ));
@@ -272,11 +285,12 @@ public Optional<CreateSessionResponse> newSession(CreateSessionRequest sessionRe
272
285
span .setStatus (Status .RESOURCE_EXHAUSTED );
273
286
attributeMap .put ("max.session.count" , EventAttribute .setValue (maxSessionCount ));
274
287
span .addEvent ("Max session count reached" , attributeMap );
275
- return Optional . empty ( );
288
+ return Either . left ( new RetrySessionRequestException ( "Max session count reached." ) );
276
289
}
277
290
if (isDraining ()) {
278
291
span .setStatus (Status .UNAVAILABLE .withDescription ("The node is draining. Cannot accept new sessions." ));
279
- return Optional .empty ();
292
+ return Either .left (
293
+ new RetrySessionRequestException ("The node is draining. Cannot accept new sessions." ));
280
294
}
281
295
282
296
// Identify possible slots to use as quickly as possible to enable concurrent session starting
@@ -296,8 +310,9 @@ public Optional<CreateSessionResponse> newSession(CreateSessionRequest sessionRe
296
310
if (slotToUse == null ) {
297
311
span .setAttribute ("error" , true );
298
312
span .setStatus (Status .NOT_FOUND );
299
- span .addEvent ("No slot matched capabilities " , attributeMap );
300
- return Optional .empty ();
313
+ span .addEvent ("No slot matched the requested capabilities. All slots are busy." , attributeMap );
314
+ return Either .left (
315
+ new RetrySessionRequestException ("No slot matched the requested capabilities. All slots are busy." ));
301
316
}
302
317
303
318
Optional <ActiveSession > possibleSession = slotToUse .apply (sessionRequest );
@@ -306,8 +321,8 @@ public Optional<CreateSessionResponse> newSession(CreateSessionRequest sessionRe
306
321
slotToUse .release ();
307
322
span .setAttribute ("error" , true );
308
323
span .setStatus (Status .NOT_FOUND );
309
- span .addEvent ("No slots available for capabilities " , attributeMap );
310
- return Optional . empty ( );
324
+ span .addEvent ("Unable to create session with the driver " , attributeMap );
325
+ return Either . left ( new SessionNotCreatedException ( "Unable to create session with the driver" ) );
311
326
}
312
327
313
328
ActiveSession session = possibleSession .get ();
@@ -327,9 +342,9 @@ public Optional<CreateSessionResponse> newSession(CreateSessionRequest sessionRe
327
342
// The session we return has to look like it came from the node, since we might be dealing
328
343
// with a webdriver implementation that only accepts connections from localhost
329
344
Session externalSession = createExternalSession (session , externalUri );
330
- return Optional . of (new CreateSessionResponse (
331
- externalSession ,
332
- getEncoder (session .getDownstreamDialect ()).apply (externalSession )));
345
+ return Either . right (new CreateSessionResponse (
346
+ externalSession ,
347
+ getEncoder (session .getDownstreamDialect ()).apply (externalSession )));
333
348
}
334
349
}
335
350
0 commit comments