fix: the count of failed deletion is incorrect in cluster mode

This commit is contained in:
Lykin 2024-04-26 16:50:43 +08:00
parent 8139481ea7
commit 2133fe44ca
1 changed files with 6 additions and 9 deletions

View File

@ -2070,8 +2070,8 @@ func (b *browserService) DeleteKey(server string, db int, k any, async bool) (re
handleDel := func(ks []string) error { handleDel := func(ks []string) error {
var delErr error var delErr error
if async && supportUnlink { if async && supportUnlink {
supportUnlink = false
if delErr = cli.Unlink(ctx, ks...).Err(); delErr != nil { if delErr = cli.Unlink(ctx, ks...).Err(); delErr != nil {
supportUnlink = false
// not support unlink? try del command // not support unlink? try del command
delErr = cli.Del(ctx, ks...).Err() delErr = cli.Del(ctx, ks...).Err()
} }
@ -2195,7 +2195,6 @@ func (b *browserService) DeleteKeys(server string, db int, ks []any, serialNo st
cancelFunc() cancelFunc()
}) })
total := len(ks) total := len(ks)
var failed atomic.Int64
var canceled bool var canceled bool
var deletedKeys = make([]any, 0, total) var deletedKeys = make([]any, 0, total)
var mutex sync.Mutex var mutex sync.Mutex
@ -2210,9 +2209,7 @@ func (b *browserService) DeleteKeys(server string, db int, ks []any, serialNo st
} }
cmders, delErr := pipe.Exec(ctx) cmders, delErr := pipe.Exec(ctx)
for j, cmder := range cmders { for j, cmder := range cmders {
if cmder.(*redis.IntCmd).Val() != 1 { if cmder.(*redis.IntCmd).Val() == 1 {
failed.Add(1)
} else {
// save deleted key // save deleted key
mutex.Lock() mutex.Lock()
deletedKeys = append(deletedKeys, ks[i+j]) deletedKeys = append(deletedKeys, ks[i+j])
@ -2239,13 +2236,13 @@ func (b *browserService) DeleteKeys(server string, db int, ks []any, serialNo st
cancelStopEvent() cancelStopEvent()
resp.Success = true resp.Success = true
resp.Data = struct { resp.Data = struct {
Canceled bool `json:"canceled"` Canceled bool `json:"canceled"`
Deleted any `json:"deleted"` Deleted any `json:"deleted"`
Failed int64 `json:"failed"` Failed int `json:"failed"`
}{ }{
Canceled: canceled, Canceled: canceled,
Deleted: deletedKeys, Deleted: deletedKeys,
Failed: failed.Load(), Failed: len(ks) - len(deletedKeys),
} }
return return
} }