Skip to content

Commit 48b5659

Browse files
committed
[grid] Removing SlotMatcher from Slot, so it can be configured in an easier way
1 parent fcfb21b commit 48b5659

6 files changed

Lines changed: 27 additions & 17 deletions

File tree

java/src/org/openqa/selenium/grid/data/NodeStatus.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public static NodeStatus fromJson(JsonInput input) {
122122
nodeId, externalUri, maxSessions, slots, availability, heartbeatPeriod, version, osInfo);
123123
}
124124

125-
public boolean hasCapability(Capabilities caps) {
126-
return slots.stream().anyMatch(slot -> slot.isSupporting(caps));
125+
public boolean hasCapability(Capabilities caps, SlotMatcher slotMatcher) {
126+
return slots.stream().anyMatch(slot -> slot.isSupporting(caps, slotMatcher));
127127
}
128128

129129
public boolean hasCapacity() {
@@ -132,9 +132,10 @@ public boolean hasCapacity() {
132132

133133
// Check if the Node's max session limit is not exceeded and has a free slot that supports the
134134
// capability.
135-
public boolean hasCapacity(Capabilities caps) {
135+
public boolean hasCapacity(Capabilities caps, SlotMatcher slotMatcher) {
136136
return slots.stream().filter(slot -> slot.getSession() != null).count() < maxSessionCount
137-
&& slots.stream().anyMatch(slot -> slot.getSession() == null && slot.isSupporting(caps));
137+
&& slots.stream()
138+
.anyMatch(slot -> slot.getSession() == null && slot.isSupporting(caps, slotMatcher));
138139
}
139140

140141
public int getMaxSessionCount() {

java/src/org/openqa/selenium/grid/data/Slot.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@ public class Slot implements Serializable {
3535
private final Capabilities stereotype;
3636
private final Session session;
3737
private final Instant lastStarted;
38-
private final SlotMatcher slotMatcher;
3938

4039
public Slot(SlotId id, Capabilities stereotype, Instant lastStarted, Session session) {
4140
this.id = Require.nonNull("Slot ID", id);
4241
this.stereotype = ImmutableCapabilities.copyOf(Require.nonNull("Stereotype", stereotype));
4342
this.lastStarted = Require.nonNull("Last started", lastStarted);
4443
this.session = session;
45-
this.slotMatcher = new DefaultSlotMatcher();
4644
}
4745

4846
private static Slot fromJson(JsonInput input) {
@@ -106,7 +104,7 @@ public Session getSession() {
106104
return session;
107105
}
108106

109-
public boolean isSupporting(Capabilities caps) {
107+
public boolean isSupporting(Capabilities caps, SlotMatcher slotMatcher) {
110108
return slotMatcher.matches(getStereotype(), caps);
111109
}
112110

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import org.openqa.selenium.grid.data.Availability;
6868
import org.openqa.selenium.grid.data.CreateSessionRequest;
6969
import org.openqa.selenium.grid.data.CreateSessionResponse;
70+
import org.openqa.selenium.grid.data.DefaultSlotMatcher;
7071
import org.openqa.selenium.grid.data.DistributorStatus;
7172
import org.openqa.selenium.grid.data.NodeAddedEvent;
7273
import org.openqa.selenium.grid.data.NodeDrainComplete;
@@ -661,7 +662,8 @@ private SlotId reserveSlot(RequestId requestId, Capabilities caps) {
661662
Lock writeLock = lock.writeLock();
662663
writeLock.lock();
663664
try {
664-
Set<SlotId> slotIds = slotSelector.selectSlot(caps, getAvailableNodes());
665+
Set<SlotId> slotIds =
666+
slotSelector.selectSlot(caps, getAvailableNodes(), new DefaultSlotMatcher());
665667
if (slotIds.isEmpty()) {
666668
LOG.log(
667669
getDebugLogLevel(),
@@ -682,7 +684,8 @@ private SlotId reserveSlot(RequestId requestId, Capabilities caps) {
682684
}
683685

684686
private boolean isNotSupported(Capabilities caps) {
685-
return getAvailableNodes().stream().noneMatch(node -> node.hasCapability(caps));
687+
return getAvailableNodes().stream()
688+
.noneMatch(node -> node.hasCapability(caps, new DefaultSlotMatcher()));
686689
}
687690

688691
private boolean reserve(SlotId id) {

java/src/org/openqa/selenium/grid/distributor/selector/DefaultSlotSelector.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.openqa.selenium.grid.data.NodeStatus;
2828
import org.openqa.selenium.grid.data.Slot;
2929
import org.openqa.selenium.grid.data.SlotId;
30+
import org.openqa.selenium.grid.data.SlotMatcher;
3031

3132
public class DefaultSlotSelector implements SlotSelector {
3233

@@ -35,7 +36,8 @@ public static SlotSelector create(Config config) {
3536
}
3637

3738
@Override
38-
public Set<SlotId> selectSlot(Capabilities capabilities, Set<NodeStatus> nodes) {
39+
public Set<SlotId> selectSlot(
40+
Capabilities capabilities, Set<NodeStatus> nodes, SlotMatcher slotMatcher) {
3941
// First, filter the Nodes that support the required capabilities. Then, the filtered Nodes
4042
// get ordered in ascendant order by the number of browsers they support.
4143
// With this, Nodes with diverse configurations (supporting many browsers, e.g. Chrome,
@@ -44,7 +46,7 @@ public Set<SlotId> selectSlot(Capabilities capabilities, Set<NodeStatus> nodes)
4446
// Nodes).
4547
// After that, Nodes are ordered by their load, last session creation, and their id.
4648
return nodes.stream()
47-
.filter(node -> node.hasCapacity(capabilities))
49+
.filter(node -> node.hasCapacity(capabilities, slotMatcher))
4850
.sorted(
4951
Comparator.comparingLong(this::getNumberOfSupportedBrowsers)
5052
// Now sort by node which has the lowest load (natural ordering)
@@ -57,7 +59,7 @@ public Set<SlotId> selectSlot(Capabilities capabilities, Set<NodeStatus> nodes)
5759
node ->
5860
node.getSlots().stream()
5961
.filter(slot -> slot.getSession() == null)
60-
.filter(slot -> slot.isSupporting(capabilities))
62+
.filter(slot -> slot.isSupporting(capabilities, slotMatcher))
6163
.map(Slot::getId))
6264
.collect(toImmutableSet());
6365
}

java/src/org/openqa/selenium/grid/distributor/selector/SlotSelector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
import org.openqa.selenium.Capabilities;
2222
import org.openqa.selenium.grid.data.NodeStatus;
2323
import org.openqa.selenium.grid.data.SlotId;
24+
import org.openqa.selenium.grid.data.SlotMatcher;
2425

2526
/**
2627
* Used to determine which {@link org.openqa.selenium.grid.node.Node} to send a particular New
2728
* Session request to.
2829
*/
2930
@FunctionalInterface
3031
public interface SlotSelector {
31-
Set<SlotId> selectSlot(Capabilities capabilities, Set<NodeStatus> nodes);
32+
Set<SlotId> selectSlot(Capabilities capabilities, Set<NodeStatus> nodes, SlotMatcher slotMatcher);
3233
}

java/test/org/openqa/selenium/grid/distributor/selector/DefaultSlotSelectorTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.openqa.selenium.ImmutableCapabilities;
4343
import org.openqa.selenium.events.EventBus;
4444
import org.openqa.selenium.events.local.GuavaEventBus;
45+
import org.openqa.selenium.grid.data.DefaultSlotMatcher;
4546
import org.openqa.selenium.grid.data.NodeId;
4647
import org.openqa.selenium.grid.data.NodeStatus;
4748
import org.openqa.selenium.grid.data.Session;
@@ -99,7 +100,7 @@ void nodesAreOrderedNodesByNumberOfSupportedBrowsers() {
99100
nodes.add(twoBrowsers);
100101
nodes.add(oneBrowser);
101102

102-
Set<SlotId> slots = selector.selectSlot(caps, nodes);
103+
Set<SlotId> slots = selector.selectSlot(caps, nodes, new DefaultSlotMatcher());
103104

104105
ImmutableSet<NodeId> nodeIds =
105106
slots.stream().map(SlotId::getOwningNodeId).distinct().collect(toImmutableSet());
@@ -123,7 +124,9 @@ void theMostLightlyLoadedNodeIsSelectedFirst() {
123124
NodeStatus heavy = createNode(Collections.singletonList(caps), 10, 6);
124125
NodeStatus massive = createNode(Collections.singletonList(caps), 10, 8);
125126

126-
Set<SlotId> ids = selector.selectSlot(caps, ImmutableSet.of(heavy, medium, lightest, massive));
127+
Set<SlotId> ids =
128+
selector.selectSlot(
129+
caps, ImmutableSet.of(heavy, medium, lightest, massive), new DefaultSlotMatcher());
127130
SlotId expected = ids.iterator().next();
128131

129132
assertThat(lightest.getSlots().stream()).anyMatch(slot -> expected.equals(slot.getId()));
@@ -138,7 +141,8 @@ void theNodeWhichHasExceededMaxSessionsIsNotSelected() {
138141
NodeStatus maximumLoad = createNode(ImmutableList.of(chrome), 12, 12);
139142

140143
Set<SlotId> ids =
141-
selector.selectSlot(chrome, ImmutableSet.of(maximumLoad, mediumLoad, lightLoad));
144+
selector.selectSlot(
145+
chrome, ImmutableSet.of(maximumLoad, mediumLoad, lightLoad), new DefaultSlotMatcher());
142146
SlotId expected = ids.iterator().next();
143147

144148
// The slot should belong to the Node with light load
@@ -172,7 +176,8 @@ void nodesAreOrderedByNumberOfSupportedBrowsersAndLoad() {
172176
lightLoadAndThreeBrowsers,
173177
mediumLoadAndTwoBrowsers,
174178
mediumLoadAndOtherTwoBrowsers,
175-
highLoadAndOneBrowser));
179+
highLoadAndOneBrowser),
180+
new DefaultSlotMatcher());
176181

177182
// The slot should belong to the Node with high load because it only supports Chrome, leaving
178183
// the other Nodes with more availability for other browsers

0 commit comments

Comments
 (0)