[go: nahoru, domu]

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

Fix DB crash due to timeout #1031

Merged
merged 9 commits into from
Nov 7, 2023

Conversation

RichDom2185
Copy link
Member

Our grading summary uses a long, raw SQL query to generate the JSON directly, bypassing our ORM. This causes issues when we have hundreds of thousands of answers to query.

Lots of details are glossed over in this PR as they have already been discussed internally.

Changes:

  • Create DB index – improves read performance
  • Splits single SQL query into three – more generous time limit/less likely to time out
  • (Mostly) decouples M, V, C layers for grading summary
    • Create separate view layer for admin grading summary view
    • Removes unnecessary joins to dynamically generating based on view model – also reduces size of data to be passed from DB to backend layer
  • Migrates JSON view generation from DB layer to backend (view) layer – reduces DB total query time

While request-response delay is only slightly improved, DB query time is improved much more significantly, as a lot of the view generation logic is done in the backend layer now. The DB only provides the bare minimum amount of data necessary to generate this view.

Tested and JSON output is of the exact same structure compared to the old query.


CAVEAT

This PR will break Avenger backlog emails. Not just because I removed the "ungraded only" functionality (which was only added in #932 and used in emails), but while compile errors are fixed to match the new function signature, it will most definitely result in a runtime error (as the email templates are not updated to reflect the new structure.

The reason I think it's fine is:

  • Email notifications are not used anywhere yet

  • I really think Add Avenger Backlog Notifications #932 is the wrong approach to generate the emails:

    image

    all_submissions_by_grader_for_index generates a view (back then was still the JSON string), which is then converted back to a "model" using Jason.decode, which is then further manipulated, and the final view is generated using the email template.

    My proposal would be to create a separate function entirely for this functionality, which would optimise everything as well. But that is out of scope of this, and will come in a follow-up PR instead.

* Use subquery instead of join for answers
* Split main model query and view model generation
* Split queries into 3
* Use ORM where possible
Fixes compatibilty with new function signature.
@coveralls
Copy link
coveralls commented Nov 6, 2023

Coverage Status

coverage: 95.284% (-0.1%) from 95.408%
when pulling b51a598 on RichDom2185:fix-db-timeout
into ee70c48 on source-academy:master.

@RichDom2185 RichDom2185 enabled auto-merge (squash) November 6, 2023 21:13
Copy link
Contributor
@YaleChen299 YaleChen299 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

|> Enum.map(fn c -> String.to_atom(c) end)
|> Enum.zip(&1)
|> Enum.into(%{}))
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we put a default case here? in case of error

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think it was necessary as I don't think the SQL will ever error (unless the DB server itself is down, in which case it would give the same error messages as the ones we have been having recently).

What do you think?

@RichDom2185 RichDom2185 merged commit f46a9cd into source-academy:master Nov 7, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants