Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cluster.impl; |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 18 | import com.codahale.metrics.Timer; |
| 19 | import com.codahale.metrics.Timer.Context; |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 22 | import com.google.common.util.concurrent.Futures; |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 23 | import org.onlab.metrics.MetricsService; |
Victor Silva | f2b9d03 | 2016-09-19 19:43:20 -0300 | [diff] [blame] | 24 | import org.onosproject.cfg.ComponentConfigService; |
| 25 | import org.onosproject.cfg.ConfigProperty; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterService; |
| 27 | import org.onosproject.cluster.ControllerNode; |
| 28 | import org.onosproject.cluster.NodeId; |
| 29 | import org.onosproject.cluster.RoleInfo; |
| 30 | import org.onosproject.core.MetricsHelper; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 31 | import org.onosproject.event.AbstractListenerManager; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.mastership.MastershipAdminService; |
| 33 | import org.onosproject.mastership.MastershipEvent; |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 34 | import org.onosproject.mastership.MastershipInfo; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.mastership.MastershipListener; |
| 36 | import org.onosproject.mastership.MastershipService; |
| 37 | import org.onosproject.mastership.MastershipStore; |
| 38 | import org.onosproject.mastership.MastershipStoreDelegate; |
| 39 | import org.onosproject.mastership.MastershipTerm; |
| 40 | import org.onosproject.mastership.MastershipTermService; |
| 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.net.MastershipRole; |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 43 | import org.onosproject.net.region.Region; |
| 44 | import org.onosproject.net.region.RegionService; |
Jordan Halterman | 713830d | 2017-10-07 13:40:44 -0700 | [diff] [blame] | 45 | import org.onosproject.upgrade.UpgradeEvent; |
| 46 | import org.onosproject.upgrade.UpgradeEventListener; |
| 47 | import org.onosproject.upgrade.UpgradeService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | import org.osgi.service.component.annotations.Activate; |
| 49 | import org.osgi.service.component.annotations.Component; |
| 50 | import org.osgi.service.component.annotations.Deactivate; |
| 51 | import org.osgi.service.component.annotations.Modified; |
| 52 | import org.osgi.service.component.annotations.Reference; |
| 53 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 56 | import java.util.ArrayList; |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 57 | import java.util.Collection; |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 58 | import java.util.Collections; |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 59 | import java.util.Comparator; |
Thomas Vachuska | 12dfdc3 | 2014-11-29 16:03:12 -0800 | [diff] [blame] | 60 | import java.util.HashMap; |
| 61 | import java.util.HashSet; |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 62 | import java.util.Iterator; |
| 63 | import java.util.List; |
Thomas Vachuska | 12dfdc3 | 2014-11-29 16:03:12 -0800 | [diff] [blame] | 64 | import java.util.Map; |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 65 | import java.util.Set; |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 66 | import java.util.concurrent.CompletableFuture; |
| 67 | |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 68 | import static com.google.common.base.Preconditions.checkNotNull; |
| 69 | import static com.google.common.collect.Lists.newArrayList; |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 70 | import static java.util.concurrent.CompletableFuture.allOf; |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 71 | import static org.onlab.metrics.MetricsUtil.startTimer; |
| 72 | import static org.onlab.metrics.MetricsUtil.stopTimer; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 73 | import static org.onosproject.net.MastershipRole.MASTER; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 74 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 75 | import static org.onosproject.security.AppPermission.Type.CLUSTER_READ; |
| 76 | import static org.onosproject.security.AppPermission.Type.CLUSTER_WRITE; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 77 | import static org.slf4j.LoggerFactory.getLogger; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 78 | import static org.onosproject.net.OsgiPropertyConstants.*; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 79 | |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 80 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 81 | /** |
| 82 | * Component providing the node-device mastership service. |
| 83 | */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 84 | @Component( |
| 85 | immediate = true, |
| 86 | service = { |
| 87 | MastershipService.class, |
| 88 | MastershipAdminService.class, |
| 89 | MastershipTermService.class, |
| 90 | MetricsHelper.class |
| 91 | }, |
| 92 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 93 | USE_REGION_FOR_BALANCE_ROLES + ":Boolean=" + USE_REGION_FOR_BALANCE_ROLES_DEFAULT, |
| 94 | REBALANCE_ROLES_ON_UPGRADE + ":Boolean=" + REBALANCE_ROLES_ON_UPGRADE_DEFAULT |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 95 | } |
| 96 | ) |
Ayaka Koshibe | 3eed2b0 | 2014-09-23 13:28:05 -0700 | [diff] [blame] | 97 | public class MastershipManager |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 98 | extends AbstractListenerManager<MastershipEvent, MastershipListener> |
| 99 | implements MastershipService, MastershipAdminService, MastershipTermService, |
| 100 | MetricsHelper { |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 101 | |
| 102 | private static final String NODE_ID_NULL = "Node ID cannot be null"; |
| 103 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 104 | private static final String ROLE_NULL = "Mastership role cannot be null"; |
| 105 | |
| 106 | private final Logger log = getLogger(getClass()); |
| 107 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 108 | private final MastershipStoreDelegate delegate = new InternalDelegate(); |
Jordan Halterman | 713830d | 2017-10-07 13:40:44 -0700 | [diff] [blame] | 109 | private final UpgradeEventListener upgradeEventListener = new InternalUpgradeEventListener(); |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 110 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 112 | protected MastershipStore store; |
| 113 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | 4a5d171 | 2014-09-23 17:49:39 -0700 | [diff] [blame] | 115 | protected ClusterService clusterService; |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 116 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 118 | protected MetricsService metricsService; |
| 119 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 121 | protected RegionService regionService; |
| 122 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Victor Silva | f2b9d03 | 2016-09-19 19:43:20 -0300 | [diff] [blame] | 124 | protected ComponentConfigService cfgService; |
| 125 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | 713830d | 2017-10-07 13:40:44 -0700 | [diff] [blame] | 127 | protected UpgradeService upgradeService; |
| 128 | |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 129 | private NodeId localNodeId; |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 130 | private Timer requestRoleTimer; |
Victor Silva | f2b9d03 | 2016-09-19 19:43:20 -0300 | [diff] [blame] | 131 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 132 | /** Use Regions for balancing roles. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 133 | protected boolean useRegionForBalanceRoles = USE_REGION_FOR_BALANCE_ROLES_DEFAULT; |
Jordan Halterman | 713830d | 2017-10-07 13:40:44 -0700 | [diff] [blame] | 134 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 135 | /** Automatically rebalance roles following an upgrade. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 136 | protected boolean rebalanceRolesOnUpgrade = REBALANCE_ROLES_ON_UPGRADE_DEFAULT; |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 137 | |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 138 | @Activate |
| 139 | public void activate() { |
Victor Silva | f2b9d03 | 2016-09-19 19:43:20 -0300 | [diff] [blame] | 140 | cfgService.registerProperties(getClass()); |
| 141 | modified(); |
| 142 | |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 143 | requestRoleTimer = createTimer("Mastership", "requestRole", "responseTime"); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 144 | localNodeId = clusterService.getLocalNode().id(); |
Jordan Halterman | 61aeb35 | 2017-10-18 16:22:17 -0700 | [diff] [blame] | 145 | upgradeService.addListener(upgradeEventListener); |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 146 | eventDispatcher.addSink(MastershipEvent.class, listenerRegistry); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 147 | store.setDelegate(delegate); |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 148 | log.info("Started"); |
| 149 | } |
| 150 | |
Victor Silva | f2b9d03 | 2016-09-19 19:43:20 -0300 | [diff] [blame] | 151 | @Modified |
| 152 | public void modified() { |
| 153 | Set<ConfigProperty> configProperties = cfgService.getProperties(getClass().getCanonicalName()); |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 154 | if (configProperties != null) { |
| 155 | for (ConfigProperty property : configProperties) { |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 156 | if (USE_REGION_FOR_BALANCE_ROLES.equals(property.name())) { |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 157 | useRegionForBalanceRoles = property.asBoolean(); |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 158 | } else if (REBALANCE_ROLES_ON_UPGRADE.equals(property.name())) { |
| 159 | rebalanceRolesOnUpgrade = property.asBoolean(); |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 160 | } |
Victor Silva | f2b9d03 | 2016-09-19 19:43:20 -0300 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 165 | @Deactivate |
| 166 | public void deactivate() { |
| 167 | eventDispatcher.removeSink(MastershipEvent.class); |
Jordan Halterman | 61aeb35 | 2017-10-18 16:22:17 -0700 | [diff] [blame] | 168 | upgradeService.removeListener(upgradeEventListener); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 169 | store.unsetDelegate(delegate); |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 170 | log.info("Stopped"); |
Victor Silva | f2b9d03 | 2016-09-19 19:43:20 -0300 | [diff] [blame] | 171 | cfgService.unregisterProperties(getClass(), false); |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 174 | @Override |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 175 | public CompletableFuture<Void> setRole(NodeId nodeId, DeviceId deviceId, MastershipRole role) { |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 176 | checkNotNull(nodeId, NODE_ID_NULL); |
| 177 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 178 | checkNotNull(role, ROLE_NULL); |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 179 | |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 180 | CompletableFuture<MastershipEvent> eventFuture = null; |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 181 | |
| 182 | switch (role) { |
| 183 | case MASTER: |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 184 | eventFuture = store.setMaster(nodeId, deviceId); |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 185 | break; |
| 186 | case STANDBY: |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 187 | eventFuture = store.setStandby(nodeId, deviceId); |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 188 | break; |
| 189 | case NONE: |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 190 | eventFuture = store.relinquishRole(nodeId, deviceId); |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 191 | break; |
| 192 | default: |
| 193 | log.info("Unknown role; ignoring"); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 194 | return CompletableFuture.completedFuture(null); |
Ayaka Koshibe | 971a38a | 2014-09-30 11:56:23 -0700 | [diff] [blame] | 195 | } |
Ayaka Koshibe | d9f693e | 2014-09-29 18:04:54 -0700 | [diff] [blame] | 196 | |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 197 | return eventFuture.thenAccept(this::post) |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 198 | .thenApply(v -> null); |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 202 | public MastershipRole getLocalRole(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 203 | checkPermission(CLUSTER_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 204 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 205 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 206 | return store.getRole(clusterService.getLocalNode().id(), deviceId); |
| 207 | } |
| 208 | |
| 209 | @Override |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 210 | public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 211 | checkPermission(CLUSTER_WRITE); |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 212 | return store.relinquishRole(localNodeId, deviceId) |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 213 | .thenAccept(this::post) |
| 214 | .thenApply(v -> null); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Override |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 218 | public CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 219 | checkPermission(CLUSTER_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 220 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 221 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 222 | final Context timer = startTimer(requestRoleTimer); |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 223 | return store.requestRole(deviceId).whenComplete((result, error) -> stopTimer(timer)); |
| 224 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 228 | public NodeId getMasterFor(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 229 | checkPermission(CLUSTER_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 230 | |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 231 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 232 | return store.getMaster(deviceId); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public Set<DeviceId> getDevicesOf(NodeId nodeId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 237 | checkPermission(CLUSTER_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 238 | |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 239 | checkNotNull(nodeId, NODE_ID_NULL); |
| 240 | return store.getDevices(nodeId); |
| 241 | } |
| 242 | |
Ayaka Koshibe | 45503ce | 2014-10-14 11:26:45 -0700 | [diff] [blame] | 243 | @Override |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 244 | public RoleInfo getNodesFor(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 245 | checkPermission(CLUSTER_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 246 | |
Ayaka Koshibe | 45503ce | 2014-10-14 11:26:45 -0700 | [diff] [blame] | 247 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 248 | return store.getNodes(deviceId); |
| 249 | } |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame] | 250 | |
| 251 | @Override |
Jordan Halterman | 0a2bd45 | 2018-06-13 17:24:58 -0700 | [diff] [blame] | 252 | public MastershipInfo getMastershipFor(DeviceId deviceId) { |
| 253 | checkPermission(CLUSTER_READ); |
| 254 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 255 | return store.getMastership(deviceId); |
| 256 | } |
| 257 | |
| 258 | @Override |
Yuta HIGUCHI | bcac499 | 2014-11-22 19:27:57 -0800 | [diff] [blame] | 259 | public MastershipTerm getMastershipTerm(DeviceId deviceId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 260 | checkPermission(CLUSTER_READ); |
Yuta HIGUCHI | bcac499 | 2014-11-22 19:27:57 -0800 | [diff] [blame] | 261 | return store.getTermFor(deviceId); |
Ayaka Koshibe | b70d34b | 2014-09-25 15:43:01 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 264 | @Override |
Yuta HIGUCHI | a22f69f | 2014-11-24 22:25:17 -0800 | [diff] [blame] | 265 | public MetricsService metricsService() { |
| 266 | return metricsService; |
| 267 | } |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 268 | |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 269 | @Override |
| 270 | public void balanceRoles() { |
| 271 | List<ControllerNode> nodes = newArrayList(clusterService.getNodes()); |
Thomas Vachuska | 12dfdc3 | 2014-11-29 16:03:12 -0800 | [diff] [blame] | 272 | Map<ControllerNode, Set<DeviceId>> controllerDevices = new HashMap<>(); |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 273 | Set<DeviceId> orphanedDevices = Sets.newHashSet(); |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 274 | int deviceCount = 0; |
| 275 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 276 | // Create buckets reflecting current ownership; do this irrespective of |
| 277 | // whether the node is active. |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 278 | for (ControllerNode node : nodes) { |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 279 | Set<DeviceId> devicesOf = new HashSet<>(getDevicesOf(node.id())); |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 280 | if (clusterService.getState(node.id()).isActive()) { |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 281 | log.info("Node {} has {} devices.", node.id(), devicesOf.size()); |
Thomas Vachuska | 12dfdc3 | 2014-11-29 16:03:12 -0800 | [diff] [blame] | 282 | deviceCount += devicesOf.size(); |
| 283 | controllerDevices.put(node, devicesOf); |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 284 | } else if (!devicesOf.isEmpty()) { |
| 285 | log.warn("Inactive node {} has {} orphaned devices.", node.id(), devicesOf.size()); |
| 286 | orphanedDevices.addAll(getDevicesOf(node.id())); |
Thomas Vachuska | 12dfdc3 | 2014-11-29 16:03:12 -0800 | [diff] [blame] | 287 | } |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 290 | if (useRegionForBalanceRoles && balanceRolesUsingRegions(controllerDevices)) { |
| 291 | return; |
| 292 | } |
| 293 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 294 | List<CompletableFuture<Void>> balanceBucketsFutures = Lists.newLinkedList(); |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 295 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 296 | // First re-balance the buckets until they are roughly even. |
| 297 | balanceControllerNodes(controllerDevices, deviceCount, balanceBucketsFutures); |
| 298 | |
| 299 | // Then attempt to distribute any orphaned devices among the buckets. |
| 300 | distributeOrphanedDevices(controllerDevices, orphanedDevices, balanceBucketsFutures); |
| 301 | |
| 302 | CompletableFuture<Void> balanceRolesFuture = |
| 303 | allOf(balanceBucketsFutures.toArray(new CompletableFuture[balanceBucketsFutures.size()])); |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 304 | |
| 305 | Futures.getUnchecked(balanceRolesFuture); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Balances the nodes specified in controllerDevices. |
| 310 | * |
| 311 | * @param controllerDevices controller nodes to devices map |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 312 | * @param deviceCount number of devices mastered by controller nodes |
| 313 | * @param futures list of setRole futures for "moved" devices |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 314 | */ |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 315 | private void balanceControllerNodes(Map<ControllerNode, Set<DeviceId>> controllerDevices, |
| 316 | int deviceCount, |
| 317 | List<CompletableFuture<Void>> futures) { |
Thomas Vachuska | 12dfdc3 | 2014-11-29 16:03:12 -0800 | [diff] [blame] | 318 | // Now re-balance the buckets until they are roughly even. |
| 319 | int rounds = controllerDevices.keySet().size(); |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 320 | for (int i = 0; i < rounds; i++) { |
| 321 | // Iterate over the buckets and find the smallest and the largest. |
Thomas Vachuska | 12dfdc3 | 2014-11-29 16:03:12 -0800 | [diff] [blame] | 322 | ControllerNode smallest = findBucket(true, controllerDevices); |
| 323 | ControllerNode largest = findBucket(false, controllerDevices); |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 324 | futures.add(balanceBuckets(smallest, largest, controllerDevices, deviceCount)); |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 325 | } |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Uses the set of orphaned devices to even out the load among the controllers. |
| 330 | * |
| 331 | * @param controllerDevices controller nodes to devices map |
| 332 | * @param orphanedDevices set of orphaned devices without an active master |
| 333 | * @param futures list of completable future to track the progress of the balancing operation |
| 334 | */ |
| 335 | private void distributeOrphanedDevices(Map<ControllerNode, Set<DeviceId>> controllerDevices, |
| 336 | Set<DeviceId> orphanedDevices, |
| 337 | List<CompletableFuture<Void>> futures) { |
| 338 | // Now re-distribute the orphaned devices into buckets until they are roughly even. |
| 339 | while (!orphanedDevices.isEmpty()) { |
| 340 | // Iterate over the buckets and find the smallest bucket. |
| 341 | ControllerNode smallest = findBucket(true, controllerDevices); |
| 342 | changeMastership(smallest, controllerDevices.get(smallest), |
| 343 | orphanedDevices, 1, futures); |
| 344 | } |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 347 | /** |
| 348 | * Finds node with the minimum/maximum devices from a list of nodes. |
| 349 | * |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 350 | * @param min true: minimum, false: maximum |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 351 | * @param controllerDevices controller nodes to devices map |
| 352 | * @return controller node with minimum/maximum devices |
| 353 | */ |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 354 | |
Thomas Vachuska | 12dfdc3 | 2014-11-29 16:03:12 -0800 | [diff] [blame] | 355 | private ControllerNode findBucket(boolean min, |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 356 | Map<ControllerNode, Set<DeviceId>> controllerDevices) { |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 357 | int xSize = min ? Integer.MAX_VALUE : -1; |
| 358 | ControllerNode xNode = null; |
Thomas Vachuska | 12dfdc3 | 2014-11-29 16:03:12 -0800 | [diff] [blame] | 359 | for (ControllerNode node : controllerDevices.keySet()) { |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 360 | int size = controllerDevices.get(node).size(); |
| 361 | if ((min && size < xSize) || (!min && size > xSize)) { |
| 362 | xSize = size; |
| 363 | xNode = node; |
| 364 | } |
| 365 | } |
| 366 | return xNode; |
| 367 | } |
| 368 | |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 369 | /** |
| 370 | * Balance the node buckets by moving devices from largest to smallest node. |
| 371 | * |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 372 | * @param smallest node that is master of the smallest number of devices |
| 373 | * @param largest node that is master of the largest number of devices |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 374 | * @param controllerDevices controller nodes to devices map |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 375 | * @param deviceCount number of devices mastered by controller nodes |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 376 | * @return list of setRole futures for "moved" devices |
| 377 | */ |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 378 | private CompletableFuture<Void> balanceBuckets(ControllerNode smallest, ControllerNode largest, |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 379 | Map<ControllerNode, Set<DeviceId>> controllerDevices, |
| 380 | int deviceCount) { |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 381 | Collection<DeviceId> minBucket = controllerDevices.get(smallest); |
| 382 | Collection<DeviceId> maxBucket = controllerDevices.get(largest); |
| 383 | int bucketCount = controllerDevices.keySet().size(); |
| 384 | |
| 385 | int delta = (maxBucket.size() - minBucket.size()) / 2; |
| 386 | delta = Math.min(deviceCount / bucketCount, delta); |
| 387 | |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 388 | List<CompletableFuture<Void>> setRoleFutures = Lists.newLinkedList(); |
| 389 | |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 390 | if (delta > 0) { |
| 391 | log.info("Attempting to move {} nodes from {} to {}...", delta, |
| 392 | largest.id(), smallest.id()); |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 393 | changeMastership(smallest, minBucket, maxBucket, delta, setRoleFutures); |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 394 | } |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 395 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 396 | return allOf(setRoleFutures.toArray(new CompletableFuture[setRoleFutures.size()])); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Changes mastership for the specified number of devices in the given source |
| 401 | * bucket to the specified node and ads those devices to the given target |
| 402 | * bucket. Also adds the futures for tracking the role reassignment progress. |
| 403 | * |
| 404 | * @param toNode target controller node |
| 405 | * @param toBucket target bucket |
| 406 | * @param fromBucket source bucket |
| 407 | * @param count number of devices |
| 408 | * @param futures futures for tracking operation progress |
| 409 | */ |
| 410 | private void changeMastership(ControllerNode toNode, Collection<DeviceId> toBucket, |
| 411 | Collection<DeviceId> fromBucket, int count, |
| 412 | List<CompletableFuture<Void>> futures) { |
| 413 | int i = 0; |
| 414 | Iterator<DeviceId> it = fromBucket.iterator(); |
| 415 | while (it.hasNext() && i < count) { |
| 416 | DeviceId deviceId = it.next(); |
| 417 | log.info("Setting {} as the master for {}", toNode.id(), deviceId); |
| 418 | futures.add(setRole(toNode.id(), deviceId, MASTER)); |
| 419 | toBucket.add(deviceId); |
| 420 | it.remove(); |
| 421 | i++; |
| 422 | } |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 425 | /** |
| 426 | * Balances the nodes considering Region information. |
| 427 | * |
| 428 | * @param allControllerDevices controller nodes to devices map |
| 429 | * @return true: nodes balanced; false: nodes not balanced |
| 430 | */ |
| 431 | private boolean balanceRolesUsingRegions(Map<ControllerNode, Set<DeviceId>> allControllerDevices) { |
| 432 | Set<Region> regions = regionService.getRegions(); |
| 433 | if (regions.isEmpty()) { |
| 434 | return false; // no balancing was done using regions. |
| 435 | } |
| 436 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 437 | // Handle nodes belonging to regions |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 438 | Set<ControllerNode> nodesInRegions = Sets.newHashSet(); |
| 439 | for (Region region : regions) { |
| 440 | Map<ControllerNode, Set<DeviceId>> activeRegionControllers = |
| 441 | balanceRolesInRegion(region, allControllerDevices); |
| 442 | nodesInRegions.addAll(activeRegionControllers.keySet()); |
| 443 | } |
| 444 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 445 | // Handle nodes not belonging to any region |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 446 | Set<ControllerNode> nodesNotInRegions = Sets.difference(allControllerDevices.keySet(), nodesInRegions); |
| 447 | if (!nodesNotInRegions.isEmpty()) { |
| 448 | int deviceCount = 0; |
| 449 | Map<ControllerNode, Set<DeviceId>> controllerDevicesNotInRegions = new HashMap<>(); |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 450 | for (ControllerNode controllerNode : nodesNotInRegions) { |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 451 | controllerDevicesNotInRegions.put(controllerNode, allControllerDevices.get(controllerNode)); |
| 452 | deviceCount += allControllerDevices.get(controllerNode).size(); |
| 453 | } |
| 454 | // Now re-balance the buckets until they are roughly even. |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 455 | List<CompletableFuture<Void>> balanceBucketsFutures = Lists.newArrayList(); |
| 456 | balanceControllerNodes(controllerDevicesNotInRegions, deviceCount, balanceBucketsFutures); |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 457 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 458 | CompletableFuture<Void> balanceRolesFuture = allOf( |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 459 | balanceBucketsFutures.toArray(new CompletableFuture[balanceBucketsFutures.size()])); |
| 460 | |
| 461 | Futures.getUnchecked(balanceRolesFuture); |
| 462 | } |
| 463 | return true; // balancing was done using regions. |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Balances the nodes in specified region. |
| 468 | * |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 469 | * @param region region in which nodes are to be balanced |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 470 | * @param allControllerDevices controller nodes to devices map |
| 471 | * @return controller nodes that were balanced |
| 472 | */ |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 473 | private Map<ControllerNode, Set<DeviceId>> |
| 474 | balanceRolesInRegion(Region region, |
| 475 | Map<ControllerNode, Set<DeviceId>> allControllerDevices) { |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 476 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 477 | // Retrieve all devices associated with specified region |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 478 | Set<DeviceId> devicesInRegion = regionService.getRegionDevices(region.id()); |
| 479 | log.info("Region {} has {} devices.", region.id(), devicesInRegion.size()); |
| 480 | if (devicesInRegion.isEmpty()) { |
| 481 | return new HashMap<>(); // no devices in this region, so nothing to balance. |
| 482 | } |
| 483 | |
| 484 | List<Set<NodeId>> mastersList = region.masters(); |
| 485 | log.info("Region {} has {} sets of masters.", region.id(), mastersList.size()); |
| 486 | if (mastersList.isEmpty()) { |
| 487 | // TODO handle devices that belong to a region, which has no masters defined |
| 488 | return new HashMap<>(); // for now just leave devices alone |
| 489 | } |
| 490 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 491 | // Get the region's preferred set of masters |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 492 | Set<DeviceId> devicesInMasters = Sets.newHashSet(); |
| 493 | Map<ControllerNode, Set<DeviceId>> regionalControllerDevices = |
| 494 | getRegionsPreferredMasters(region, devicesInMasters, allControllerDevices); |
| 495 | |
| 496 | // Now re-balance the buckets until they are roughly even. |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 497 | List<CompletableFuture<Void>> balanceBucketsFutures = Lists.newArrayList(); |
| 498 | balanceControllerNodes(regionalControllerDevices, devicesInMasters.size(), balanceBucketsFutures); |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 499 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 500 | // Handle devices that are not currently mastered by the master node set |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 501 | Set<DeviceId> devicesNotMasteredWithControllers = Sets.difference(devicesInRegion, devicesInMasters); |
| 502 | if (!devicesNotMasteredWithControllers.isEmpty()) { |
| 503 | // active controllers in master node set are already balanced, just |
| 504 | // assign device mastership in sequence |
| 505 | List<ControllerNode> sorted = new ArrayList<>(regionalControllerDevices.keySet()); |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 506 | Collections.sort(sorted, Comparator.comparingInt(o -> (regionalControllerDevices.get(o)).size())); |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 507 | int deviceIndex = 0; |
| 508 | for (DeviceId deviceId : devicesNotMasteredWithControllers) { |
| 509 | ControllerNode cnode = sorted.get(deviceIndex % sorted.size()); |
| 510 | balanceBucketsFutures.add(setRole(cnode.id(), deviceId, MASTER)); |
| 511 | regionalControllerDevices.get(cnode).add(deviceId); |
| 512 | deviceIndex++; |
| 513 | } |
| 514 | } |
| 515 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 516 | CompletableFuture<Void> balanceRolesFuture = |
| 517 | allOf(balanceBucketsFutures.toArray(new CompletableFuture[balanceBucketsFutures.size()])); |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 518 | |
| 519 | Futures.getUnchecked(balanceRolesFuture); |
| 520 | |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 521 | // Update the map before returning |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 522 | regionalControllerDevices.forEach((controllerNode, deviceIds) -> { |
| 523 | regionalControllerDevices.put(controllerNode, new HashSet<>(getDevicesOf(controllerNode.id()))); |
| 524 | }); |
| 525 | |
| 526 | return regionalControllerDevices; |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Get region's preferred set of master nodes - the first master node set that has at |
| 531 | * least one active node. |
| 532 | * |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 533 | * @param region region for which preferred set of master nodes is requested |
| 534 | * @param devicesInMasters device set to track devices in preferred set of master nodes |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 535 | * @param allControllerDevices controller nodes to devices map |
| 536 | * @return region's preferred master nodes (and devices that use them as masters) |
| 537 | */ |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 538 | private Map<ControllerNode, Set<DeviceId>> |
| 539 | getRegionsPreferredMasters(Region region, |
| 540 | Set<DeviceId> devicesInMasters, |
| 541 | Map<ControllerNode, Set<DeviceId>> allControllerDevices) { |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 542 | Map<ControllerNode, Set<DeviceId>> regionalControllerDevices = new HashMap<>(); |
| 543 | int listIndex = 0; |
Thomas Vachuska | f2e09cb | 2017-11-06 15:17:06 -0800 | [diff] [blame] | 544 | for (Set<NodeId> masterSet : region.masters()) { |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 545 | log.info("Region {} masters set {} has {} nodes.", |
| 546 | region.id(), listIndex, masterSet.size()); |
| 547 | if (masterSet.isEmpty()) { // nothing on this level |
| 548 | listIndex++; |
| 549 | continue; |
| 550 | } |
| 551 | // Create buckets reflecting current ownership. |
| 552 | for (NodeId nodeId : masterSet) { |
Michele Santuari | 6ebb36e | 2016-03-28 10:12:04 -0700 | [diff] [blame] | 553 | if (clusterService.getState(nodeId).isActive()) { |
Claudine Chiu | dce0815 | 2016-03-09 18:19:28 +0000 | [diff] [blame] | 554 | ControllerNode controllerNode = clusterService.getNode(nodeId); |
| 555 | Set<DeviceId> devicesOf = new HashSet<>(allControllerDevices.get(controllerNode)); |
| 556 | regionalControllerDevices.put(controllerNode, devicesOf); |
| 557 | devicesInMasters.addAll(devicesOf); |
| 558 | log.info("Active Node {} has {} devices.", nodeId, devicesOf.size()); |
| 559 | } |
| 560 | } |
| 561 | if (!regionalControllerDevices.isEmpty()) { |
| 562 | break; // now have a set of >0 active controllers |
| 563 | } |
| 564 | listIndex++; // keep on looking |
| 565 | } |
| 566 | return regionalControllerDevices; |
| 567 | } |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 568 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 569 | public class InternalDelegate implements MastershipStoreDelegate { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 570 | @Override |
| 571 | public void notify(MastershipEvent event) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 572 | post(event); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 573 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Jordan Halterman | 713830d | 2017-10-07 13:40:44 -0700 | [diff] [blame] | 576 | private class InternalUpgradeEventListener implements UpgradeEventListener { |
| 577 | @Override |
| 578 | public void event(UpgradeEvent event) { |
| 579 | if (rebalanceRolesOnUpgrade && |
| 580 | (event.type() == UpgradeEvent.Type.COMMITTED || event.type() == UpgradeEvent.Type.RESET)) { |
| 581 | balanceRoles(); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
Ayaka Koshibe | 1660969 | 2014-09-23 12:46:15 -0700 | [diff] [blame] | 586 | } |