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: Rework definition validation #2720

Conversation

AndrewSisley
Copy link
Contributor

Relevant issue(s)

Resolves #2537

Description

Reworks definition validation, standardizing the rule signatures, allowing rule reuse across different contexts, and hopefully improving their readability. Performance of the rules will have decreased slightly, but on col/schema update that is unimportant, performance of createCollections (called when creating via SDL docs) has probably improved slightly due to a reduction in datastore calls.

This is largely in preparation for #2401 and the removal/simplification of SetActiveVersion.

@AndrewSisley AndrewSisley added area/schema Related to the schema system area/collections Related to the collections system refactor This issue specific to or requires *notable* refactoring of existing codebases and components code quality Related to improving code quality labels Jun 14, 2024
@AndrewSisley AndrewSisley added this to the DefraDB v0.12 milestone Jun 14, 2024
@AndrewSisley AndrewSisley requested a review from a team June 14, 2024 14:15
@AndrewSisley AndrewSisley self-assigned this Jun 14, 2024
@AndrewSisley AndrewSisley changed the title refactor: Rework definition validation refactor: Rework definition validation Jun 14, 2024
@AndrewSisley AndrewSisley force-pushed the 2537-definition-validator-refactor branch from ea8c8c6 to cbf2d2c Compare June 14, 2024 14:16
Copy link

codecov bot commented Jun 14, 2024

Codecov Report

Attention: Patch coverage is 91.62162% with 31 lines in your changes missing coverage. Please review.

Project coverage is 78.04%. Comparing base (4ee61f7) to head (a55f317).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #2720      +/-   ##
===========================================
+ Coverage    77.95%   78.04%   +0.09%     
===========================================
  Files          310      310              
  Lines        23113    23231     +118     
===========================================
+ Hits         18016    18129     +113     
- Misses        3716     3718       +2     
- Partials      1381     1384       +3     
Flag Coverage Δ
all-tests 78.04% <91.62%> (+0.09%) ⬆️

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

Files Coverage Δ
internal/db/errors.go 66.32% <100.00%> (+1.34%) ⬆️
internal/db/schema.go 81.25% <100.00%> (+0.50%) ⬆️
internal/db/view.go 64.71% <77.78%> (-3.48%) ⬇️
internal/db/definition_validation.go 95.12% <94.76%> (+1.52%) ⬆️
internal/db/collection_define.go 75.26% <77.61%> (+1.06%) ⬆️

... and 8 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4ee61f7...a55f317. Read the comment docs.

oldState := newDefinitionState(oldCols, map[string]client.SchemaDescription{})

for _, validator := range updateValidators {
err := validator(ctx, db, newState, oldState)
Copy link
Member

Choose a reason for hiding this comment

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

question: would it be possible to return all validator errors? It would be helpful to know all of the problems with a schema / collection.

Copy link
Contributor Author

@AndrewSisley AndrewSisley Jun 14, 2024

Choose a reason for hiding this comment

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

Was this what you meant in #2568 (I wasn't sure if that ticket was for this, or some other validation somewhere)?

It should be fine and easy to do, although the resultant errors might be quite noisy (some errors will likely result in many other errors being returned).

Can we do this in another PR/issue if we want it? Then we can test it properly (I want to avoid changing the tests as much as possible in this refactor PR)

Copy link
Member

Choose a reason for hiding this comment

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

Was this what you meant in #2568 (I wasn't sure if that ticket was for this, or some other validation somewhere)?

Yeah I was referencing that issue.

Can we do this in another PR/issue if we want it? Then we can test it properly (I want to avoid changing the tests as much as possible in this refactor PR)

Yeah that's fine with me. I think its purely a user experience issue and not relevant to this PR.

Copy link
Collaborator

@fredcarle fredcarle left a comment

Choose a reason for hiding this comment

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

LGTM. Just a couple minor todos prior to merging.

return nil, err
}
for i, def := range newDefinitions {
schema := def.Schema
Copy link
Collaborator

Choose a reason for hiding this comment

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

todo: Since schema is not used before being reassigned, please use def.Schema directly in CreateSchemaVersion bellow and instanciate schema at that time.

Copy link
Contributor Author

@AndrewSisley AndrewSisley Jun 14, 2024

Choose a reason for hiding this comment

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

Nice spot, removing.

  • sort out schema vars

}
for i, def := range newDefinitions {
schema := def.Schema
txn := mustGetContextTxn(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

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

todo: mustGetContextTxn should be called just once, prior to getAllActiveDefinitions above.

Copy link
Contributor Author

@AndrewSisley AndrewSisley Jun 14, 2024

Choose a reason for hiding this comment

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

Will move it to just after getAllActiveDefinitions

  • Move txn declaration(s)

@AndrewSisley AndrewSisley force-pushed the 2537-definition-validator-refactor branch 3 times, most recently from 17c30d1 to 603b016 Compare June 14, 2024 21:40
It doesn't make sense to handle these one by one, especially long term where a mix of multiple collections and schemas may be patched in the same call and validation must only take into account the final result.  A happy and necessary side effect of this commit is the schema version id is now set before validating new collections - this will simplify the new validation framework.
This means that we can validate that they have been set correctly, as well as simplifying some rules.
The new types will be applied to schema updates soon too.  A handful of tests have changed as the order in which rules execute has changed.
A handful of tests have changed due to the order of rule execution changing.
They are mapped and interacted with by users by name, so this rule can never fail - changing the name would trip up other rules though
A handful of tests have changed due to changes in the order in which rules are executed
Is now handled by the (existing) validation rules.
Sorry, I spotted late that all the definitions were being validated on ever iteration
@AndrewSisley AndrewSisley force-pushed the 2537-definition-validator-refactor branch from 603b016 to a55f317 Compare June 14, 2024 21:40
@AndrewSisley AndrewSisley merged commit 2b70154 into sourcenetwork:develop Jun 17, 2024
38 of 39 checks passed
@AndrewSisley AndrewSisley deleted the 2537-definition-validator-refactor branch June 17, 2024 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/collections Related to the collections system area/schema Related to the schema system code quality Related to improving code quality refactor This issue specific to or requires *notable* refactoring of existing codebases and components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor Collection and Schema validation
3 participants