Skip to content

Commit 2decee4

Browse files
committed
[grid] Reworking how new session requests are processed
We were querying the queue a bit too much, in every single iteration. Now we check if the Grid has availability and then we query the queue for session requests. #10242
1 parent d61295a commit 2decee4

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

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

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;
7272
import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;
7373
import org.openqa.selenium.grid.sessionqueue.config.NewSessionQueueOptions;
74+
import org.openqa.selenium.internal.Debug;
7475
import org.openqa.selenium.internal.Either;
7576
import org.openqa.selenium.internal.Require;
7677
import org.openqa.selenium.remote.SessionId;
@@ -436,7 +437,10 @@ public Either<SessionNotCreatedException, CreateSessionResponse> newSession(Sess
436437
// in this next block of code.
437438
SlotId selectedSlot = reserveSlot(request.getRequestId(), caps);
438439
if (selectedSlot == null) {
439-
LOG.info(String.format("Unable to find slot for request %s. May retry: %s ", request.getRequestId(), caps));
440+
LOG.info(
441+
String.format("Unable to find a free slot for request %s. %s ",
442+
request.getRequestId(),
443+
caps));
440444
retry = true;
441445
continue;
442446
}
@@ -595,35 +599,37 @@ private class NewSessionRunnable implements Runnable {
595599

596600
@Override
597601
public void run() {
598-
List<SessionRequestCapability> queueContents = sessionQueue.getQueueContents();
599-
if (rejectUnsupportedCaps) {
600-
checkMatchingSlot(queueContents);
601-
}
602-
int initialSize = queueContents.size();
603-
boolean retry = initialSize != 0;
604-
605-
while (retry) {
602+
boolean loop = true;
603+
while (loop) {
606604
// We deliberately run this outside of a lock: if we're unsuccessful
607605
// starting the session, we just put the request back on the queue.
608606
// This does mean, however, that under high contention, we might end
609607
// up starving a session request.
610608
Set<Capabilities> stereotypes =
611-
getAvailableNodes().stream()
612-
.filter(NodeStatus::hasCapacity)
613-
.map(
614-
node ->
615-
node.getSlots().stream()
616-
.map(Slot::getStereotype)
617-
.collect(Collectors.toSet()))
618-
.flatMap(Collection::stream)
619-
.collect(Collectors.toSet());
620-
621-
Optional<SessionRequest> maybeRequest = sessionQueue.getNextAvailable(stereotypes);
622-
maybeRequest.ifPresent(req -> sessionCreatorExecutor.execute(() -> handleNewSessionRequest(req)));
623-
624-
int currentSize = sessionQueue.getQueueContents().size();
625-
retry = currentSize != 0 && currentSize != initialSize;
626-
initialSize = currentSize;
609+
getAvailableNodes()
610+
.stream()
611+
.filter(NodeStatus::hasCapacity)
612+
.map(
613+
node ->
614+
node
615+
.getSlots()
616+
.stream()
617+
.map(Slot::getStereotype)
618+
.collect(Collectors.toSet()))
619+
.flatMap(Collection::stream)
620+
.collect(Collectors.toSet());
621+
622+
if (!stereotypes.isEmpty()) {
623+
Optional<SessionRequest> maybeRequest = sessionQueue.getNextAvailable(stereotypes);
624+
maybeRequest.ifPresent(
625+
req -> sessionCreatorExecutor.execute(() -> handleNewSessionRequest(req)));
626+
loop = maybeRequest.isPresent();
627+
} else {
628+
loop = false;
629+
}
630+
}
631+
if (rejectUnsupportedCaps) {
632+
checkMatchingSlot(sessionQueue.getQueueContents());
627633
}
628634
}
629635

@@ -659,7 +665,8 @@ private void handleNewSessionRequest(SessionRequest sessionRequest) {
659665

660666
if (response.isLeft() && response.left() instanceof RetrySessionRequestException) {
661667
try(Span childSpan = span.createSpan("distributor.retry")) {
662-
LOG.info("Retrying");
668+
LOG.log(Debug.getDebugLogLevel(),
669+
String.format("Retrying %s", sessionRequest.getDesiredCapabilities()));
663670
boolean retried = sessionQueue.retryAddToQueue(sessionRequest);
664671

665672
attributeMap.put("request.retry_add", EventAttribute.setValue(retried));

0 commit comments

Comments
 (0)