| 1 |
# Copyright 2016 Huawei Technologies India Pvt. Ltd. |
| 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 |
from neutron_lib.callbacks import events |
| 17 |
from neutron_lib.callbacks import registry |
| 18 |
from neutron_lib import context as nl_context |
| 19 |
from neutron_lib.db import api as db_api |
| 20 |
from neutron_lib.objects import registry as obj_reg |
| 21 |
from oslo_db import exception as db_exc |
| 22 |
from oslo_log import log as logging |
| 23 |
from sqlalchemy import sql |
| 24 |
|
| 25 |
from neutron.agent.common import utils |
| 26 |
from neutron.db.models import agent as agent_model |
| 27 |
from neutron.scheduler import base_resource_filter |
| 28 |
from neutron.scheduler import base_scheduler |
| 29 |
|
| 30 |
from neutron_dynamic_routing._i18n import _LI, _LW |
| 31 |
from neutron_dynamic_routing.api.rpc.callbacks import resources as dr_resources |
| 32 |
from neutron_dynamic_routing.db import bgp_db |
| 33 |
from neutron_dynamic_routing.db import bgp_dragentscheduler_db as bgp_dras_db |
| 34 |
from neutron_dynamic_routing.services.bgp.common import constants as bgp_consts |
| 35 |
|
| 36 |
LOG = logging.getLogger(__name__) |
| 37 |
BGP_SPEAKER_PER_DRAGENT = 1 |
| 38 |
|
| 39 |
|
| 40 |
class BgpDrAgentFilter(base_resource_filter.BaseResourceFilter): |
| 41 |
|
| 42 |
def bind(self, context, agents, bgp_speaker_id, force_scheduling=False): |
| 43 |
"""Bind the BgpSpeaker to a BgpDrAgent.""" |
| 44 |
bound_agents = agents[:] |
| 45 |
for agent in agents: |
| 46 |
# saving agent_id to use it after rollback to avoid |
| 47 |
# DetachedInstanceError |
| 48 |
agent_id = agent.id |
| 49 |
binding = bgp_dras_db.BgpSpeakerDrAgentBinding() |
| 50 |
binding.agent_id = agent_id |
| 51 |
binding.bgp_speaker_id = bgp_speaker_id |
| 52 |
try: |
| 53 |
with db_api.CONTEXT_WRITER.using(context): |
| 54 |
context.session.add(binding) |
| 55 |
except db_exc.DBDuplicateEntry: |
| 56 |
# it's totally ok, someone just did our job! |
| 57 |
bound_agents.remove(agent) |
| 58 |
LOG.info(_LI('BgpDrAgent %s already present'), agent_id) |
| 59 |
LOG.debug('BgpSpeaker %(bgp_speaker_id)s is scheduled to be ' |
| 60 |
'hosted by BgpDrAgent %(agent_id)s', |
| 61 |
{'bgp_speaker_id': bgp_speaker_id, |
| 62 |
'agent_id': agent_id}) |
| 63 |
super().bind(context, bound_agents, |
| 64 |
bgp_speaker_id, force_scheduling) |
| 65 |
|
| 66 |
def filter_agents(self, plugin, context, bgp_speaker): |
| 67 |
"""Return the agents that can host the BgpSpeaker.""" |
| 68 |
agents_dict = self._get_bgp_speaker_hostable_dragents( |
| 69 |
plugin, context, bgp_speaker) |
| 70 |
if not agents_dict['hostable_agents'] or agents_dict['n_agents'] <= 0: |
| 71 |
return {'n_agents': 0, |
| 72 |
'hostable_agents': [], |
| 73 |
'hosted_agents': []} |
| 74 |
return agents_dict |
| 75 |
|
| 76 |
def _get_active_dragents(self, plugin, context): |
| 77 |
"""Return a list of active BgpDrAgents.""" |
| 78 |
with db_api.CONTEXT_READER.using(context): |
| 79 |
active_dragents = plugin.get_agent_objects( |
| 80 |
context, filters={ |
| 81 |
'agent_type': [bgp_consts.AGENT_TYPE_BGP_ROUTING], |
| 82 |
'admin_state_up': [True]}) |
| 83 |
if not active_dragents: |
| 84 |
return [] |
| 85 |
return active_dragents |
| 86 |
|
| 87 |
def _get_num_dragents_hosting_bgp_speaker(self, bgp_speaker_id, |
| 88 |
dragent_bindings): |
| 89 |
return sum(1 if dragent_binding.bgp_speaker_id == bgp_speaker_id else 0 |
| 90 |
for dragent_binding in dragent_bindings) |
| 91 |
|
| 92 |
def _get_bgp_speaker_hostable_dragents(self, plugin, context, bgp_speaker): |
| 93 |
"""Return number of additional BgpDrAgents which will actually host |
| 94 |
the given BgpSpeaker and a list of BgpDrAgents which can host the |
| 95 |
given BgpSpeaker |
| 96 |
""" |
| 97 |
# only one BgpSpeaker can be hosted by a BgpDrAgent for now. |
| 98 |
dragents_per_bgp_speaker = BGP_SPEAKER_PER_DRAGENT |
| 99 |
dragent_bindings = plugin.get_dragent_bgp_speaker_bindings(context) |
| 100 |
agents_hosting = [dragent_binding.agent_id |
| 101 |
for dragent_binding in dragent_bindings] |
| 102 |
|
| 103 |
num_dragents_hosting_bgp_speaker = ( |
| 104 |
self._get_num_dragents_hosting_bgp_speaker(bgp_speaker['id'], |
| 105 |
dragent_bindings)) |
| 106 |
n_agents = dragents_per_bgp_speaker - num_dragents_hosting_bgp_speaker |
| 107 |
if n_agents <= 0: |
| 108 |
return {'n_agents': 0, |
| 109 |
'hostable_agents': [], |
| 110 |
'hosted_agents': []} |
| 111 |
|
| 112 |
active_dragents = self._get_active_dragents(plugin, context) |
| 113 |
hostable_dragents = [ |
| 114 |
agent for agent in set(active_dragents) |
| 115 |
if agent.id not in agents_hosting and plugin.is_eligible_agent( |
| 116 |
active=True, agent=agent) |
| 117 |
] |
| 118 |
if not hostable_dragents: |
| 119 |
return {'n_agents': 0, |
| 120 |
'hostable_agents': [], |
| 121 |
'hosted_agents': []} |
| 122 |
|
| 123 |
n_agents = min(len(hostable_dragents), n_agents) |
| 124 |
return {'n_agents': n_agents, |
| 125 |
'hostable_agents': hostable_dragents, |
| 126 |
'hosted_agents': num_dragents_hosting_bgp_speaker} |
| 127 |
|
| 128 |
|
| 129 |
class BgpDrAgentSchedulerBase(BgpDrAgentFilter): |
| 130 |
|
| 131 |
periodic_actions = True |
| 132 |
|
| 133 |
def _register_callbacks(self): |
| 134 |
registry.subscribe(self.schedule_bgp_speaker_callback, |
| 135 |
dr_resources.BGP_SPEAKER, |
| 136 |
events.AFTER_CREATE) |
| 137 |
|
| 138 |
def schedule_all_unscheduled_bgp_speakers(self, context): |
| 139 |
"""Call schedule_unscheduled_bgp_speakers for all hosts. |
| 140 |
""" |
| 141 |
|
| 142 |
with db_api.CONTEXT_WRITER.using(context): |
| 143 |
query = context.session.query(agent_model.Agent.host).distinct() |
| 144 |
for agent in query: |
| 145 |
self.schedule_unscheduled_bgp_speakers(context, agent[0]) |
| 146 |
return True |
| 147 |
|
| 148 |
def schedule_unscheduled_bgp_speakers(self, context, host): |
| 149 |
"""Schedule unscheduled BgpSpeaker to a BgpDrAgent. |
| 150 |
""" |
| 151 |
|
| 152 |
LOG.debug('Started auto-scheduling on host %s', host) |
| 153 |
with db_api.CONTEXT_WRITER.using(context): |
| 154 |
bgp_dragent = obj_reg.load_class('Agent').get_object( |
| 155 |
context, |
| 156 |
agent_type=bgp_consts.AGENT_TYPE_BGP_ROUTING, |
| 157 |
host=host, |
| 158 |
admin_state_up=True) |
| 159 |
if not bgp_dragent: |
| 160 |
LOG.debug('No enabled BgpDrAgent on host %s', host) |
| 161 |
return False |
| 162 |
|
| 163 |
if utils.is_agent_down( |
| 164 |
bgp_dragent.heartbeat_timestamp): |
| 165 |
LOG.warning(_LW('BgpDrAgent %s is down'), bgp_dragent.id) |
| 166 |
return False |
| 167 |
|
| 168 |
if self._is_bgp_speaker_hosted(context, bgp_dragent['id']): |
| 169 |
# One BgpDrAgent can only host one BGP speaker |
| 170 |
LOG.debug('BgpDrAgent already hosting a speaker on host %s. ' |
| 171 |
'Cannot schedule an another one', host) |
| 172 |
return False |
| 173 |
|
| 174 |
unscheduled_speakers = self._get_unscheduled_bgp_speakers(context) |
| 175 |
if not unscheduled_speakers: |
| 176 |
LOG.debug('Nothing to auto-schedule on host %s', host) |
| 177 |
return False |
| 178 |
|
| 179 |
self.bind(context, [bgp_dragent], unscheduled_speakers[0]) |
| 180 |
return True |
| 181 |
|
| 182 |
def _is_bgp_speaker_hosted(self, context, agent_id): |
| 183 |
speaker_binding_model = bgp_dras_db.BgpSpeakerDrAgentBinding |
| 184 |
|
| 185 |
query = context.session.query(speaker_binding_model) |
| 186 |
query = query.filter(speaker_binding_model.agent_id == agent_id) |
| 187 |
|
| 188 |
return query.count() > 0 |
| 189 |
|
| 190 |
def _get_unscheduled_bgp_speakers(self, context): |
| 191 |
"""BGP speakers that needs to be scheduled. |
| 192 |
""" |
| 193 |
|
| 194 |
no_agent_binding = ~sql.exists().where( |
| 195 |
bgp_db.BgpSpeaker.id == |
| 196 |
bgp_dras_db.BgpSpeakerDrAgentBinding.bgp_speaker_id) |
| 197 |
query = context.session.query(bgp_db.BgpSpeaker.id).filter( |
| 198 |
no_agent_binding) |
| 199 |
return [bgp_speaker_id_[0] for bgp_speaker_id_ in query] |
| 200 |
|
| 201 |
def schedule_bgp_speaker_callback(self, resource, event, trigger, payload): |
| 202 |
plugin = payload.metadata['plugin'] |
| 203 |
bgp_speaker = payload.latest_state |
| 204 |
if event == events.AFTER_CREATE: |
| 205 |
ctx = nl_context.get_admin_context() |
| 206 |
plugin.schedule_bgp_speaker(ctx, bgp_speaker) |
| 207 |
|
| 208 |
|
| 209 |
class ChanceScheduler(base_scheduler.BaseChanceScheduler, |
| 210 |
BgpDrAgentSchedulerBase): |
| 211 |
|
| 212 |
def __init__(self): |
| 213 |
super().__init__(self) |
| 214 |
self._register_callbacks() |
| 215 |
|
| 216 |
|
| 217 |
class WeightScheduler(base_scheduler.BaseWeightScheduler, |
| 218 |
BgpDrAgentSchedulerBase): |
| 219 |
|
| 220 |
def __init__(self): |
| 221 |
super().__init__(self) |
| 222 |
self._register_callbacks() |
| 223 |
|
| 224 |
|
| 225 |
class StaticScheduler(base_scheduler.BaseScheduler, |
| 226 |
BgpDrAgentFilter): |
| 227 |
|
| 228 |
periodic_actions = False |
| 229 |
|
| 230 |
def schedule_all_unscheduled_bgp_speakers(self, context): |
| 231 |
return True |
| 232 |
|
| 233 |
def schedule_unscheduled_bgp_speakers(self, context, host): |
| 234 |
return True |
| 235 |
|
| 236 |
def select(self, plugin, context, resource_hostable_agents, |
| 237 |
resource_hosted_agents, num_agents_needed): |
| 238 |
|
| 239 |
return [] |