forked from brahma-adshonor/gorr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_pipeline_hook.go
152 lines (123 loc) · 3.84 KB
/
redis_pipeline_hook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package gorr
import (
"context"
"fmt"
"reflect"
"github.com/go-redis/redis"
)
// handle redis pipeline
type redisHook struct {
id string
}
func (rh *redisHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
return ctx, nil
}
func (rh *redisHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
return nil
}
func (rh *redisHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
if !GlobalMgr.ShouldRecord() {
GlobalMgr.notifier("calling redisHook.BeforeProcessPipeline for replaying\n", rh.id, []byte(""))
for _, cc := range cmds {
key := buildRedisCmdKey(rh.id, cc)
addKeyToRedisCmd(cc, key)
}
return ctx, errRedisPipeNorm
}
return ctx, nil
}
func (rh *redisHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error {
if GlobalMgr.ShouldRecord() {
GlobalMgr.notifier("calling redisHook.AfterProcessPipeline for recording\n", rh.id, []byte(""))
for _, cc := range cmds {
key := buildRedisCmdKey(rh.id, cc)
saveRedisCmdValue(key, cc)
}
return errRedisPipeNorm
}
return nil
}
func redisPipelineExec(p *redis.Pipeline) ([]redis.Cmder, error) {
r, err := redisPipelineExecTramp(p)
if err == errRedisPipeNorm {
return r, nil
}
return r, err
}
func redisPipelineExecTramp(p *redis.Pipeline) ([]redis.Cmder, error) {
fmt.Printf("dummy function for regrestion testing")
fmt.Printf("dummy function for regrestion testing:%v", p)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if p != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return nil, nil
}
func redisPipelineExecContext(p *redis.Pipeline, ctx context.Context) ([]redis.Cmder, error) {
r, err := redisPipelineExecContextTramp(p, ctx)
if err == errRedisPipeNorm {
return r, nil
}
return r, err
}
func redisPipelineExecContextTramp(p *redis.Pipeline, ctx context.Context) ([]redis.Cmder, error) {
fmt.Printf("dummy function for regrestion testing:%v", p)
fmt.Printf("dummy function for regrestion testing:%v", ctx)
for i := 0; i < 100000; i++ {
fmt.Printf("id:%d\n", i)
go func() { fmt.Printf("hello world\n") }()
}
if p != nil {
panic("trampoline function is not allowed to be called directlyis not allowed to be called")
}
return nil, nil
}
func redisPipelineProcessor(id string, cmd []redis.Cmder, old func(cmd []redis.Cmder) error) error {
cs := ""
var err error
for _, cc := range cmd {
cs = buildRedisCmdKey(cs, cc)
}
GlobalMgr.notifier("calling client.Pipeline.ProcessWrapper", cs, []byte(""))
if GlobalMgr.ShouldRecord() {
err = old(cmd)
if err != nil && err != redis.Nil {
GlobalMgr.notifier("redis Client.Pipeline.ProcessWrapper() recording failed", cs, []byte(err.Error()))
return err
}
for _, cc := range cmd {
key := buildRedisCmdKey(id, cc)
saveRedisCmdValue(key, cc)
}
} else {
for _, cc := range cmd {
key := buildRedisCmdKey(id, cc)
addKeyToRedisCmd(cc, key)
}
}
return err
}
// client.Pipeline.Process() wrapper
func clientPipelineProcessWrapper(c *redis.Client, oldProcess func(cmd []redis.Cmder) error) func([]redis.Cmder) error {
return func(cmd []redis.Cmder) error {
id := buildRedisClientId(c)
return redisPipelineProcessor(id, cmd, oldProcess)
}
}
func clusterClientPipelineProcessWrapper(c *redis.ClusterClient, oldProcess func(cmd []redis.Cmder) error) func([]redis.Cmder) error {
return func(cmd []redis.Cmder) error {
id := buildRedisClusterClientId(c)
return redisPipelineProcessor(id, cmd, oldProcess)
}
}
func wrapRedisPipelineProcessor(c interface{}, fn func(func([]redis.Cmder) error) func([]redis.Cmder) error) bool {
m := reflect.ValueOf(c).MethodByName("WrapProcessPipeline")
if !m.IsNil() {
m.Call([]reflect.Value{reflect.ValueOf(fn)})
return true
}
return false
}