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; |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 51 | import static org.onosproject.mlb.OsgiPropertyConstants.SCHEDULE_PERIOD; |
| 52 | import static org.onosproject.mlb.OsgiPropertyConstants.SCHEDULE_PERIOD_DEFAULT; |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
| 55 | /** |
| 56 | * An app to perform automatic load balancing in response to events. Load balancing events are triggered by any |
| 57 | * change in mastership and are limited to a frequency of one every 30 seconds, all load balancing is run on an outside |
| 58 | * thread executor that must only have one thread due to issues that can occur is multiple balancing events occur in |
| 59 | * parallel. |
| 60 | */ |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 61 | @Component( |
| 62 | immediate = true, |
| 63 | property = { |
| 64 | SCHEDULE_PERIOD + ":Integer=" + SCHEDULE_PERIOD_DEFAULT |
| 65 | } |
| 66 | ) |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 67 | public class MastershipLoadBalancer { |
| 68 | |
| 69 | private final Logger log = getLogger(getClass()); |
| 70 | |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 71 | /** Period to schedule balancing the mastership to be shared as evenly as by all online instances. */ |
| 72 | private int schedulePeriod = SCHEDULE_PERIOD_DEFAULT; |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 73 | |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 74 | private static final String REBALANCE_MASTERSHIP = "rebalance/mastership"; |
| 75 | |
| 76 | private NodeId localId; |
| 77 | |
| 78 | private AtomicBoolean isLeader = new AtomicBoolean(false); |
| 79 | |
| 80 | private AtomicReference<Future> nextTask = new AtomicReference<>(); |
| 81 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 83 | protected MastershipService mastershipService; |
| 84 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 86 | protected MastershipAdminService mastershipAdminService; |
| 87 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 89 | protected LeadershipService leadershipService; |
| 90 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 92 | protected RegionService regionService; |
| 93 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 95 | protected ClusterService clusterService; |
| 96 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 98 | protected ComponentConfigService cfgService; |
| 99 | |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 100 | private InnerLeadershipListener leadershipListener = new InnerLeadershipListener(); |
| 101 | |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 102 | /* This listener is used to trigger balancing for any mastership event |
| 103 | * which will include switches changing state between active and inactive |
| 104 | * 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] | 105 | */ |
| 106 | private InnerMastershipListener mastershipListener = new InnerMastershipListener(); |
| 107 | |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 108 | /* Used to trigger balancing on region events where there was either a |
| 109 | * change on the master sets of a given region or a change on the devices |
| 110 | * that belong to a region. |
| 111 | */ |
| 112 | private InnerRegionListener regionEventListener = new InnerRegionListener(); |
| 113 | |
| 114 | /* Ensures that all executions do not interfere with one another (single |
| 115 | * thread) and that they are apart from each other by at least what is |
| 116 | * defined as the schedulePeriod. |
| 117 | */ |
| 118 | private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor( |
| 119 | groupedThreads("MastershipLoadBalancer", "%d", log)); |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 120 | |
| 121 | @Activate |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 122 | public void activate(ComponentContext context) { |
| 123 | cfgService.registerProperties(getClass()); |
| 124 | modified(context); |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 125 | mastershipService.addListener(mastershipListener); |
| 126 | localId = clusterService.getLocalNode().id(); |
| 127 | leadershipService.addListener(leadershipListener); |
| 128 | leadershipService.runForLeadership(REBALANCE_MASTERSHIP); |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 129 | regionService.addListener(regionEventListener); |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 130 | log.info("Started"); |
| 131 | } |
| 132 | |
| 133 | @Deactivate |
| 134 | public void deactivate() { |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 135 | cfgService.unregisterProperties(getClass(), false); |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 136 | mastershipService.removeListener(mastershipListener); |
| 137 | leadershipService.withdraw(REBALANCE_MASTERSHIP); |
| 138 | leadershipService.removeListener(leadershipListener); |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 139 | regionService.removeListener(regionEventListener); |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 140 | cancelBalance(); |
| 141 | executorService.shutdown(); |
| 142 | log.info("Stopped"); |
| 143 | } |
| 144 | |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 145 | @Modified |
| 146 | public void modified(ComponentContext context) { |
| 147 | readComponentConfiguration(context); |
| 148 | cancelBalance(); |
| 149 | scheduleBalance(); |
| 150 | log.info("modified"); |
| 151 | } |
| 152 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 153 | private synchronized void processLeaderChange(NodeId newLeader) { |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 154 | boolean currLeader = newLeader.equals(localId); |
| 155 | if (isLeader.getAndSet(currLeader) != currLeader) { |
| 156 | if (currLeader) { |
| 157 | scheduleBalance(); |
| 158 | } else { |
| 159 | cancelBalance(); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 164 | // Sets flag at execution to indicate there is currently a scheduled |
| 165 | // rebalancing. As soon as it starts running, the flag is set back to |
| 166 | // null and another rebalancing can be queued. |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 167 | private void scheduleBalance() { |
| 168 | if (isLeader.get() && nextTask.get() == null) { |
| 169 | |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 170 | Future task = executorService.schedule(new BalanceTask(), |
| 171 | schedulePeriod, TimeUnit.SECONDS); |
| 172 | |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 173 | if (!nextTask.compareAndSet(null, task)) { |
| 174 | task.cancel(false); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 179 | private class BalanceTask implements Runnable { |
| 180 | |
| 181 | @Override |
| 182 | public void run() { |
| 183 | // nextTask is now running, free the spot so that it is possible |
| 184 | // to queue up another upcoming task. |
| 185 | nextTask.set(null); |
| 186 | |
| 187 | mastershipAdminService.balanceRoles(); |
| 188 | log.info("Completed balance roles"); |
| 189 | } |
| 190 | } |
| 191 | |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 192 | private void cancelBalance() { |
| 193 | Future task = nextTask.getAndSet(null); |
| 194 | if (task != null) { |
| 195 | task.cancel(false); |
| 196 | } |
| 197 | } |
| 198 | |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 199 | /** |
| 200 | * Extracts properties from the component configuration context. |
| 201 | * |
| 202 | * @param context the component context |
| 203 | */ |
| 204 | private void readComponentConfiguration(ComponentContext context) { |
| 205 | Dictionary<?, ?> properties = context.getProperties(); |
| 206 | |
| 207 | Integer newSchedulePeriod = Tools.getIntegerProperty(properties, |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 208 | SCHEDULE_PERIOD); |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 209 | if (newSchedulePeriod == null) { |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 210 | schedulePeriod = SCHEDULE_PERIOD_DEFAULT; |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 211 | log.info("Schedule period is not configured, default value is {}", |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 212 | SCHEDULE_PERIOD_DEFAULT); |
sangyun-han | 55e1798 | 2016-08-03 15:45:38 +0900 | [diff] [blame] | 213 | } else { |
| 214 | schedulePeriod = newSchedulePeriod; |
| 215 | log.info("Configured. Schedule period is configured to {}", schedulePeriod); |
| 216 | } |
| 217 | } |
| 218 | |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 219 | private class InnerMastershipListener implements MastershipListener { |
| 220 | |
| 221 | @Override |
| 222 | public void event(MastershipEvent event) { |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 223 | scheduleBalance(); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | private class InnerLeadershipListener implements LeadershipEventListener { |
| 228 | @Override |
| 229 | public boolean isRelevant(LeadershipEvent event) { |
| 230 | return REBALANCE_MASTERSHIP.equals(event.subject().topic()); |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void event(LeadershipEvent event) { |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 235 | processLeaderChange(event.subject().leaderNodeId()); |
Aaron Kruglikov | 2932798 | 2015-10-06 17:15:16 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
Victor Silva | 6b1c6d2 | 2016-09-20 17:00:59 -0300 | [diff] [blame] | 238 | |
| 239 | private class InnerRegionListener implements RegionListener { |
| 240 | @Override |
| 241 | public void event(RegionEvent event) { |
| 242 | switch (event.type()) { |
| 243 | case REGION_MEMBERSHIP_CHANGED: |
| 244 | case REGION_UPDATED: |
| 245 | scheduleBalance(); |
| 246 | break; |
| 247 | default: |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | } |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 252 | } |