-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Implement GTID tracking. #1633
base: master
Are you sure you want to change the base?
Implement GTID tracking. #1633
Conversation
Co-authored-by: Daniel Joos <[email protected]>
WalkthroughThe changes introduce enhancements to the MySQL driver for Go, focusing on session tracking and GTID management. New constants for session tracking types are defined, and a boolean field Changes
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 using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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: 7
🧹 Outside diff range and nitpick comments (2)
result.go (1)
42-42
: Add a comment for thegtids
field inmysqlResult
struct.Consider adding a comment to explain the purpose of the
gtids
field to improve code readability and maintainability.driver_test.go (1)
2621-2682
: Modifying global server variables may affect other testsChanging global server variables like
gtid_mode
andenforce_gtid_consistency
within a test can impact other tests running concurrently or subsequently, leading to flaky or unpredictable test results. Consider isolating these changes by running the test in a separate environment or serializing tests that depend on global state.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (5)
- const.go (1 hunks)
- driver_test.go (1 hunks)
- dsn.go (3 hunks)
- packets.go (5 hunks)
- result.go (3 hunks)
🔇 Additional comments (10)
const.go (2)
191-199
: Summary: New session tracking constants addedThe changes in this file are focused on adding new constants for session tracking, including GTID tracking. These additions align well with the PR objectives and enhance the driver's capability to handle various session tracking types.
The new constants provide a solid foundation for implementing GTID tracking and other session-related features. Ensure that these constants are utilized effectively in the implementation of the GTID tracking functionality.
191-199
: LGTM! Consider adding a descriptive comment.The new session tracking constants are well-defined and align with the PR objectives. The use of
iota
is appropriate for this set of related constants.Consider adding a comment above this constant block to explain its purpose and relation to MySQL session tracking types. For example:
// Session tracking types as defined by MySQL // https://dev.mysql.com/doc/dev/mysql-server/latest/group__group__cs__session__track__type.html const ( sessionTrackSystemVariables = iota // ... (rest of the constants) )Let's verify if these constants are used correctly in the implementation:
✅ Verification successful
Verified:
The usage of
sessionTrackGtids
inpackets.go
is correct and aligns with the intended session tracking implementation.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Check for the usage of new session tracking constants # Test: Search for usage of session tracking constants rg --type go -e 'sessionTrack\w+' -g '!const.go'Length of output: 100
dsn.go (4)
73-73
: LGTM: TrackSessionState field added correctlyThe addition of the
TrackSessionState
boolean field to theConfig
struct is well-placed and properly documented. This aligns with the PR objective of implementing GTID tracking functionality.
327-330
: LGTM: FormatDSN method updated correctlyThe
FormatDSN
method has been properly updated to include theTrackSessionState
parameter in the DSN string when it's set to true. The implementation is consistent with other boolean parameters and is correctly placed within the method.
589-595
: LGTM: parseDSNParams function updated correctlyThe
parseDSNParams
function has been properly updated to handle the "trackSessionState" parameter. The implementation is consistent with other boolean parameters, includes appropriate error handling, and maintains consistency with theFormatDSN
method.
Line range hint
1-695
: Overall assessment: GTID tracking implementation looks goodThe changes made to implement GTID tracking functionality in the
dsn.go
file are well-executed and consistent. The newTrackSessionState
field has been properly added to theConfig
struct, and the necessary updates have been made to theFormatDSN
method andparseDSNParams
function. These changes align well with the PR objectives and maintain the existing code style and patterns.To further validate the implementation:
This script will help ensure that the new
TrackSessionState
field is being used correctly in other parts of the codebase, that DSN strings are being constructed properly with the new parameter, and that appropriate tests have been added for the new functionality.✅ Verification successful
Verification Complete: GTID Tracking Implementation Confirmed
The
TrackSessionState
field is properly utilized inpackets.go
, DSN strings include the necessary parameter indriver_test.go
, and relevant tests (TestGTIDTracking
) have been added. All aspects align with the PR objectives and maintain codebase integrity.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Verify that the TrackSessionState field is used correctly throughout the codebase # Check for any usage of TrackSessionState echo "Checking for usage of TrackSessionState:" rg --type go "TrackSessionState" --glob "!dsn.go" # Check for any references to trackSessionState in DSN strings echo "Checking for trackSessionState in DSN strings:" rg --type go 'trackSessionState=true' # Check for any new tests related to GTID tracking echo "Checking for new GTID tracking tests:" rg --type go "TestGTID" --glob "*_test.go"Length of output: 583
packets.go (3)
293-295
: Correctly enable session state tracking when configuredThe addition of the
clientSessionTrack
flag whenTrackSessionState
is enabled ensures that the client can receive session state change information from the server as intended.
550-550
: Initializing GTID slice for result trackingAppending an empty string to
mc.result.gtids
inreadResultSetHeaderPacket
initializes the GTID slice properly, aligning the GTID tracking with each result set.
Line range hint
659-736
: Verify the correctness of session state parsing inhandleOkPacket
The implementation for parsing session state changes and extracting GTIDs appears thorough. However, please verify that the loop correctly processes all session state changes without misalignment.
To confirm the loop processes the session states accurately, you can run the following script:
This script checks that the
handleOkPacket
function contains the correct loop structure for parsing session state changes.driver_test.go (1)
2621-2682
: Handle insufficient privileges when changing global variablesExecuting
SET GLOBAL
commands requires appropriate privileges. The test may fail if the user lacks these permissions. Consider adding checks to verify if the necessary privileges are available and skip the test or handle the error gracefully if they are not.To confirm whether the current user has the required permissions, run the following script:
// LastGTID returns the GTID of the last result, if available. | ||
LastGTID() (string, error) | ||
|
||
// AllLastGTIDs returns a slice containing |
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.
Incomplete method comment for AllLastGTIDs()
.
The comment for AllLastGTIDs()
is incomplete:
// AllLastGTIDs returns a slice containing
Please complete the comment to accurately describe what the method returns.
func (res *mysqlResult) LastGTID() (string, error) { | ||
return res.gtids[len(res.gtids)-1], nil | ||
} |
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.
Potential panic in LastGTID()
due to empty gtids
slice.
If res.gtids
is empty, accessing res.gtids[len(res.gtids)-1]
will cause an index out of range panic.
Consider adding a check to ensure res.gtids
is not empty before accessing the last element:
func (res *mysqlResult) LastGTID() (string, error) {
+ if len(res.gtids) == 0 {
+ return "", errors.New("no GTIDs available")
+ }
return res.gtids[len(res.gtids)-1], nil
}
Alternatively, define the expected behavior when gtids
is empty and handle it appropriately.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
func (res *mysqlResult) LastGTID() (string, error) { | |
return res.gtids[len(res.gtids)-1], nil | |
} | |
func (res *mysqlResult) LastGTID() (string, error) { | |
if len(res.gtids) == 0 { | |
return "", errors.New("no GTIDs available") | |
} | |
return res.gtids[len(res.gtids)-1], nil | |
} |
c := conn.(*mysqlConn) | ||
|
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.
Check type assertion when casting to *mysqlConn
The type assertion conn.(*mysqlConn)
may panic if conn
is not of the expected type. It's safer to check the assertion and handle the error gracefully.
Suggested fix:
- c := conn.(*mysqlConn)
+ c, ok := conn.(*mysqlConn)
+ if !ok {
+ return fmt.Errorf("expected *mysqlConn, got %T", conn)
+ }
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
c := conn.(*mysqlConn) | |
c, ok := conn.(*mysqlConn) | |
if !ok { | |
return fmt.Errorf("expected *mysqlConn, got %T", conn) | |
} |
t.Fatal(err) | ||
} | ||
|
||
gtid, err = res.(Result).LastGTID() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if gtid != "" { | ||
t.Fatalf("expected empty gtid, got %v", gtid) | ||
} | ||
|
||
_, err = c.Exec("SET SESSION session_track_gtids = ALL_GTIDS", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
res, err = c.Exec("INSERT INTO test (value) VALUES ('a'), ('b')", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
gtid, err = res.(Result).LastGTID() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if gtid == "" { | ||
t.Fatal("expected non-empty gtid") | ||
} |
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.
Avoid calling t.Fatal
inside conn.Raw
callbacks
Calling t.Fatal
inside the conn.Raw
callback can cause unexpected behavior because it might not correctly terminate the test when called from a nested function. Instead, return the error from the callback and handle it after the conn.Raw
call.
Suggested fix:
conn.Raw(func(conn any) error {
c, ok := conn.(*mysqlConn)
if !ok {
return fmt.Errorf("expected *mysqlConn, got %T", conn)
}
res, err := c.Exec("INSERT INTO test (value) VALUES ('a'), ('b')", nil)
if err != nil {
- t.Fatal(err)
+ return err
}
gtid, err = res.(Result).LastGTID()
if err != nil {
- t.Fatal(err)
+ return err
}
if gtid != "" {
- t.Fatalf("expected empty gtid, got %v", gtid)
+ return fmt.Errorf("expected empty gtid, got %v", gtid)
}
_, err = c.Exec("SET SESSION session_track_gtids = ALL_GTIDS", nil)
if err != nil {
- t.Fatal(err)
+ return err
}
res, err = c.Exec("INSERT INTO test (value) VALUES ('a'), ('b')", nil)
if err != nil {
- t.Fatal(err)
+ return err
}
gtid, err = res.(Result).LastGTID()
if err != nil {
- t.Fatal(err)
+ return err
}
if gtid == "" {
- t.Fatal("expected non-empty gtid")
+ return fmt.Errorf("expected non-empty gtid")
}
return nil
})
+if err != nil {
+ t.Fatal(err)
+}
Committable suggestion was skipped due to low confidence.
_, err := dbt.db.Exec("SET GLOBAL gtid_mode = OFF_PERMISSIVE") | ||
if err != nil { | ||
t.Fatalf("failed while trying to reset gtid_mode: %v", err) | ||
} | ||
}() |
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.
Avoid calling t.Fatalf
inside deferred functions
Similar to the previous instance, replace t.Fatalf
with t.Errorf
inside deferred functions to prevent improper test termination.
Suggested fix:
defer func() {
_, err := dbt.db.Exec("SET GLOBAL gtid_mode = OFF_PERMISSIVE")
if err != nil {
- t.Fatalf("failed while trying to reset gtid_mode: %v", err)
+ t.Errorf("failed while trying to reset gtid_mode: %v", err)
}
}()
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
_, err := dbt.db.Exec("SET GLOBAL gtid_mode = OFF_PERMISSIVE") | |
if err != nil { | |
t.Fatalf("failed while trying to reset gtid_mode: %v", err) | |
} | |
}() | |
_, err := dbt.db.Exec("SET GLOBAL gtid_mode = OFF_PERMISSIVE") | |
if err != nil { | |
t.Errorf("failed while trying to reset gtid_mode: %v", err) | |
} | |
}() |
_, err := dbt.db.Exec("SET GLOBAL gtid_mode = OFF") | ||
if err != nil { | ||
t.Fatalf("failed while trying to reset gtid_mode: %v", err) | ||
} | ||
}() |
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.
Avoid calling t.Fatalf
inside deferred functions
Using t.Fatalf
inside a deferred function can lead to unexpected behavior because it may not stop the test execution as intended. Instead, use t.Errorf
to report the error and ensure the test fails by checking for errors after the deferred function executes.
Suggested fix:
defer func() {
_, err := dbt.db.Exec("SET GLOBAL gtid_mode = OFF")
if err != nil {
- t.Fatalf("failed while trying to reset gtid_mode: %v", err)
+ t.Errorf("failed while trying to reset gtid_mode: %v", err)
}
}()
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
_, err := dbt.db.Exec("SET GLOBAL gtid_mode = OFF") | |
if err != nil { | |
t.Fatalf("failed while trying to reset gtid_mode: %v", err) | |
} | |
}() | |
_, err := dbt.db.Exec("SET GLOBAL gtid_mode = OFF") | |
if err != nil { | |
t.Errorf("failed while trying to reset gtid_mode: %v", err) | |
} | |
}() |
_, err := dbt.db.Exec("SET GLOBAL gtid_mode = ON_PERMISSIVE") | ||
if err != nil { | ||
t.Fatalf("failed while trying to reset gtid_mode: %v", err) | ||
} | ||
}() |
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.
Avoid calling t.Fatalf
inside deferred functions
Using t.Fatalf
in deferred functions can cause interference with the test flow. Replace it with t.Errorf
and handle the error appropriately after the deferred function.
Suggested fix:
defer func() {
_, err := dbt.db.Exec("SET GLOBAL gtid_mode = ON_PERMISSIVE")
if err != nil {
- t.Fatalf("failed while trying to reset gtid_mode: %v", err)
+ t.Errorf("failed while trying to reset gtid_mode: %v", err)
}
}()
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
_, err := dbt.db.Exec("SET GLOBAL gtid_mode = ON_PERMISSIVE") | |
if err != nil { | |
t.Fatalf("failed while trying to reset gtid_mode: %v", err) | |
} | |
}() | |
_, err := dbt.db.Exec("SET GLOBAL gtid_mode = ON_PERMISSIVE") | |
if err != nil { | |
t.Errorf("failed while trying to reset gtid_mode: %v", err) | |
} | |
}() |
How important this feature is? I want to implement compression protocol support in next version. I don't want to merge any new features until next version is released. |
I also want this feature in my project. Although I forked this driver and implemented feature that could read gtids from the ok packet, I quiet expect official support. |
Description
This implements GTID tracking when GTID mode is enabled on the server,
TrackSessionState
connection config is enabled, andsession_track_gtids
is set to eitherALL_GTIDS
orOWN_GTID
.To access the GTID,
sql.Conn.Raw()
has to be used like this:There's also
AllLastGTIDs
onmysql.Result
for use with multi-statement queries.Checklist
Summary by CodeRabbit
New Features
Bug Fixes
Tests