diff options
author | Kaushik Lingarkar <kaushikl@codeaurora.org> | 2021-01-25 15:58:51 -0800 |
---|---|---|
committer | Kaushik Lingarkar <kaushikl@codeaurora.org> | 2021-01-25 15:58:51 -0800 |
commit | 2f29ef1c4c7c9893af0638ebc575cbd4b369118c (patch) | |
tree | 6c3a10273fd81e930d1b6e780ce9ae0e4a139221 | |
parent | 141f223240b15d3576f2d45525ed12b6ce7bc9bf (diff) | |
parent | e57889deb8245c0a61a1febd7aa2b6662cc83929 (diff) |
Merge branch 'stable-3.0' into stable-3.1
* stable-3.0:
Use volatile and AtomicIntegers to be thread safe
Change-Id: I0be6a13344043a48f2fc4a0367559f5b5f1fbca9
-rw-r--r-- | src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationState.java | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationState.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationState.java index df8f3f4..b948be0 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationState.java +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationState.java @@ -17,6 +17,7 @@ package com.googlesource.gerrit.plugins.replication; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import org.eclipse.jgit.transport.RemoteRefUpdate; @@ -24,7 +25,7 @@ import org.eclipse.jgit.transport.URIish; public class ReplicationState { - private boolean allScheduled; + private volatile boolean allScheduled; private final PushResultProcessing pushResultProcessing; private final Lock countingLock = new ReentrantLock(); @@ -33,8 +34,8 @@ public class ReplicationState { private static class RefReplicationStatus { private final String project; private final String ref; - private int nodesToReplicateCount; - private int replicatedNodesCount; + private final AtomicInteger nodesToReplicateCount = new AtomicInteger(); + private final AtomicInteger replicatedNodesCount = new AtomicInteger(); RefReplicationStatus(String project, String ref) { this.project = project; @@ -42,13 +43,13 @@ public class ReplicationState { } public boolean allDone() { - return replicatedNodesCount == nodesToReplicateCount; + return replicatedNodesCount.get() == nodesToReplicateCount.get(); } } private final Table<String, String, RefReplicationStatus> statusByProjectRef; - private int totalPushTasksCount; - private int finishedPushTasksCount; + private final AtomicInteger totalPushTasksCount = new AtomicInteger(); + private final AtomicInteger finishedPushTasksCount = new AtomicInteger(); ReplicationState(PushResultProcessing processing) { pushResultProcessing = processing; @@ -58,15 +59,15 @@ public class ReplicationState { public void increasePushTaskCount(String project, String ref) { countingLock.lock(); try { - getRefStatus(project, ref).nodesToReplicateCount++; - totalPushTasksCount++; + getRefStatus(project, ref).nodesToReplicateCount.getAndIncrement(); + totalPushTasksCount.getAndIncrement(); } finally { countingLock.unlock(); } } public boolean hasPushTask() { - return totalPushTasksCount != 0; + return totalPushTasksCount.get() != 0; } public void notifyRefReplicated( @@ -82,14 +83,14 @@ public class ReplicationState { countingLock.lock(); try { RefReplicationStatus refStatus = getRefStatus(project, ref); - refStatus.replicatedNodesCount++; - finishedPushTasksCount++; + refStatus.replicatedNodesCount.getAndIncrement(); + finishedPushTasksCount.getAndIncrement(); if (allScheduled) { if (refStatus.allDone()) { completedRefStatus = statusByProjectRef.remove(project, ref); } - allPushTaksCompleted = finishedPushTasksCount == totalPushTasksCount; + allPushTaksCompleted = finishedPushTasksCount.get() == totalPushTasksCount.get(); } } finally { countingLock.unlock(); @@ -108,7 +109,7 @@ public class ReplicationState { countingLock.lock(); try { allScheduled = true; - if (finishedPushTasksCount < totalPushTasksCount) { + if (finishedPushTasksCount.get() < totalPushTasksCount.get()) { return; } } finally { @@ -120,7 +121,7 @@ public class ReplicationState { private void doAllPushTasksCompleted() { fireRemainingOnRefReplicatedToAllNodes(); - pushResultProcessing.onAllRefsReplicatedToAllNodes(totalPushTasksCount); + pushResultProcessing.onAllRefsReplicatedToAllNodes(totalPushTasksCount.get()); allPushTasksFinished.countDown(); } @@ -135,7 +136,7 @@ public class ReplicationState { private void doRefPushTasksCompleted(RefReplicationStatus refStatus) { pushResultProcessing.onRefReplicatedToAllNodes( - refStatus.project, refStatus.ref, refStatus.nodesToReplicateCount); + refStatus.project, refStatus.ref, refStatus.nodesToReplicateCount.get()); } private RefReplicationStatus getRefStatus(String project, String ref) { |