diff mbox series

[ovs-dev,v3,8/8] utilities: usdt-scripts: Retry on dp cache miss.

Message ID 20250227172340.4120887-9-amorenoz@redhat.com
State Changes Requested
Delegated to: aaron conole
Headers show
Series utilities: upcall_monitor: Monitor drops. | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/cirrus-robot success cirrus build: passed
ovsrobot/github-robot-_Build_and_Test success github build: passed

Commit Message

Adrián Moreno Feb. 27, 2025, 5:23 p.m. UTC
If the dp cache request misses, retry but only a couple of times so we
don't overload the system and only if we're not running on an externally
provided cache.

Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
---
 utilities/usdt-scripts/usdt_lib.py | 50 ++++++++++++++++++++----------
 1 file changed, 34 insertions(+), 16 deletions(-)

Comments

Eelco Chaudron March 11, 2025, 3:20 p.m. UTC | #1
On 27 Feb 2025, at 18:23, Adrian Moreno wrote:

> If the dp cache request misses, retry but only a couple of times so we
> don't overload the system and only if we're not running on an externally
> provided cache.
>
> Signed-off-by: Adrian Moreno <amorenoz@redhat.com>

The change looks good to me. We can always enhance this method if we encounter a situation where a longer trace requires multiple updates due to POD additions.

Acked-by: Eelco Chaudron <echaudro@redhat.com>
diff mbox series

Patch

diff --git a/utilities/usdt-scripts/usdt_lib.py b/utilities/usdt-scripts/usdt_lib.py
index d8659102f..335b586e8 100644
--- a/utilities/usdt-scripts/usdt_lib.py
+++ b/utilities/usdt-scripts/usdt_lib.py
@@ -19,43 +19,60 @@  import subprocess
 class DpPortMapping:
     """Class used to retrieve and cache datapath port numbers to port names."""
 
+    MAX_REQUESTS = 2
+
     def __init__(self):
         self.cache_map = None
+        self.n_requests = 0
 
     def get_map(self):
         """Get the cache map."""
-        if not self.cache_map:
-            self._get_mapping()
+        self._get_mapping()
 
         return self.cache_map
 
     def set_map(self, cache_map):
         """Override the internal cache map."""
         self.cache_map = cache_map
+        self.n_requests = self.MAX_REQUESTS
 
-    def get_port_name(self, dp, port_no):
-        """Get the port name from a port number."""
-        if self.cache_map is None:
-            self._get_mapping()
+    def _retry(self, func):
+        self._get_mapping()
 
-        if not self.cache_map.get(dp):
-            return None
+        result = func(self.cache_map)
 
-        for name, num in self.cache_map[dp].items():
-            if num == port_no:
-                return name
+        if not result:
+            self._get_mapping(refresh=True)
+            return func(self.cache_map)
 
-        return None
+        return result
+
+    def get_port_name(self, dp, port_no):
+        """Get the port name from a port number."""
+        def _get_port_name(cache_map):
+            if not cache_map.get(dp):
+                return None
+            for name, num in cache_map[dp].items():
+                if num == port_no:
+                    return name
+
+        return self._retry(_get_port_name)
 
     def get_port_number(self, dp, port):
         """Get the port number from a port name."""
-        if self.cache_map is None:
-            self._get_mapping()
+        def _get_port_number(cache_map):
+            return cache_map.get(dp, {}).get(port, None)
 
-        return self.cache_map.get(dp, {}).get(port, None)
+        return self._retry(_get_port_number)
 
-    def _get_mapping(self):
+    def _get_mapping(self, refresh=False):
         """Get the datapath port mapping from the running OVS."""
+        if self.n_requests >= self.MAX_REQUESTS \
+           or (self.cache_map and not refresh):
+            return
+
+        self.n_requests += 1
+
         try:
             output = subprocess.check_output(
                 ["ovs-appctl", "dpctl/show"], encoding="utf8"
@@ -82,3 +99,4 @@  class DpPortMapping:
                     self.cache_map[current_dp] = {
                         match.group(2): int(match.group(1))
                     }
+