-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[prism] Support AnyOf in Prism. #33705
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"log/slog" | ||
"os" | ||
"os/exec" | ||
"slices" | ||
"time" | ||
|
||
fnpb "github.com/apache/beam/sdks/v2/go/pkg/beam/model/fnexecution_v1" | ||
|
@@ -46,16 +47,26 @@ import ( | |
|
||
func runEnvironment(ctx context.Context, j *jobservices.Job, env string, wk *worker.W) error { | ||
logger := j.Logger.With(slog.String("envID", wk.Env)) | ||
// TODO fix broken abstraction. | ||
// We're starting a worker pool here, because that's the loopback environment. | ||
// It's sort of a mess, largely because of loopback, which has | ||
// a different flow from a provisioned docker container. | ||
e := j.Pipeline.GetComponents().GetEnvironments()[env] | ||
|
||
if e.GetUrn() == urns.EnvAnyOf { | ||
// We've been given a choice! | ||
ap := &pipepb.AnyOfEnvironmentPayload{} | ||
if err := (proto.UnmarshalOptions{}).Unmarshal(e.GetPayload(), ap); err != nil { | ||
logger.Error("unmarshaling any environment payload", "error", err) | ||
return err | ||
} | ||
e = selectAnyOfEnv(ap) | ||
logger.Info("AnyEnv resolved", "selectedUrn", e.GetUrn(), "worker", wk.ID) | ||
// Process the environment as normal. | ||
} | ||
|
||
switch e.GetUrn() { | ||
case urns.EnvExternal: | ||
ep := &pipepb.ExternalPayload{} | ||
if err := (proto.UnmarshalOptions{}).Unmarshal(e.GetPayload(), ep); err != nil { | ||
logger.Error("unmarshing external environment payload", "error", err) | ||
logger.Error("unmarshaling external environment payload", "error", err) | ||
return err | ||
} | ||
go func() { | ||
externalEnvironment(ctx, ep, wk) | ||
|
@@ -65,13 +76,15 @@ func runEnvironment(ctx context.Context, j *jobservices.Job, env string, wk *wor | |
case urns.EnvDocker: | ||
dp := &pipepb.DockerPayload{} | ||
if err := (proto.UnmarshalOptions{}).Unmarshal(e.GetPayload(), dp); err != nil { | ||
logger.Error("unmarshing docker environment payload", "error", err) | ||
logger.Error("unmarshaling docker environment payload", "error", err) | ||
return err | ||
} | ||
return dockerEnvironment(ctx, logger, dp, wk, j.ArtifactEndpoint()) | ||
case urns.EnvProcess: | ||
pp := &pipepb.ProcessPayload{} | ||
if err := (proto.UnmarshalOptions{}).Unmarshal(e.GetPayload(), pp); err != nil { | ||
logger.Error("unmarshing docker environment payload", "error", err) | ||
logger.Error("unmarshaling process environment payload", "error", err) | ||
return err | ||
} | ||
go func() { | ||
processEnvironment(ctx, pp, wk) | ||
|
@@ -83,6 +96,33 @@ func runEnvironment(ctx context.Context, j *jobservices.Job, env string, wk *wor | |
} | ||
} | ||
|
||
func selectAnyOfEnv(ap *pipepb.AnyOfEnvironmentPayload) *pipepb.Environment { | ||
// Prefer external, then process, then docker, unknown environments are 0. | ||
ranks := map[string]int{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't handle nested AnyOf. Do we care? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe we do. We could, but I think we shouldn't. I'd say it's to Beam's benefit to not encourage that sort of thing. |
||
urns.EnvDocker: 1, | ||
urns.EnvProcess: 5, | ||
urns.EnvExternal: 10, | ||
} | ||
|
||
envs := ap.GetEnvironments() | ||
|
||
slices.SortStableFunc(envs, func(a, b *pipepb.Environment) int { | ||
rankA := ranks[a.GetUrn()] | ||
rankB := ranks[b.GetUrn()] | ||
|
||
// Reverse the comparison so our favourite is at the front | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively we could give the ranks golf-style scoring, or pick the last one in the list below. If this is the most idiomatic I'm fine with that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reversal was partly to have an easier selection for the most choice environment: just the one at 0. I did consider the golf weighting but I'm also making use of the default zero value for anything in the map. So i biased for the higher weight being priority, vs risk a weird priority inversion if someone puts in negative numbers. I could avoid the negatives with a uint instead... As for the idiomatic question: it would be less idiomatic to return the "normal" compare number, then reverse the list. Neither really save lines of code (vs documentation). There is the argument that then the code is self documenting, but we don't really care about the order, just that we picked the "favourite". Finally, we can always change it later. |
||
switch { | ||
case rankA > rankB: | ||
return -1 // Usually "greater than" would be 1 | ||
case rankA < rankB: | ||
return 1 | ||
} | ||
return 0 | ||
}) | ||
// Pick our favourite. | ||
return envs[0] | ||
} | ||
|
||
func externalEnvironment(ctx context.Context, ep *pipepb.ExternalPayload, wk *worker.W) { | ||
conn, err := grpc.Dial(ep.GetEndpoint().GetUrl(), grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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 internal | ||
|
||
import ( | ||
"testing" | ||
|
||
pipepb "github.com/apache/beam/sdks/v2/go/pkg/beam/model/pipeline_v1" | ||
"github.com/apache/beam/sdks/v2/go/pkg/beam/runners/prism/internal/urns" | ||
) | ||
|
||
func TestSelectAnyOf(t *testing.T) { | ||
tests := []struct { | ||
name, want string | ||
wantTag string | ||
envs []*pipepb.Environment | ||
}{ | ||
{name: "singleDefault", want: urns.EnvDefault, envs: []*pipepb.Environment{{Urn: urns.EnvDefault}}}, | ||
{name: "singleDocker", want: urns.EnvDocker, envs: []*pipepb.Environment{{Urn: urns.EnvDocker}}}, | ||
{name: "singleProcess", want: urns.EnvProcess, envs: []*pipepb.Environment{{Urn: urns.EnvProcess}}}, | ||
{name: "singleExternal", want: urns.EnvExternal, envs: []*pipepb.Environment{{Urn: urns.EnvExternal}}}, | ||
{name: "multiplePickExternal_1", want: urns.EnvExternal, envs: []*pipepb.Environment{{Urn: urns.EnvExternal}, {Urn: urns.EnvDocker}, {Urn: urns.EnvProcess}}}, | ||
{name: "multiplePickExternal_2", want: urns.EnvExternal, envs: []*pipepb.Environment{{Urn: urns.EnvDocker}, {Urn: urns.EnvProcess}, {Urn: urns.EnvExternal}}}, | ||
{name: "multiplePickProcess", want: urns.EnvProcess, envs: []*pipepb.Environment{{Urn: urns.EnvDocker}, {Urn: urns.EnvProcess}}}, | ||
{name: "multiplePickDocker", want: urns.EnvDocker, envs: []*pipepb.Environment{{Urn: urns.EnvDefault}, {Urn: urns.EnvDocker}}}, | ||
{name: "multiplePickFirstExternal", want: urns.EnvExternal, wantTag: "first", envs: []*pipepb.Environment{{Urn: urns.EnvExternal, Payload: []byte("first")}, {Urn: urns.EnvExternal, Payload: []byte("second")}}}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
selected := selectAnyOfEnv(&pipepb.AnyOfEnvironmentPayload{Environments: test.envs}) | ||
if selected.GetUrn() != test.want { | ||
t.Errorf("expected %v, got %v", test.want, selected.GetUrn()) | ||
} | ||
if got, want := string(selected.GetPayload()), test.wantTag; got != want { | ||
t.Errorf("expected payload with tag %v, got %v", want, got) | ||
} | ||
}) | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this TODO just obsolete? (I'm not seeing a pool started below, changed or not.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's mostly referring to the "one environment, one worker" notion. The "pool" for example could be several "w orkers" all in the same environment.
If we ever decide to make prism distributed and scalable, then this code would need to change anyway, so the TODO is moot.