Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.base.Preconditions; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Maps; |
| 24 | import com.google.common.collect.Multimap; |
| 25 | import com.google.common.collect.Sets; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 26 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
| 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 32 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 33 | import org.onosproject.app.ApplicationDescription; |
| 34 | import org.onosproject.app.ApplicationEvent; |
| 35 | import org.onosproject.app.ApplicationException; |
| 36 | import org.onosproject.app.ApplicationState; |
| 37 | import org.onosproject.app.ApplicationStore; |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 38 | import org.onosproject.app.ApplicationStoreDelegate; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 39 | import org.onosproject.cluster.ClusterService; |
| 40 | import org.onosproject.cluster.ControllerNode; |
| 41 | import org.onosproject.common.app.ApplicationArchive; |
| 42 | import org.onosproject.core.Application; |
| 43 | import org.onosproject.core.ApplicationId; |
sangyun-han | 6d33e80 | 2016-08-05 13:36:33 +0900 | [diff] [blame] | 44 | import org.onosproject.app.ApplicationIdStore; |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 45 | import org.onosproject.core.CoreService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 46 | import org.onosproject.core.DefaultApplication; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 47 | import org.onosproject.security.Permission; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 48 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 49 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 50 | import org.onosproject.store.serializers.KryoNamespaces; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 51 | import org.onosproject.store.service.ConsistentMap; |
| 52 | import org.onosproject.store.service.MapEvent; |
| 53 | import org.onosproject.store.service.MapEventListener; |
| 54 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 55 | import org.onosproject.store.service.StorageException; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 56 | import org.onosproject.store.service.StorageService; |
Madan Jampani | c5a1754 | 2016-08-22 10:26:11 -0700 | [diff] [blame] | 57 | import org.onosproject.store.service.Topic; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 58 | import org.onosproject.store.service.Versioned; |
| 59 | import org.onosproject.store.service.DistributedPrimitive.Status; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 60 | import org.slf4j.Logger; |
| 61 | |
| 62 | import java.io.ByteArrayInputStream; |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 63 | import java.io.IOException; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 64 | import java.io.InputStream; |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 65 | import java.util.List; |
| 66 | import java.util.Optional; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 67 | import java.util.Set; |
| 68 | import java.util.concurrent.CountDownLatch; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 69 | import java.util.concurrent.ExecutorService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 70 | import java.util.concurrent.ScheduledExecutorService; |
Jonathan Hart | d6fb053 | 2016-01-21 17:28:20 -0800 | [diff] [blame] | 71 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 72 | import java.util.concurrent.atomic.AtomicBoolean; |
| 73 | import java.util.function.Consumer; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 74 | import java.util.function.Function; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 75 | import java.util.stream.Collectors; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 76 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 77 | import static com.google.common.collect.Multimaps.newSetMultimap; |
| 78 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 79 | import static com.google.common.io.ByteStreams.toByteArray; |
Thomas Vachuska | 7cb9239 | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 80 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 81 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 82 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 83 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | adba152 | 2015-06-04 15:08:30 -0700 | [diff] [blame] | 84 | import static org.onlab.util.Tools.randomDelay; |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 85 | import static org.onosproject.app.ApplicationEvent.Type.*; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 86 | import static org.onosproject.store.app.DistributedApplicationStore.InternalState.*; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 87 | import static org.slf4j.LoggerFactory.getLogger; |
| 88 | |
| 89 | /** |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 90 | * Manages inventory of applications in a distributed data store providing |
| 91 | * stronger consistency guarantees. |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 92 | */ |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 93 | @Component(immediate = true) |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 94 | @Service |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 95 | public class DistributedApplicationStore extends ApplicationArchive |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 96 | implements ApplicationStore { |
| 97 | |
| 98 | private final Logger log = getLogger(getClass()); |
| 99 | |
| 100 | private static final MessageSubject APP_BITS_REQUEST = new MessageSubject("app-bits-request"); |
| 101 | |
Thomas Vachuska | adba152 | 2015-06-04 15:08:30 -0700 | [diff] [blame] | 102 | private static final int MAX_LOAD_RETRIES = 5; |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 103 | private static final int RETRY_DELAY_MS = 2_000; |
| 104 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 105 | private static final int FETCH_TIMEOUT_MS = 10_000; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 106 | |
Jonathan Hart | d6fb053 | 2016-01-21 17:28:20 -0800 | [diff] [blame] | 107 | private static final int APP_LOAD_DELAY_MS = 500; |
| 108 | |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 109 | private static List<String> pendingApps = Lists.newArrayList(); |
| 110 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 111 | public enum InternalState { |
| 112 | INSTALLED, ACTIVATED, DEACTIVATED |
| 113 | } |
| 114 | |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 115 | private ScheduledExecutorService executor; |
Thomas Vachuska | 7cb9239 | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 116 | private ExecutorService messageHandlingExecutor, activationExecutor; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 117 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 118 | private ConsistentMap<ApplicationId, InternalApplicationHolder> apps; |
Madan Jampani | c5a1754 | 2016-08-22 10:26:11 -0700 | [diff] [blame] | 119 | private Topic<Application> appActivationTopic; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | protected ClusterCommunicationService clusterCommunicator; |
| 123 | |
| 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected ClusterService clusterService; |
| 126 | |
| 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 128 | protected StorageService storageService; |
| 129 | |
| 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 131 | protected ApplicationIdStore idStore; |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 132 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 133 | private final InternalAppsListener appsListener = new InternalAppsListener(); |
Madan Jampani | c5a1754 | 2016-08-22 10:26:11 -0700 | [diff] [blame] | 134 | private final Consumer<Application> appActivator = new AppActivator(); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 135 | |
| 136 | private Consumer<Status> statusChangeListener; |
| 137 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 138 | // Multimap to track which apps are required by others apps |
| 139 | // app -> { required-by, ... } |
| 140 | // Apps explicitly activated will be required by the CORE app |
| 141 | private final Multimap<ApplicationId, ApplicationId> requiredBy = |
| 142 | synchronizedSetMultimap(newSetMultimap(Maps.newHashMap(), Sets::newHashSet)); |
| 143 | |
| 144 | private ApplicationId coreAppId; |
| 145 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 146 | @Activate |
| 147 | public void activate() { |
Thomas Vachuska | 7cb9239 | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 148 | messageHandlingExecutor = newSingleThreadExecutor(groupedThreads("onos/store/app", |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 149 | "message-handler", log)); |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 150 | clusterCommunicator.addSubscriber(APP_BITS_REQUEST, |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 151 | bytes -> new String(bytes, Charsets.UTF_8), |
| 152 | name -> { |
| 153 | try { |
| 154 | log.info("Sending bits for application {}", name); |
| 155 | return toByteArray(getApplicationInputStream(name)); |
| 156 | } catch (IOException e) { |
| 157 | throw new StorageException(e); |
| 158 | } |
| 159 | }, |
| 160 | Function.identity(), |
| 161 | messageHandlingExecutor); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 162 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 163 | apps = storageService.<ApplicationId, InternalApplicationHolder>consistentMapBuilder() |
| 164 | .withName("onos-apps") |
| 165 | .withRelaxedReadConsistency() |
| 166 | .withSerializer(Serializer.using(KryoNamespaces.API, |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 167 | InternalApplicationHolder.class, |
| 168 | InternalState.class)) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 169 | .build(); |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 170 | |
Madan Jampani | c5a1754 | 2016-08-22 10:26:11 -0700 | [diff] [blame] | 171 | appActivationTopic = storageService.getTopic("onos-apps-activation-topic", |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 172 | Serializer.using(KryoNamespaces.API)); |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame] | 173 | |
Thomas Vachuska | 7cb9239 | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 174 | activationExecutor = newSingleThreadExecutor(groupedThreads("onos/store/app", |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 175 | "app-activation", log)); |
Thomas Vachuska | 7cb9239 | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 176 | appActivationTopic.subscribe(appActivator, activationExecutor); |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame] | 177 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 178 | executor = newSingleThreadScheduledExecutor(groupedThreads("onos/app", "store", log)); |
| 179 | statusChangeListener = status -> { |
| 180 | if (status == Status.ACTIVE) { |
| 181 | executor.execute(this::bootstrapExistingApplications); |
| 182 | } |
| 183 | }; |
Thomas Vachuska | 7cb9239 | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 184 | apps.addListener(appsListener, activationExecutor); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 185 | apps.addStatusChangeListener(statusChangeListener); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 186 | coreAppId = getId(CoreService.CORE_APP_NAME); |
Jordan Halterman | ffceb12 | 2018-01-09 13:02:45 -0800 | [diff] [blame^] | 187 | |
| 188 | activateExistingApplications(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 189 | log.info("Started"); |
| 190 | } |
| 191 | |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 192 | /** |
Jordan Halterman | ffceb12 | 2018-01-09 13:02:45 -0800 | [diff] [blame^] | 193 | * Activates applications locally on startup. |
| 194 | */ |
| 195 | private void activateExistingApplications() { |
| 196 | getApplicationNames().forEach(appName -> { |
| 197 | // Only update the application state if the application has already been installed. |
| 198 | ApplicationId appId = getId(appName); |
| 199 | if (appId != null) { |
| 200 | Application application = getApplication(appId); |
| 201 | if (application != null) { |
| 202 | InternalApplicationHolder appHolder = Versioned.valueOrNull(apps.get(application.id())); |
| 203 | if (appHolder != null && appHolder.state == ACTIVATED && !isActive(appName)) { |
| 204 | setActive(appName); |
| 205 | updateTime(appName); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | }); |
| 210 | } |
| 211 | |
| 212 | /** |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 213 | * Processes existing applications from the distributed map. This is done to |
| 214 | * account for events that this instance may be have missed due to a staggered start. |
| 215 | */ |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 216 | private void bootstrapExistingApplications() { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 217 | apps.asJavaMap().forEach((appId, holder) -> setupApplicationAndNotify(appId, holder.app(), holder.state())); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 221 | * Loads the application inventory from the disk and activates apps if |
| 222 | * they are marked to be active. |
| 223 | */ |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 224 | private void loadFromDisk() { |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 225 | getApplicationNames().forEach(appName -> { |
| 226 | Application app = loadFromDisk(appName); |
| 227 | if (app != null && isActive(app.id().name())) { |
| 228 | activate(app.id(), false); |
| 229 | // TODO Load app permissions |
| 230 | } |
| 231 | }); |
| 232 | } |
| 233 | |
| 234 | private Application loadFromDisk(String appName) { |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 235 | pendingApps.add(appName); |
| 236 | |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 237 | for (int i = 0; i < MAX_LOAD_RETRIES; i++) { |
| 238 | try { |
| 239 | // Directly return if app already exists |
| 240 | ApplicationId appId = getId(appName); |
| 241 | if (appId != null) { |
Ray Milkey | 64591a6 | 2016-01-12 09:50:45 -0800 | [diff] [blame] | 242 | Application application = getApplication(appId); |
| 243 | if (application != null) { |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 244 | pendingApps.remove(appName); |
Ray Milkey | 64591a6 | 2016-01-12 09:50:45 -0800 | [diff] [blame] | 245 | return application; |
| 246 | } |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 247 | } |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 248 | |
| 249 | ApplicationDescription appDesc = getApplicationDescription(appName); |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 250 | |
| 251 | Optional<String> loop = appDesc.requiredApps().stream() |
| 252 | .filter(app -> pendingApps.contains(app)).findAny(); |
| 253 | if (loop.isPresent()) { |
| 254 | log.error("Circular app dependency detected: {} -> {}", pendingApps, loop.get()); |
| 255 | pendingApps.remove(appName); |
| 256 | return null; |
| 257 | } |
| 258 | |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 259 | boolean success = appDesc.requiredApps().stream() |
| 260 | .noneMatch(requiredApp -> loadFromDisk(requiredApp) == null); |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 261 | pendingApps.remove(appName); |
| 262 | |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 263 | return success ? create(appDesc, false) : null; |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 264 | |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 265 | } catch (Exception e) { |
| 266 | log.warn("Unable to load application {} from disk; retrying", appName); |
| 267 | 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] | 268 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 269 | } |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 270 | pendingApps.remove(appName); |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 271 | return null; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | @Deactivate |
| 275 | public void deactivate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 276 | clusterCommunicator.removeSubscriber(APP_BITS_REQUEST); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 277 | apps.removeStatusChangeListener(statusChangeListener); |
| 278 | apps.removeListener(appsListener); |
Madan Jampani | c5a1754 | 2016-08-22 10:26:11 -0700 | [diff] [blame] | 279 | appActivationTopic.unsubscribe(appActivator); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 280 | messageHandlingExecutor.shutdown(); |
Thomas Vachuska | 7cb9239 | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 281 | activationExecutor.shutdown(); |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 282 | executor.shutdown(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 283 | log.info("Stopped"); |
| 284 | } |
| 285 | |
| 286 | @Override |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 287 | public void setDelegate(ApplicationStoreDelegate delegate) { |
| 288 | super.setDelegate(delegate); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 289 | executor.execute(this::bootstrapExistingApplications); |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 290 | executor.schedule((Runnable) this::loadFromDisk, APP_LOAD_DELAY_MS, TimeUnit.MILLISECONDS); |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | @Override |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 294 | public Set<Application> getApplications() { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 295 | return ImmutableSet.copyOf(apps.values() |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 296 | .stream() |
| 297 | .map(Versioned::value) |
| 298 | .map(InternalApplicationHolder::app) |
| 299 | .collect(Collectors.toSet())); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public ApplicationId getId(String name) { |
| 304 | return idStore.getAppId(name); |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public Application getApplication(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 309 | InternalApplicationHolder appHolder = Versioned.valueOrNull(apps.get(appId)); |
| 310 | return appHolder != null ? appHolder.app() : null; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public ApplicationState getState(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 315 | InternalApplicationHolder appHolder = Versioned.valueOrNull(apps.get(appId)); |
| 316 | InternalState state = appHolder != null ? appHolder.state() : null; |
| 317 | return state == null ? null : state == ACTIVATED ? ApplicationState.ACTIVE : ApplicationState.INSTALLED; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public Application create(InputStream appDescStream) { |
| 322 | ApplicationDescription appDesc = saveApplication(appDescStream); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 323 | if (hasPrerequisites(appDesc)) { |
| 324 | return create(appDesc, true); |
| 325 | } |
Jonathan Hart | 14651b5 | 2016-06-07 17:59:53 -0700 | [diff] [blame] | 326 | // Purge bits off disk if we don't have prerequisites to allow app to be |
| 327 | // reinstalled later |
| 328 | purgeApplication(appDesc.name()); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 329 | throw new ApplicationException("Missing dependencies for app " + appDesc.name()); |
| 330 | } |
| 331 | |
| 332 | private boolean hasPrerequisites(ApplicationDescription app) { |
Yuta HIGUCHI | 56a7a6b | 2017-02-22 15:17:42 -0800 | [diff] [blame] | 333 | for (String required : app.requiredApps()) { |
| 334 | ApplicationId id = getId(required); |
| 335 | if (id == null || getApplication(id) == null) { |
| 336 | log.error("{} required for {} not available", required, app.name()); |
| 337 | return false; |
| 338 | } |
| 339 | } |
| 340 | return true; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 343 | private Application create(ApplicationDescription appDesc, boolean updateTime) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 344 | Application app = registerApp(appDesc); |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 345 | if (updateTime) { |
| 346 | updateTime(app.id().name()); |
| 347 | } |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 348 | InternalApplicationHolder previousApp = |
| 349 | Versioned.valueOrNull(apps.putIfAbsent(app.id(), new InternalApplicationHolder(app, INSTALLED, null))); |
| 350 | return previousApp != null ? previousApp.app() : app; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public void remove(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 355 | uninstallDependentApps(appId); |
| 356 | apps.remove(appId); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 359 | // Uninstalls all apps that depend on the given app. |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 360 | private void uninstallDependentApps(ApplicationId appId) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 361 | getApplications().stream() |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 362 | .filter(a -> a.requiredApps().contains(appId.name())) |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 363 | .forEach(a -> remove(a.id())); |
| 364 | } |
| 365 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 366 | @Override |
| 367 | public void activate(ApplicationId appId) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 368 | activate(appId, coreAppId); |
| 369 | } |
| 370 | |
| 371 | private void activate(ApplicationId appId, ApplicationId forAppId) { |
| 372 | requiredBy.put(appId, forAppId); |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 373 | activate(appId, true); |
| 374 | } |
| 375 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 376 | |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 377 | private void activate(ApplicationId appId, boolean updateTime) { |
Jonathan Hart | bd85f78 | 2016-06-20 16:13:37 -0700 | [diff] [blame] | 378 | Versioned<InternalApplicationHolder> vAppHolder = apps.get(appId); |
| 379 | if (vAppHolder != null) { |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 380 | if (updateTime) { |
| 381 | updateTime(appId.name()); |
| 382 | } |
Jonathan Hart | bd85f78 | 2016-06-20 16:13:37 -0700 | [diff] [blame] | 383 | activateRequiredApps(vAppHolder.value().app()); |
| 384 | |
| 385 | apps.computeIf(appId, v -> v != null && v.state() != ACTIVATED, |
| 386 | (k, v) -> new InternalApplicationHolder( |
| 387 | v.app(), ACTIVATED, v.permissions())); |
Madan Jampani | c5a1754 | 2016-08-22 10:26:11 -0700 | [diff] [blame] | 388 | appActivationTopic.publish(vAppHolder.value().app()); |
Thomas Vachuska | 6b826f4 | 2017-08-29 15:21:57 -0700 | [diff] [blame] | 389 | appActivationTopic.publish(null); // FIXME: Once ONOS-6977 is fixed |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 393 | // Activates all apps required by this application. |
| 394 | private void activateRequiredApps(Application app) { |
| 395 | app.requiredApps().stream().map(this::getId).forEach(id -> activate(id, app.id())); |
| 396 | } |
| 397 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 398 | @Override |
| 399 | public void deactivate(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 400 | deactivateDependentApps(appId); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 401 | deactivate(appId, coreAppId); |
| 402 | } |
| 403 | |
| 404 | private void deactivate(ApplicationId appId, ApplicationId forAppId) { |
| 405 | requiredBy.remove(appId, forAppId); |
| 406 | if (requiredBy.get(appId).isEmpty()) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 407 | AtomicBoolean stateChanged = new AtomicBoolean(false); |
| 408 | apps.computeIf(appId, |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 409 | v -> v != null && v.state() != DEACTIVATED, |
| 410 | (k, v) -> { |
| 411 | stateChanged.set(true); |
| 412 | return new InternalApplicationHolder(v.app(), DEACTIVATED, v.permissions()); |
| 413 | }); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 414 | if (stateChanged.get()) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 415 | updateTime(appId.name()); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 416 | deactivateRequiredApps(appId); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 417 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 421 | // Deactivates all apps that require this application. |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 422 | private void deactivateDependentApps(ApplicationId appId) { |
| 423 | apps.values() |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 424 | .stream() |
| 425 | .map(Versioned::value) |
| 426 | .filter(a -> a.state() == ACTIVATED) |
| 427 | .filter(a -> a.app().requiredApps().contains(appId.name())) |
| 428 | .forEach(a -> deactivate(a.app().id())); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | // Deactivates all apps required by this application. |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 432 | private void deactivateRequiredApps(ApplicationId appId) { |
| 433 | getApplication(appId).requiredApps() |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 434 | .stream() |
| 435 | .map(this::getId) |
| 436 | .map(apps::get) |
| 437 | .map(Versioned::value) |
| 438 | .filter(a -> a.state() == ACTIVATED) |
| 439 | .forEach(a -> deactivate(a.app().id(), appId)); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 442 | @Override |
| 443 | public Set<Permission> getPermissions(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 444 | InternalApplicationHolder app = Versioned.valueOrNull(apps.get(appId)); |
| 445 | return app != null ? ImmutableSet.copyOf(app.permissions()) : ImmutableSet.of(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | @Override |
| 449 | public void setPermissions(ApplicationId appId, Set<Permission> permissions) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 450 | AtomicBoolean permissionsChanged = new AtomicBoolean(false); |
| 451 | Versioned<InternalApplicationHolder> appHolder = apps.computeIf(appId, |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 452 | v -> v != null && !Sets.symmetricDifference(v.permissions(), permissions).isEmpty(), |
| 453 | (k, v) -> { |
| 454 | permissionsChanged.set(true); |
| 455 | return new InternalApplicationHolder(v.app(), v.state(), ImmutableSet.copyOf(permissions)); |
| 456 | }); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 457 | if (permissionsChanged.get()) { |
Ray Milkey | 2f6f907 | 2016-08-23 13:18:50 -0700 | [diff] [blame] | 458 | notifyDelegate(new ApplicationEvent(APP_PERMISSIONS_CHANGED, appHolder.value().app())); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Madan Jampani | c5a1754 | 2016-08-22 10:26:11 -0700 | [diff] [blame] | 462 | private class AppActivator implements Consumer<Application> { |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame] | 463 | @Override |
Madan Jampani | c5a1754 | 2016-08-22 10:26:11 -0700 | [diff] [blame] | 464 | public void accept(Application app) { |
Thomas Vachuska | 6b826f4 | 2017-08-29 15:21:57 -0700 | [diff] [blame] | 465 | if (app != null) { // FIXME: Once ONOS-6977 is fixed |
| 466 | String appName = app.id().name(); |
| 467 | installAppIfNeeded(app); |
| 468 | setActive(appName); |
| 469 | notifyDelegate(new ApplicationEvent(APP_ACTIVATED, app)); |
| 470 | } |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 474 | /** |
| 475 | * Listener to application state distributed map changes. |
| 476 | */ |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 477 | private final class InternalAppsListener |
| 478 | implements MapEventListener<ApplicationId, InternalApplicationHolder> { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 479 | @Override |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 480 | public void event(MapEvent<ApplicationId, InternalApplicationHolder> event) { |
Thomas Vachuska | 4fcdae7 | 2015-03-24 10:22:54 -0700 | [diff] [blame] | 481 | if (delegate == null) { |
| 482 | return; |
| 483 | } |
| 484 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 485 | ApplicationId appId = event.key(); |
| 486 | InternalApplicationHolder newApp = event.newValue() == null ? null : event.newValue().value(); |
| 487 | InternalApplicationHolder oldApp = event.oldValue() == null ? null : event.oldValue().value(); |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 488 | if (event.type() == MapEvent.Type.UPDATE && (newApp == null || oldApp == null || |
| 489 | newApp.state() == oldApp.state())) { |
| 490 | log.warn("Can't update the application {}", event.key()); |
Ray Milkey | fd4f8d3 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 491 | return; |
| 492 | } |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 493 | if ((event.type() == MapEvent.Type.INSERT || event.type() == MapEvent.Type.UPDATE) && newApp != null) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 494 | setupApplicationAndNotify(appId, newApp.app(), newApp.state()); |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 495 | } else if (event.type() == MapEvent.Type.REMOVE && oldApp != null) { |
Ray Milkey | 2f6f907 | 2016-08-23 13:18:50 -0700 | [diff] [blame] | 496 | notifyDelegate(new ApplicationEvent(APP_UNINSTALLED, oldApp.app())); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 497 | purgeApplication(appId.name()); |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 498 | } else { |
| 499 | log.warn("Can't perform {} on application {}", event.type(), event.key()); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 504 | private void setupApplicationAndNotify(ApplicationId appId, Application app, InternalState state) { |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame] | 505 | // ACTIVATED state is handled separately in NextAppToActivateValueListener |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 506 | if (state == INSTALLED) { |
| 507 | fetchBitsIfNeeded(app); |
Ray Milkey | 2f6f907 | 2016-08-23 13:18:50 -0700 | [diff] [blame] | 508 | notifyDelegate(new ApplicationEvent(APP_INSTALLED, app)); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 509 | } else if (state == DEACTIVATED) { |
| 510 | clearActive(appId.name()); |
Ray Milkey | 2f6f907 | 2016-08-23 13:18:50 -0700 | [diff] [blame] | 511 | notifyDelegate(new ApplicationEvent(APP_DEACTIVATED, app)); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 515 | /** |
| 516 | * Determines if the application bits are available locally. |
| 517 | */ |
| 518 | private boolean appBitsAvailable(Application app) { |
| 519 | try { |
| 520 | ApplicationDescription appDesc = getApplicationDescription(app.id().name()); |
| 521 | return appDesc.version().equals(app.version()); |
| 522 | } catch (ApplicationException e) { |
| 523 | return false; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Fetches the bits from the cluster peers if necessary. |
| 529 | */ |
| 530 | private void fetchBitsIfNeeded(Application app) { |
| 531 | if (!appBitsAvailable(app)) { |
Thomas Vachuska | 7cb9239 | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 532 | fetchBits(app, false); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Installs the application if necessary from the application peers. |
| 538 | */ |
| 539 | private void installAppIfNeeded(Application app) { |
| 540 | if (!appBitsAvailable(app)) { |
Thomas Vachuska | 7cb9239 | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 541 | fetchBits(app, true); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Fetches the bits from the cluster peers. |
| 547 | */ |
Thomas Vachuska | 7cb9239 | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 548 | private void fetchBits(Application app, boolean delegateInstallation) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 549 | ControllerNode localNode = clusterService.getLocalNode(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 550 | CountDownLatch latch = new CountDownLatch(1); |
| 551 | |
| 552 | // FIXME: send message with name & version to make sure we don't get served old bits |
| 553 | |
| 554 | log.info("Downloading bits for application {}", app.id().name()); |
| 555 | for (ControllerNode node : clusterService.getNodes()) { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 556 | if (latch.getCount() == 0) { |
| 557 | break; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 558 | } |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 559 | if (node.equals(localNode)) { |
| 560 | continue; |
| 561 | } |
| 562 | clusterCommunicator.sendAndReceive(app.id().name(), |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 563 | APP_BITS_REQUEST, |
| 564 | s -> s.getBytes(Charsets.UTF_8), |
| 565 | Function.identity(), |
| 566 | node.id()) |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 567 | .whenCompleteAsync((bits, error) -> { |
| 568 | if (error == null && latch.getCount() > 0) { |
| 569 | saveApplication(new ByteArrayInputStream(bits)); |
| 570 | log.info("Downloaded bits for application {} from node {}", |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 571 | app.id().name(), node.id()); |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 572 | latch.countDown(); |
Thomas Vachuska | 7cb9239 | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 573 | if (delegateInstallation) { |
| 574 | notifyDelegate(new ApplicationEvent(APP_INSTALLED, app)); |
| 575 | } |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 576 | } else if (error != null) { |
| 577 | log.warn("Unable to fetch bits for application {} from node {}", |
Andrea Campanella | 6bf308c | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 578 | app.id().name(), node.id()); |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 579 | } |
| 580 | }, executor); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | try { |
| 584 | if (!latch.await(FETCH_TIMEOUT_MS, MILLISECONDS)) { |
| 585 | log.warn("Unable to fetch bits for application {}", app.id().name()); |
| 586 | } |
| 587 | } catch (InterruptedException e) { |
| 588 | log.warn("Interrupted while fetching bits for application {}", app.id().name()); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /** |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 593 | * Produces a registered application from the supplied description. |
| 594 | */ |
| 595 | private Application registerApp(ApplicationDescription appDesc) { |
| 596 | ApplicationId appId = idStore.registerApplication(appDesc.name()); |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 597 | return new DefaultApplication(appId, |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 598 | appDesc.version(), |
| 599 | appDesc.title(), |
| 600 | appDesc.description(), |
| 601 | appDesc.origin(), |
| 602 | appDesc.category(), |
| 603 | appDesc.url(), |
| 604 | appDesc.readme(), |
| 605 | appDesc.icon(), |
| 606 | appDesc.role(), |
| 607 | appDesc.permissions(), |
| 608 | appDesc.featuresRepo(), |
| 609 | appDesc.features(), |
| 610 | appDesc.requiredApps()); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 611 | } |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 612 | |
| 613 | /** |
| 614 | * Internal class for holding app information. |
| 615 | */ |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 616 | private static final class InternalApplicationHolder { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 617 | private final Application app; |
| 618 | private final InternalState state; |
| 619 | private final Set<Permission> permissions; |
| 620 | |
| 621 | @SuppressWarnings("unused") |
| 622 | private InternalApplicationHolder() { |
| 623 | app = null; |
| 624 | state = null; |
| 625 | permissions = null; |
| 626 | } |
| 627 | |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 628 | private InternalApplicationHolder(Application app, InternalState state, Set<Permission> permissions) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 629 | this.app = Preconditions.checkNotNull(app); |
| 630 | this.state = state; |
| 631 | this.permissions = permissions == null ? null : ImmutableSet.copyOf(permissions); |
| 632 | } |
| 633 | |
| 634 | public Application app() { |
| 635 | return app; |
| 636 | } |
| 637 | |
| 638 | public InternalState state() { |
| 639 | return state; |
| 640 | } |
| 641 | |
| 642 | public Set<Permission> permissions() { |
| 643 | return permissions; |
| 644 | } |
| 645 | |
| 646 | @Override |
| 647 | public String toString() { |
| 648 | return MoreObjects.toStringHelper(getClass()) |
| 649 | .add("app", app.id()) |
| 650 | .add("state", state) |
| 651 | .toString(); |
| 652 | } |
| 653 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 654 | } |