Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [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 | package org.onosproject.store.cluster.impl; |
| 17 | |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
Jordan Halterman | 1f2e804 | 2017-10-18 16:38:03 -0700 | [diff] [blame] | 19 | import org.onosproject.cfg.ComponentConfigService; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 20 | import org.onosproject.cluster.ClusterService; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 21 | import org.onosproject.cluster.Leadership; |
| 22 | import org.onosproject.cluster.LeadershipEvent; |
| 23 | import org.onosproject.cluster.LeadershipStore; |
| 24 | import org.onosproject.cluster.LeadershipStoreDelegate; |
| 25 | import org.onosproject.cluster.NodeId; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 26 | import org.onosproject.core.Version; |
| 27 | import org.onosproject.core.VersionService; |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 28 | import org.onosproject.event.Change; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 29 | import org.onosproject.store.AbstractStore; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 30 | import org.onosproject.store.service.CoordinationService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | import org.onosproject.store.service.DistributedPrimitive.Status; |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 32 | import org.onosproject.store.service.LeaderElector; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 33 | import org.onosproject.upgrade.UpgradeEvent; |
| 34 | import org.onosproject.upgrade.UpgradeEventListener; |
| 35 | import org.onosproject.upgrade.UpgradeService; |
Jordan Halterman | 1f2e804 | 2017-10-18 16:38:03 -0700 | [diff] [blame] | 36 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | import org.osgi.service.component.annotations.Activate; |
| 38 | import org.osgi.service.component.annotations.Component; |
| 39 | import org.osgi.service.component.annotations.Deactivate; |
| 40 | import org.osgi.service.component.annotations.Modified; |
| 41 | import org.osgi.service.component.annotations.Reference; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 44 | import java.util.Dictionary; |
| 45 | import java.util.Map; |
| 46 | import java.util.Objects; |
| 47 | import java.util.concurrent.ExecutorService; |
| 48 | import java.util.concurrent.Executors; |
| 49 | import java.util.function.Consumer; |
| 50 | import java.util.stream.Collectors; |
| 51 | |
Jordan Halterman | 1f2e804 | 2017-10-18 16:38:03 -0700 | [diff] [blame] | 52 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jordan Halterman | 1f2e804 | 2017-10-18 16:38:03 -0700 | [diff] [blame] | 53 | import static org.onlab.util.Tools.get; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 54 | import static org.onlab.util.Tools.groupedThreads; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | import static org.osgi.service.component.annotations.ReferenceCardinality.MANDATORY; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 56 | import static org.slf4j.LoggerFactory.getLogger; |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 57 | import static org.onosproject.store.OsgiPropertyConstants.*; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 58 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 59 | /** |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 60 | * Implementation of {@code LeadershipStore} that makes use of a {@link LeaderElector} |
| 61 | * primitive. |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 62 | */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 63 | @Component( |
| 64 | immediate = true, |
| 65 | service = LeadershipStore.class, |
| 66 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 67 | ELECTION_TIMEOUT_MILLIS + ":Long=" + ELECTION_TIMEOUT_MILLIS_DEFAULT |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 68 | } |
| 69 | ) |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 70 | public class DistributedLeadershipStore |
| 71 | extends AbstractStore<LeadershipEvent, LeadershipStoreDelegate> |
| 72 | implements LeadershipStore { |
| 73 | |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 74 | private static final char VERSION_SEP = '|'; |
| 75 | |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 76 | private final Logger log = getLogger(getClass()); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 77 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 78 | @Reference(cardinality = MANDATORY) |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 79 | protected ClusterService clusterService; |
| 80 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 81 | @Reference(cardinality = MANDATORY) |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 82 | protected CoordinationService storageService; |
| 83 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 84 | @Reference(cardinality = MANDATORY) |
Jordan Halterman | 1f2e804 | 2017-10-18 16:38:03 -0700 | [diff] [blame] | 85 | protected ComponentConfigService configService; |
| 86 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 87 | @Reference(cardinality = MANDATORY) |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 88 | protected VersionService versionService; |
| 89 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 90 | @Reference(cardinality = MANDATORY) |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 91 | protected UpgradeService upgradeService; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 92 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 93 | //@Property(name = "electionTimeoutMillis", longValue = DEFAULT_ELECTION_TIMEOUT_MILLIS, |
| 94 | // label = "the leader election timeout in milliseconds") |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 95 | private long electionTimeoutMillis = ELECTION_TIMEOUT_MILLIS_DEFAULT; |
Jordan Halterman | 1f2e804 | 2017-10-18 16:38:03 -0700 | [diff] [blame] | 96 | |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 97 | private ExecutorService statusChangeHandler; |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 98 | private NodeId localNodeId; |
| 99 | private LeaderElector leaderElector; |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 100 | private final Map<String, Leadership> localLeaderCache = Maps.newConcurrentMap(); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 101 | private final UpgradeEventListener upgradeListener = new InternalUpgradeEventListener(); |
Madan Jampani | 72282af | 2016-02-23 14:23:52 -0800 | [diff] [blame] | 102 | |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 103 | private final Consumer<Change<Leadership>> leadershipChangeListener = |
| 104 | change -> { |
| 105 | Leadership oldValue = change.oldValue(); |
| 106 | Leadership newValue = change.newValue(); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 107 | |
| 108 | // If the topic is not relevant to this version, skip the event. |
| 109 | if (!isLocalTopic(newValue.topic())) { |
| 110 | return; |
| 111 | } |
| 112 | |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 113 | boolean leaderChanged = !Objects.equals(oldValue.leader(), newValue.leader()); |
| 114 | boolean candidatesChanged = !Objects.equals(oldValue.candidates(), newValue.candidates()); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 115 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 116 | LeadershipEvent.Type eventType = null; |
| 117 | if (leaderChanged && candidatesChanged) { |
| 118 | eventType = LeadershipEvent.Type.LEADER_AND_CANDIDATES_CHANGED; |
| 119 | } |
| 120 | if (leaderChanged && !candidatesChanged) { |
| 121 | eventType = LeadershipEvent.Type.LEADER_CHANGED; |
| 122 | } |
| 123 | if (!leaderChanged && candidatesChanged) { |
| 124 | eventType = LeadershipEvent.Type.CANDIDATES_CHANGED; |
| 125 | } |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 126 | notifyDelegate(new LeadershipEvent(eventType, new Leadership( |
| 127 | parseTopic(change.newValue().topic()), |
| 128 | change.newValue().leader(), |
| 129 | change.newValue().candidates()))); |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 130 | // Update local cache of currently held leaderships |
| 131 | if (Objects.equals(newValue.leaderNodeId(), localNodeId)) { |
| 132 | localLeaderCache.put(newValue.topic(), newValue); |
| 133 | } else { |
| 134 | localLeaderCache.remove(newValue.topic()); |
| 135 | } |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 138 | private final Consumer<Status> clientStatusListener = status -> |
| 139 | statusChangeHandler.execute(() -> handleStatusChange(status)); |
| 140 | |
| 141 | private void handleStatusChange(Status status) { |
| 142 | // Notify mastership Service of disconnect and reconnect |
| 143 | if (status == Status.ACTIVE) { |
| 144 | // Service Restored |
| 145 | localLeaderCache.forEach((topic, leadership) -> leaderElector.run(topic, localNodeId)); |
| 146 | leaderElector.getLeaderships().forEach((topic, leadership) -> |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 147 | notifyDelegate(new LeadershipEvent( |
| 148 | LeadershipEvent.Type.SERVICE_RESTORED, |
| 149 | new Leadership( |
| 150 | parseTopic(leadership.topic()), |
| 151 | leadership.leader(), |
| 152 | leadership.candidates())))); |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 153 | } else if (status == Status.SUSPENDED) { |
| 154 | // Service Suspended |
| 155 | localLeaderCache.forEach((topic, leadership) -> |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 156 | notifyDelegate(new LeadershipEvent( |
| 157 | LeadershipEvent.Type.SERVICE_DISRUPTED, |
| 158 | new Leadership( |
| 159 | parseTopic(leadership.topic()), |
| 160 | leadership.leader(), |
| 161 | leadership.candidates())))); |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 162 | } else { |
| 163 | // Should be only inactive state |
| 164 | return; |
| 165 | } |
| 166 | } |
| 167 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 168 | @Activate |
| 169 | public void activate() { |
Jordan Halterman | 1f2e804 | 2017-10-18 16:38:03 -0700 | [diff] [blame] | 170 | configService.registerProperties(getClass()); |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 171 | statusChangeHandler = Executors.newSingleThreadExecutor( |
| 172 | groupedThreads("onos/store/dist/cluster/leadership", "status-change-handler", log)); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 173 | localNodeId = clusterService.getLocalNode().id(); |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 174 | leaderElector = storageService.leaderElectorBuilder() |
Jordan Halterman | 46c5eaa | 2018-01-24 16:46:55 -0800 | [diff] [blame] | 175 | .withName("onos-leadership-elections") |
| 176 | .withElectionTimeout(electionTimeoutMillis) |
| 177 | .withRelaxedReadConsistency() |
| 178 | .build() |
| 179 | .asLeaderElector(); |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 180 | leaderElector.addChangeListener(leadershipChangeListener); |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 181 | leaderElector.addStatusChangeListener(clientStatusListener); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 182 | upgradeService.addListener(upgradeListener); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 183 | log.info("Started"); |
| 184 | } |
| 185 | |
Jordan Halterman | 1f2e804 | 2017-10-18 16:38:03 -0700 | [diff] [blame] | 186 | @Modified |
| 187 | public void modified(ComponentContext context) { |
| 188 | if (context == null) { |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | Dictionary<?, ?> properties = context.getProperties(); |
| 193 | long newElectionTimeoutMillis; |
| 194 | try { |
| 195 | String s = get(properties, "electionTimeoutMillis"); |
| 196 | newElectionTimeoutMillis = isNullOrEmpty(s) ? electionTimeoutMillis : Long.parseLong(s.trim()); |
| 197 | } catch (NumberFormatException | ClassCastException e) { |
| 198 | log.warn("Malformed configuration detected; using defaults", e); |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 199 | newElectionTimeoutMillis = ELECTION_TIMEOUT_MILLIS_DEFAULT; |
Jordan Halterman | 1f2e804 | 2017-10-18 16:38:03 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | if (newElectionTimeoutMillis != electionTimeoutMillis) { |
| 203 | electionTimeoutMillis = newElectionTimeoutMillis; |
| 204 | leaderElector = storageService.leaderElectorBuilder() |
| 205 | .withName("onos-leadership-elections") |
| 206 | .withElectionTimeout(electionTimeoutMillis) |
Jordan Halterman | e74e629 | 2018-05-15 00:52:31 -0700 | [diff] [blame] | 207 | .withRelaxedReadConsistency() |
Jordan Halterman | 1f2e804 | 2017-10-18 16:38:03 -0700 | [diff] [blame] | 208 | .build() |
| 209 | .asLeaderElector(); |
| 210 | } |
| 211 | } |
| 212 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 213 | @Deactivate |
| 214 | public void deactivate() { |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 215 | leaderElector.removeChangeListener(leadershipChangeListener); |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 216 | leaderElector.removeStatusChangeListener(clientStatusListener); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 217 | upgradeService.removeListener(upgradeListener); |
Jon Hall | 7a8bfc6 | 2016-05-26 17:59:04 -0700 | [diff] [blame] | 218 | statusChangeHandler.shutdown(); |
Jordan Halterman | 1f2e804 | 2017-10-18 16:38:03 -0700 | [diff] [blame] | 219 | configService.unregisterProperties(getClass(), false); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 220 | log.info("Stopped"); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public Leadership addRegistration(String topic) { |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 225 | leaderElector.run(getLocalTopic(topic), localNodeId); |
| 226 | return getLeadership(topic); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void removeRegistration(String topic) { |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 231 | leaderElector.withdraw(getLocalTopic(topic)); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void removeRegistration(NodeId nodeId) { |
Madan Jampani | 78be249 | 2016-06-03 23:27:07 -0700 | [diff] [blame] | 236 | leaderElector.evict(nodeId); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public boolean moveLeadership(String topic, NodeId toNodeId) { |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 241 | return leaderElector.anoint(getTopicFor(topic, toNodeId), toNodeId); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public boolean makeTopCandidate(String topic, NodeId nodeId) { |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 246 | return leaderElector.promote(getTopicFor(topic, nodeId), nodeId); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public Leadership getLeadership(String topic) { |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 251 | Leadership leadership = leaderElector.getLeadership(getActiveTopic(topic)); |
| 252 | return leadership != null ? new Leadership( |
| 253 | parseTopic(leadership.topic()), |
| 254 | leadership.leader(), |
| 255 | leadership.candidates()) : null; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public Map<String, Leadership> getLeaderships() { |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 260 | Map<String, Leadership> leaderships = leaderElector.getLeaderships(); |
| 261 | return leaderships.entrySet().stream() |
| 262 | .filter(e -> isActiveTopic(e.getKey())) |
| 263 | .collect(Collectors.toMap(e -> parseTopic(e.getKey()), |
| 264 | e -> new Leadership(parseTopic(e.getKey()), e.getValue().leader(), e.getValue().candidates()))); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Returns a leader elector topic namespaced with the local node's version. |
| 269 | * |
| 270 | * @param topic the base topic |
| 271 | * @return a topic string namespaced with the local node's version |
| 272 | */ |
| 273 | private String getLocalTopic(String topic) { |
| 274 | return topic + VERSION_SEP + versionService.version(); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Returns a leader elector topic namespaced with the current cluster version. |
| 279 | * |
| 280 | * @param topic the base topic |
| 281 | * @return a topic string namespaced with the current cluster version |
| 282 | */ |
| 283 | private String getActiveTopic(String topic) { |
| 284 | return topic + VERSION_SEP + upgradeService.getVersion(); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Returns whether the given topic is a topic for the local version. |
| 289 | * |
| 290 | * @param topic the topic to check |
| 291 | * @return whether the given topic is relevant to the local version |
| 292 | */ |
| 293 | private boolean isLocalTopic(String topic) { |
| 294 | return topic.endsWith(versionService.version().toString()); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Returns whether the given topic is a topic for the current cluster version. |
| 299 | * |
| 300 | * @param topic the topic to check |
| 301 | * @return whether the given topic is relevant to the current cluster version |
| 302 | */ |
| 303 | private boolean isActiveTopic(String topic) { |
| 304 | return topic.endsWith(VERSION_SEP + upgradeService.getVersion().toString()); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Parses a topic string, returning the base topic. |
| 309 | * |
| 310 | * @param topic the topic string to parse |
| 311 | * @return the base topic string |
| 312 | */ |
| 313 | private String parseTopic(String topic) { |
| 314 | return topic.substring(0, topic.lastIndexOf(VERSION_SEP)); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Returns the versioned topic for the given node. |
| 319 | * |
| 320 | * @param topic the topic for the given node |
| 321 | * @param nodeId the node for which to return the namespaced topic |
| 322 | * @return the versioned topic for the given node |
| 323 | */ |
| 324 | private String getTopicFor(String topic, NodeId nodeId) { |
| 325 | Version nodeVersion = clusterService.getVersion(nodeId); |
| 326 | return nodeVersion != null ? topic + VERSION_SEP + nodeVersion : topic + VERSION_SEP + versionService.version(); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Internal upgrade event listener. |
| 331 | */ |
| 332 | private class InternalUpgradeEventListener implements UpgradeEventListener { |
| 333 | @Override |
| 334 | public void event(UpgradeEvent event) { |
| 335 | if (event.type() == UpgradeEvent.Type.UPGRADED || event.type() == UpgradeEvent.Type.ROLLED_BACK) { |
| 336 | // Iterate through all current leaderships for the new version and trigger events. |
| 337 | for (Leadership leadership : getLeaderships().values()) { |
| 338 | notifyDelegate(new LeadershipEvent( |
| 339 | LeadershipEvent.Type.LEADER_AND_CANDIDATES_CHANGED, |
| 340 | leadership)); |
| 341 | } |
| 342 | } |
| 343 | } |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 344 | } |
| 345 | } |