Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM Plugin for Pixiu #469

Merged
merged 21 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ require (
google.golang.org/grpc v1.45.0
google.golang.org/protobuf v1.28.0
gopkg.in/yaml.v2 v2.4.0
mosn.io/proxy-wasm-go-host v0.1.0
vimagination.zapto.org/byteio v0.0.0-20200222190125-d27cba0f0b10
vimagination.zapto.org/memio v0.0.0-20200222190306-588ebc67b97d // indirect
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ github.com/soheilhy/cmux v0.1.5-0.20210205191134-5ec6847320e5 h1:GJTW+uNMIV1RKwo
github.com/soheilhy/cmux v0.1.5-0.20210205191134-5ec6847320e5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
Expand Down Expand Up @@ -799,6 +800,8 @@ github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxW
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/wasmerio/wasmer-go v1.0.3 h1:9pWIlIqUKxALvFlWK8+Zy90qyqxd+8wlyVG91txh1TU=
github.com/wasmerio/wasmer-go v1.0.3/go.mod h1:0gzVdSfg6pysA6QVp6iVRPTagC6Wq9pOE8J86WKb2Fk=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -1323,6 +1326,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
mosn.io/proxy-wasm-go-host v0.1.0 h1:WtXMiOpuDyx3Vruf/BUAiu4bo30rSPx68E5vxEBTT6g=
mosn.io/proxy-wasm-go-host v0.1.0/go.mod h1:wBRY9Xjf77GZhvAuEc6nQXZ6dQK39kNU2ok/+Z8mSU8=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
Expand Down
2 changes: 1 addition & 1 deletion igt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ else
export EXT_NAME ?=
endif

CGO ?= 0
CGO ?= 1
maxingg marked this conversation as resolved.
Show resolved Hide resolved
ifeq ($(DEBUG), true)
BUILD_TYPE := debug
GCFLAGS := -gcflags="all=-N -l"
Expand Down
1 change: 1 addition & 0 deletions pkg/common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
HTTPApiConfigFilter = "dgp.filter.http.apiconfig"
HTTPTimeoutFilter = "dgp.filter.http.timeout"
TracingFilter = "dgp.filters.tracing"
HTTPWasmFilter = "dgp.filter.http.webassembly"
HTTPCircuitBreakerFilter = "dgp.filter.http.circuitbreaker"
HTTPAuthJwtFilter = "dgp.filter.http.auth.jwt"
HTTPCorsFilter = "dgp.filter.http.cors"
Expand Down
107 changes: 107 additions & 0 deletions pkg/filter/http/proxywasm/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 proxywasm

import (
"github.com/apache/dubbo-go-pixiu/pkg/common/constant"
"github.com/apache/dubbo-go-pixiu/pkg/common/extension/filter"
"github.com/apache/dubbo-go-pixiu/pkg/context/http"
"github.com/apache/dubbo-go-pixiu/pkg/logger"
"github.com/apache/dubbo-go-pixiu/pkg/wasm"
)

func init() {
filter.RegisterHttpFilter(&Plugin{})
}

type (
Plugin struct {
}

WasmFilterFactory struct {
cfg *Config
}

WasmFilter struct {
abiContextWrappers map[string]*wasm.ABIContextWrapper
}

Config struct {
WasmServices []*Service `yaml:"wasm_services" json:"wasm_services" mapstructure:"wasm_services"`
}

Service struct {
Name string `yaml:"name" json:"name" mapstructure:"name"`
}
)

func (w *WasmFilter) Decode(ctx *http.HttpContext) filter.FilterStatus {
const HEADER = "header"

// sample: display http header
wrapper := w.abiContextWrappers[HEADER]
wrapper.Context.Instance.Lock(wrapper.Context)
defer wrapper.Context.Instance.Unlock()

_ = wrapper.Context.GetExports().ProxyOnContextCreate(wrapper.ContextID, wasm.GetServiceRootID(HEADER))
maxingg marked this conversation as resolved.
Show resolved Hide resolved

_, _ = wrapper.Context.GetExports().ProxyOnRequestHeaders(wrapper.ContextID, int32(len(ctx.Request.Header)), 1)

_ = wrapper.Context.GetExports().ProxyOnDelete(wrapper.ContextID)

return filter.Continue
}

func (w *WasmFilter) Encode(ctx *http.HttpContext) filter.FilterStatus {
for _, wrapper := range w.abiContextWrappers {
if err := wasm.ContextDone(wrapper); err != nil {
logger.Warnf("[dubbo-go-pixiu] wasmFilter contextDone call failed: %v", err)
}
}
return filter.Continue
}

func (factory *WasmFilterFactory) Config() interface{} {
return factory.cfg
}

func (factory *WasmFilterFactory) Apply() error {
return nil
}

func (factory *WasmFilterFactory) PrepareFilterChain(ctx *http.HttpContext, chain filter.FilterChain) error {
filter := &WasmFilter{
abiContextWrappers: make(map[string]*wasm.ABIContextWrapper),
}
for _, service := range factory.cfg.WasmServices {
filter.abiContextWrappers[service.Name] = wasm.CreateABIContextByName(service.Name, ctx)
}
chain.AppendDecodeFilters(filter)
chain.AppendEncodeFilters(filter)
return nil
}

func (p *Plugin) Kind() string {
return constant.HTTPWasmFilter
}

func (p *Plugin) CreateFilterFactory() (filter.HttpFilterFactory, error) {
return &WasmFilterFactory{
cfg: &Config{},
}, nil
}
1 change: 1 addition & 0 deletions pkg/model/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Bootstrap struct {
Metric Metric `yaml:"metric" json:"metric" mapstructure:"metric"`
Node *Node `yaml:"node" json:"node" mapstructure:"node"`
Trace *TracerConfig `yaml:"tracing" json:"tracing" mapstructure:"tracing"`
Wasm *WasmConfig `yaml:"wasm" json:"wasm" mapstructure:"wasm"`
}

// Node node info for dynamic identifier
Expand Down
29 changes: 29 additions & 0 deletions pkg/model/wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 model

// WasmConfig contains all services' config.
type WasmConfig struct {
Services []WasmService `yaml:"services" json:"services" mapstructure:"services"`
}

// WasmService includes all information about the specific application.
type WasmService struct {
Name string `yaml:"name" json:"name" mapstructure:"name"`
Config map[string]interface{} `yaml:"config" json:"config" mapstructure:"config"`
}
1 change: 1 addition & 0 deletions pkg/pluginregistry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
_ "github.com/apache/dubbo-go-pixiu/pkg/filter/http/httpproxy"
_ "github.com/apache/dubbo-go-pixiu/pkg/filter/http/loadbalancer"
_ "github.com/apache/dubbo-go-pixiu/pkg/filter/http/proxyrewrite"
_ "github.com/apache/dubbo-go-pixiu/pkg/filter/http/proxywasm"
_ "github.com/apache/dubbo-go-pixiu/pkg/filter/http/remote"
_ "github.com/apache/dubbo-go-pixiu/pkg/filter/metric"
_ "github.com/apache/dubbo-go-pixiu/pkg/filter/network/dubboproxy"
Expand Down
89 changes: 89 additions & 0 deletions pkg/wasm/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 wasm

import (
"sync"
)

import (
"github.com/apache/dubbo-go-pixiu/pkg/config"
"github.com/apache/dubbo-go-pixiu/pkg/context/http"
"github.com/apache/dubbo-go-pixiu/pkg/logger"
"github.com/apache/dubbo-go-pixiu/pkg/model"
)

type WasmManager struct {
model *model.WasmConfig
serviceMap map[string]*WasmService
}

var manager *WasmManager
var once sync.Once

// InitWasmManager loads config in conf.yaml
func InitWasmManager(model *model.WasmConfig) {
manager = &WasmManager{
model: model,
serviceMap: make(map[string]*WasmService),
}

for _, service := range model.Services {
wasmService, err := createWasmService(service)
if err != nil {
logger.Warnf("[dubbo-go-pixiu] wasm config error: %v", err)
continue
}
manager.serviceMap[service.Name] = wasmService
}
}

// GetWasmServiceByName get WasmService By Name
func getWasmServiceByName(name string) *WasmService {
once.Do(func() {
bs := config.GetBootstrap()
InitWasmManager(bs.Wasm)
})
if wasmService, exist := manager.serviceMap[name]; exist {
return wasmService
}
logger.Warnf("[dubbo-go-pixiu] no wasmService named %v, check pixiu conf.yaml", name)

return nil
}

func GetServiceRootID(name string) int32 {
wasmService := getWasmServiceByName(name)
return wasmService.rootContextID
}

// CreateAbIContextByName create abiContext according to every request
func CreateABIContextByName(name string, ctx *http.HttpContext) *ABIContextWrapper {
wasmService := getWasmServiceByName(name)

if wasmService != nil {
return wasmService.createABIContextWrapper(ctx)
}
return nil
}

// ContextDone put wasmInstance back to Pool.
func ContextDone(wrapper *ABIContextWrapper) error {
wasmService := getWasmServiceByName(wrapper.name)
return wasmService.putWasmInstance(wrapper.Context.Instance)
}
Loading