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

PHP DB code: clean up the logic, and allow for > 1 readonly replica #5697

Merged
merged 4 commits into from
Aug 31, 2024

Conversation

davidpanderson
Copy link
Contributor

Also always use mysqli interface

@brevilo
Copy link
Contributor

brevilo commented Jul 19, 2024

Also always use mysqli interface

Please, use atomic commits so that one can review separate topics separately.

html/inc/boinc_db.inc Outdated Show resolved Hide resolved
html/inc/boinc_db.inc Outdated Show resolved Hide resolved
html/inc/boinc_db.inc Outdated Show resolved Hide resolved
@brevilo
Copy link
Contributor

brevilo commented Jul 19, 2024

Apart from the above I like the approach. Flexible while not overly complex.

Remove the 'use $db_name' query; don't need that with mysqli
@davidpanderson
Copy link
Contributor Author

I removed the 'use db_name' query (not needed with mysqli)
and made the default 'localhost' explicit.

@brevilo
Copy link
Contributor

brevilo commented Jul 19, 2024

@bema-aei should ultimately review this as well (I can't add him).

@bema-aei
Copy link
Contributor

bema-aei commented Jul 31, 2024

This works fro us in principle, with the change I mentioned above. I updated my original branch with your changes, with two important changes:

  • including a change that affects the whole web code ("use mysqli everywhere") in another commit with very limited scope and not even mentioning it in the commit message is just asking for trouble. It's difficult to review and difficult to test. Worst of all, if someone of some project later finds it to cause trouble for him, he is unlikely to ever find the commit that breaks his project. So I split your commit in two (leaving the author intact).
  • I added the commit I mentioned above that is required to make this work for us. It allows to address multiple DB hosts in sequence without changing too much code.
  • I also changed the debug output to suit my needs, then reverted it. You can re-add it by reverting the 'Revert' commit if needed.

Please have a look at that branch.

- a close() method, closes the connection
- a $dbnum member: which replica you're connected to (0 if main DB)

Also, BoincDb doesn't inherit DbConn.
Copy link

codecov bot commented Aug 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 10.53%. Comparing base (f2be5af) to head (da1727c).
Report is 161 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #5697      +/-   ##
============================================
- Coverage     10.53%   10.53%   -0.01%     
  Complexity     1068     1068              
============================================
  Files           279      279              
  Lines         35874    35891      +17     
  Branches       8412     8417       +5     
============================================
  Hits           3780     3780              
- Misses        31700    31717      +17     
  Partials        394      394              

see 8 files with indirect coverage changes

@AenBleidd
Copy link
Member

@bema-aei, @brevilo, are you ok with this PR? Does it need anything else (like testing on your side) or we can move forward and merge it?

@AenBleidd
Copy link
Member

@bema-aei, @brevilo, could you please take a look at this PR and test it?
Thank you in advance.

@bema-aei
Copy link
Contributor

First of all I like the idea of allowing an arbitrary number of replicas. And thank you for implementing a close() function.

This code in get() is still unchanged, i.e. if $instance is set, get() will return the same instance even if $dbnum is different.

At least this doesn't correspond to what I proposed in my branch. Is the "user" of this code required to close one connection / instance before opening another? If so, it should be noted somewhere, I didn't expect that.

@bema-aei
Copy link
Contributor

Also too I there is an uncaught exception if the DB connection is refused for some reason. I get:

PHP Fatal error:  Uncaught mysqli_sql_exception: Connection refused in /srv/BOINC/live-webcode-test/html/inc/db_conn.inc:37
Stack trace:
#0 /srv/BOINC/live-webcode-test/html/inc/db_conn.inc(37): mysqli->__construct()
#1 /srv/BOINC/live-webcode-test/html/inc/boinc_db.inc(65): DbConn->init_conn()
#2 /srv/BOINC/live-webcode-test/html/inc/boinc_db.inc(117): BoincDb::get_aux()
#3 /srv/BOINC/live-webcode-test/html/ops/eah_server_status.php(681): BoincDb::get()
#4 /srv/BOINC/live-webcode-test/html/ops/eah_server_status.php(1108): seconds_behind_master()
#5 {main}
  thrown in /srv/BOINC/live-webcode-test/html/inc/db_conn.inc on line 37

@davidpanderson
Copy link
Contributor Author

  • I added the check for different dbnum in get()
  • Yes, you can only use on DB at a time
  • An exception would be thrown if had previously called
    mysqli_report(MYSQLI_REPORT_STRICT);
    presumably because you want to handle exceptions.
    But in any case I added a try/catch.

if open to different dbnum, close and reconnect.
Don't throw exception if connect fails
@bema-aei
Copy link
Contributor

Thanks, this works for us technically. We do use a recent MariaDB, which has some defaults different from MySQL, hence the exception.

However, for reasons I pointed out already I can't really approve the first commit which combines different features w/o even mentioning in the commit message, so I can't recommend to merge this branch as it is.

@brevilo
Copy link
Contributor

brevilo commented Aug 30, 2024

We do use a recent MariaDB, which has some defaults different from MySQL, hence the exception.

How does a PHP module's (mysqli) config relate to the MySQL flavor used as the DB backend?

@bema-aei
Copy link
Contributor

We do use a recent MariaDB, which has some defaults different from MySQL, hence the exception.

How does a PHP module's (mysqli) config relate to the MySQL flavor used as the DB backend?

Oh. Sorry, was mislead. Indeed, that's the mysqli config.

@davidpanderson davidpanderson merged commit 66440f9 into master Aug 31, 2024
146 checks passed
@davidpanderson davidpanderson deleted the dpa_replica branch August 31, 2024 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants