Skip to content

Commit

Permalink
[grid] Add lock while finding a node in GridModel. Fix a log typo. (#…
Browse files Browse the repository at this point in the history
…8980)

Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
  • Loading branch information
pujagani and diemol authored Dec 18, 2020
1 parent 2cc9ea2 commit e78881d
Showing 1 changed file with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,41 +305,47 @@ private void reserve(NodeStatus status, Slot slot) {
public void setSession(SlotId slotId, Session session) {
Require.nonNull("Slot ID", slotId);

AvailabilityAndNode node = findNode(slotId.getOwningNodeId());
if (node == null) {
LOG.warning("Grid model and reality have diverged. Unable to find node " + slotId.getOwningNodeId());
return;
}
Lock writeLock = lock.writeLock();
writeLock.lock();
try {
AvailabilityAndNode node = findNode(slotId.getOwningNodeId());
if (node == null) {
LOG.warning("Grid model and reality have diverged. Unable to find node " + slotId.getOwningNodeId());
return;
}

Optional<Slot> maybeSlot = node.status.getSlots().stream()
.filter(slot -> slotId.equals(slot.getId()))
.findFirst();
Optional<Slot> maybeSlot = node.status.getSlots().stream()
.filter(slot -> slotId.equals(slot.getId()))
.findFirst();

if (!maybeSlot.isPresent()) {
LOG.warning("Grid model and reality have diverged. Unable to find slot " + slotId);
return;
}
if (!maybeSlot.isPresent()) {
LOG.warning("Grid model and reality have diverged. Unable to find slot " + slotId);
return;
}

Slot slot = maybeSlot.get();
Optional<Session> maybeSession = slot.getSession();
if (!maybeSession.isPresent()) {
LOG.warning("Grid model and reality have diverged. Slot is not reserved. " + slotId);
return;
}
Slot slot = maybeSlot.get();
Optional<Session> maybeSession = slot.getSession();
if (!maybeSession.isPresent()) {
LOG.warning("Grid model and reality have diverged. Slot is not reserved. " + slotId);
return;
}

Session current = maybeSession.get();
if (!RESERVED.equals(current.getId())) {
LOG.warning("Gid model and reality have diverged. Slot has session and is not reserved. " + slotId);
return;
}
Session current = maybeSession.get();
if (!RESERVED.equals(current.getId())) {
LOG.warning("Grid model and reality have diverged. Slot has session and is not reserved. " + slotId);
return;
}

Slot updated = new Slot(
slot.getId(),
slot.getStereotype(),
session == null ? slot.getLastStarted() : session.getStartTime(),
Optional.ofNullable(session));
Slot updated = new Slot(
slot.getId(),
slot.getStereotype(),
session == null ? slot.getLastStarted() : session.getStartTime(),
Optional.ofNullable(session));

amend(node.availability, node.status, updated);
amend(node.availability, node.status, updated);
} finally {
writeLock.unlock();
}
}

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

0 comments on commit e78881d

Please sign in to comment.