Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -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 | 7a8de84 | 2016-03-07 20:56:35 -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 | |
| 17 | package org.onosproject.cluster.impl; |
| 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.osgi.service.component.runtime.ServiceComponentRuntime; |
| 20 | import org.osgi.service.component.annotations.Activate; |
| 21 | import org.osgi.service.component.annotations.Component; |
| 22 | import org.osgi.service.component.annotations.Deactivate; |
| 23 | import org.osgi.service.component.annotations.Reference; |
| 24 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 25 | import org.apache.karaf.features.Feature; |
| 26 | import org.apache.karaf.features.FeaturesService; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 27 | import org.onosproject.cluster.ClusterAdminService; |
| 28 | import org.osgi.framework.Bundle; |
| 29 | import org.osgi.framework.BundleContext; |
| 30 | import org.osgi.service.component.ComponentContext; |
Jordan Halterman | dfc4855 | 2018-11-02 11:08:30 -0700 | [diff] [blame] | 31 | import org.osgi.service.component.runtime.dto.ComponentConfigurationDTO; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 32 | import org.osgi.service.component.runtime.dto.ComponentDescriptionDTO; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
Jordan Halterman | af3461c | 2019-01-07 10:03:46 -0800 | [diff] [blame] | 36 | import java.util.concurrent.Executors; |
| 37 | import java.util.concurrent.ScheduledExecutorService; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 38 | import java.util.concurrent.ScheduledFuture; |
| 39 | import java.util.concurrent.TimeUnit; |
| 40 | |
Jordan Halterman | af3461c | 2019-01-07 10:03:46 -0800 | [diff] [blame] | 41 | import static org.onlab.util.Tools.groupedThreads; |
| 42 | |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Monitors the system to make sure that all bundles and their components |
| 45 | * are properly activated and keeps the cluster node service appropriately |
| 46 | * updated. |
| 47 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | @Component(immediate = true) |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 49 | public class ComponentsMonitor { |
| 50 | |
| 51 | private Logger log = LoggerFactory.getLogger(getClass()); |
| 52 | |
Jordan Halterman | 86fdd28 | 2019-01-07 10:25:32 -0800 | [diff] [blame] | 53 | private static final long STARTUP_PERIOD = 2500; |
| 54 | private static final long ACTIVE_PERIOD = 60000; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 55 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 57 | protected FeaturesService featuresService; |
| 58 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 60 | protected ServiceComponentRuntime scrService; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 61 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 63 | protected ClusterAdminService clusterAdminService; |
| 64 | |
| 65 | private BundleContext bundleContext; |
Jordan Halterman | af3461c | 2019-01-07 10:03:46 -0800 | [diff] [blame] | 66 | |
| 67 | private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor( |
| 68 | groupedThreads("components-monitor", "%d", log)); |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 69 | private ScheduledFuture<?> poller; |
Jordan Halterman | 86fdd28 | 2019-01-07 10:25:32 -0800 | [diff] [blame] | 70 | private boolean pollerBackedOff; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 71 | |
| 72 | @Activate |
| 73 | protected void activate(ComponentContext context) { |
| 74 | bundleContext = context.getBundleContext(); |
Jordan Halterman | 86fdd28 | 2019-01-07 10:25:32 -0800 | [diff] [blame] | 75 | poller = executor.scheduleAtFixedRate( |
| 76 | this::checkStartedState, STARTUP_PERIOD, STARTUP_PERIOD, TimeUnit.MILLISECONDS); |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 77 | log.info("Started"); |
| 78 | } |
| 79 | |
| 80 | @Deactivate |
| 81 | protected void deactivate() { |
| 82 | poller.cancel(false); |
Jordan Halterman | af3461c | 2019-01-07 10:03:46 -0800 | [diff] [blame] | 83 | executor.shutdownNow(); |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 84 | log.info("Stopped"); |
| 85 | } |
| 86 | |
Jordan Halterman | 86fdd28 | 2019-01-07 10:25:32 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Increases the rate at which {@link #checkStartedState()} is called once the node has been fully started. |
| 89 | */ |
| 90 | private void backoffPoller() { |
| 91 | if (!pollerBackedOff) { |
| 92 | poller.cancel(false); |
| 93 | poller = executor.scheduleAtFixedRate( |
| 94 | this::checkStartedState, ACTIVE_PERIOD, ACTIVE_PERIOD, TimeUnit.MILLISECONDS); |
| 95 | pollerBackedOff = true; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Decreases the rate at which {@link #checkStartedState()} is called when the node becomes unready. |
| 101 | */ |
| 102 | private void revertPoller() { |
| 103 | if (pollerBackedOff) { |
| 104 | poller.cancel(false); |
| 105 | poller = executor.scheduleAtFixedRate( |
| 106 | this::checkStartedState, STARTUP_PERIOD, STARTUP_PERIOD, TimeUnit.MILLISECONDS); |
| 107 | pollerBackedOff = false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Checks whether all components are active and marks the node READY if so. |
| 113 | */ |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 114 | private void checkStartedState() { |
Jordan Halterman | 86fdd28 | 2019-01-07 10:25:32 -0800 | [diff] [blame] | 115 | boolean isFullyStarted = isFullyStarted(); |
| 116 | clusterAdminService.markFullyStarted(isFullyStarted); |
| 117 | |
| 118 | // If the node is fully started, decrease the rate at which we poll component states. |
| 119 | // Otherwise, increase the rate at which we poll component states until the node becomes ready. |
| 120 | if (isFullyStarted) { |
| 121 | backoffPoller(); |
| 122 | } else { |
| 123 | revertPoller(); |
| 124 | } |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Scans the system to make sure that all bundles and their components |
| 129 | * are fully started. |
| 130 | * |
| 131 | * @return true if all bundles and their components are active |
| 132 | */ |
| 133 | private boolean isFullyStarted() { |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 134 | try { |
| 135 | for (Feature feature : featuresService.listInstalledFeatures()) { |
| 136 | if (!isFullyStarted(feature)) { |
| 137 | return false; |
| 138 | } |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 139 | } |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 140 | return true; |
| 141 | } catch (Exception ex) { |
| 142 | return false; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 143 | } |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Ray Milkey | 9ee53ef | 2019-01-07 13:02:38 -0800 | [diff] [blame] | 146 | private boolean needToCheck(Feature feature) { |
| 147 | // We only need to check core ONOS features, not external ones. |
| 148 | return feature.getId().startsWith("onos-") && |
| 149 | !feature.getId().contains("thirdparty"); |
| 150 | } |
| 151 | |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 152 | private boolean isFullyStarted(Feature feature) { |
Ray Milkey | 9ee53ef | 2019-01-07 13:02:38 -0800 | [diff] [blame] | 153 | if (needToCheck(feature)) { |
| 154 | try { |
| 155 | return feature.getBundles().stream() |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 156 | .map(info -> bundleContext.getBundle()) |
Ray Milkey | f13feb8 | 2016-08-30 10:38:59 -0700 | [diff] [blame] | 157 | .allMatch(this::isFullyStarted); |
Ray Milkey | 9ee53ef | 2019-01-07 13:02:38 -0800 | [diff] [blame] | 158 | } catch (NullPointerException npe) { |
| 159 | // FIXME: Remove this catch block when Felix fixes the bug |
| 160 | // Due to a bug in the Felix implementation, this can throw an NPE. |
| 161 | // Catch the error and do something sensible with it. |
| 162 | return false; |
| 163 | } |
| 164 | } else { |
| 165 | return true; |
Ray Milkey | f13feb8 | 2016-08-30 10:38:59 -0700 | [diff] [blame] | 166 | } |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | private boolean isFullyStarted(Bundle bundle) { |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 170 | for (ComponentDescriptionDTO component : scrService.getComponentDescriptionDTOs(bundle)) { |
Jordan Halterman | dfc4855 | 2018-11-02 11:08:30 -0700 | [diff] [blame] | 171 | if (scrService.isComponentEnabled(component)) { |
| 172 | for (ComponentConfigurationDTO config : scrService.getComponentConfigurationDTOs(component)) { |
| 173 | if (config.state != ComponentConfigurationDTO.ACTIVE) { |
| 174 | return false; |
| 175 | } |
| 176 | } |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | return true; |
| 180 | } |
| 181 | |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 182 | } |