forked from ETS-Next-Gen/writing_observer
-
Notifications
You must be signed in to change notification settings - Fork 2
Extension vs Dashboard Load Balancing #135
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
Open
Brendon-Hablutzel
wants to merge
12
commits into
master
Choose a base branch
from
load-balancing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5ffad23
add functionality for static load balancing on the backend
Brendon-Hablutzel 7d9b163
switch from header to query param for load balancing
Brendon-Hablutzel c14d569
switch to sha256 for deterministic hashing
Brendon-Hablutzel 453f3f4
minor refactor to id hashing for load balancing
Brendon-Hablutzel ef7ec2b
fix typo in load balancing nginx config
Brendon-Hablutzel 095aa48
add hashed id to url as query param in extension
Brendon-Hablutzel a96f5b0
refactor load balancing to be between dashboard and extension requests
Brendon-Hablutzel 3a778c2
implement configurable route disabling on the backend, and clean up s…
Brendon-Hablutzel c20ff94
revert irrelevant changes
Brendon-Hablutzel 7511bbe
revert another minor but unncessary change (related to importing spac…
Brendon-Hablutzel b8eb482
remove unused port cli argument
Brendon-Hablutzel 23be1c9
create a bash script for running two instances of learning observer
Brendon-Hablutzel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Define upstream servers | ||
| upstream extension_server { server localhost:8888; } | ||
| upstream dashboard_server { server localhost:8889; } | ||
|
|
||
| map $arg_source $upstream_server { | ||
| default dashboard_server; # default if no match or query param is absent | ||
| "extension" extension_server; | ||
| "dashboard" dashboard_server; | ||
| } | ||
|
|
||
| server { | ||
| listen 80; | ||
|
|
||
| location / { | ||
| proxy_set_header Upgrade $http_upgrade; | ||
| proxy_set_header Connection "upgrade"; | ||
|
|
||
| # Pass original host to the upstream server | ||
| proxy_set_header Host $host; | ||
|
|
||
| # Other WebSocket headers | ||
| proxy_http_version 1.1; | ||
| proxy_set_header X-Real-IP $remote_addr; | ||
| proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
| proxy_set_header X-Forwarded-Proto $scheme; | ||
|
|
||
| # Route the request to the selected upstream server | ||
| proxy_pass http://$upstream_server; | ||
| proxy_read_timeout 3600; | ||
| proxy_send_timeout 3600; | ||
| } | ||
| } | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # NGINX Setup and Config | ||
|
|
||
| This document contains basic info related to using nginx in the context of writing observer. | ||
|
|
||
| ## Static Routing Use Case Config | ||
|
|
||
| In order to improve event processing efficiency, we will use nginx to route requests to different processes based on whether they are incoming data events (from the extension), or requests for data (from the dashboard). The associated config can be found at `/devops/nginx-static-loadbalance.conf` in this repository. | ||
|
|
||
| At the top of that file, the `upstream`s indicate the servers that should be used for each type of request, `dashboard` or `extension`. | ||
|
|
||
| The next section, which includes the `map` directive, tells nginx to look for a url query parameter called source (`$arg_source`: `$arg` indicates that it is a url query param, and `source` gives the name). If `source=dashboard`, then it routes to the dashboard server, if `source=extension`, it routes to the extension server, and if no `source` is provided, then it defaults to the dashboard server. | ||
|
|
||
| Below, in the `server` block, are general instructions for handling proxying. | ||
|
|
||
| ## Usage | ||
|
|
||
| To deploy nginx, take the appropriate `.conf` file and put it in `/etc/nginx/sites-enabled`. Then, you can use `sudo nginx -t` to validate the config and ensure it is correct--this will print any detected syntax errors in the config files. Finally, restart nginx to apply the new config (for example, with `sudo systemctl restart nginx`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| import getpass | ||
| import os | ||
| import pmss | ||
| import secrets | ||
| import sys | ||
|
|
||
|
|
@@ -33,6 +34,20 @@ | |
|
|
||
| from learning_observer.utility_handlers import * | ||
|
|
||
| pmss.register_field( | ||
| name='disable_extension_routes', | ||
| type=pmss.pmsstypes.TYPES.boolean, | ||
| description='Whether to disable extension-related API routes', | ||
| default=False | ||
| ) | ||
|
|
||
| pmss.register_field( | ||
| name='disable_dashboard_routes', | ||
| type=pmss.pmsstypes.TYPES.boolean, | ||
| description='Whether to disable dashboard-related API routes', | ||
| default=False | ||
| ) | ||
|
|
||
|
|
||
| def add_routes(app): | ||
| ''' | ||
|
|
@@ -62,9 +77,20 @@ def tracemalloc_handler(request): | |
| aiohttp.web.get('/debug/tracemalloc/', tracemalloc_handler), | ||
| ]) | ||
|
|
||
| register_dashboard_api(app) | ||
| if not settings.pmss_settings.disable_dashboard_routes(types=['server']): | ||
| register_dashboard_api(app) | ||
| debug_log("Dashbord routes are enabled") | ||
| else: | ||
| debug_log("Dashboard routes are disabled") | ||
|
|
||
| register_static_routes(app) | ||
| register_incoming_event_views(app) | ||
|
|
||
| if not settings.pmss_settings.disable_extension_routes(types=['server']): | ||
| register_incoming_event_views(app) | ||
| debug_log("Extension routes are enabled") | ||
| else: | ||
| debug_log("Extension routes are disabled") | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. General comment: I could see instances where we want to enable/disable a lot more of the Learning Observer routes on the system. |
||
| register_debug_routes(app) | ||
| learning_observer.google.initialize_and_register_routes(app) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #!/usr/bin/env bash | ||
| # =============================== | ||
| # This a modified version RunLearningObserver.sh that automatically starts two processes, | ||
| # each with a different creds.yaml ('creds-a.yaml' and 'creds-b.yaml')--this should be | ||
| # used for static routing, where one creds.yaml has only dashboard routes enabled, | ||
| # and the other has only extension routes enabled. Note that these creds.yaml should | ||
| # specify different ports. | ||
| # | ||
| # This bash script provides a simple wrapper to run the | ||
| # learning observer service and pipe the data to a logfile | ||
| # over time this should be integrated into the systemd | ||
| # service process. This uses static variables to specify | ||
| # the location of the virtualenv and the command and | ||
| # specifies the location for the running logfile. | ||
|
|
||
| # System Variables | ||
| # -------------------------------------- | ||
| VIRTUALENV_PATH="/usr/local/share/projects/WritingObserver/VirtualENVs/WOvenv" | ||
| #VIRTUALENV_PYTHON="/usr/local/share/Projects/WritingObserver/VirtualENVs/learning_observer/bin/python3.9" | ||
| LEARNING_OBSERVER_LOC="/usr/local/share/projects/WritingObserver/Repositories/ArgLab_writing_observer/learning_observer" | ||
| LOGFILE_DEST="/usr/local/share/projects/WritingObserver/Repositories/ArgLab_writing_observer/learning_observer/learning_observer/logs" | ||
|
|
||
| # Make the logfile name | ||
| # --------------------------------------- | ||
| LOG_DATE=$(date "+%m-%d-%Y--%H-%M-%S") | ||
| LOGFILE_NAME="$LOGFILE_DEST/learning_observer_service_$LOG_DATE.log" | ||
| echo $LOG_NAME; | ||
|
|
||
|
|
||
| # Run both processes | ||
| # -------------------------------------- | ||
| echo "Running Learning Observer Service..." | ||
| cd $LEARNING_OBSERVER_LOC | ||
| source $VIRTUALENV_PATH/bin/activate | ||
| nohup python learning_observer --config-file=creds-a.yaml > $LOGFILE_NAME 2>&1 & | ||
| PROCESS_ID=$! | ||
| echo $PROCESS_ID > $LOGFILE_DEST/run.pid | ||
|
|
||
| # NOTE: if this should go to separate log file location, modify here | ||
| nohup python learning_observer --config-file=creds-b.yaml > $LOGFILE_NAME 2>&1 & | ||
| PROCESS_ID=$! | ||
| echo $PROCESS_ID > $LOGFILE_DEST/run.pid | ||
|
|
||
| # Set the number of allowed open files to something large 8192 | ||
| prlimit --pid $PROCESS_ID --nofile=8192 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We really ought to have the url not hardcoded and pulled from a config of some sort, but that's way beyond the scope of this PR and the work you are doing.
Mostly leaving this note to push it to the front of my brain (I'm most likely to work on it)