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 ...@@ -89,14 +89,18 @@ public interface LeadershipService
89 * Returns the current leader board. 89 * Returns the current leader board.
90 * 90 *
91 * @return mapping from topic to leadership info. 91 * @return mapping from topic to leadership info.
92 + * @deprecated 1.6.0 Goldeneye release. Replace usages with {@link #getLeadership(String)}
92 */ 93 */
94 + @Deprecated
93 Map<String, Leadership> getLeaderBoard(); 95 Map<String, Leadership> getLeaderBoard();
94 96
95 /** 97 /**
96 * Returns the candidate nodes for each topic. 98 * Returns the candidate nodes for each topic.
97 * 99 *
98 * @return A mapping from topics to corresponding list of candidates. 100 * @return A mapping from topics to corresponding list of candidates.
101 + * @deprecated 1.6.0 Goldeneye release. Replace usages with {@link #getLeadership(String)}
99 */ 102 */
103 + @Deprecated
100 default Map<String, List<NodeId>> getCandidates() { 104 default Map<String, List<NodeId>> getCandidates() {
101 return ImmutableMap.copyOf(Maps.transformValues(getLeaderBoard(), v -> ImmutableList.copyOf(v.candidates()))); 105 return ImmutableMap.copyOf(Maps.transformValues(getLeaderBoard(), v -> ImmutableList.copyOf(v.candidates())));
102 } 106 }
......
...@@ -43,6 +43,7 @@ import java.util.concurrent.ScheduledExecutorService; ...@@ -43,6 +43,7 @@ import java.util.concurrent.ScheduledExecutorService;
43 import java.util.concurrent.TimeUnit; 43 import java.util.concurrent.TimeUnit;
44 import java.util.concurrent.atomic.AtomicBoolean; 44 import java.util.concurrent.atomic.AtomicBoolean;
45 import java.util.stream.Collectors; 45 import java.util.stream.Collectors;
46 +import java.util.stream.IntStream;
46 47
47 /** 48 /**
48 * Manages the assignment of intent keyspace partitions to instances. 49 * Manages the assignment of intent keyspace partitions to instances.
...@@ -135,7 +136,8 @@ public class IntentPartitionManager implements IntentPartitionService { ...@@ -135,7 +136,8 @@ public class IntentPartitionManager implements IntentPartitionService {
135 136
136 @Override 137 @Override
137 public boolean isMine(Key intentKey) { 138 public boolean isMine(Key intentKey) {
138 - return Objects.equals(leadershipService.getLeader(getPartitionPath(getPartitionForKey(intentKey))), 139 + return Objects.equals(leadershipService.getLeadership(getPartitionPath(getPartitionForKey(intentKey)))
140 + .leaderNodeId(),
139 localNodeId); 141 localNodeId);
140 } 142 }
141 143
...@@ -177,24 +179,20 @@ public class IntentPartitionManager implements IntentPartitionService { ...@@ -177,24 +179,20 @@ public class IntentPartitionManager implements IntentPartitionService {
177 179
178 int myShare = (int) Math.ceil((double) NUM_PARTITIONS / activeNodes); 180 int myShare = (int) Math.ceil((double) NUM_PARTITIONS / activeNodes);
179 181
180 - List<Leadership> myPartitions = leadershipService.getLeaderBoard().values() 182 + List<String> myPartitions = IntStream.range(0, NUM_PARTITIONS)
181 - .stream() 183 + .mapToObj(this::getPartitionPath)
182 - .filter(l -> localNodeId.equals(l.leaderNodeId())) 184 + .map(leadershipService::getLeadership)
183 - .filter(l -> l.topic().startsWith(ELECTION_PREFIX)) 185 + .filter(Objects::nonNull)
186 + .filter(leadership -> localNodeId.equals(leadership.leaderNodeId()))
187 + .map(Leadership::topic)
184 .collect(Collectors.toList()); 188 .collect(Collectors.toList());
185 189
186 int relinquish = myPartitions.size() - myShare; 190 int relinquish = myPartitions.size() - myShare;
187 191
188 - if (relinquish <= 0) {
189 - return;
190 - }
191 -
192 for (int i = 0; i < relinquish; i++) { 192 for (int i = 0; i < relinquish; i++) {
193 - String topic = myPartitions.get(i).topic(); 193 + String topic = myPartitions.get(i);
194 leadershipService.withdraw(topic); 194 leadershipService.withdraw(topic);
195 - 195 + executor.schedule(() -> recontest(topic), BACKOFF_TIME, TimeUnit.SECONDS);
196 - executor.schedule(() -> recontest(topic),
197 - BACKOFF_TIME, TimeUnit.SECONDS);
198 } 196 }
199 } 197 }
200 198
......
...@@ -33,11 +33,11 @@ import org.onosproject.common.event.impl.TestEventDispatcher; ...@@ -33,11 +33,11 @@ import org.onosproject.common.event.impl.TestEventDispatcher;
33 import org.onosproject.net.intent.Key; 33 import org.onosproject.net.intent.Key;
34 34
35 import java.util.Arrays; 35 import java.util.Arrays;
36 -import java.util.HashMap;
37 import java.util.HashSet; 36 import java.util.HashSet;
38 -import java.util.Map; 37 +import java.util.List;
39 import java.util.Objects; 38 import java.util.Objects;
40 import java.util.Set; 39 import java.util.Set;
40 +
41 import static junit.framework.TestCase.assertFalse; 41 import static junit.framework.TestCase.assertFalse;
42 import static org.easymock.EasyMock.anyObject; 42 import static org.easymock.EasyMock.anyObject;
43 import static org.easymock.EasyMock.anyString; 43 import static org.easymock.EasyMock.anyString;
...@@ -100,29 +100,22 @@ public class IntentPartitionManagerTest { ...@@ -100,29 +100,22 @@ public class IntentPartitionManagerTest {
100 * @param numMine number of partitions that should be owned by the local node 100 * @param numMine number of partitions that should be owned by the local node
101 */ 101 */
102 private void setUpLeadershipService(int numMine) { 102 private void setUpLeadershipService(int numMine) {
103 - 103 + List<NodeId> allNodes = Arrays.asList(MY_NODE_ID, OTHER_NODE_ID);
104 - Map<String, Leadership> leaderBoard = new HashMap<>();
105 -
106 for (int i = 0; i < numMine; i++) { 104 for (int i = 0; i < numMine; i++) {
107 - expect(leadershipService.getLeader(ELECTION_PREFIX + i)) 105 + expect(leadershipService.getLeadership(ELECTION_PREFIX + i))
108 - .andReturn(MY_NODE_ID).anyTimes(); 106 + .andReturn(new Leadership(ELECTION_PREFIX + i,
109 - leaderBoard.put(ELECTION_PREFIX + i, 107 + new Leader(MY_NODE_ID, 1, 1000),
110 - new Leadership(ELECTION_PREFIX + i, 108 + allNodes))
111 - new Leader(MY_NODE_ID, 0, 0), 109 + .anyTimes();
112 - Arrays.asList(MY_NODE_ID)));
113 } 110 }
114 111
115 for (int i = numMine; i < IntentPartitionManager.NUM_PARTITIONS; i++) { 112 for (int i = numMine; i < IntentPartitionManager.NUM_PARTITIONS; i++) {
116 - expect(leadershipService.getLeader(ELECTION_PREFIX + i)) 113 + expect(leadershipService.getLeadership(ELECTION_PREFIX + i))
117 - .andReturn(OTHER_NODE_ID).anyTimes(); 114 + .andReturn(new Leadership(ELECTION_PREFIX + i,
118 - 115 + new Leader(OTHER_NODE_ID, 1, 1000),
119 - leaderBoard.put(ELECTION_PREFIX + i, 116 + allNodes))
120 - new Leadership(ELECTION_PREFIX + i, 117 + .anyTimes();
121 - new Leader(OTHER_NODE_ID, 0, 0),
122 - Arrays.asList(OTHER_NODE_ID)));
123 } 118 }
124 -
125 - expect(leadershipService.getLeaderBoard()).andReturn(leaderBoard).anyTimes();
126 } 119 }
127 120
128 /** 121 /**
......