You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/11/07 18:49:17 UTC

[GitHub] [beam] lostluck commented on a diff in pull request #23994: Implement simple go wasmer example

lostluck commented on code in PR #23994:
URL: https://github.com/apache/beam/pull/23994#discussion_r1015773416


##########
sdks/go/examples/wasm/wasm.go:
##########
@@ -0,0 +1,134 @@
+// 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.
+
+// wasm is a simple example that loads and executes a wasm file function.
+// simple.wasm and simple.rs were copied from the example provided by the wasmer-go library:
+// https://github.com/wasmerio/wasmer-go/tree/master/examples/appendices
+//
+// New Concepts:
+// 1. Load a wasm file compiled using https://wasmer.io/
+// 2. Execute a wasm function within a DoFn
+package main
+
+import (
+	"context"
+	_ "embed"
+	"fmt"
+
+	"github.com/apache/beam/sdks/v2/go/pkg/beam"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/debug"
+	"github.com/wasmerio/wasmer-go/wasmer"
+)
+
+const (
+	wasmFunctionName = "sum"
+)
+
+//go:embed simple.wasm
+var simple []byte
+
+func init() {
+	// register.DoFnXxY registers a struct DoFn so that it can be correctly
+	// serialized and does some optimization to avoid runtime reflection. Since
+	// wasmFn's ProcessElement func has 1 input (elem) and 2 outputs (int32, error),
+	// we use register.DoFn1x2 and provide its input and output types as its constraints.
+	// Struct DoFns must be registered for a pipeline to run.
+	register.DoFn1x2[elem, int32, error](&wasmFn{})
+	// register.FunctionXxY registers a functional DoFn to optimize execution at runtime.
+	// vToElemFn takes 1 input (int32) and returns 1 output (elem).
+	register.Function1x1(vToElemFn)
+}
+
+func main() {
+	if err := run(context.Background()); err != nil {
+		panic(err)
+	}
+}
+
+func run(ctx context.Context) error {

Review Comment:
   This is missing a beam.Init() call, which implies it may not work outside of direct or  local runner contexts.
   
   Has this been tested and verified on any portable runners? The Go Direct Runner is insufficient alone, and we need to validate on Dataflow, or Flink, or the Python portable runner. Any portable runner really.



##########
sdks/go/examples/wasm/wasm.go:
##########
@@ -0,0 +1,134 @@
+// 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.
+
+// wasm is a simple example that loads and executes a wasm file function.
+// simple.wasm and simple.rs were copied from the example provided by the wasmer-go library:
+// https://github.com/wasmerio/wasmer-go/tree/master/examples/appendices
+//
+// New Concepts:
+// 1. Load a wasm file compiled using https://wasmer.io/
+// 2. Execute a wasm function within a DoFn
+package main
+
+import (
+	"context"
+	_ "embed"
+	"fmt"
+
+	"github.com/apache/beam/sdks/v2/go/pkg/beam"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/debug"
+	"github.com/wasmerio/wasmer-go/wasmer"
+)
+
+const (
+	wasmFunctionName = "sum"
+)
+
+//go:embed simple.wasm

Review Comment:
   Spectacular! This is good since it will probably avoid issues with distributed runners, since it's simply compiled in.



##########
sdks/go/examples/wasm/wasm.go:
##########
@@ -0,0 +1,134 @@
+// 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.
+
+// wasm is a simple example that loads and executes a wasm file function.
+// simple.wasm and simple.rs were copied from the example provided by the wasmer-go library:
+// https://github.com/wasmerio/wasmer-go/tree/master/examples/appendices
+//
+// New Concepts:
+// 1. Load a wasm file compiled using https://wasmer.io/
+// 2. Execute a wasm function within a DoFn
+package main
+
+import (
+	"context"
+	_ "embed"
+	"fmt"
+
+	"github.com/apache/beam/sdks/v2/go/pkg/beam"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/debug"
+	"github.com/wasmerio/wasmer-go/wasmer"

Review Comment:
   Tangent question: Did you consider Wazero instead? https://github.com/tetratelabs/wazero
   Wasmer is robustly supported (and we should have this example), but wazero has the non-trivial benefit of no CGO dependencies. (Not that I see using beam outside of AMD64 and aarch64, but it means it's all quickly compiled with Go.)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org