You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "ashb (via GitHub)" <gi...@apache.org> on 2025/09/24 21:30:30 UTC
[PR] Separate out Go tasks and Go worker in to separate processes [airflow]
ashb opened a new pull request, #56079:
URL: https://github.com/apache/airflow/pull/56079
This change is a big one (too big if I'm honest, but I cannot for the life of
my think of a way of usefully separating this out) to the architecture of how
the Go-SDk works.
Previously the tasks ("the registry") and the executor would be compiled in to
a single binary, and the go worker could only execute Tasks for a single DAG
version. Neither of these were good properties for production-ready code.
This changes the architecture as follows
```
┌──────────────────────┐ ┌───────────────────────────┐
│ airflow-go-worker │ │ DAG Bundle Plugin Binary │
│ │ │ │
│ ┌──────────────────┐ │ │ ┌─────────────────────┐ │
│ │ Celery Consumer │ │ │ │ Task Registry │ │
│ └────────┬─────────┘ │ │ └─────────┬───────────┘ │
│ │ │ │ │ │
│ ┌────────▼─────────┐ │ │ ┌─────────▼───────────┐ │
│ │ Plugin Client │◄┼─────┼─┤ Plugin Server │ │
│ └──────────────────┘ │ │ └─────────────────────┘ │
│ │ │ │
└──────────────────────┘ └───────────────────────────┘
```
The `airflow-go-worker` in this diagram is the Executor Worker -- i.e. it gets
messages off a Celery queue and then hands off the Workload to another
process, communication to which is handled via Hashicorp's go-plugin.
Go-plugin is a well tested library used to implement both Terraform Providers
and Granfana plugins.
The package layout of this change is roughly as follows:
- `bundle/bundlev1/bundlev1server`
The server/bundle side of the plugin interface.
Contains the main entrypoint that bundles should call, `Serve()`
- `bundle/bundlev1`
The bundle-side types and interfaces. This is mostly what dag bundles will
import to register tasks:
```go
import "github.com/apache/airflow/go-sdk/bundle/bundlev1"
// ...
func (m *myBundle) RegisterDags(dagbag bundlev1.Registry) error {
tutorial_dag := bundle.AddDag("tutorial_dag")
tutorial_dag.AddTask(extract)
tutorial_dag.AddTask(transform)
tutorial_dag.AddTask(load)
}
```
- `internal/protov1`
The protobuf source and generated files, shared between clients and servers.
- `pkg/bundles/shared`
Config and code shared between clients and servers that is
global/non-versioned/multi-version-aware.
- `pkg/bundles/bundleclientv1`
The client used to speak to v1 Bundle binaries.
<!--
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.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on PR #56079:
URL: https://github.com/apache/airflow/pull/56079#issuecomment-3333534297
> I happen to see different styles of logging through out -- fmt, log.x, hcLog, can we make those uniform, later?
Fmt should be removed, those were likely debug prints I left in.
log.x (using slog.Logger) vs hclog. No, sadly not. Or I don't think so. hclog is what the go-plugin module speaks, so I've attempted to bridge between slog and hclog. All "user" code should be using slog, as should almost all calls to loggers in our code. Are there any places where I use hclog directly?
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2378802480
##########
go-sdk/pkg/bundles/shared/interface.go:
##########
@@ -0,0 +1,31 @@
+// Licensed to the Apache Software Foundation (ASF) under one
Review Comment:
I guess we can yeah -- there was more things in here, but I moved them elsewhere. Good idea.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on PR #56079:
URL: https://github.com/apache/airflow/pull/56079#issuecomment-3333841969
> > I happen to see different styles of logging through out -- fmt, log.x, hcLog, can we make those uniform, later?
>
> Fmt should be removed, those were likely debug prints I left in.
>
> log.x (using slog.Logger) vs hclog. No, sadly not. Or I don't think so. hclog is what the go-plugin module speaks, so I've attempted to bridge between slog and hclog. All "user" code should be using slog, as should almost all calls to loggers in our code. Are there any places where I use hclog directly?
Just checked, not used directly, we are good here.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2378819321
##########
go-sdk/pkg/worker/task.go:
##########
@@ -0,0 +1,158 @@
+// 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 worker
+
+import (
+ "context"
+ "fmt"
+ "log/slog"
+ "reflect"
+ "runtime"
+
+ "github.com/apache/airflow/go-sdk/sdk"
+)
+
+type (
+ taskFunction struct {
+ fn reflect.Value
+ fullName string
+ }
+ Task interface {
+ Execute(ctx context.Context, logger *slog.Logger) error
+ }
+)
+
+var _ Task = (*taskFunction)(nil)
+
+func NewTaskFunction(fn any) (Task, error) {
+ v := reflect.ValueOf(fn)
+ fullName := runtime.FuncForPC(v.Pointer()).Name()
+ f := &taskFunction{v, fullName}
+ return f, f.validateFn(v.Type())
+}
+
+func (f *taskFunction) Execute(ctx context.Context, logger *slog.Logger) error {
+ fnType := f.fn.Type()
+
+ reflectArgs := make([]reflect.Value, fnType.NumIn())
+ for i := range reflectArgs {
Review Comment:
Good comment, yes we could.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2378831142
##########
go-sdk/celery/app.go:
##########
@@ -80,27 +72,58 @@ func Run(ctx context.Context, config Config) error {
celery.WithBroker(broker),
)
+ tasks := &celeryTasksRunner{d}
+ fmt.Printf("%#v\n", viper.AllKeys())
Review Comment:
```suggestion
```
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb merged PR #56079:
URL: https://github.com/apache/airflow/pull/56079
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2377192020
##########
go-sdk/bundle/bundlev1/registry.go:
##########
@@ -89,6 +123,7 @@ func (r *registry) LookupTask(dagId, taskId string) (task Task, exists bool) {
r.RLock()
defer r.RUnlock()
+ fmt.Printf("tasksFuncMap: %#v\n", r.taskFuncMap)
Review Comment:
Er, yes. Good spot :)
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2378994069
##########
go-sdk/bundle/bundlev1/bundlev1server/doc.go:
##########
@@ -0,0 +1,25 @@
+// Licensed to the Apache Software Foundation (ASF) under one
Review Comment:
Updated, removed the `pkg/bundles/client` folder
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2378646161
##########
go-sdk/bundle/bundlev1/bundlev1server/plugin.go:
##########
@@ -0,0 +1,185 @@
+// 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 bundlev1server
+
+import (
+ "context"
+ "errors"
+ "log/slog"
+ "os"
+ "sync"
+
+ "github.com/google/uuid"
+ "github.com/hashicorp/go-hclog"
+ "github.com/hashicorp/go-plugin"
+ "github.com/spf13/viper"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
+
+ "github.com/apache/airflow/go-sdk/bundle/bundlev1"
+ proto "github.com/apache/airflow/go-sdk/internal/protov1"
+ "github.com/apache/airflow/go-sdk/pkg/api"
+ "github.com/apache/airflow/go-sdk/pkg/worker"
+)
+
+// BundleGRPCPlugin is an implementation of the github.com/hashicorp/go-plugin#Plugin and
+// github.com/hashicorp/go-plugin#GRPCPlugin interfaces, indicating how to
+// serve [bundlev1.BundleProvider] as gRPC plugins for go-plugin.
+type BundleGRPCPlugin struct {
+ plugin.NetRPCUnsupportedPlugin
+ Factory func() bundlev1.BundleProvider
+}
+
+// Type assetion -- it must be a grpc plugin
+var _ plugin.GRPCPlugin = (*BundleGRPCPlugin)(nil)
+
+// Type assetion -- it must be a rpc plugin (even if it just returns errors)
Review Comment:
```suggestion
// Type assertion -- it must be a rpc plugin (even if it just returns errors)
```
##########
go-sdk/bundle/bundlev1/bundlev1server/plugin.go:
##########
@@ -0,0 +1,185 @@
+// 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 bundlev1server
+
+import (
+ "context"
+ "errors"
+ "log/slog"
+ "os"
+ "sync"
+
+ "github.com/google/uuid"
+ "github.com/hashicorp/go-hclog"
+ "github.com/hashicorp/go-plugin"
+ "github.com/spf13/viper"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
+
+ "github.com/apache/airflow/go-sdk/bundle/bundlev1"
+ proto "github.com/apache/airflow/go-sdk/internal/protov1"
+ "github.com/apache/airflow/go-sdk/pkg/api"
+ "github.com/apache/airflow/go-sdk/pkg/worker"
+)
+
+// BundleGRPCPlugin is an implementation of the github.com/hashicorp/go-plugin#Plugin and
+// github.com/hashicorp/go-plugin#GRPCPlugin interfaces, indicating how to
+// serve [bundlev1.BundleProvider] as gRPC plugins for go-plugin.
+type BundleGRPCPlugin struct {
+ plugin.NetRPCUnsupportedPlugin
+ Factory func() bundlev1.BundleProvider
+}
+
+// Type assetion -- it must be a grpc plugin
Review Comment:
```suggestion
// Type assertion -- it must be a grpc plugin
```
##########
go-sdk/pkg/bundles/shared/interface.go:
##########
@@ -0,0 +1,31 @@
+// 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 shared contains shared data between the host and plugins.
Review Comment:
```suggestion
// Package shared contains shared data between the worker and plugins.
```
##########
go-sdk/bundle/bundlev1/bundlev1server/plugin.go:
##########
@@ -0,0 +1,185 @@
+// 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 bundlev1server
+
+import (
+ "context"
+ "errors"
+ "log/slog"
+ "os"
+ "sync"
+
+ "github.com/google/uuid"
+ "github.com/hashicorp/go-hclog"
+ "github.com/hashicorp/go-plugin"
+ "github.com/spf13/viper"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
+
+ "github.com/apache/airflow/go-sdk/bundle/bundlev1"
+ proto "github.com/apache/airflow/go-sdk/internal/protov1"
+ "github.com/apache/airflow/go-sdk/pkg/api"
+ "github.com/apache/airflow/go-sdk/pkg/worker"
+)
+
+// BundleGRPCPlugin is an implementation of the github.com/hashicorp/go-plugin#Plugin and
+// github.com/hashicorp/go-plugin#GRPCPlugin interfaces, indicating how to
+// serve [bundlev1.BundleProvider] as gRPC plugins for go-plugin.
+type BundleGRPCPlugin struct {
+ plugin.NetRPCUnsupportedPlugin
+ Factory func() bundlev1.BundleProvider
+}
+
+// Type assetion -- it must be a grpc plugin
+var _ plugin.GRPCPlugin = (*BundleGRPCPlugin)(nil)
+
+// Type assetion -- it must be a rpc plugin (even if it just returns errors)
+var _ plugin.Plugin = (*BundleGRPCPlugin)(nil)
+
+func (p *BundleGRPCPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
+ impl := p.Factory()
+ proto.RegisterDagBundleServer(s, &server{
+ Impl: impl,
+ })
+ return nil
+}
+
+// GRPCClient implements plugin.GRPCPlugin.
+func (p *BundleGRPCPlugin) GRPCClient(
+ ctx context.Context,
+ broker *plugin.GRPCBroker,
+ c *grpc.ClientConn,
+) (interface{}, error) {
+ return nil, errors.New("bundlev1server only implements gRPC servers")
+}
+
+type server struct {
+ sync.RWMutex
+ proto.UnimplementedDagBundleServer
+ Impl bundlev1.BundleProvider
+
+ bundle bundlev1.Bundle
+}
+
+func (g *server) GetMetadata(
+ ctx context.Context,
+ _ *proto.GetMetadata_Request,
+) (*proto.GetMetadata_Response, error) {
+ ver := g.Impl.GetBundleVersion()
+
+ info := proto.BundleInfo_builder{
+ Name: &ver.Name,
+ Version: ver.Version,
+ }.Build()
+ resp := proto.GetMetadata_Response_builder{Bundle: info}.Build()
+
+ return resp, nil
+}
+
+func (g *server) getCachedBundle(_ context.Context) bundlev1.Bundle {
+ g.RWMutex.RLock()
+ defer g.RWMutex.RUnlock()
+ return g.bundle
+}
+
+func (g *server) getBundle(ctx context.Context) (bundlev1.Bundle, error) {
+ if b := g.getCachedBundle(ctx); b != nil {
+ return b, nil
+ }
+
+ g.RWMutex.Lock()
+ defer g.RWMutex.Unlock()
+
+ reg := bundlev1.New()
+ err := g.Impl.RegisterDags(reg)
+ if err != nil {
+ return nil, err
+ }
+ g.bundle = reg
+
+ return g.bundle, err
+}
+
+func (g *server) Execute(
+ ctx context.Context,
+ req *proto.Execute_Request,
+) (*proto.Execute_Response, error) {
+ if executeTask := req.GetTask(); executeTask != nil {
+ return nil, g.executeTask(ctx, executeTask)
+ }
+
+ which := req.WhichWorkload().String()
+ return nil, status.Errorf(codes.Unimplemented, "Unimplmeneted workload %q", which)
+}
+
+func (g *server) executeTask(ctx context.Context, executeTask *proto.ExecuteTaskWorkload) error {
+ ti := executeTask.GetTi()
+ bundle := executeTask.GetBundleInfo()
+ id, err := uuid.Parse(ti.GetId().GetValue())
+ if err != nil {
+ return status.Errorf(codes.InvalidArgument, "unable to parse UUID: %s", err)
+ }
+ workload := api.ExecuteTaskWorkload{
+ Token: executeTask.GetToken(),
+ TI: bundlev1.TaskInstance{
+ DagId: ti.GetDagId(),
+ Id: id,
+ RunId: ti.GetRunId(),
+ TaskId: ti.GetTaskId(),
+ TryNumber: int(ti.GetTryNumber()),
+ // TODO: support otel context carrier
+ // ContextCarrier: (map[string]any)(ti.GetOtelContext()),
+ },
+ BundleInfo: bundlev1.BundleInfo{
+ Name: bundle.GetName(),
+ },
+ }
+
+ if ti.HasMapIndex() {
+ idx := int(ti.GetMapIndex())
+ workload.TI.MapIndex = &idx
+ }
+
+ if bundle.HasVersion() {
+ ver := bundle.GetVersion()
+ workload.BundleInfo.Version = &ver
+ }
+
+ if executeTask.HasLogPath() {
+ path := executeTask.GetLogPath()
+ workload.LogPath = &path
+ }
+
+ dagBundle, err := g.getBundle(ctx)
+ if err != nil {
+ return status.Errorf(codes.NotFound, "dag bundle not found: %#v", workload.BundleInfo)
+ }
+
+ w := worker.NewWithBundle(dagBundle, slog.Default())
+
+ hclog.Default().
+ Warn("Does viper work", "url", viper.GetString("execution-api-url"), "env", os.Getenv("AIRFLOW__EXECUTION_API_URL"))
+ w, err = w.WithServer(viper.GetString("execution-api-url"))
+ if err != nil {
+ slog.ErrorContext(ctx, "Error setting ExecutionAPI sxerver for worker", "err", err)
Review Comment:
```suggestion
slog.ErrorContext(ctx, "Error setting ExecutionAPI server for worker", "err", err)
```
##########
go-sdk/celery/app.go:
##########
@@ -80,27 +72,58 @@ func Run(ctx context.Context, config Config) error {
celery.WithBroker(broker),
)
+ tasks := &celeryTasksRunner{d}
+ fmt.Printf("%#v\n", viper.AllKeys())
+
for _, queue := range config.Queues {
app.Register(
"execute_workload",
queue,
func(ctx context.Context, p *celery.TaskParam) error {
- p.NameArgs("payload")
- payload := p.MustString("payload")
-
- var workload api.ExecuteTaskWorkload
- if err := json.Unmarshal([]byte(payload), &workload); err != nil {
- return err
+ err := tasks.ExecuteWorkloadTask(ctx, p)
+ if err != nil {
+ slog.ErrorContext(ctx, "Celery Task failed", "err", err)
}
- return worker.ExecuteTaskWorkload(ctx, workload)
+ return err
},
)
}
slog.Info("waiting for tasks", "queues", config.Queues)
- err = app.Run(ctx)
+ return app.Run(ctx)
+}
+
+func (state *celeryTasksRunner) ExecuteWorkloadTask(
+ ctx context.Context,
+ p *celery.TaskParam,
+) error {
+ p.NameArgs("payload")
+ payload := p.MustString("payload")
+
+ var workload bundlev1.ExecuteTaskWorkload
+ if err := json.Unmarshal([]byte(payload), &workload); err != nil {
+ return err
+ }
+
+ client, err := state.ClientForBundle(workload.BundleInfo.Name, workload.BundleInfo.Version)
+ if err != nil {
+ // TODO: This Should write something to the log file
+ return err
+ }
+ // TODO: Don't kill the backend process here, but instead kill it after a bit of idleness. See if we can
+ // reuse the process for multiple tasks too
+ defer client.Kill()
Review Comment:
Maybe use context with timeout to kill it?
```
ctx, cancel := context.WithTimeout(context.Background(), <some timeout>)
defer cancel()
go func() {
<-ctx.Done()
client.Kill()
}()
```
##########
go-sdk/bundle/bundlev1/bundlev1server/doc.go:
##########
@@ -0,0 +1,25 @@
+// Licensed to the Apache Software Foundation (ASF) under one
Review Comment:
Doesn't have to be done but bundleserver can live in `pkg/bundles`? Or is there a reason for new top level directory for server and client for bundles?
##########
go-sdk/pkg/bundles/shared/interface.go:
##########
@@ -0,0 +1,31 @@
+// 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 shared contains shared data between the host and plugins.
+package shared
+
+import (
+ "github.com/hashicorp/go-plugin"
+)
+
+// Handshake is a common handshake that is shared by plugin and host.
Review Comment:
```suggestion
// Handshake is a common handshake that is shared by plugin and worker.
```
##########
go-sdk/pkg/bundles/shared/interface.go:
##########
@@ -0,0 +1,31 @@
+// Licensed to the Apache Software Foundation (ASF) under one
Review Comment:
Again a nit, but could we call this `handshake.go`?
##########
go-sdk/pkg/bundles/shared/discovery.go:
##########
@@ -0,0 +1,252 @@
+// 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 shared
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "log/slog"
+ "os"
+ "os/exec"
+ "path/filepath"
+
+ "github.com/hashicorp/go-hclog"
+ "github.com/hashicorp/go-plugin"
+ "github.com/spf13/viper"
+
+ "github.com/apache/airflow/go-sdk/bundle/bundlev1"
+ "github.com/apache/airflow/go-sdk/pkg/bundles/bundleclientv1"
+ "github.com/apache/airflow/go-sdk/pkg/logging/shclog"
+)
+
+// Discovery handles finding and loading DAG bundles
+type Discovery struct {
+ logger *slog.Logger
+ hcLogger hclog.Logger
+ bundlesFolder string
+ bundles map[string]map[string]string // bundle_name -> version -> path to binary
+}
+
+var BundleNotFound = errors.New("")
+
+// NewDiscovery creates a an object responsible for finding and looking for possible DAG bundle binaries
+func NewDiscovery(bundlesFolder string, logger *slog.Logger) *Discovery {
+ if logger == nil {
+ logger = slog.Default()
+ }
+
+ return &Discovery{
+ logger: logger,
+ bundlesFolder: bundlesFolder,
+ bundles: make(map[string]map[string]string),
+ hcLogger: shclog.New(logger),
+ }
+}
+
+func (d *Discovery) versionsForNamedBundle(name string) map[string]string {
+ versions, exists := d.bundles[name]
+ if exists {
+ return versions
+ }
+
+ // We couldn't find the specific named bundle
+
+ // First we see if a "default" was configured
+ versions, exists = d.bundles[viper.GetString("bundles.default_bundle")]
+ if exists {
+ return versions
+ }
+
+ // Else we see if there is exactly one bundle name registered, and if so we return that
+ if len(d.bundles) == 1 {
+ exists = true
+ for key, versions := range d.bundles {
+ // Just pull the first value out
+ d.logger.Debug(
+ "Using sole bundle as fallback bundle",
+ "bundle",
+ name,
+ "fallback_bundle",
+ key,
+ )
+ return versions
+ }
+ }
+ return nil
+}
+
+func (d *Discovery) ClientForBundle(name string, version *string) (*plugin.Client, error) {
+ var key string
+ if version != nil {
+ key = *version
+ }
+
+ versions := d.versionsForNamedBundle(name)
+ if versions == nil {
+ // We couldn't find the specific named bundle
+ return nil, fmt.Errorf(
+ "%wno dag bundle named %q found (and no fallback suitable)",
+ BundleNotFound,
+ name,
+ )
+ }
+
+ cmd, exists := versions[key]
+ if !exists {
+ // We couldn't find the specific version, but lets see if we have just a single version and use that in
+ // its place
+ if key == "" && len(versions) == 1 {
+ exists = true
+ for key, cmd = range versions {
+ // Just pull the first value out
+ d.logger.Info(
+ "Unable to find unversioned bundle as requested, using only version as fallback",
+ "bundle",
+ name,
+ "fallback_version",
+ key,
+ )
+ break
+ }
+ }
+ }
+
+ if !exists {
+ if key == "" {
+ key = "<unversioned>"
+ }
+ return nil, fmt.Errorf("%wno version %q found for dag bundle %q", BundleNotFound, key, name)
+ }
+ return d.makeClient(cmd), nil
+}
+
+func (d *Discovery) DiscoverBundles(ctx context.Context) error {
+ // Find all files in the bundles directory
+ files, err := filepath.Glob(filepath.Join(d.bundlesFolder, "*"))
+ if err != nil {
+ return fmt.Errorf("failed to read bundles directory: %w", err)
+ }
+
+ self, err := os.Executable()
+ if err != nil {
+ self = ""
+ }
+
+ for _, file := range files {
+ if ctx.Err() != nil {
+ // Check if we are done.
+ return ctx.Err()
+ }
+
+ // Check if file is executable
+ if !isExecutable(file) {
+ continue
+ }
+
+ abs, err := filepath.Abs(file)
+ if err != nil {
+ d.logger.Warn("Unable to load resolve file path", "file", file, "err", err)
+ continue
+ }
+ if self != "" && self == abs {
+ d.logger.Warn("Not trying to load ourselves as a plugin", "file", file)
+ continue
+ }
+
+ d.logger.Debug("Found potential bundle", slog.String("path", file))
+
+ // TODO: Use a sync.WaitGroup to parallelize running multiple procs without blowing concurrency up and fork-bombing
+ // the host
+ bundle, err := d.getBundleVersionInfo(file)
+ if err != nil {
+ d.logger.Warn("Unable to load BundleMetadata", "file", file, "err", err)
+ continue
Review Comment:
This would silently skip the bundle if lets say the binary has wrong permissions?
##########
go-sdk/celery/app.go:
##########
@@ -80,27 +72,58 @@ func Run(ctx context.Context, config Config) error {
celery.WithBroker(broker),
)
+ tasks := &celeryTasksRunner{d}
+ fmt.Printf("%#v\n", viper.AllKeys())
Review Comment:
Accidentally added?
##########
go-sdk/pkg/worker/task.go:
##########
@@ -0,0 +1,158 @@
+// 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 worker
+
+import (
+ "context"
+ "fmt"
+ "log/slog"
+ "reflect"
+ "runtime"
+
+ "github.com/apache/airflow/go-sdk/sdk"
+)
+
+type (
+ taskFunction struct {
+ fn reflect.Value
+ fullName string
+ }
+ Task interface {
+ Execute(ctx context.Context, logger *slog.Logger) error
+ }
+)
+
+var _ Task = (*taskFunction)(nil)
+
+func NewTaskFunction(fn any) (Task, error) {
+ v := reflect.ValueOf(fn)
+ fullName := runtime.FuncForPC(v.Pointer()).Name()
+ f := &taskFunction{v, fullName}
+ return f, f.validateFn(v.Type())
+}
+
+func (f *taskFunction) Execute(ctx context.Context, logger *slog.Logger) error {
+ fnType := f.fn.Type()
+
+ reflectArgs := make([]reflect.Value, fnType.NumIn())
+ for i := range reflectArgs {
Review Comment:
Not right now, but worth considering. We do reflection args validation during "execution" here, could we maybe do it during registration? (execution is a hot path)
##########
go-sdk/bundle/bundlev1/bundlev1server/doc.go:
##########
@@ -0,0 +1,25 @@
+// Licensed to the Apache Software Foundation (ASF) under one
Review Comment:
pkg/bundles/bundlev1/server/ <= server implementation
pkg/bundles/bundlev1/client/ <= client implementation
Maybe
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2378807931
##########
go-sdk/pkg/bundles/shared/discovery.go:
##########
@@ -0,0 +1,252 @@
+// 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 shared
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "log/slog"
+ "os"
+ "os/exec"
+ "path/filepath"
+
+ "github.com/hashicorp/go-hclog"
+ "github.com/hashicorp/go-plugin"
+ "github.com/spf13/viper"
+
+ "github.com/apache/airflow/go-sdk/bundle/bundlev1"
+ "github.com/apache/airflow/go-sdk/pkg/bundles/bundleclientv1"
+ "github.com/apache/airflow/go-sdk/pkg/logging/shclog"
+)
+
+// Discovery handles finding and loading DAG bundles
+type Discovery struct {
+ logger *slog.Logger
+ hcLogger hclog.Logger
+ bundlesFolder string
+ bundles map[string]map[string]string // bundle_name -> version -> path to binary
+}
+
+var BundleNotFound = errors.New("")
+
+// NewDiscovery creates a an object responsible for finding and looking for possible DAG bundle binaries
+func NewDiscovery(bundlesFolder string, logger *slog.Logger) *Discovery {
+ if logger == nil {
+ logger = slog.Default()
+ }
+
+ return &Discovery{
+ logger: logger,
+ bundlesFolder: bundlesFolder,
+ bundles: make(map[string]map[string]string),
+ hcLogger: shclog.New(logger),
+ }
+}
+
+func (d *Discovery) versionsForNamedBundle(name string) map[string]string {
+ versions, exists := d.bundles[name]
+ if exists {
+ return versions
+ }
+
+ // We couldn't find the specific named bundle
+
+ // First we see if a "default" was configured
+ versions, exists = d.bundles[viper.GetString("bundles.default_bundle")]
+ if exists {
+ return versions
+ }
+
+ // Else we see if there is exactly one bundle name registered, and if so we return that
+ if len(d.bundles) == 1 {
+ exists = true
+ for key, versions := range d.bundles {
+ // Just pull the first value out
+ d.logger.Debug(
+ "Using sole bundle as fallback bundle",
+ "bundle",
+ name,
+ "fallback_bundle",
+ key,
+ )
+ return versions
+ }
+ }
+ return nil
+}
+
+func (d *Discovery) ClientForBundle(name string, version *string) (*plugin.Client, error) {
+ var key string
+ if version != nil {
+ key = *version
+ }
+
+ versions := d.versionsForNamedBundle(name)
+ if versions == nil {
+ // We couldn't find the specific named bundle
+ return nil, fmt.Errorf(
+ "%wno dag bundle named %q found (and no fallback suitable)",
+ BundleNotFound,
+ name,
+ )
+ }
+
+ cmd, exists := versions[key]
+ if !exists {
+ // We couldn't find the specific version, but lets see if we have just a single version and use that in
+ // its place
+ if key == "" && len(versions) == 1 {
+ exists = true
+ for key, cmd = range versions {
+ // Just pull the first value out
+ d.logger.Info(
+ "Unable to find unversioned bundle as requested, using only version as fallback",
+ "bundle",
+ name,
+ "fallback_version",
+ key,
+ )
+ break
+ }
+ }
+ }
+
+ if !exists {
+ if key == "" {
+ key = "<unversioned>"
+ }
+ return nil, fmt.Errorf("%wno version %q found for dag bundle %q", BundleNotFound, key, name)
+ }
+ return d.makeClient(cmd), nil
+}
+
+func (d *Discovery) DiscoverBundles(ctx context.Context) error {
+ // Find all files in the bundles directory
+ files, err := filepath.Glob(filepath.Join(d.bundlesFolder, "*"))
+ if err != nil {
+ return fmt.Errorf("failed to read bundles directory: %w", err)
+ }
+
+ self, err := os.Executable()
+ if err != nil {
+ self = ""
+ }
+
+ for _, file := range files {
+ if ctx.Err() != nil {
+ // Check if we are done.
+ return ctx.Err()
+ }
+
+ // Check if file is executable
+ if !isExecutable(file) {
+ continue
+ }
+
+ abs, err := filepath.Abs(file)
+ if err != nil {
+ d.logger.Warn("Unable to load resolve file path", "file", file, "err", err)
+ continue
+ }
+ if self != "" && self == abs {
+ d.logger.Warn("Not trying to load ourselves as a plugin", "file", file)
+ continue
+ }
+
+ d.logger.Debug("Found potential bundle", slog.String("path", file))
+
+ // TODO: Use a sync.WaitGroup to parallelize running multiple procs without blowing concurrency up and fork-bombing
+ // the host
+ bundle, err := d.getBundleVersionInfo(file)
+ if err != nil {
+ d.logger.Warn("Unable to load BundleMetadata", "file", file, "err", err)
+ continue
Review Comment:
This line wouldn't, but L158
```go
if !isExecutable(file) {
continue
}
```
would. We could add a debug log in there
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2378800560
##########
go-sdk/bundle/bundlev1/bundlev1server/doc.go:
##########
@@ -0,0 +1,25 @@
+// Licensed to the Apache Software Foundation (ASF) under one
Review Comment:
I didn't make this clear I guess, but the intent is:
`bundles/` <-- things users will use in writing their tasks/bundles
`pkg/` <-- anything "quasi-internal" i.e. I don't expect or want users to use it directly, but I also don't believe in putting things truly private (which `internal/` is)
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2378809560
##########
go-sdk/celery/app.go:
##########
@@ -80,27 +72,58 @@ func Run(ctx context.Context, config Config) error {
celery.WithBroker(broker),
)
+ tasks := &celeryTasksRunner{d}
+ fmt.Printf("%#v\n", viper.AllKeys())
Review Comment:
Yup!
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2378813522
##########
go-sdk/celery/app.go:
##########
@@ -80,27 +72,58 @@ func Run(ctx context.Context, config Config) error {
celery.WithBroker(broker),
)
+ tasks := &celeryTasksRunner{d}
+ fmt.Printf("%#v\n", viper.AllKeys())
+
for _, queue := range config.Queues {
app.Register(
"execute_workload",
queue,
func(ctx context.Context, p *celery.TaskParam) error {
- p.NameArgs("payload")
- payload := p.MustString("payload")
-
- var workload api.ExecuteTaskWorkload
- if err := json.Unmarshal([]byte(payload), &workload); err != nil {
- return err
+ err := tasks.ExecuteWorkloadTask(ctx, p)
+ if err != nil {
+ slog.ErrorContext(ctx, "Celery Task failed", "err", err)
}
- return worker.ExecuteTaskWorkload(ctx, workload)
+ return err
},
)
}
slog.Info("waiting for tasks", "queues", config.Queues)
- err = app.Run(ctx)
+ return app.Run(ctx)
+}
+
+func (state *celeryTasksRunner) ExecuteWorkloadTask(
+ ctx context.Context,
+ p *celery.TaskParam,
+) error {
+ p.NameArgs("payload")
+ payload := p.MustString("payload")
+
+ var workload bundlev1.ExecuteTaskWorkload
+ if err := json.Unmarshal([]byte(payload), &workload); err != nil {
+ return err
+ }
+
+ client, err := state.ClientForBundle(workload.BundleInfo.Name, workload.BundleInfo.Version)
+ if err != nil {
+ // TODO: This Should write something to the log file
+ return err
+ }
+ // TODO: Don't kill the backend process here, but instead kill it after a bit of idleness. See if we can
+ // reuse the process for multiple tasks too
+ defer client.Kill()
Review Comment:
This comment is more about reusing the plugin process (i.e. you can absolutely use one plugin process to run multiple tasks concurrently.
So it would be a whole separate "spawn the process, run things, then once that plugin bundle has been idle for a configured time, say 1 or 5 minutes, _then_ kill it". So I don't think a timeout would help, as that would possibly kill it early if it's busy.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2377178424
##########
go-sdk/celery/commands/run.go:
##########
@@ -45,5 +54,12 @@ func init() {
runCmd.Flags().
StringP("execution-api-url", "e", "http://localhost:8080/execution/", "Execution API to connect to")
runCmd.Flags().StringSliceP("queues", "q", []string{"default"}, "Celery queues to listen on")
+ runCmd.Flags().
+ StringP("bundles-folder", "", "", "Folder containing the compiled dag bundle executables")
+
runCmd.MarkFlagRequired("broker-address")
+ runCmd.MarkFlagRequired("bundles-folder")
+ runCmd.Flags().
+ SetAnnotation("bundles-folder", "viper-mapping", []string{"celery.broker-address"})
Review Comment:
```suggestion
runCmd.Flags().
SetAnnotation("broker-address", "viper-mapping", []string{"celery.broker-address"})
```
##########
go-sdk/bundle/bundlev1/bundlev1server/plugin.go:
##########
@@ -0,0 +1,185 @@
+// 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 bundlev1server
+
+import (
+ "context"
+ "errors"
+ "log/slog"
+ "os"
+ "sync"
+
+ "github.com/google/uuid"
+ "github.com/hashicorp/go-hclog"
+ "github.com/hashicorp/go-plugin"
+ "github.com/spf13/viper"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
+
+ "github.com/apache/airflow/go-sdk/bundle/bundlev1"
+ proto "github.com/apache/airflow/go-sdk/internal/protov1"
+ "github.com/apache/airflow/go-sdk/pkg/api"
+ "github.com/apache/airflow/go-sdk/pkg/worker"
+)
+
+// BundleGRPCPlugin is an implementation of the github.com/hashicorp/go-plugin#Plugin and
+// github.com/hashicorp/go-plugin#GRPCPlugin interfaces, indicating how to
+// serve [bundlev1.BundleProvider] as gRPC plugins for go-plugin.
+type BundleGRPCPlugin struct {
+ plugin.NetRPCUnsupportedPlugin
+ Factory func() bundlev1.BundleProvider
+}
+
+// Type assetion -- it must be a grpc plugin
+var _ plugin.GRPCPlugin = (*BundleGRPCPlugin)(nil)
+
+// Type assetion -- it must be a rpc plugin (even if it just returns errors)
+var _ plugin.Plugin = (*BundleGRPCPlugin)(nil)
+
+func (p *BundleGRPCPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
+ impl := p.Factory()
+ proto.RegisterDagBundleServer(s, &server{
+ Impl: impl,
+ })
+ return nil
+}
+
+// GRPCClient implements plugin.GRPCPlugin.
+func (p *BundleGRPCPlugin) GRPCClient(
+ ctx context.Context,
+ broker *plugin.GRPCBroker,
+ c *grpc.ClientConn,
+) (interface{}, error) {
+ return nil, errors.New("bundlev1server only implements gRPC servers")
+}
+
+type server struct {
+ sync.RWMutex
+ proto.UnimplementedDagBundleServer
+ Impl bundlev1.BundleProvider
+
+ bundle bundlev1.Bundle
+}
+
+func (g *server) GetMetadata(
+ ctx context.Context,
+ _ *proto.GetMetadata_Request,
+) (*proto.GetMetadata_Response, error) {
+ ver := g.Impl.GetBundleVersion()
+
+ info := proto.BundleInfo_builder{
+ Name: &ver.Name,
+ Version: ver.Version,
+ }.Build()
+ resp := proto.GetMetadata_Response_builder{Bundle: info}.Build()
+
+ return resp, nil
+}
+
+func (g *server) getCachedBundle(_ context.Context) bundlev1.Bundle {
+ g.RWMutex.RLock()
+ defer g.RWMutex.RUnlock()
+ return g.bundle
+}
+
+func (g *server) getBundle(ctx context.Context) (bundlev1.Bundle, error) {
+ if b := g.getCachedBundle(ctx); b != nil {
+ return b, nil
+ }
+
+ g.RWMutex.Lock()
+ defer g.RWMutex.Unlock()
+
+ reg := bundlev1.New()
+ err := g.Impl.RegisterDags(reg)
+ if err != nil {
+ return nil, err
+ }
+ g.bundle = reg
+
+ return g.bundle, err
+}
+
+func (g *server) Execute(
+ ctx context.Context,
+ req *proto.Execute_Request,
+) (*proto.Execute_Response, error) {
+ if executeTask := req.GetTask(); executeTask != nil {
+ return nil, g.executeTask(ctx, executeTask)
+ }
+
+ which := req.WhichWorkload().String()
+ return nil, status.Errorf(codes.Unimplemented, "Unimplmeneted workload %q", which)
+}
+
+func (g *server) executeTask(ctx context.Context, executeTask *proto.ExecuteTaskWorkload) error {
+ ti := executeTask.GetTi()
+ bundle := executeTask.GetBundleInfo()
+ id, err := uuid.Parse(ti.GetId().GetValue())
+ if err != nil {
+ return status.Errorf(codes.InvalidArgument, "unable to parse UUID: %s", err)
+ }
+ workload := api.ExecuteTaskWorkload{
+ Token: executeTask.GetToken(),
+ TI: bundlev1.TaskInstance{
+ DagId: ti.GetDagId(),
+ Id: id,
+ RunId: ti.GetRunId(),
+ TaskId: ti.GetTaskId(),
+ TryNumber: int(ti.GetTryNumber()),
+ // TODO: support otel context carrier
+ // ContextCarrier: (map[string]any)(ti.GetOtelContext()),
+ },
+ BundleInfo: bundlev1.BundleInfo{
+ Name: bundle.GetName(),
+ },
+ }
+
+ if ti.HasMapIndex() {
+ idx := int(ti.GetMapIndex())
+ workload.TI.MapIndex = &idx
+ }
+
+ if bundle.HasVersion() {
+ ver := bundle.GetVersion()
+ workload.BundleInfo.Version = &ver
+ }
+
+ if executeTask.HasLogPath() {
+ path := executeTask.GetLogPath()
+ workload.LogPath = &path
+ }
+
+ dagBundle, err := g.getBundle(ctx)
+ if err != nil {
+ return status.Errorf(codes.NotFound, "dag bundle found: %#v", workload.BundleInfo)
+ }
Review Comment:
```suggestion
if err != nil {
return status.Errorf(codes.NotFound, "dag bundle NOT found: %#v", workload.BundleInfo)
}
```
##########
go-sdk/bundle/bundlev1/registry.go:
##########
@@ -89,6 +123,7 @@ func (r *registry) LookupTask(dagId, taskId string) (task Task, exists bool) {
r.RLock()
defer r.RUnlock()
+ fmt.Printf("tasksFuncMap: %#v\n", r.taskFuncMap)
Review Comment:
Assuming this is for debugging?
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2377176206
##########
go-sdk/bundle/bundlev1/bundlev1server/plugin.go:
##########
@@ -0,0 +1,185 @@
+// 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 bundlev1server
+
+import (
+ "context"
+ "errors"
+ "log/slog"
+ "os"
+ "sync"
+
+ "github.com/google/uuid"
+ "github.com/hashicorp/go-hclog"
+ "github.com/hashicorp/go-plugin"
+ "github.com/spf13/viper"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
+
+ "github.com/apache/airflow/go-sdk/bundle/bundlev1"
+ proto "github.com/apache/airflow/go-sdk/internal/protov1"
+ "github.com/apache/airflow/go-sdk/pkg/api"
+ "github.com/apache/airflow/go-sdk/pkg/worker"
+)
+
+// BundleGRPCPlugin is an implementation of the github.com/hashicorp/go-plugin#Plugin and
+// github.com/hashicorp/go-plugin#GRPCPlugin interfaces, indicating how to
+// serve [bundlev1.BundleProvider] as gRPC plugins for go-plugin.
+type BundleGRPCPlugin struct {
+ plugin.NetRPCUnsupportedPlugin
+ Factory func() bundlev1.BundleProvider
+}
+
+// Type assetion -- it must be a grpc plugin
+var _ plugin.GRPCPlugin = (*BundleGRPCPlugin)(nil)
+
+// Type assetion -- it must be a rpc plugin (even if it just returns errors)
+var _ plugin.Plugin = (*BundleGRPCPlugin)(nil)
+
+func (p *BundleGRPCPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
+ impl := p.Factory()
+ proto.RegisterDagBundleServer(s, &server{
+ Impl: impl,
+ })
+ return nil
+}
+
+// GRPCClient implements plugin.GRPCPlugin.
+func (p *BundleGRPCPlugin) GRPCClient(
+ ctx context.Context,
+ broker *plugin.GRPCBroker,
+ c *grpc.ClientConn,
+) (interface{}, error) {
+ return nil, errors.New("bundlev1server only implements gRPC servers")
+}
+
+type server struct {
+ sync.RWMutex
+ proto.UnimplementedDagBundleServer
+ Impl bundlev1.BundleProvider
+
+ bundle bundlev1.Bundle
+}
+
+func (g *server) GetMetadata(
+ ctx context.Context,
+ _ *proto.GetMetadata_Request,
+) (*proto.GetMetadata_Response, error) {
+ ver := g.Impl.GetBundleVersion()
+
+ info := proto.BundleInfo_builder{
+ Name: &ver.Name,
+ Version: ver.Version,
+ }.Build()
+ resp := proto.GetMetadata_Response_builder{Bundle: info}.Build()
+
+ return resp, nil
+}
+
+func (g *server) getCachedBundle(_ context.Context) bundlev1.Bundle {
+ g.RWMutex.RLock()
+ defer g.RWMutex.RUnlock()
+ return g.bundle
+}
+
+func (g *server) getBundle(ctx context.Context) (bundlev1.Bundle, error) {
+ if b := g.getCachedBundle(ctx); b != nil {
+ return b, nil
+ }
+
+ g.RWMutex.Lock()
+ defer g.RWMutex.Unlock()
+
+ reg := bundlev1.New()
+ err := g.Impl.RegisterDags(reg)
+ if err != nil {
+ return nil, err
+ }
+ g.bundle = reg
+
+ return g.bundle, err
+}
+
+func (g *server) Execute(
+ ctx context.Context,
+ req *proto.Execute_Request,
+) (*proto.Execute_Response, error) {
+ if executeTask := req.GetTask(); executeTask != nil {
+ return nil, g.executeTask(ctx, executeTask)
+ }
+
+ which := req.WhichWorkload().String()
+ return nil, status.Errorf(codes.Unimplemented, "Unimplmeneted workload %q", which)
+}
+
+func (g *server) executeTask(ctx context.Context, executeTask *proto.ExecuteTaskWorkload) error {
+ ti := executeTask.GetTi()
+ bundle := executeTask.GetBundleInfo()
+ id, err := uuid.Parse(ti.GetId().GetValue())
+ if err != nil {
+ return status.Errorf(codes.InvalidArgument, "unable to parse UUID: %s", err)
+ }
+ workload := api.ExecuteTaskWorkload{
+ Token: executeTask.GetToken(),
+ TI: bundlev1.TaskInstance{
+ DagId: ti.GetDagId(),
+ Id: id,
+ RunId: ti.GetRunId(),
+ TaskId: ti.GetTaskId(),
+ TryNumber: int(ti.GetTryNumber()),
+ // TODO: support otel context carrier
+ // ContextCarrier: (map[string]any)(ti.GetOtelContext()),
+ },
+ BundleInfo: bundlev1.BundleInfo{
+ Name: bundle.GetName(),
+ },
+ }
+
+ if ti.HasMapIndex() {
+ idx := int(ti.GetMapIndex())
+ workload.TI.MapIndex = &idx
+ }
+
+ if bundle.HasVersion() {
+ ver := bundle.GetVersion()
+ workload.BundleInfo.Version = &ver
+ }
+
+ if executeTask.HasLogPath() {
+ path := executeTask.GetLogPath()
+ workload.LogPath = &path
+ }
+
+ dagBundle, err := g.getBundle(ctx)
+ if err != nil {
+ return status.Errorf(codes.NotFound, "dag bundle found: %#v", workload.BundleInfo)
+ }
Review Comment:
```suggestion
if err != nil {
return status.Errorf(codes.NotFound, "dag bundle not found: %#v", workload.BundleInfo)
}
```
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Separate out Go tasks and Go worker in to separate processes [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #56079:
URL: https://github.com/apache/airflow/pull/56079#discussion_r2377193925
##########
go-sdk/bundle/bundlev1/registry.go:
##########
@@ -89,6 +123,7 @@ func (r *registry) LookupTask(dagId, taskId string) (task Task, exists bool) {
r.RLock()
defer r.RUnlock()
+ fmt.Printf("tasksFuncMap: %#v\n", r.taskFuncMap)
Review Comment:
```suggestion
```
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org