Remove mutating methods in IntentData
Change-Id: I1d9ac694922f4a12b2d94a92b64be2c336c31ae3
Showing
5 changed files
with
42 additions
and
42 deletions
... | @@ -68,6 +68,27 @@ public class IntentData { //FIXME need to make this "immutable" | ... | @@ -68,6 +68,27 @@ public class IntentData { //FIXME need to make this "immutable" |
68 | } | 68 | } |
69 | 69 | ||
70 | /** | 70 | /** |
71 | + * Creates a new intent data object. | ||
72 | + * | ||
73 | + * @param intent intent this metadata references | ||
74 | + * @param state intent state | ||
75 | + * @param version version of the intent for this key | ||
76 | + * @param origin ID of the node where the data was originally created | ||
77 | + */ | ||
78 | + public IntentData(Intent intent, IntentState state, Timestamp version, NodeId origin) { | ||
79 | + checkNotNull(intent); | ||
80 | + checkNotNull(state); | ||
81 | + checkNotNull(version); | ||
82 | + checkNotNull(origin); | ||
83 | + | ||
84 | + this.intent = intent; | ||
85 | + this.state = state; | ||
86 | + this.request = state; | ||
87 | + this.version = version; | ||
88 | + this.origin = origin; | ||
89 | + } | ||
90 | + | ||
91 | + /** | ||
71 | * Copy constructor. | 92 | * Copy constructor. |
72 | * | 93 | * |
73 | * @param intentData intent data to copy | 94 | * @param intentData intent data to copy |
... | @@ -84,6 +105,18 @@ public class IntentData { //FIXME need to make this "immutable" | ... | @@ -84,6 +105,18 @@ public class IntentData { //FIXME need to make this "immutable" |
84 | errorCount = intentData.errorCount; | 105 | errorCount = intentData.errorCount; |
85 | } | 106 | } |
86 | 107 | ||
108 | + /** | ||
109 | + * Create a new instance based on the original instance with new installables. | ||
110 | + * | ||
111 | + * @param original original data | ||
112 | + * @param installables new installable intents to set | ||
113 | + */ | ||
114 | + public IntentData(IntentData original, List<Intent> installables) { | ||
115 | + this(original); | ||
116 | + | ||
117 | + this.installables = ImmutableList.copyOf(checkNotNull(installables)); | ||
118 | + } | ||
119 | + | ||
87 | // kryo constructor | 120 | // kryo constructor |
88 | protected IntentData() { | 121 | protected IntentData() { |
89 | intent = null; | 122 | intent = null; |
... | @@ -131,15 +164,6 @@ public class IntentData { //FIXME need to make this "immutable" | ... | @@ -131,15 +164,6 @@ public class IntentData { //FIXME need to make this "immutable" |
131 | } | 164 | } |
132 | 165 | ||
133 | /** | 166 | /** |
134 | - * Sets the origin, which is the node that created the intent. | ||
135 | - * | ||
136 | - * @param origin origin instance | ||
137 | - */ | ||
138 | - public void setOrigin(NodeId origin) { | ||
139 | - this.origin = origin; | ||
140 | - } | ||
141 | - | ||
142 | - /** | ||
143 | * Returns the origin node that created this intent. | 167 | * Returns the origin node that created this intent. |
144 | * | 168 | * |
145 | * @return origin node ID | 169 | * @return origin node ID |
... | @@ -158,20 +182,6 @@ public class IntentData { //FIXME need to make this "immutable" | ... | @@ -158,20 +182,6 @@ public class IntentData { //FIXME need to make this "immutable" |
158 | } | 182 | } |
159 | 183 | ||
160 | /** | 184 | /** |
161 | - * Sets the version for this intent data. | ||
162 | - * <p> | ||
163 | - * The store should call this method only once when the IntentData is | ||
164 | - * first passed into the pending map. Ideally, an IntentData is timestamped | ||
165 | - * on the same thread that the called used to submit the intents. | ||
166 | - * </p> | ||
167 | - * | ||
168 | - * @param version the version/timestamp for this intent data | ||
169 | - */ | ||
170 | - public void setVersion(Timestamp version) { | ||
171 | - this.version = version; | ||
172 | - } | ||
173 | - | ||
174 | - /** | ||
175 | * Increments the error count for this intent. | 185 | * Increments the error count for this intent. |
176 | */ | 186 | */ |
177 | public void incrementErrorCount() { | 187 | public void incrementErrorCount() { |
... | @@ -198,15 +208,6 @@ public class IntentData { //FIXME need to make this "immutable" | ... | @@ -198,15 +208,6 @@ public class IntentData { //FIXME need to make this "immutable" |
198 | } | 208 | } |
199 | 209 | ||
200 | /** | 210 | /** |
201 | - * Sets the intent installables to the given list of intents. | ||
202 | - * | ||
203 | - * @param installables list of installables for this intent | ||
204 | - */ | ||
205 | - public void setInstallables(List<Intent> installables) { | ||
206 | - this.installables = ImmutableList.copyOf(installables); | ||
207 | - } | ||
208 | - | ||
209 | - /** | ||
210 | * Returns the installables associated with this intent. | 211 | * Returns the installables associated with this intent. |
211 | * | 212 | * |
212 | * @return list of installable intents | 213 | * @return list of installable intents | ... | ... |
... | @@ -156,7 +156,7 @@ public class SimpleIntentStore | ... | @@ -156,7 +156,7 @@ public class SimpleIntentStore |
156 | @Override | 156 | @Override |
157 | public void addPending(IntentData data) { | 157 | public void addPending(IntentData data) { |
158 | if (data.version() == null) { // recompiled intents will already have a version | 158 | if (data.version() == null) { // recompiled intents will already have a version |
159 | - data.setVersion(new SystemClockTimestamp()); | 159 | + data = new IntentData(data.intent(), data.state(), new SystemClockTimestamp()); |
160 | } | 160 | } |
161 | synchronized (this) { | 161 | synchronized (this) { |
162 | IntentData existingData = pending.get(data.key()); | 162 | IntentData existingData = pending.get(data.key()); | ... | ... |
... | @@ -57,14 +57,12 @@ class Compiling implements IntentProcessPhase { | ... | @@ -57,14 +57,12 @@ class Compiling implements IntentProcessPhase { |
57 | List<Intent> compiled = processor.compile(data.intent(), | 57 | List<Intent> compiled = processor.compile(data.intent(), |
58 | //TODO consider passing an optional here in the future | 58 | //TODO consider passing an optional here in the future |
59 | stored.isPresent() ? stored.get().installables() : null); | 59 | stored.isPresent() ? stored.get().installables() : null); |
60 | - data.setInstallables(compiled); | 60 | + return Optional.of(new Installing(processor, new IntentData(data, compiled), stored)); |
61 | - return Optional.of(new Installing(processor, data, stored)); | ||
62 | } catch (IntentException e) { | 61 | } catch (IntentException e) { |
63 | log.debug("Unable to compile intent {} due to: {}", data.intent(), e); | 62 | log.debug("Unable to compile intent {} due to: {}", data.intent(), e); |
64 | if (stored.isPresent() && !stored.get().installables().isEmpty()) { | 63 | if (stored.isPresent() && !stored.get().installables().isEmpty()) { |
65 | // removing orphaned flows and deallocating resources | 64 | // removing orphaned flows and deallocating resources |
66 | - data.setInstallables(stored.get().installables()); | 65 | + return Optional.of(new Withdrawing(processor, new IntentData(data, stored.get().installables()))); |
67 | - return Optional.of(new Withdrawing(processor, data)); | ||
68 | } else { | 66 | } else { |
69 | return Optional.of(new Failed(data)); | 67 | return Optional.of(new Failed(data)); |
70 | } | 68 | } | ... | ... |
... | @@ -64,7 +64,6 @@ final class WithdrawRequest implements IntentProcessPhase { | ... | @@ -64,7 +64,6 @@ final class WithdrawRequest implements IntentProcessPhase { |
64 | } | 64 | } |
65 | } | 65 | } |
66 | 66 | ||
67 | - data.setInstallables(stored.get().installables()); | 67 | + return Optional.of(new Withdrawing(processor, new IntentData(data, stored.get().installables()))); |
68 | - return Optional.of(new Withdrawing(processor, data)); | ||
69 | } | 68 | } |
70 | } | 69 | } | ... | ... |
... | @@ -251,10 +251,12 @@ public class GossipIntentStore | ... | @@ -251,10 +251,12 @@ public class GossipIntentStore |
251 | checkNotNull(data); | 251 | checkNotNull(data); |
252 | 252 | ||
253 | if (data.version() == null) { | 253 | if (data.version() == null) { |
254 | - data.setVersion(new WallClockTimestamp()); | 254 | + pendingMap.put(data.key(), new IntentData(data.intent(), data.state(), |
255 | + new WallClockTimestamp(), clusterService.getLocalNode().id())); | ||
256 | + } else { | ||
257 | + pendingMap.put(data.key(), new IntentData(data.intent(), data.state(), | ||
258 | + data.version(), clusterService.getLocalNode().id())); | ||
255 | } | 259 | } |
256 | - data.setOrigin(clusterService.getLocalNode().id()); | ||
257 | - pendingMap.put(data.key(), new IntentData(data)); | ||
258 | } | 260 | } |
259 | 261 | ||
260 | @Override | 262 | @Override | ... | ... |
-
Please register or login to post a comment