This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
External Plugin Service (grpc) (#330)
* Add fastapi plugin Signed-off-by: Kevin Su <[email protected]> * Add dummy plugin Signed-off-by: Kevin Su <[email protected]> * nit Signed-off-by: Kevin Su <[email protected]> * test Signed-off-by: Kevin Su <[email protected]> * test Signed-off-by: Kevin Su <[email protected]> * test Signed-off-by: Kevin Su <[email protected]> * test Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * grpc plugin Signed-off-by: Kevin Su <[email protected]> * updated idl version Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * add grpc plugin Signed-off-by: Kevin Su <[email protected]> * nit Signed-off-by: Kevin Su <[email protected]> * nit Signed-off-by: Kevin Su <[email protected]> * nit Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * update idl Signed-off-by: Kevin Su <[email protected]> * more tests Signed-off-by: Kevin Su <[email protected]> * lint Signed-off-by: Kevin Su <[email protected]> * write output Signed-off-by: Kevin Su <[email protected]> * remove prev state Signed-off-by: Kevin Su <[email protected]> * bump idl Signed-off-by: Kevin Su <[email protected]> * lint Signed-off-by: Kevin Su <[email protected]> * wip Signed-off-by: Kevin Su <[email protected]> * test Signed-off-by: Kevin Su <[email protected]> * update idl Signed-off-by: Kevin Su <[email protected]> * rename Signed-off-by: Kevin Su <[email protected]> * more test Signed-off-by: Kevin Su <[email protected]> * remove grpcTokenKey Signed-off-by: Kevin Su <[email protected]> * Add SupportedTaskTypes Signed-off-by: Kevin Su <[email protected]> * nit Signed-off-by: Kevin Su <[email protected]> * nit Signed-off-by: Kevin Su <[email protected]> * cache connection Signed-off-by: Kevin Su <[email protected]> * more tests Signed-off-by: Kevin Su <[email protected]> * fixes tests Signed-off-by: Kevin Su <[email protected]> * fixes tests Signed-off-by: Kevin Su <[email protected]> * more tests Signed-off-by: Kevin Su <[email protected]> * lint Signed-off-by: Kevin Su <[email protected]> * remove bigquery_query_job_task Signed-off-by: Kevin Su <[email protected]> * add bigquery_query_job_task Signed-off-by: Kevin Su <[email protected]> * set random value for SupportedTaskTypes Signed-off-by: Kevin Su <[email protected]> --------- Signed-off-by: Kevin Su <[email protected]>
- Loading branch information
Showing
7 changed files
with
628 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package grpc | ||
|
||
import ( | ||
"time" | ||
|
||
pluginsConfig "github.com/flyteorg/flyteplugins/go/tasks/config" | ||
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core" | ||
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/webapi" | ||
"github.com/flyteorg/flytestdlib/config" | ||
) | ||
|
||
var ( | ||
defaultConfig = Config{ | ||
WebAPI: webapi.PluginConfig{ | ||
ResourceQuotas: map[core.ResourceNamespace]int{ | ||
"default": 1000, | ||
}, | ||
ReadRateLimiter: webapi.RateLimiterConfig{ | ||
Burst: 100, | ||
QPS: 10, | ||
}, | ||
WriteRateLimiter: webapi.RateLimiterConfig{ | ||
Burst: 100, | ||
QPS: 10, | ||
}, | ||
Caching: webapi.CachingConfig{ | ||
Size: 500000, | ||
ResyncInterval: config.Duration{Duration: 30 * time.Second}, | ||
Workers: 10, | ||
MaxSystemFailures: 5, | ||
}, | ||
ResourceMeta: nil, | ||
}, | ||
ResourceConstraints: core.ResourceConstraintsSpec{ | ||
ProjectScopeResourceConstraint: &core.ResourceConstraint{ | ||
Value: 100, | ||
}, | ||
NamespaceScopeResourceConstraint: &core.ResourceConstraint{ | ||
Value: 50, | ||
}, | ||
}, | ||
DefaultGrpcEndpoint: "dns:///external-plugin-service.flyte.svc.cluster.local:80", | ||
SupportedTaskTypes: []string{"task_type_1", "task_type_2"}, | ||
} | ||
|
||
configSection = pluginsConfig.MustRegisterSubSection("external-plugin-service", &defaultConfig) | ||
) | ||
|
||
// Config is config for 'databricks' plugin | ||
type Config struct { | ||
// WebAPI defines config for the base WebAPI plugin | ||
WebAPI webapi.PluginConfig `json:"webApi" pflag:",Defines config for the base WebAPI plugin."` | ||
|
||
// ResourceConstraints defines resource constraints on how many executions to be created per project/overall at any given time | ||
ResourceConstraints core.ResourceConstraintsSpec `json:"resourceConstraints" pflag:"-,Defines resource constraints on how many executions to be created per project/overall at any given time."` | ||
|
||
DefaultGrpcEndpoint string `json:"defaultGrpcEndpoint" pflag:",The default grpc endpoint of external plugin service."` | ||
|
||
// Maps endpoint to their plugin handler. {TaskType: Endpoint} | ||
EndpointForTaskTypes map[string]string `json:"endpointForTaskTypes" pflag:"-,"` | ||
|
||
// SupportedTaskTypes is a list of task types that are supported by this plugin. | ||
SupportedTaskTypes []string `json:"supportedTaskTypes" pflag:"-,Defines a list of task types that are supported by this plugin."` | ||
} | ||
|
||
func GetConfig() *Config { | ||
return configSection.GetConfig().(*Config) | ||
} | ||
|
||
func SetConfig(cfg *Config) error { | ||
return configSection.SetConfig(cfg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package grpc | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetAndSetConfig(t *testing.T) { | ||
cfg := defaultConfig | ||
cfg.WebAPI.Caching.Workers = 1 | ||
cfg.WebAPI.Caching.ResyncInterval.Duration = 5 * time.Second | ||
err := SetConfig(&cfg) | ||
assert.NoError(t, err) | ||
assert.Equal(t, &cfg, GetConfig()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,268 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
flyteIdlCore "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core" | ||
pluginCore "github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core" | ||
pluginCoreMocks "github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core/mocks" | ||
ioMocks "github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/io/mocks" | ||
|
||
"github.com/flyteorg/flyteidl/clients/go/coreutils" | ||
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/plugins" | ||
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/service" | ||
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery" | ||
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core" | ||
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/webapi" | ||
"github.com/flyteorg/flyteplugins/tests" | ||
"github.com/flyteorg/flytestdlib/contextutils" | ||
"github.com/flyteorg/flytestdlib/promutils" | ||
"github.com/flyteorg/flytestdlib/promutils/labeled" | ||
"github.com/flyteorg/flytestdlib/storage" | ||
"github.com/flyteorg/flytestdlib/utils" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"google.golang.org/grpc" | ||
"k8s.io/apimachinery/pkg/util/rand" | ||
) | ||
|
||
type MockPlugin struct { | ||
Plugin | ||
} | ||
|
||
type MockClient struct { | ||
} | ||
|
||
func (m *MockClient) CreateTask(_ context.Context, _ *service.TaskCreateRequest, _ ...grpc.CallOption) (*service.TaskCreateResponse, error) { | ||
return &service.TaskCreateResponse{JobId: "job-id"}, nil | ||
} | ||
|
||
func (m *MockClient) GetTask(_ context.Context, _ *service.TaskGetRequest, _ ...grpc.CallOption) (*service.TaskGetResponse, error) { | ||
return &service.TaskGetResponse{State: service.State_SUCCEEDED, Outputs: &flyteIdlCore.LiteralMap{ | ||
Literals: map[string]*flyteIdlCore.Literal{ | ||
"arr": coreutils.MustMakeLiteral([]interface{}{[]interface{}{"a", "b"}, []interface{}{1, 2}}), | ||
}, | ||
}}, nil | ||
} | ||
|
||
func (m *MockClient) DeleteTask(_ context.Context, _ *service.TaskDeleteRequest, _ ...grpc.CallOption) (*service.TaskDeleteResponse, error) { | ||
return &service.TaskDeleteResponse{}, nil | ||
} | ||
|
||
func mockGetClientFunc(_ context.Context, _ string, _ map[string]*grpc.ClientConn) (service.ExternalPluginServiceClient, error) { | ||
return &MockClient{}, nil | ||
} | ||
|
||
func mockGetBadClientFunc(_ context.Context, _ string, _ map[string]*grpc.ClientConn) (service.ExternalPluginServiceClient, error) { | ||
return nil, fmt.Errorf("error") | ||
} | ||
|
||
func TestEndToEnd(t *testing.T) { | ||
iter := func(ctx context.Context, tCtx pluginCore.TaskExecutionContext) error { | ||
return nil | ||
} | ||
|
||
cfg := defaultConfig | ||
cfg.WebAPI.ResourceQuotas = map[core.ResourceNamespace]int{} | ||
cfg.WebAPI.Caching.Workers = 1 | ||
cfg.WebAPI.Caching.ResyncInterval.Duration = 5 * time.Second | ||
err := SetConfig(&cfg) | ||
assert.NoError(t, err) | ||
|
||
databricksConfDict := map[string]interface{}{ | ||
"name": "flytekit databricks plugin example", | ||
"new_cluster": map[string]string{ | ||
"spark_version": "11.0.x-scala2.12", | ||
"node_type_id": "r3.xlarge", | ||
"num_workers": "4", | ||
}, | ||
"timeout_seconds": 3600, | ||
"max_retries": 1, | ||
} | ||
databricksConfig, err := utils.MarshalObjToStruct(databricksConfDict) | ||
assert.NoError(t, err) | ||
sparkJob := plugins.SparkJob{DatabricksConf: databricksConfig, DatabricksToken: "token", SparkConf: map[string]string{"spark.driver.bindAddress": "127.0.0.1"}} | ||
st, err := utils.MarshalPbToStruct(&sparkJob) | ||
assert.NoError(t, err) | ||
|
||
inputs, _ := coreutils.MakeLiteralMap(map[string]interface{}{"x": 1}) | ||
template := flyteIdlCore.TaskTemplate{ | ||
Type: "bigquery_query_job_task", | ||
Custom: st, | ||
} | ||
basePrefix := storage.DataReference("fake://bucket/prefix/") | ||
|
||
t.Run("run a job", func(t *testing.T) { | ||
pluginEntry := pluginmachinery.CreateRemotePlugin(newMockGrpcPlugin()) | ||
plugin, err := pluginEntry.LoadPlugin(context.TODO(), newFakeSetupContext("test1")) | ||
assert.NoError(t, err) | ||
|
||
phase := tests.RunPluginEndToEndTest(t, plugin, &template, inputs, nil, nil, iter) | ||
assert.Equal(t, true, phase.Phase().IsSuccess()) | ||
}) | ||
|
||
t.Run("failed to create a job", func(t *testing.T) { | ||
grpcPlugin := newMockGrpcPlugin() | ||
grpcPlugin.PluginLoader = func(ctx context.Context, iCtx webapi.PluginSetupContext) (webapi.AsyncPlugin, error) { | ||
return &MockPlugin{ | ||
Plugin{ | ||
metricScope: iCtx.MetricsScope(), | ||
cfg: GetConfig(), | ||
getClient: mockGetBadClientFunc, | ||
}, | ||
}, nil | ||
} | ||
pluginEntry := pluginmachinery.CreateRemotePlugin(grpcPlugin) | ||
plugin, err := pluginEntry.LoadPlugin(context.TODO(), newFakeSetupContext("test2")) | ||
assert.NoError(t, err) | ||
|
||
tCtx := getTaskContext(t) | ||
tr := &pluginCoreMocks.TaskReader{} | ||
tr.OnRead(context.Background()).Return(&template, nil) | ||
tCtx.OnTaskReader().Return(tr) | ||
inputReader := &ioMocks.InputReader{} | ||
inputReader.OnGetInputPrefixPath().Return(basePrefix) | ||
inputReader.OnGetInputPath().Return(basePrefix + "/inputs.pb") | ||
inputReader.OnGetMatch(mock.Anything).Return(inputs, nil) | ||
tCtx.OnInputReader().Return(inputReader) | ||
|
||
trns, err := plugin.Handle(context.Background(), tCtx) | ||
assert.Error(t, err) | ||
assert.Equal(t, trns.Info().Phase(), core.PhaseUndefined) | ||
err = plugin.Abort(context.Background(), tCtx) | ||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("failed to read task template", func(t *testing.T) { | ||
tCtx := getTaskContext(t) | ||
tr := &pluginCoreMocks.TaskReader{} | ||
tr.OnRead(context.Background()).Return(nil, fmt.Errorf("read fail")) | ||
tCtx.OnTaskReader().Return(tr) | ||
|
||
grpcPlugin := newMockGrpcPlugin() | ||
pluginEntry := pluginmachinery.CreateRemotePlugin(grpcPlugin) | ||
plugin, err := pluginEntry.LoadPlugin(context.TODO(), newFakeSetupContext("test3")) | ||
assert.NoError(t, err) | ||
|
||
trns, err := plugin.Handle(context.Background(), tCtx) | ||
assert.Error(t, err) | ||
assert.Equal(t, trns.Info().Phase(), core.PhaseUndefined) | ||
}) | ||
|
||
t.Run("failed to read inputs", func(t *testing.T) { | ||
tCtx := getTaskContext(t) | ||
tr := &pluginCoreMocks.TaskReader{} | ||
tr.OnRead(context.Background()).Return(&template, nil) | ||
tCtx.OnTaskReader().Return(tr) | ||
inputReader := &ioMocks.InputReader{} | ||
inputReader.OnGetInputPrefixPath().Return(basePrefix) | ||
inputReader.OnGetInputPath().Return(basePrefix + "/inputs.pb") | ||
inputReader.OnGetMatch(mock.Anything).Return(nil, fmt.Errorf("read fail")) | ||
tCtx.OnInputReader().Return(inputReader) | ||
|
||
grpcPlugin := newMockGrpcPlugin() | ||
pluginEntry := pluginmachinery.CreateRemotePlugin(grpcPlugin) | ||
plugin, err := pluginEntry.LoadPlugin(context.TODO(), newFakeSetupContext("test4")) | ||
assert.NoError(t, err) | ||
|
||
trns, err := plugin.Handle(context.Background(), tCtx) | ||
assert.Error(t, err) | ||
assert.Equal(t, trns.Info().Phase(), core.PhaseUndefined) | ||
}) | ||
} | ||
|
||
func getTaskContext(t *testing.T) *pluginCoreMocks.TaskExecutionContext { | ||
latestKnownState := atomic.Value{} | ||
pluginStateReader := &pluginCoreMocks.PluginStateReader{} | ||
pluginStateReader.OnGetMatch(mock.Anything).Return(0, nil).Run(func(args mock.Arguments) { | ||
o := args.Get(0) | ||
x, err := json.Marshal(latestKnownState.Load()) | ||
assert.NoError(t, err) | ||
assert.NoError(t, json.Unmarshal(x, &o)) | ||
}) | ||
pluginStateWriter := &pluginCoreMocks.PluginStateWriter{} | ||
pluginStateWriter.OnPutMatch(mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) { | ||
latestKnownState.Store(args.Get(1)) | ||
}) | ||
|
||
pluginStateWriter.OnReset().Return(nil).Run(func(args mock.Arguments) { | ||
latestKnownState.Store(nil) | ||
}) | ||
|
||
execID := rand.String(3) | ||
tID := &pluginCoreMocks.TaskExecutionID{} | ||
tID.OnGetGeneratedName().Return(execID + "-my-task-1") | ||
tID.OnGetID().Return(flyteIdlCore.TaskExecutionIdentifier{ | ||
TaskId: &flyteIdlCore.Identifier{ | ||
ResourceType: flyteIdlCore.ResourceType_TASK, | ||
Project: "a", | ||
Domain: "d", | ||
Name: "n", | ||
Version: "abc", | ||
}, | ||
NodeExecutionId: &flyteIdlCore.NodeExecutionIdentifier{ | ||
NodeId: "node1", | ||
ExecutionId: &flyteIdlCore.WorkflowExecutionIdentifier{ | ||
Project: "a", | ||
Domain: "d", | ||
Name: "exec", | ||
}, | ||
}, | ||
RetryAttempt: 0, | ||
}) | ||
tMeta := &pluginCoreMocks.TaskExecutionMetadata{} | ||
tMeta.OnGetTaskExecutionID().Return(tID) | ||
resourceManager := &pluginCoreMocks.ResourceManager{} | ||
resourceManager.OnAllocateResourceMatch(mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(pluginCore.AllocationStatusGranted, nil) | ||
resourceManager.OnReleaseResourceMatch(mock.Anything, mock.Anything, mock.Anything).Return(nil) | ||
|
||
basePrefix := storage.DataReference("fake://bucket/prefix/" + execID) | ||
outputWriter := &ioMocks.OutputWriter{} | ||
outputWriter.OnGetRawOutputPrefix().Return("/sandbox/") | ||
outputWriter.OnGetOutputPrefixPath().Return(basePrefix) | ||
outputWriter.OnGetErrorPath().Return(basePrefix + "/error.pb") | ||
outputWriter.OnGetOutputPath().Return(basePrefix + "/outputs.pb") | ||
outputWriter.OnGetCheckpointPrefix().Return("/checkpoint") | ||
outputWriter.OnGetPreviousCheckpointsPrefix().Return("/prev") | ||
|
||
tCtx := &pluginCoreMocks.TaskExecutionContext{} | ||
tCtx.OnOutputWriter().Return(outputWriter) | ||
tCtx.OnResourceManager().Return(resourceManager) | ||
tCtx.OnPluginStateReader().Return(pluginStateReader) | ||
tCtx.OnPluginStateWriter().Return(pluginStateWriter) | ||
tCtx.OnTaskExecutionMetadata().Return(tMeta) | ||
return tCtx | ||
} | ||
|
||
func newMockGrpcPlugin() webapi.PluginEntry { | ||
return webapi.PluginEntry{ | ||
ID: "external-plugin-service", | ||
SupportedTaskTypes: []core.TaskType{"bigquery_query_job_task"}, | ||
PluginLoader: func(ctx context.Context, iCtx webapi.PluginSetupContext) (webapi.AsyncPlugin, error) { | ||
return &MockPlugin{ | ||
Plugin{ | ||
metricScope: iCtx.MetricsScope(), | ||
cfg: GetConfig(), | ||
getClient: mockGetClientFunc, | ||
}, | ||
}, nil | ||
}, | ||
} | ||
} | ||
|
||
func newFakeSetupContext(name string) *pluginCoreMocks.SetupContext { | ||
fakeResourceRegistrar := pluginCoreMocks.ResourceRegistrar{} | ||
fakeResourceRegistrar.On("RegisterResourceQuota", mock.Anything, mock.Anything, mock.Anything).Return(nil) | ||
labeled.SetMetricKeys(contextutils.NamespaceKey) | ||
|
||
fakeSetupContext := pluginCoreMocks.SetupContext{} | ||
fakeSetupContext.OnMetricsScope().Return(promutils.NewScope(name)) | ||
fakeSetupContext.OnResourceRegistrar().Return(&fakeResourceRegistrar) | ||
|
||
return &fakeSetupContext | ||
} |
Oops, something went wrong.