Make ObjectiveInstaller thread safe

Objective installer may have been dispatched to a different thread and
numAttempts was possibly accessed from multiple threads

This solution is to create a new instance for the next trial

Change-Id: I5d85f87567241f3e072c38f094eb5c7ba511a6a5
This commit is contained in:
Sho SHIMIZU 2015-07-01 14:39:11 -07:00 committed by Gerrit Code Review
parent eef67ae18f
commit f45d85d88d

View File

@ -58,6 +58,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.concurrent.Executors.newFixedThreadPool; import static java.util.concurrent.Executors.newFixedThreadPool;
import static org.onlab.util.Tools.groupedThreads; import static org.onlab.util.Tools.groupedThreads;
import static org.onosproject.security.AppGuard.checkPermission; import static org.onosproject.security.AppGuard.checkPermission;
@ -150,18 +151,21 @@ public class FlowObjectiveManager implements FlowObjectiveService {
private final DeviceId deviceId; private final DeviceId deviceId;
private final Objective objective; private final Objective objective;
private int numAttempts = 0; private final int numAttempts;
public ObjectiveInstaller(DeviceId deviceId, Objective objective) { public ObjectiveInstaller(DeviceId deviceId, Objective objective) {
this.deviceId = deviceId; this(deviceId, objective, 1);
this.objective = objective; }
public ObjectiveInstaller(DeviceId deviceId, Objective objective, int attemps) {
this.deviceId = checkNotNull(deviceId);
this.objective = checkNotNull(objective);
this.numAttempts = checkNotNull(attemps);
} }
@Override @Override
public void run() { public void run() {
try { try {
numAttempts++;
Pipeliner pipeliner = getDevicePipeliner(deviceId); Pipeliner pipeliner = getDevicePipeliner(deviceId);
if (pipeliner != null) { if (pipeliner != null) {
@ -174,7 +178,7 @@ public class FlowObjectiveManager implements FlowObjectiveService {
} }
} else if (numAttempts < INSTALL_RETRY_ATTEMPTS) { } else if (numAttempts < INSTALL_RETRY_ATTEMPTS) {
Thread.sleep(INSTALL_RETRY_INTERVAL); Thread.sleep(INSTALL_RETRY_INTERVAL);
executorService.submit(this); executorService.submit(new ObjectiveInstaller(deviceId, objective, numAttempts + 1));
} else { } else {
// Otherwise we've tried a few times and failed, report an // Otherwise we've tried a few times and failed, report an
// error back to the user. // error back to the user.