Madan Jampani
Committed by Gerrit Code Review

MastershipService.relinquishRole returns CompletableFuture + Block deviceDiconne…

…cted until role relinquish is complete

Change-Id: I081df48fc05fdca2e452a937a093d5caa16091ed
......@@ -55,8 +55,9 @@ public interface MastershipService {
* for this device, no master selection will occur.
*
* @param deviceId the identifier of the device
* @return future that is completed when relinquish is complete
*/
void relinquishMastership(DeviceId deviceId);
CompletableFuture<Void> relinquishMastership(DeviceId deviceId);
/**
* Returns the current master for a given device.
......
......@@ -38,7 +38,8 @@ public class MastershipServiceAdapter implements MastershipService {
}
@Override
public void relinquishMastership(DeviceId deviceId) {
public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
return null;
}
@Override
......
......@@ -95,12 +95,13 @@ public class MastershipManager
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected MetricsService metricsService;
private NodeId localNodeId;
private Timer requestRoleTimer;
@Activate
public void activate() {
requestRoleTimer = createTimer("Mastership", "requestRole", "responseTime");
localNodeId = clusterService.getLocalNode().id();
eventDispatcher.addSink(MastershipEvent.class, listenerRegistry);
store.setDelegate(delegate);
log.info("Started");
......@@ -136,11 +137,8 @@ public class MastershipManager
return CompletableFuture.completedFuture(null);
}
return eventFuture.whenComplete((event, error) -> {
if (event != null) {
post(event);
}
}).thenApply(v -> null);
return eventFuture.thenAccept(this::post)
.thenApply(v -> null);
}
@Override
......@@ -152,15 +150,11 @@ public class MastershipManager
}
@Override
public void relinquishMastership(DeviceId deviceId) {
public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
checkPermission(Permission.CLUSTER_WRITE);
store.relinquishRole(clusterService.getLocalNode().id(), deviceId)
.whenComplete((event, error) -> {
if (event != null) {
post(event);
}
});
return store.relinquishRole(localNodeId, deviceId)
.thenAccept(this::post)
.thenApply(v -> null);
}
@Override
......
......@@ -16,7 +16,6 @@
package org.onosproject.net.device.impl;
import com.google.common.collect.Lists;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
......@@ -411,8 +410,15 @@ public class DeviceManager
}
});
} finally {
//relinquish master role and ability to be backup.
mastershipService.relinquishMastership(deviceId);
try {
//relinquish master role and ability to be backup.
mastershipService.relinquishMastership(deviceId).get();
} catch (InterruptedException e) {
log.warn("Interrupted while reliquishing role for {}", deviceId);
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
log.error("Exception thrown while relinquishing role for {}", deviceId, e);
}
}
}
......
......@@ -311,6 +311,11 @@ public class DeviceManagerTest {
}
@Override
public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
return CompletableFuture.completedFuture(null);
}
@Override
public MastershipTerm getMastershipTerm(DeviceId deviceId) {
// FIXME: just returning something not null
return MastershipTerm.of(NID_LOCAL, 1);
......
......@@ -37,6 +37,7 @@ import org.onosproject.net.Annotations;
import org.onosproject.net.DefaultAnnotations;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.MastershipRole;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
import org.onosproject.net.SparseAnnotations;
......@@ -62,6 +63,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
......@@ -854,6 +856,10 @@ public class GossipDeviceStoreTest {
public NodeId getMasterFor(DeviceId deviceId) {
return NID1;
}
@Override
public CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId) {
return CompletableFuture.completedFuture(null);
}
}
private static final class TestGossipDeviceStore extends GossipDeviceStore {
......
......@@ -502,8 +502,8 @@ public class LLDPLinkProviderTest {
}
@Override
public void relinquishMastership(DeviceId deviceId) {
public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
return null;
}
@Override
......