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 Rule Evaluation Logic for Handling Multiple Rules of the Same Type - Part 2 (Data Backfilling) #2206

Merged

Conversation

Vyom-Yadav
Copy link
Member

@Vyom-Yadav Vyom-Yadav commented Jan 26, 2024

Fixes #1609


Changes and implications:

  • entity_profiles.contextual_rules are modified to have rule name key with rule_type or rule_type-rand_uuid in case of collisions

  • Existing null rule_evaluations.rule_name columns are filled by referring to name key in entity_profiles.contextual_rules

  • Handlers and Sql queries are updated to reflect not null constraint on rule_evaluations.rule_name column

  • migration_profile_backfill_log table is created to support migrating down

  • The rules are reconciled by the subsequent InternalProfileInitEventTopic, and ExecuteEntityEventTopic events. Profiles/Repositories not receiving these types of events won't be reconciled to update them to the latest state.

  • Reconciliation can potentially open new PRs/Security Advisories, etc., as the duplicate rule's row (entry) was overridden by the other rule of the same type.

@Vyom-Yadav Vyom-Yadav requested a review from a team as a code owner January 26, 2024 13:13
@Vyom-Yadav Vyom-Yadav force-pushed the issue-1609-data-backfilling branch from f7e83c6 to 7692134 Compare January 26, 2024 18:09
@Vyom-Yadav

This comment was marked as resolved.

@Vyom-Yadav Vyom-Yadav force-pushed the issue-1609-data-backfilling branch from 7692134 to 6712735 Compare January 26, 2024 18:32
@Vyom-Yadav Vyom-Yadav changed the title WIP: Fix Rule Evaluation Logic for Handling Multiple Rules of the Same Type - Part 2 (Data Backfilling) Fix Rule Evaluation Logic for Handling Multiple Rules of the Same Type - Part 2 (Data Backfilling) Jan 26, 2024
@Vyom-Yadav Vyom-Yadav force-pushed the issue-1609-data-backfilling branch 8 times, most recently from 23ffdfb to 753233e Compare February 2, 2024 18:28
…e - Part 2 (Data Backfilling)

* entity_profiles.contextual_rules are modified to have rule name key with rule_type or 'rule_type-rand_uuid' in case of collisions

* Existing rule_evaluations.rule_name columns are filled by referring to entity_profiles.contextual_rules[*].name key

* Handlers and Sql queries are updated to reflect not null constraint on rule_evaluations.rule_name column

* migration_profile_backfill_log table is created to support migrating down

Signed-off-by: Vyom-Yadav <[email protected]>
@Vyom-Yadav Vyom-Yadav force-pushed the issue-1609-data-backfilling branch from 753233e to 9ecd282 Compare February 5, 2024 12:28
Comment on lines +104 to +105
INSERT
INTO migration_profile_backfill_log (profile_id)
Copy link
Member

Choose a reason for hiding this comment

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

I like this approach, thank you!

@@ -2,7 +2,7 @@
-- name: UpsertRuleEvaluations :one
INSERT INTO rule_evaluations (
profile_id, repository_id, artifact_id, pull_request_id, rule_type_id, entity, rule_name
) VALUES ($1, $2, $3, $4, $5, $6, sqlc.narg(rule_name))
) VALUES ($1, $2, $3, $4, $5, $6, $7)
Copy link
Member

Choose a reason for hiding this comment

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

It took me a bit to verify. this, so I'm going to record it. I think this is safe because the migration script runs before the server update.

Because migrations run before the server code update, we could have the server attempting to insert rule_name == NULL from https://github.com/stacklok/minder/blob/b7fb73a53159a74dd6307a4dd7de4c614ec8aef1/internal/engine/eval_status.go#L129 unless we've backfilled the table. Once the table is backfilled, that shouldn't happen, and all the following rule evaluations should have name set:

Path:

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 think this is safe because the migration script runs before the server update.
...
The migration is selecting the rows FOR UPDATE, which should hopefully block any attempts to evaluate rules while the migration is in progress.

Exactly. Even if some rule is under evaluation and has fetched all required data from DB before the migration script began, the migration script would lock the tables and set the NOT NULL constraint on the column. So after the migration is complete and the engine gets access to the table, upserts would fail if they don't have a rule_name.

To prevent losing those events to some extent, we have retry mechanisms. In subsequent tries, the server would fetch rule_name from the DB to perform a legal operation.

@evankanderson evankanderson merged commit f485a3d into mindersec:main Feb 5, 2024
19 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.

Incorrect profile evaluation when using multiples rules of the same rule type
2 participants