Skip to content

fix allocate id - #22006

Merged
mergify[bot] merged 1 commit into
matrixorigin:2.2-devfrom
aptend:alloc-id-2.2
Jun 12, 2025
Merged

mergify[bot] merged 1 commit into
matrixorigin:2.2-devfrom
aptend:alloc-id-2.2

Conversation

@aptend

@aptend aptend commented Jun 11, 2025 •

Copy link
Copy Markdown
Contributor

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 #22005

What this PR does / why we need it:

  • Fix incorrect error handling in the AllocateID function

PR Type

Bug fix


Description

• Fixed incorrect error handling in AllocateID function
• Added proper mutex unlock and error return logic
• Improved error logging for ID allocation failures
• Applied code formatting improvements to function signatures


Changes walkthrough 📝

Relevant files
Bug fix
hakeeper_client.go
Fix AllocateID error handling and formatting                         

pkg/logservice/hakeeper_client.go

• Fixed AllocateID function to properly handle errors and unlock mutex

• Added defer statement for mutex unlock to prevent deadlocks
•
Improved error logging with proper error return instead of continuing
loop
• Applied code formatting to multiple function signatures for
better readability

+53/-24 

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @qodo-code-review

    Copy link
    Copy Markdown

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis 🔶

    22005 - Partially compliant

    Compliant requirements:

    • Fix ID allocation problems through improved error handling in AllocateID function

    Non-compliant requirements:

    • Direct resolution of "Unknown database tpch_1g" error is not evident in the code changes
    • "internal error: reserved table ID 6" issue resolution is not clearly addressed

    Requires further human verification:

    • Verification that the AllocateID fix resolves the database creation and table ID reservation errors
    • Testing in standalone regression environment to confirm the fix works end-to-end

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

    Logic Error

    The AllocateID function has a critical flaw where it returns firstID and err after successful allocation, but err might still contain the previous error from the retry loop. This could return a valid ID with a non-nil error, causing inconsistent behavior.

    	return firstID, err
    }
    Missing Logic

    After successful ID allocation, the function should validate that firstID is not 0 before returning, similar to the validation done in the fast path. The removed validation logic for zero IDs is no longer present.

    c.mu.sharedAllocID.nextID = firstID + 1
    c.mu.sharedAllocID.lastID = firstID + c.cfg.AllocateIDBatch - 1
    return firstID, err

    @mergify
    mergify Bot requested a review from XuPeng-SH June 11, 2025 03:50
    @mergify mergify Bot added the kind/bug Something isn't working label Jun 11, 2025
    @qodo-code-review

    qodo-code-review Bot commented Jun 11, 2025 •

    Copy link
    Copy Markdown

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Fix mutex unlock placement

    The defer c.mu.Unlock() statement is placed after the early return path, which
    means it won't execute when the mutex is already unlocked in the if block. This
    creates inconsistent mutex handling and potential race conditions.

    pkg/logservice/hakeeper_client.go [346-359]

     c.mu.Lock()
    +defer c.mu.Unlock()
    +
     if c.mu.sharedAllocID.nextID != c.mu.sharedAllocID.lastID {
         v := c.mu.sharedAllocID.nextID
         c.mu.sharedAllocID.nextID++
    -    c.mu.Unlock()
         if v == 0 {
             logutil.Error("id should not be 0",
                 zap.Uint64("nextID", c.mu.sharedAllocID.nextID),
                 zap.Uint64("lastID", c.mu.sharedAllocID.lastID))
         }
         return v, nil
     }
     
    -defer c.mu.Unlock()
    -

    [To ensure code accuracy, apply this suggestion manually]

    Suggestion importance[1-10]: 9

    __

    Why: The suggestion correctly identifies a critical issue. The current PR code unlocks the mutex twice if the initial if condition is met, leading to a panic. The proposed change moves defer c.mu.Unlock() to right after c.mu.Lock() and removes the manual c.mu.Unlock(), which is the standard and correct way to handle mutexes in Go, preventing deadlocks and double-unlock panics.

    High
    • Update

    This branch was previously deployed

    1 inactive deployment
    ci — eb1e3cf0 Deployed Jun 12, 2025 by mergify[bot]
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    kind/bug Something isn't working Review effort 3/5 size/S Denotes a PR that changes [10,99] lines

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    4 participants