Skip to content

Commit e78881d

Browse files
pujaganidiemol
andauthored
[grid] Add lock while finding a node in GridModel. Fix a log typo. (#8980)
Co-authored-by: Diego Molina <[email protected]>
1 parent 2cc9ea2 commit e78881d

File tree

1 file changed

+35
-29
lines changed
  • java/server/src/org/openqa/selenium/grid/distributor/local

1 file changed

+35
-29
lines changed

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

+35-29
Original file line numberDiff line numberDiff line change
@@ -305,41 +305,47 @@ private void reserve(NodeStatus status, Slot slot) {
305305
public void setSession(SlotId slotId, Session session) {
306306
Require.nonNull("Slot ID", slotId);
307307

308-
AvailabilityAndNode node = findNode(slotId.getOwningNodeId());
309-
if (node == null) {
310-
LOG.warning("Grid model and reality have diverged. Unable to find node " + slotId.getOwningNodeId());
311-
return;
312-
}
308+
Lock writeLock = lock.writeLock();
309+
writeLock.lock();
310+
try {
311+
AvailabilityAndNode node = findNode(slotId.getOwningNodeId());
312+
if (node == null) {
313+
LOG.warning("Grid model and reality have diverged. Unable to find node " + slotId.getOwningNodeId());
314+
return;
315+
}
313316

314-
Optional<Slot> maybeSlot = node.status.getSlots().stream()
315-
.filter(slot -> slotId.equals(slot.getId()))
316-
.findFirst();
317+
Optional<Slot> maybeSlot = node.status.getSlots().stream()
318+
.filter(slot -> slotId.equals(slot.getId()))
319+
.findFirst();
317320

318-
if (!maybeSlot.isPresent()) {
319-
LOG.warning("Grid model and reality have diverged. Unable to find slot " + slotId);
320-
return;
321-
}
321+
if (!maybeSlot.isPresent()) {
322+
LOG.warning("Grid model and reality have diverged. Unable to find slot " + slotId);
323+
return;
324+
}
322325

323-
Slot slot = maybeSlot.get();
324-
Optional<Session> maybeSession = slot.getSession();
325-
if (!maybeSession.isPresent()) {
326-
LOG.warning("Grid model and reality have diverged. Slot is not reserved. " + slotId);
327-
return;
328-
}
326+
Slot slot = maybeSlot.get();
327+
Optional<Session> maybeSession = slot.getSession();
328+
if (!maybeSession.isPresent()) {
329+
LOG.warning("Grid model and reality have diverged. Slot is not reserved. " + slotId);
330+
return;
331+
}
329332

330-
Session current = maybeSession.get();
331-
if (!RESERVED.equals(current.getId())) {
332-
LOG.warning("Gid model and reality have diverged. Slot has session and is not reserved. " + slotId);
333-
return;
334-
}
333+
Session current = maybeSession.get();
334+
if (!RESERVED.equals(current.getId())) {
335+
LOG.warning("Grid model and reality have diverged. Slot has session and is not reserved. " + slotId);
336+
return;
337+
}
335338

336-
Slot updated = new Slot(
337-
slot.getId(),
338-
slot.getStereotype(),
339-
session == null ? slot.getLastStarted() : session.getStartTime(),
340-
Optional.ofNullable(session));
339+
Slot updated = new Slot(
340+
slot.getId(),
341+
slot.getStereotype(),
342+
session == null ? slot.getLastStarted() : session.getStartTime(),
343+
Optional.ofNullable(session));
341344

342-
amend(node.availability, node.status, updated);
345+
amend(node.availability, node.status, updated);
346+
} finally {
347+
writeLock.unlock();
348+
}
343349
}
344350

345351
private void amend(Availability availability, NodeStatus status, Slot slot) {

0 commit comments

Comments
 (0)