diff --git a/neutron/agent/ovsdb/native/helpers.py b/neutron/agent/ovsdb/native/helpers.py index 4a7c28f2d0e..849b19b4438 100644 --- a/neutron/agent/ovsdb/native/helpers.py +++ b/neutron/agent/ovsdb/native/helpers.py @@ -26,6 +26,6 @@ ml2_ovs_conf.register_ovs_opts(cfg=cfg.CONF) enable_connection_uri = functools.partial( priv_helpers.enable_connection_uri, - log_fail_as_error=False, check_exit_code=False, + log_fail_as_error=False, check_exit_code=True, timeout=cfg.CONF.OVS.ovsdb_timeout, inactivity_probe=cfg.CONF.OVS.of_inactivity_probe * 1000) diff --git a/neutron/privileged/agent/ovsdb/native/helpers.py b/neutron/privileged/agent/ovsdb/native/helpers.py index f5650c07589..1a3ae2cad7d 100644 --- a/neutron/privileged/agent/ovsdb/native/helpers.py +++ b/neutron/privileged/agent/ovsdb/native/helpers.py @@ -13,11 +13,15 @@ # under the License. from oslo_concurrency import processutils +from oslo_log import log as logging from oslo_utils import netutils from neutron import privileged +LOG = logging.getLogger(__name__) + + def _connection_to_manager_uri(conn_uri): proto, addr = conn_uri.split(':', 1) ip, port = netutils.parse_host_port(addr) @@ -45,5 +49,30 @@ def enable_connection_uri(conn_uri, log_fail_as_error=False, '--', 'add', 'Open_vSwitch', '.', 'manager_options', '@manager'] if probe is not None: cmd += ['--', 'set', 'Manager', man_uri, 'inactivity_probe=%s' % probe] - return processutils.execute(*cmd, log_errors=log_fail_as_error, - check_exit_code=check_exit_code) + try: + processutils.execute(*cmd, log_errors=log_fail_as_error, + check_exit_code=check_exit_code) + except processutils.ProcessExecutionError as pe: + LOG.warning("OVS Manager creation failed, it might already " + "exist (stderr: %s).", pe.stderr) + if probe is None: + LOG.debug("No new value for inactivity_probe, re-creation of " + "OVS Manager is not necessary") + return + + # Try to fetch Manager table as it is already exists and see if + # inactivity_probe is already the desired value + cmd = ['ovs-vsctl', '--timeout=%d' % timeout, '--id=@manager', + '--', 'get', 'Manager', man_uri, 'inactivity_probe'] + in_probe = processutils.execute(*cmd, log_errors=log_fail_as_error, + check_exit_code=True) + if in_probe[0].strip() == str(probe): + LOG.info("OVS Manager is already created and inactivity_probe " + "is set to %s.", in_probe[0].strip()) + return in_probe + cmd = ['ovs-vsctl', '--timeout=%d' % timeout, '--', 'set', + 'Manager', man_uri, 'inactivity_probe=%s' % probe] + processutils.execute(*cmd, log_errors=log_fail_as_error, + check_exit_code=True) + LOG.info("OVS Manager was set with new inactivity_probe " + "value %s.", probe) diff --git a/neutron/tests/functional/agent/ovsdb/native/test_helpers.py b/neutron/tests/functional/agent/ovsdb/native/test_helpers.py index 4d33f755a1e..76877965e7e 100644 --- a/neutron/tests/functional/agent/ovsdb/native/test_helpers.py +++ b/neutron/tests/functional/agent/ovsdb/native/test_helpers.py @@ -51,3 +51,29 @@ class EnableConnectionUriTestCase(base.BaseSudoTestCase): for connection in manager_connections: self.assertNotIn(connection, ovs.ovsdb.get_manager().execute()) + + def test_add_manager_overwrites_existing_manager(self): + ovs = ovs_lib.BaseOVS() + + _port = self.useFixture(port.ExclusivePort( + const.PROTO_NAME_TCP, + start=net_helpers.OVS_MANAGER_TEST_PORT_FIRST, + end=net_helpers.OVS_MANAGER_TEST_PORT_LAST)).port + ovsdb_cfg_connection = 'tcp:127.0.0.1:%s' % _port + manager_connection = 'ptcp:%s:127.0.0.1' % _port + + helpers.enable_connection_uri(ovsdb_cfg_connection, + inactivity_probe=10) + self.addCleanup(ovs.ovsdb.remove_manager(manager_connection).execute) + # First call of enable_connection_uri cretes the manager + # and the list returned by get_manager contains it: + my_mans = ovs.ovsdb.get_manager().execute() + self.assertIn(manager_connection, my_mans) + + # after 2nd call of enable_connection_uri with new value of + # inactivity_probe will keep the original manager only the + # inactivity_probe value is set: + helpers.enable_connection_uri(ovsdb_cfg_connection, + inactivity_probe=100) + my_mans = ovs.ovsdb.get_manager().execute() + self.assertIn(manager_connection, my_mans)