Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dualtor] Improve mux_simulator #16164

Merged
merged 3 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 52 additions & 24 deletions ansible/roles/vm_set/files/mux_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
import traceback
import time

if sys.version_info.major == 2:
from multiprocessing.pool import ThreadPool
else:
from concurrent.futures import ThreadPoolExecutor as ThreadPool

from collections import defaultdict
from logging.handlers import RotatingFileHandler

Expand Down Expand Up @@ -51,6 +56,19 @@
unicode = str


MUX_SIMULATOR_LOGO = [
'',
'## ## ## ## ## ## ###### #### ## ## ## ## ## ### ######## ####### ######## ', # noqa E501
'### ### ## ## ## ## ## ## ## ### ### ## ## ## ## ## ## ## ## ## ## ', # noqa E501
'#### #### ## ## ## ## ## ## #### #### ## ## ## ## ## ## ## ## ## ## ', # noqa E501
'## ### ## ## ## ### ###### ## ## ### ## ## ## ## ## ## ## ## ## ######## ', # noqa E501
'## ## ## ## ## ## ## ## ## ## ## ## ## ######### ## ## ## ## ## ', # noqa E501
'## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ', # noqa E501
'## ## ####### ## ## ###### #### ## ## ####### ######## ## ## ## ####### ## ## ', # noqa E501
'',
]


# ============================================ Error Handlers ============================================ #

@app.errorhandler(Exception)
Expand Down Expand Up @@ -559,9 +577,12 @@ def clear_flap_counter(self):

class Muxes(object):

MUXES_CONCURRENCY = 4

def __init__(self, vm_set):
self.vm_set = vm_set
self.muxes = {}
self.thread_pool = ThreadPool(Muxes.MUXES_CONCURRENCY)
for bridge in self._mux_bridges():
bridge_fields = bridge.split('-')
port_index = int(bridge_fields[-1])
Expand Down Expand Up @@ -596,7 +617,8 @@ def set_active_side(self, new_active_side, port_index=None):
mux.set_active_side(new_active_side)
return mux.status
else:
[mux.set_active_side(new_active_side) for mux in self.muxes.values()]
list(self.thread_pool.map(lambda args: Mux.set_active_side(*args),
[(mux, new_active_side) for mux in self.muxes.values()]))
return {mux.bridge: mux.status for mux in self.muxes.values()}

def update_flows(self, new_action, out_sides, port_index=None):
Expand All @@ -605,7 +627,8 @@ def update_flows(self, new_action, out_sides, port_index=None):
mux.update_flows(new_action, out_sides)
return mux.status
else:
[mux.update_flows(new_action, out_sides) for mux in self.muxes.values()]
list(self.thread_pool.map(lambda args: Mux.update_flows(*args),
[(mux, new_action, out_sides) for mux in self.muxes.values()]))
return {mux.bridge: mux.status for mux in self.muxes.values()}

def reset_flows(self, port_index=None):
Expand Down Expand Up @@ -931,6 +954,21 @@ def log_message(vm_set):
return {"success": True}


def setup_mux_simulator(http_port, vm_set, verbose):
if verbose == 1:
app.logger.setLevel(logging.DEBUG)
app.config['VERBOSE'] = True
else:
app.logger.setLevel(logging.INFO)
app.config['VERBOSE'] = False

config_logging(http_port)
app.logger.info('\n'.join(MUX_SIMULATOR_LOGO))
app.logger.info('Starting server on port {}'.format(http_port))
create_muxes(vm_set)
app.logger.info('####################### STARTING HTTP SERVER #######################')


