| 1 |
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# 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, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
import random |
| 17 |
|
| 18 |
from neutron_lib.plugins import directory |
| 19 |
from oslo_config import cfg |
| 20 |
from oslo_log import log as logging |
| 21 |
from oslo_service import loopingcall |
| 22 |
|
| 23 |
from neutron.agent.common import utils |
| 24 |
from neutron.db import agents_db |
| 25 |
from neutron_lib import context as neutron_context |
| 26 |
|
| 27 |
from networking_l2gw.services.l2gateway.common import config |
| 28 |
from networking_l2gw.services.l2gateway.common import constants as srv_const |
| 29 |
|
| 30 |
LOG = logging.getLogger(__name__) |
| 31 |
# DNM: Do Not Merge, test patch only! |
| 32 |
|
| 33 |
|
| 34 |
class L2GatewayAgentScheduler(agents_db.AgentDbMixin): |
| 35 |
"""L2gateway agent scheduler class. |
| 36 |
|
| 37 |
This maintains active and inactive agents and |
| 38 |
select monitor and transact agents. |
| 39 |
""" |
| 40 |
_plugin = None |
| 41 |
_l2gwplugin = None |
| 42 |
|
| 43 |
def __init__(self, agent_rpc, notifier=None): |
| 44 |
super().__init__() |
| 45 |
self.notifier = notifier |
| 46 |
config.register_l2gw_opts_helper() |
| 47 |
self.monitor_interval = cfg.CONF.periodic_monitoring_interval |
| 48 |
self.agent_rpc = agent_rpc |
| 49 |
|
| 50 |
@property |
| 51 |
def l2gwplugin(self): |
| 52 |
if self._l2gwplugin is None: |
| 53 |
self._l2gwplugin = directory.get_plugin(srv_const.L2GW) |
| 54 |
return self._l2gwplugin |
| 55 |
|
| 56 |
@property |
| 57 |
def plugin(self): |
| 58 |
if self._plugin is None: |
| 59 |
self._plugin = directory.get_plugin() |
| 60 |
return self._plugin |
| 61 |
|
| 62 |
def initialize_thread(self): |
| 63 |
"""Initialization of L2gateway agent scheduler thread.""" |
| 64 |
try: |
| 65 |
monitor_thread = loopingcall.FixedIntervalLoopingCall( |
| 66 |
self.monitor_agent_state) |
| 67 |
monitor_thread.start( |
| 68 |
interval=self.monitor_interval, |
| 69 |
initial_delay=random.randint(self.monitor_interval, |
| 70 |
self.monitor_interval * 2)) |
| 71 |
LOG.debug("Successfully initialized L2gateway agent scheduler" |
| 72 |
" thread with loop interval %s", self.monitor_interval) |
| 73 |
except Exception: |
| 74 |
LOG.error("Cannot initialize agent scheduler thread") |
| 75 |
|
| 76 |
def _select_agent_type(self, context, agents_to_process): |
| 77 |
"""Select the Monitor agent.""" |
| 78 |
# Various cases to be handled: |
| 79 |
# 1. Check if there is a single active L2 gateway agent. |
| 80 |
# If only one agent is active, then make it the Monitor agent. |
| 81 |
# 2. Else, in the list of the active agents, if there does not |
| 82 |
# exist Monitor agent, then make the agent that |
| 83 |
# started first as the Monitor agent. |
| 84 |
# 3. If multiple Monitor agents exist (case where the Monitor agent |
| 85 |
# gets disconnected from the Neutron server and another agent |
| 86 |
# becomes the Monitor agent and then the original Monitor agent |
| 87 |
# connects back within the agent downtime value), then we need to |
| 88 |
# send the fanout message so that only one becomes the Monitor |
| 89 |
# agent. |
| 90 |
|
| 91 |
# Check if there already exists Monitor agent and it's the only one. |
| 92 |
monitor_agents = [ |
| 93 |
x for x in agents_to_process |
| 94 |
if x['configurations'].get(srv_const.L2GW_AGENT_TYPE) == |
| 95 |
srv_const.MONITOR] |
| 96 |
if len(monitor_agents) == 1: |
| 97 |
return |
| 98 |
|
| 99 |
# We either have more than one Monitor agent, |
| 100 |
# or there does not exist Monitor agent. |
| 101 |
# We will decide which agent should be the Monitor agent. |
| 102 |
chosen_agent = None |
| 103 |
if len(agents_to_process) == 1: |
| 104 |
# Only one agent is configured. |
| 105 |
# Make it the Monitor agent |
| 106 |
chosen_agent = agents_to_process[0] |
| 107 |
else: |
| 108 |
# Select the agent with the oldest started_at |
| 109 |
# timestamp as the Monitor agent. |
| 110 |
sorted_active_agents = sorted(agents_to_process, |
| 111 |
key=lambda k: k['started_at']) |
| 112 |
chosen_agent = sorted_active_agents[0] |
| 113 |
self.agent_rpc.set_monitor_agent(context, chosen_agent['host']) |
| 114 |
|
| 115 |
def monitor_agent_state(self): |
| 116 |
"""Represents L2gateway agent scheduler thread. |
| 117 |
|
| 118 |
Maintains list of active and inactive agents based on |
| 119 |
the heartbeat recorded. |
| 120 |
""" |
| 121 |
context = neutron_context.get_admin_context() |
| 122 |
try: |
| 123 |
all_agents = self.plugin.get_agents( |
| 124 |
context, |
| 125 |
filters={'agent_type': [srv_const.AGENT_TYPE_L2GATEWAY]}) |
| 126 |
except Exception: |
| 127 |
LOG.exception("Unable to get the agent list. Continuing...") |
| 128 |
return |
| 129 |
|
| 130 |
# Reset the agents that will be processed for selecting the |
| 131 |
# Monitor agent |
| 132 |
agents_to_process = [] |
| 133 |
for agent in all_agents: |
| 134 |
if not utils.is_agent_down(agent['heartbeat_timestamp']): |
| 135 |
agents_to_process.append(agent) |
| 136 |
if agents_to_process: |
| 137 |
self._select_agent_type(context, agents_to_process) |
| 138 |
return |