Committed by
Gerrit Code Review
Serializing batch execution on per-instance basis for intents
Change-Id: Idda3f4a65e78567302d91ba0070e78d435eea8fd
Showing
2 changed files
with
29 additions
and
6 deletions
... | @@ -155,6 +155,9 @@ public class IntentManager | ... | @@ -155,6 +155,9 @@ public class IntentManager |
155 | 155 | ||
156 | @Override | 156 | @Override |
157 | public void execute(IntentOperations operations) { | 157 | public void execute(IntentOperations operations) { |
158 | + if (operations.operations().isEmpty()) { | ||
159 | + return; | ||
160 | + } | ||
158 | batchService.addIntentOperations(operations); | 161 | batchService.addIntentOperations(operations); |
159 | } | 162 | } |
160 | 163 | ... | ... |
... | @@ -15,7 +15,7 @@ | ... | @@ -15,7 +15,7 @@ |
15 | */ | 15 | */ |
16 | package org.onlab.onos.store.intent.impl; | 16 | package org.onlab.onos.store.intent.impl; |
17 | 17 | ||
18 | -import com.google.common.collect.ImmutableSet; | 18 | +import com.google.common.collect.Sets; |
19 | import org.apache.felix.scr.annotations.Activate; | 19 | import org.apache.felix.scr.annotations.Activate; |
20 | import org.apache.felix.scr.annotations.Component; | 20 | import org.apache.felix.scr.annotations.Component; |
21 | import org.apache.felix.scr.annotations.Deactivate; | 21 | import org.apache.felix.scr.annotations.Deactivate; |
... | @@ -25,7 +25,9 @@ import org.onlab.onos.net.intent.IntentBatchService; | ... | @@ -25,7 +25,9 @@ import org.onlab.onos.net.intent.IntentBatchService; |
25 | import org.onlab.onos.net.intent.IntentOperations; | 25 | import org.onlab.onos.net.intent.IntentOperations; |
26 | import org.slf4j.Logger; | 26 | import org.slf4j.Logger; |
27 | 27 | ||
28 | -import java.util.HashSet; | 28 | +import java.util.Collection; |
29 | +import java.util.LinkedList; | ||
30 | +import java.util.Queue; | ||
29 | import java.util.Set; | 31 | import java.util.Set; |
30 | 32 | ||
31 | import static com.google.common.base.Preconditions.checkNotNull; | 33 | import static com.google.common.base.Preconditions.checkNotNull; |
... | @@ -37,7 +39,8 @@ import static org.slf4j.LoggerFactory.getLogger; | ... | @@ -37,7 +39,8 @@ import static org.slf4j.LoggerFactory.getLogger; |
37 | public class DistributedIntentBatchQueue implements IntentBatchService { | 39 | public class DistributedIntentBatchQueue implements IntentBatchService { |
38 | 40 | ||
39 | private final Logger log = getLogger(getClass()); | 41 | private final Logger log = getLogger(getClass()); |
40 | - private final Set<IntentOperations> pendingBatches = new HashSet<>(); | 42 | + private final Queue<IntentOperations> pendingBatches = new LinkedList<>(); |
43 | + private final Set<IntentOperations> currentBatches = Sets.newHashSet(); | ||
41 | private IntentBatchDelegate delegate; | 44 | private IntentBatchDelegate delegate; |
42 | 45 | ||
43 | @Activate | 46 | @Activate |
... | @@ -53,18 +56,35 @@ public class DistributedIntentBatchQueue implements IntentBatchService { | ... | @@ -53,18 +56,35 @@ public class DistributedIntentBatchQueue implements IntentBatchService { |
53 | @Override | 56 | @Override |
54 | public void addIntentOperations(IntentOperations operations) { | 57 | public void addIntentOperations(IntentOperations operations) { |
55 | checkState(delegate != null, "No delegate set"); | 58 | checkState(delegate != null, "No delegate set"); |
59 | + synchronized (this) { | ||
56 | pendingBatches.add(operations); | 60 | pendingBatches.add(operations); |
57 | - delegate.execute(operations); | 61 | + if (currentBatches.isEmpty()) { |
62 | + IntentOperations work = pendingBatches.poll(); | ||
63 | + currentBatches.add(work); | ||
64 | + delegate.execute(work); | ||
65 | + } | ||
66 | + } | ||
58 | } | 67 | } |
59 | 68 | ||
60 | @Override | 69 | @Override |
61 | public void removeIntentOperations(IntentOperations operations) { | 70 | public void removeIntentOperations(IntentOperations operations) { |
62 | - pendingBatches.remove(operations); | 71 | + // we allow at most one outstanding batch at a time |
72 | + synchronized (this) { | ||
73 | + checkState(currentBatches.remove(operations), "Operations not found in current ops."); | ||
74 | + checkState(currentBatches.isEmpty(), "More than one outstanding batch."); | ||
75 | + IntentOperations work = pendingBatches.poll(); | ||
76 | + if (work != null) { | ||
77 | + currentBatches.add(work); | ||
78 | + delegate.execute(work); | ||
79 | + } | ||
80 | + } | ||
63 | } | 81 | } |
64 | 82 | ||
65 | @Override | 83 | @Override |
66 | public Set<IntentOperations> getIntentOperations() { | 84 | public Set<IntentOperations> getIntentOperations() { |
67 | - return ImmutableSet.copyOf(pendingBatches); | 85 | + Set<IntentOperations> set = Sets.newHashSet(currentBatches); |
86 | + set.addAll((Collection) pendingBatches); | ||
87 | + return set; | ||
68 | } | 88 | } |
69 | 89 | ||
70 | @Override | 90 | @Override | ... | ... |
-
Please register or login to post a comment