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