Skip to content

[improvement] frontend: add sql to fatal log #21609

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

Open
wants to merge 5 commits into
base: 2.1-dev
Choose a base branch
from

Conversation

volgariver6
Copy link
Contributor

@volgariver6 volgariver6 commented Mar 27, 2025

User description

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #21602

What this PR does / why we need it:

add sql to fatal log


PR Type

Enhancement, Tests


Description

  • Enhanced StartStatement to include SQL string parameter.

  • Updated all relevant function calls to pass SQL string.

  • Improved error logging with SQL context in StartStatement.

  • Adjusted and added test cases to validate new functionality.


Changes walkthrough 📝

Relevant files
Enhancement
4 files
mysql_cmd_executor.go
Pass SQL string to `StartStatement` in executor                   
+1/-1     
sql_executor.go
Updated SQL executor to pass SQL string to `StartStatement`
+2/-2     
types.go
Modified Workspace interface to include SQL string in StartStatement
+1/-1     
types.go
Enhanced `StartStatement` with SQL string and improved logging
+5/-2     
Tests
6 files
txn_mock.go
Mock `StartStatement` updated to accept SQL string             
+4/-4     
txn_test.go
Adjusted tests to include SQL string in `StartStatement` 
+13/-13 
util_test.go
Updated mock expectations for `StartStatement` with SQL string
+2/-2     
disttae_engine_test.go
Updated tests to include SQL string in `StartStatement`   
+2/-2     
util.go
Adjusted utility function to pass SQL string to `StartStatement`
+1/-1     
workspace_test.go
Updated workspace tests to include SQL string in `StartStatement`
+6/-6     

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link

    Qodo Merge was enabled for this repository. To continue using it, please link your Git account with your Qodo account here.

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Error Handling

    The StartStatement function now includes SQL in the fatal error message, but it might be better to handle this error more gracefully rather than calling logutil.Fatal which terminates the process.

    if txn.startStatementCalled {
    	logutil.Fatal("BUG: StartStatement called twice",
    		zap.String("txn", hex.EncodeToString(txn.op.Txn().ID)),
    		zap.String("SQL", sql),
    	)
    Empty SQL Parameter

    Many test cases are passing empty strings as SQL parameters. Consider using more realistic SQL statements in tests to better validate the new functionality.

    wsp.StartStatement("")

    Copy link

    qodo-merge-pro bot commented Mar 27, 2025

    Qodo Merge was enabled for this repository. To continue using it, please link your Git account with your Qodo account here.

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Avoid fatal program termination
    Suggestion Impact:The commit partially implemented the suggestion by making the error handling more conditional. It now uses logutil.Error instead of logutil.Fatal, but only in test environments (when flag.Lookup('test.v') is not nil). The function signature was also changed to accept a tree.Statement instead of a string.

    code diff:

     	if txn.startStatementCalled {
    -		logutil.Fatal("BUG: StartStatement called twice",
    +		var sql string
    +		if stmt != nil {
    +			fmtCtx := tree.NewFmtCtx(dialect.MYSQL, tree.WithQuoteString(true))
    +			stmt.Format(fmtCtx)
    +			sql = fmtCtx.String()
    +		}
    +		log := logutil.Fatal
    +		if flag.Lookup("test.v") != nil {
    +			log = logutil.Error
    +		}
    +		log("BUG: StartStatement called twice",
     			zap.String("txn", hex.EncodeToString(txn.op.Txn().ID)),
     			zap.String("SQL", sql),
     		)

    The current implementation uses logutil.Fatal which will terminate the program
    when StartStatement is called twice. Consider using a less drastic approach like
    logging an error and returning, especially since this is now capturing the SQL
    statement for debugging purposes.

    pkg/vm/engine/disttae/types.go [528-537]

     func (txn *Transaction) StartStatement(sql string) {
     	if txn.startStatementCalled {
    -		logutil.Fatal("BUG: StartStatement called twice",
    +		logutil.Error("BUG: StartStatement called twice",
     			zap.String("txn", hex.EncodeToString(txn.op.Txn().ID)),
     			zap.String("SQL", sql),
     		)
    +		return
     	}
     	txn.startStatementCalled = true
     	txn.incrStatementCalled = false
     }

    [Suggestion has been applied]

    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly identifies a potential reliability issue. Using logutil.Fatal causes program termination when StartStatement is called twice, which is unnecessarily drastic. Logging the error and returning would allow the system to continue operating, making it more resilient while still capturing the debugging information.

    Medium
    • Update

    Copy link
    Contributor

    mergify bot commented Mar 27, 2025

    This pull request has been removed from the queue for the following reason: checks failed.

    The merge conditions cannot be satisfied due to failing checks:

    You may have to fix your CI before adding the pull request to the queue again.

    If you want to requeue this pull request, you can post a @mergifyio requeue comment.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    kind/enhancement Review effort 2/5 size/M Denotes a PR that changes [100,499] lines
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    7 participants