-
Notifications
You must be signed in to change notification settings - Fork 0
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
Postgres fixes: Concurrency issue, connection leak, and fixes to guard CRUD operations #83
base: main
Are you sure you want to change the base?
Conversation
…en starting with more than one worker using postgres client)
compose.yml
Outdated
@@ -9,7 +9,7 @@ services: | |||
volumes: | |||
- ./postgres:/data/postgres | |||
ports: | |||
- "5432:5432" | |||
- "5932:5432" |
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.
Is this change intentional?
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.
good catch shouldn't be checked in
PGPORT: 5432 | ||
PGDATABASE: postgres | ||
PGHOST: postgres | ||
PGUSER: ${PGUSER:-postgres} | ||
PGPASSWORD: ${PGPASSWORD:-changeme} |
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.
I'm starting to think we should just have two different Dockfiles and compose files to run with PG vs config.py
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.
agreed 👌🏼
# Support for providing env vars as uvicorn does not support supplying args to create_app | ||
# - Usage: GR_CONFIG_FILE_PATH=config.py GR_ENV_FILE=.env PORT=8080 uvicorn --factory 'guardrails_api.app:create_app' --host 0.0.0.0 --port $PORT --workers 2 --timeout-keep-alive 90 | ||
# - Usage: gunicorn -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:$PORT --timeout=90 --workers=2 "guardrails_api.app:create_app(None, None, $PORT)" |
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.
That's rough. We'll have to address this in the OSS's cli as well since it calls this.
with engine.begin() as connection: | ||
lock_acquired = connection.execute(text(f"SELECT pg_try_advisory_lock({lock_id});")).scalar() | ||
if lock_acquired: | ||
self.run_initialization(connection) | ||
# Release the lock after initialization is complete | ||
connection.execute(text(f"SELECT pg_advisory_unlock({lock_id});")) | ||
|
||
def run_initialization(self, connection): | ||
# Perform the actual initialization tasks | ||
from guardrails_api.models import GuardItem, GuardItemAudit # noqa | ||
Base.metadata.create_all(bind=self.engine) | ||
|
||
# Execute custom SQL extensions and triggers | ||
connection.execute(text(INIT_EXTENSIONS)) | ||
connection.execute(text(AUDIT_FUNCTION)) | ||
connection.execute(text(AUDIT_TRIGGER)) |
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.
Good patch. We probably just need a real migration tool in the long run since sql-alchemy doesn't do updates.
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.
totally I believe they can be kicked off programatically as well so we can probably add this to lifecycle hooks
Pull Request Type
Link to Notion Task or Github Issue
Summary of Feature(s)
Summary of Bug Fix(es)
gunicorn
need to useuvicorn
workers (WSGI / ASGI incompatibility) due to the FastAPI switchdb = next(self.get_db())
was essentially causing connections to proliferate due to not being properly closed, so opted for contextmanager approachdb = next(self.get_db())
was causing some changes not to commit properly so deletions / updates weren't working properly due to inconsistent states during db.commitsPrevious Behaviour
UDATE
/DELETE
API CRUD operations were not workingNew Behaviour
UDATE
/DELETE
API CRUD operations workingOther details
util_
methods, these are utility functions which don't create db connections but rather take db sessions as arguments and perform auxiliary actions for the non util functions which are called directly by API controllers and do start db sessionsDependencies