-
Notifications
You must be signed in to change notification settings - Fork 1
/
govw.go
273 lines (218 loc) · 5.99 KB
/
govw.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
package govw
import (
"bufio"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"time"
)
// endOfLine is represent byte code for symbol of end of line: `\n`
const endOfLine = 10
// VWModel contain information about VW model file
// If `Updatable` field is `true`, the system will be track of the
// changes model file and restart the daemon if necessary
type VWModel struct {
Path string
ModTime time.Time
Updatable bool
}
// VWDaemon contain information about VW daemon
type VWDaemon struct {
BinPath string
Port [2]int
Children int
Model VWModel
Test bool
TCPQueue chan *net.TCPConn
}
// Predict contain result of prediction
type Prediction struct {
Value float64
Tag string
}
// NewDaemon method return instanse of new Vowpal Wabbit daemon
func NewDaemon(binPath string, ports [2]int, children int, modelPath string, test bool, updatable bool) VWDaemon {
info, err := os.Stat(modelPath)
if err != nil {
log.Fatal(err)
}
return VWDaemon{
BinPath: binPath,
Port: ports,
Children: children,
Model: VWModel{modelPath, info.ModTime(), updatable},
Test: test,
}
}
func (vw *VWDaemon) getTCPConn() *net.TCPConn {
tcpAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", vw.Port[0]))
if err != nil {
log.Fatal("Error while resolving IP addr: ", err)
}
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
log.Fatal("Error while dialing TCP", err)
}
return conn
}
func (vw *VWDaemon) makeTCPConnQueue() {
log.Println("Start creating TCP connections queue:", len(vw.TCPQueue))
size := vw.Children / 2
for i := 0; i < size; i++ {
vw.TCPQueue <- vw.getTCPConn()
}
log.Println("Queue of TCP connections for VW is created:", len(vw.TCPQueue))
}
// Run method send command for starting new VW daemon.
func (vw *VWDaemon) Run() error {
if vw.IsNotDead(3, 200) {
vw.Stop()
}
cmd := fmt.Sprintf("vw --daemon --threads --quiet --port %d --num_children %d", vw.Port[0], vw.Children)
if vw.Model.Path != "" {
cmd += fmt.Sprintf(" -i %s", vw.Model.Path)
}
if vw.Test {
cmd += " -t"
}
if _, err := runCommand(cmd, true); err != nil {
panic(err)
}
if !vw.IsExist(5, 500) {
log.Fatal("Failed to start daemon!")
}
log.Printf("Vowpal wabbit daemon is running on port: %d", vw.Port[0])
vw.TCPQueue = make(chan *net.TCPConn, vw.Children/2)
vw.makeTCPConnQueue()
return nil
}
// Stop current daemon
func (vw *VWDaemon) Stop() error {
log.Println("Try stop daemon on port:", vw.Port[0])
cmd := fmt.Sprintf("pkill -9 -f \"vw.*--port %d\"", vw.Port[0])
if _, err := runCommand(cmd, true); err != nil {
panic(err)
}
for i := 0; i < 5; i++ {
if vw.IsNotDead(10, 500) {
log.Println("Failed to stop daemon! Try №", i+1)
cmd := fmt.Sprintf("pkill -9 -f \"vw.*--port %d\"", vw.Port[0])
if _, err := runCommand(cmd, true); err != nil {
panic(err)
}
} else {
break
}
}
log.Println("Stoped VW daemon on port:", vw.Port[0])
log.Printf("Start closing TCP connections to: %d (%d)\n", vw.Port[0], len(vw.TCPQueue))
for i := 0; i < len(vw.TCPQueue); i++ {
conn := <-vw.TCPQueue
conn.Close()
}
log.Println("End closing TCP connections to:", vw.Port[0])
return nil
}
// Predict method get predictions strings then send data to VW daemon
// for getting prediction result and return list of predictions result.
func (vw *VWDaemon) Predict(pData ...string) ([]*Prediction, error) {
size := len(pData)
result := make([]*Prediction, size)
data := []byte(strings.Join(pData, "\n"))
data = append(data, endOfLine)
failed := false
for {
conn := <-vw.TCPQueue
if _, err := conn.Write(data); err != nil {
log.Println("Error while writing to VW TCP connections: ", err, vw.Port[0])
continue
}
reader := bufio.NewReader(conn)
for i := 0; i < size; i++ {
res, err := reader.ReadString('\n')
if err != nil {
if err.Error() != "EOF" {
log.Println("Error while reading VW response: ", err, vw.Port[0], conn.RemoteAddr())
log.Println("Retry predict value!")
}
failed = true
break
}
result[i] = ParsePredictResult(&res)
}
if failed {
failed = false
continue
}
if strings.HasSuffix(conn.RemoteAddr().String(), strconv.Itoa(vw.Port[0])) {
vw.TCPQueue <- conn
} else {
conn.Close()
}
return result, nil
}
}
func (vw *VWDaemon) WorkersCount() (int, error) {
cmd := fmt.Sprintf("pgrep -f 'vw.*--port %d' | wc -l", vw.Port[0])
res, err := runCommand(cmd, false)
if err != nil {
return 0, err
}
count, err := strconv.Atoi(strings.Trim(string(res), "\n"))
if err != nil {
return 0, err
}
// We should substract 1 from count, to get clear result without
// side effect of using `sh -c` command in `exec.Command`.
return count - 1, nil
}
// IsNotDead method checks if VW daemon and all of his childrens is running.
// You shoud defain count of tries and delay in milliseconds between each try.
func (vw *VWDaemon) IsNotDead(tries int, delay int) bool {
var count int
var err error
for i := 0; i < tries; i++ {
count, err = vw.WorkersCount()
if count > 0 {
return true
}
time.Sleep(time.Millisecond * time.Duration(delay))
}
if err != nil {
log.Fatal("Can't getting VW workers count.", err)
}
return false
}
// IsExist method checks if VW daemon and all of his childrens is running.
// You shoud defain count of tries and delay in milliseconds between each try.
func (vw *VWDaemon) IsExist(tries int, delay int) bool {
var count int
var err error
for i := 0; i < tries; i++ {
count, err = vw.WorkersCount()
// We add 1 to `vw.children`, because we still have the parent process.
if count == vw.Children+1 {
return true
}
time.Sleep(time.Millisecond * time.Duration(delay))
}
if err != nil {
log.Fatal("Can't getting VW workers count.", err)
}
return false
}
// IsChanged method checks whether the model file has been modified.
func (model *VWModel) IsChanged() (bool, error) {
info, err := os.Stat(model.Path)
if err != nil {
log.Println(err)
return false, err
}
if model.ModTime != info.ModTime() {
return true, nil
}
return false, nil
}