mirror of
https://github.com/openvswitch/ovs
synced 2025-08-22 01:51:26 +00:00
utilities: usdt-scripts: Retry on dp cache miss.
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> Signed-off-by: Eelco Chaudron <echaudro@redhat.com>
This commit is contained in:
parent
340c574e46
commit
ae6b778abb
@ -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 _retry(self, func):
|
||||
self._get_mapping()
|
||||
|
||||
result = func(self.cache_map)
|
||||
|
||||
if not result:
|
||||
self._get_mapping(refresh=True)
|
||||
return func(self.cache_map)
|
||||
|
||||
return result
|
||||
|
||||
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 _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
|
||||
|
||||
if not self.cache_map.get(dp):
|
||||
return None
|
||||
|
||||
for name, num in self.cache_map[dp].items():
|
||||
if num == port_no:
|
||||
return name
|
||||
|
||||
return None
|
||||
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))
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user