Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [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 | |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 17 | package org.onosproject.routeservice.impl; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 18 | |
| 19 | import org.onosproject.cluster.ClusterEvent; |
| 20 | import org.onosproject.cluster.ClusterEventListener; |
| 21 | import org.onosproject.cluster.ClusterService; |
Charles Chan | 0cc4450 | 2018-01-29 15:25:52 -0800 | [diff] [blame] | 22 | import org.onosproject.cluster.ControllerNode; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 23 | import org.onosproject.cluster.NodeId; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 24 | import org.onosproject.routeservice.ResolvedRoute; |
| 25 | import org.onosproject.routeservice.Route; |
| 26 | import org.onosproject.routeservice.RouteAdminService; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 27 | import org.onosproject.store.serializers.KryoNamespaces; |
Charles Chan | 0cc4450 | 2018-01-29 15:25:52 -0800 | [diff] [blame] | 28 | import org.onosproject.store.service.AsyncDistributedLock; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 29 | import org.onosproject.store.service.DistributedPrimitive; |
| 30 | import org.onosproject.store.service.Serializer; |
| 31 | import org.onosproject.store.service.StorageService; |
| 32 | import org.onosproject.store.service.WorkQueue; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
Charles Chan | 0cc4450 | 2018-01-29 15:25:52 -0800 | [diff] [blame] | 36 | import java.time.Duration; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 37 | import java.util.Collection; |
| 38 | import java.util.concurrent.ScheduledExecutorService; |
| 39 | import java.util.stream.Collectors; |
| 40 | |
| 41 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 42 | import static org.onlab.util.Tools.groupedThreads; |
| 43 | |
| 44 | /** |
| 45 | * Monitors cluster nodes and removes routes if a cluster node becomes unavailable. |
| 46 | */ |
| 47 | public class RouteMonitor { |
| 48 | |
| 49 | private final Logger log = LoggerFactory.getLogger(this.getClass()); |
| 50 | |
| 51 | private static final String TOPIC = "route-reaper"; |
Charles Chan | 0cc4450 | 2018-01-29 15:25:52 -0800 | [diff] [blame] | 52 | private static final String LOCK_NAME = "route-monitor-lock"; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 53 | private static final int NUM_PARALLEL_JOBS = 10; |
| 54 | |
| 55 | private RouteAdminService routeService; |
| 56 | private final ClusterService clusterService; |
| 57 | private StorageService storageService; |
| 58 | |
Charles Chan | 0cc4450 | 2018-01-29 15:25:52 -0800 | [diff] [blame] | 59 | private final AsyncDistributedLock asyncLock; |
| 60 | |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 61 | private WorkQueue<NodeId> queue; |
| 62 | |
| 63 | private final InternalClusterListener clusterListener = new InternalClusterListener(); |
| 64 | |
| 65 | private final ScheduledExecutorService reaperExecutor = |
| 66 | newSingleThreadScheduledExecutor(groupedThreads("route/reaper", "", log)); |
| 67 | |
| 68 | /** |
| 69 | * Creates a new route monitor. |
| 70 | * |
| 71 | * @param routeService route service |
| 72 | * @param clusterService cluster service |
| 73 | * @param storageService storage service |
| 74 | */ |
| 75 | public RouteMonitor(RouteAdminService routeService, |
| 76 | ClusterService clusterService, StorageService storageService) { |
| 77 | this.routeService = routeService; |
| 78 | this.clusterService = clusterService; |
| 79 | this.storageService = storageService; |
| 80 | |
Charles Chan | 0cc4450 | 2018-01-29 15:25:52 -0800 | [diff] [blame] | 81 | asyncLock = storageService.lockBuilder().withName(LOCK_NAME).build(); |
| 82 | |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 83 | clusterService.addListener(clusterListener); |
| 84 | |
| 85 | queue = storageService.getWorkQueue(TOPIC, Serializer.using(KryoNamespaces.API)); |
| 86 | queue.addStatusChangeListener(this::statusChange); |
| 87 | |
| 88 | startProcessing(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Shuts down the route monitor. |
| 93 | */ |
| 94 | public void shutdown() { |
| 95 | stopProcessing(); |
| 96 | clusterService.removeListener(clusterListener); |
Charles Chan | 0cc4450 | 2018-01-29 15:25:52 -0800 | [diff] [blame] | 97 | asyncLock.unlock(); |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | private void statusChange(DistributedPrimitive.Status status) { |
| 101 | switch (status) { |
| 102 | case ACTIVE: |
| 103 | startProcessing(); |
| 104 | break; |
| 105 | case SUSPENDED: |
| 106 | stopProcessing(); |
| 107 | break; |
| 108 | case INACTIVE: |
| 109 | default: |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private void startProcessing() { |
| 115 | queue.registerTaskProcessor(this::cleanRoutes, NUM_PARALLEL_JOBS, reaperExecutor); |
| 116 | } |
| 117 | |
| 118 | private void stopProcessing() { |
| 119 | queue.stopProcessing(); |
| 120 | } |
| 121 | |
| 122 | private void cleanRoutes(NodeId node) { |
| 123 | log.info("Cleaning routes from unavailable node {}", node); |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 124 | Collection<Route> routes = routeService.getRouteTables().stream() |
| 125 | .flatMap(id -> routeService.getRoutes(id).stream()) |
| 126 | .flatMap(route -> route.allRoutes().stream()) |
| 127 | .map(ResolvedRoute::route) |
| 128 | .filter(r -> r.sourceNode().equals(node)) |
| 129 | .collect(Collectors.toList()); |
Charles Chan | 0cc4450 | 2018-01-29 15:25:52 -0800 | [diff] [blame] | 130 | if (node.equals(clusterService.getLocalNode().id())) { |
| 131 | log.debug("Do not remove routes from local nodes {}", node); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if (clusterService.getState(node) == ControllerNode.State.READY) { |
| 136 | log.debug("Do not remove routes from active nodes {}", node); |
| 137 | return; |
| 138 | } |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 139 | |
| 140 | log.debug("Withdrawing routes: {}", routes); |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 141 | routeService.withdraw(routes); |
| 142 | } |
| 143 | |
| 144 | private class InternalClusterListener implements ClusterEventListener { |
| 145 | |
| 146 | @Override |
| 147 | public void event(ClusterEvent event) { |
| 148 | switch (event.type()) { |
| 149 | case INSTANCE_DEACTIVATED: |
| 150 | NodeId id = event.subject().id(); |
| 151 | log.info("Node {} deactivated", id); |
Charles Chan | 0cc4450 | 2018-01-29 15:25:52 -0800 | [diff] [blame] | 152 | |
| 153 | // DistributedLock is introduced to guarantee that minority nodes won't try to remove |
| 154 | // routes that originated from majority nodes. |
| 155 | // Adding 15 seconds retry for the leadership election to be completed. |
| 156 | asyncLock.tryLock(Duration.ofSeconds(15)).whenComplete((result, error) -> { |
| 157 | if (result != null && result.isPresent()) { |
| 158 | log.debug("Lock obtained. Put {} into removal queue", id); |
| 159 | queue.addOne(id); |
| 160 | asyncLock.unlock(); |
| 161 | } else { |
| 162 | log.debug("Fail to obtain lock. Do not remove routes from {}", id); |
| 163 | } |
| 164 | }); |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 165 | break; |
| 166 | case INSTANCE_ADDED: |
| 167 | case INSTANCE_REMOVED: |
| 168 | case INSTANCE_ACTIVATED: |
| 169 | case INSTANCE_READY: |
| 170 | default: |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | } |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 175 | } |