-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Help to recover from corrupted levelqueue #24912
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9cd00ad
fix
wxiaoguang d27b4ac
improve tests
wxiaoguang 9b715c1
Merge branch 'main' into help-corrupted-levelquque
wxiaoguang feed53f
Merge branch 'main' into help-corrupted-levelquque
wxiaoguang ffe49ad
Merge branch 'main' into help-corrupted-levelquque
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,18 +6,21 @@ package queue | |
import ( | ||
"context" | ||
"sync" | ||
"unsafe" | ||
"sync/atomic" | ||
|
||
"code.gitea.io/gitea/modules/nosql" | ||
"code.gitea.io/gitea/modules/queue/lqinternal" | ||
|
||
"gitea.com/lunny/levelqueue" | ||
"github.com/syndtr/goleveldb/leveldb" | ||
) | ||
|
||
type baseLevelQueueUnique struct { | ||
internal *levelqueue.UniqueQueue | ||
conn string | ||
cfg *BaseConfig | ||
internal atomic.Pointer[levelqueue.UniqueQueue] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why must this be atomic now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because |
||
|
||
conn string | ||
cfg *BaseConfig | ||
db *leveldb.DB | ||
|
||
mu sync.Mutex // the levelqueue.UniqueQueue is not thread-safe, there is no mutex protecting the underlying queue&set together | ||
} | ||
|
@@ -29,68 +32,57 @@ func newBaseLevelQueueUnique(cfg *BaseConfig) (baseQueue, error) { | |
if err != nil { | ||
return nil, err | ||
} | ||
q := &baseLevelQueueUnique{conn: conn, cfg: cfg} | ||
q.internal, err = levelqueue.NewUniqueQueue(db, []byte(cfg.QueueFullName), []byte(cfg.SetFullName), false) | ||
q := &baseLevelQueueUnique{conn: conn, cfg: cfg, db: db} | ||
lq, err := levelqueue.NewUniqueQueue(db, []byte(cfg.QueueFullName), []byte(cfg.SetFullName), false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
q.internal.Store(lq) | ||
return q, nil | ||
} | ||
|
||
func (q *baseLevelQueueUnique) PushItem(ctx context.Context, data []byte) error { | ||
return baseLevelQueueCommon(q.cfg, q.internal, &q.mu).PushItem(ctx, data) | ||
c := baseLevelQueueCommon(q.cfg, &q.mu, func() baseLevelQueuePushPoper { return q.internal.Load() }) | ||
return c.PushItem(ctx, data) | ||
} | ||
|
||
func (q *baseLevelQueueUnique) PopItem(ctx context.Context) ([]byte, error) { | ||
return baseLevelQueueCommon(q.cfg, q.internal, &q.mu).PopItem(ctx) | ||
c := baseLevelQueueCommon(q.cfg, &q.mu, func() baseLevelQueuePushPoper { return q.internal.Load() }) | ||
return c.PopItem(ctx) | ||
} | ||
|
||
func (q *baseLevelQueueUnique) HasItem(ctx context.Context, data []byte) (bool, error) { | ||
q.mu.Lock() | ||
defer q.mu.Unlock() | ||
return q.internal.Has(data) | ||
return q.internal.Load().Has(data) | ||
} | ||
|
||
func (q *baseLevelQueueUnique) Len(ctx context.Context) (int, error) { | ||
q.mu.Lock() | ||
defer q.mu.Unlock() | ||
return int(q.internal.Len()), nil | ||
return int(q.internal.Load().Len()), nil | ||
} | ||
|
||
func (q *baseLevelQueueUnique) Close() error { | ||
q.mu.Lock() | ||
defer q.mu.Unlock() | ||
err := q.internal.Close() | ||
err := q.internal.Load().Close() | ||
q.db = nil // the db is not managed by us, it's managed by the nosql manager | ||
_ = nosql.GetManager().CloseLevelDB(q.conn) | ||
return err | ||
} | ||
|
||
func (q *baseLevelQueueUnique) RemoveAll(ctx context.Context) error { | ||
q.mu.Lock() | ||
defer q.mu.Unlock() | ||
|
||
type levelUniqueQueue struct { | ||
q *levelqueue.Queue | ||
set *levelqueue.Set | ||
db *leveldb.DB | ||
} | ||
lq := (*levelUniqueQueue)(unsafe.Pointer(q.internal)) | ||
|
||
for lq.q.Len() > 0 { | ||
if _, err := lq.q.LPop(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// the "set" must be cleared after the "list" because there is no transaction. | ||
// it's better to have duplicate items than losing items. | ||
members, err := lq.set.Members() | ||
lqinternal.RemoveLevelQueueKeys(q.db, []byte(q.cfg.QueueFullName)) | ||
lqinternal.RemoveLevelQueueKeys(q.db, []byte(q.cfg.SetFullName)) | ||
lq, err := levelqueue.NewUniqueQueue(q.db, []byte(q.cfg.QueueFullName), []byte(q.cfg.SetFullName), false) | ||
if err != nil { | ||
return err // seriously corrupted | ||
} | ||
for _, v := range members { | ||
_, _ = lq.set.Remove(v) | ||
return err | ||
} | ||
old := q.internal.Load() | ||
q.internal.Store(lq) | ||
_ = old.Close() // Not ideal for concurrency. Luckily, the levelqueue only sets its db=nil because it doesn't manage the db, so far so good | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package lqinternal | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
|
||
"github.com/syndtr/goleveldb/leveldb" | ||
"github.com/syndtr/goleveldb/leveldb/opt" | ||
) | ||
|
||
func QueueItemIDBytes(id int64) []byte { | ||
buf := make([]byte, 8) | ||
binary.PutVarint(buf, id) | ||
return buf | ||
} | ||
|
||
func QueueItemKeyBytes(prefix []byte, id int64) []byte { | ||
key := make([]byte, len(prefix), len(prefix)+1+8) | ||
copy(key, prefix) | ||
key = append(key, '-') | ||
return append(key, QueueItemIDBytes(id)...) | ||
} | ||
|
||
func RemoveLevelQueueKeys(db *leveldb.DB, namePrefix []byte) { | ||
keyPrefix := make([]byte, len(namePrefix)+1) | ||
copy(keyPrefix, namePrefix) | ||
keyPrefix[len(namePrefix)] = '-' | ||
|
||
it := db.NewIterator(nil, &opt.ReadOptions{Strict: opt.NoStrict}) | ||
defer it.Release() | ||
for it.Next() { | ||
if bytes.HasPrefix(it.Key(), keyPrefix) { | ||
_ = db.Delete(it.Key(), nil) | ||
} | ||
} | ||
} | ||
|
||
func ListLevelQueueKeys(db *leveldb.DB) (res [][]byte) { | ||
it := db.NewIterator(nil, &opt.ReadOptions{Strict: opt.NoStrict}) | ||
defer it.Release() | ||
for it.Next() { | ||
res = append(res, it.Key()) | ||
} | ||
return res | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why does deleting an item corrupt the queue?
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.
See this PR's description:
if the keys in LevelDB went out-of-sync, the LevelQueue itself doesn't have the ability to recover, eg:
It's levelqueue's bug, not Gitea's. The levelqueue.LPop was written as this.