~ubuntu-branches/ubuntu/utopic/neutron/utopic-updates

« back to all changes in this revision

Viewing changes to neutron/db/l3_hascheduler_db.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-10-03 18:45:23 UTC
  • mfrom: (1.1.15)
  • Revision ID: package-import@ubuntu.com-20141003184523-4mt6dy1q3j8n30c9
Tags: 1:2014.2~rc1-0ubuntu1
* New upstream release candidate:
  - d/p/*: Refreshed.
  - d/control: Add python-requests-mock to BD's.
  - d/control: Align versioned requirements with upstream.
* Transition linuxbridge and openvswitch plugin users to modular
  layer 2 plugin (LP: #1323729):
  - d/control: Mark removed plugin packages as transitional, depend
    on neutron-plugin-ml2, mark oldlibs/extra.
  - d/neutron-plugin-{linuxbridge,openvswitch}.install: Drop.
  - d/control: Depend on neutron-plugin-ml2 for linuxbridge
    agent package.
  - d/neutron-plugin-linuxbridge-agent.upstart: Use ml2 plugin
    configuration files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2014 eNovance SAS <licensing@enovance.com>
 
2
#
 
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
 
4
# not use this file except in compliance with the License. You may obtain
 
5
# a copy of the License at
 
6
#
 
7
#      http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
12
# License for the specific language governing permissions and limitations
 
13
# under the License.
 
14
 
 
15
from sqlalchemy import func
 
16
from sqlalchemy import sql
 
17
 
 
18
from neutron.db import agents_db
 
19
from neutron.db import l3_agentschedulers_db as l3_sch_db
 
20
from neutron.db import l3_attrs_db
 
21
from neutron.db import l3_db
 
22
 
 
23
 
 
24
class L3_HA_scheduler_db_mixin(l3_sch_db.L3AgentSchedulerDbMixin):
 
25
 
 
26
    def get_ha_routers_l3_agents_count(self, context):
 
27
        """Return a map between HA routers and how many agents every
 
28
        router is scheduled to.
 
29
        """
 
30
 
 
31
        # Postgres requires every column in the select to be present in
 
32
        # the group by statement when using an aggregate function.
 
33
        # One solution is to generate a subquery and join it with the desired
 
34
        # columns.
 
35
        binding_model = l3_sch_db.RouterL3AgentBinding
 
36
        sub_query = (context.session.query(
 
37
            binding_model.router_id,
 
38
            func.count(binding_model.router_id).label('count')).
 
39
            join(l3_attrs_db.RouterExtraAttributes,
 
40
                 binding_model.router_id ==
 
41
                 l3_attrs_db.RouterExtraAttributes.router_id).
 
42
            join(l3_db.Router).
 
43
            filter(l3_attrs_db.RouterExtraAttributes.ha == sql.true()).
 
44
            group_by(binding_model.router_id).subquery())
 
45
 
 
46
        query = (context.session.query(
 
47
                 l3_db.Router.id, l3_db.Router.tenant_id, sub_query.c.count).
 
48
                 join(sub_query))
 
49
        return query
 
50
 
 
51
    def get_l3_agents_ordered_by_num_routers(self, context, agent_ids):
 
52
        query = (context.session.query(agents_db.Agent, func.count(
 
53
            l3_sch_db.RouterL3AgentBinding.router_id).label('count')).
 
54
            outerjoin(l3_sch_db.RouterL3AgentBinding).
 
55
            group_by(agents_db.Agent.id).
 
56
            filter(agents_db.Agent.id.in_(agent_ids)).
 
57
            order_by('count'))
 
58
 
 
59
        return [record[0] for record in query]