1 # Copyright (c) 2015 Red Hat, Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
16 from six.moves import queue as Queue
20 from ovs.db import idl
21 from ovs import poller
24 from neutron.agent.ovsdb.native import helpers
25 from neutron.agent.ovsdb.native import idlutils
28 class TransactionQueue(Queue.Queue, object):
29 def __init__(self, *args, **kwargs):
30 super(TransactionQueue, self).__init__(*args, **kwargs)
32 self.alertin = os.fdopen(alertpipe[0], 'r', 0)
33 self.alertout = os.fdopen(alertpipe[1], 'w', 0)
35 def get_nowait(self, *args, **kwargs):
37 result = super(TransactionQueue, self).get_nowait(*args, **kwargs)
43 def put(self, *args, **kwargs):
44 super(TransactionQueue, self).put(*args, **kwargs)
45 self.alertout.write('X')
49 def alert_fileno(self):
50 return self.alertin.fileno()
53 class Connection(object):
54 def __init__(self, connection, timeout, schema_name):
56 self.connection = connection
57 self.timeout = timeout
58 self.txns = TransactionQueue(1)
59 self.lock = threading.Lock()
60 self.schema_name = schema_name
64 if self.idl is not None:
68 helper = idlutils.get_schema_helper(self.connection,
71 # We may have failed do to set-manager not being called
72 helpers.enable_connection_uri(self.connection)
74 # There is a small window for a race, so retry up to a second
75 @retrying.retry(wait_exponential_multiplier=10,
77 def do_get_schema_helper():
78 return idlutils.get_schema_helper(self.connection,
80 helper = do_get_schema_helper()
83 self.idl = idl.Idl(self.connection, helper)
84 idlutils.wait_for_change(self.idl, self.timeout)
85 self.poller = poller.Poller()
86 self.thread = threading.Thread(target=self.run)
87 self.thread.setDaemon(True)
92 self.idl.wait(self.poller)
93 self.poller.fd_wait(self.txns.alert_fileno, poller.POLLIN)
96 txn = self.txns.get_nowait()
99 txn.results.put(txn.do_commit())
100 except Exception as ex:
101 er = idlutils.ExceptionResult(ex=ex,
102 tb=traceback.format_exc())
104 self.txns.task_done()
106 def queue_txn(self, txn):