/
wasmrunnable.go
159 lines (125 loc) · 4.17 KB
/
wasmrunnable.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
package engine
import (
"encoding/json"
"os"
"github.com/suborbital/reactr/api"
"github.com/suborbital/reactr/engine/moduleref"
"github.com/suborbital/reactr/engine/runtime"
"github.com/suborbital/reactr/request"
"github.com/suborbital/reactr/scheduler"
"github.com/pkg/errors"
)
//wasmRunner represents a wasm-based runnable
type wasmRunner struct {
env *runtime.WasmEnvironment
}
// newRunnerFromFile returns a new *wasmRunner
func newRunnerFromFile(filepath string, api api.HostAPI) (*wasmRunner, error) {
file, err := os.Open(filepath)
if err != nil {
return nil, errors.Wrap(err, "failed to Open")
}
ref := moduleref.RefWithReader("", "", file)
runner := newRunnerFromRef(ref, api)
return runner, nil
}
// newRunnerFromRef creates a wasmRunner from a moduleRef
func newRunnerFromRef(ref *moduleref.WasmModuleRef, api api.HostAPI) *wasmRunner {
builder := runtimeBuilder(ref, api)
environment := runtime.NewEnvironment(builder)
r := &wasmRunner{
env: environment,
}
return r
}
// Run runs a wasmRunner
func (w *wasmRunner) Run(job scheduler.Job, ctx *scheduler.Ctx) (interface{}, error) {
var jobBytes []byte
var req *request.CoordinatedRequest
// check if the job is a CoordinatedRequest (pointer or bytes), and set up the WasmInstance if so
if jobReq, ok := job.Data().(*request.CoordinatedRequest); ok {
req = jobReq
} else if jobReq, err := request.FromJSON(job.Bytes()); err == nil {
req = jobReq
} else {
// if it's not a request, treat it as normal data
bytes, bytesErr := interfaceToBytes(job.Data())
if bytesErr != nil {
return nil, errors.Wrap(bytesErr, "failed to parse job for Wasm Runnable")
}
jobBytes = bytes
}
if req != nil {
// save the coordinated request into the
// job context for use by the API package
ctx.Context = api.ContextWithRequest(ctx.Context, req)
jobBytes = req.Body
}
var output []byte
var runErr error
var callErr error
if err := w.env.UseInstance(ctx, func(instance *runtime.WasmInstance, ident int32) {
inPointer, writeErr := instance.WriteMemory(jobBytes)
if writeErr != nil {
runErr = errors.Wrap(writeErr, "failed to instance.writeMemory")
return
}
// execute the Runnable's Run function, passing the input data and ident
// set runErr but don't return because the ExecutionResult error should also be grabbed
_, callErr = instance.Call("run_e", inPointer, int32(len(jobBytes)), ident)
// get the results from the instance
output, runErr = instance.ExecutionResult()
// deallocate the memory used for the input
instance.Deallocate(inPointer, len(jobBytes))
}); err != nil {
return nil, errors.Wrap(err, "failed to useInstance")
}
if runErr != nil {
// we do not wrap the error here as we want to
// propogate its exact type to the caller (specifically scheduler.RunErr)
return nil, runErr
}
if callErr != nil {
// if the runnable didn't return an explicit runErr, still check to see if there was an
// error executing the module in the first place. It's posslble for both to be non-nil
// in which case returning the runErr takes precedence, which is why it's checked first.
return nil, errors.Wrap(callErr, "wasm execution error")
}
if req != nil {
resp := &request.CoordinatedResponse{
Output: output,
RespHeaders: req.RespHeaders,
}
return resp, nil
}
return output, nil
}
// OnChange runs when a worker starts using this Runnable
func (w *wasmRunner) OnChange(evt scheduler.ChangeEvent) error {
switch evt {
case scheduler.ChangeTypeStart:
if err := w.env.AddInstance(); err != nil {
return errors.Wrap(err, "failed to addInstance")
}
case scheduler.ChangeTypeStop:
if err := w.env.RemoveInstance(); err != nil {
return errors.Wrap(err, "failed to removeInstance")
}
}
return nil
}
func interfaceToBytes(data interface{}) ([]byte, error) {
// if data is []byte or string, return it as-is
if b, ok := data.([]byte); ok {
return b, nil
} else if s, ok := data.(string); ok {
return []byte(s), nil
}
// otherwise, assume it's a struct of some kind,
// so JSON marshal it and return it
dataJSON, err := json.Marshal(data)
if err != nil {
return nil, errors.Wrap(err, "failed to Marshal job data")
}
return dataJSON, nil
}