From 509332e9ccbfa6894a4cbae560680b6e9e708924 Mon Sep 17 00:00:00 2001 From: TLSM Date: Tue, 7 Feb 2023 05:56:01 -0500 Subject: [PATCH 1/2] Fix comment eager loading Following #485, we began investigating post/comment rendering bottlenecks. The most immediate issue is the eager comment loading (merged in 23a8fb966385) did not seem fully operative: query logs showed comments and associated FKs were being lazy loaded again (linear query quantity in number of rendered comments). In fact, CPU load seemed even worse than previous lazy loading. Bisect revealed first bad commit: fb77cbcc2b5 which fixed post view counters by committing the SQLAlchemy session instead of flushing, following upstream's fix. However, committing a session has the unfortunate side effect of dumping cached session objects, such as the previously loaded comment objects and their relationships, causing fallback to the old lazy behavior. We fix this here by explicitly telling SQLAlchemy to not expire the session on commit. Hopefully this will simultaneously resolve the elevated DB CPU load observed in production and speed up page rendering again. --- files/routes/posts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/files/routes/posts.py b/files/routes/posts.py index 8f05f0bc5..e72bb9319 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -235,6 +235,7 @@ def post_id(pid, anything=None, v=None, sub=None): post.replies = get_comment_trees_eager(top_comment_ids, sort, v) post.views += 1 + g.db.expire_on_commit = False g.db.add(post) g.db.commit() From 43f329badf4756e05c00937053a8ddbbac9dd030 Mon Sep 17 00:00:00 2001 From: TLSM Date: Tue, 7 Feb 2023 07:04:02 -0500 Subject: [PATCH 2/2] Fix Session expiry for tests This is likely not an issue for production (since each request will get its own SQLAlchemy session), but `scoped_session` results in the tests reuseing the same Session across tests. The tests rely on the default session expiry behavior. --- files/routes/posts.py | 1 + 1 file changed, 1 insertion(+) diff --git a/files/routes/posts.py b/files/routes/posts.py index e72bb9319..03d97ee19 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -238,6 +238,7 @@ def post_id(pid, anything=None, v=None, sub=None): g.db.expire_on_commit = False g.db.add(post) g.db.commit() + g.db.expire_on_commit = True if request.headers.get("Authorization"): return post.json else: