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

*: maintain the active users for privilege data #56709

Merged
merged 27 commits into from
Oct 30, 2024

Conversation

tiancaiamao
Copy link
Contributor

@tiancaiamao tiancaiamao commented Oct 17, 2024

What problem does this PR solve?

Issue Number: ref #55563

Problem Summary:

What changed and how does it work?

Add an activeUsers field to the privilege.Handler.
This is a step towrads supporting 2.5M users, later we'll only keep the active users' data in-memory.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-tests-checked release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 17, 2024
Copy link

tiprow bot commented Oct 17, 2024

Hi @tiancaiamao. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Copy link

codecov bot commented Oct 17, 2024

Codecov Report

Attention: Patch coverage is 79.00763% with 55 lines in your changes missing coverage. Please review.

Project coverage is 57.4224%. Comparing base (5ab6738) to head (b8f6092).
Report is 43 commits behind head on master.

Additional details and impacted files
@@                Coverage Diff                @@
##             master     #56709         +/-   ##
=================================================
- Coverage   73.3167%   57.4224%   -15.8944%     
=================================================
  Files          1636       1801        +165     
  Lines        453659     647223     +193564     
=================================================
+ Hits         332608     371651      +39043     
- Misses       100674     250234     +149560     
- Partials      20377      25338       +4961     
Flag Coverage Δ
integration 39.5301% <78.2442%> (?)
unit 72.5787% <70.9923%> (+0.0534%) ⬆️

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

Components Coverage Δ
dumpling 52.9478% <ø> (ø)
parser ∅ <ø> (∅)
br 63.1883% <ø> (+17.1766%) ⬆️

@ti-chi-bot ti-chi-bot bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 23, 2024
@tiancaiamao
Copy link
Contributor Author

/retest

Copy link

tiprow bot commented Oct 28, 2024

@tiancaiamao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

pkg/privilege/privileges/cache.go Show resolved Hide resolved
u := user.Username
h := user.Hostname
if len(user.AuthUsername) > 0 && len(user.AuthHostname) > 0 {
u = user.AuthUsername
h = user.AuthHostname
}
if err := p.Handle.ensureActiveUser(u); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure about the future usage. Will something happen between ensureActiveUser and Get and evict the active user?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently there is no evict operation, evict make thing more complex.

pkg/privilege/privileges/cache.go Outdated Show resolved Hide resolved
pkg/privilege/privileges/cache.go Outdated Show resolved Hide resolved
})
ret.buildGlobalMap()

ret.RoleGraph = diff.RoleGraph
Copy link
Contributor

Choose a reason for hiding this comment

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

(I will check later if only User and GlobalPriv need dedup, and RoleGraph can be directly used)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

RoleGraph is a full load, because I found that the table definitions differ from the others.
Other other tables has the User+Host column, while this table does not have it.

pkg/privilege/privileges/cache.go Outdated Show resolved Hide resolved
pkg/privilege/privileges/cache.go Outdated Show resolved Hide resolved
pkg/privilege/privileges/cache.go Outdated Show resolved Hide resolved
@ti-chi-bot ti-chi-bot bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Oct 29, 2024
pkg/privilege/privilege.go Outdated Show resolved Hide resolved
@@ -1633,12 +1815,48 @@ func (p *MySQLPrivilege) getAllRoles(user, host string) []*auth.RoleIdentity {

// Handle wraps MySQLPrivilege providing thread safe access.
type Handle struct {
sctx sessionctx.Context
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use the type sqlexec.RestrictedSQLExecutor if we only use the context to execute some SQLs to reduce the dependency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sqlexec.RestrictedSQLExecutor is not enough here.
We need something like a sqlexec.RestrictedSQLExecutor pool to avoid the single instance been used by multiple threads. @lcwangchao

Copy link
Collaborator

Choose a reason for hiding this comment

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

If so, keeping a sessionctx.Context inside has the same problem.

pkg/privilege/privileges/tidb_auth_token_test.go Outdated Show resolved Hide resolved
pkg/privilege/privileges/cache.go Show resolved Hide resolved
columnsPriv []columnsPrivRecord
defaultRoles []defaultRoleRecord

globalPriv []globalPrivRecord
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why need to keep a immutable globalPriv and a mutable Global? Any problem to just keep one Global field?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make the apply diff logic simpler if we're using one single slice representation...
we can copy and paste a lot code and just handle them the same way.

It's possible to keep different representation for different field, or even using Map for almost all field, but we need to add more code.

@tiancaiamao
Copy link
Contributor Author

/retest

Copy link

tiprow bot commented Oct 30, 2024

@tiancaiamao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

pkg/privilege/privileges/cache.go Outdated Show resolved Hide resolved
pkg/privilege/privileges/cache.go Outdated Show resolved Hide resolved
@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Oct 30, 2024
@tiancaiamao
Copy link
Contributor Author

/retest

Copy link

tiprow bot commented Oct 30, 2024

@tiancaiamao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

return cmp
}
switch {
case x.DB > y.DB:
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can use strings.Compare

for i := 0; i < len(p.User); i++ {
record := &p.User[i]
func (p *MySQLPrivilege) matchIdentity(sctx sqlexec.RestrictedSQLExecutor, user, host string, skipNameResolve bool) *UserRecord {
for i := 0; i < len(p.user); i++ {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: for i := range p.User

@@ -1011,8 +1194,8 @@ func (p *MySQLPrivilege) matchIdentity(user, host string, skipNameResolve bool)

// matchResoureGroup finds an identity to match resource group.
func (p *MySQLPrivilege) matchResoureGroup(resourceGroupName string) *UserRecord {
for i := 0; i < len(p.User); i++ {
record := &p.User[i]
for i := 0; i < len(p.user); i++ {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

@@ -1090,8 +1273,8 @@ func (p *MySQLPrivilege) matchTables(user, host, db, table string) *tablesPrivRe
}

func (p *MySQLPrivilege) matchColumns(user, host, db, table, column string) *columnsPrivRecord {
for i := 0; i < len(p.ColumnsPriv); i++ {
record := &p.ColumnsPriv[i]
for i := 0; i < len(p.columnsPriv); i++ {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link

ti-chi-bot bot commented Oct 30, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: lance6716, lcwangchao

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added lgtm approved and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Oct 30, 2024
Copy link

ti-chi-bot bot commented Oct 30, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-10-30 10:42:37.160397932 +0000 UTC m=+432869.999553478: ☑️ agreed by lcwangchao.
  • 2024-10-30 12:43:56.172825283 +0000 UTC m=+440149.011980830: ☑️ agreed by lance6716.

@lance6716
Copy link
Contributor

lance6716 commented Oct 30, 2024

It's OK to fix my comments in next PR or ignore them @tiancaiamao

@ti-chi-bot ti-chi-bot bot merged commit cf5a617 into pingcap:master Oct 30, 2024
25 checks passed
@tiancaiamao tiancaiamao deleted the active-user branch November 1, 2024 02:24
@tiancaiamao
Copy link
Contributor Author

This commit could make the login process slower, because now we need to load the users data.
And also some scenario like show grants for some other users.

The general DDL/DML cases should not be affected, this just affects a minority of privileges related.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. type/performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants