作者:磐基CMChaos混沌工程团队,成为CNCF项目ChaosBlade的 Maintainer
概述
本文以 2 个新增 Redis 原子事件为例,帮助刚接触 ChaosBlade 的社区同学快速入门开源贡献。
开源贡献前提:了解混沌工程和掌握 Golang 开发。
Redis 实际使用过程中会存在故障演练需求。
例如:
根据故障演练需求价值,决定是否有必要新增相关混沌工程原子事件。
middleware 项目:包含 Nginx、Redis 等中间件相关实验核心代码。
项目地址:https://github.com/chaosblade-io/chaosblade-exec-middleware
该目录放置 Redis 原子事件核心代码
mkdir chaosblade-exec-middleware/exec/redis
package redis
import (
"github.com/chaosblade-io/chaosblade-spec-go/spec"
)
type RedisCommandSpec struct {
spec.BaseExpModelCommandSpec
}
func (*RedisCommandSpec) Name() string {
return "redis"
}
func (*RedisCommandSpec) ShortDesc() string {
return "Redis experiment"
}
func (*RedisCommandSpec) LongDesc() string {
return "Redis experiment"
}
func NewRedisCommandSpec() spec.ExpModelCommandSpec {
return &RedisCommandSpec{
spec.BaseExpModelCommandSpec{
ExpActions: []spec.ExpActionCommandSpec{
NewCacheExpireActionSpec(),
NewCacheLimitActionSpec(),
},
ExpFlags: []spec.ExpFlagSpec{},
},
}
}
model 目录位置:chaosblade-exec-middleware/exec/model/目录
model 目录不同文件对应不同系统支持:
model 具体代码:
chaosblade-exec-middleware/exec/model/[1]
package model
import (
"github.com/chaosblade-io/chaosblade-exec-middleware/exec/nginx"
"github.com/chaosblade-io/chaosblade-exec-middleware/exec/redis"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
)
// GetAllExpModels returns the experiment model specs in the project.
// Support for other project about chaosblade
func GetAllExpModels() []spec.ExpModelCommandSpec {
return []spec.ExpModelCommandSpec{
nginx.NewNginxCommandSpec(),
redis.NewRedisCommandSpec(),
}
具体文件:添加 Redis 到 chaosblade-exec-middleware/build/spec.go
具体代码:
chaosblade-exec-middleware/build/spec.go[2]
...
// getModels returns experiment models in the project
func getModels() *spec.Models {
modelCommandSpecs := []spec.ExpModelCommandSpec{
nginx.NewNginxCommandSpec(),
redis.NewRedisCommandSpec(), // <== Redis相关
}
specModels := make([]*spec.Models, 0)
for _, modeSpec := range modelCommandSpecs {
flagSpecs := append(modeSpec.Flags(), model.GetSSHExpFlags()...)
modeSpec.SetFlags(flagSpecs)
specModel := util.ConvertSpecToModels(modeSpec, spec.ExpPrepareModel{}, "host")
specModels = append(specModels, specModel)
}
return util.MergeModels(specModels...)
}
...
chaosblade-exec-middleware/exec/redis/redis_cache_expire.go[3]
chaosblade-exec-middleware/exec/redis/redis_cache_limit.go[4]
参考相关文档
make build_darwin
make build_linux
make build_linux_arm
说明:其他系统编译说明参考 官方文档[5]
将测试服务器 chaosblade 目录以下文件替换为新版本
#示例1:expire a key
blade create redis cache-expire --addr 192.168.56.101:6379 --password 123456 --key test1 --expiry 1m
#示例2:expire all keys only when the new expiry is greater than current one
blade create redis cache-expire --addr 192.168.56.101:6379 --password 123456 --option GT --expiry 1m
# 实验前
192.168.56.101:6379> set test1 test2
OK
192.168.56.101:6379> get test1
"test2"
192.168.56.101:6379> expire test1 3000
(integer) 1
192.168.56.101:6379> ttl test1
(integer) 2924
# 实验后
# blade create redis cache-expire --addr 192.168.56.101:6379 --password 123456 --option GT --expiry 1m
192.168.56.101:6379> ttl test1
(integer) 58
# 示例: set maxmemory to 256M
blade create redis cache-limit --addr 192.168.56.101:6379 --password 123456 --size 256M
# 实验前
192.168.56.101:6379> config get maxmemory
1) "maxmemory"
2) "0"
# 实验后
# blade create redis cache-limit --addr 192.168.56.101:6379 --password 123456 --size 256M
192.168.56.101:6379> config get maxmemory
1) "maxmemory"
2) "256000000"
说明:测试有 bug 或待优化,重复开发、调试和编译步骤
# commit
git commit -s -m "add 2 redis experiments"
# push
git push origin dev
ChaosBlade 官方网址:https://chaosblade.io/[6] ChaosBlade Github : https://github.com/chaosblade-io/chaosblade[7] ChaosBlade 钉钉社区交流群:23177705
[1]
chaosblade-exec-middleware/exec/model/: https://github.com/chaosblade-io/chaosblade-exec-middleware/tree/main/exec/model
[2]
chaosblade-exec-middleware/build/spec.go: https://github.com/chaosblade-io/chaosblade-exec-middleware/blob/main/build/spec.go
[3]
chaosblade-exec-middleware/exec/redis/redis_cache_expire.go: https://github.com/chaosblade-io/chaosblade-exec-middleware/blob/main/exec/redis/redis_cache_expire.go
[4]
chaosblade-exec-middleware/exec/redis/redis_cache_limit.go: https://github.com/chaosblade-io/chaosblade-exec-middleware/blob/main/exec/redis/redis_cache_limit.go
[5]
官方文档: https://github.com/chaosblade-io/chaosblade/blob/master/README_CN.md
[6]
https://chaosblade.io/: https://chaosblade.io/
[7]
https://github.com/chaosblade-io/chaosblade: https://github.com/chaosblade-io/chaosblade