Madan Jampani
Committed by Gerrit Code Review

Mark couple of expensive LeadershipService methods as deprecated

Change-Id: Iff9207a917de5855153f641ddabe1afba0178d53
......@@ -89,14 +89,18 @@ public interface LeadershipService
* Returns the current leader board.
*
* @return mapping from topic to leadership info.
* @deprecated 1.6.0 Goldeneye release. Replace usages with {@link #getLeadership(String)}
*/
@Deprecated
Map<String, Leadership> getLeaderBoard();
/**
* Returns the candidate nodes for each topic.
*
* @return A mapping from topics to corresponding list of candidates.
* @deprecated 1.6.0 Goldeneye release. Replace usages with {@link #getLeadership(String)}
*/
@Deprecated
default Map<String, List<NodeId>> getCandidates() {
return ImmutableMap.copyOf(Maps.transformValues(getLeaderBoard(), v -> ImmutableList.copyOf(v.candidates())));
}
......
......@@ -43,6 +43,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* Manages the assignment of intent keyspace partitions to instances.
......@@ -135,7 +136,8 @@ public class IntentPartitionManager implements IntentPartitionService {
@Override
public boolean isMine(Key intentKey) {
return Objects.equals(leadershipService.getLeader(getPartitionPath(getPartitionForKey(intentKey))),
return Objects.equals(leadershipService.getLeadership(getPartitionPath(getPartitionForKey(intentKey)))
.leaderNodeId(),
localNodeId);
}
......@@ -177,24 +179,20 @@ public class IntentPartitionManager implements IntentPartitionService {
int myShare = (int) Math.ceil((double) NUM_PARTITIONS / activeNodes);
List<Leadership> myPartitions = leadershipService.getLeaderBoard().values()
.stream()
.filter(l -> localNodeId.equals(l.leaderNodeId()))
.filter(l -> l.topic().startsWith(ELECTION_PREFIX))
List<String> myPartitions = IntStream.range(0, NUM_PARTITIONS)
.mapToObj(this::getPartitionPath)
.map(leadershipService::getLeadership)
.filter(Objects::nonNull)
.filter(leadership -> localNodeId.equals(leadership.leaderNodeId()))
.map(Leadership::topic)
.collect(Collectors.toList());
int relinquish = myPartitions.size() - myShare;
if (relinquish <= 0) {
return;
}
for (int i = 0; i < relinquish; i++) {
String topic = myPartitions.get(i).topic();
String topic = myPartitions.get(i);
leadershipService.withdraw(topic);
executor.schedule(() -> recontest(topic),
BACKOFF_TIME, TimeUnit.SECONDS);
executor.schedule(() -> recontest(topic), BACKOFF_TIME, TimeUnit.SECONDS);
}
}
......
......@@ -33,11 +33,11 @@ import org.onosproject.common.event.impl.TestEventDispatcher;
import org.onosproject.net.intent.Key;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import static junit.framework.TestCase.assertFalse;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.anyString;
......@@ -100,29 +100,22 @@ public class IntentPartitionManagerTest {
* @param numMine number of partitions that should be owned by the local node
*/
private void setUpLeadershipService(int numMine) {
Map<String, Leadership> leaderBoard = new HashMap<>();
List<NodeId> allNodes = Arrays.asList(MY_NODE_ID, OTHER_NODE_ID);
for (int i = 0; i < numMine; i++) {
expect(leadershipService.getLeader(ELECTION_PREFIX + i))
.andReturn(MY_NODE_ID).anyTimes();
leaderBoard.put(ELECTION_PREFIX + i,
new Leadership(ELECTION_PREFIX + i,
new Leader(MY_NODE_ID, 0, 0),
Arrays.asList(MY_NODE_ID)));
expect(leadershipService.getLeadership(ELECTION_PREFIX + i))
.andReturn(new Leadership(ELECTION_PREFIX + i,
new Leader(MY_NODE_ID, 1, 1000),
allNodes))
.anyTimes();
}
for (int i = numMine; i < IntentPartitionManager.NUM_PARTITIONS; i++) {
expect(leadershipService.getLeader(ELECTION_PREFIX + i))
.andReturn(OTHER_NODE_ID).anyTimes();
leaderBoard.put(ELECTION_PREFIX + i,
new Leadership(ELECTION_PREFIX + i,
new Leader(OTHER_NODE_ID, 0, 0),
Arrays.asList(OTHER_NODE_ID)));
expect(leadershipService.getLeadership(ELECTION_PREFIX + i))
.andReturn(new Leadership(ELECTION_PREFIX + i,
new Leader(OTHER_NODE_ID, 1, 1000),
allNodes))
.anyTimes();
}
expect(leadershipService.getLeaderBoard()).andReturn(leaderBoard).anyTimes();
}
/**
......