Skip to content

Commit

Permalink
fix readonly mode on wasmserver
Browse files Browse the repository at this point in the history
  • Loading branch information
anthdm committed Dec 31, 2023
1 parent bedac30 commit ad6d96c
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 201 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
.PHONY: proto

build:
@go build -o bin/ffaas cmd/ffaas/main.go
@go build -o bin/api cmd/api/main.go
@go build -o bin/wasmserver cmd/wasmserver/main.go

wasmserver:
@go build -o bin/wasmserver cmd/wasmserver/main.go
wasmserver: build
@./bin/wasmserver

run: build
@./bin/ffaas --seed
api: build
@./bin/api --seed

test:
@go test ./pkg/* -v
Expand Down
21 changes: 15 additions & 6 deletions cmd/ffaas/main.go → cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/anthdm/ffaas/pkg/api"
"github.com/anthdm/ffaas/pkg/config"
"github.com/anthdm/ffaas/pkg/runtime"
"github.com/anthdm/ffaas/pkg/storage"
"github.com/anthdm/ffaas/pkg/types"
"github.com/anthdm/ffaas/pkg/version"
Expand All @@ -19,7 +18,6 @@ import (
)

func main() {

var (
modCache = storage.NewDefaultModCache()
metricStore = storage.NewMemoryMetricStore()
Expand All @@ -36,7 +34,10 @@ func main() {
log.Fatal(err)
}

store, err := storage.NewBoltStore(config.Get().StoragePath)
cfg := storage.NewBoltConfig().
WithPath(config.Get().StoragePath).
WithReadOnly(false)
store, err := storage.NewBoltStore(cfg)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -93,9 +94,17 @@ func seedEndpoint(store storage.Store, cache storage.ModCacher) {
store.CreateDeploy(deploy)
fmt.Printf("endpoint: %s\n", endpoint.URL)

compCache := wazero.NewCompilationCache()
runtime.Compile(context.Background(), compCache, deploy.Blob)
cache.Put(endpoint.ID, compCache)
// compCache := wazero.NewCompilationCache()
// compile(context.TODO(), compCache, deploy.Blob)
// fmt.Printf("%+v\n", compCache)
// cache.Put(endpoint.ID, compCache)
}

func compile(ctx context.Context, cache wazero.CompilationCache, blob []byte) {
config := wazero.NewRuntimeConfig().WithCompilationCache(cache)
runtime := wazero.NewRuntimeWithConfig(ctx, config)
defer runtime.Close(ctx)
runtime.CompileModule(ctx, blob)
}

func banner() string {
Expand Down
5 changes: 4 additions & 1 deletion cmd/wasmserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func main() {
log.Fatal(err)
}

store, err := storage.NewBoltStore(".db")
cfg := storage.NewBoltConfig().
WithPath(config.Get().StoragePath).
WithReadOnly(true)
store, err := storage.NewBoltStore(cfg)
if err != nil {
log.Fatal(err)
}
Expand Down
Binary file modified examples/go/app.wasm
Binary file not shown.
5 changes: 3 additions & 2 deletions examples/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"math/rand"
"net/http"

ffaas "github.com/anthdm/ffaas/sdk"
)

func myHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -12,6 +14,5 @@ func myHandler(w http.ResponseWriter, r *http.Request) {
}

func main() {
fmt.Println("from the wasm guest")
//ffaas.HandleFunc(myHandler)
ffaas.HandleFunc(myHandler)
}
75 changes: 72 additions & 3 deletions pkg/actrs/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package actrs
import (
"context"
"fmt"
"io"
"log/slog"
"net/http"

Expand All @@ -13,6 +14,8 @@ import (
"github.com/stealthrocket/wasi-go"
"github.com/stealthrocket/wasi-go/imports"
"github.com/tetratelabs/wazero"
wapi "github.com/tetratelabs/wazero/api"
"github.com/vmihailenco/msgpack/v5"
)

const KindRuntime = "runtime"
Expand Down Expand Up @@ -46,9 +49,10 @@ func (r *Runtime) Receive(c *actor.Context) {
return
}
cache := wazero.NewCompilationCache()
r.exec(context.TODO(), deploy.Blob, cache, endpoint.Environment)
httpmod, _ := NewRequestModule(msg)
r.exec(context.TODO(), deploy.Blob, cache, endpoint.Environment, httpmod)
resp := &proto.HTTPResponse{
Response: []byte("hello"),
Response: httpmod.responseBytes,
RequestID: msg.ID,
StatusCode: http.StatusOK,
}
Expand All @@ -57,7 +61,7 @@ func (r *Runtime) Receive(c *actor.Context) {
}
}

func (r *Runtime) exec(ctx context.Context, blob []byte, cache wazero.CompilationCache, env map[string]string) {
func (r *Runtime) exec(ctx context.Context, blob []byte, cache wazero.CompilationCache, env map[string]string, httpmod *RequestModule) {
config := wazero.NewRuntimeConfig().WithCompilationCache(cache)
runtime := wazero.NewRuntimeWithConfig(ctx, config)
defer runtime.Close(ctx)
Expand Down Expand Up @@ -90,6 +94,8 @@ func (r *Runtime) exec(ctx context.Context, blob []byte, cache wazero.Compilatio
}
defer system.Close(ctx)

httpmod.Instanciate(ctx, runtime)

_, err = runtime.InstantiateModule(ctx, mod, wazero.NewModuleConfig())
if err != nil {
slog.Warn("failed to instanciate guest module", "err", err)
Expand All @@ -106,3 +112,66 @@ func envMapToSlice(env map[string]string) []string {
}
return slice
}

type RequestModule struct {
requestBytes []byte
responseBytes []byte
}

func NewRequestModule(req *proto.HTTPRequest) (*RequestModule, error) {
b, err := msgpack.Marshal(req)
if err != nil {
return nil, err
}
return &RequestModule{
requestBytes: b,
}, nil
}

func (r *RequestModule) WriteResponse(w io.Writer) (int, error) {
return w.Write(r.responseBytes)
}

func (r *RequestModule) Instanciate(ctx context.Context, runtime wazero.Runtime) error {
_, err := runtime.NewHostModuleBuilder("env").
NewFunctionBuilder().
WithGoModuleFunction(r.moduleMalloc(), []wapi.ValueType{}, []wapi.ValueType{wapi.ValueTypeI32}).
Export("malloc").
NewFunctionBuilder().
WithGoModuleFunction(r.moduleWriteRequest(), []wapi.ValueType{wapi.ValueTypeI32}, []wapi.ValueType{}).
Export("write_request").
NewFunctionBuilder().
WithGoModuleFunction(r.moduleWriteResponse(), []wapi.ValueType{wapi.ValueTypeI32, wapi.ValueTypeI32}, []wapi.ValueType{}).
Export("write_response").
Instantiate(ctx)
return err
}

func (r *RequestModule) Close(ctx context.Context) error {
r.responseBytes = nil
r.requestBytes = nil
return nil
}

func (r *RequestModule) moduleMalloc() wapi.GoModuleFunc {
return func(ctx context.Context, module wapi.Module, stack []uint64) {
size := uint64(len(r.requestBytes))
stack[0] = uint64(wapi.DecodeU32(size))
}
}

func (r *RequestModule) moduleWriteRequest() wapi.GoModuleFunc {
return func(ctx context.Context, module wapi.Module, stack []uint64) {
offset := wapi.DecodeU32(stack[0])
module.Memory().Write(offset, r.requestBytes)
}
}

func (r *RequestModule) moduleWriteResponse() wapi.GoModuleFunc {
return func(ctx context.Context, module wapi.Module, stack []uint64) {
offset := wapi.DecodeU32(stack[0])
size := wapi.DecodeU32(stack[1])
resp, _ := module.Memory().Read(offset, size)
r.responseBytes = resp
}
}
177 changes: 0 additions & 177 deletions pkg/runtime/runtime.go

This file was deleted.

Loading

0 comments on commit ad6d96c

Please sign in to comment.