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