Skip to content

Commit 66785b3

Browse files
authored
Merge branch 'master' into ndyakov/CAE-1672-cas-cad
2 parents 72ec67e + c176672 commit 66785b3

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
@@ -8802,7 +8802,7 @@ var _ = Describe("Commands", func() {
88028802
})
88038803
})
88048804

8805-
Describe("SlowLogGet", func() {
8805+
Describe("SlowLog", func() {
88068806
It("returns slow query result", func() {
88078807
const key = "slowlog-log-slower-than"
88088808

@@ -8819,6 +8819,114 @@ var _ = Describe("Commands", func() {
88198819
Expect(err).NotTo(HaveOccurred())
88208820
Expect(len(result)).NotTo(BeZero())
88218821
})
8822+
8823+
It("returns the number of slow queries", Label("NonRedisEnterprise"), func() {
8824+
// Reset slowlog
8825+
err := client.SlowLogReset(ctx).Err()
8826+
Expect(err).NotTo(HaveOccurred())
8827+
8828+
const key = "slowlog-log-slower-than"
8829+
8830+
old := client.ConfigGet(ctx, key).Val()
8831+
// first slowlog entry is the config set command itself
8832+
client.ConfigSet(ctx, key, "0")
8833+
defer client.ConfigSet(ctx, key, old[key])
8834+
8835+
// Set a key to trigger a slow query, and this is the second slowlog entry
8836+
client.Set(ctx, "test", "true", 0)
8837+
result, err := client.SlowLogLen(ctx).Result()
8838+
Expect(err).NotTo(HaveOccurred())
8839+
Expect(result).Should(Equal(int64(2)))
8840+
8841+
// Reset slowlog
8842+
err = client.SlowLogReset(ctx).Err()
8843+
Expect(err).NotTo(HaveOccurred())
8844+
8845+
// Check if slowlog is empty, this is the first slowlog entry after reset
8846+
result, err = client.SlowLogLen(ctx).Result()
8847+
Expect(err).NotTo(HaveOccurred())
8848+
Expect(result).Should(Equal(int64(1)))
8849+
})
8850+
})
8851+
8852+
Describe("Latency", Label("NonRedisEnterprise"), func() {
8853+
It("returns latencies", func() {
8854+
const key = "latency-monitor-threshold"
8855+
8856+
old := client.ConfigGet(ctx, key).Val()
8857+
client.ConfigSet(ctx, key, "1")
8858+
defer client.ConfigSet(ctx, key, old[key])
8859+
8860+
err := client.Do(ctx, "DEBUG", "SLEEP", 0.01).Err()
8861+
Expect(err).NotTo(HaveOccurred())
8862+
8863+
result, err := client.Latency(ctx).Result()
8864+
Expect(err).NotTo(HaveOccurred())
8865+
Expect(len(result)).NotTo(BeZero())
8866+
})
8867+
8868+
It("reset all latencies", func() {
8869+
const key = "latency-monitor-threshold"
8870+
8871+
result, err := client.Latency(ctx).Result()
8872+
// reset all latencies
8873+
err = client.LatencyReset(ctx).Err()
8874+
Expect(err).NotTo(HaveOccurred())
8875+
8876+
old := client.ConfigGet(ctx, key).Val()
8877+
client.ConfigSet(ctx, key, "1")
8878+
defer client.ConfigSet(ctx, key, old[key])
8879+
8880+
// get latency after reset
8881+
result, err = client.Latency(ctx).Result()
8882+
Expect(err).NotTo(HaveOccurred())
8883+
Expect(len(result)).Should(Equal(0))
8884+
8885+
// create a new latency
8886+
err = client.Do(ctx, "DEBUG", "SLEEP", 0.01).Err()
8887+
Expect(err).NotTo(HaveOccurred())
8888+
8889+
// get latency after create a new latency
8890+
result, err = client.Latency(ctx).Result()
8891+
Expect(err).NotTo(HaveOccurred())
8892+
Expect(len(result)).Should(Equal(1))
8893+
8894+
// reset all latencies again
8895+
err = client.LatencyReset(ctx).Err()
8896+
Expect(err).NotTo(HaveOccurred())
8897+
8898+
// get latency after reset again
8899+
result, err = client.Latency(ctx).Result()
8900+
Expect(err).NotTo(HaveOccurred())
8901+
Expect(len(result)).Should(Equal(0))
8902+
})
8903+
8904+
It("reset latencies by add event name args", func() {
8905+
const key = "latency-monitor-threshold"
8906+
8907+
old := client.ConfigGet(ctx, key).Val()
8908+
client.ConfigSet(ctx, key, "1")
8909+
defer client.ConfigSet(ctx, key, old[key])
8910+
8911+
result, err := client.Latency(ctx).Result()
8912+
Expect(err).NotTo(HaveOccurred())
8913+
Expect(len(result)).Should(Equal(0))
8914+
8915+
err = client.Do(ctx, "DEBUG", "SLEEP", 0.01).Err()
8916+
Expect(err).NotTo(HaveOccurred())
8917+
8918+
result, err = client.Latency(ctx).Result()
8919+
Expect(err).NotTo(HaveOccurred())
8920+
Expect(len(result)).Should(Equal(1))
8921+
8922+
// reset latency by event name
8923+
err = client.LatencyReset(ctx, result[0].Name).Err()
8924+
Expect(err).NotTo(HaveOccurred())
8925+
8926+
result, err = client.Latency(ctx).Result()
8927+
Expect(err).NotTo(HaveOccurred())
8928+
Expect(len(result)).Should(Equal(0))
8929+
})
88228930
})
88238931
})
88248932

0 commit comments

Comments
 (0)