-
Notifications
You must be signed in to change notification settings - Fork 0
/
workers.go
296 lines (199 loc) · 4.5 KB
/
workers.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package main
import (
"errors"
"fmt"
"log"
"math/rand"
"os"
"path"
"strings"
"sync"
"time"
)
const vwExtension = ".vw"
type workerReq struct {
modelName string
exs []string
}
type workerResp struct {
err error
preds []float64
}
type vwModel struct {
Name string `json:"name"`
LastModified time.Time `json:"last_modified"`
}
type vwWorker struct {
dir string
models map[string]*VW
mmtimes map[string]time.Time
mlock sync.Mutex
in chan *workerReq
out chan *workerResp
}
type vwWorkers struct {
workers []*vwWorker
}
func newWorker(modelsDir string) *vwWorker {
w := &vwWorker{
dir: modelsDir,
models: make(map[string]*VW),
mmtimes: make(map[string]time.Time),
in: make(chan *workerReq),
out: make(chan *workerResp),
}
w.loadAll()
// reload
go func() {
for {
time.Sleep(30 * time.Second)
log.Println("vw worker: reloading")
w.loadAll()
}
}()
// message loop
go w.run()
return w
}
func getLastModifiedTime(fullPath string) (time.Time, error) {
fi, err := os.Stat(fullPath)
if err != nil {
return time.Time{}, err
}
return fi.ModTime(), nil
}
func (w *vwWorker) getActiveModels() (active []*vwModel) {
w.mlock.Lock()
defer w.mlock.Unlock()
for name, _ := range w.models {
active = append(active, &vwModel{Name: name, LastModified: w.mmtimes[name]})
}
return
}
func (w *vwWorker) shouldLoad(key, fullPath string) bool {
lastm, err := getLastModifiedTime(fullPath)
if err != nil {
log.Fatalln("shouldLoad: getLastModifiedTime:", err)
}
lastmStored := w.mmtimes[key]
return lastm.After(lastmStored)
}
func (w *vwWorker) load(key, fullPath string) {
vw := NewVW(fullPath)
w.mlock.Lock()
oldVW, found := w.models[key]
// free old
if found {
oldVW.Finish()
}
w.models[key] = vw
w.mlock.Unlock()
//
// store last modified time
lastm, err := getLastModifiedTime(fullPath)
if err != nil {
log.Fatalln("load: getLastModifiedTime:", err)
}
w.mmtimes[key] = lastm
}
func (w *vwWorker) loadAll() {
//
// load
d, err := os.Open(w.dir)
defer d.Close()
if err != nil {
log.Fatalln("loadAll: os.Open:", err)
}
fis, err := d.Readdir(-1)
if err != nil {
log.Fatalln("loadAll: Readdir:", err)
}
//
// load any new or changed models
onDisk := make(map[string]struct{})
for _, fi := range fis {
if !strings.HasSuffix(fi.Name(), vwExtension) {
continue
}
// the file name, minus the .vw extension, is the key
key := strings.TrimSuffix(fi.Name(), vwExtension)
fullPath := path.Join(w.dir, fi.Name())
if w.shouldLoad(key, fullPath) {
w.load(key, fullPath)
log.Println("load: loaded", fi.Name(), "to", key)
}
onDisk[key] = struct{}{}
}
//
// delete models that are no longer on disk
w.mlock.Lock()
toDelete := []string{}
for key, _ := range w.models {
_, found := onDisk[key]
if !found {
toDelete = append(toDelete, key)
}
}
for _, key := range toDelete {
vw := w.models[key]
vw.Finish()
delete(w.models, key)
log.Println("loadAll: deleted", key)
}
w.mlock.Unlock()
log.Println("loadAll: have", len(w.models), "models")
}
func (w *vwWorker) predict(modelName string, exs []string) ([]float64, error) {
w.in <- &workerReq{modelName: modelName, exs: exs}
resp := <-w.out
if resp.err != nil {
return nil, resp.err
}
if len(resp.preds) != len(exs) {
return nil, errors.New("len(preds) != len(exs)")
}
return resp.preds, nil
}
func (w *vwWorker) predictSingle(modelName, ex string) (float64, error) {
w.in <- &workerReq{modelName: modelName, exs: []string{ex}}
resp := <-w.out
if resp.err != nil {
return 0.0, resp.err
}
if len(resp.preds) != 1 {
return 0.0, errors.New("no prediction")
}
return resp.preds[0], nil
}
func (w *vwWorker) run() {
fmt.Println("running")
for {
req := <-w.in
var resp workerResp
w.mlock.Lock()
vw, found := w.models[req.modelName]
if found {
for _, ex := range req.exs {
resp.preds = append(resp.preds, vw.Predict(ex))
}
} else {
resp.err = fmt.Errorf("model %q not found", req.modelName)
}
w.mlock.Unlock()
w.out <- &resp
}
}
func newWorkers(modelsDir string) *vwWorkers {
ws := &vwWorkers{}
for i := 0; i < nWorkers; i++ {
ws.workers = append(ws.workers, newWorker(modelsDir))
}
return ws
}
func (ws *vwWorkers) getActiveModels() []*vwModel {
return ws.workers[0].getActiveModels()
}
func (ws *vwWorkers) predict(modelName string, exs []string) ([]float64, error) {
w := ws.workers[rand.Int()%len(ws.workers)]
return w.predict(modelName, exs)
}