Skip to content

Commit 3cb5ab3

Browse files
authored
Merge branch 'master' into ndyakov/state-machine-conn
2 parents 41024f7 + c176672 commit 3cb5ab3

File tree

3 files changed

+218
-1
lines changed

3 files changed

+218
-1
lines changed

command.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3794,6 +3794,83 @@ func (cmd *SlowLogCmd) readReply(rd *proto.Reader) error {
37943794

37953795
//-----------------------------------------------------------------------
37963796

3797+
type Latency struct {
3798+
Name string
3799+
Time time.Time
3800+
Latest time.Duration
3801+
Max time.Duration
3802+
}
3803+
3804+
type LatencyCmd struct {
3805+
baseCmd
3806+
val []Latency
3807+
}
3808+
3809+
var _ Cmder = (*LatencyCmd)(nil)
3810+
3811+
func NewLatencyCmd(ctx context.Context, args ...interface{}) *LatencyCmd {
3812+
return &LatencyCmd{
3813+
baseCmd: baseCmd{
3814+
ctx: ctx,
3815+
args: args,
3816+
},
3817+
}
3818+
}
3819+
3820+
func (cmd *LatencyCmd) SetVal(val []Latency) {
3821+
cmd.val = val
3822+
}
3823+
3824+
func (cmd *LatencyCmd) Val() []Latency {
3825+
return cmd.val
3826+
}
3827+
3828+
func (cmd *LatencyCmd) Result() ([]Latency, error) {
3829+
return cmd.val, cmd.err
3830+
}
3831+
3832+
func (cmd *LatencyCmd) String() string {
3833+
return cmdString(cmd, cmd.val)
3834+
}
3835+
3836+
func (cmd *LatencyCmd) readReply(rd *proto.Reader) error {
3837+
n, err := rd.ReadArrayLen()
3838+
if err != nil {
3839+
return err
3840+
}
3841+
cmd.val = make([]Latency, n)
3842+
for i := 0; i < len(cmd.val); i++ {
3843+
nn, err := rd.ReadArrayLen()
3844+
if err != nil {
3845+
return err
3846+
}
3847+
if nn < 3 {
3848+
return fmt.Errorf("redis: got %d elements in latency get, expected at least 3", nn)
3849+
}
3850+
if cmd.val[i].Name, err = rd.ReadString(); err != nil {
3851+
return err
3852+
}
3853+
createdAt, err := rd.ReadInt()
3854+
if err != nil {
3855+
return err
3856+
}
3857+
cmd.val[i].Time = time.Unix(createdAt, 0)
3858+
latest, err := rd.ReadInt()
3859+
if err != nil {
3860+
return err
3861+
}
3862+
cmd.val[i].Latest = time.Duration(latest) * time.Millisecond
3863+
maximum, err := rd.ReadInt()
3864+
if err != nil {
3865+
return err
3866+
}
3867+
cmd.val[i].Max = time.Duration(maximum) * time.Millisecond
3868+
}
3869+
return nil
3870+
}
3871+
3872+
//-----------------------------------------------------------------------
3873+
37973874
type MapStringInterfaceCmd struct {
37983875
baseCmd
37993876

commands.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,13 @@ type Cmdable interface {
211211
ShutdownNoSave(ctx context.Context) *StatusCmd
212212
SlaveOf(ctx context.Context, host, port string) *StatusCmd
213213
SlowLogGet(ctx context.Context, num int64) *SlowLogCmd
214+
SlowLogLen(ctx context.Context) *IntCmd
215+
SlowLogReset(ctx context.Context) *StatusCmd
214216
Time(ctx context.Context) *TimeCmd
215217
DebugObject(ctx context.Context, key string) *StringCmd
216218
MemoryUsage(ctx context.Context, key string, samples ...int) *IntCmd
219+
Latency(ctx context.Context) *LatencyCmd
220+
LatencyReset(ctx context.Context, events ...interface{}) *StatusCmd
217221

218222
ModuleLoadex(ctx context.Context, conf *ModuleLoadexConfig) *StringCmd
219223

@@ -673,6 +677,34 @@ func (c cmdable) SlowLogGet(ctx context.Context, num int64) *SlowLogCmd {
673677
return cmd
674678
}
675679

680+
func (c cmdable) SlowLogLen(ctx context.Context) *IntCmd {
681+
cmd := NewIntCmd(ctx, "slowlog", "len")
682+
_ = c(ctx, cmd)
683+
return cmd
684+
}
685+
686+
func (c cmdable) SlowLogReset(ctx context.Context) *StatusCmd {
687+
cmd := NewStatusCmd(ctx, "slowlog", "reset")
688+
_ = c(ctx, cmd)
689+
return cmd
690+
}
691+
692+
func (c cmdable) Latency(ctx context.Context) *LatencyCmd {
693+
cmd := NewLatencyCmd(ctx, "latency", "latest")
694+
_ = c(ctx, cmd)
695+
return cmd
696+
}
697+
698+
func (c cmdable) LatencyReset(ctx context.Context, events ...interface{}) *StatusCmd {
699+
args := make([]interface{}, 2+len(events))
700+
args[0] = "latency"
701+
args[1] = "reset"
702+
copy(args[2:], events)
703+
cmd := NewStatusCmd(ctx, args...)
704+
_ = c(ctx, cmd)
705+
return cmd
706+
}
707+
676708
func (c cmdable) Sync(_ context.Context) {
677709
panic("not implemented")
678710
}

commands_test.go

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8294,7 +8294,7 @@ var _ = Describe("Commands", func() {
82948294
})
82958295
})
82968296

8297-
Describe("SlowLogGet", func() {
8297+
Describe("SlowLog", func() {
82988298
It("returns slow query result", func() {
82998299
const key = "slowlog-log-slower-than"
83008300

@@ -8311,6 +8311,114 @@ var _ = Describe("Commands", func() {
83118311
Expect(err).NotTo(HaveOccurred())
83128312
Expect(len(result)).NotTo(BeZero())
83138313
})
8314+
8315+
It("returns the number of slow queries", Label("NonRedisEnterprise"), func() {
8316+
// Reset slowlog
8317+
err := client.SlowLogReset(ctx).Err()
8318+
Expect(err).NotTo(HaveOccurred())
8319+
8320+
const key = "slowlog-log-slower-than"
8321+
8322+
old := client.ConfigGet(ctx, key).Val()
8323+
// first slowlog entry is the config set command itself
8324+
client.ConfigSet(ctx, key, "0")
8325+
defer client.ConfigSet(ctx, key, old[key])
8326+
8327+
// Set a key to trigger a slow query, and this is the second slowlog entry
8328+
client.Set(ctx, "test", "true", 0)
8329+
result, err := client.SlowLogLen(ctx).Result()
8330+
Expect(err).NotTo(HaveOccurred())
8331+
Expect(result).Should(Equal(int64(2)))
8332+
8333+
// Reset slowlog
8334+
err = client.SlowLogReset(ctx).Err()
8335+
Expect(err).NotTo(HaveOccurred())
8336+
8337+
// Check if slowlog is empty, this is the first slowlog entry after reset
8338+
result, err = client.SlowLogLen(ctx).Result()
8339+
Expect(err).NotTo(HaveOccurred())
8340+
Expect(result).Should(Equal(int64(1)))
8341+
})
8342+
})
8343+
8344+
Describe("Latency", Label("NonRedisEnterprise"), func() {
8345+
It("returns latencies", func() {
8346+
const key = "latency-monitor-threshold"
8347+
8348+
old := client.ConfigGet(ctx, key).Val()
8349+
client.ConfigSet(ctx, key, "1")
8350+
defer client.ConfigSet(ctx, key, old[key])
8351+
8352+
err := client.Do(ctx, "DEBUG", "SLEEP", 0.01).Err()
8353+
Expect(err).NotTo(HaveOccurred())
8354+
8355+
result, err := client.Latency(ctx).Result()
8356+
Expect(err).NotTo(HaveOccurred())
8357+
Expect(len(result)).NotTo(BeZero())
8358+
})
8359+
8360+
It("reset all latencies", func() {
8361+
const key = "latency-monitor-threshold"
8362+
8363+
result, err := client.Latency(ctx).Result()
8364+
// reset all latencies
8365+
err = client.LatencyReset(ctx).Err()
8366+
Expect(err).NotTo(HaveOccurred())
8367+
8368+
old := client.ConfigGet(ctx, key).Val()
8369+
client.ConfigSet(ctx, key, "1")
8370+
defer client.ConfigSet(ctx, key, old[key])
8371+
8372+
// get latency after reset
8373+
result, err = client.Latency(ctx).Result()
8374+
Expect(err).NotTo(HaveOccurred())
8375+
Expect(len(result)).Should(Equal(0))
8376+
8377+
// create a new latency
8378+
err = client.Do(ctx, "DEBUG", "SLEEP", 0.01).Err()
8379+
Expect(err).NotTo(HaveOccurred())
8380+
8381+
// get latency after create a new latency
8382+
result, err = client.Latency(ctx).Result()
8383+
Expect(err).NotTo(HaveOccurred())
8384+
Expect(len(result)).Should(Equal(1))
8385+
8386+
// reset all latencies again
8387+
err = client.LatencyReset(ctx).Err()
8388+
Expect(err).NotTo(HaveOccurred())
8389+
8390+
// get latency after reset again
8391+
result, err = client.Latency(ctx).Result()
8392+
Expect(err).NotTo(HaveOccurred())
8393+
Expect(len(result)).Should(Equal(0))
8394+
})
8395+
8396+
It("reset latencies by add event name args", func() {
8397+
const key = "latency-monitor-threshold"
8398+
8399+
old := client.ConfigGet(ctx, key).Val()
8400+
client.ConfigSet(ctx, key, "1")
8401+
defer client.ConfigSet(ctx, key, old[key])
8402+
8403+
result, err := client.Latency(ctx).Result()
8404+
Expect(err).NotTo(HaveOccurred())
8405+
Expect(len(result)).Should(Equal(0))
8406+
8407+
err = client.Do(ctx, "DEBUG", "SLEEP", 0.01).Err()
8408+
Expect(err).NotTo(HaveOccurred())
8409+
8410+
result, err = client.Latency(ctx).Result()
8411+
Expect(err).NotTo(HaveOccurred())
8412+
Expect(len(result)).Should(Equal(1))
8413+
8414+
// reset latency by event name
8415+
err = client.LatencyReset(ctx, result[0].Name).Err()
8416+
Expect(err).NotTo(HaveOccurred())
8417+
8418+
result, err = client.Latency(ctx).Result()
8419+
Expect(err).NotTo(HaveOccurred())
8420+
Expect(len(result)).Should(Equal(0))
8421+
})
83148422
})
83158423
})
83168424

0 commit comments

Comments
 (0)