This repository has been archived by the owner on Jun 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
615 lines (538 loc) · 17 KB
/
pool.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"gopkg.in/yaml.v2"
"github.com/fsouza/go-dockerclient"
"github.com/go-redis/redis"
)
var (
ErrDockerAlreadyInitialized = fmt.Errorf("docker client already initialized")
ErrRedisAlreadyInitialized = fmt.Errorf("redis client already initialized")
ErrDatalabInstanceNotAvailable = fmt.Errorf("instance not available")
ErrDatalabScriptInvalid = fmt.Errorf("datalab script error")
ErrDatalabInstanceNotRunning = fmt.Errorf("instance not running")
)
// store
// nonce:${nonce} -> ${cid} ${ttl}
// container:${cid} -> { ${event_name} -> ${timestamp} }
type DatalabPool struct {
mutex *sync.RWMutex
config *DatalabConfig
dockerEvents chan *docker.APIEvents
dockerClient *docker.Client
redisClient *redis.Client
store DataStore
}
func NewDatalabPool(config *DatalabConfig) *DatalabPool {
return &DatalabPool{
config: config,
mutex: &sync.RWMutex{},
dockerEvents: make(chan *docker.APIEvents, 1),
dockerClient: nil,
redisClient: nil,
}
}
func (pool *DatalabPool) Initialize(ctx context.Context) (err error) {
pool.mutex.Lock()
defer pool.mutex.Unlock()
if err = pool.initDocker(ctx, pool.config.DockerEndpoint); err != nil {
return
}
if err = pool.initRedis(ctx, pool.config.RedisCacheURL); err != nil {
return
}
pool.store = NewRedisDataStore(pool.redisClient, "datalab")
log.Printf("datalab pool initialized")
return
}
func (pool *DatalabPool) initDocker(ctx context.Context, endpoint string) (err error) {
if pool.dockerClient != nil {
return ErrDockerAlreadyInitialized
}
pool.dockerClient, err = docker.NewClient(endpoint)
if err != nil {
pool.dockerClient = nil
return
}
go pool.processContainerCleanup(ctx)
go pool.processDockerEvents(ctx)
pool.dockerClient.AddEventListener(pool.dockerEvents)
log.Printf("docker client initialized: %s", endpoint)
return
}
func (pool *DatalabPool) initRedis(ctx context.Context, endpoint string) (err error) {
var options *redis.Options
if pool.redisClient != nil {
return ErrRedisAlreadyInitialized
}
if options, err = redis.ParseURL(endpoint); err != nil {
return
}
pool.redisClient = redis.NewClient(options).WithContext(ctx)
log.Printf("redis client initialized: %s", endpoint)
return
}
func (pool *DatalabPool) processContainerCleanup(ctx context.Context) {
var ticker = time.NewTicker(10 * time.Second)
for {
select {
case <-ctx.Done():
log.Printf("DONE: container cleanup goroutine")
ticker.Stop()
return
case <-ticker.C:
// check idle containers
pool.checkIdleContainers()
}
}
}
func (pool *DatalabPool) checkIdleContainers() {
var prefix = "container"
var pattern = fmt.Sprintf("%s:*", prefix)
var cidList []string
var err error
cidList, err = pool.store.Keys(pattern)
if err != nil {
log.Printf("fetch keys failure, pattern = %s, error = %v", pattern, err)
return
}
for _, containerKey := range cidList {
var containerID = containerKey[len(prefix)+1:]
var data map[string]string
data, err = pool.store.HGetAll(pool.makeContainerKey(containerID))
if err != nil || len(data) == 0 {
log.Printf("get container data failure: %s, err = %v, data = %v", containerID, err, data)
continue
}
log.Printf("checking idle containers: %s, data = %v", containerID, data)
// check existance
var container *docker.Container
container, err = pool.getInstanceByID(containerID)
if container == nil {
log.Printf("container already disapeared, remove from store: %s", containerID)
pool.store.Delete(pool.makeContainerKey(containerID))
continue
}
// timeout
var lastIdleResetTimestamp, lastKeepAliveTimestamp int
if lastKeepAliveTimestamp, err = strconv.Atoi(data[UserEventKeepAlive]); err != nil {
log.Printf("get container keepalive time: %s, err = %v", containerID, err)
// continue
lastKeepAliveTimestamp = 0
}
if lastIdleResetTimestamp, err = strconv.Atoi(data[UserEventIdleReset]); err != nil {
log.Printf("get container idle reset time: %s, err = %v", containerID, err)
// continue
lastIdleResetTimestamp = 0
}
var now = int(time.Now().Unix())
var idleTimeout = now - lastIdleResetTimestamp
if idleTimeout > pool.config.InstanceIdleTimeout {
log.Printf("checking idle containers: %s, idle timeout: %d, kill container", containerID, idleTimeout)
pool.KillInstance(containerID)
continue
}
var keepAliveTimeout = now - lastKeepAliveTimestamp
if keepAliveTimeout > pool.config.InstanceKeepAliveTimeout {
log.Printf("checking idle containers: %s, keepalive timeout: %d, kill container", containerID, keepAliveTimeout)
pool.KillInstance(containerID)
continue
}
}
}
func (pool *DatalabPool) processDockerEvents(ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Printf("DONE: container docker events goroutine")
return
case event, ok := <-pool.dockerEvents:
if !ok {
log.Printf("ERROR: docker events closed, quit goroutine")
return
}
if event == nil {
break
}
log.Printf("docker event: %#v", event)
switch event.Type {
case "container":
pool.onEventContainer(event)
}
}
}
}
func (pool *DatalabPool) onEventContainer(event *docker.APIEvents) {
switch event.Action {
case "destroy":
// check and cleanup user folder
// Actor:docker.APIActor{
// ID:"050e5823627a2bec06a43ee5b483d150945e84f8d42500ce188f82dadc208e88",
// Attributes:map[string]string{
// "datalab_id":"1",
// "image":"gcr.io/cloud-datalab/datalab:latest",
// "name":"datalab_01"
// }
// }
var actor = event.Actor
var containerID = actor.ID
var err error
var datalabID int
if datalabID, err = strconv.Atoi(actor.Attributes["datalab_id"]); err != nil {
log.Printf("attributes datalab_id == null, ignore container: %#v", actor.Attributes)
break
}
pool.cleanupInstanceDirectory(containerID, datalabID)
}
}
func (pool *DatalabPool) cleanupInstanceDirectory(containerID string, datalabID int) {
os.MkdirAll(pool.config.DataBackupHome, 0755)
var variables = pool.makeContainerVairables(datalabID)
var datalabName = variables["datalab_name"]
var datalabHome = variables["datalab_home"]
var containerBackHome = filepath.Join(pool.config.DataBackupHome, datalabName+"-"+containerID)
var err = os.Rename(datalabHome, containerBackHome)
log.Printf("backup userdata from '%s' to '%s', err = %v", datalabHome, containerBackHome, err)
}
type HookCallback func() error
func (pool *DatalabPool) AllocateInstance(
nonce string, filename string, content []byte,
) (containerID string, hookAfterStart HookCallback, err error) {
pool.mutex.Lock()
defer pool.mutex.Unlock()
var datalabID int
datalabID, err = pool.findNextDatalabID()
if err != nil {
log.Printf("could not find available datalab id")
return
}
var variables map[string]string
variables = pool.makeContainerVairables(datalabID)
err = pool.prepareContainerDirectory(variables["datalab_home"])
if err != nil {
return
}
containerID, err = pool.createInstance(datalabID, variables)
if err != nil {
log.Printf("create datalab instance failure: %v, err: %v", datalabID, err)
return
}
err = pool.setNonceContainer(nonce, containerID, pool.config.NonceTimeout)
if err != nil {
log.Printf("set nonce container failure: %v, err: %v", datalabID, err)
return
}
err = pool.populateInstanceData(variables["datalab_home"], filename, content)
if err != nil {
log.Printf("populate instance data: %v, err = %v", datalabID, err)
return
}
hookAfterStart = func() error {
log.Printf("start running AfterStart: %v", datalabID)
variables["default_filename"] = filename
return pool.runScript(pool.config.HookAfterStart, variables)
}
return
}
func (pool *DatalabPool) populateInstanceData(datalabHome string, filename string, content []byte) (err error) {
var (
contentDirectory string
fout *os.File
contentFilename string
)
contentDirectory = filepath.Join(datalabHome, "content")
err = os.MkdirAll(contentDirectory, 0755)
if err != nil {
log.Printf("create content directory failure: %s, err = %v", contentDirectory, err)
return
}
contentFilename = filepath.Join(contentDirectory, filename)
if content != nil && len(content) > 0 {
if fout, err = os.Create(contentFilename); err != nil {
log.Printf("create content file failure: %s, err = %v", contentFilename, err)
return
}
defer fout.Close()
fout.Write(content)
} else {
log.Printf("removing empty file: %v", contentFilename)
os.Remove(contentFilename)
}
return
}
func (pool *DatalabPool) makeNonceKey(nonce string) string {
return fmt.Sprintf("nonce:%s", nonce)
}
func (pool *DatalabPool) setNonceContainer(nonce string, containerID string, ttl int) (err error) {
var key = pool.makeNonceKey(nonce)
err = pool.store.Set(key, containerID)
if err != nil {
return
}
err = pool.store.Expire(key, ttl)
return
}
func (pool *DatalabPool) makeContainerVairables(datalabID int) map[string]string {
var datalabName string = fmt.Sprintf("datalab_%02d", datalabID)
var datalabHome string = filepath.Join(pool.config.DataHome, datalabName)
datalabHome, _ = filepath.Abs(datalabHome)
return map[string]string{
"datalab_id": fmt.Sprintf("%d", datalabID),
"datalab_name": datalabName,
"datalab_port": fmt.Sprintf("%d", pool.config.BasePort+datalabID),
"datalab_home": datalabHome,
}
}
func (pool *DatalabPool) createInstance(datalabID int, variables map[string]string) (containerID string, err error) {
// currUser, _ := user.Current()
// ustr := fmt.Sprintf("%s:%s", currUser.Uid, currUser.Gid),
var container *docker.Container
var createContainerOptions = docker.CreateContainerOptions{
Name: variables["datalab_name"],
Config: &docker.Config{
User: "0",
Image: "gcr.io/cloud-datalab/datalab:latest",
Labels: map[string]string{
"datalab_id": variables["datalab_id"],
},
Env: []string{
`HOME=/content`,
`DATALAB_ENV=GCE`,
`DATALAB_DEBUG=true`,
`DATALAB_SETTINGS_OVERRIDES={"enableAutoGCSBackups":true,"consoleLogLevel":"info"}`,
`DATALAB_GIT_AUTHOR=backup@localhost`,
`DATALAB_INITIAL_USER_SETTINGS=`,
},
},
HostConfig: &docker.HostConfig{
AutoRemove: pool.config.InstanceAutoRemove,
Binds: []string{
fmt.Sprintf("%s/content:/content", variables["datalab_home"]),
fmt.Sprintf("%s/tmp:/tmp", variables["datalab_home"]),
},
PortBindings: map[docker.Port][]docker.PortBinding{
"8080/tcp": []docker.PortBinding{
docker.PortBinding{HostIP: "0.0.0.0", HostPort: variables["datalab_port"]},
},
},
},
}
container, err = pool.dockerClient.CreateContainer(createContainerOptions)
createOptions, _ := yaml.Marshal(createContainerOptions)
log.Printf("create container options:\n%v", string(createOptions))
containerDetails, _ := yaml.Marshal(container)
log.Printf("created container details:\n%v", string(containerDetails))
if err != nil {
log.Printf("create instance failure: %v, err: %v", datalabID, err)
return
}
err = pool.dockerClient.StartContainer(container.ID, nil)
if err != nil {
log.Printf("start instance failure: %v - %v, err: %v", container.ID, container.Name, err)
return
}
pool.UpdateInstanceUserEvent(container.ID, UserEventIdleReset)
pool.UpdateInstanceUserEvent(container.ID, UserEventKeepAlive)
containerID = container.ID
return
}
func (pool *DatalabPool) prepareContainerDirectory(datalabHome string) (err error) {
var contentDirectory = filepath.Join(datalabHome, "content")
err = os.MkdirAll(contentDirectory, 0755)
return
}
func (pool *DatalabPool) runScript(script string, variables map[string]string) (err error) {
log.Printf("running script: %s", strings.Replace(script, "\n", " ", -1))
log.Printf("running script variables: %#v", variables)
var startCommandLine string
startCommandLine = pool.replaceStringVariables(script, variables)
var cmdList []string
cmdList = strings.Split(strings.TrimSpace(startCommandLine), "\n")
if len(cmdList) == 0 {
log.Printf("cmdline is empty: %v", cmdList)
return
}
var deadLine = time.Now().Add(time.Duration(pool.config.HookScriptMaxExecutionSeconds) * time.Second)
ctx, cancel := context.WithDeadline(context.Background(), deadLine)
defer cancel()
err = pool.executeCommandAndWait(ctx, cmdList)
if err != nil {
log.Printf("run script error = %+v", err)
}
log.Printf("run script success: %v", cmdList)
return
}
func (pool *DatalabPool) executeCommandAndWait(ctx context.Context, cmdList []string) error {
var cmd *exec.Cmd
cmd = exec.CommandContext(ctx, cmdList[0], cmdList[1:]...)
var err error
var cmdStdoutReader, cmdStderrReader io.ReadCloser
cmdStdoutReader, err = cmd.StdoutPipe()
if err != nil {
log.Printf("create stdout pipe failure: err = %v", err)
return err
}
defer cmdStdoutReader.Close()
cmdStderrReader, err = cmd.StderrPipe()
if err != nil {
log.Printf("create stderr pipe failure: err = %v", err)
return err
}
defer cmdStderrReader.Close()
cmd.Start()
go CtxCopy(ctx, os.Stdout, cmdStdoutReader)
go CtxCopy(ctx, os.Stderr, cmdStderrReader)
if err = cmd.Wait(); err != nil {
log.Printf("run script failure: %v, err = %v", cmdList, err)
return err
}
return nil
}
func (pool *DatalabPool) replaceStringVariables(source string, variables map[string]string) string {
for k, v := range variables {
var replacer string
replacer = fmt.Sprintf("${%s}", k)
source = strings.Replace(source, replacer, v, -1)
}
return source
}
func (pool *DatalabPool) findNextDatalabID() (id int, err error) {
var containers []docker.APIContainers
containers, err = pool.dockerClient.ListContainers(docker.ListContainersOptions{
All: true,
Filters: map[string][]string{
"label": []string{"datalab_id"},
},
})
if err != nil {
return
}
var ports = make(map[int]string)
for _, item := range containers {
var port int
var err2 error
if port, err2 = strconv.Atoi(item.Labels["datalab_id"]); err2 != nil {
continue
}
ports[port] = item.ID
}
for id = 1; id <= pool.config.MaxInstanceCount; id++ {
if _, ok := ports[id]; !ok {
return
}
}
id = 0
err = ErrDatalabInstanceNotAvailable
return
}
func (pool *DatalabPool) GetInstanceByDatalabID(datalabID int) (containerID string, err error) {
pool.mutex.RLock()
defer pool.mutex.RUnlock()
var containers []docker.APIContainers
containers, err = pool.dockerClient.ListContainers(docker.ListContainersOptions{
Filters: map[string][]string{
"label": []string{
fmt.Sprintf("datalab_id=%d", datalabID),
},
},
})
if err != nil {
return
}
if len(containers) < 1 {
log.Printf("containers not found: %d, count = %d", datalabID, len(containers))
err = fmt.Errorf("container not found or not unique")
return
}
containerID = containers[0].ID
return
}
func (pool *DatalabPool) GetInstanceByNonce(nonce string, checkRunning bool) (containerID string, err error) {
pool.mutex.RLock()
defer pool.mutex.RUnlock()
var key = pool.makeNonceKey(nonce)
containerID, err = pool.store.Get(key)
if err != nil {
log.Printf("nonce not found: %v, err = %v", nonce, err)
return
}
if checkRunning {
var container *docker.Container
container, err = pool.getInstanceByID(containerID)
if err != nil {
log.Printf("container id not found: %v", containerID)
return
}
if !container.State.Running {
err = ErrDatalabInstanceNotRunning
log.Printf("container id not running: %v", containerID)
return
}
}
return
}
func (pool *DatalabPool) getInstanceByID(containerID string) (container *docker.Container, err error) {
container, err = pool.dockerClient.InspectContainer(containerID)
return
}
func (pool *DatalabPool) GetInstanceAddressByContainerID(containerID string) (instAddress string, err error) {
pool.mutex.RLock()
defer pool.mutex.RUnlock()
var container *docker.Container
container, err = pool.getInstanceByID(containerID)
if err != nil {
log.Printf("container not found: %v", containerID)
return
}
if !container.State.Running {
err = ErrDatalabInstanceNotRunning
log.Printf("container not running: %v", containerID)
return
}
var (
idstr string
datalabID int
variables map[string]string
)
idstr = container.Config.Labels["datalab_id"]
datalabID, err = strconv.Atoi(idstr)
if err != nil {
log.Printf("label datalab_id is invalid: containerID = %s, label = %s, err = %v", containerID, idstr, err)
return
}
variables = pool.makeContainerVairables(datalabID)
instAddress = fmt.Sprintf("http://127.0.0.1:%s", variables["datalab_port"])
return
}
func (pool *DatalabPool) KillInstance(containerID string) (err error) {
pool.mutex.Lock()
defer pool.mutex.Unlock()
log.Printf("start killing instance: %s", containerID)
err = pool.dockerClient.KillContainer(docker.KillContainerOptions{
ID: containerID,
})
pool.store.Delete(pool.makeContainerKey(containerID))
return
}
const (
UserEventIdleReset = "UserEventIdleReset"
UserEventKeepAlive = "UserEventKeepAlive"
)
func (pool *DatalabPool) UpdateInstanceUserEvent(containerID string, event string) {
var containerKey = pool.makeContainerKey(containerID)
var now = fmt.Sprintf("%d", time.Now().Unix())
pool.store.HSet(containerKey, event, now)
}
func (pool *DatalabPool) makeContainerKey(containerID string) string {
return fmt.Sprintf("container:%s", containerID)
}