mirror of
https://github.com/openvswitch/ovs
synced 2025-08-22 09:58:01 +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 DpPortMapping:
|
||||||
"""Class used to retrieve and cache datapath port numbers to port names."""
|
"""Class used to retrieve and cache datapath port numbers to port names."""
|
||||||
|
|
||||||
|
MAX_REQUESTS = 2
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.cache_map = None
|
self.cache_map = None
|
||||||
|
self.n_requests = 0
|
||||||
|
|
||||||
def get_map(self):
|
def get_map(self):
|
||||||
"""Get the cache map."""
|
"""Get the cache map."""
|
||||||
if not self.cache_map:
|
self._get_mapping()
|
||||||
self._get_mapping()
|
|
||||||
|
|
||||||
return self.cache_map
|
return self.cache_map
|
||||||
|
|
||||||
def set_map(self, cache_map):
|
def set_map(self, cache_map):
|
||||||
"""Override the internal cache map."""
|
"""Override the internal cache map."""
|
||||||
self.cache_map = 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):
|
def get_port_name(self, dp, port_no):
|
||||||
"""Get the port name from a port number."""
|
"""Get the port name from a port number."""
|
||||||
if self.cache_map is None:
|
def _get_port_name(cache_map):
|
||||||
self._get_mapping()
|
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 self._retry(_get_port_name)
|
||||||
return None
|
|
||||||
|
|
||||||
for name, num in self.cache_map[dp].items():
|
|
||||||
if num == port_no:
|
|
||||||
return name
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_port_number(self, dp, port):
|
def get_port_number(self, dp, port):
|
||||||
"""Get the port number from a port name."""
|
"""Get the port number from a port name."""
|
||||||
if self.cache_map is None:
|
def _get_port_number(cache_map):
|
||||||
self._get_mapping()
|
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."""
|
"""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:
|
try:
|
||||||
output = subprocess.check_output(
|
output = subprocess.check_output(
|
||||||
["ovs-appctl", "dpctl/show"], encoding="utf8"
|
["ovs-appctl", "dpctl/show"], encoding="utf8"
|
||||||
@ -82,3 +99,4 @@ class DpPortMapping:
|
|||||||
self.cache_map[current_dp] = {
|
self.cache_map[current_dp] = {
|
||||||
match.group(2): int(match.group(1))
|
match.group(2): int(match.group(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user