Sho SHIMIZU

Remove mutating methods in IntentData

Change-Id: I1d9ac694922f4a12b2d94a92b64be2c336c31ae3
......@@ -68,6 +68,27 @@ public class IntentData { //FIXME need to make this "immutable"
}
/**
* Creates a new intent data object.
*
* @param intent intent this metadata references
* @param state intent state
* @param version version of the intent for this key
* @param origin ID of the node where the data was originally created
*/
public IntentData(Intent intent, IntentState state, Timestamp version, NodeId origin) {
checkNotNull(intent);
checkNotNull(state);
checkNotNull(version);
checkNotNull(origin);
this.intent = intent;
this.state = state;
this.request = state;
this.version = version;
this.origin = origin;
}
/**
* Copy constructor.
*
* @param intentData intent data to copy
......@@ -84,6 +105,18 @@ public class IntentData { //FIXME need to make this "immutable"
errorCount = intentData.errorCount;
}
/**
* Create a new instance based on the original instance with new installables.
*
* @param original original data
* @param installables new installable intents to set
*/
public IntentData(IntentData original, List<Intent> installables) {
this(original);
this.installables = ImmutableList.copyOf(checkNotNull(installables));
}
// kryo constructor
protected IntentData() {
intent = null;
......@@ -131,15 +164,6 @@ public class IntentData { //FIXME need to make this "immutable"
}
/**
* Sets the origin, which is the node that created the intent.
*
* @param origin origin instance
*/
public void setOrigin(NodeId origin) {
this.origin = origin;
}
/**
* Returns the origin node that created this intent.
*
* @return origin node ID
......@@ -158,20 +182,6 @@ public class IntentData { //FIXME need to make this "immutable"
}
/**
* Sets the version for this intent data.
* <p>
* The store should call this method only once when the IntentData is
* first passed into the pending map. Ideally, an IntentData is timestamped
* on the same thread that the called used to submit the intents.
* </p>
*
* @param version the version/timestamp for this intent data
*/
public void setVersion(Timestamp version) {
this.version = version;
}
/**
* Increments the error count for this intent.
*/
public void incrementErrorCount() {
......@@ -198,15 +208,6 @@ public class IntentData { //FIXME need to make this "immutable"
}
/**
* Sets the intent installables to the given list of intents.
*
* @param installables list of installables for this intent
*/
public void setInstallables(List<Intent> installables) {
this.installables = ImmutableList.copyOf(installables);
}
/**
* Returns the installables associated with this intent.
*
* @return list of installable intents
......
......@@ -156,7 +156,7 @@ public class SimpleIntentStore
@Override
public void addPending(IntentData data) {
if (data.version() == null) { // recompiled intents will already have a version
data.setVersion(new SystemClockTimestamp());
data = new IntentData(data.intent(), data.state(), new SystemClockTimestamp());
}
synchronized (this) {
IntentData existingData = pending.get(data.key());
......
......@@ -57,14 +57,12 @@ class Compiling implements IntentProcessPhase {
List<Intent> compiled = processor.compile(data.intent(),
//TODO consider passing an optional here in the future
stored.isPresent() ? stored.get().installables() : null);
data.setInstallables(compiled);
return Optional.of(new Installing(processor, data, stored));
return Optional.of(new Installing(processor, new IntentData(data, compiled), stored));
} catch (IntentException e) {
log.debug("Unable to compile intent {} due to: {}", data.intent(), e);
if (stored.isPresent() && !stored.get().installables().isEmpty()) {
// removing orphaned flows and deallocating resources
data.setInstallables(stored.get().installables());
return Optional.of(new Withdrawing(processor, data));
return Optional.of(new Withdrawing(processor, new IntentData(data, stored.get().installables())));
} else {
return Optional.of(new Failed(data));
}
......
......@@ -64,7 +64,6 @@ final class WithdrawRequest implements IntentProcessPhase {
}
}
data.setInstallables(stored.get().installables());
return Optional.of(new Withdrawing(processor, data));
return Optional.of(new Withdrawing(processor, new IntentData(data, stored.get().installables())));
}
}
......
......@@ -251,10 +251,12 @@ public class GossipIntentStore
checkNotNull(data);
if (data.version() == null) {
data.setVersion(new WallClockTimestamp());
pendingMap.put(data.key(), new IntentData(data.intent(), data.state(),
new WallClockTimestamp(), clusterService.getLocalNode().id()));
} else {
pendingMap.put(data.key(), new IntentData(data.intent(), data.state(),
data.version(), clusterService.getLocalNode().id()));
}
data.setOrigin(clusterService.getLocalNode().id());
pendingMap.put(data.key(), new IntentData(data));
}
@Override
......