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

refactor: send commands in pipeline #754

Draft
wants to merge 1 commit into
base: v4
Choose a base branch
from
Draft
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
24 changes: 21 additions & 3 deletions internal/client/redis.go
Original file line number Diff line number Diff line change
@@ -92,18 +92,36 @@ func (r *Redis) Send(args ...string) {
if err != nil {
log.Panicf(err.Error())
}
r.flush()
r.Flush()
}

func (r *Redis) SendToBuffer(args ...string) {
argsInterface := make([]interface{}, len(args))
for inx, item := range args {
argsInterface[inx] = item
}
err := r.protoWriter.WriteArgs(argsInterface)
if err != nil {
log.Panicf(err.Error())
}
}

func (r *Redis) SendBytes(buf []byte) {
_, err := r.writer.Write(buf)
if err != nil {
log.Panicf(err.Error())
}
r.flush()
r.Flush()
}

func (r *Redis) SendBytesToBuffer(buf []byte) {
_, err := r.writer.Write(buf)
if err != nil {
log.Panicf(err.Error())
}
}

func (r *Redis) flush() {
func (r *Redis) Flush() {
err := r.writer.Flush()
if err != nil {
log.Panicf(err.Error())
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ type AdvancedOptions struct {
// ignore: redis-shake will skip restore the key when meet "Target key name is busy" error.
RDBRestoreCommandBehavior string `mapstructure:"rdb_restore_command_behavior" default:"panic"`

PipelineCountLimit uint64 `mapstructure:"pipeline_count_limit" default:"1024"`
PipelineCountLimit uint32 `mapstructure:"pipeline_count_limit" default:"1024"`
TargetRedisClientMaxQuerybufLen int64 `mapstructure:"target_redis_client_max_querybuf_len" default:"1024000000"`
TargetRedisProtoMaxBulkLen uint64 `mapstructure:"target_redis_proto_max_bulk_len" default:"512000000"`

14 changes: 13 additions & 1 deletion internal/writer/redis_standalone_writer.go
Original file line number Diff line number Diff line change
@@ -38,6 +38,9 @@ type redisStandaloneWriter struct {
UnansweredBytes int64 `json:"unanswered_bytes"`
UnansweredEntries int64 `json:"unanswered_entries"`
}

// the number of commands cached in the pipeline
bufCnt atomic.Uint32
}

func NewRedisStandaloneWriter(opts *RedisWriterOptions) Writer {
@@ -58,6 +61,9 @@ func NewRedisStandaloneWriter(opts *RedisWriterOptions) Writer {
}

func (w *redisStandaloneWriter) Close() {
if w.bufCnt.Load() > 0 {
w.client.Flush()
}
if !w.offReply {
close(w.chWaitReply)
w.chWg.Wait()
@@ -81,7 +87,13 @@ func (w *redisStandaloneWriter) Write(e *entry.Entry) {
atomic.AddInt64(&w.stat.UnansweredBytes, e.SerializedSize)
atomic.AddInt64(&w.stat.UnansweredEntries, 1)
}
w.client.SendBytes(bytes)

limit := config.Opt.Advanced.PipelineCountLimit
w.client.SendBytesToBuffer(bytes)
if w.bufCnt.Add(1) >= limit {
w.bufCnt.CompareAndSwap(limit, 0)
w.client.Flush()
}
}

func (w *redisStandaloneWriter) switchDbTo(newDbId int) {