/
control.go
275 lines (227 loc) · 6.62 KB
/
control.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package gnf
/*
Copyright 2019 NFSB Research Team & Developers
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"NFSB/DataStruct"
"fmt"
"os"
"os/signal"
"syscall"
"time"
zmq "github.com/pebbe/zmq4"
)
func mockSendController() {
context, _ := zmq.NewContext()
publisher, _ := context.NewSocket(zmq.PUB)
defer publisher.Close()
_ = publisher.Bind("tcp://*:6667")
fmt.Println("sleeping....")
time.Sleep(1 * time.Second)
_, _ = publisher.SendMessage([][]byte{[]byte("all"),
DataStruct.Encode(&DataStruct.UserData{Action: "interrupt"})}, 0)
_, _ = publisher.SendMessage([][]byte{[]byte("all"),
DataStruct.Encode(&DataStruct.UserData{
Action: "load",
NewWorkLoad: false,
WorkLoadFile: "Config/workload_template"})}, 0)
time.Sleep(10 * time.Second)
_, _ = publisher.SendMessage([][]byte{[]byte("all"),
DataStruct.Encode(&DataStruct.UserData{
Action: "run",
NewWorkLoad: false,
WorkLoadFile: "Config/workload_template"})}, 0)
time.Sleep(3 * time.Second)
_, _ = publisher.SendMessage([][]byte{[]byte("all"),
DataStruct.Encode(&DataStruct.UserData{Action: "interrupt"})}, 0)
time.Sleep(3 * time.Second)
_, _ = publisher.SendMessage([][]byte{[]byte("all"),
DataStruct.Encode(&DataStruct.UserData{Action: "quit"})}, 0)
}
func mockRecvController() {
var cxt *zmq.Context
var sub *zmq.Socket
var err error
if cxt, err = zmq.NewContext(); err != nil {
fmt.Println("err1=", err)
return
}
if sub, err = cxt.NewSocket(zmq.SUB); err != nil {
fmt.Println("err2=", err)
return
}
defer sub.Close()
if err = sub.Connect("tcp://127.0.0.1:6668"); err != nil {
fmt.Println("err3=", err)
return
}
if err = sub.SetSubscribe("stat"); err != nil {
fmt.Println("err4=", err)
return
}
for i := 0; i < 1; i++ {
tuple, _ := sub.RecvMessageBytes(0)
//fmt.Println("mockRecvController got !!!")
data := DecodeBmStat(tuple[1])
fmt.Println(data.String())
}
_, _ = fmt.Fprintln(os.Stdout, "mockRecvController exit")
}
/*
exit condition: when isDone is closed or upon exception
upon exit: does not close any channel
upon exception: may send exeCmd{ErrorExit, "exeRecvThread exception"}
*/
func exeRecvThread(allToExe chan<- exeCmd, isDone <-chan bool, ip string, port string) {
var cxt *zmq.Context
var sub *zmq.Socket
var err error
if cxt, err = zmq.NewContext(); err != nil {
allToExe <- exeCmd{ErrorExit, "exeRecvThread exception"}
fmt.Println("err1=", err)
return
}
if sub, err = cxt.NewSocket(zmq.SUB); err != nil {
allToExe <- exeCmd{ErrorExit, "exeRecvThread exception"}
fmt.Println("err2=", err)
return
}
defer sub.Close()
if err = sub.Connect("tcp://" + ip + ":" + port); err != nil {
allToExe <- exeCmd{ErrorExit, "exeRecvThread exception"}
fmt.Println("err3=", err)
return
}
if err = sub.SetSubscribe("all"); err != nil {
allToExe <- exeCmd{ErrorExit, "exeRecvThread exception"}
fmt.Println("err4=", err)
return
}
myIp, _ := getIp() // If migrate to different servers, need to pass in the address
if err = sub.SetSubscribe(myIp); err != nil {
allToExe <- exeCmd{ErrorExit, "exeRecvThread exception"}
fmt.Println("err5=", err)
return
}
// a thread that listens to the controller
recvChan := make(chan []byte)
go func() {
for {
if tuple, err := sub.RecvMessageBytes(0); err == nil {
recvChan <- tuple[1]
} else {
fmt.Println("recvChan error,", err)
}
}
}()
allToExe <- exeCmd{Ready, "exeRecvThread is ready"}
for {
select {
case <-isDone:
//_, _ = fmt.Fprintln(os.Stdout, "exeRecvThread try to exit - 5")
allToExe <- exeCmd{NormalExit, "exeRecvThread"}
return
case tuple := <-recvChan:
data := DataStruct.Decode(tuple)
switch data.Action {
case "quit":
allToExe <- exeCmd{GnfStop, ""}
case "interrupt":
allToExe <- exeCmd{BmkStop, ""}
case "load", "run":
var sig exeSig
if data.Action == "load" {
sig = CtrlLoad
} else {
sig = CtrlRun
}
if data.NewWorkLoad {
fname := "workload_temp"
if ret := writeWorkloadFile(data.WorkLoadFile, fname); ret != -1 {
allToExe <- exeCmd{sig, fname}
} else {
fmt.Println("error in writing ")
}
} else {
allToExe <- exeCmd{sig, data.WorkLoadFile}
}
}
}
}
}
/*
exit condition: when isDone is closed or upon exception
upon exit: does not close any channel
upon exception: may send exeCmd{ErrorExit, "exeSendThread exception"}
*/
func exeSendThread(allToExe chan<- exeCmd, isDone <-chan bool, exeToCtl <-chan BmStats, port string) {
var cxt *zmq.Context
var pub *zmq.Socket
var err error
if cxt, err = zmq.NewContext(); err != nil {
allToExe <- exeCmd{ErrorExit, "exeSendThread exception"}
fmt.Println("err1=", err)
return
}
if pub, err = cxt.NewSocket(zmq.PUB); err != nil {
allToExe <- exeCmd{ErrorExit, "exeSendThread exception"}
fmt.Println("err2=", err)
return
}
defer pub.Close()
if err = pub.Bind("tcp://*:" + port); err != nil {
allToExe <- exeCmd{ErrorExit, "exeSendThread exception"}
fmt.Println("err2=", err)
return
}
allToExe <- exeCmd{Ready, "exeSendThread is ready"}
for {
select {
case <-isDone:
//_, _ = fmt.Fprintln(os.Stdout, "exeSendThread try to exit - 4")
allToExe <- exeCmd{NormalExit, "exeSendThread"}
return
case stat := <-exeToCtl:
fmt.Println("looping...")
bytes := EncodeBmStat(&stat)
//fmt.Println(bytes)
if _, err := pub.SendMessage([][]byte{[]byte("stat"), bytes}, 0); err != nil {
//fmt.Println("err3=", err)
} else {
//fmt.Println("send,", ret)
}
}
}
}
/*
exit condition: when isDone is closed
upon exit: does not close any channel
upon exception: no known possible exception, no exception signal
*/
func exeSignThread(allToExe chan<- exeCmd, isDone <-chan bool) {
sigToExe := make(chan os.Signal)
signal.Notify(sigToExe, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case <-isDone:
signal.Stop(sigToExe)
close(sigToExe)
//_, _ = fmt.Fprintln(os.Stdout, "exeSignThread try to exit - 3")
allToExe <- exeCmd{NormalExit, "exeSignThread"}
return
case <-sigToExe:
//fmt.Println("GnfStop sending")
allToExe <- exeCmd{GnfStop, "exeSignThread"}
//fmt.Println("GnfStop sent")
}
}
}