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; |
| 22 | import org.onosproject.cluster.NodeId; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 23 | import org.onosproject.routeservice.ResolvedRoute; |
| 24 | import org.onosproject.routeservice.Route; |
| 25 | import org.onosproject.routeservice.RouteAdminService; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 26 | import org.onosproject.store.serializers.KryoNamespaces; |
| 27 | import org.onosproject.store.service.DistributedPrimitive; |
| 28 | import org.onosproject.store.service.Serializer; |
| 29 | import org.onosproject.store.service.StorageService; |
| 30 | import org.onosproject.store.service.WorkQueue; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import java.util.Collection; |
| 35 | import java.util.concurrent.ScheduledExecutorService; |
| 36 | import java.util.stream.Collectors; |
| 37 | |
| 38 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 39 | import static org.onlab.util.Tools.groupedThreads; |
| 40 | |
| 41 | /** |
| 42 | * Monitors cluster nodes and removes routes if a cluster node becomes unavailable. |
| 43 | */ |
| 44 | public class RouteMonitor { |
| 45 | |
| 46 | private final Logger log = LoggerFactory.getLogger(this.getClass()); |
| 47 | |
| 48 | private static final String TOPIC = "route-reaper"; |
| 49 | private static final int NUM_PARALLEL_JOBS = 10; |
| 50 | |
| 51 | private RouteAdminService routeService; |
| 52 | private final ClusterService clusterService; |
| 53 | private StorageService storageService; |
| 54 | |
| 55 | private WorkQueue<NodeId> queue; |
| 56 | |
| 57 | private final InternalClusterListener clusterListener = new InternalClusterListener(); |
| 58 | |
| 59 | private final ScheduledExecutorService reaperExecutor = |
| 60 | newSingleThreadScheduledExecutor(groupedThreads("route/reaper", "", log)); |
| 61 | |
| 62 | /** |
| 63 | * Creates a new route monitor. |
| 64 | * |
| 65 | * @param routeService route service |
| 66 | * @param clusterService cluster service |
| 67 | * @param storageService storage service |
| 68 | */ |
| 69 | public RouteMonitor(RouteAdminService routeService, |
| 70 | ClusterService clusterService, StorageService storageService) { |
| 71 | this.routeService = routeService; |
| 72 | this.clusterService = clusterService; |
| 73 | this.storageService = storageService; |
| 74 | |
| 75 | clusterService.addListener(clusterListener); |
| 76 | |
| 77 | queue = storageService.getWorkQueue(TOPIC, Serializer.using(KryoNamespaces.API)); |
| 78 | queue.addStatusChangeListener(this::statusChange); |
| 79 | |
| 80 | startProcessing(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Shuts down the route monitor. |
| 85 | */ |
| 86 | public void shutdown() { |
| 87 | stopProcessing(); |
| 88 | clusterService.removeListener(clusterListener); |
| 89 | } |
| 90 | |
| 91 | private void statusChange(DistributedPrimitive.Status status) { |
| 92 | switch (status) { |
| 93 | case ACTIVE: |
| 94 | startProcessing(); |
| 95 | break; |
| 96 | case SUSPENDED: |
| 97 | stopProcessing(); |
| 98 | break; |
| 99 | case INACTIVE: |
| 100 | default: |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private void startProcessing() { |
| 106 | queue.registerTaskProcessor(this::cleanRoutes, NUM_PARALLEL_JOBS, reaperExecutor); |
| 107 | } |
| 108 | |
| 109 | private void stopProcessing() { |
| 110 | queue.stopProcessing(); |
| 111 | } |
| 112 | |
| 113 | private void cleanRoutes(NodeId node) { |
| 114 | log.info("Cleaning routes from unavailable node {}", node); |
| 115 | |
| 116 | Collection<Route> routes = routeService.getRouteTables().stream() |
| 117 | .flatMap(id -> routeService.getRoutes(id).stream()) |
| 118 | .flatMap(route -> route.allRoutes().stream()) |
| 119 | .map(ResolvedRoute::route) |
| 120 | .filter(r -> r.sourceNode().equals(node)) |
| 121 | .collect(Collectors.toList()); |
| 122 | |
| 123 | log.debug("Withdrawing routes: {}", routes); |
| 124 | |
| 125 | routeService.withdraw(routes); |
| 126 | } |
| 127 | |
| 128 | private class InternalClusterListener implements ClusterEventListener { |
| 129 | |
| 130 | @Override |
| 131 | public void event(ClusterEvent event) { |
| 132 | switch (event.type()) { |
| 133 | case INSTANCE_DEACTIVATED: |
| 134 | NodeId id = event.subject().id(); |
| 135 | log.info("Node {} deactivated", id); |
| 136 | queue.addOne(id); |
| 137 | break; |
| 138 | case INSTANCE_ADDED: |
| 139 | case INSTANCE_REMOVED: |
| 140 | case INSTANCE_ACTIVATED: |
| 141 | case INSTANCE_READY: |
| 142 | default: |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | } |