-
Notifications
You must be signed in to change notification settings - Fork 1
/
chaos.go
189 lines (174 loc) · 5.08 KB
/
chaos.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package chaos
import (
"context"
"flag"
"math/rand"
"runtime"
"strconv"
"time"
"github.com/spf13/pflag"
"go.uber.org/zap"
"github.com/chaosblade-io/chaosblade-exec-golang/chaos/action"
"github.com/chaosblade-io/chaosblade-exec-golang/chaos/log"
"github.com/chaosblade-io/chaosblade-exec-golang/chaos/manager"
"github.com/chaosblade-io/chaosblade-exec-golang/chaos/matcher"
"github.com/chaosblade-io/chaosblade-exec-golang/chaos/model"
"github.com/chaosblade-io/chaosblade-exec-golang/chaos/model/response"
"github.com/chaosblade-io/chaosblade-exec-golang/chaos/scenario"
"github.com/chaosblade-io/chaosblade-exec-golang/chaos/transport"
)
var (
chaosFlagSet *pflag.FlagSet
ip string
port int
scenarioEnable bool
)
func init() {
chaosFlagSet = pflag.NewFlagSet("chaos", pflag.ContinueOnError)
flag.StringVar(&ip, "chaos-ip", "127.0.0.1", "chaos service host ip")
flag.IntVar(&port, "chaos-port", 9526, "chaos service port")
flag.BoolVar(&scenarioEnable, "chaos-scenario-enable", false, "chaos scenario enable or not")
}
// Must be invoked
func Init() {
transport.Run(ip, strconv.Itoa(port))
}
type Chaos struct {
name string
matchers map[string]matcher.Matcher
}
func New() *Chaos {
name := getTargetName()
log.Zap.Debug("new chaos name: " + name)
return &Chaos{
name: name,
matchers: make(map[string]matcher.Matcher, 0),
}
}
func (c *Chaos) Name() string {
return c.name
}
func (c *Chaos) Matchers() map[string]matcher.Matcher {
return c.matchers
}
func (c *Chaos) AddMatchers(matchers ...matcher.Matcher) {
for _, m := range matchers {
if _, ok := c.matchers[m.Name()]; ok {
log.Zap.Warn("the matcher exists, so skip it", zap.String("name", m.Name()))
continue
}
c.matchers[m.Name()] = m
}
}
func (c *Chaos) Set(ctx context.Context, action action.Action) response.Response {
return c.SetWithIndex(ctx, 0, action)
}
// 是否
func (c *Chaos) SetWithIndex(ctx context.Context, actionIndex int, act action.Action) response.Response {
if manager.CircuitEnabled() {
log.Zap.Debug("in circuit, skip to execute chaos")
return response.ReturnExperimentInCircuit()
}
// for experiments exporting
scenario.Register(c.Name(), c.matchers, act)
experiment := manager.GetExperiment(c.Name(), act.Name(), strconv.Itoa(actionIndex))
if experiment == nil {
return response.ReturnExperimentNotFound()
}
for key, value := range experiment.Flags {
if _, ok := action.CommonActionFlagMap[key]; ok {
continue
}
if _, ok := act.Flags()[key]; ok {
continue
}
matcher, ok := c.Matchers()[key]
if !ok {
log.Zap.Debug("can not find the experiment matcher",
zap.String("target", experiment.Target),
zap.String("action", experiment.Action),
zap.Int("index", actionIndex),
zap.String("matcher", key))
return response.ReturnExperimentMatcherNotFound(key)
}
if matched, err := matcher.Match(value); err != nil || !matched {
log.Zap.Debug("the matcher does not match",
zap.String("target", experiment.Target),
zap.String("action", experiment.Action),
zap.Int("index", actionIndex),
zap.String("matcher", key),
zap.String("expectedValue", value),
)
return response.ReturnExperimentNotMatched(key)
}
}
less, err := c.lessEffectedLimit(experiment)
if err != nil {
return response.ReturnFail(response.IllegalParameters, err.Error())
}
if !less {
return response.ReturnExperimentLimited()
}
experiment.Lock.Lock()
defer experiment.Lock.Unlock()
experiment.Inc()
log.Zap.Info("matched experiment",
zap.String("target", experiment.Target),
zap.String("action", experiment.Action),
zap.Int("index", actionIndex),
zap.Any("flags", experiment.Flags),
)
response := act.Execute(ctx, experiment)
if !response.Success {
log.Zap.Warn("execute experiment failed",
zap.String("target", experiment.Target),
zap.String("action", experiment.Action),
zap.Int("index", actionIndex),
zap.Any("flags", experiment.Flags),
zap.String("error", response.Error))
experiment.Dec()
return response
}
return response
}
// lessEffectedLimit
func (c *Chaos) lessEffectedLimit(experiment *model.Experiment) (bool, error) {
effectCountValue := experiment.Flags[action.EffectCount.Name]
effectCountPercent := experiment.Flags[action.EffectPercent.Name]
if effectCountValue == "" && effectCountPercent == "" {
return true, nil
}
if effectCountValue != "" {
effectCount, err := strconv.Atoi(effectCountValue)
if err != nil {
return false, err
}
if experiment.Count >= int64(effectCount) {
return false, nil
}
}
if effectCountPercent != "" {
effectPercent, err := strconv.Atoi(effectCountPercent)
if err != nil {
return false, err
}
if effectPercent != 100 {
rand.Seed(time.Now().UnixNano())
randValue := rand.Intn(100) + 1
if randValue > effectPercent {
return false, nil
}
}
}
return true, nil
}
// examples.(*Business).Execute
// github.com/chaosblade-io/chaosblade-exec-golang/demo.Log
// examples.init
func getTargetName() string {
pc := make([]uintptr, 15)
n := runtime.Callers(3, pc)
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
return frame.Function
}