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; |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Multimap; |
| 22 | import com.google.common.collect.Sets; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.apache.felix.scr.annotations.Service; |
| 29 | import org.onlab.util.KryoNamespace; |
| 30 | import org.onosproject.app.ApplicationDescription; |
| 31 | import org.onosproject.app.ApplicationEvent; |
| 32 | import org.onosproject.app.ApplicationException; |
| 33 | import org.onosproject.app.ApplicationState; |
| 34 | import org.onosproject.app.ApplicationStore; |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 35 | import org.onosproject.app.ApplicationStoreDelegate; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 36 | import org.onosproject.cluster.ClusterService; |
| 37 | import org.onosproject.cluster.ControllerNode; |
| 38 | import org.onosproject.common.app.ApplicationArchive; |
| 39 | import org.onosproject.core.Application; |
| 40 | import org.onosproject.core.ApplicationId; |
| 41 | import org.onosproject.core.ApplicationIdStore; |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 42 | import org.onosproject.core.CoreService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 43 | import org.onosproject.core.DefaultApplication; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 44 | import org.onosproject.security.Permission; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 45 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 46 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 47 | import org.onosproject.store.serializers.KryoNamespaces; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 48 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 49 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 50 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 51 | import org.onosproject.store.service.LogicalClockService; |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 52 | import org.onosproject.store.service.MultiValuedTimestamp; |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 53 | import org.onosproject.store.service.StorageException; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 54 | import org.onosproject.store.service.StorageService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 55 | import org.slf4j.Logger; |
| 56 | |
| 57 | import java.io.ByteArrayInputStream; |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 58 | import java.io.IOException; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 59 | import java.io.InputStream; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 60 | import java.util.Set; |
| 61 | import java.util.concurrent.CountDownLatch; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 62 | import java.util.concurrent.ExecutorService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 63 | import java.util.concurrent.Executors; |
| 64 | import java.util.concurrent.ScheduledExecutorService; |
Jonathan Hart | d6fb053 | 2016-01-21 17:28:20 -0800 | [diff] [blame] | 65 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 66 | import java.util.function.Function; |
| 67 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 68 | import static com.google.common.collect.Multimaps.newSetMultimap; |
| 69 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 70 | import static com.google.common.io.ByteStreams.toByteArray; |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame^] | 71 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 72 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 73 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | adba152 | 2015-06-04 15:08:30 -0700 | [diff] [blame] | 74 | import static org.onlab.util.Tools.randomDelay; |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 75 | import static org.onosproject.app.ApplicationEvent.Type.*; |
| 76 | import static org.onosproject.store.app.GossipApplicationStore.InternalState.*; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 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 | |
Thomas Vachuska | adba152 | 2015-06-04 15:08:30 -0700 | [diff] [blame] | 94 | private static final int MAX_LOAD_RETRIES = 5; |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 95 | private static final int RETRY_DELAY_MS = 2_000; |
| 96 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 97 | private static final int FETCH_TIMEOUT_MS = 10_000; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 98 | |
Jonathan Hart | d6fb053 | 2016-01-21 17:28:20 -0800 | [diff] [blame] | 99 | private static final int APP_LOAD_DELAY_MS = 500; |
| 100 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 101 | public enum InternalState { |
| 102 | INSTALLED, ACTIVATED, DEACTIVATED |
| 103 | } |
| 104 | |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 105 | private ScheduledExecutorService executor; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 106 | private ExecutorService messageHandlingExecutor; |
| 107 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 108 | private EventuallyConsistentMap<ApplicationId, Application> apps; |
| 109 | private EventuallyConsistentMap<Application, InternalState> states; |
| 110 | private EventuallyConsistentMap<Application, Set<Permission>> permissions; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 113 | protected ClusterCommunicationService clusterCommunicator; |
| 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected ClusterService clusterService; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 119 | protected StorageService storageService; |
| 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 122 | protected LogicalClockService clockService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 123 | |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected ApplicationIdStore idStore; |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 126 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 127 | // Multimap to track which apps are required by others apps |
| 128 | // app -> { required-by, ... } |
| 129 | // Apps explicitly activated will be required by the CORE app |
| 130 | private final Multimap<ApplicationId, ApplicationId> requiredBy = |
| 131 | synchronizedSetMultimap(newSetMultimap(Maps.newHashMap(), Sets::newHashSet)); |
| 132 | |
| 133 | private ApplicationId coreAppId; |
| 134 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 135 | @Activate |
| 136 | public void activate() { |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 137 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 138 | .register(KryoNamespaces.API) |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 139 | .register(MultiValuedTimestamp.class) |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 140 | .register(InternalState.class); |
| 141 | |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame^] | 142 | executor = newSingleThreadScheduledExecutor(groupedThreads("onos/app", "store", log)); |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 143 | |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 144 | messageHandlingExecutor = Executors.newSingleThreadExecutor( |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame^] | 145 | groupedThreads("onos/store/app", "message-handler", log)); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 146 | |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 147 | clusterCommunicator.<String, byte[]>addSubscriber(APP_BITS_REQUEST, |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 148 | bytes -> new String(bytes, Charsets.UTF_8), |
| 149 | name -> { |
| 150 | try { |
| 151 | return toByteArray(getApplicationInputStream(name)); |
| 152 | } catch (IOException e) { |
| 153 | throw new StorageException(e); |
| 154 | } |
| 155 | }, |
| 156 | Function.identity(), |
| 157 | messageHandlingExecutor); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 158 | |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 159 | // FIXME: Consider consolidating into a single map. |
| 160 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 161 | apps = storageService.<ApplicationId, Application>eventuallyConsistentMapBuilder() |
| 162 | .withName("apps") |
| 163 | .withSerializer(serializer) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 164 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 165 | .build(); |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 166 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 167 | states = storageService.<Application, InternalState>eventuallyConsistentMapBuilder() |
| 168 | .withName("app-states") |
| 169 | .withSerializer(serializer) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 170 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 171 | .build(); |
| 172 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 173 | states.addListener(new InternalAppStatesListener()); |
| 174 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 175 | permissions = storageService.<Application, Set<Permission>>eventuallyConsistentMapBuilder() |
| 176 | .withName("app-permissions") |
| 177 | .withSerializer(serializer) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 178 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 179 | .build(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 180 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 181 | coreAppId = getId(CoreService.CORE_APP_NAME); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 182 | log.info("Started"); |
| 183 | } |
| 184 | |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 185 | /** |
| 186 | * Loads the application inventory from the disk and activates apps if |
| 187 | * they are marked to be active. |
| 188 | */ |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 189 | private void loadFromDisk() { |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 190 | getApplicationNames().forEach(appName -> { |
| 191 | Application app = loadFromDisk(appName); |
| 192 | if (app != null && isActive(app.id().name())) { |
| 193 | activate(app.id(), false); |
| 194 | // TODO Load app permissions |
| 195 | } |
| 196 | }); |
| 197 | } |
| 198 | |
| 199 | private Application loadFromDisk(String appName) { |
| 200 | for (int i = 0; i < MAX_LOAD_RETRIES; i++) { |
| 201 | try { |
| 202 | // Directly return if app already exists |
| 203 | ApplicationId appId = getId(appName); |
| 204 | if (appId != null) { |
Ray Milkey | 64591a6 | 2016-01-12 09:50:45 -0800 | [diff] [blame] | 205 | Application application = getApplication(appId); |
| 206 | if (application != null) { |
| 207 | return application; |
| 208 | } |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 209 | } |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 210 | |
| 211 | ApplicationDescription appDesc = getApplicationDescription(appName); |
| 212 | boolean success = appDesc.requiredApps().stream() |
| 213 | .noneMatch(requiredApp -> loadFromDisk(requiredApp) == null); |
| 214 | return success ? create(appDesc, false) : null; |
| 215 | } catch (Exception e) { |
| 216 | log.warn("Unable to load application {} from disk; retrying", appName); |
| 217 | randomDelay(RETRY_DELAY_MS); //FIXME: This is a deliberate hack; fix in Falcon |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 218 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 219 | } |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 220 | return null; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Deactivate |
| 224 | public void deactivate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 225 | clusterCommunicator.removeSubscriber(APP_BITS_REQUEST); |
| 226 | messageHandlingExecutor.shutdown(); |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 227 | executor.shutdown(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 228 | apps.destroy(); |
| 229 | states.destroy(); |
| 230 | permissions.destroy(); |
| 231 | log.info("Stopped"); |
| 232 | } |
| 233 | |
| 234 | @Override |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 235 | public void setDelegate(ApplicationStoreDelegate delegate) { |
| 236 | super.setDelegate(delegate); |
Jonathan Hart | d6fb053 | 2016-01-21 17:28:20 -0800 | [diff] [blame] | 237 | executor.schedule(() -> loadFromDisk(), APP_LOAD_DELAY_MS, TimeUnit.MILLISECONDS); |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 241 | public Set<Application> getApplications() { |
| 242 | return ImmutableSet.copyOf(apps.values()); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public ApplicationId getId(String name) { |
| 247 | return idStore.getAppId(name); |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public Application getApplication(ApplicationId appId) { |
| 252 | return apps.get(appId); |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public ApplicationState getState(ApplicationId appId) { |
| 257 | Application app = apps.get(appId); |
| 258 | InternalState s = app == null ? null : states.get(app); |
| 259 | return s == null ? null : s == ACTIVATED ? |
| 260 | ApplicationState.ACTIVE : ApplicationState.INSTALLED; |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public Application create(InputStream appDescStream) { |
| 265 | ApplicationDescription appDesc = saveApplication(appDescStream); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 266 | if (hasPrerequisites(appDesc)) { |
| 267 | return create(appDesc, true); |
| 268 | } |
| 269 | throw new ApplicationException("Missing dependencies for app " + appDesc.name()); |
| 270 | } |
| 271 | |
| 272 | private boolean hasPrerequisites(ApplicationDescription app) { |
| 273 | return !app.requiredApps().stream().map(n -> getId(n)) |
| 274 | .anyMatch(id -> id == null || getApplication(id) == null); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 277 | private Application create(ApplicationDescription appDesc, boolean updateTime) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 278 | Application app = registerApp(appDesc); |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 279 | if (updateTime) { |
| 280 | updateTime(app.id().name()); |
| 281 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 282 | apps.put(app.id(), app); |
| 283 | states.put(app, INSTALLED); |
| 284 | return app; |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void remove(ApplicationId appId) { |
| 289 | Application app = apps.get(appId); |
| 290 | if (app != null) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 291 | uninstallDependentApps(app); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 292 | apps.remove(appId); |
| 293 | states.remove(app); |
| 294 | permissions.remove(app); |
| 295 | } |
| 296 | } |
| 297 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 298 | // Uninstalls all apps that depend on the given app. |
| 299 | private void uninstallDependentApps(Application app) { |
| 300 | getApplications().stream() |
| 301 | .filter(a -> a.requiredApps().contains(app.id().name())) |
| 302 | .forEach(a -> remove(a.id())); |
| 303 | } |
| 304 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 305 | @Override |
| 306 | public void activate(ApplicationId appId) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 307 | activate(appId, coreAppId); |
| 308 | } |
| 309 | |
| 310 | private void activate(ApplicationId appId, ApplicationId forAppId) { |
| 311 | requiredBy.put(appId, forAppId); |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 312 | activate(appId, true); |
| 313 | } |
| 314 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 315 | |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 316 | private void activate(ApplicationId appId, boolean updateTime) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 317 | Application app = apps.get(appId); |
| 318 | if (app != null) { |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 319 | if (updateTime) { |
| 320 | updateTime(appId.name()); |
| 321 | } |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 322 | activateRequiredApps(app); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 323 | states.put(app, ACTIVATED); |
| 324 | } |
| 325 | } |
| 326 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 327 | // Activates all apps required by this application. |
| 328 | private void activateRequiredApps(Application app) { |
| 329 | app.requiredApps().stream().map(this::getId).forEach(id -> activate(id, app.id())); |
| 330 | } |
| 331 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 332 | @Override |
| 333 | public void deactivate(ApplicationId appId) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 334 | deactivateDependentApps(getApplication(appId)); |
| 335 | deactivate(appId, coreAppId); |
| 336 | } |
| 337 | |
| 338 | private void deactivate(ApplicationId appId, ApplicationId forAppId) { |
| 339 | requiredBy.remove(appId, forAppId); |
| 340 | if (requiredBy.get(appId).isEmpty()) { |
| 341 | Application app = apps.get(appId); |
| 342 | if (app != null) { |
| 343 | updateTime(appId.name()); |
| 344 | states.put(app, DEACTIVATED); |
| 345 | deactivateRequiredApps(app); |
| 346 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 350 | // Deactivates all apps that require this application. |
| 351 | private void deactivateDependentApps(Application app) { |
| 352 | getApplications().stream() |
| 353 | .filter(a -> states.get(a) == ACTIVATED) |
| 354 | .filter(a -> a.requiredApps().contains(app.id().name())) |
| 355 | .forEach(a -> deactivate(a.id())); |
| 356 | } |
| 357 | |
| 358 | // Deactivates all apps required by this application. |
| 359 | private void deactivateRequiredApps(Application app) { |
| 360 | app.requiredApps().stream().map(this::getId).map(this::getApplication) |
| 361 | .filter(a -> states.get(a) == ACTIVATED) |
| 362 | .forEach(a -> deactivate(a.id(), app.id())); |
| 363 | } |
| 364 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 365 | @Override |
| 366 | public Set<Permission> getPermissions(ApplicationId appId) { |
| 367 | Application app = apps.get(appId); |
| 368 | return app != null ? permissions.get(app) : null; |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public void setPermissions(ApplicationId appId, Set<Permission> permissions) { |
| 373 | Application app = getApplication(appId); |
| 374 | if (app != null) { |
| 375 | this.permissions.put(app, permissions); |
| 376 | delegate.notify(new ApplicationEvent(APP_PERMISSIONS_CHANGED, app)); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Listener to application state distributed map changes. |
| 382 | */ |
| 383 | private final class InternalAppStatesListener |
| 384 | implements EventuallyConsistentMapListener<Application, InternalState> { |
| 385 | @Override |
| 386 | public void event(EventuallyConsistentMapEvent<Application, InternalState> event) { |
Thomas Vachuska | 4fcdae7 | 2015-03-24 10:22:54 -0700 | [diff] [blame] | 387 | // If we do not have a delegate, refuse to process any events entirely. |
| 388 | // This is to allow the anti-entropy to kick in and process the events |
| 389 | // perhaps a bit later, but with opportunity to notify delegate. |
| 390 | if (delegate == null) { |
| 391 | return; |
| 392 | } |
| 393 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 394 | Application app = event.key(); |
| 395 | InternalState state = event.value(); |
| 396 | |
| 397 | if (event.type() == PUT) { |
| 398 | if (state == INSTALLED) { |
| 399 | fetchBitsIfNeeded(app); |
| 400 | delegate.notify(new ApplicationEvent(APP_INSTALLED, app)); |
| 401 | |
| 402 | } else if (state == ACTIVATED) { |
| 403 | installAppIfNeeded(app); |
| 404 | setActive(app.id().name()); |
| 405 | delegate.notify(new ApplicationEvent(APP_ACTIVATED, app)); |
| 406 | |
| 407 | } else if (state == DEACTIVATED) { |
| 408 | clearActive(app.id().name()); |
| 409 | delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app)); |
| 410 | } |
| 411 | } else if (event.type() == REMOVE) { |
| 412 | delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app)); |
| 413 | purgeApplication(app.id().name()); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Determines if the application bits are available locally. |
| 420 | */ |
| 421 | private boolean appBitsAvailable(Application app) { |
| 422 | try { |
| 423 | ApplicationDescription appDesc = getApplicationDescription(app.id().name()); |
| 424 | return appDesc.version().equals(app.version()); |
| 425 | } catch (ApplicationException e) { |
| 426 | return false; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Fetches the bits from the cluster peers if necessary. |
| 432 | */ |
| 433 | private void fetchBitsIfNeeded(Application app) { |
| 434 | if (!appBitsAvailable(app)) { |
| 435 | fetchBits(app); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Installs the application if necessary from the application peers. |
| 441 | */ |
| 442 | private void installAppIfNeeded(Application app) { |
| 443 | if (!appBitsAvailable(app)) { |
| 444 | fetchBits(app); |
| 445 | delegate.notify(new ApplicationEvent(APP_INSTALLED, app)); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Fetches the bits from the cluster peers. |
| 451 | */ |
| 452 | private void fetchBits(Application app) { |
| 453 | ControllerNode localNode = clusterService.getLocalNode(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 454 | CountDownLatch latch = new CountDownLatch(1); |
| 455 | |
| 456 | // FIXME: send message with name & version to make sure we don't get served old bits |
| 457 | |
| 458 | log.info("Downloading bits for application {}", app.id().name()); |
| 459 | for (ControllerNode node : clusterService.getNodes()) { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 460 | if (latch.getCount() == 0) { |
| 461 | break; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 462 | } |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 463 | if (node.equals(localNode)) { |
| 464 | continue; |
| 465 | } |
| 466 | clusterCommunicator.sendAndReceive(app.id().name(), |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 467 | APP_BITS_REQUEST, |
| 468 | s -> s.getBytes(Charsets.UTF_8), |
| 469 | Function.identity(), |
| 470 | node.id()) |
| 471 | .whenCompleteAsync((bits, error) -> { |
| 472 | if (error == null && latch.getCount() > 0) { |
| 473 | saveApplication(new ByteArrayInputStream(bits)); |
| 474 | log.info("Downloaded bits for application {} from node {}", |
| 475 | app.id().name(), node.id()); |
| 476 | latch.countDown(); |
| 477 | } else if (error != null) { |
| 478 | log.warn("Unable to fetch bits for application {} from node {}", |
| 479 | app.id().name(), node.id()); |
| 480 | } |
| 481 | }, executor); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | try { |
| 485 | if (!latch.await(FETCH_TIMEOUT_MS, MILLISECONDS)) { |
| 486 | log.warn("Unable to fetch bits for application {}", app.id().name()); |
| 487 | } |
| 488 | } catch (InterruptedException e) { |
| 489 | log.warn("Interrupted while fetching bits for application {}", app.id().name()); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /** |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 494 | * Prunes applications which are not in the map, but are on disk. |
| 495 | */ |
| 496 | private void pruneUninstalledApps() { |
| 497 | for (String name : getApplicationNames()) { |
| 498 | if (getApplication(getId(name)) == null) { |
| 499 | Application app = registerApp(getApplicationDescription(name)); |
| 500 | delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app)); |
| 501 | purgeApplication(app.id().name()); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Produces a registered application from the supplied description. |
| 508 | */ |
| 509 | private Application registerApp(ApplicationDescription appDesc) { |
| 510 | ApplicationId appId = idStore.registerApplication(appDesc.name()); |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 511 | return new DefaultApplication(appId, |
| 512 | appDesc.version(), |
| 513 | appDesc.title(), |
| 514 | appDesc.description(), |
| 515 | appDesc.origin(), |
| 516 | appDesc.category(), |
| 517 | appDesc.url(), |
| 518 | appDesc.readme(), |
| 519 | appDesc.icon(), |
| 520 | appDesc.role(), |
| 521 | appDesc.permissions(), |
| 522 | appDesc.featuresRepo(), |
| 523 | appDesc.features(), |
| 524 | appDesc.requiredApps()); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 525 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 526 | } |