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())); |
Jordan Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 120 | prototype.setStorageDnsService(metadata.getStorageDnsService()); |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 121 | prototype.setStorage(metadata.getStorageNodes() |
| 122 | .stream() |
| 123 | .map(this::toPrototype) |
| 124 | .collect(Collectors.toSet())); |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 125 | prototype.setClusterSecret(metadata.getClusterSecret()); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 126 | return prototype; |
| 127 | } |
| 128 | |
| 129 | private NodePrototype toPrototype(Node node) { |
| 130 | NodePrototype prototype = new NodePrototype(); |
| 131 | prototype.setId(node.id().id()); |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 132 | prototype.setHost(node.host()); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 133 | prototype.setPort(node.tcpPort()); |
| 134 | return prototype; |
| 135 | } |
| 136 | |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 137 | @Override |
| 138 | public void setClusterMetadata(ClusterMetadata metadata) { |
| 139 | try { |
Ray Milkey | 8320088 | 2017-11-13 19:18:21 -0800 | [diff] [blame] | 140 | File configFile = new File(metadataUrl.replaceFirst("file://", "")); |
| 141 | Files.createParentDirs(configFile); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 142 | ClusterMetadataPrototype metadataPrototype = toPrototype(metadata); |
| 143 | mapper.writeValue(configFile, metadataPrototype); |
Ray Milkey | 8320088 | 2017-11-13 19:18:21 -0800 | [diff] [blame] | 144 | cachedMetadata.set(fetchMetadata(metadataUrl)); |
| 145 | providerService.clusterMetadataChanged(new Versioned<>(metadata, configFile.lastModified())); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 146 | } catch (IOException e) { |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 147 | throw new IllegalArgumentException(e); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void addActivePartitionMember(PartitionId partitionId, NodeId nodeId) { |
| 153 | throw new UnsupportedOperationException(); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void removeActivePartitionMember(PartitionId partitionId, NodeId nodeId) { |
| 158 | throw new UnsupportedOperationException(); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Set<NodeId> getActivePartitionMembers(PartitionId partitionId) { |
| 163 | throw new UnsupportedOperationException(); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public boolean isAvailable() { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 168 | try { |
| 169 | URL url = new URL(metadataUrl); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 170 | if ("file".equals(url.getProtocol())) { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 171 | File file = new File(metadataUrl.replaceFirst("file://", "")); |
| 172 | return file.exists(); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 173 | } else { |
Jordan Halterman | 46c7690 | 2017-08-24 14:55:25 -0700 | [diff] [blame] | 174 | // Return true for HTTP URLs since we allow blocking until HTTP servers come up |
| 175 | return "http".equals(url.getProtocol()); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 176 | } |
| 177 | } catch (Exception e) { |
Jon Hall | db2cf75 | 2016-06-28 16:24:45 -0700 | [diff] [blame] | 178 | log.warn("Exception accessing metadata file at {}:", metadataUrl, e); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 179 | return false; |
| 180 | } |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 183 | private Versioned<ClusterMetadata> blockForMetadata(String metadataUrl) { |
Ray Milkey | 3717e60 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 184 | long iterations = 0; |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 185 | for (;;) { |
Jordan Halterman | 9ada34a | 2017-10-23 16:31:50 -0700 | [diff] [blame] | 186 | try { |
| 187 | Versioned<ClusterMetadata> metadata = fetchMetadata(metadataUrl); |
| 188 | if (metadata != null) { |
| 189 | return metadata; |
| 190 | } |
| 191 | } catch (Exception e) { |
| 192 | log.warn("Exception attempting to access metadata file at {}: {}", metadataUrl, e); |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | try { |
Ray Milkey | 961b19f | 2018-02-05 13:47:55 -0800 | [diff] [blame] | 196 | Thread.sleep((int) Math.pow(2, iterations < 7 ? ++iterations : iterations) * 10L); |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 197 | } catch (InterruptedException e) { |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 198 | Thread.currentThread().interrupt(); |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 199 | throw new IllegalStateException(e); |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 204 | private static NodeId getNodeId(NodePrototype node) { |
| 205 | if (node.getId() != null) { |
| 206 | return NodeId.nodeId(node.getId()); |
| 207 | } else if (node.getHost() != null) { |
| 208 | return NodeId.nodeId(node.getHost()); |
| 209 | } else if (node.getIp() != null) { |
| 210 | return NodeId.nodeId(node.getIp()); |
| 211 | } else { |
| 212 | return NodeId.nodeId(UUID.randomUUID().toString()); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | private static String getNodeHost(NodePrototype node) { |
| 217 | if (node.getHost() != null) { |
| 218 | return node.getHost(); |
| 219 | } else if (node.getIp() != null) { |
| 220 | return node.getIp(); |
| 221 | } else { |
| 222 | throw new IllegalArgumentException( |
| 223 | "Malformed cluster configuration: No host specified for node " + node.getId()); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | private static int getNodePort(NodePrototype node) { |
| 228 | if (node.getPort() != null) { |
| 229 | return node.getPort(); |
| 230 | } |
| 231 | return DefaultControllerNode.DEFAULT_PORT; |
| 232 | } |
| 233 | |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 234 | private Versioned<ClusterMetadata> fetchMetadata(String metadataUrl) { |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 235 | try { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 236 | URL url = new URL(metadataUrl); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 237 | ClusterMetadataPrototype metadata = null; |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 238 | long version = 0; |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 239 | if ("file".equals(url.getProtocol())) { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 240 | File file = new File(metadataUrl.replaceFirst("file://", "")); |
| 241 | version = file.lastModified(); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 242 | metadata = mapper.readValue(new FileInputStream(file), ClusterMetadataPrototype.class); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 243 | } else if ("http".equals(url.getProtocol())) { |
Jordan Halterman | da26837 | 2017-09-19 11:18:08 -0700 | [diff] [blame] | 244 | try { |
| 245 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 246 | if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { |
| 247 | log.warn("Could not reach metadata URL {}. Retrying...", url); |
| 248 | return null; |
| 249 | } |
| 250 | if (conn.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) { |
| 251 | return null; |
| 252 | } |
| 253 | version = conn.getLastModified(); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 254 | metadata = mapper.readValue(conn.getInputStream(), ClusterMetadataPrototype.class); |
Jordan Halterman | da26837 | 2017-09-19 11:18:08 -0700 | [diff] [blame] | 255 | } catch (IOException e) { |
Jordan Halterman | 46c7690 | 2017-08-24 14:55:25 -0700 | [diff] [blame] | 256 | log.warn("Could not reach metadata URL {}. Retrying...", url); |
| 257 | return null; |
| 258 | } |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 259 | } |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 260 | |
zhiyong ke | 60e4000 | 2017-04-13 13:50:47 +0800 | [diff] [blame] | 261 | if (null == metadata) { |
| 262 | log.warn("Metadata is null in the function fetchMetadata"); |
| 263 | throw new NullPointerException(); |
| 264 | } |
Jordan Halterman | dbfff06 | 2017-06-19 10:31:32 -0700 | [diff] [blame] | 265 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 266 | return new Versioned<>(new ClusterMetadata( |
| 267 | PROVIDER_ID, |
| 268 | metadata.getName(), |
| 269 | metadata.getNode() != null ? |
| 270 | new DefaultControllerNode( |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 271 | getNodeId(metadata.getNode()), |
| 272 | getNodeHost(metadata.getNode()), |
| 273 | getNodePort(metadata.getNode())) : null, |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 274 | metadata.getController() |
| 275 | .stream() |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 276 | .map(node -> new DefaultControllerNode(getNodeId(node), getNodeHost(node), getNodePort(node))) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 277 | .collect(Collectors.toSet()), |
Jordan Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 278 | metadata.getStorageDnsService(), |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 279 | metadata.getStorage() |
| 280 | .stream() |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 281 | .map(node -> new DefaultControllerNode(getNodeId(node), getNodeHost(node), getNodePort(node))) |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 282 | .collect(Collectors.toSet()), |
| 283 | metadata.getClusterSecret()), |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 284 | version); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 285 | } catch (IOException e) { |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 286 | throw new IllegalArgumentException(e); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 287 | } |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 288 | } |
| 289 | |
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 | * Monitors the metadata url for any updates and notifies providerService accordingly. |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 292 | */ |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 293 | private void watchUrl(String metadataUrl) { |
Ray Milkey | e3708c7 | 2017-11-13 13:10:46 -0800 | [diff] [blame] | 294 | if (!isAvailable()) { |
| 295 | return; |
| 296 | } |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 297 | // TODO: We are merely polling the url. |
| 298 | // 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] | 299 | try { |
| 300 | Versioned<ClusterMetadata> latestMetadata = fetchMetadata(metadataUrl); |
| 301 | if (cachedMetadata.get() != null && latestMetadata != null |
| 302 | && cachedMetadata.get().version() < latestMetadata.version()) { |
| 303 | cachedMetadata.set(latestMetadata); |
| 304 | providerService.clusterMetadataChanged(latestMetadata); |
| 305 | } |
| 306 | } catch (Exception e) { |
| 307 | log.error("Unable to parse metadata : ", e); |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 308 | } |
| 309 | } |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 310 | |
| 311 | private static class ClusterMetadataPrototype { |
| 312 | private String name; |
| 313 | private NodePrototype node; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 314 | private Set<NodePrototype> controller = Sets.newHashSet(); |
Jordan Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 315 | private String storageDnsService; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 316 | private Set<NodePrototype> storage = Sets.newHashSet(); |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 317 | private String clusterSecret; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 318 | |
| 319 | public String getName() { |
| 320 | return name; |
| 321 | } |
| 322 | |
| 323 | public void setName(String name) { |
| 324 | this.name = name; |
| 325 | } |
| 326 | |
| 327 | public NodePrototype getNode() { |
| 328 | return node; |
| 329 | } |
| 330 | |
| 331 | public void setNode(NodePrototype node) { |
| 332 | this.node = node; |
| 333 | } |
| 334 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 335 | public Set<NodePrototype> getController() { |
| 336 | return controller; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 339 | public void setController(Set<NodePrototype> controller) { |
| 340 | this.controller = controller; |
| 341 | } |
| 342 | |
Jordan Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 343 | public String getStorageDnsService() { |
| 344 | return storageDnsService; |
| 345 | } |
| 346 | |
| 347 | public void setStorageDnsService(String storageDnsService) { |
| 348 | this.storageDnsService = storageDnsService; |
| 349 | } |
| 350 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 351 | public Set<NodePrototype> getStorage() { |
| 352 | return storage; |
| 353 | } |
| 354 | |
| 355 | public void setStorage(Set<NodePrototype> storage) { |
| 356 | this.storage = storage; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 357 | } |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 358 | |
| 359 | public void setClusterSecret(String clusterSecret) { |
| 360 | this.clusterSecret = clusterSecret; |
| 361 | } |
| 362 | |
| 363 | public String getClusterSecret() { |
| 364 | return clusterSecret; |
| 365 | } |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | private static class NodePrototype { |
| 369 | private String id; |
| 370 | private String ip; |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 371 | private String host; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 372 | private Integer port; |
| 373 | |
| 374 | public String getId() { |
| 375 | return id; |
| 376 | } |
| 377 | |
| 378 | public void setId(String id) { |
| 379 | this.id = id; |
| 380 | } |
| 381 | |
| 382 | public String getIp() { |
| 383 | return ip; |
| 384 | } |
| 385 | |
| 386 | public void setIp(String ip) { |
| 387 | this.ip = ip; |
| 388 | } |
| 389 | |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 390 | public String getHost() { |
| 391 | return host; |
| 392 | } |
| 393 | |
| 394 | public void setHost(String host) { |
| 395 | this.host = host; |
| 396 | } |
| 397 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 398 | public Integer getPort() { |
| 399 | return port; |
| 400 | } |
| 401 | |
| 402 | public void setPort(Integer port) { |
| 403 | this.port = port; |
| 404 | } |
| 405 | } |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 406 | } |