/
server.go
439 lines (388 loc) · 11.4 KB
/
server.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// 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.
package manager
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/go-logr/logr"
zapOpt "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/util/json"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/paddlepaddle/paddlecloud/pkg/apis/sampleset/v1alpha1"
"github.com/paddlepaddle/paddlecloud/pkg/sampleset/common"
"github.com/paddlepaddle/paddlecloud/pkg/sampleset/driver"
)
type Server struct {
driver.Driver
Log logr.Logger
ctx context.Context
cancel context.CancelFunc
watcher *fsnotify.Watcher
svrOpt *common.ServerOptions
rootOpt *common.RootCmdOptions
doers map[string]func([]byte) error
optResMap map[string]string
}
func NewServer(rootOpt *common.RootCmdOptions, svrOpt *common.ServerOptions) (*Server, error) {
driverName := v1alpha1.DriverName(rootOpt.Driver)
csiDriver, err := driver.GetDriver(driverName)
if err != nil {
return nil, err
}
if !strings.HasPrefix(svrOpt.ServerDir, "/") {
return nil, fmt.Errorf("path must begin with /")
}
// configure zap log and create a logger
zapLog := zap.New(func(o *zap.Options) {
o.Development = rootOpt.Development
}, func(o *zap.Options) {
o.ZapOpts = append(o.ZapOpts, zapOpt.AddCaller())
},
func(o *zap.Options) {
if !rootOpt.Development {
encCfg := zapOpt.NewProductionEncoderConfig()
encCfg.EncodeLevel = zapcore.CapitalLevelEncoder
encCfg.EncodeTime = zapcore.ISO8601TimeEncoder
o.Encoder = zapcore.NewConsoleEncoder(encCfg)
}
})
// create file system notify watcher and add dir to watch
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
// make maps
optResMap := make(map[string]string)
doer := make(map[string]func([]byte) error)
// make Cancel Context
ctx, cancel := context.WithCancel(context.Background())
server := &Server{
ctx: ctx,
cancel: cancel,
doers: doer,
rootOpt: rootOpt,
svrOpt: svrOpt,
Log: zapLog,
watcher: watcher,
Driver: csiDriver,
optResMap: optResMap,
}
return server, nil
}
func (s *Server) Run() error {
defer s.cancel()
defer s.watcher.Close()
// add job doer for watcher's event
s.doers[common.PathSyncOptions] = s.doSync
s.doers[common.PathClearOptions] = s.doClear
s.doers[common.PathRmrOptions] = s.doRmr
s.doers[common.PathWarmupOptions] = s.doWarmup
// add options to result map key-value pair
s.optResMap[common.PathSyncOptions] = common.PathSyncResult
s.optResMap[common.PathClearOptions] = common.PathClearResult
s.optResMap[common.PathRmrOptions] = common.PathRmrResult
s.optResMap[common.PathWarmupOptions] = common.PathWarmupResult
// add static file server handlers
if err := s.addStaticHandlers(
common.PathCacheStatus,
common.PathSyncResult,
common.PathClearResult,
common.PathRmrResult,
common.PathWarmupResult,
common.PathSyncOptions,
common.PathClearOptions,
common.PathRmrOptions,
common.PathWarmupOptions,
); err != nil {
return err
}
// add upload option file handlers
s.addUploadHandlers(
common.PathSyncOptions,
common.PathClearOptions,
common.PathRmrOptions,
common.PathWarmupOptions)
if err := s.addWatchDirs(
common.PathSyncOptions,
common.PathClearOptions,
common.PathRmrOptions,
common.PathWarmupOptions,
); err != nil {
return err
}
// (TODO: xiaolao) Add one goroutine to clear options and results file
go s.watchAndDo()
go s.writeCacheStatus()
s.Log.V(1).Info("===== run server ======")
addr := fmt.Sprintf(":%d", common.RuntimeServicePort)
return http.ListenAndServe(addr, nil)
}
func (s *Server) uploadHandleFunc(pattern string) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
err := fmt.Errorf("http method %s not support", req.Method)
s.Log.Error(err, "error occur when upload sync options")
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
s.Log.Error(err, "read request body error")
w.WriteHeader(http.StatusBadRequest)
return
}
// if cancel context is closed, create a new cancel context and sign it to server
if errors.Is(s.ctx.Err(), context.Canceled) {
ctx, cancel := context.WithCancel(context.Background())
s.ctx = ctx
s.cancel = cancel
}
// if terminate signal is not none, then terminate the task in progress,
// and pop all tasks waiting in event queue,
jobName := req.URL.Query().Get(common.TerminateSignal)
if jobName != "" {
s.cancel()
}
fileName := req.Header.Get("filename")
if fileName == "" {
e := fmt.Errorf("handle upload %s fail", pattern)
s.Log.Error(e, "can not get filename from request header")
w.WriteHeader(http.StatusBadRequest)
return
} else if fileName == common.TerminateSignal {
s.Log.V(1).Info("upload terminate signal successfully")
w.WriteHeader(http.StatusOK)
return
}
dirPath := s.svrOpt.ServerDir + pattern
filePath := dirPath + "/" + fileName
err = ioutil.WriteFile(filePath, body, os.ModePerm)
if err != nil {
s.Log.Error(err, "write file error", "file", filePath)
w.WriteHeader(http.StatusInternalServerError)
return
}
s.Log.V(1).Info("upload options success", "file", filePath, "route", pattern)
w.WriteHeader(http.StatusOK)
return
}
}
func (s *Server) watchAndDo() {
for {
select {
case event, ok := <-s.watcher.Events:
if !ok {
return
}
s.handleEvent(event)
case err, ok := <-s.watcher.Errors:
if !ok {
return
}
s.Log.Error(err, "watcher get error")
}
}
}
func (s *Server) handleEvent(event fsnotify.Event) {
s.Log.V(1).Info("get event", "event", event.String())
if event.Op != fsnotify.Create {
s.Log.V(1).Info("event operation is create")
return
}
switch extractPattern(event.Name) {
case common.PathSyncOptions:
s.do(common.PathSyncOptions, event.Name)
case common.PathClearOptions:
s.do(common.PathClearOptions, event.Name)
case common.PathRmrOptions:
s.do(common.PathRmrOptions, event.Name)
case common.PathWarmupOptions:
s.do(common.PathWarmupOptions, event.Name)
default:
err := fmt.Errorf("extract pattern error")
s.Log.Error(err, "can not deal with none")
}
}
func (s *Server) do(pattern string, optionFile string) {
body, err := ioutil.ReadFile(optionFile)
if err != nil {
msg := fmt.Sprintf("read file %s error: %s", optionFile, err.Error())
s.writeResultFile(common.JobStatusFail, msg, pattern, optionFile)
return
}
doer, exist := s.doers[pattern]
if !exist {
msg := fmt.Sprintf("can not find pattern's doer")
s.writeResultFile(common.JobStatusFail, msg, pattern, optionFile)
return
}
s.writeResultFile(common.JobStatusRunning, "", pattern, optionFile)
if err := doer(body); err != nil {
s.writeResultFile(common.JobStatusFail, err.Error(), pattern, optionFile)
return
}
s.writeResultFile(common.JobStatusSuccess, "", pattern, optionFile)
}
func (s *Server) writeResultFile(status common.JobStatus, message string, pattern string, optionFile string) {
if message != "" {
err := errors.New(message)
s.Log.Error(err, "")
}
result := common.JobResult{
Status: status,
Message: message,
}
body, err := json.Marshal(result)
if err != nil {
s.Log.Error(err, "marshal result error", "result", result)
return
}
resultPath, exist := s.optResMap[pattern]
if !exist {
s.Log.Error(errors.New("result pattern not find"), "")
return
}
optionPaths := strings.Split(optionFile, "/")
filename := optionPaths[len(optionPaths)-1]
filePath := s.svrOpt.ServerDir + resultPath + "/" + filename
if err := ioutil.WriteFile(filePath, body, os.ModePerm); err != nil {
s.Log.Error(err, "write file error", "file", filePath)
}
s.Log.V(1).Info("write result success",
"file", filePath, "result", result, "pattern", pattern)
}
func (s *Server) doSync(body []byte) error {
s.Log.V(1).Info("begin do sync")
opt := &v1alpha1.SyncJobOptions{}
err := json.Unmarshal(body, opt)
if err != nil {
return err
}
return s.DoSyncJob(s.ctx, opt, s.Log)
}
func (s *Server) doClear(body []byte) error {
s.Log.V(1).Info("begin do clear")
opt := &v1alpha1.ClearJobOptions{}
err := json.Unmarshal(body, opt)
if err != nil {
return err
}
return s.DoClearJob(s.ctx, opt, s.Log)
}
func (s *Server) doWarmup(body []byte) error {
s.Log.V(1).Info("begin do warmup")
opt := &v1alpha1.WarmupJobOptions{}
err := json.Unmarshal(body, opt)
if err != nil {
return err
}
return s.DoWarmupJob(s.ctx, opt, s.Log)
}
func (s *Server) doRmr(body []byte) error {
s.Log.V(1).Info("begin do rmr")
opt := &v1alpha1.RmrJobOptions{}
err := json.Unmarshal(body, opt)
if err != nil {
return err
}
return s.DoRmrJob(s.ctx, opt, s.Log)
}
func (s *Server) addWatchDirs(patterns ...string) error {
for _, pattern := range patterns {
err := s.watcher.Add(s.svrOpt.ServerDir + pattern)
if err != nil {
return fmt.Errorf("add watcher dir %s error", pattern)
}
}
return nil
}
func (s *Server) addStaticHandlers(patterns ...string) error {
// Add static file server
http.Handle("/", http.FileServer(http.Dir(s.svrOpt.ServerDir)))
for _, pattern := range patterns {
path := s.svrOpt.ServerDir + pattern
if _, err := os.Stat(path); err != nil {
if !os.IsNotExist(err) {
return err
}
e := os.MkdirAll(path, os.ModePerm)
if e != nil {
return e
}
}
handler := http.FileServer(http.Dir(path))
http.Handle(pattern, http.StripPrefix(pattern, handler))
}
return nil
}
func (s *Server) addUploadHandlers(patterns ...string) {
for _, pattern := range patterns {
uploadUrl := common.PathUploadPrefix + pattern
http.HandleFunc(uploadUrl, s.uploadHandleFunc(pattern))
}
}
func (s *Server) writeCacheStatus() {
filePath := s.svrOpt.ServerDir +
common.PathCacheStatus + "/" +
common.FilePathCacheInfo
var interval int64 = 5
for {
if interval >= s.svrOpt.Interval {
time.Sleep(time.Duration(s.svrOpt.Interval) * time.Second)
} else {
time.Sleep(time.Duration(interval) * time.Second)
interval += 2
}
status := &v1alpha1.CacheStatus{}
err := s.CreateCacheStatus(s.svrOpt, status)
if err != nil {
status.ErrorMassage = err.Error()
s.Log.Error(err, "")
}
body, err := json.Marshal(status)
if err != nil {
s.Log.Error(err, "marshal cache status error")
continue
}
err = ioutil.WriteFile(filePath, body, os.ModePerm)
if err != nil {
s.Log.Error(err, "write cache status error")
continue
}
s.Log.V(1).Info("write cache status success")
}
}
func extractPattern(path string) string {
if strings.Contains(path, common.PathSyncOptions) {
return common.PathSyncOptions
}
if strings.Contains(path, common.PathClearOptions) {
return common.PathClearOptions
}
if strings.Contains(path, common.PathRmrOptions) {
return common.PathRmrOptions
}
if strings.Contains(path, common.PathWarmupOptions) {
return common.PathWarmupOptions
}
return ""
}