Skip to content
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

Implement bulk key fetching and deleting for large queue #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,11 @@ func mustInitializeQueueBackend() {
queueBackend, err = backendfactory.NewBackend(logger, backendconfig.Config{
BackendType: cmdOpts.Backend,
Redis: &backendconfig.RedisConfig{
KeyPrefix: cmdOpts.Redis.KeyPrefix,
Client: cmdOpts.Redis.NewClient(),
Backoff: cmdOpts.Redis.Backoff,
ChunkSizeInGet: cmdOpts.Redis.ChunkSizeInGet,
KeyPrefix: cmdOpts.Redis.KeyPrefix,
Client: cmdOpts.Redis.NewClient(),
Backoff: cmdOpts.Redis.Backoff,
ChunkSizeInGet: cmdOpts.Redis.ChunkSizeInGet,
ChunkSizeInDelete: cmdOpts.Redis.ChunkSizeInDelete,
},
})

Expand Down
12 changes: 7 additions & 5 deletions pkg/backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ type Config struct {
}

type RedisConfig struct {
KeyPrefix string
Client *redis.Client
Backoff BackoffConfig
ChunkSizeInGet int
KeyPrefix string
Client *redis.Client
Backoff BackoffConfig
ChunkSizeInGet int
ChunkSizeInDelete int
}

// TODO: support UniversalOptions
Expand All @@ -52,7 +53,8 @@ type RedisClientConfig struct {
IdleTimeout time.Duration `json:"idleTimeout" yaml:"idleTimeout" default:"5m"`
IdleCheckFrequency time.Duration `json:"idleCheckFrequency" yaml:"idleCheckFrequency" default:"1m"`

ChunkSizeInGet int `json:"chunkSizeInGet" yaml:"chunkSizeInGet" default:"10000"`
ChunkSizeInGet int `json:"chunkSizeInGet" yaml:"chunkSizeInGet" default:"10000"`
ChunkSizeInDelete int `json:"chunkSizeInDelete" yaml:"chunkSizeInGet" default:"1000"`
shioshiota marked this conversation as resolved.
Show resolved Hide resolved
}

func (c RedisClientConfig) NewClient() *redis.Client {
Expand Down
12 changes: 10 additions & 2 deletions pkg/backend/redis/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (b *Backend) DeleteQueue(ctx context.Context, queueName string) error {
// .. task_keys = collect task keys
// WATCh task_keys
// MULTI
// DEL {queue_key} worker_keys task_keys
// UNLINK {queue_key} worker_keys task_keys
shioshiota marked this conversation as resolved.
Show resolved Hide resolved
// HDEL {all_queues_key} {queueName}
// EXEC
txf := func(tx *redis.Tx) error {
Expand All @@ -240,8 +240,16 @@ func (b *Backend) DeleteQueue(ctx context.Context, queueName string) error {
tx.Watch(taskKeysToDelete...)
keysToDelete = append(keysToDelete, taskKeysToDelete...)

chunkSize := b.ChunkSizeInGet
shioshiota marked this conversation as resolved.
Show resolved Hide resolved
numOfKeysToDelete := len(keysToDelete)
_, err = tx.TxPipelined(func(pipe redis.Pipeliner) error {
pipe.Del(keysToDelete...)
for begin := 0; begin < numOfKeysToDelete; begin += chunkSize {
end := begin + chunkSize
if end > numOfKeysToDelete {
end = numOfKeysToDelete
}
pipe.Unlink(keysToDelete[begin:end]...)
}
pipe.HDel(b.allQueuesKey(), queue.Spec.Name)
return nil
})
Expand Down
9 changes: 5 additions & 4 deletions pkg/backend/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ var _ = Describe("Backend", func() {
ibackend, err := NewBackend(logger, backendconfig.Config{
BackendType: "redis",
Redis: &backendconfig.RedisConfig{
KeyPrefix: "test",
Client: client,
Backoff: backoffConfig,
ChunkSizeInGet: 1000,
KeyPrefix: "test",
Client: client,
Backoff: backoffConfig,
ChunkSizeInGet: 1000,
ChunkSizeInDelete: 1000,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding tests for chunked deletion (large queue)??

Copy link
Contributor Author

@shioshiota shioshiota Sep 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test for chunked deletion.
f2b8503

},
})
Expect(err).NotTo(HaveOccurred())
Expand Down
31 changes: 23 additions & 8 deletions pkg/backend/redis/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ func (b *Backend) getTasksByUIDs(queueUID string, taskUIDs []string, filter func
}

func (b *Backend) getTasks(queueUID string, filter func(*task.Task) bool, lggr zerolog.Logger) ([]*task.Task, error) {
taskUIDs, err := b.Client.SMembers(b.tasksKey(queueUID)).Result()
if err == redis.Nil {
return []*task.Task{}, nil
}
taskUIDs, err := b.allTaskUIDsByQueueUID(b.Client, queueUID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -938,10 +935,7 @@ func (b *Backend) allTasksKeysForDeleteQueue(rds redis.Cmdable, queueUID string)
b.deadletterQueueKey(queueUID),
b.pendingTaskQueueKey(queueUID),
}
taskUIDs, err := rds.SMembers(b.tasksKey(queueUID)).Result()
if err == redis.Nil {
return []string{}, nil
}
taskUIDs, err := b.allTaskUIDsByQueueUID(rds, queueUID)
if err != nil {
return []string{}, err
}
Expand All @@ -950,3 +944,24 @@ func (b *Backend) allTasksKeysForDeleteQueue(rds redis.Cmdable, queueUID string)
}
return keysToDelete, nil
}

func (b *Backend) allTaskUIDsByQueueUID(rds redis.Cmdable, queueUID string) ([]string, error) {
var chunkSize = int64(b.ChunkSizeInGet)
var cursor uint64
var taskUIDs []string
for {
keys, nextCursor, err := rds.SScan(b.tasksKey(queueUID), cursor, "", chunkSize).Result()
if err == redis.Nil {
return []string{}, nil
}
if err != nil {
return []string{}, err
}
taskUIDs = append(taskUIDs, keys...)
cursor = nextCursor
if cursor == 0 {
break
}
}
return taskUIDs, nil
}
7 changes: 4 additions & 3 deletions pkg/worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ var _ = Describe("Worker", func() {
bcknd, err = backendfactory.NewBackend(logger, backendconfig.Config{
BackendType: "redis",
Redis: &backendconfig.RedisConfig{
Client: client,
Backoff: backendConfig,
ChunkSizeInGet: 1000,
Client: client,
Backoff: backendConfig,
ChunkSizeInGet: 1000,
ChunkSizeInDelete: 1000,
},
})
Expect(err).NotTo(HaveOccurred())
Expand Down