-
Notifications
You must be signed in to change notification settings - Fork 982
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move maintaing the RowCount into an async task (#16027)
- Loading branch information
Showing
7 changed files
with
228 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pretend | ||
|
||
from celery.schedules import crontab | ||
|
||
from warehouse.accounts.models import User | ||
from warehouse.packaging.models import File, Project, Release | ||
from warehouse.utils import row_counter | ||
|
||
from ...common.db.packaging import FileFactory, ProjectFactory, ReleaseFactory | ||
|
||
|
||
def test_compute_row_counts(db_request): | ||
project1 = ProjectFactory() | ||
project2 = ProjectFactory() | ||
release1 = ReleaseFactory(project=project1) | ||
release2 = ReleaseFactory(project=project2) | ||
release3 = ReleaseFactory(project=project2) | ||
FileFactory(release=release1) | ||
FileFactory(release=release2) | ||
FileFactory(release=release3, packagetype="sdist") | ||
FileFactory(release=release3, packagetype="bdist_wheel") | ||
|
||
counts = dict( | ||
db_request.db.query(row_counter.RowCount.table_name, row_counter.RowCount.count) | ||
.filter( | ||
row_counter.RowCount.table_name.in_( | ||
[ | ||
Project.__tablename__, | ||
Release.__tablename__, | ||
File.__tablename__, | ||
User.__tablename__, | ||
] | ||
) | ||
) | ||
.all() | ||
) | ||
|
||
assert counts == {"users": 0, "projects": 0, "releases": 0, "release_files": 0} | ||
|
||
row_counter.compute_row_counts(db_request) | ||
|
||
counts = dict( | ||
db_request.db.query(row_counter.RowCount.table_name, row_counter.RowCount.count) | ||
.filter( | ||
row_counter.RowCount.table_name.in_( | ||
[ | ||
Project.__tablename__, | ||
Release.__tablename__, | ||
File.__tablename__, | ||
User.__tablename__, | ||
] | ||
) | ||
) | ||
.all() | ||
) | ||
|
||
assert counts == {"users": 3, "projects": 2, "releases": 3, "release_files": 4} | ||
|
||
|
||
def test_includeme(): | ||
config = pretend.stub(add_periodic_task=pretend.call_recorder(lambda c, f: None)) | ||
row_counter.includeme(config) | ||
assert config.add_periodic_task.calls == [ | ||
pretend.call(crontab(minute="*/5"), row_counter.compute_row_counts), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
warehouse/migrations/versions/cec0316503a5_remove_count_rows_triggers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
Remove count_rows() triggers | ||
Revision ID: cec0316503a5 | ||
Revises: 78ecf599841c | ||
Create Date: 2024-05-30 16:46:07.355604 | ||
""" | ||
|
||
from alembic import op | ||
|
||
revision = "cec0316503a5" | ||
down_revision = "78ecf599841c" | ||
|
||
|
||
def upgrade(): | ||
op.execute("DROP TRIGGER update_row_count ON users") | ||
op.execute("DROP TRIGGER update_row_count ON release_files") | ||
op.execute("DROP TRIGGER update_row_count ON releases") | ||
op.execute("DROP TRIGGER update_row_count ON projects") | ||
op.execute("DROP FUNCTION count_rows()") | ||
|
||
|
||
def downgrade(): | ||
op.execute( | ||
""" CREATE FUNCTION count_rows() | ||
RETURNS TRIGGER AS | ||
' | ||
BEGIN | ||
IF TG_OP = ''INSERT'' THEN | ||
UPDATE row_counts | ||
SET count = count + 1 | ||
WHERE table_name = TG_RELNAME; | ||
ELSIF TG_OP = ''DELETE'' THEN | ||
UPDATE row_counts | ||
SET count = count - 1 | ||
WHERE table_name = TG_RELNAME; | ||
END IF; | ||
RETURN NULL; | ||
END; | ||
' LANGUAGE plpgsql; | ||
""" | ||
) | ||
|
||
op.execute("LOCK TABLE projects IN SHARE ROW EXCLUSIVE MODE") | ||
op.execute("LOCK TABLE releases IN SHARE ROW EXCLUSIVE MODE") | ||
op.execute("LOCK TABLE release_files IN SHARE ROW EXCLUSIVE MODE") | ||
op.execute("LOCK TABLE users IN SHARE ROW EXCLUSIVE MODE") | ||
|
||
op.execute( | ||
""" CREATE TRIGGER update_row_count | ||
AFTER INSERT OR DELETE ON projects | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE count_rows(); | ||
""" | ||
) | ||
|
||
op.execute( | ||
""" CREATE TRIGGER update_row_count | ||
AFTER INSERT OR DELETE ON releases | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE count_rows(); | ||
""" | ||
) | ||
|
||
op.execute( | ||
""" CREATE TRIGGER update_row_count | ||
AFTER INSERT OR DELETE ON release_files | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE count_rows(); | ||
""" | ||
) | ||
|
||
op.execute( | ||
""" CREATE TRIGGER update_row_count | ||
AFTER INSERT OR DELETE ON users | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE count_rows(); | ||
""" | ||
) | ||
|
||
op.execute( | ||
""" INSERT INTO row_counts (table_name, count) | ||
VALUES ('projects', (SELECT COUNT(*) FROM projects)); | ||
""" | ||
) | ||
|
||
op.execute( | ||
""" INSERT INTO row_counts (table_name, count) | ||
VALUES ('releases', (SELECT COUNT(*) FROM releases)); | ||
""" | ||
) | ||
|
||
op.execute( | ||
""" INSERT INTO row_counts (table_name, count) | ||
VALUES ('release_files', (SELECT COUNT(*) FROM release_files)); | ||
""" | ||
) | ||
|
||
op.execute( | ||
""" INSERT INTO row_counts (table_name, count) | ||
VALUES ('users', (SELECT COUNT(*) FROM users)); | ||
""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters