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

disable populating log_context based on commit_sha/commit_id #977

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 12 additions & 43 deletions helpers/log_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sentry_sdk import get_current_span
from shared.config import get_config

from database.models.core import Commit, Owner, Repository
from database.models.core import Owner, Repository

log = logging.getLogger("log_context")

Expand All @@ -28,6 +28,11 @@ class LogContext:
repo_name: str | None = None
repo_id: int | None = None
commit_sha: str | None = None

# TODO: begin populating this again or remove it
# we can populate this again if we reduce query load by passing the Commit,
# Owner, and Repository models we use to populate the log context into the
# various task implementations so they don't fetch the same models again
commit_id: int | None = None

checkpoints_data: dict = field(default_factory=lambda: {})
Expand Down Expand Up @@ -58,48 +63,13 @@ def populate_from_sqlalchemy(self, dbsession):
return

try:
can_identify_commit = self.commit_id is not None or (
self.commit_sha is not None and self.repo_id is not None
)

# commit_id or (commit_sha + repo_id) is enough to get everything else
if can_identify_commit:
query = (
dbsession.query(
Commit.id_,
Commit.commitid,
Repository.repoid,
Repository.name,
Owner.ownerid,
Owner.username,
Owner.service,
Owner.plan,
)
.join(Commit.repository)
.join(Repository.owner)
)

if self.commit_id is not None:
query = query.filter(Commit.id_ == self.commit_id)
else:
query = query.filter(
Commit.commitid == self.commit_sha,
Commit.repoid == self.repo_id,
)

(
self.commit_id,
self.commit_sha,
self.repo_id,
self.repo_name,
self.owner_id,
self.owner_username,
self.owner_service,
self.owner_plan,
) = query.first()
# - commit_id (or commit_sha + repo_id) is enough to get everything else
# - however, this slams the commit table and we rarely really need the
# DB PK for a commit, so we don't do this.
# - repo_id is enough to get repo and owner
# - owner_id is just enough to get owner

# repo_id is enough to get repo and owner
elif self.repo_id:
if self.repo_id:
Copy link
Contributor

Choose a reason for hiding this comment

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

So the intent here is to reenables the log context without the commit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

the stated problem is that populating the log context slammed the commit table. turns out if we don't query the commit table the only thing we are likely to lose is the DB PK of the commit which we don't tend to use anyway. so this PR stops querying the commit table

this may just move the problem, and we should maybe do the followup described in the PR description anyway

query = (
dbsession.query(
Repository.name,
Expand All @@ -120,7 +90,6 @@ def populate_from_sqlalchemy(self, dbsession):
self.owner_plan,
) = query.first()

# owner_id is just enough for owner
elif self.owner_id:
query = dbsession.query(
Owner.username, Owner.service, Owner.plan
Expand Down
16 changes: 5 additions & 11 deletions helpers/tests/unit/test_log_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,11 @@ def test_populate_just_commit_sha(dbsession):


def test_populate_just_commit_id(dbsession):
owner, repo, commit = create_db_records(dbsession)
_owner, _repo, commit = create_db_records(dbsession)
log_context = LogContext(commit_id=commit.id_)
log_context.populate_from_sqlalchemy(dbsession)

assert log_context == LogContext(
repo_id=repo.repoid,
repo_name="example-python",
owner_id=owner.ownerid,
owner_username="codecove2e",
owner_service="github",
owner_plan="users-basic",
commit_sha=commit.commitid,
commit_id=commit.id_,
)

Expand All @@ -104,7 +97,6 @@ def test_populate_repo_and_commit_sha(dbsession):
owner_service="github",
owner_plan="users-basic",
commit_sha=commit.commitid,
commit_id=commit.id_,
)


Expand Down Expand Up @@ -135,7 +127,9 @@ async def check_context_in_coroutine():

def test_as_dict(dbsession, mocker):
owner, repo, commit = create_db_records(dbsession)
log_context = LogContext(commit_id=commit.id_, task_name="foo", task_id="bar")
log_context = LogContext(
commit_sha=commit.commitid, repo_id=repo.repoid, task_name="foo", task_id="bar"
)
log_context.populate_from_sqlalchemy(dbsession)

mock_span = mocker.Mock()
Expand All @@ -147,7 +141,7 @@ def test_as_dict(dbsession, mocker):
assert log_context.as_dict() == {
"task_name": "foo",
"task_id": "bar",
"commit_id": commit.id_,
"commit_id": None,
"commit_sha": commit.commitid,
"repo_id": repo.repoid,
"repo_name": repo.name,
Expand Down
Loading