Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.mlb; |
| 18 | |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 19 | import org.onlab.util.Tools; |
| 20 | import org.onosproject.cfg.ComponentConfigService; |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 21 | import org.onosproject.cluster.ClusterService; |
| 22 | import org.onosproject.cluster.LeadershipEvent; |
| 23 | import org.onosproject.cluster.LeadershipEventListener; |
| 24 | import org.onosproject.cluster.LeadershipService; |
| 25 | import org.onosproject.cluster.NodeId; |
| 26 | import org.onosproject.mastership.MastershipAdminService; |
| 27 | import org.onosproject.mastership.MastershipEvent; |
| 28 | import org.onosproject.mastership.MastershipListener; |
| 29 | import org.onosproject.mastership.MastershipService; |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 30 | import org.onosproject.net.region.RegionEvent; |
| 31 | import org.onosproject.net.region.RegionListener; |
| 32 | import org.onosproject.net.region.RegionService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 33 | import org.osgi.service.component.ComponentContext; |
| 34 | import org.osgi.service.component.annotations.Activate; |
| 35 | import org.osgi.service.component.annotations.Component; |
| 36 | import org.osgi.service.component.annotations.Deactivate; |
| 37 | import org.osgi.service.component.annotations.Modified; |
| 38 | import org.osgi.service.component.annotations.Reference; |
| 39 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 42 | import java.util.Dictionary; |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 43 | import java.util.concurrent.Executors; |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 44 | import java.util.concurrent.Future; |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 45 | import java.util.concurrent.ScheduledExecutorService; |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 46 | import java.util.concurrent.TimeUnit; |
| 47 | import java.util.concurrent.atomic.AtomicBoolean; |
| 48 | import java.util.concurrent.atomic.AtomicReference; |
| 49 | |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 50 | import static org.onlab.util.Tools.groupedThreads; |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 51 | import static org.slf4j.LoggerFactory.getLogger; |
| 52 | |
| 53 | /** |
| 54 | * An app to perform automatic load balancing in response to events. Load balancing events are triggered by any |
| 55 | * change in mastership and are limited to a frequency of one every 30 seconds, all load balancing is run on an outside |
| 56 | * thread executor that must only have one thread due to issues that can occur is multiple balancing events occur in |
| 57 | * parallel. |
| 58 | */ |
| 59 | @Component(immediate = true) |
| 60 | public class MastershipLoadBalancer { |
| 61 | |
| 62 | private final Logger log = getLogger(getClass()); |
| 63 | |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 64 | private static final int DEFAULT_SCHEDULE_PERIOD = 30; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 65 | //@Property(name = "schedulePeriod", intValue = DEFAULT_SCHEDULE_PERIOD, |
| 66 | // label = "Period to schedule balancing the mastership to be shared as evenly as by all online instances.") |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 67 | private int schedulePeriod = DEFAULT_SCHEDULE_PERIOD; |
| 68 | |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 69 | private static final String REBALANCE_MASTERSHIP = "rebalance/mastership"; |
| 70 | |
| 71 | private NodeId localId; |
| 72 | |
| 73 | private AtomicBoolean isLeader = new AtomicBoolean(false); |
| 74 | |
| 75 | private AtomicReference<Future> nextTask = new AtomicReference<>(); |
| 76 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 78 | protected MastershipService mastershipService; |
| 79 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 81 | protected MastershipAdminService mastershipAdminService; |
| 82 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 84 | protected LeadershipService leadershipService; |
| 85 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 87 | protected RegionService regionService; |
| 88 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 90 | protected ClusterService clusterService; |
| 91 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 93 | protected ComponentConfigService cfgService; |
| 94 | |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 95 | private InnerLeadershipListener leadershipListener = new InnerLeadershipListener(); |
| 96 | |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 97 | /* This listener is used to trigger balancing for any mastership event |
| 98 | * which will include switches changing state between active and inactive |
| 99 | * states as well as the same variety of event occurring with ONOS nodes. |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 100 | */ |
| 101 | private InnerMastershipListener mastershipListener = new InnerMastershipListener(); |
| 102 | |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 103 | /* Used to trigger balancing on region events where there was either a |
| 104 | * change on the master sets of a given region or a change on the devices |
| 105 | * that belong to a region. |
| 106 | */ |
| 107 | private InnerRegionListener regionEventListener = new InnerRegionListener(); |
| 108 | |
| 109 | /* Ensures that all executions do not interfere with one another (single |
| 110 | * thread) and that they are apart from each other by at least what is |
| 111 | * defined as the schedulePeriod. |
| 112 | */ |
| 113 | private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor( |
| 114 | groupedThreads("MastershipLoadBalancer", "%d", log)); |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 115 | |
| 116 | @Activate |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 117 | public void activate(ComponentContext context) { |
| 118 | cfgService.registerProperties(getClass()); |
| 119 | modified(context); |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 120 | mastershipService.addListener(mastershipListener); |
| 121 | localId = clusterService.getLocalNode().id(); |
| 122 | leadershipService.addListener(leadershipListener); |
| 123 | leadershipService.runForLeadership(REBALANCE_MASTERSHIP); |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 124 | regionService.addListener(regionEventListener); |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 125 | log.info("Started"); |
| 126 | } |
| 127 | |
| 128 | @Deactivate |
| 129 | public void deactivate() { |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 130 | cfgService.unregisterProperties(getClass(), false); |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 131 | mastershipService.removeListener(mastershipListener); |
| 132 | leadershipService.withdraw(REBALANCE_MASTERSHIP); |
| 133 | leadershipService.removeListener(leadershipListener); |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 134 | regionService.removeListener(regionEventListener); |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 135 | cancelBalance(); |
| 136 | executorService.shutdown(); |
| 137 | log.info("Stopped"); |
| 138 | } |
| 139 | |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 140 | @Modified |
| 141 | public void modified(ComponentContext context) { |
| 142 | readComponentConfiguration(context); |
| 143 | cancelBalance(); |
| 144 | scheduleBalance(); |
| 145 | log.info("modified"); |
| 146 | } |
| 147 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 148 | private synchronized void processLeaderChange(NodeId newLeader) { |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 149 | boolean currLeader = newLeader.equals(localId); |
| 150 | if (isLeader.getAndSet(currLeader) != currLeader) { |
| 151 | if (currLeader) { |
| 152 | scheduleBalance(); |
| 153 | } else { |
| 154 | cancelBalance(); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 159 | // Sets flag at execution to indicate there is currently a scheduled |
| 160 | // rebalancing. As soon as it starts running, the flag is set back to |
| 161 | // null and another rebalancing can be queued. |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 162 | private void scheduleBalance() { |
| 163 | if (isLeader.get() && nextTask.get() == null) { |
| 164 | |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 165 | Future task = executorService.schedule(new BalanceTask(), |
| 166 | schedulePeriod, TimeUnit.SECONDS); |
| 167 | |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 168 | if (!nextTask.compareAndSet(null, task)) { |
| 169 | task.cancel(false); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 174 | private class BalanceTask implements Runnable { |
| 175 | |
| 176 | @Override |
| 177 | public void run() { |
| 178 | // nextTask is now running, free the spot so that it is possible |
| 179 | // to queue up another upcoming task. |
| 180 | nextTask.set(null); |
| 181 | |
| 182 | mastershipAdminService.balanceRoles(); |
| 183 | log.info("Completed balance roles"); |
| 184 | } |
| 185 | } |
| 186 | |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 187 | private void cancelBalance() { |
| 188 | Future task = nextTask.getAndSet(null); |
| 189 | if (task != null) { |
| 190 | task.cancel(false); |
| 191 | } |
| 192 | } |
| 193 | |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 194 | /** |
| 195 | * Extracts properties from the component configuration context. |
| 196 | * |
| 197 | * @param context the component context |
| 198 | */ |
| 199 | private void readComponentConfiguration(ComponentContext context) { |
| 200 | Dictionary<?, ?> properties = context.getProperties(); |
| 201 | |
| 202 | Integer newSchedulePeriod = Tools.getIntegerProperty(properties, |
| 203 | "schedulePeriod"); |
| 204 | if (newSchedulePeriod == null) { |
| 205 | schedulePeriod = DEFAULT_SCHEDULE_PERIOD; |
| 206 | log.info("Schedule period is not configured, default value is {}", |
| 207 | DEFAULT_SCHEDULE_PERIOD); |
| 208 | } else { |
| 209 | schedulePeriod = newSchedulePeriod; |
| 210 | log.info("Configured. Schedule period is configured to {}", schedulePeriod); |
| 211 | } |
| 212 | } |
| 213 | |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 214 | private class InnerMastershipListener implements MastershipListener { |
| 215 | |
| 216 | @Override |
| 217 | public void event(MastershipEvent event) { |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 218 | scheduleBalance(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private class InnerLeadershipListener implements LeadershipEventListener { |
| 223 | @Override |
| 224 | public boolean isRelevant(LeadershipEvent event) { |
| 225 | return REBALANCE_MASTERSHIP.equals(event.subject().topic()); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void event(LeadershipEvent event) { |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 230 | processLeaderChange(event.subject().leaderNodeId()); |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 233 | |
| 234 | private class InnerRegionListener implements RegionListener { |
| 235 | @Override |
| 236 | public void event(RegionEvent event) { |
| 237 | switch (event.type()) { |
| 238 | case REGION_MEMBERSHIP_CHANGED: |
| 239 | case REGION_UPDATED: |
| 240 | scheduleBalance(); |
| 241 | break; |
| 242 | default: |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | } |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 247 | } |