-
Notifications
You must be signed in to change notification settings - Fork 1
/
chaos.go
88 lines (76 loc) · 1.92 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
// Copyright 2020 Justice Nanhou. All rights reserved.
// license that can be found in the LICENSE file.
package chaos
import (
"fmt"
"log"
"strings"
"sync"
"github.com/google/uuid"
)
// Context chaos Context
// type Context context.Context
// RegisterAgent register a new Agent
func RegisterAgent(a *Agent) {
// AgentSotre()[a.Name] = a
if len(a.ID) == 0 {
a.ID = genID()
}
GetAgentStoreInstance().Add(a)
}
func genID() string {
id := uuid.New().String()
res := strings.Split(id, "-")
return res[len(res)-1]
}
// RegisterAgentAndScale register a new Agent and scale
func RegisterAgentAndScale(a *Agent, scale int) {
for i := 0; i < scale; i++ {
agt := *a
name := fmt.Sprintf("%v_%v", a.Name, i)
agt.Name = name
GetAgentStoreInstance().Add(&agt)
}
}
// StartAgent ...
func StartAgent(wg *sync.WaitGroup, agent *Agent) {
go func(wg *sync.WaitGroup, agent *Agent) {
defer wg.Done()
agent.start()
agent.doTakeDown()
}(wg, agent)
}
// Start start all agents
func Start() {
var wg sync.WaitGroup
log.Println("--------------------------------")
log.Println("--------- Start chaos ----------")
log.Println("--------------------------------")
setUpAllAgent()
// fmt.Printf("agentStore count: %v\n", GetAgentStoreInstance().Count())
wg.Add(GetAgentStoreInstance().Count())
for _, agent := range GetAgentStoreInstance().List() {
StartAgent(&wg, agent)
}
wg.Wait()
// acl.GetDispatcherInstance().Close()
log.Println("--------------------------------")
log.Println("--------- End chaos ----------")
log.Println("--------------------------------")
}
func setUpAllAgent() {
for _, agent := range GetAgentStoreInstance().List() {
agent.doSetup()
}
}
// RegisterAndRunAgent ...
func RegisterAndRunAgent(wg *sync.WaitGroup, agent *Agent) {
RegisterAgent(agent)
agent.doSetup()
StartAgent(wg, agent)
}
// StopAgent ..
func StopAgent(agentID string) {
agent := GetAgentStoreInstance().Get(agentID)
agent.stop()
}