Skip to content

Commit 56aa539

Browse files
authored
[mux simulaotr] Improve mux simulator toggle performance (#16508)
What is the motivation for this PR? Use thread-pool to parallel run the mux toggles. This code is from PR: #16164, which is reverted. Let's have the change here. Signed-off-by: Longxiang [email protected] How did you do it? As the motivation. How did you verify/test it? Run on dualtor/dualtor-120 testbed. Signed-off-by: Longxiang <[email protected]>
1 parent 4181dbc commit 56aa539

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

ansible/roles/vm_set/files/mux_simulator.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
import traceback
1515
import time
1616

17+
if sys.version_info.major == 2:
18+
from multiprocessing.pool import ThreadPool
19+
else:
20+
from concurrent.futures import ThreadPoolExecutor as ThreadPool
21+
1722
from collections import defaultdict
1823
from logging.handlers import RotatingFileHandler
1924

@@ -559,9 +564,12 @@ def clear_flap_counter(self):
559564

560565
class Muxes(object):
561566

567+
MUXES_CONCURRENCY = 4
568+
562569
def __init__(self, vm_set):
563570
self.vm_set = vm_set
564571
self.muxes = {}
572+
self.thread_pool = ThreadPool(Muxes.MUXES_CONCURRENCY)
565573
for bridge in self._mux_bridges():
566574
bridge_fields = bridge.split('-')
567575
port_index = int(bridge_fields[-1])
@@ -596,7 +604,8 @@ def set_active_side(self, new_active_side, port_index=None):
596604
mux.set_active_side(new_active_side)
597605
return mux.status
598606
else:
599-
[mux.set_active_side(new_active_side) for mux in self.muxes.values()]
607+
list(self.thread_pool.map(lambda args: Mux.set_active_side(*args),
608+
[(mux, new_active_side) for mux in self.muxes.values()]))
600609
return {mux.bridge: mux.status for mux in self.muxes.values()}
601610

602611
def update_flows(self, new_action, out_sides, port_index=None):
@@ -605,7 +614,8 @@ def update_flows(self, new_action, out_sides, port_index=None):
605614
mux.update_flows(new_action, out_sides)
606615
return mux.status
607616
else:
608-
[mux.update_flows(new_action, out_sides) for mux in self.muxes.values()]
617+
list(self.thread_pool.map(lambda args: Mux.update_flows(*args),
618+
[(mux, new_action, out_sides) for mux in self.muxes.values()]))
609619
return {mux.bridge: mux.status for mux in self.muxes.values()}
610620

611621
def reset_flows(self, port_index=None):

0 commit comments

Comments
 (0)