Browse Source

Merge "Target proper Chassis events for agent liveness"

tags/6.0.0.0b1
Zuul 3 months ago
parent
commit
4f690e1b23

+ 66
- 26
networking_ovn/ovsdb/ovsdb_monitor.py View File

@@ -12,6 +12,8 @@
12 12
 #    License for the specific language governing permissions and limitations
13 13
 #    under the License.
14 14
 
15
+import abc
16
+
15 17
 from neutron_lib.plugins import constants
16 18
 from neutron_lib.plugins import directory
17 19
 from neutron_lib.utils import helpers
@@ -30,34 +32,70 @@ from networking_ovn.common import utils
30 32
 LOG = log.getLogger(__name__)
31 33
 
32 34
 
33
-class ChassisAgentEvent(row_event.RowEvent):
35
+class BaseEvent(row_event.RowEvent):
36
+    table = None
37
+    events = tuple()
38
+
34 39
     def __init__(self):
35
-        table = 'Chassis'
36
-        events = (self.ROW_CREATE, self.ROW_UPDATE, self.ROW_DELETE)
37
-        super(ChassisAgentEvent, self).__init__(events, table, None)
38
-        self.event_name = 'ChassisAgentEvent'
40
+        self.event_name = self.__class__.__name__
41
+        super(BaseEvent, self).__init__(self.events, self.table, None)
42
+
43
+    @abc.abstractmethod
44
+    def match_fn(self, event, row, old=None):
45
+        """Define match criteria other than table/event"""
46
+
47
+    def matches(self, event, row, old=None):
48
+        if row._table.name != self.table or event not in self.events:
49
+            return False
50
+        if not self.match_fn(event, row, old):
51
+            return False
52
+        LOG.debug("%s : Matched %s, %s, %s %s", self.event_name, self.table,
53
+                  event, self.conditions, self.old_conditions)
54
+        return True
55
+
56
+
57
+class ChassisAgentDeleteEvent(BaseEvent):
58
+    table = 'Chassis'
59
+    events = (BaseEvent.ROW_DELETE,)
39 60
 
40 61
     def run(self, event, row, old):
41
-        if event != self.ROW_DELETE:
42
-            stats.AgentStats.add_stat(row.uuid, row.nb_cfg)
43
-
44
-            # Update the metadata agent stats
45
-            metadata_nb_cfg = row.external_ids.get(
46
-                ovn_const.OVN_AGENT_METADATA_SB_CFG_KEY, None)
47
-            if event == self.ROW_UPDATE and metadata_nb_cfg:
48
-                try:
49
-                    old_metadata_nb_cfg = old.external_ids.get(
50
-                        ovn_const.OVN_AGENT_METADATA_SB_CFG_KEY, None)
51
-                except AttributeError:
52
-                    return
53
-
54
-                if metadata_nb_cfg != old_metadata_nb_cfg:
55
-                    stats.AgentStats.add_stat(
56
-                        utils.ovn_metadata_name(row.uuid),
57
-                        int(metadata_nb_cfg))
58
-        else:
59
-            stats.AgentStats.del_agent(row.uuid)
60
-            stats.AgentStats.del_agent(utils.ovn_metadata_name(row.uuid))
62
+        stats.AgentStats.del_agent(row.uuid)
63
+        stats.AgentStats.del_agent(utils.ovn_metadata_name(row.uuid))
64
+
65
+    def match_fn(self, event, row, old=None):
66
+        return True
67
+
68
+
69
+class ChassisGatewayAgentEvent(BaseEvent):
70
+    table = 'Chassis'
71
+    events = (BaseEvent.ROW_CREATE, BaseEvent.ROW_UPDATE)
72
+
73
+    def match_fn(self, event, row, old=None):
74
+        return event == self.ROW_CREATE or getattr(old, 'nb_cfg', False)
75
+
76
+    def run(self, event, row, old):
77
+        stats.AgentStats.add_stat(row.uuid, row.nb_cfg)
78
+
79
+
80
+class ChassisMetadataAgentEvent(BaseEvent):
81
+    table = 'Chassis'
82
+    events = (BaseEvent.ROW_CREATE, BaseEvent.ROW_UPDATE)
83
+
84
+    @staticmethod
85
+    def _metadata_nb_cfg(row):
86
+        return int(row.external_ids[ovn_const.OVN_AGENT_METADATA_SB_CFG_KEY])
87
+
88
+    def match_fn(self, event, row, old=None):
89
+        if event == self.ROW_CREATE:
90
+            return True
91
+        try:
92
+            return self._metadata_nb_cfg(row) != self._metadata_nb_cfg(old)
93
+        except (AttributeError, KeyError):
94
+            return False
95
+
96
+    def run(self, event, row, old):
97
+        stats.AgentStats.add_stat(utils.ovn_metadata_name(row.uuid),
98
+                                  self._metadata_nb_cfg(row))
61 99
 
62 100
 
63 101
 class ChassisEvent(row_event.RowEvent):
@@ -237,7 +275,9 @@ class BaseOvnSbIdl(connection.OvsdbIdl):
237 275
     def __init__(self, remote, schema):
238 276
         super(BaseOvnSbIdl, self).__init__(remote, schema)
239 277
         self.notify_handler = event.RowEventHandler()
240
-        self.notify_handler.watch_event(ChassisAgentEvent())
278
+        self.notify_handler.watch_events([
279
+            ChassisAgentDeleteEvent(), ChassisMetadataAgentEvent(),
280
+            ChassisGatewayAgentEvent()])
241 281
 
242 282
     @classmethod
243 283
     def from_server(cls, connection_string, schema_name):

+ 15
- 0
networking_ovn/tests/functional/test_metadata_agent.py View File

@@ -23,6 +23,7 @@ from ovsdbapp import event as ovsdb_event
23 23
 from networking_ovn.agent.metadata import agent
24 24
 from networking_ovn.agent.metadata import ovsdb
25 25
 from networking_ovn.agent.metadata import server as metadata_server
26
+from networking_ovn.agent import stats
26 27
 from networking_ovn.common import constants as ovn_const
27 28
 from networking_ovn.conf.agent.metadata import config as meta
28 29
 from networking_ovn.tests.functional import base
@@ -121,3 +122,17 @@ class TestMetadataAgent(base.TestOVNFunctionalBase):
121 122
         # chassis with the nb_cfg, 2 revisions, one for the network transaction
122 123
         # and another one for the port
123 124
         self.assertTrue(row_event.wait())
125
+
126
+    def test_updating_metadata_doesnt_update_controller_stats(self):
127
+        chassis = self.sb_api.lookup('Chassis', self.chassis_name)
128
+        self.assertNotIn(ovn_const.OVN_AGENT_METADATA_SB_CFG_KEY,
129
+                         chassis.external_ids)
130
+        nb_cfg = stats.AgentStats.get_stat(chassis.uuid).nb_cfg
131
+        new_nb_cfg = nb_cfg + 1
132
+        row_event = MetadataAgentHealthEvent(chassis.name, new_nb_cfg)
133
+        self.handler.watch_event(row_event)
134
+        self.sb_api.update_metadata_health_status(
135
+            chassis.name, new_nb_cfg).execute(check_error=True)
136
+        self.assertTrue(row_event.wait())
137
+        self.assertEqual(stats.AgentStats.get_stat(chassis.uuid).nb_cfg,
138
+                         nb_cfg)

Loading…
Cancel
Save