/
executor.go
537 lines (463 loc) · 13.9 KB
/
executor.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
package executor
import (
"archive/zip"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"github.com/jinzhu/gorm"
"github.com/bnb-chain/greenfield-execution-provider/common"
"github.com/bnb-chain/greenfield-execution-provider/model"
"github.com/bnb-chain/greenfield-execution-provider/util"
sdkClient "github.com/bnb-chain/greenfield-go-sdk/client"
"github.com/bnb-chain/greenfield-go-sdk/types"
)
const downloadDir = "download"
const executableConfigFileName = "ExecutableConfig.json"
var outputDir string
var outputFilesName []string
var inputDir string
var inputFilesName []string
var outputBucketName string
type Executor struct {
DB *gorm.DB
Client sdkClient.Client
currentTaskId int64
receipt Receipt
}
type Receipt struct {
gasUsed uint64
returnCode string
resultObjectId string
logObjectId string
}
type ExecutableConfig struct {
Name string `json:"name"`
Version string `json:"version"`
Author string `json:"author"`
BinaryDigest string `json:"binaryDigest"`
SourceCodeFile string `json:"sourceCodeFile"`
SourceCodeDigest string `json:"sourceCodeDigest"`
Abi struct {
Entry string `json:"entry"`
Signature string `json:"signature"`
} `json:"abi"`
Executable struct {
WasmMainFile string `json:"wasmMainFile"`
WasmLibraries string `json:"wasmLibraries"`
} `json:"executable"`
Data struct {
InputDir string `json:"inputDir"`
InputFiles []string `json:"inputFiles"`
OutputDir string `json:"outputDir"`
LogDir string `json:"logDir"`
OutputFiles []string `json:"outputFiles"`
} `json:"data"`
Capabilities struct {
FileOps struct {
NativeFile struct {
Read bool `json:"read"`
Write bool `json:"write"`
Create bool `json:"create"`
} `json:"nativeFile"`
} `json:"fileOps"`
} `json:"capabilities"`
}
type ExecutionReport struct {
GasUsed uint64 `json:"gasUsed"`
ResultMsg string `json:"resultMsg"`
}
func unzipFile(fileName string, dst string) {
util.Logger.Infof("try to unzip file %s to %s\n", fileName, dst)
archive, err := zip.OpenReader(fileName)
if err != nil {
panic(err)
}
defer archive.Close()
for _, f := range archive.File {
filePath := filepath.Join(dst, f.Name)
util.Logger.Info("unzipping file ", filePath)
if f.FileInfo().IsDir() {
util.Logger.Info("creating directory..." + filePath)
os.MkdirAll(filePath, os.ModePerm)
continue
}
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
panic(err)
}
dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
panic(err)
}
fileInArchive, err := f.Open()
if err != nil {
panic(err)
}
if _, err := io.Copy(dstFile, fileInArchive); err != nil {
panic(err)
}
dstFile.Close()
fileInArchive.Close()
}
}
// NewExecutor returns the executor instance
func NewExecutor(db *gorm.DB, client sdkClient.Client) *Executor {
return &Executor{
db,
client,
0,
Receipt{
gasUsed: 0,
returnCode: "",
resultObjectId: "",
logObjectId: "",
},
}
}
// Start starts the routines of executor
func (ex *Executor) Start() {
for {
time.Sleep(common.ExecutorFetchInterval)
go ex.tryInvokeExecuteTask()
}
}
func (ex *Executor) tryInvokeExecuteTask() {
// 1. load executeTask from db, compare the taskID
executionTask := model.ExecutionTask{}
err := ex.DB.Model(&model.ExecutionTask{}).Where("status = ? and task_id > ?", model.ExecutionTaskStatusStatusInit,
ex.currentTaskId).Order("task_id asc").Take(&executionTask).Error
if err != nil {
util.Logger.Error("tryInvokeExecuteTake error " + err.Error())
return
} else {
util.Logger.Error("find executionTask: " + executionTask.ExecutionObjectId)
}
ex.currentTaskId = executionTask.TaskId
// 2. download binary and data
executableConfig, execDir, err := ex.downloadExecutable(executionTask.ExecutionObjectId)
if err != nil {
return
}
err = ex.downloadInputFiles(executionTask.InputFiles, executableConfig)
if err != nil {
return
}
outputDir = executableConfig.Data.OutputDir
if err := os.MkdirAll(outputDir, os.ModePerm); err != nil {
panic(err)
}
maxGasEnv := "MAX_GAS=" + executionTask.MaxGas
// TODO: use ExecutableConfig.json to identify the path
wasmFileEnv := "WASM_FILE=" + filepath.Base(execDir) + "/" + executableConfig.Executable.WasmMainFile
inputDir = executableConfig.Data.InputDir
inputFilesName = executableConfig.Data.InputFiles
var inputFiles string
for i := 0; i < len(inputFilesName); i++ {
inputFiles += inputDir + "/" + inputFilesName[i] + " "
}
inputFileEnv := "INPUT_FILES=" + inputFiles
outputFilesName = executableConfig.Data.OutputFiles
var outputFiles string
for j := 0; j < len(outputFilesName); j++ {
util.Logger.Infof("generate output files: %s/%s\n", outputDir, outputFilesName[j])
outputFiles += outputDir + "/" + outputFilesName[j] + " "
}
outputFileEnv := "OUTPUT_FILES=" + outputFiles
argsEnv := []string{maxGasEnv, wasmFileEnv, inputFileEnv, outputFileEnv}
abs, err := filepath.Abs("./")
wasmMountDir, _ := filepath.Abs(execDir)
inputMountDir := abs + "/" + inputDir
outputMountDir := abs + "/" + outputDir
if err != nil {
panic(err)
}
// Pull Docker image
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
defer cli.Close()
reader, err := cli.ImagePull(ctx, "gnfdexec/gnfdexe:latest", dockerTypes.ImagePullOptions{})
if err != nil {
panic(err)
}
defer reader.Close()
io.Copy(os.Stdout, reader)
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: "gnfdexec/gnfdexe",
Env: argsEnv,
Tty: false,
}, &container.HostConfig{
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: wasmMountDir,
Target: "/opt/gnfd/workdir/" + filepath.Base(execDir),
},
{
Type: mount.TypeBind,
Source: inputMountDir,
Target: "/opt/gnfd/workdir/input",
},
{
Type: mount.TypeBind,
Source: outputMountDir,
Target: "/opt/gnfd/workdir/output",
},
},
}, nil, nil, "")
if err != nil {
panic(err)
}
util.Logger.Infof("start Container " + resp.ID)
if err := cli.ContainerStart(ctx, resp.ID, dockerTypes.ContainerStartOptions{}); err != nil {
util.Logger.Errorf(err.Error())
panic(err)
}
util.Logger.Infof("wait Container " + resp.ID)
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
util.Logger.Errorf(err.Error())
panic(err)
}
case <-statusCh:
}
out, err := cli.ContainerLogs(ctx, resp.ID, dockerTypes.ContainerLogsOptions{ShowStdout: true})
if err != nil {
panic(err)
}
f, err := os.Create(outputDir + "/log.txt")
io.Copy(f, out)
executeReport, err := readExecuteReport("./output/report.json")
if err != nil {
util.Logger.Errorf(err.Error())
ex.receipt.returnCode = err.Error()
}
ex.receipt.returnCode = executeReport.ResultMsg
ex.receipt.gasUsed = executeReport.GasUsed
// 4. stop and destroy container
stopAndRemoveContainer(ctx, cli, resp.ID)
// 5. upload result data and logs
err = ex.uploadResultsAndLogs()
if err != nil {
return
}
// 6. write receipt into db
err = ex.writeReceipt()
if err != nil {
return
}
return
}
func stopAndRemoveContainer(ctx context.Context, cli *client.Client, id string) {
util.Logger.Infof("stop and destroy container: " + id)
if err := cli.ContainerStop(ctx, id, nil); err != nil {
panic(err)
}
removeOptions := dockerTypes.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
}
if err := cli.ContainerRemove(ctx, id, removeOptions); err != nil {
util.Logger.Errorf("Unable to remove container: %s\n", err)
}
}
func readExecuteReport(reportJson string) (ExecutionReport, error) {
report := ExecutionReport{0, "nil"}
// Open report.json
jsonFile, err := os.Open(reportJson)
if err != nil {
return report, err
}
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
return report, err
}
err = json.Unmarshal(byteValue, &report)
return report, err
}
func (ex *Executor) downloadObject(objectId string) (string, error) {
// create download dir
_ = os.Mkdir(downloadDir, os.ModePerm)
objectInfo, err := ex.Client.HeadObjectByID(context.Background(), objectId)
if err != nil {
return "", err
}
// remember the executable object bucket name for result upload
outputBucketName = objectInfo.BucketName
ior, _, err := ex.Client.GetObject(context.Background(), objectInfo.BucketName, objectInfo.ObjectName, types.GetObjectOption{})
if err != nil {
return "", err
}
bts, err := io.ReadAll(ior)
if err != nil {
return "", err
}
objectPath := fmt.Sprintf("./%s/%s", downloadDir, objectInfo.ObjectName)
err = os.WriteFile(objectPath, bts, 0644)
if err != nil {
return "", err
}
return objectPath, err
}
func (ex *Executor) downloadExecutable(objectId string) (ExecutableConfig, string, error) {
util.Logger.Infof("try to download executable, objectId=%s", objectId)
executableZip, err := ex.downloadObject(objectId)
if err != nil {
util.Logger.Errorf("download executable failed, err=%s", err.Error())
return ExecutableConfig{}, "", err
}
unzipFile(executableZip, "./")
configDir, err := findDirectoryWithFile("./", executableConfigFileName)
if err != nil {
util.Logger.Errorf("can not find executable config file, err=%s", err.Error())
return ExecutableConfig{}, "", err
}
util.Logger.Infof("find work dir of wasm at %s\n", configDir)
var executableConfig ExecutableConfig
executableConfig, err = readExecutableConfig(configDir)
if err != nil {
util.Logger.Errorf("can not parse executable config file, err=%s,", err.Error())
}
return executableConfig, configDir, err
}
func findDirectoryWithFile(root, targetFile string) (string, error) {
var result string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Name() == targetFile {
abs, err := filepath.Abs(path)
if err != nil {
return err
}
result = filepath.Dir(abs)
return filepath.SkipDir
}
return nil
})
if err != nil {
return "", err
}
if result == "" {
return "", fmt.Errorf("file '%s' not found in '%s'", targetFile, root)
}
return result, nil
}
func readExecutableConfig(path string) (ExecutableConfig, error) {
config := ExecutableConfig{}
// Open json
jsonFile, err := os.Open(path + "/" + executableConfigFileName)
if err != nil {
return config, err
}
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
return config, err
}
err = json.Unmarshal(byteValue, &config)
return config, err
}
func (ex *Executor) downloadInputFiles(objectIds string, config ExecutableConfig) error {
util.Logger.Infof("try to download inputs, objects=%s", objectIds)
inputObjects := make([]string, 0)
err := json.Unmarshal([]byte(objectIds), &inputObjects)
if err != nil {
return err
}
for _, objectId := range inputObjects {
inputPath, err := ex.downloadObject(objectId)
if err != nil {
return err
}
if strings.HasSuffix(inputPath, ".zip") {
unzipFile(inputPath, ".")
// check InputDir
_, err = findDirectoryWithFile(".", config.Data.InputDir)
if err != nil {
util.Logger.Errorf("Can not find inputDir err=%s\n", err.Error())
return err
}
}
}
return nil
}
func (ex *Executor) uploadFile(dir string, fileName string) (string, error) {
// read file
filePath := dir + "/" + fileName
dataBuf, err := os.ReadFile(filePath)
if err != nil {
util.Logger.Error("Can not read input file: " + filePath)
return "", err
}
util.Logger.Infof("---> CreateObject (%s) and HeadObject into bucket (%s) <---\n", fileName, outputBucketName)
uploadTx, err := ex.Client.CreateObject(context.Background(), outputBucketName, fileName, bytes.NewReader(dataBuf), types.CreateObjectOptions{})
if err != nil {
util.Logger.Error("Error create object: " + err.Error())
return "", err
}
_, err = ex.Client.WaitForTx(context.Background(), uploadTx)
if err != nil {
util.Logger.Error("Error wait TX: " + err.Error())
return "", err
}
time.Sleep(5 * time.Second)
util.Logger.Infof("---> PutObject (%s) <---\n", fileName)
err = ex.Client.PutObject(context.Background(), outputBucketName, fileName, int64(len(dataBuf)),
bytes.NewReader(dataBuf), types.PutObjectOptions{})
if err != nil {
util.Logger.Error("Error put object: " + err.Error())
return "", err
}
time.Sleep(10 * time.Second)
dataObjectInfo, err := ex.Client.HeadObject(context.Background(), outputBucketName, fileName)
if err != nil {
util.Logger.Error("Error HeadObject: " + err.Error())
return "", err
}
util.Logger.Infof("Upload object %s, get ObjectID %s\n", filePath, dataObjectInfo.Id.String())
return dataObjectInfo.Id.String(), nil
}
func (ex *Executor) uploadResultsAndLogs() error {
resultObjectId, err := ex.uploadFile(outputDir, "result.txt")
if err != nil {
return err
}
logObjectId, err := ex.uploadFile(outputDir, "log.txt")
ex.receipt.resultObjectId = resultObjectId
ex.receipt.logObjectId = logObjectId
return err
}
func (ex *Executor) writeReceipt() error {
err := ex.DB.Model(&model.ExecutionTask{}).Where("status = ? and task_id = ?", model.ExecutionTaskStatusStatusInit,
ex.currentTaskId).Updates(
map[string]interface{}{
"status": model.ExecutionTaskStatusStatusExecuted,
"gas_used": ex.receipt.gasUsed,
"execution_status": ex.receipt.returnCode,
"result_data_uri": ex.receipt.resultObjectId,
"log_data_uri": ex.receipt.logObjectId,
}).Error
if err != nil {
util.Logger.Error("Fail to update executed task")
return err
}
return err
}