Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[grid] Add lock while finding a node in GridModel setSession() method. Fix a log typo. #8980

Merged
merged 2 commits into from
Dec 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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