l3_agent list_networks should be wrapped in try except

Bug #1062734 reported by Koaps
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
neutron
Won't Fix
Undecided
Unassigned

Bug Description

I was noticing an error in my logs for l3_agent and took a bit to figure out what it was telling me.

2012-10-06 01:10:26 ERROR [quantum.agent.l3_agent] Error running l3_nat daemon_loop
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/quantum-2013.1-py2.6.egg/quantum/agent/l3_agent.py", line 173, in daemon_loop
    self.do_single_loop()
  File "/usr/lib/python2.6/site-packages/quantum-2013.1-py2.6.egg/quantum/agent/l3_agent.py", line 204, in do_single_loop
    target_ex_net_id = self._fetch_external_net_id()
  File "/usr/lib/python2.6/site-packages/quantum-2013.1-py2.6.egg/quantum/agent/l3_agent.py", line 185, in _fetch_external_net_id
    ex_nets = self.qclient.list_networks(**params)['networks']
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 102, in with_params
    ret = self.function(instance, *args, **kwargs)
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 244, in list_networks
    return self.get(self.networks_path, params=_params)
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 525, in get
    headers=headers, params=params)
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 510, in retry_request
    headers=headers, params=params)
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 455, in do_request
    self._handle_fault_response(status_code, replybody)
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 436, in _handle_fault_response
    exception_handler_v20(status_code, des_error_body)
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 82, in exception_handler_v20
    message=message)
QuantumClientException: [Errno 111] ECONNREFUSED

This happens when the database has no tables created yet.

It can be seen using the code from quantumclient.v2_0.client

>>> from quantumclient.v2_0 import client
>>> client20 = client.Client(username='admin', password='password', auth_url='http://localhost:5000/v2.0', tenant_name='Demo')
>>> client20.tenant = 'default'
>>> client20.format = 'json'
>>> nets = client20.list_networks()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 102, in with_params
    ret = self.function(instance, *args, **kwargs)
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 244, in list_networks
    return self.get(self.networks_path, params=_params)
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 525, in get
    headers=headers, params=params)
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 510, in retry_request
    headers=headers, params=params)
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 455, in do_request
    self._handle_fault_response(status_code, replybody)
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 436, in _handle_fault_response
    exception_handler_v20(status_code, des_error_body)
  File "/usr/lib/python2.6/site-packages/python_quantumclient-2.1.1-py2.6.egg/quantumclient/v2_0/client.py", line 82, in exception_handler_v20
    message=message)
quantumclient.common.exceptions.QuantumClientException: [Errno 111] Connection refused

Just to make it more friendly, wrapping it in a try except seems to make things much nicer:

>>> from quantumclient.v2_0 import client
>>> client20 = client.Client(username='admin', password='password', auth_url='http://localhost:5000/v2.0', tenant_name='Demo')
>>> client20.tenant = 'default'
>>> client20.format = 'json'
>>> params = {'router:external': True}
>>> try:
... nets = self.client20.list_networks(**params)['networks']
... print nets
... except:
... print "Connection to database failed"
...
Connection to database failed
>>>

To make it so you don't need to wrap a all the returns, it might be a good idea to set ex_net so you don't get an error when doing the len():

    def _fetch_external_net_id(self):
        """Find UUID of single external network for this agent"""
        if self.conf.gateway_external_network_id:
            return self.conf.gateway_external_network_id

        ex_nets = {}
        params = {'router:external': True}
        try:
                ex_nets = self.qclient.list_networks(**params)['networks']
        except:
                LOG.exception("Connection to database failed")

        if len(ex_nets) > 1:
            raise Exception("must configure 'gateway_external_network_id' if "
                            "Quantum has more than one external network.")
        if len(ex_nets) == 0:
            return None
        return ex_nets[0]['id']

This also happens for list_routers:
    def do_single_loop(self):

#............skipped code lines

        # identify and update new or modified routers
        for r in self.qclient.list_routers()['routers']:

I tried wrapping it with a new function:

    def _fetch_routers(self):
        routers = {}
        try:
                routers = self.qclient.list_routers()['routers']
        except:
                LOG.exception("Connection to database failed")

        return routers

    def do_single_loop(self):

#............skipped code lines

        # identify and update new or modified routers
        #for r in self.qclient.list_routers()['routers']:
        for r in routers:

But that didn't catch the error for some reason, my python must be lacking some mojo.

Revision history for this message
dan wendlandt (danwent) wrote :

this is a reasonable suggestion I think, but this code should be going away once we update the L3 agent to use RPC: https://blueprints.launchpad.net/quantum/+spec/rpc-for-l3-agent

Changed in quantum:
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.