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

Optimize enqueueing tasks performance #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
44 changes: 32 additions & 12 deletions internal/rdb/rdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"fmt"
"math"
"sync"
"time"

"github.com/google/uuid"
Expand All @@ -28,6 +29,7 @@ const LeaseDuration = 30 * time.Second
type RDB struct {
client redis.UniversalClient
clock timeutil.Clock
m sync.Map
}

// NewRDB returns a new instance of RDB.
Expand Down Expand Up @@ -112,8 +114,11 @@ func (r *RDB) Enqueue(ctx context.Context, msg *base.TaskMessage) error {
if err != nil {
return errors.E(op, errors.Unknown, fmt.Sprintf("cannot encode message: %v", err))
}
if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
if _, ok := r.m.Load(msg.Queue); !ok {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this for atomicity:

Suggested change
if _, ok := r.m.Load(msg.Queue); !ok {
if previous, _ := r.m.Swap(msg.Queue, true); previous == nil {

Since we have many nodes, we probably don't need to check the error.

if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
}
r.m.Store(msg.Queue, struct{}{})
}
keys := []string{
base.TaskKey(msg.Queue, msg.ID),
Expand Down Expand Up @@ -174,8 +179,11 @@ func (r *RDB) EnqueueUnique(ctx context.Context, msg *base.TaskMessage, ttl time
if err != nil {
return errors.E(op, errors.Internal, "cannot encode task message: %v", err)
}
if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
if _, ok := r.m.Load(msg.Queue); !ok {
if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
}
r.m.Store(msg.Queue, struct{}{})
}
keys := []string{
msg.UniqueKey,
Expand Down Expand Up @@ -529,8 +537,11 @@ func (r *RDB) AddToGroup(ctx context.Context, msg *base.TaskMessage, groupKey st
if err != nil {
return errors.E(op, errors.Unknown, fmt.Sprintf("cannot encode message: %v", err))
}
if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
if _, ok := r.m.Load(msg.Queue); !ok {
if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
}
r.m.Store(msg.Queue, struct{}{})
}
keys := []string{
base.TaskKey(msg.Queue, msg.ID),
Expand Down Expand Up @@ -591,8 +602,11 @@ func (r *RDB) AddToGroupUnique(ctx context.Context, msg *base.TaskMessage, group
if err != nil {
return errors.E(op, errors.Unknown, fmt.Sprintf("cannot encode message: %v", err))
}
if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
if _, ok := r.m.Load(msg.Queue); !ok {
if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
}
r.m.Store(msg.Queue, struct{}{})
}
keys := []string{
base.TaskKey(msg.Queue, msg.ID),
Expand Down Expand Up @@ -648,8 +662,11 @@ func (r *RDB) Schedule(ctx context.Context, msg *base.TaskMessage, processAt tim
if err != nil {
return errors.E(op, errors.Unknown, fmt.Sprintf("cannot encode message: %v", err))
}
if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
if _, ok := r.m.Load(msg.Queue); !ok {
if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
}
r.m.Store(msg.Queue, struct{}{})
}
keys := []string{
base.TaskKey(msg.Queue, msg.ID),
Expand Down Expand Up @@ -707,8 +724,11 @@ func (r *RDB) ScheduleUnique(ctx context.Context, msg *base.TaskMessage, process
if err != nil {
return errors.E(op, errors.Internal, fmt.Sprintf("cannot encode task message: %v", err))
}
if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
if _, ok := r.m.Load(msg.Queue); !ok {
if err := r.client.SAdd(ctx, base.AllQueues, msg.Queue).Err(); err != nil {
return errors.E(op, errors.Unknown, &errors.RedisCommandError{Command: "sadd", Err: err})
}
r.m.Store(msg.Queue, struct{}{})
}
keys := []string{
msg.UniqueKey,
Expand Down
43 changes: 43 additions & 0 deletions internal/rdb/rdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,49 @@ func TestEnqueueTaskIdConflictError(t *testing.T) {
}
}

func TestEnqueueQueueCache(t *testing.T) {
r := setup(t)
defer r.Close()
t1 := h.NewTaskMessageWithQueue("sync1", nil, "q1")
t2 := h.NewTaskMessageWithQueue("sync2", nil, "q2")
t3 := h.NewTaskMessageWithQueue("sync3", nil, "q1")

err := r.Enqueue(context.Background(), t1)
if err != nil {
t.Fatalf("(*RDB).Enqueue(msg) = %v, want nil", err)
}

// Check queue is in the AllQueues set.
if !r.client.SIsMember(context.Background(), base.AllQueues, t1.Queue).Val() {
t.Fatalf("%q is not a member of SET %q", t1.Queue, base.AllQueues)
}

err = r.Enqueue(context.Background(), t2)
if err != nil {
t.Fatalf("(*RDB).Enqueue(msg) = %v, want nil", err)
}

if !r.client.SIsMember(context.Background(), base.AllQueues, t2.Queue).Val() {
t.Fatalf("%q is not a member of SET %q", t2.Queue, base.AllQueues)
}

// Delete q1 from AllQueues
err = r.client.SRem(context.Background(), base.AllQueues, "q1").Err()
if err != nil {
t.Fatalf("Redis SREM = %v, want nil", err)
}

// Enqueue another task to q1, won't update the queue because already cached in-memory
err = r.Enqueue(context.Background(), t3)
if err != nil {
t.Fatalf("(*RDB).Enqueue(msg) = %v, want nil", err)
}

if r.client.SIsMember(context.Background(), base.AllQueues, t3.Queue).Val() {
t.Fatalf("%q is a member of SET %q", t3.Queue, base.AllQueues)
}
}

func TestEnqueueUnique(t *testing.T) {
r := setup(t)
defer r.Close()
Expand Down
Loading