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

refactor!: Database #16961

Merged
merged 93 commits into from
Jul 22, 2022
Merged

Conversation

gavindsouza
Copy link
Collaborator

@gavindsouza gavindsouza commented May 23, 2022

Changes

  • Database Pooling
  • Client change from pymysql to mariadb
  • Database Refactor
    • Separate Connection, Helper & Transformation layers
    • Optimize SQL execution & logging - execution of SQL faster by 30%, introduced LazyDecode, LazyMogrify methods
    • Remove dependency of non-core utils over third-party interfaces
    • Support read_only connections via Database class
  • CI
    • MariaDB 10.6
    • Faster fails on conflicts
    • Consolidate system dependency setup scripts
    • Skip Cypress recorder tests [don't test anything of value it seems lol] (w @ankush)
  • Standardize query type checking with optimized is_query_type util
  • Remove redundant/dead code
  • Add verbosity for commands
  • Better typing support on core DB APIs
  • Allow for_update via frappe.get_last_doc

Note: The client change & pooling were removed via 006ebcb due to the added instability of MariaDB client + managing the additional system dependency. I will test and update the client once the new client reaches the complete required parity + stability. Once, we're there it's just as simple as reverting the mentioned commit to switch over :D

@gavindsouza gavindsouza force-pushed the mariadb-client-refactor branch 3 times, most recently from 6a0238e to 0baac56 Compare May 23, 2022 13:26
@ankush ankush linked an issue May 23, 2022 that may be closed by this pull request
@gavindsouza

This comment was marked as outdated.

@gavindsouza gavindsouza linked an issue May 24, 2022 that may be closed by this pull request
Move to use MariaDB's official Python client written in C instead of
the PyMySQL library. This change doesn't rid Frappe of the PyMySQL
library. Instead, it continues to utilize it for the ER module and
converter methods until the MariaDB library adds support for the same.

Ticket: https://jira.mariadb.org/projects/CONPY/issues/CONPY-203
* Make all methods static
* Add typing hints
* Don't safe fetch attribute value for errno - MariaDB exceptions if
  raised will have errno attr in them. If the class doesn't have one,
  it's not meant to be passed in these methods.
This change has only been done to separate and club only like methods /
utils together
* Drop mandatory unused arg in method call
* Use pluck instead of list comprehension + subscripting
Start initial pool of 4 (_POOL_SIZE) connections for a given site. When
the pool is exhausted, generate new connections upon request and add
them to the pool. Max allowed size for each pool is 64 (_MAX_POOL_SIZE)
connections. However, you may have more than 64 active connections, just
that they won't be pooled but destroyed upon job completion/end of
request/similar cycle.
For the times you don't want to use pooling ;) Calling
get_connection/connect will close all pooled connections
for the current process / worker!
_SITE_POOLS[frappe.local.site].read_only will hold ConnectionPool for
replica database connections. _SITE_POOLS[frappe.local.site].default
will hold pool for connections that allow read+writes
Help table has been deprecated for a while. It's safe to get rid of the
API too.
This logic mirror how replica connections are handled
…database_connection_pooling

Also, don't pool root connections
* Simplify logic: DRY, lesser indentation & all DAT
* Utilize newer APIs, f-strings & more
* Cleaner namespace
* Conform inconsistent behaviours
* Reduce _POOL_SIZE from 4 to 1. New pools will have just one
  connection. They can scale up as per requirement there after.
* Set auto_connect flag in MariaDB connection - https://mariadb.com/docs/connect/programming-languages/python/connect/
@stale stale bot removed the inactive label Jul 15, 2022
@codecov
Copy link

codecov bot commented Jul 20, 2022

Codecov Report

Merging #16961 (f40214b) into develop (b1e9bc8) will decrease coverage by 1.85%.
The diff coverage is 72.23%.

@@             Coverage Diff             @@
##           develop   #16961      +/-   ##
===========================================
- Coverage    59.17%   57.31%   -1.86%     
===========================================
  Files          772      771       -1     
  Lines        69261    69321      +60     
  Branches      6036     6035       -1     
===========================================
- Hits         40983    39733    -1250     
- Misses       24825    26099    +1274     
- Partials      3453     3489      +36     
Flag Coverage Δ
server 61.16% <72.23%> (-2.82%) ⬇️
ui-tests 50.58% <ø> (-0.20%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@gavindsouza gavindsouza changed the title feat!: MariaDB Database Connection Pooling + Client change refactor: Database Jul 21, 2022
@gavindsouza gavindsouza changed the title refactor: Database refactor!: Database Jul 21, 2022
@gavindsouza
Copy link
Collaborator Author

gavindsouza commented Jul 21, 2022

@gavindsouza gavindsouza force-pushed the mariadb-client-refactor branch 2 times, most recently from 9d12c53 to becfa8b Compare July 21, 2022 12:11
This is supposed to be a temporary switch to make the parent PR easier
to digest. MariaDB client has some issues with release, and system
dependencies.

This commit may be reverted to enable mariadb client again.
frappe/__init__.py Outdated Show resolved Hide resolved
frappe/__init__.py Show resolved Hide resolved
frappe/__init__.py Outdated Show resolved Hide resolved
frappe/database/database.py Show resolved Hide resolved
gavindsouza and others added 5 commits July 22, 2022 13:16
db.default_port wil be available as a class attribute to hold defaults
for DB types.

Usage: frappe.conf.db_port or frappe.db.default_port
Why: I couldn't run the mariadb command because the defaults aren't set
for my system. server is remote / containerized. Setting port in
equivalent mysql command fixes this.
There existed inconsistencies between db_query & db's fallback for min
datetime in str format - missing decimal seconds places. Now, we're
storing the default string once and re-using it to reduce
inconsistencies or room for human errors.
Co-authored-by: Ankush Menat <ankushmenat@gmail.com>
@gavindsouza gavindsouza removed the add-test-cases Add test case to validate fix or enhancement label Jul 22, 2022
@gavindsouza
Copy link
Collaborator Author

The last ERPNext CI build was all green, re-triggered it for confirmation.

@gavindsouza
Copy link
Collaborator Author

All Passing 🥳 for Frappe & ERPNext

image
image

@gavindsouza gavindsouza merged commit b52fdbb into frappe:develop Jul 22, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants