Debian Codegraph @

neutron_vpnaas/db/vpn/vpn_agentschedulers_db.py

raw
1 # Copyright (c) 2013 OpenStack Foundation.
2 # Copyright (c) 2023 SysEleven GmbH.
3 # All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16
17 import random
18
19 from neutron.extensions import router_availability_zone as router_az
20 from neutron import worker as neutron_worker
21 from neutron_lib import context as ncontext
22 from neutron_lib.db import api as db_api
23 from neutron_lib.db import model_base
24 from neutron_lib.plugins import constants as plugin_const
25 from neutron_lib.plugins import directory
26 from oslo_config import cfg
27 from oslo_db import exception as db_exc
28 from oslo_log import log as logging
29 import oslo_messaging
30 import sqlalchemy as sa
31 from sqlalchemy import func
32
33 from neutron_vpnaas._i18n import _
34 from neutron_vpnaas.db.vpn import vpn_models
35 from neutron_vpnaas.extensions import vpn_agentschedulers
36 from neutron_vpnaas.services.vpn.common.constants import AGENT_TYPE_VPN
37
38
39 LOG = logging.getLogger(__name__)
40
41 VPN_AGENTS_SCHEDULER_OPTS = [
42 cfg.StrOpt('vpn_scheduler_driver',
43 default='neutron_vpnaas.scheduler.vpn_agent_scheduler'
44 '.LeastRoutersScheduler',
45 help=_('Driver to use for scheduling '
46 'router to a VPN agent')),
47 cfg.BoolOpt('vpn_auto_schedule', default=True,
48 help=_('Allow auto scheduling of routers to VPN agent.')),
49 cfg.BoolOpt('allow_automatic_vpnagent_failover', default=False,
50 help=_('Automatically reschedule routers from offline VPN '
51 'agents to online VPN agents.')),
52 ]
53
54 cfg.CONF.register_opts(VPN_AGENTS_SCHEDULER_OPTS)
55
56
57 class RouterVPNAgentBinding(model_base.BASEV2):
58 """Represents binding between neutron routers and VPN agents."""
59
60 router_id = sa.Column(sa.String(36),
61 sa.ForeignKey("routers.id", ondelete='CASCADE'),
62 primary_key=True,
63 unique=True,
64 nullable=False)
65 vpn_agent_id = sa.Column(sa.String(36), primary_key=True, nullable=False)
66
67
68 class VPNAgentSchedulerDbMixin(
69 vpn_agentschedulers.VPNAgentSchedulerPluginBase):
70 """Mixin class to add VPN agent scheduler extension to plugins
71 using the VPN agent.
72 """
73
74 vpn_scheduler = None
75 agent_notifiers = {}
76
77 @property
78 def l3_plugin(self):
79 return directory.get_plugin(plugin_const.L3)
80
81 @property
82 def core_plugin(self):
83 return directory.get_plugin()
84
85 def add_periodic_vpn_agent_status_check(self):
86 if not cfg.CONF.allow_automatic_vpnagent_failover:
87 LOG.info("Skipping periodic VPN agent status check because "
88 "automatic rescheduling is disabled.")
89 return
90
91 interval = max(cfg.CONF.agent_down_time // 2, 1)
92 # add random initial delay to allow agents to check in after the
93 # neutron server first starts. random to offset multiple servers
94 initial_delay = random.randint(interval, interval * 2)
95
96 check_worker = neutron_worker.PeriodicWorker(
97 self.reschedule_vpnservices_from_down_agents,
98 interval, initial_delay)
99 self.add_worker(check_worker)
100
101 def reschedule_vpnservices_from_down_agents(self):
102 """Reschedule VPN services from down VPN agents.
103
104 VPN services are scheduled per router.
105 """
106 context = ncontext.get_admin_context()
107 try:
108 down_bindings = self.get_down_router_bindings(context)
109
110 agents_back_online = set()
111 for binding in down_bindings:
112 if binding.vpn_agent_id in agents_back_online:
113 continue
114 agent = self.core_plugin.get_agent(context,
115 binding.vpn_agent_id)
116 if agent['alive']:
117 agents_back_online.add(binding.vpn_agent_id)
118 continue
119
120 LOG.warning(
121 "Rescheduling vpn services for router %(router)s from "
122 "agent %(agent)s because the agent is not alive.",
123 {'router': binding.router_id,
124 'agent': binding.vpn_agent_id})
125 try:
126 self.reschedule_router(context, binding.router_id, agent)
127 except (vpn_agentschedulers.RouterReschedulingFailed,
128 oslo_messaging.RemoteError):
129 # Catch individual rescheduling errors here
130 # so one broken one doesn't stop the iteration.
131 LOG.exception("Failed to reschedule vpn services for "
132 "router %s", binding.router_id)
133 except Exception:
134 # we want to be thorough and catch whatever is raised
135 # to avoid loop abortion
136 LOG.exception("Exception encountered during vpn service "
137 "rescheduling.")
138
139 @db_api.CONTEXT_READER
140 def get_down_router_bindings(self, context):
141 vpn_agents = self.get_vpn_agents(context, active=False)
142 if not vpn_agents:
143 return []
144 vpn_agent_ids = [vpn_agent['id'] for vpn_agent in vpn_agents]
145
146 query = context.session.query(RouterVPNAgentBinding)
147 query = query.filter(
148 RouterVPNAgentBinding.vpn_agent_id.in_(vpn_agent_ids))
149 return query.all()
150
151 def validate_agent_router_combination(self, context, agent, router):
152 """Validate if the router can be correctly assigned to the agent.
153
154 :raises: InvalidVPNAgent if attempting to assign router to an
155 unsuitable agent (disabled, type != VPN, incompatible configuration)
156 """
157 if agent['agent_type'] != AGENT_TYPE_VPN:
158 raise vpn_agentschedulers.InvalidVPNAgent(id=agent['id'])
159
160 @db_api.CONTEXT_READER
161 def check_agent_router_scheduling_needed(self, context, agent, router):
162 """Check if the scheduling of router's VPN services is needed.
163
164 :raises: RouterHostedByVPNAgent if router is already assigned
165 to a different agent.
166 :returns: True if scheduling is needed, otherwise False
167 """
168 router_id = router['id']
169 agent_id = agent['id']
170 query = context.session.query(RouterVPNAgentBinding)
171 bindings = query.filter_by(router_id=router_id).all()
172 if not bindings:
173 return True
174 for binding in bindings:
175 if binding.vpn_agent_id == agent_id:
176 # router already bound to the agent we need
177 return False
178 # Router is already bound to some agent
179 raise vpn_agentschedulers.RouterHostedByVPNAgent(
180 router_id=router_id,
181 agent_id=bindings[0].vpn_agent_id)
182
183 def create_router_to_agent_binding(self, context, router_id, agent_id):
184 """Create router to VPN agent binding."""
185 try:
186 with db_api.CONTEXT_WRITER.using(context):
187 binding = RouterVPNAgentBinding()
188 binding.vpn_agent_id = agent_id
189 binding.router_id = router_id
190 context.session.add(binding)
191 except db_exc.DBDuplicateEntry:
192 LOG.debug('VPN service of router %(router_id)s has already been '
193 'scheduled to a VPN agent.',
194 {'router_id': router_id})
195 return False
196 except db_exc.DBReferenceError:
197 LOG.debug('Router %s has already been removed '
198 'by concurrent operation', router_id)
199 return False
200
201 LOG.debug('VPN service of router %(router_id)s is scheduled to '
202 'VPN agent %(agent_id)s',
203 {'router_id': router_id, 'agent_id': agent_id})
204 return True
205
206 def add_router_to_vpn_agent(self, context, agent_id, router_id):
207 """Add a VPN agent to host VPN services of a router."""
208 with db_api.CONTEXT_WRITER.using(context):
209 router = self.l3_plugin.get_router(context, router_id)
210 agent = self.core_plugin.get_agent(context, agent_id)
211 self.validate_agent_router_combination(context, agent, router)
212 if not self.check_agent_router_scheduling_needed(
213 context, agent, router):
214 return
215 try:
216 success = self.create_router_to_agent_binding(
217 context, router['id'], agent['id'])
218 except db_exc.DBError:
219 success = False
220
221 if not success:
222 raise vpn_agentschedulers.RouterSchedulingFailed(
223 router_id=router_id, agent_id=agent_id)
224
225 # notify agent
226 vpn_notifier = self.agent_notifiers.get(AGENT_TYPE_VPN)
227 if vpn_notifier:
228 vpn_notifier.vpnservice_added_to_agent(
229 context, [router_id], agent['host'])
230
231 # update port binding
232 self.vpn_router_agent_binding_changed(
233 context, router_id, agent['host'])
234
235 def remove_router_from_vpn_agent(self, context, agent_id, router_id):
236 """Remove the router from VPN agent.
237
238 After removal, the VPN service(s) of the router will be non-hosted
239 until there is an update which leads to re-schedule or the router is
240 added to another agent manually.
241 """
242 agent = self.core_plugin.get_agent(context, agent_id)
243
244 self._unbind_router(context, router_id, agent_id)
245
246 vpn_notifier = self.agent_notifiers.get(AGENT_TYPE_VPN)
247 if vpn_notifier:
248 vpn_notifier.vpnservice_removed_from_agent(
249 context, router_id, agent['host'])
250
251 def _unbind_router(self, context, router_id, agent_id):
252 with db_api.CONTEXT_WRITER.using(context):
253 query = context.session.query(RouterVPNAgentBinding)
254 query = query.filter(
255 RouterVPNAgentBinding.router_id == router_id,
256 RouterVPNAgentBinding.vpn_agent_id == agent_id)
257 return query.delete()
258
259 def reschedule_router(self, context, router_id, cur_agent):
260 """Reschedule router to a new VPN agent
261
262 Remove the router from the agent currently hosting it and
263 schedule it again
264 """
265 with db_api.CONTEXT_WRITER.using(context):
266 deleted = self._unbind_router(context, router_id, cur_agent['id'])
267 if not deleted:
268 # If nothing was deleted, the binding didn't exist anymore
269 # because some other server deleted the binding concurrently.
270 # Stop here.
271 return
272
273 new_agent = self.schedule_router(context, router_id)
274 if not new_agent:
275 # No new_agent means that another server scheduled the
276 # router concurrently. Don't raise RouterReschedulingFailed.
277 return
278
279 self._notify_agents_router_rescheduled(context, router_id,
280 cur_agent, new_agent)
281 # update port binding
282 self.vpn_router_agent_binding_changed(
283 context, router_id, new_agent['host'])
284
285 def _notify_agents_router_rescheduled(self, context, router_id,
286 old_agent, new_agent):
287 vpn_notifier = self.agent_notifiers.get(AGENT_TYPE_VPN)
288 if not vpn_notifier:
289 return
290
291 old_host = old_agent['host']
292 new_host = new_agent['host']
293 if old_host != new_host:
294 vpn_notifier.vpnservice_removed_from_agent(
295 context, router_id, old_host)
296
297 try:
298 vpn_notifier.vpnservice_added_to_agent(
299 context, [router_id], new_host)
300 except oslo_messaging.MessagingException:
301 self._unbind_router(context, router_id, new_agent['id'])
302 raise vpn_agentschedulers.RouterReschedulingFailed(
303 router_id=router_id)
304
305 @db_api.CONTEXT_READER
306 def list_routers_on_vpn_agent(self, context, agent_id):
307 query = context.session.query(RouterVPNAgentBinding.router_id)
308 query = query.filter(RouterVPNAgentBinding.vpn_agent_id == agent_id)
309
310 router_ids = [item[0] for item in query]
311 if router_ids:
312 return {'routers':
313 self.l3_plugin.get_routers(context,
314 filters={'id': router_ids})}
315 else:
316 # Exception will be thrown if the requested agent does not exist.
317 self.core_plugin.get_agent(context, agent_id)
318 return {'routers': []}
319
320 @db_api.CONTEXT_READER
321 def get_vpn_agents_hosting_routers(self, context, router_ids, active=None):
322 if not router_ids:
323 return []
324 query = context.session.query(RouterVPNAgentBinding)
325 query = query.filter(RouterVPNAgentBinding.router_id.in_(router_ids))
326
327 filters = {'id': [binding.vpn_agent_id for binding in query]}
328 vpn_agents = self.core_plugin.get_agents(context, filters=filters)
329 if active is not None:
330 vpn_agents = [agent
331 for agent in vpn_agents
332 if agent['alive'] == active]
333 return vpn_agents
334
335 def list_vpn_agents_hosting_router(self, context, router_id):
336 vpn_agents = self.get_vpn_agents_hosting_routers(context, [router_id])
337 return {'agents': vpn_agents}
338
339 def get_vpn_agents(self, context, active=None, host=None):
340 filters = {'agent_type': [AGENT_TYPE_VPN]}
341 if host is not None:
342 filters['host'] = [host]
343 vpn_agents = self.core_plugin.get_agents(context, filters=filters)
344 if active is None:
345 return vpn_agents
346 else:
347 return [vpn_agent
348 for vpn_agent in vpn_agents
349 if vpn_agent['alive'] == active]
350
351 def get_vpn_agent_on_host(self, context, host, active=None):
352 agents = self.get_vpn_agents(context, active=active, host=host)
353 if agents:
354 return agents[0]
355
356 @db_api.CONTEXT_READER
357 def get_unscheduled_vpn_routers(self, context, router_ids=None):
358 """Get IDs of routers which have unscheduled VPN services."""
359 query = context.session.query(vpn_models.VPNService.router_id)
360 query = query.outerjoin(
361 RouterVPNAgentBinding,
362 vpn_models.VPNService.router_id == RouterVPNAgentBinding.router_id)
363 query = query.filter(RouterVPNAgentBinding.vpn_agent_id.is_(None))
364 if router_ids:
365 query = query.filter(
366 vpn_models.VPNService.router_id.in_(router_ids))
367 return [router_id for router_id, in query.all()]
368
369 def auto_schedule_routers(self, context, vpn_agent):
370 if self.vpn_scheduler:
371 return self.vpn_scheduler.auto_schedule_routers(
372 self, context, vpn_agent)
373
374 def schedule_router(self, context, router, candidates=None):
375 """Schedule VPN services of a router to a VPN agent.
376
377 Returns the chosen agent; None if another server scheduled the
378 router concurrently.
379 Raises RouterReschedulingFailed if no suitable agent is found.
380 """
381 if self.vpn_scheduler:
382 return self.vpn_scheduler.schedule(
383 self, context, router, candidates=candidates)
384
385 @db_api.CONTEXT_READER
386 def get_vpn_agent_with_min_routers(self, context, agent_ids):
387 """Return VPN agent with the least number of routers."""
388 if not agent_ids:
389 return None
390 query = context.session.query(
391 RouterVPNAgentBinding.vpn_agent_id,
392 func.count(RouterVPNAgentBinding.router_id).label('count'))
393 query = query.group_by(RouterVPNAgentBinding.vpn_agent_id)
394 query = query.order_by('count')
395 query = query.filter(RouterVPNAgentBinding.vpn_agent_id.in_(agent_ids))
396 used_agent_ids = [agent_id for agent_id, _ in query.all()]
397 unused_agent_ids = set(agent_ids) - set(used_agent_ids)
398 if unused_agent_ids:
399 return unused_agent_ids.pop()
400 else:
401 return used_agent_ids[0]
402
403 def get_hosts_to_notify(self, context, router_id):
404 """Returns all hosts to send notification about router update"""
405 agents = self.get_vpn_agents_hosting_routers(context, [router_id],
406 active=True)
407 return [a['host'] for a in agents]
408
409
410 class AZVPNAgentSchedulerDbMixin(VPNAgentSchedulerDbMixin,
411 router_az.RouterAvailabilityZonePluginBase):
412 """Mixin class to add availability_zone supported VPN agent scheduler."""
413
414 def get_router_availability_zones(self, router):
415 return list({agent.availability_zone for agent in router.vpn_agents})