Skip to content

Commit

Permalink
Merge pull request #153 from twasyl/synchronization-for-optimisation
Browse files Browse the repository at this point in the history
[JENKINS-65821] Introducing some synchronisation mechanisms to prevent some race condition
  • Loading branch information
bitwiseman committed Jun 7, 2021
2 parents 620362f + 4f4aaa5 commit 927249b
Showing 1 changed file with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ public final class StandardGraphLookupView implements GraphLookupView, GraphList

static final String INCOMPLETE = "";

/** Map the blockStartNode to its endNode, to accellerate a range of operations */
/** Map the blockStartNode to its endNode, to accelerate a range of operations */
HashMap<String, String> blockStartToEnd = new HashMap<>();

/** Map a node to its nearest enclosing block */
HashMap<String, String> nearestEnclosingBlock = new HashMap<>();

public void clearCache() {
public synchronized void clearCache() {
blockStartToEnd.clear();
nearestEnclosingBlock.clear();
}

/** Update with a new node added to the flowgraph */
@Override
public void onNewHead(@Nonnull FlowNode newHead) {
public synchronized void onNewHead(@Nonnull FlowNode newHead) {
if (newHead instanceof BlockEndNode) {
blockStartToEnd.put(((BlockEndNode)newHead).getStartNode().getId(), newHead.getId());
String overallEnclosing = nearestEnclosingBlock.get(((BlockEndNode) newHead).getStartNode().getId());
Expand Down Expand Up @@ -87,7 +87,7 @@ public boolean isActive(@Nonnull FlowNode node) {
}

// Do a brute-force scan for the block end matching the start, caching info along the way for future use
BlockEndNode bruteForceScanForEnd(@Nonnull BlockStartNode start) {
synchronized BlockEndNode bruteForceScanForEnd(@Nonnull BlockStartNode start) {
DepthFirstScanner scan = new DepthFirstScanner();
scan.setup(start.getExecution().getCurrentHeads());
for (FlowNode f : scan) {
Expand All @@ -112,11 +112,8 @@ BlockEndNode bruteForceScanForEnd(@Nonnull BlockStartNode start) {
return null;
}




/** Do a brute-force scan for the enclosing blocks **/
BlockStartNode bruteForceScanForEnclosingBlock(@Nonnull final FlowNode node) {
synchronized BlockStartNode bruteForceScanForEnclosingBlock(@Nonnull final FlowNode node) {
FlowNode current = node;

while (!(current instanceof FlowStartNode)) { // Hunt back for enclosing blocks, a potentially expensive operation
Expand Down Expand Up @@ -157,7 +154,7 @@ BlockStartNode bruteForceScanForEnclosingBlock(@Nonnull final FlowNode node) {

@CheckForNull
@Override
public BlockEndNode getEndNode(@Nonnull final BlockStartNode startNode) {
public synchronized BlockEndNode getEndNode(@Nonnull final BlockStartNode startNode) {

String id = blockStartToEnd.get(startNode.getId());
if (id != null) {
Expand All @@ -177,7 +174,7 @@ public BlockEndNode getEndNode(@Nonnull final BlockStartNode startNode) {

@CheckForNull
@Override
public BlockStartNode findEnclosingBlockStart(@Nonnull FlowNode node) {
public synchronized BlockStartNode findEnclosingBlockStart(@Nonnull FlowNode node) {
if (node instanceof FlowStartNode || node instanceof FlowEndNode) {
return null;
}
Expand Down

0 comments on commit 927249b

Please sign in to comment.