From 33da01060531b58f05e422be676884cb221a8793 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Tue, 7 Apr 2026 17:02:29 +0800 Subject: [PATCH] feat: Implement deadlock retry mechanism for chunk creation Added a new utility function, WithDeadlockRetry, to handle MySQL deadlock errors during chunk creation. This function retries the operation up to three times with exponential backoff. Updated the CreateChunks method to utilize this new function, ensuring robust handling of concurrent write scenarios and preventing potential deadlocks. Additionally, improved documentation for CreateChunks to clarify the use of Omit for avoiding auto-increment column issues. --- internal/application/repository/chunk.go | 7 +++- internal/common/db_retry.go | 45 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 internal/common/db_retry.go diff --git a/internal/application/repository/chunk.go b/internal/application/repository/chunk.go index 990ebc12..daba1217 100644 --- a/internal/application/repository/chunk.go +++ b/internal/application/repository/chunk.go @@ -24,7 +24,12 @@ func NewChunkRepository(db *gorm.DB) interfaces.ChunkRepository { return &chunkRepository{db: db} } -// CreateChunks creates multiple chunks in batches +// CreateChunks creates multiple chunks in batches. +// Uses Omit("SeqID") so GORM won't include the auto-increment column in the +// INSERT, which avoids MySQL generating ON DUPLICATE KEY UPDATE and the +// resulting gap-lock deadlocks under concurrent writes. +// A deadlock retry wrapper is kept as defense-in-depth for any remaining +// edge cases on secondary unique indexes. func (r *chunkRepository) CreateChunks(ctx context.Context, chunks []*types.Chunk) error { for _, chunk := range chunks { chunk.Content = common.CleanInvalidUTF8(chunk.Content) diff --git a/internal/common/db_retry.go b/internal/common/db_retry.go new file mode 100644 index 00000000..c4ce4376 --- /dev/null +++ b/internal/common/db_retry.go @@ -0,0 +1,45 @@ +package common + +import ( + "context" + "strings" + "time" + + "github.com/Tencent/WeKnora/internal/logger" +) + +const defaultMaxDeadlockRetries = 3 + +// WithDeadlockRetry retries fn when MySQL returns Error 1213 (deadlock). +// Retries up to 3 times with exponential backoff (50ms, 100ms, 200ms). +// Safe for all DB drivers — non-deadlock errors are returned immediately. +func WithDeadlockRetry(ctx context.Context, fn func() error) error { + var err error + for attempt := 0; attempt <= defaultMaxDeadlockRetries; attempt++ { + err = fn() + if err == nil || !IsDeadlockError(err) { + return err + } + if attempt < defaultMaxDeadlockRetries { + wait := time.Duration(50<