if __name__ == '__main__':
usage = '\n'.join([
'Start mux simulator server at specified port:',
Expand All @@ -943,29 +981,19 @@ def log_message(vm_set):

http_port = sys.argv[1]
arg_vm_set = sys.argv[2]
verbose = ('-v' in sys.argv)

if '-v' in sys.argv:
app.logger.setLevel(logging.DEBUG)
app.config['VERBOSE'] = True
else:
app.logger.setLevel(logging.INFO)
app.config['VERBOSE'] = False
setup_mux_simulator(http_port, arg_vm_set, verbose)

config_logging(http_port)
MUX_LOGO = '\n'.join([
'',
'## ## ## ## ## ## ###### #### ## ## ## ## ## ### ######## ####### ######## ', # noqa E501
'### ### ## ## ## ## ## ## ## ### ### ## ## ## ## ## ## ## ## ## ## ', # noqa E501
'#### #### ## ## ## ## ## ## #### #### ## ## ## ## ## ## ## ## ## ## ', # noqa E501
'## ### ## ## ## ### ###### ## ## ### ## ## ## ## ## ## ## ## ## ######## ', # noqa E501
'## ## ## ## ## ## ## ## ## ## ## ## ## ######### ## ## ## ## ## ', # noqa E501
'## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ', # noqa E501
'## ## ####### ## ## ###### #### ## ## ####### ######## ## ## ## ####### ## ## ', # noqa E501
'',
])
app.logger.info(MUX_LOGO)
app.logger.info('Starting server on port {}'.format(sys.argv[1]))
create_muxes(arg_vm_set)
app.logger.info('####################### STARTING HTTP SERVER #######################')
socket.setdefaulttimeout(60)
app.run(host='0.0.0.0', port=http_port, threaded=True) # nosemgrep
else:
http_port = os.environ.get("MUX_SIMULATOR_HTTP_PORT")
arg_vm_set = os.environ.get("MUX_SIMULATOR_VM_SET")
if http_port is None:
raise RuntimeError("No http port is provided.")
if arg_vm_set is None:
raise RuntimeError("No VM set is provided.")
verbose = int(os.environ.get("MUX_SIMULATOR_VERBOSITY", "1"))

setup_mux_simulator(http_port, arg_vm_set, verbose)
18 changes: 18 additions & 0 deletions ansible/roles/vm_set/tasks/control_mux_simulator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
set_fact:
flask_version: "1.1.2"
werkzeug_version: "1.0.1"
gunicorn_version: "19.10.0"
python_command: "python"
futures_version: "3.4.0"

- name: Use newer Flask version for pip3
set_fact:
flask_version: "2.3.3"
python_command: "python3"
gunicorn_version: "23.0.0"
when: pip_executable == "pip3"

- name: Use newer Werkzeug version for pip3
Expand All @@ -39,6 +42,21 @@
become: yes
environment: "{{ proxy_env | default({}) }}"

- name: Install gunicorn
pip: name=gunicorn version={{ gunicorn_version }} state=forcereinstall executable={{ pip_executable }}
lolyu marked this conversation as resolved.
Show resolved Hide resolved
become: yes
environment: "{{ proxy_env | default({}) }}"

- name: Install futures
pip: name=futures version={{ futures_version }} state=forcereinstall executable={{ pip_executable }}
become: yes
environment: "{{ proxy_env | default({}) }}"
when: pip_executable == "pip"

- name: Increase backlog
shell: sysctl -w net.core.somaxconn=1024
become: true

- name: Copy the mux simulator to test server
copy:
src: mux_simulator.py
Expand Down
2 changes: 1 addition & 1 deletion ansible/roles/vm_set/templates/mux-simulator.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Description=mux simulator
After=network.target

[Service]
ExecStart=/usr/bin/env {{python_command}} {{ abs_root_path }}/mux_simulator_{{ mux_simulator_port }}.py {{ mux_simulator_port }} {{ vm_set_name }} -v
ExecStart=/usr/local/bin/gunicorn --timeout 60 --preload --bind 0.0.0.0:{{ mux_simulator_port }} --chdir {{ abs_root_path }} mux_simulator_{{ mux_simulator_port }}:app -w 1 --threads 8 --env MUX_SIMULATOR_HTTP_PORT={{ mux_simulator_port }} --env MUX_SIMULATOR_VM_SET={{ vm_set_name }} --env MUX_SIMULATOR_VERBOSITY=1
Loading