Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -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 | ad3c526 | 2016-01-20 00:50:17 -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.cluster.impl; |
| 17 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 18 | import java.io.File; |
| 19 | import java.io.FileInputStream; |
| 20 | import java.io.IOException; |
| 21 | import java.net.HttpURLConnection; |
| 22 | import java.net.URL; |
| 23 | import java.util.Set; |
| 24 | import java.util.UUID; |
| 25 | import java.util.concurrent.ScheduledExecutorService; |
| 26 | import java.util.concurrent.TimeUnit; |
| 27 | import java.util.concurrent.atomic.AtomicReference; |
| 28 | import java.util.stream.Collectors; |
| 29 | |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 30 | import com.fasterxml.jackson.databind.ObjectMapper; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 31 | import com.google.common.collect.Sets; |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 32 | import com.google.common.io.Files; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 33 | |
| 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.Reference; |
| 38 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 39 | |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 40 | import org.onosproject.cluster.ClusterMetadata; |
| 41 | import org.onosproject.cluster.ClusterMetadataProvider; |
| 42 | import org.onosproject.cluster.ClusterMetadataProviderRegistry; |
| 43 | import org.onosproject.cluster.ClusterMetadataProviderService; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 44 | import org.onosproject.cluster.DefaultControllerNode; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 45 | import org.onosproject.cluster.Node; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 46 | import org.onosproject.cluster.NodeId; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 47 | import org.onosproject.cluster.PartitionId; |
| 48 | import org.onosproject.net.provider.ProviderId; |
| 49 | import org.onosproject.store.service.Versioned; |
| 50 | import org.slf4j.Logger; |
| 51 | |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 52 | import static com.google.common.base.Preconditions.checkState; |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 53 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 54 | import static org.onlab.util.Tools.groupedThreads; |
| 55 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Provider of {@link ClusterMetadata cluster metadata} sourced from a local config file. |
| 59 | */ |
| 60 | @Component(immediate = true) |
| 61 | public class ConfigFileBasedClusterMetadataProvider implements ClusterMetadataProvider { |
| 62 | |
| 63 | private final Logger log = getLogger(getClass()); |
| 64 | |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 65 | private static final String CONFIG_DIR = "../config"; |
| 66 | private static final String CONFIG_FILE_NAME = "cluster.json"; |
| 67 | private static final File CONFIG_FILE = new File(CONFIG_DIR, CONFIG_FILE_NAME); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 68 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 70 | protected ClusterMetadataProviderRegistry providerRegistry; |
| 71 | |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 72 | private static final ProviderId PROVIDER_ID = new ProviderId("file", "none"); |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 73 | private final AtomicReference<Versioned<ClusterMetadata>> cachedMetadata = new AtomicReference<>(); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 74 | private final ScheduledExecutorService configFileChangeDetector = |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 75 | newSingleThreadScheduledExecutor(groupedThreads("onos/cluster/metadata/config-watcher", "", log)); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 76 | |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 77 | private String metadataUrl; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 78 | private ObjectMapper mapper; |
| 79 | private ClusterMetadataProviderService providerService; |
| 80 | |
| 81 | @Activate |
| 82 | public void activate() { |
| 83 | mapper = new ObjectMapper(); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 84 | providerService = providerRegistry.register(this); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 85 | metadataUrl = System.getProperty("onos.cluster.metadata.uri", "file://" + CONFIG_DIR + "/" + CONFIG_FILE); |
| 86 | configFileChangeDetector.scheduleWithFixedDelay(() -> watchUrl(metadataUrl), 100, 500, TimeUnit.MILLISECONDS); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 87 | log.info("Started"); |
| 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | public void deactivate() { |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 92 | configFileChangeDetector.shutdown(); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 93 | providerRegistry.unregister(this); |
| 94 | log.info("Stopped"); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public ProviderId id() { |
| 99 | return PROVIDER_ID; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public Versioned<ClusterMetadata> getClusterMetadata() { |
| 104 | checkState(isAvailable()); |
| 105 | synchronized (this) { |
| 106 | if (cachedMetadata.get() == null) { |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 107 | cachedMetadata.set(blockForMetadata(metadataUrl)); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 108 | } |
| 109 | return cachedMetadata.get(); |
| 110 | } |
| 111 | } |
| 112 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 113 | private ClusterMetadataPrototype toPrototype(ClusterMetadata metadata) { |
| 114 | ClusterMetadataPrototype prototype = new ClusterMetadataPrototype(); |
| 115 | prototype.setName(metadata.getName()); |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 116 | prototype.setController(metadata.getNodes() |
| 117 | .stream() |
| 118 | .map(this::toPrototype) |
| 119 | .collect(Collectors.toSet())); |
| 120 | prototype.setStorage(metadata.getStorageNodes() |
| 121 | .stream() |
| 122 | .map(this::toPrototype) |
| 123 | .collect(Collectors.toSet())); |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 124 | prototype.setClusterSecret(metadata.getClusterSecret()); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 125 | return prototype; |
| 126 | } |
| 127 | |
| 128 | private NodePrototype toPrototype(Node node) { |
| 129 | NodePrototype prototype = new NodePrototype(); |
| 130 | prototype.setId(node.id().id()); |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 131 | prototype.setHost(node.host()); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 132 | prototype.setPort(node.tcpPort()); |
| 133 | return prototype; |
| 134 | } |
| 135 | |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 136 | @Override |
| 137 | public void setClusterMetadata(ClusterMetadata metadata) { |
| 138 | try { |
Ray Milkey | 8320088 | 2017-11-13 19:18:21 -0800 | [diff] [blame] | 139 | File configFile = new File(metadataUrl.replaceFirst("file://", "")); |
| 140 | Files.createParentDirs(configFile); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 141 | ClusterMetadataPrototype metadataPrototype = toPrototype(metadata); |
| 142 | mapper.writeValue(configFile, metadataPrototype); |
Ray Milkey | 8320088 | 2017-11-13 19:18:21 -0800 | [diff] [blame] | 143 | cachedMetadata.set(fetchMetadata(metadataUrl)); |
| 144 | providerService.clusterMetadataChanged(new Versioned<>(metadata, configFile.lastModified())); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 145 | } catch (IOException e) { |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 146 | throw new IllegalArgumentException(e); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void addActivePartitionMember(PartitionId partitionId, NodeId nodeId) { |
| 152 | throw new UnsupportedOperationException(); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void removeActivePartitionMember(PartitionId partitionId, NodeId nodeId) { |
| 157 | throw new UnsupportedOperationException(); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Set<NodeId> getActivePartitionMembers(PartitionId partitionId) { |
| 162 | throw new UnsupportedOperationException(); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public boolean isAvailable() { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 167 | try { |
| 168 | URL url = new URL(metadataUrl); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 169 | if ("file".equals(url.getProtocol())) { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 170 | File file = new File(metadataUrl.replaceFirst("file://", "")); |
| 171 | return file.exists(); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 172 | } else { |
Jordan Halterman | 46c7690 | 2017-08-24 14:55:25 -0700 | [diff] [blame] | 173 | // Return true for HTTP URLs since we allow blocking until HTTP servers come up |
| 174 | return "http".equals(url.getProtocol()); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 175 | } |
| 176 | } catch (Exception e) { |
Jon Hall | db2cf75 | 2016-06-28 16:24:45 -0700 | [diff] [blame] | 177 | log.warn("Exception accessing metadata file at {}:", metadataUrl, e); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 178 | return false; |
| 179 | } |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 182 | private Versioned<ClusterMetadata> blockForMetadata(String metadataUrl) { |
Ray Milkey | 3717e60 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 183 | long iterations = 0; |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 184 | for (;;) { |
Jordan Halterman | 9ada34a | 2017-10-23 16:31:50 -0700 | [diff] [blame] | 185 | try { |
| 186 | Versioned<ClusterMetadata> metadata = fetchMetadata(metadataUrl); |
| 187 | if (metadata != null) { |
| 188 | return metadata; |
| 189 | } |
| 190 | } catch (Exception e) { |
| 191 | log.warn("Exception attempting to access metadata file at {}: {}", metadataUrl, e); |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | try { |
Ray Milkey | 961b19f | 2018-02-05 13:47:55 -0800 | [diff] [blame] | 195 | Thread.sleep((int) Math.pow(2, iterations < 7 ? ++iterations : iterations) * 10L); |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 196 | } catch (InterruptedException e) { |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 197 | Thread.currentThread().interrupt(); |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 198 | throw new IllegalStateException(e); |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 203 | private static NodeId getNodeId(NodePrototype node) { |
| 204 | if (node.getId() != null) { |
| 205 | return NodeId.nodeId(node.getId()); |
| 206 | } else if (node.getHost() != null) { |
| 207 | return NodeId.nodeId(node.getHost()); |
| 208 | } else if (node.getIp() != null) { |
| 209 | return NodeId.nodeId(node.getIp()); |
| 210 | } else { |
| 211 | return NodeId.nodeId(UUID.randomUUID().toString()); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private static String getNodeHost(NodePrototype node) { |
| 216 | if (node.getHost() != null) { |
| 217 | return node.getHost(); |
| 218 | } else if (node.getIp() != null) { |
| 219 | return node.getIp(); |
| 220 | } else { |
| 221 | throw new IllegalArgumentException( |
| 222 | "Malformed cluster configuration: No host specified for node " + node.getId()); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private static int getNodePort(NodePrototype node) { |
| 227 | if (node.getPort() != null) { |
| 228 | return node.getPort(); |
| 229 | } |
| 230 | return DefaultControllerNode.DEFAULT_PORT; |
| 231 | } |
| 232 | |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 233 | private Versioned<ClusterMetadata> fetchMetadata(String metadataUrl) { |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 234 | try { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 235 | URL url = new URL(metadataUrl); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 236 | ClusterMetadataPrototype metadata = null; |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 237 | long version = 0; |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 238 | if ("file".equals(url.getProtocol())) { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 239 | File file = new File(metadataUrl.replaceFirst("file://", "")); |
| 240 | version = file.lastModified(); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 241 | metadata = mapper.readValue(new FileInputStream(file), ClusterMetadataPrototype.class); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 242 | } else if ("http".equals(url.getProtocol())) { |
Jordan Halterman | da26837 | 2017-09-19 11:18:08 -0700 | [diff] [blame] | 243 | try { |
| 244 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 245 | if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { |
| 246 | log.warn("Could not reach metadata URL {}. Retrying...", url); |
| 247 | return null; |
| 248 | } |
| 249 | if (conn.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) { |
| 250 | return null; |
| 251 | } |
| 252 | version = conn.getLastModified(); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 253 | metadata = mapper.readValue(conn.getInputStream(), ClusterMetadataPrototype.class); |
Jordan Halterman | da26837 | 2017-09-19 11:18:08 -0700 | [diff] [blame] | 254 | } catch (IOException e) { |
Jordan Halterman | 46c7690 | 2017-08-24 14:55:25 -0700 | [diff] [blame] | 255 | log.warn("Could not reach metadata URL {}. Retrying...", url); |
| 256 | return null; |
| 257 | } |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 258 | } |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 259 | |
zhiyong ke | 60e4000 | 2017-04-13 13:50:47 +0800 | [diff] [blame] | 260 | if (null == metadata) { |
| 261 | log.warn("Metadata is null in the function fetchMetadata"); |
| 262 | throw new NullPointerException(); |
| 263 | } |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 264 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 265 | return new Versioned<>(new ClusterMetadata( |
| 266 | PROVIDER_ID, |
| 267 | metadata.getName(), |
| 268 | metadata.getNode() != null ? |
| 269 | new DefaultControllerNode( |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 270 | getNodeId(metadata.getNode()), |
| 271 | getNodeHost(metadata.getNode()), |
| 272 | getNodePort(metadata.getNode())) : null, |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 273 | metadata.getController() |
| 274 | .stream() |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 275 | .map(node -> new DefaultControllerNode(getNodeId(node), getNodeHost(node), getNodePort(node))) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 276 | .collect(Collectors.toSet()), |
| 277 | metadata.getStorage() |
| 278 | .stream() |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 279 | .map(node -> new DefaultControllerNode(getNodeId(node), getNodeHost(node), getNodePort(node))) |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 280 | .collect(Collectors.toSet()), |
| 281 | metadata.getClusterSecret()), |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 282 | version); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 283 | } catch (IOException e) { |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 284 | throw new IllegalArgumentException(e); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 285 | } |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 288 | /** |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 289 | * Monitors the metadata url for any updates and notifies providerService accordingly. |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 290 | */ |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 291 | private void watchUrl(String metadataUrl) { |
Ray Milkey | e3708c7 | 2017-11-13 13:10:46 -0800 | [diff] [blame] | 292 | if (!isAvailable()) { |
| 293 | return; |
| 294 | } |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 295 | // TODO: We are merely polling the url. |
| 296 | // This can be easily addressed for files. For http urls we need to move to a push style protocol. |
dvaddire | 49add80 | 2017-08-23 09:45:49 +0530 | [diff] [blame] | 297 | try { |
| 298 | Versioned<ClusterMetadata> latestMetadata = fetchMetadata(metadataUrl); |
| 299 | if (cachedMetadata.get() != null && latestMetadata != null |
| 300 | && cachedMetadata.get().version() < latestMetadata.version()) { |
| 301 | cachedMetadata.set(latestMetadata); |
| 302 | providerService.clusterMetadataChanged(latestMetadata); |
| 303 | } |
| 304 | } catch (Exception e) { |
| 305 | log.error("Unable to parse metadata : ", e); |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 306 | } |
| 307 | } |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 308 | |
| 309 | private static class ClusterMetadataPrototype { |
| 310 | private String name; |
| 311 | private NodePrototype node; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 312 | private Set<NodePrototype> controller = Sets.newHashSet(); |
| 313 | private Set<NodePrototype> storage = Sets.newHashSet(); |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 314 | private String clusterSecret; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 315 | |
| 316 | public String getName() { |
| 317 | return name; |
| 318 | } |
| 319 | |
| 320 | public void setName(String name) { |
| 321 | this.name = name; |
| 322 | } |
| 323 | |
| 324 | public NodePrototype getNode() { |
| 325 | return node; |
| 326 | } |
| 327 | |
| 328 | public void setNode(NodePrototype node) { |
| 329 | this.node = node; |
| 330 | } |
| 331 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 332 | public Set<NodePrototype> getController() { |
| 333 | return controller; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 336 | public void setController(Set<NodePrototype> controller) { |
| 337 | this.controller = controller; |
| 338 | } |
| 339 | |
| 340 | public Set<NodePrototype> getStorage() { |
| 341 | return storage; |
| 342 | } |
| 343 | |
| 344 | public void setStorage(Set<NodePrototype> storage) { |
| 345 | this.storage = storage; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 346 | } |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 347 | |
| 348 | public void setClusterSecret(String clusterSecret) { |
| 349 | this.clusterSecret = clusterSecret; |
| 350 | } |
| 351 | |
| 352 | public String getClusterSecret() { |
| 353 | return clusterSecret; |
| 354 | } |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | private static class NodePrototype { |
| 358 | private String id; |
| 359 | private String ip; |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 360 | private String host; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 361 | private Integer port; |
| 362 | |
| 363 | public String getId() { |
| 364 | return id; |
| 365 | } |
| 366 | |
| 367 | public void setId(String id) { |
| 368 | this.id = id; |
| 369 | } |
| 370 | |
| 371 | public String getIp() { |
| 372 | return ip; |
| 373 | } |
| 374 | |
| 375 | public void setIp(String ip) { |
| 376 | this.ip = ip; |
| 377 | } |
| 378 | |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 379 | public String getHost() { |
| 380 | return host; |
| 381 | } |
| 382 | |
| 383 | public void setHost(String host) { |
| 384 | this.host = host; |
| 385 | } |
| 386 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 387 | public Integer getPort() { |
| 388 | return port; |
| 389 | } |
| 390 | |
| 391 | public void setPort(Integer port) { |
| 392 | this.port = port; |
| 393 | } |
| 394 | } |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 395 | } |