~niedbalski/ubuntu/vivid/neutron/fixes-1447803

« back to all changes in this revision

Viewing changes to neutron/plugins/bigswitch/db/consistency_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:
14
14
#    under the License.
15
15
import sqlalchemy as sa
16
16
 
17
 
from neutron.common import exceptions
18
17
from neutron.db import api as db
19
18
from neutron.db import model_base
20
19
from neutron.openstack.common import log as logging
22
21
LOG = logging.getLogger(__name__)
23
22
 
24
23
 
25
 
class MultipleReadForUpdateCalls(exceptions.NeutronException):
26
 
    message = _("Only one read_for_update call may be made at a time.")
27
 
 
28
 
 
29
24
class ConsistencyHash(model_base.BASEV2):
30
25
    '''
31
26
    A simple table to store the latest consistency hash
41
36
 
42
37
class HashHandler(object):
43
38
    '''
44
 
    A wrapper object to keep track of the session and hold the SQL
45
 
    lock between the read and the update to prevent other servers
46
 
    from reading the hash during a transaction.
 
39
    A wrapper object to keep track of the session between the read
 
40
    and the update operations.
47
41
    '''
48
42
    def __init__(self, context=None, hash_id='1'):
49
43
        self.hash_id = hash_id
50
44
        self.session = db.get_session() if not context else context.session
51
45
        self.hash_db_obj = None
52
 
        self.transaction = None
53
46
 
54
47
    def read_for_update(self):
55
 
        if self.transaction:
56
 
            raise MultipleReadForUpdateCalls()
57
 
        self.transaction = self.session.begin(subtransactions=True)
58
48
        # REVISIT(kevinbenton): locking here with the DB is prone to deadlocks
59
49
        # in various multi-REST-call scenarios (router intfs, flips, etc).
60
50
        # Since it doesn't work in Galera deployments anyway, another sync
61
51
        # mechanism will have to be introduced to prevent inefficient double
62
52
        # syncs in HA deployments.
63
 
        res = (self.session.query(ConsistencyHash).
64
 
               filter_by(hash_id=self.hash_id).first())
 
53
        with self.session.begin(subtransactions=True):
 
54
            res = (self.session.query(ConsistencyHash).
 
55
                   filter_by(hash_id=self.hash_id).first())
65
56
        if not res:
66
57
            return ''
67
58
        self.hash_db_obj = res
69
60
 
70
61
    def put_hash(self, hash):
71
62
        hash = hash or ''
72
 
        if not self.transaction:
73
 
            self.transaction = self.session.begin(subtransactions=True)
74
 
        if self.hash_db_obj is not None:
75
 
            self.hash_db_obj.hash = hash
76
 
        else:
77
 
            conhash = ConsistencyHash(hash_id=self.hash_id, hash=hash)
78
 
            self.session.merge(conhash)
79
 
        self.close_update_session()
 
63
        with self.session.begin(subtransactions=True):
 
64
            if self.hash_db_obj is not None:
 
65
                self.hash_db_obj.hash = hash
 
66
            else:
 
67
                conhash = ConsistencyHash(hash_id=self.hash_id, hash=hash)
 
68
                self.session.merge(conhash)
80
69
        LOG.debug(_("Consistency hash for group %(hash_id)s updated "
81
70
                    "to %(hash)s"), {'hash_id': self.hash_id, 'hash': hash})
82
 
 
83
 
    def close_update_session(self):
84
 
        if not self.transaction:
85
 
            return
86
 
        self.transaction.commit()
87
 
        self.transaction = None