Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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.app; |
| 17 | |
Yuta HIGUCHI | 8029205 | 2015-02-10 23:11:59 -0800 | [diff] [blame] | 18 | import com.google.common.base.Charsets; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.util.concurrent.ListenableFuture; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.util.KryoNamespace; |
| 28 | import org.onosproject.app.ApplicationDescription; |
| 29 | import org.onosproject.app.ApplicationEvent; |
| 30 | import org.onosproject.app.ApplicationException; |
| 31 | import org.onosproject.app.ApplicationState; |
| 32 | import org.onosproject.app.ApplicationStore; |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 33 | import org.onosproject.app.ApplicationStoreDelegate; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 34 | import org.onosproject.cluster.ClusterService; |
| 35 | import org.onosproject.cluster.ControllerNode; |
| 36 | import org.onosproject.common.app.ApplicationArchive; |
| 37 | import org.onosproject.core.Application; |
| 38 | import org.onosproject.core.ApplicationId; |
| 39 | import org.onosproject.core.ApplicationIdStore; |
| 40 | import org.onosproject.core.DefaultApplication; |
| 41 | import org.onosproject.core.Permission; |
| 42 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 43 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 44 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 45 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 46 | import org.onosproject.store.impl.MultiValuedTimestamp; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 47 | import org.onosproject.store.impl.WallclockClockManager; |
| 48 | import org.onosproject.store.serializers.KryoNamespaces; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 49 | import org.onosproject.store.service.ClockService; |
| 50 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 51 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 52 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 53 | import org.onosproject.store.service.StorageService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | |
| 56 | import java.io.ByteArrayInputStream; |
| 57 | import java.io.IOException; |
| 58 | import java.io.InputStream; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 59 | import java.util.Set; |
| 60 | import java.util.concurrent.CountDownLatch; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 61 | import java.util.concurrent.ExecutorService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 62 | import java.util.concurrent.Executors; |
| 63 | import java.util.concurrent.ScheduledExecutorService; |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 64 | import java.util.concurrent.atomic.AtomicLong; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 65 | |
| 66 | import static com.google.common.io.ByteStreams.toByteArray; |
| 67 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 68 | import static org.onlab.util.Tools.groupedThreads; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 69 | import static org.onosproject.app.ApplicationEvent.Type.APP_ACTIVATED; |
| 70 | import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED; |
| 71 | import static org.onosproject.app.ApplicationEvent.Type.APP_INSTALLED; |
| 72 | import static org.onosproject.app.ApplicationEvent.Type.APP_PERMISSIONS_CHANGED; |
| 73 | import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED; |
| 74 | import static org.onosproject.store.app.GossipApplicationStore.InternalState.ACTIVATED; |
| 75 | import static org.onosproject.store.app.GossipApplicationStore.InternalState.DEACTIVATED; |
| 76 | import static org.onosproject.store.app.GossipApplicationStore.InternalState.INSTALLED; |
| 77 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; |
| 78 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 79 | import static org.slf4j.LoggerFactory.getLogger; |
| 80 | |
| 81 | /** |
| 82 | * Manages inventory of applications in a distributed data store that uses |
| 83 | * optimistic replication and gossip based anti-entropy techniques. |
| 84 | */ |
| 85 | @Component(immediate = true) |
| 86 | @Service |
| 87 | public class GossipApplicationStore extends ApplicationArchive |
| 88 | implements ApplicationStore { |
| 89 | |
| 90 | private final Logger log = getLogger(getClass()); |
| 91 | |
| 92 | private static final MessageSubject APP_BITS_REQUEST = new MessageSubject("app-bits-request"); |
| 93 | |
| 94 | private static final int FETCH_TIMEOUT_MS = 10_000; |
| 95 | private static final int LOAD_TIMEOUT_MS = 5_000; |
| 96 | |
| 97 | public enum InternalState { |
| 98 | INSTALLED, ACTIVATED, DEACTIVATED |
| 99 | } |
| 100 | |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 101 | private ScheduledExecutorService executor; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 102 | private ExecutorService messageHandlingExecutor; |
| 103 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 104 | private EventuallyConsistentMap<ApplicationId, Application> apps; |
| 105 | private EventuallyConsistentMap<Application, InternalState> states; |
| 106 | private EventuallyConsistentMap<Application, Set<Permission>> permissions; |
| 107 | |
| 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected ClusterCommunicationService clusterCommunicator; |
| 110 | |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 112 | protected ClusterService clusterService; |
| 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 115 | protected StorageService storageService; |
| 116 | |
| 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 118 | protected ApplicationIdStore idStore; |
| 119 | |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 120 | private final AtomicLong sequence = new AtomicLong(); |
| 121 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 122 | @Activate |
| 123 | public void activate() { |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 124 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 125 | .register(KryoNamespaces.API) |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 126 | .register(MultiValuedTimestamp.class) |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 127 | .register(InternalState.class); |
| 128 | |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 129 | executor = Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/app", "store")); |
| 130 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 131 | messageHandlingExecutor = Executors.newSingleThreadExecutor( |
| 132 | groupedThreads("onos/store/app", "message-handler")); |
| 133 | |
| 134 | clusterCommunicator.addSubscriber(APP_BITS_REQUEST, new InternalBitServer(), messageHandlingExecutor); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 135 | |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 136 | // FIXME: Consider consolidating into a single map. |
| 137 | |
| 138 | ClockService<ApplicationId, Application> appsClockService = (appId, app) -> |
| 139 | new MultiValuedTimestamp<>(getUpdateTime(appId.name()), |
| 140 | sequence.incrementAndGet()); |
| 141 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 142 | apps = storageService.<ApplicationId, Application>eventuallyConsistentMapBuilder() |
| 143 | .withName("apps") |
| 144 | .withSerializer(serializer) |
| 145 | .withClockService(appsClockService) |
| 146 | .build(); |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 147 | |
| 148 | ClockService<Application, InternalState> statesClockService = (app, state) -> |
| 149 | new MultiValuedTimestamp<>(getUpdateTime(app.id().name()), |
| 150 | sequence.incrementAndGet()); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 151 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 152 | states = storageService.<Application, InternalState>eventuallyConsistentMapBuilder() |
| 153 | .withName("app-states") |
| 154 | .withSerializer(serializer) |
| 155 | .withClockService(statesClockService) |
| 156 | .build(); |
| 157 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 158 | states.addListener(new InternalAppStatesListener()); |
| 159 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 160 | permissions = storageService.<Application, Set<Permission>>eventuallyConsistentMapBuilder() |
| 161 | .withName("app-permissions") |
| 162 | .withSerializer(serializer) |
| 163 | .withClockService(new WallclockClockManager<>()) |
| 164 | .build(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 165 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 166 | log.info("Started"); |
| 167 | } |
| 168 | |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 169 | /** |
| 170 | * Loads the application inventory from the disk and activates apps if |
| 171 | * they are marked to be active. |
| 172 | */ |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 173 | private void loadFromDisk() { |
| 174 | for (String name : getApplicationNames()) { |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame^] | 175 | Application app = create(getApplicationDescription(name), false); |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 176 | if (app != null && isActive(app.id().name())) { |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame^] | 177 | activate(app.id(), false); |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 178 | // load app permissions |
| 179 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | @Deactivate |
| 184 | public void deactivate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 185 | clusterCommunicator.removeSubscriber(APP_BITS_REQUEST); |
| 186 | messageHandlingExecutor.shutdown(); |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 187 | executor.shutdown(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 188 | apps.destroy(); |
| 189 | states.destroy(); |
| 190 | permissions.destroy(); |
| 191 | log.info("Stopped"); |
| 192 | } |
| 193 | |
| 194 | @Override |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 195 | public void setDelegate(ApplicationStoreDelegate delegate) { |
| 196 | super.setDelegate(delegate); |
| 197 | loadFromDisk(); |
| 198 | // executor.schedule(this::pruneUninstalledApps, LOAD_TIMEOUT_MS, MILLISECONDS); |
| 199 | } |
| 200 | |
| 201 | @Override |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 202 | public Set<Application> getApplications() { |
| 203 | return ImmutableSet.copyOf(apps.values()); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public ApplicationId getId(String name) { |
| 208 | return idStore.getAppId(name); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public Application getApplication(ApplicationId appId) { |
| 213 | return apps.get(appId); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public ApplicationState getState(ApplicationId appId) { |
| 218 | Application app = apps.get(appId); |
| 219 | InternalState s = app == null ? null : states.get(app); |
| 220 | return s == null ? null : s == ACTIVATED ? |
| 221 | ApplicationState.ACTIVE : ApplicationState.INSTALLED; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public Application create(InputStream appDescStream) { |
| 226 | ApplicationDescription appDesc = saveApplication(appDescStream); |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame^] | 227 | return create(appDesc, true); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame^] | 230 | private Application create(ApplicationDescription appDesc, boolean updateTime) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 231 | Application app = registerApp(appDesc); |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame^] | 232 | if (updateTime) { |
| 233 | updateTime(app.id().name()); |
| 234 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 235 | apps.put(app.id(), app); |
| 236 | states.put(app, INSTALLED); |
| 237 | return app; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void remove(ApplicationId appId) { |
| 242 | Application app = apps.get(appId); |
| 243 | if (app != null) { |
| 244 | apps.remove(appId); |
| 245 | states.remove(app); |
| 246 | permissions.remove(app); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public void activate(ApplicationId appId) { |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame^] | 252 | activate(appId, true); |
| 253 | } |
| 254 | |
| 255 | private void activate(ApplicationId appId, boolean updateTime) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 256 | Application app = apps.get(appId); |
| 257 | if (app != null) { |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame^] | 258 | if (updateTime) { |
| 259 | updateTime(appId.name()); |
| 260 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 261 | states.put(app, ACTIVATED); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void deactivate(ApplicationId appId) { |
| 267 | Application app = apps.get(appId); |
| 268 | if (app != null) { |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame^] | 269 | updateTime(appId.name()); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 270 | states.put(app, DEACTIVATED); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public Set<Permission> getPermissions(ApplicationId appId) { |
| 276 | Application app = apps.get(appId); |
| 277 | return app != null ? permissions.get(app) : null; |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public void setPermissions(ApplicationId appId, Set<Permission> permissions) { |
| 282 | Application app = getApplication(appId); |
| 283 | if (app != null) { |
| 284 | this.permissions.put(app, permissions); |
| 285 | delegate.notify(new ApplicationEvent(APP_PERMISSIONS_CHANGED, app)); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Listener to application state distributed map changes. |
| 291 | */ |
| 292 | private final class InternalAppStatesListener |
| 293 | implements EventuallyConsistentMapListener<Application, InternalState> { |
| 294 | @Override |
| 295 | public void event(EventuallyConsistentMapEvent<Application, InternalState> event) { |
Thomas Vachuska | 4fcdae7 | 2015-03-24 10:22:54 -0700 | [diff] [blame] | 296 | // If we do not have a delegate, refuse to process any events entirely. |
| 297 | // This is to allow the anti-entropy to kick in and process the events |
| 298 | // perhaps a bit later, but with opportunity to notify delegate. |
| 299 | if (delegate == null) { |
| 300 | return; |
| 301 | } |
| 302 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 303 | Application app = event.key(); |
| 304 | InternalState state = event.value(); |
| 305 | |
| 306 | if (event.type() == PUT) { |
| 307 | if (state == INSTALLED) { |
| 308 | fetchBitsIfNeeded(app); |
| 309 | delegate.notify(new ApplicationEvent(APP_INSTALLED, app)); |
| 310 | |
| 311 | } else if (state == ACTIVATED) { |
| 312 | installAppIfNeeded(app); |
| 313 | setActive(app.id().name()); |
| 314 | delegate.notify(new ApplicationEvent(APP_ACTIVATED, app)); |
| 315 | |
| 316 | } else if (state == DEACTIVATED) { |
| 317 | clearActive(app.id().name()); |
| 318 | delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app)); |
| 319 | } |
| 320 | } else if (event.type() == REMOVE) { |
| 321 | delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app)); |
| 322 | purgeApplication(app.id().name()); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Determines if the application bits are available locally. |
| 329 | */ |
| 330 | private boolean appBitsAvailable(Application app) { |
| 331 | try { |
| 332 | ApplicationDescription appDesc = getApplicationDescription(app.id().name()); |
| 333 | return appDesc.version().equals(app.version()); |
| 334 | } catch (ApplicationException e) { |
| 335 | return false; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Fetches the bits from the cluster peers if necessary. |
| 341 | */ |
| 342 | private void fetchBitsIfNeeded(Application app) { |
| 343 | if (!appBitsAvailable(app)) { |
| 344 | fetchBits(app); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Installs the application if necessary from the application peers. |
| 350 | */ |
| 351 | private void installAppIfNeeded(Application app) { |
| 352 | if (!appBitsAvailable(app)) { |
| 353 | fetchBits(app); |
| 354 | delegate.notify(new ApplicationEvent(APP_INSTALLED, app)); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Fetches the bits from the cluster peers. |
| 360 | */ |
| 361 | private void fetchBits(Application app) { |
| 362 | ControllerNode localNode = clusterService.getLocalNode(); |
| 363 | ClusterMessage message = new ClusterMessage(localNode.id(), APP_BITS_REQUEST, |
Yuta HIGUCHI | 8029205 | 2015-02-10 23:11:59 -0800 | [diff] [blame] | 364 | app.id().name().getBytes(Charsets.UTF_8)); |
| 365 | //Map<ControllerNode, ListenableFuture<byte[]>> futures = new HashMap<>(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 366 | CountDownLatch latch = new CountDownLatch(1); |
| 367 | |
| 368 | // FIXME: send message with name & version to make sure we don't get served old bits |
| 369 | |
| 370 | log.info("Downloading bits for application {}", app.id().name()); |
| 371 | for (ControllerNode node : clusterService.getNodes()) { |
| 372 | try { |
| 373 | ListenableFuture<byte[]> future = clusterCommunicator.sendAndReceive(message, node.id()); |
| 374 | future.addListener(new InternalBitListener(app, node, future, latch), executor); |
| 375 | } catch (IOException e) { |
| 376 | log.debug("Unable to request bits for application {} from node {}", |
| 377 | app.id().name(), node.id()); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | try { |
| 382 | if (!latch.await(FETCH_TIMEOUT_MS, MILLISECONDS)) { |
| 383 | log.warn("Unable to fetch bits for application {}", app.id().name()); |
| 384 | } |
| 385 | } catch (InterruptedException e) { |
| 386 | log.warn("Interrupted while fetching bits for application {}", app.id().name()); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Responder to requests for application bits. |
| 392 | */ |
| 393 | private class InternalBitServer implements ClusterMessageHandler { |
| 394 | @Override |
| 395 | public void handle(ClusterMessage message) { |
Yuta HIGUCHI | 8029205 | 2015-02-10 23:11:59 -0800 | [diff] [blame] | 396 | String name = new String(message.payload(), Charsets.UTF_8); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 397 | try { |
| 398 | message.respond(toByteArray(getApplicationInputStream(name))); |
| 399 | } catch (Exception e) { |
| 400 | log.debug("Unable to read bits for application {}", name); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Processes completed fetch requests. |
| 407 | */ |
| 408 | private class InternalBitListener implements Runnable { |
| 409 | private final Application app; |
| 410 | private final ControllerNode node; |
| 411 | private final ListenableFuture<byte[]> future; |
| 412 | private final CountDownLatch latch; |
| 413 | |
| 414 | public InternalBitListener(Application app, ControllerNode node, |
| 415 | ListenableFuture<byte[]> future, CountDownLatch latch) { |
| 416 | this.app = app; |
| 417 | this.node = node; |
| 418 | this.future = future; |
| 419 | this.latch = latch; |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public void run() { |
| 424 | if (latch.getCount() > 0 && !future.isCancelled()) { |
| 425 | try { |
| 426 | byte[] bits = future.get(1, MILLISECONDS); |
| 427 | saveApplication(new ByteArrayInputStream(bits)); |
| 428 | log.info("Downloaded bits for application {} from node {}", |
| 429 | app.id().name(), node.id()); |
| 430 | latch.countDown(); |
| 431 | } catch (Exception e) { |
| 432 | log.warn("Unable to fetch bits for application {} from node {}", |
| 433 | app.id().name(), node.id()); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Prunes applications which are not in the map, but are on disk. |
| 441 | */ |
| 442 | private void pruneUninstalledApps() { |
| 443 | for (String name : getApplicationNames()) { |
| 444 | if (getApplication(getId(name)) == null) { |
| 445 | Application app = registerApp(getApplicationDescription(name)); |
| 446 | delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app)); |
| 447 | purgeApplication(app.id().name()); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Produces a registered application from the supplied description. |
| 454 | */ |
| 455 | private Application registerApp(ApplicationDescription appDesc) { |
| 456 | ApplicationId appId = idStore.registerApplication(appDesc.name()); |
| 457 | return new DefaultApplication(appId, appDesc.version(), appDesc.description(), |
| 458 | appDesc.origin(), appDesc.permissions(), |
| 459 | appDesc.featuresRepo(), appDesc.features()); |
| 460 | } |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 461 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 462 | } |
| 463 | |