0
点赞
收藏
分享

微信扫一扫

混沌工程工具-阿里ChaosBlade的实现说明之三

前面的文章请参考:

《混沌工程工具-阿里ChaoBlade的实现说明》

《混沌工程工具-阿里ChaosBlade的实现说明之二》

前面提到了ChaosBlade的几个功能,今天描述下它是怎么实现的网络丢包和延时的。

丢包模拟

模拟丢包

[root@7dgroup2 chaosblade-0.2.0]# ./blade create network loss --interface eth0 --percent 50
{"code":200,"success":true,"result":"c29053229c16c839"}
[root@7dgroup2 chaosblade-0.2.0]#

丢包效果

(base) GaoLouMac:~ Zee$ ping 101.201.210.163
PING 101.201.210.163 (101.201.210.163): 56 data bytes
64 bytes from 101.201.210.163: icmp_seq=0 ttl=50 time=95.615 ms
64 bytes from 101.201.210.163: icmp_seq=1 ttl=50 time=78.823 ms
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3
64 bytes from 101.201.210.163: icmp_seq=4 ttl=50 time=127.879 ms
64 bytes from 101.201.210.163: icmp_seq=5 ttl=50 time=123.282 ms
64 bytes from 101.201.210.163: icmp_seq=6 ttl=50 time=129.193 ms
Request timeout for icmp_seq 7
Request timeout for icmp_seq 8
64 bytes from 101.201.210.163: icmp_seq=9 ttl=50 time=123.712 ms
Request timeout for icmp_seq 10
64 bytes from 101.201.210.163: icmp_seq=11 ttl=50 time=36.746 ms
64 bytes from 101.201.210.163: icmp_seq=12 ttl=50 time=114.155 ms
Request timeout for icmp_seq 13
Request timeout for icmp_seq 14
64 bytes from 101.201.210.163: icmp_seq=15 ttl=50 time=91.469 ms
Request timeout for icmp_seq 16
64 bytes from 101.201.210.163: icmp_seq=17 ttl=50 time=56.911 ms
64 bytes from 101.201.210.163: icmp_seq=18 ttl=50 time=113.380 ms
Request timeout for icmp_seq 19

模拟丢包的代码实现

// addQdiscForLoss
func addQdiscForLoss(channel exec.Channel, ctx context.Context, netInterface string, percent string) *transport.Response {
  // invoke tc qdisc add dev ${networkPort} root handle 1: prio bands 4
  response := channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s root handle 1: prio bands 4`, netInterface))
  if !response.Success {
    // invoke stop
    stopLossNetFunc(netInterface)
    bin.PrintErrAndExit(response.Err)
    return response
  }
  response = channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s parent 1:4 handle 40: netem loss %s%%`, netInterface, percent))
  if !response.Success {
    // invoke stop
    stopLossNetFunc(netInterface)
    bin.PrintErrAndExit(response.Err)
    return response
  }
  return response
}

通过以上代码,可以看到ChaosBlade是通过traffic control添加过滤器队列、分类、过滤器来实现的。也就是tc的netem loss。

关于traffic control的原理和操作方法,可以参考《性能场景之网络模拟》。

模拟网络延时

模拟网络延时

[root@7dgroup2 chaosblade-0.2.0]# ./blade create network delay --interface eth0 --time 3000
{"code":200,"success":true,"result":"b9e568d93dcbb5cb"}
[root@7dgroup2 chaosblade-0.2.0]#

模拟效果

(base) GaoLouMac:~ Zee$ telnet 101.201.210.163 9100
Trying 101.201.210.163...


// 这里有三秒的延时


Connected to 101.201.210.163.
Escape character is '^]'.

模拟延时的代码实现

func startDelayNet(netInterface, time, offset, localPort, remotePort, excludePort string) {
  ctx := context.Background()
  // assert localPort and remotePort
  if localPort == "" && remotePort == "" && excludePort == "" {
    response := channel.Run(ctx, "tc", fmt.Sprintf(`qdisc add dev %s root netem delay %sms %sms`, netInterface, time, offset))
    if !response.Success {
      bin.PrintErrAndExit(response.Err)
    }
    bin.PrintOutputAndExit(response.Result.(string))
    return
  }
  response := addQdiscForDelay(channel, ctx, netInterface, time, offset)
  if localPort == "" && remotePort == "" && excludePort != "" {
    response = addExcludePortFilterForDelay(excludePort, netInterface, response, channel, ctx)
    bin.PrintOutputAndExit(response.Result.(string))
    return
  }
  response = addLocalOrRemotePortForDelay(localPort, response, channel, ctx, netInterface, remotePort)
  bin.PrintOutputAndExit(response.Result.(string))
}

通过以上代码,可以看到ChaosBlade是也是通过traffic control添加过滤器队列、分类、过滤器来实现的网络延时。也就是tc的netem delay。



也即是ChaosBalde是通过将tc来实现的模拟丢包和延时。

举报

相关推荐

0 条评论