-
Notifications
You must be signed in to change notification settings - Fork 333
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: use rwlock for modifiable session data #4232
refactor: use rwlock for modifiable session data #4232
Conversation
WalkthroughThe recent changes involve refining the Changes
Sequence Diagram(s)Changes don't include a new feature or modifications to the control flow that necessitate a sequence diagram. Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
8cec5ac
to
6eba6f6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files ignored due to path filters (1)
Cargo.lock
is excluded by!**/*.lock
Files selected for processing (24)
- src/auth/src/permission.rs (1 hunks)
- src/auth/tests/mod.rs (2 hunks)
- src/common/datasource/Cargo.toml (1 hunks)
- src/common/function/src/system/database.rs (1 hunks)
- src/operator/src/expr_factory.rs (2 hunks)
- src/operator/src/statement.rs (2 hunks)
- src/pipeline/src/manager/table.rs (1 hunks)
- src/query/src/optimizer/type_conversion.rs (1 hunks)
- src/query/src/plan.rs (1 hunks)
- src/query/src/range_select/plan_rewrite.rs (1 hunks)
- src/query/src/sql.rs (1 hunks)
- src/script/src/table.rs (1 hunks)
- src/servers/src/export_metrics.rs (1 hunks)
- src/servers/src/grpc/authorize.rs (3 hunks)
- src/servers/src/grpc/greptime_handler.rs (3 hunks)
- src/servers/src/http/authorize.rs (3 hunks)
- src/servers/src/http/prometheus.rs (1 hunks)
- src/servers/src/mysql/handler.rs (1 hunks)
- src/servers/src/postgres/handler.rs (1 hunks)
- src/servers/tests/http/authorize.rs (3 hunks)
- src/servers/tests/http/http_handler_test.rs (4 hunks)
- src/servers/tests/py_script/mod.rs (1 hunks)
- src/session/src/context.rs (14 hunks)
- src/session/src/lib.rs (3 hunks)
Files skipped from review due to trivial changes (5)
- src/operator/src/statement.rs
- src/query/src/range_select/plan_rewrite.rs
- src/servers/src/grpc/authorize.rs
- src/servers/src/mysql/handler.rs
- src/servers/tests/http/http_handler_test.rs
Additional comments not posted (57)
src/common/datasource/Cargo.toml (1)
34-36
: Addition of "async" feature toorc-rust
dependency.The addition of the "async" feature to the
orc-rust
dependency seems appropriate given the context of the PR. This change aligns with the need for asynchronous capabilities in the codebase.src/auth/src/permission.rs (2)
45-47
: Simplified method signature by requiringUserInfoRef
.The change from
Option<UserInfoRef>
toUserInfoRef
simplifies the method signature and eliminates the need for handlingNone
cases. This improves code readability and reduces potential errors.
53-55
: Updated implementation to match the newPermissionChecker
trait signature.The implementation of
PermissionChecker
forOption<&PermissionCheckerRef>
has been updated to match the new trait signature. This change ensures consistency and correctness.src/auth/tests/mod.rs (4)
30-32
: Updated test implementation to match the newPermissionChecker
trait signature.The test implementation has been updated to match the new
PermissionChecker
trait signature. This ensures that the tests are consistent with the updated code.
48-50
: Updated test cases to useUserInfoRef
directly.The test cases have been updated to use
UserInfoRef
directly instead ofOption<UserInfoRef>
. This change ensures that the tests accurately reflect the new method signature and simplifies the test logic.
54-56
: Updated SQL statement test case to useUserInfoRef
directly.The SQL statement test case has been updated to use
UserInfoRef
directly. This change ensures that the tests are consistent with the updated code and simplifies the test logic.
62-63
: Updated error handling test case to useUserInfoRef
directly.The error handling test case has been updated to use
UserInfoRef
directly. This change ensures that the tests are consistent with the updated code and simplifies the test logic.src/common/function/src/system/database.rs (1)
81-81
: Updatedcurrent_schema
method call tocurrent_schema_raw
.The method call has been updated to
current_schema_raw
to align with the changes in theQueryContext
struct. This change ensures consistency and correctness in the codebase.src/servers/tests/py_script/mod.rs (1)
63-63
: Updatedcurrent_schema
method call tocurrent_schema_raw
.The method call has been updated to
current_schema_raw
to align with the changes in theQueryContext
struct. This change ensures consistency and correctness in the codebase.src/session/src/lib.rs (13)
20-20
: Ensure correct usage ofRwLock
.The
RwLock
is used correctly here to wrap theString
type, allowing for concurrent read and write access.
34-38
: Ensure correct usage ofRwLock
in struct fields.The
RwLock
is used correctly to wrapString
,UserInfoRef
, andTimezone
types, allowing for concurrent read and write access.
51-55
: Ensure correct initialization ofRwLock
fields.The
RwLock
fields are correctly initialized withArc::new(RwLock::new(...))
to ensure thread-safe access.
62-69
: Ensure correct cloning ofRwLock
fields.The
RwLock
fields are correctly cloned for use in theQueryContextBuilder
.
82-83
: Ensure correct implementation oftimezone
getter.The
timezone
method correctly reads theRwLock
and clones theTimezone
value.
87-87
: Ensure correct implementation ofset_timezone
method.The
set_timezone
method correctly writes to theRwLock
.
91-91
: Ensure correct implementation ofuser_info
getter.The
user_info
method correctly reads theRwLock
and clones theUserInfoRef
value.
95-95
: Ensure correct implementation ofset_user_info
method.The
set_user_info
method correctly writes to theRwLock
.
99-99
: Ensure correct implementation ofset_catalog
method.The
set_catalog
method correctly writes to theRwLock
.
103-103
: Ensure correct implementation ofcatalog
getter.The
catalog
method correctly reads theRwLock
and clones theString
value.
107-107
: Ensure correct implementation ofschema
getter.The
schema
method correctly reads theRwLock
and clones theString
value.
111-111
: Ensure correct implementation ofset_schema
method.The
set_schema
method correctly writes to theRwLock
.
115-115
: Ensure correct usage ofcatalog
andschema
methods inget_db_string
.The
get_db_string
method correctly uses thecatalog
andschema
methods to build the database string.src/servers/tests/http/authorize.rs (3)
31-31
: Ensure correct handling of user information in test.The
ctx.current_user()
method is correctly used to retrieve the current user information.
42-42
: Ensure correct handling of user information in test.The
ctx.current_user()
method is correctly used to retrieve the current user information.
83-83
: Ensure correct handling of user information in test.The
ctx.current_user()
method is correctly used to retrieve the current user information.src/servers/src/grpc/greptime_handler.rs (3)
131-131
: Ensure correct handling of user information inauth
function.The
auth
function now directly returnsauth::userinfo_by_name(None)
whenuser_provider
isNone
.
133-133
: Ensure correct handling of user information inauth
function.The
auth
function now directly returnsauth::userinfo_by_name(None)
whenuser_provider
isNone
.
198-199
: Ensure correct usage ofcurrent_schema_raw
andtimezone_raw
increate_query_context
.The
create_query_context
function correctly usescurrent_schema_raw
andtimezone_raw
to set the schema and timezone in the query context.src/query/src/plan.rs (1)
251-251
: Ensure correct usage ofcurrent_schema_raw
in test.The
current_schema_raw
method is correctly used to set the schema in the query context.src/servers/src/export_metrics.rs (1)
250-252
: Update usage ofcurrent_schema_raw
The change from
current_schema
tocurrent_schema_raw
is consistent with the PR objectives. This ensures that the schema is wrapped inArc<RwLock>
, allowing for modifiable session data.src/session/src/context.rs (17)
18-18
: ImportRwLock
The import of
RwLock
is necessary for the changes made to theQueryContext
struct.
40-43
: ReplaceArcSwap
withArc<RwLock>
inQueryContext
The change from
ArcSwap
toArc<RwLock>
forcurrent_schema
,current_user
, andtimezone
aligns with the PR objectives to allow modifiable session data. This is a crucial change for handling concurrent access safely.
63-73
: Addcurrent_schema_raw
andtimezone_raw
methodsThe addition of
current_schema_raw
andtimezone_raw
methods in theQueryContextBuilder
is consistent with the changes to useArc<RwLock>
. These methods ensure that the schema and timezone are wrapped inArc<RwLock>
.
79-81
: CloneArc<RwLock>
fields inQueryContext
The clone implementation for
QueryContext
correctly clones theArc<RwLock>
fields, ensuring that the cloned context maintains the same references.
95-96
: Updatecurrent_schema
andtimezone
inFrom<RegionRequestHeader> for QueryContext
The updates to use
Arc<RwLock>
forcurrent_schema
andtimezone
in theFrom<RegionRequestHeader> for QueryContext
implementation are consistent with the overall changes.
107-108
: Updatecurrent_schema
andtimezone
inFrom<api::v1::QueryContext> for QueryContext
The updates to use
Arc<RwLock>
forcurrent_schema
andtimezone
in theFrom<api::v1::QueryContext> for QueryContext
implementation are consistent with the overall changes.
126-127
: ReadArc<RwLock>
fields inFrom<QueryContext> for api::v1::QueryContext
The changes to read from
Arc<RwLock>
forcurrent_schema
andtimezone
in theFrom<QueryContext> for api::v1::QueryContext
implementation are necessary to obtain the values correctly.
141-141
: UseArc<RwLock>
forcurrent_schema
inQueryContext::with
The change to use
Arc<RwLock>
forcurrent_schema
in theQueryContext::with
method is consistent with the overall changes to theQueryContext
struct.
159-159
: UseArc<RwLock>
forcurrent_schema
inQueryContext::with_db_name
The change to use
Arc<RwLock>
forcurrent_schema
in theQueryContext::with_db_name
method is consistent with the overall changes to theQueryContext
struct.
164-164
: Readcurrent_schema
fromArc<RwLock>
The change to read
current_schema
fromArc<RwLock>
in thecurrent_schema
method ofQueryContext
is necessary to obtain the value correctly.
168-168
: Writecurrent_schema
toArc<RwLock>
The change to write
current_schema
toArc<RwLock>
in theset_current_schema
method ofQueryContext
is necessary to update the value correctly.
185-186
: Readtimezone
fromArc<RwLock>
The change to read
timezone
fromArc<RwLock>
in thetimezone
method ofQueryContext
is necessary to obtain the value correctly.
189-190
: Writetimezone
toArc<RwLock>
The change to write
timezone
toArc<RwLock>
in theset_timezone
method ofQueryContext
is necessary to update the value correctly.
193-194
: Readcurrent_user
fromArc<RwLock>
The change to read
current_user
fromArc<RwLock>
in thecurrent_user
method ofQueryContext
is necessary to obtain the value correctly.
197-199
: Writecurrent_user
toArc<RwLock>
The change to write
current_user
toArc<RwLock>
in theset_current_user
method ofQueryContext
is necessary to update the value correctly.
238-244
: UseArc<RwLock>
inQueryContextBuilder::build
The change to use
Arc<RwLock>
forcurrent_schema
,current_user
, andtimezone
in theQueryContextBuilder::build
method is necessary to construct theQueryContext
correctly.
263-265
: CloneArc<RwLock>
fields inQueryContextBuilder::from_existing
The change to clone
Arc<RwLock>
fields in theQueryContextBuilder::from_existing
method is consistent with the overall changes to theQueryContext
struct.src/script/src/table.rs (1)
340-340
: Update usage ofcurrent_schema_raw
The change from
current_schema
tocurrent_schema_raw
is consistent with the PR objectives. This ensures that the schema is wrapped inArc<RwLock>
, allowing for modifiable session data.src/servers/src/http/authorize.rs (4)
65-65
: Update usage ofcurrent_schema_raw
The change from
current_schema
tocurrent_schema_raw
in theinner_auth
function is consistent with the PR objectives. This ensures that the schema is wrapped inArc<RwLock>
, allowing for modifiable session data.
68-69
: Update usage oftimezone_raw
The change from
timezone
totimezone_raw
in theinner_auth
function is consistent with the PR objectives. This ensures that the timezone is wrapped inArc<RwLock>
, allowing for modifiable session data.
78-78
: Set current user directlyThe change to set the current user directly without wrapping in
Some
is consistent with the updated function signature ofset_current_user
.
106-106
: Set authenticated userThe change to set the authenticated user directly in the
inner_auth
function is consistent with the updated function signature ofset_current_user
.src/pipeline/src/manager/table.rs (1)
207-207
: Update usage ofcurrent_schema_raw
The change from
current_schema
tocurrent_schema_raw
is consistent with the PR objectives. This ensures that the schema is wrapped inArc<RwLock>
, allowing for modifiable session data.src/query/src/optimizer/type_conversion.rs (1)
148-148
: Verify the correctness of direct reference totimezone
.The change from
self.query_ctx.timezone().as_ref()
to&self.query_ctx.timezone()
should be verified to ensure it correctly refers to thetimezone
field.src/operator/src/expr_factory.rs (1)
683-683
: Verify the correctness of direct reference totimezone
.The change from
QueryContextBuilder::default().timezone(Timezone::from_tz_string("+08:00").unwrap())
toQueryContextBuilder::default().timezone_raw(Timezone::from_tz_string("+08:00").unwrap())
should be verified to ensure it correctly refers to thetimezone
field.Verification successful
The
timezone_raw
method inQueryContextBuilder
is correctly implemented. The change to use.timezone_raw(Timezone::from_tz_string("+08:00").unwrap())
is valid.
src/session/src/context.rs
: Methodtimezone_raw
is correctly defined and implemented.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `timezone` field in `QueryContextBuilder`. # Test: Search for the method definition and its usage. Expect: Correct references to the `timezone` field. rg --type rust $'QueryContextBuilder'Length of output: 4487
Script:
#!/bin/bash # Description: Verify the implementation of `timezone_raw` method in `QueryContextBuilder`. # Search for the method definition within the `QueryContextBuilder` implementation ast-grep --lang rust --pattern 'impl QueryContextBuilder { $$$ pub fn timezone_raw(&self, $_: Timezone) -> &Self { $$$ } $$$ }'Length of output: 141
Script:
#!/bin/bash # Description: Verify the existence of any method related to `timezone` in `QueryContextBuilder`. # Search for any method related to `timezone` in the `QueryContextBuilder` implementation rg --type rust 'impl QueryContextBuilder' -A 20 | rg 'timezone'Length of output: 529
src/servers/src/http/prometheus.rs (1)
625-625
: Verify the correctness of direct reference toschema
.The change from
current_schema(schema.to_string())
tocurrent_schema_raw(schema.to_string())
should be verified to ensure it correctly refers to theschema
field.Verification successful
Verified the correctness of direct reference to
schema
.The change from
current_schema(schema.to_string())
tocurrent_schema_raw(schema.to_string())
correctly uses thecurrent_schema_raw
method to set thecurrent_schema
field in theQueryContextBuilder
.
src/servers/src/http/prometheus.rs
, line 625Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `schema` field in `QueryContextBuilder`. # Test: Search for the method definition and its usage. Expect: Correct references to the `schema` field. rg --type rust $'QueryContextBuilder'Length of output: 4487
Script:
#!/bin/bash # Description: Locate the definition of the `current_schema_raw` method in the `QueryContextBuilder` implementation and verify its usage. # Search for the `current_schema_raw` method definition in the `QueryContextBuilder` implementation. rg --type rust 'fn current_schema_raw' --context 5Length of output: 612
Script:
#!/bin/bash # Description: Verify the usage of the `current_schema` field within the `QueryContextBuilder` implementation. # Search for the `current_schema` field definition and its usage within the `QueryContextBuilder` implementation. rg --type rust 'current_schema' --context 5Length of output: 53321
src/query/src/sql.rs (1)
1024-1024
: Ensuretimezone_raw
method usage is appropriate.The method
timezone_raw
is used here. Verify that it correctly replaces the previoustimezone
method and that it aligns with the newRwLock
structure fortimezone
.Verification successful
Verification successful for
timezone_raw
method usage.The
timezone_raw
method correctly replaces the previoustimezone
method and aligns with theRwLock
structure fortimezone
. The usage insrc/query/src/sql.rs
is appropriate and consistent with the changes.
timezone_raw
method implementation:src/session/src/context.rs
RwLock<Timezone>
structure:src/session/src/lib.rs
,src/session/src/context.rs
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that `timezone_raw` method is correctly implemented and used in the codebase. # Test: Search for the `timezone_raw` method implementation. Expect: Find the correct implementation. rg --type rust $'fn timezone_raw' # Test: Search for all usages of `timezone_raw`. Expect: Verify all usages are appropriate. rg --type rust $'timezone_raw'Length of output: 792
Script:
#!/bin/bash # Description: Verify that the previous `timezone` method's implementation and usage. # Test: Search for the `timezone` method implementation. Expect: Find the correct implementation. rg --type rust $'fn timezone' # Test: Search for all usages of `timezone`. Expect: Verify all usages are appropriate. rg --type rust $'timezone'Length of output: 34797
Script:
#!/bin/bash # Description: Verify the `RwLock` structure for `timezone`. # Test: Search for `RwLock` usage with `timezone`. Expect: Verify the structure. rg --type rust 'RwLock<Timezone>'Length of output: 159
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4232 +/- ##
==========================================
- Coverage 84.97% 84.65% -0.32%
==========================================
Files 1054 1057 +3
Lines 187154 187495 +341
==========================================
- Hits 159037 158732 -305
- Misses 28117 28763 +646 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (7)
- src/operator/src/expr_factory.rs (2 hunks)
- src/operator/src/statement.rs (2 hunks)
- src/query/src/sql.rs (1 hunks)
- src/servers/src/grpc/greptime_handler.rs (3 hunks)
- src/servers/src/http/authorize.rs (3 hunks)
- src/session/src/context.rs (11 hunks)
- src/session/src/lib.rs (2 hunks)
Files skipped from review as they are similar to previous changes (5)
- src/operator/src/expr_factory.rs
- src/operator/src/statement.rs
- src/query/src/sql.rs
- src/servers/src/http/authorize.rs
- src/session/src/context.rs
Additional comments not posted (18)
src/session/src/lib.rs (15)
20-20
: Ensure correct usage ofRwLock
.The import of
RwLock
is necessary for the changes. Ensure thatRwLock
is used correctly throughout the codebase.
34-35
: Consider the necessity ofArc
.The
Session
struct usesArc
forRwLock
fields. Evaluate ifArc
is necessary, given thatSession
is notClone
.Would you like to explore alternatives for managing shared mutable state without
Arc
?
42-48
: Encapsulation of mutable fields.The introduction of
MutableInner
to encapsulate mutable fields is a good practice for better organization and potential future extensions.
50-58
: Default implementation forMutableInner
.The default implementation of
MutableInner
initializes fields with default values. Ensure these defaults are appropriate for all use cases.
67-70
: Initialization ofSession
withRwLock
.The
Session
struct is correctly initialized withRwLock
forcatalog
andmutable_inner
. Ensure that all necessary initializations are covered.
76-79
: Thread-safe access tocatalog
.The
new_query_context
method provides thread-safe access tocatalog
usingRwLock
. Ensure similar patterns are followed for other mutable fields.
94-95
: Thread-safe access totimezone
.The
timezone
method provides thread-safe read access totimezone
usingRwLock
.
99-100
: Thread-safe modification oftimezone
.The
set_timezone
method provides thread-safe write access totimezone
usingRwLock
.
104-104
: Thread-safe access touser_info
.The
user_info
method provides thread-safe read access touser_info
usingRwLock
.
108-108
: Thread-safe modification ofuser_info
.The
set_user_info
method provides thread-safe write access touser_info
usingRwLock
.
112-112
: Thread-safe modification ofcatalog
.The
set_catalog
method provides thread-safe write access tocatalog
usingRwLock
.
116-116
: Thread-safe access tocatalog
.The
catalog
method provides thread-safe read access tocatalog
usingRwLock
.
120-120
: Thread-safe access toschema
.The
schema
method provides thread-safe read access toschema
usingRwLock
.
124-124
: Thread-safe modification ofschema
.The
set_schema
method provides thread-safe write access toschema
usingRwLock
.
128-128
: Thread-safe access to database string.The
get_db_string
method provides thread-safe read access tocatalog
andschema
usingRwLock
.src/servers/src/grpc/greptime_handler.rs (3)
131-131
: Ensure correct handling ofUserInfoRef
.The
auth
function now directly handlesUserInfoRef
. Ensure that this change is consistent across the codebase.
133-133
: Simplified handling ofNone
case foruser_provider
.The early return for the
None
case ofuser_provider
simplifies the logic.
199-199
: Thread-safe access totimezone
.The
create_query_context
function correctly sets thetimezone
using the newRwLock
based implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- src/session/src/lib.rs (2 hunks)
Files skipped from review as they are similar to previous changes (1)
- src/session/src/lib.rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I hereby agree to the terms of the GreptimeDB CLA.
Refer to a related PR or issue link (optional)
What's changed and what's your intention?
Follow up for #4218 . Need a rebase after #4218 merged.
Some of the properties, like schema and timezone, are modifiable within a session.
Our original design is to generate a read-only
QueryContext
, then pass it with all the execution process path. This works well before we have statements that modify some of the session data, likeSET time_zone
andUSE
.After that, we introduce a new method called
update_session
to give back modified properties to session, after query execution. Seegreptimedb/src/servers/src/mysql/handler.rs
Line 107 in b739c9f
update_session
has to check each modifiable field and update the session. This is becauseArcSwap
data structure is not clonable. So we cannot share reference between the parentSession
and the childQueryContext
.This main point of this patch is to change those modifiable fields from
ArcSwap
toArc<RwLock>
so that we can share it between parent and child. Note that becauseSession
is associated with a signle connection, so actually there is no race condition with reading and updating those modifiable fields.This patch also include a feature flag fix for orc-rust to exclude it datafusion dependency.
Checklist
Summary by CodeRabbit
Refactor
ArcSwap
withArc<RwLock>
for fields inSession
struct, enhancing concurrent access management.Bug Fixes
Chores
async
feature fororc-rust
, enhancing asynchronous capabilities.