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

Add pkg/querier package #4849

Merged
merged 6 commits into from
Nov 1, 2024
Merged

Add pkg/querier package #4849

merged 6 commits into from
Nov 1, 2024

Conversation

rdimitrov
Copy link
Member

Summary

The following PR adds a querier package to pkg. Its purpose is to expose a set of tools to interact with the Minder database.
The wrappers for managing the ruletypes and profiles are wrapped around their service alternatives as making changes to these entries without validating them can cause conflicts in the profile references and evaluations.

Change Type

Mark the type of change your PR introduces:

  • Bug fix (resolves an issue without affecting existing features)
  • Feature (adds new functionality without breaking changes)
  • Breaking change (may impact existing functionalities or require documentation updates)
  • Documentation (updates or additions to documentation)
  • Refactoring or test improvements (no bug fixes or new functionality)

Testing

Outline how the changes were tested, including steps to reproduce and any relevant configurations.
Attach screenshots if helpful.

Review Checklist:

  • Reviewed my own code for quality and clarity.
  • Added comments to complex or tricky code sections.
  • Updated any affected documentation.
  • Included tests that validate the fix or feature.
  • Checked that related changes are merged.

@coveralls
Copy link

coveralls commented Oct 31, 2024

Pull Request Test Coverage Report for Build 11631537336

Details

  • 0 of 204 (0.0%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.4%) to 54.835%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pkg/querier/bundle.go 0 21 0.0%
pkg/querier/profile.go 0 31 0.0%
pkg/querier/ruletype.go 0 31 0.0%
pkg/querier/project.go 0 56 0.0%
pkg/querier/querier.go 0 65 0.0%
Totals Coverage Status
Change from base Build 11631234497: -0.4%
Covered Lines: 15537
Relevant Lines: 28334

💛 - Coveralls

Copy link
Member

@evankanderson evankanderson left a comment

Choose a reason for hiding this comment

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

How are you feeling about this approach having started it?

I'm a bit nervous about the interaction with transactions; I think I'd rather expose methods to manage a sql.Tx outside the interface than try to bundle it into the interface if you need transaction support. (Or you could include the transaction in the context, which might be a nice API)

Comment on lines 2761 to 2796

// BundleSubscription represents a subscription to a bundle.
message BundleSubscription {
string id = 1;
string project_id = 2;
string bundle_id = 3;
string current_version = 4;
}
Copy link
Member

Choose a reason for hiding this comment

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

It feels like this should be someplace different from the external API, since it's not referenced by any clients.

We have an internal/proto, but maybe it makes sense to have a pkg/proto (and exclude that from coverage)?

ProfileHandlers
BundleHandlers
Close()
Commit() error
Copy link
Member

Choose a reason for hiding this comment

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

Do you need a way to start a transaction, given that you have a Commit method?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, it looks like you need to throw away the object (and the connection pool) after each transaction?

eleftherias
eleftherias previously approved these changes Nov 1, 2024
RuleTypeHandlers
ProfileHandlers
BundleHandlers
CommitTx() error
Copy link
Member

Choose a reason for hiding this comment

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

I'd probably call this Commit and Cancel, and document that the object can't be used after calling these.

return fmt.Errorf("no transaction to cancel")
}

// initDb function initializes the database connection and transaction details, if needed
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't do anything with transactions, does it?

var _ Querier = (*Type)(nil)

// New creates a new instance of the querier
func New(ctx context.Context, config *server.Config) (Store, func(), error) {
Copy link
Member

Choose a reason for hiding this comment

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

You may want to define a type for the second return argument (it's not clear what you do with a func() on its own).

}

// Type represents the querier type
type Type struct {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this type exported?

@@ -1023,6 +1023,8 @@ message Project {
// display_names are short *non-unique* strings to provide
// a user-friendly name for presentation in lists, etc.
string display_name = 5;
bool is_organization = 8;
Copy link
Member

Choose a reason for hiding this comment

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

Discussed offline, but I don't think we want to change our public API in this PR.

evankanderson
evankanderson previously approved these changes Nov 1, 2024
return nil, nil, fmt.Errorf("unable to setup eventer: %w", err)
}
// Return the new Type
return &querierType{
Copy link
Member

Choose a reason for hiding this comment

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

Oh, this is cute -- you're using querierType as both Store and Querier. I think that won't hurt you; anyone who goes to cast one to the other gets what's coming to them.

}
// Return the new Type
return &querierType{
store: store,
Copy link
Member

Choose a reason for hiding this comment

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

You probably don't need both querier and store with the new implementation, but I'm not going to begrudge you 8 bytes.

Copy link
Member Author

Choose a reason for hiding this comment

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

You know this unlocks my embedded programming side if you do that 😁

Comment on lines 108 to 115
if q.tx != nil {
err := q.store.Commit(q.tx)
// Clear the transaction and the querier
q.tx = nil
q.querier = nil
return err
}
return fmt.Errorf("no transaction to commit")
Copy link
Member

Choose a reason for hiding this comment

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

No need to fix here, but I'd tend to use the "early exit" model for these sorts of checks:

Suggested change
if q.tx != nil {
err := q.store.Commit(q.tx)
// Clear the transaction and the querier
q.tx = nil
q.querier = nil
return err
}
return fmt.Errorf("no transaction to commit")
if q.tx == nil {
return fmt.Errorf("no transaction to commit")
}
err := q.store.Commit(q.tx)
// Clear the transaction and the querier
q.tx = nil
q.querier = nil
return err

When reading code, I find that I need to keep a mental stack open whenever I'm in a conditional to recall what the code path is with and without the conditional. Having conditionals that close quickly with early returns lets me retire that call stack in my head, and leaves the "most common" code path running to the end of the function.

Comment on lines 132 to 138
zerolog.Ctx(ctx).Debug().
Str("name", cfg.Database.Name).
Str("host", cfg.Database.Host).
Str("user", cfg.Database.User).
Str("ssl_mode", cfg.Database.SSLMode).
Int("port", cfg.Database.Port).
Msg("connecting to minder database")
Copy link
Member

Choose a reason for hiding this comment

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

We log these inside GetDBConnection too, don't we?

@evankanderson
Copy link
Member

To be clear, the above are nits, and I'm fine with the code going in as-is.

@rdimitrov rdimitrov merged commit dc6fce9 into main Nov 1, 2024
24 checks passed
@rdimitrov rdimitrov deleted the move-db branch November 1, 2024 17:29
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.

5 participants