HIGUCHI Yuta
Committed by Gerrit Code Review

Bug ONOS-2030: Wait for mastership

Change-Id: Ie05c44d227f198900a68d7dc8f6a4aed96da9dcc
...@@ -61,6 +61,7 @@ import java.util.Collection; ...@@ -61,6 +61,7 @@ import java.util.Collection;
61 import java.util.List; 61 import java.util.List;
62 import java.util.Objects; 62 import java.util.Objects;
63 import java.util.concurrent.CompletableFuture; 63 import java.util.concurrent.CompletableFuture;
64 +import java.util.concurrent.ExecutionException;
64 import java.util.concurrent.ScheduledExecutorService; 65 import java.util.concurrent.ScheduledExecutorService;
65 import java.util.concurrent.TimeUnit; 66 import java.util.concurrent.TimeUnit;
66 67
...@@ -332,12 +333,23 @@ public class DeviceManager ...@@ -332,12 +333,23 @@ public class DeviceManager
332 333
333 log.info("Device {} connected", deviceId); 334 log.info("Device {} connected", deviceId);
334 // check my Role 335 // check my Role
335 - mastershipService.requestRoleFor(deviceId); 336 + CompletableFuture<MastershipRole> role = mastershipService.requestRoleFor(deviceId);
337 + try {
338 + // Device subsystem must wait for role assignment
339 + // to avoid losing Device information.
340 + // (This node could be the only Node connected to the Device.)
341 + role.get();
342 + } catch (InterruptedException e) {
343 + log.warn("Interrupted while waiting role-assignment for {}", deviceId);
344 + Thread.currentThread().interrupt();
345 + } catch (ExecutionException e) {
346 + log.error("Exception thrown while waiting role-assignment for {}",
347 + deviceId, e);
348 + }
349 +
336 final MastershipTerm term = termService.getMastershipTerm(deviceId); 350 final MastershipTerm term = termService.getMastershipTerm(deviceId);
337 if (term == null || !localNodeId.equals(term.master())) { 351 if (term == null || !localNodeId.equals(term.master())) {
338 log.info("Role of this node is STANDBY for {}", deviceId); 352 log.info("Role of this node is STANDBY for {}", deviceId);
339 - // TODO: Do we need to explicitly tell the Provider that
340 - // this instance is not the MASTER
341 applyRole(deviceId, MastershipRole.STANDBY); 353 applyRole(deviceId, MastershipRole.STANDBY);
342 } else { 354 } else {
343 log.info("Role of this node is MASTER for {}", deviceId); 355 log.info("Role of this node is MASTER for {}", deviceId);
...@@ -349,8 +361,6 @@ public class DeviceManager ...@@ -349,8 +361,6 @@ public class DeviceManager
349 DeviceEvent event = store.createOrUpdateDevice(provider().id(), 361 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
350 deviceId, deviceDescription); 362 deviceId, deviceDescription);
351 363
352 - // If there was a change of any kind, tell the provider
353 - // that this instance is the master.
354 if (event != null) { 364 if (event != null) {
355 log.trace("event: {} {}", event.type(), event); 365 log.trace("event: {} {}", event.type(), event);
356 post(event); 366 post(event);
......