forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Add Grpc registry store for Go feature server #362
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
Open
piket
wants to merge
3
commits into
master
Choose a base branch
from
grpc-registry-store-go
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,147 @@ | ||
| package registry | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/feast-dev/feast/go/protos/feast/core" | ||
| registryPb "github.com/feast-dev/feast/go/protos/feast/registry" | ||
| "github.com/rs/zerolog/log" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
| "google.golang.org/protobuf/types/known/emptypb" | ||
| ) | ||
|
|
||
| type GrpcRegistryStore struct { | ||
| project string | ||
| client registryPb.RegistryServerClient | ||
| conn *grpc.ClientConn | ||
| } | ||
|
|
||
| // NewGrpcRegistryStore creates a gRPC-backed registry store. | ||
| // | ||
| // TLS is enabled when any of the following are true (mirrors Python RemoteRegistryConfig): | ||
| // - config.Path uses the "grpcs://" scheme | ||
| // - config.IsTls is true | ||
| // - config.Cert is set (path to a PEM certificate file) | ||
| // | ||
| // Without TLS, the connection is insecure. config.Path may be bare "host:port", | ||
| // "grpc://host:port", or "grpcs://host:port". | ||
| func NewGrpcRegistryStore(config *RegistryConfig, project string) (*GrpcRegistryStore, error) { | ||
| target, schemeIsTLS := parseGrpcTarget(config.Path) | ||
| useTLS := schemeIsTLS || config.IsTls || config.Cert != "" | ||
|
|
||
| var dialOpts []grpc.DialOption | ||
| if useTLS { | ||
| tlsCreds, err := buildTLSCredentials(config.Cert) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| dialOpts = append(dialOpts, grpc.WithTransportCredentials(tlsCreds)) | ||
| } else { | ||
| dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
| } | ||
|
|
||
| conn, err := grpc.NewClient(target, dialOpts...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| log.Info().Msgf("Using gRPC Feature Registry: %s", config.Path) | ||
| return &GrpcRegistryStore{ | ||
| project: project, | ||
| client: registryPb.NewRegistryServerClient(conn), | ||
| conn: conn, | ||
| }, nil | ||
| } | ||
|
|
||
| // buildTLSCredentials returns TLS transport credentials. If certPath is non-empty | ||
| // the PEM file is loaded as a custom root CA (for self-signed certs), otherwise | ||
| // the system certificate pool is used. | ||
| func buildTLSCredentials(certPath string) (credentials.TransportCredentials, error) { | ||
| if certPath == "" { | ||
| return credentials.NewTLS(&tls.Config{}), nil | ||
| } | ||
| pemBytes, err := os.ReadFile(certPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("grpc registry: reading cert file %q: %w", certPath, err) | ||
| } | ||
| pool := x509.NewCertPool() | ||
| if !pool.AppendCertsFromPEM(pemBytes) { | ||
| return nil, fmt.Errorf("grpc registry: no valid PEM certificates found in %q", certPath) | ||
| } | ||
| return credentials.NewTLS(&tls.Config{RootCAs: pool}), nil | ||
| } | ||
|
|
||
| // parseGrpcTarget strips grpc:// or grpcs:// scheme prefixes and returns | ||
| // the target address along with whether TLS should be used. | ||
| func parseGrpcTarget(path string) (target string, useTLS bool) { | ||
| if strings.HasPrefix(path, "https://") { | ||
| return strings.TrimPrefix(path, "https://"), true | ||
| } | ||
| if strings.HasPrefix(path, "http://") { | ||
| return strings.TrimPrefix(path, "http://"), false | ||
| } | ||
| return path, false | ||
| } | ||
|
Comment on lines
+81
to
+91
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'm confused on this. I see the comment above is for it stripping It should strip both based on what |
||
|
|
||
| func (g *GrpcRegistryStore) getEntity(name string, allowCache bool) (*core.Entity, error) { | ||
| return g.client.GetEntity(context.Background(), ®istryPb.GetEntityRequest{ | ||
| Name: name, | ||
| Project: g.project, | ||
| AllowCache: allowCache, | ||
| }) | ||
| } | ||
|
|
||
| func (g *GrpcRegistryStore) getFeatureView(name string, allowCache bool) (*core.FeatureView, error) { | ||
| return g.client.GetFeatureView(context.Background(), ®istryPb.GetFeatureViewRequest{ | ||
| Name: name, | ||
| Project: g.project, | ||
| AllowCache: allowCache, | ||
| }) | ||
| } | ||
|
|
||
| func (g *GrpcRegistryStore) getSortedFeatureView(name string, allowCache bool) (*core.SortedFeatureView, error) { | ||
| return g.client.GetSortedFeatureView(context.Background(), ®istryPb.GetSortedFeatureViewRequest{ | ||
| Name: name, | ||
| Project: g.project, | ||
| AllowCache: allowCache, | ||
| }) | ||
| } | ||
|
|
||
| func (g *GrpcRegistryStore) getOnDemandFeatureView(name string, allowCache bool) (*core.OnDemandFeatureView, error) { | ||
| return g.client.GetOnDemandFeatureView(context.Background(), ®istryPb.GetOnDemandFeatureViewRequest{ | ||
| Name: name, | ||
| Project: g.project, | ||
| AllowCache: allowCache, | ||
| }) | ||
| } | ||
|
|
||
| func (g *GrpcRegistryStore) getFeatureService(name string, allowCache bool) (*core.FeatureService, error) { | ||
| return g.client.GetFeatureService(context.Background(), ®istryPb.GetFeatureServiceRequest{ | ||
| Name: name, | ||
| Project: g.project, | ||
| AllowCache: allowCache, | ||
| }) | ||
| } | ||
|
|
||
| func (g *GrpcRegistryStore) GetRegistryProto() (*core.Registry, error) { | ||
| return g.client.Proto(context.Background(), &emptypb.Empty{}) | ||
| } | ||
|
|
||
| func (g *GrpcRegistryStore) UpdateRegistryProto(rp *core.Registry) error { | ||
| return &NotImplementedError{FunctionName: "UpdateRegistryProto"} | ||
| } | ||
|
|
||
| func (g *GrpcRegistryStore) Teardown() error { | ||
| return g.conn.Close() | ||
| } | ||
|
|
||
| func (g *GrpcRegistryStore) HasFallback() bool { | ||
| return true | ||
| } | ||
This file contains hidden or 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,191 @@ | ||
| //go:build !integration | ||
|
|
||
| package registry | ||
|
|
||
| import ( | ||
| "context" | ||
| "net" | ||
| "testing" | ||
|
|
||
| "github.com/feast-dev/feast/go/protos/feast/core" | ||
| registryPb "github.com/feast-dev/feast/go/protos/feast/registry" | ||
| "github.com/stretchr/testify/assert" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/connectivity" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
| "google.golang.org/grpc/test/bufconn" | ||
| "google.golang.org/protobuf/types/known/emptypb" | ||
| ) | ||
|
|
||
| // testRegistryServer is a minimal RegistryServer implementation for testing. | ||
| type testRegistryServer struct { | ||
| registryPb.UnimplementedRegistryServerServer | ||
| } | ||
|
|
||
| func (s *testRegistryServer) GetEntity(_ context.Context, req *registryPb.GetEntityRequest) (*core.Entity, error) { | ||
| return &core.Entity{Spec: &core.EntitySpecV2{Name: req.Name}}, nil | ||
| } | ||
|
|
||
| func (s *testRegistryServer) GetFeatureView(_ context.Context, req *registryPb.GetFeatureViewRequest) (*core.FeatureView, error) { | ||
| return &core.FeatureView{Spec: &core.FeatureViewSpec{Name: req.Name}}, nil | ||
| } | ||
|
|
||
| func (s *testRegistryServer) GetSortedFeatureView(_ context.Context, req *registryPb.GetSortedFeatureViewRequest) (*core.SortedFeatureView, error) { | ||
| return &core.SortedFeatureView{Spec: &core.SortedFeatureViewSpec{Name: req.Name}}, nil | ||
| } | ||
|
|
||
| func (s *testRegistryServer) GetOnDemandFeatureView(_ context.Context, req *registryPb.GetOnDemandFeatureViewRequest) (*core.OnDemandFeatureView, error) { | ||
| return &core.OnDemandFeatureView{Spec: &core.OnDemandFeatureViewSpec{Name: req.Name}}, nil | ||
| } | ||
|
|
||
| func (s *testRegistryServer) GetFeatureService(_ context.Context, req *registryPb.GetFeatureServiceRequest) (*core.FeatureService, error) { | ||
| return &core.FeatureService{Spec: &core.FeatureServiceSpec{Name: req.Name}}, nil | ||
| } | ||
|
|
||
| func (s *testRegistryServer) Proto(_ context.Context, _ *emptypb.Empty) (*core.Registry, error) { | ||
| return &core.Registry{}, nil | ||
| } | ||
|
|
||
| // newTestGrpcStore spins up an in-process gRPC server using bufconn and returns | ||
| // a GrpcRegistryStore backed by it, along with a cleanup function. | ||
| func newTestGrpcStore(t *testing.T, project string) (*GrpcRegistryStore, func()) { | ||
| t.Helper() | ||
|
|
||
| listener := bufconn.Listen(1024 * 1024) | ||
| srv := grpc.NewServer() | ||
| registryPb.RegisterRegistryServerServer(srv, &testRegistryServer{}) | ||
| go func() { | ||
| if err := srv.Serve(listener); err != nil && err != grpc.ErrServerStopped { | ||
| t.Errorf("bufconn server error: %v", err) | ||
| } | ||
| }() | ||
|
|
||
| conn, err := grpc.NewClient( | ||
| "passthrough:///bufconn", | ||
| grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
| grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { | ||
| return listener.DialContext(ctx) | ||
| }), | ||
| ) | ||
| if err != nil { | ||
| t.Fatalf("failed to create grpc client: %v", err) | ||
| } | ||
|
|
||
| store := &GrpcRegistryStore{ | ||
| project: project, | ||
| client: registryPb.NewRegistryServerClient(conn), | ||
| conn: conn, | ||
| } | ||
|
|
||
| cleanup := func() { | ||
| conn.Close() | ||
| srv.Stop() | ||
| listener.Close() | ||
| } | ||
| return store, cleanup | ||
| } | ||
|
|
||
| func TestGrpcGetEntity(t *testing.T) { | ||
| store, cleanup := newTestGrpcStore(t, "test_project") | ||
| defer cleanup() | ||
|
|
||
| result, err := store.getEntity("test_entity", true) | ||
|
|
||
| assert.Nil(t, err) | ||
| assert.Equal(t, "test_entity", result.Spec.Name) | ||
| } | ||
|
|
||
| func TestGrpcGetFeatureView(t *testing.T) { | ||
| store, cleanup := newTestGrpcStore(t, "test_project") | ||
| defer cleanup() | ||
|
|
||
| result, err := store.getFeatureView("test_feature_view", true) | ||
|
|
||
| assert.Nil(t, err) | ||
| assert.Equal(t, "test_feature_view", result.Spec.Name) | ||
| } | ||
|
|
||
| func TestGrpcGetSortedFeatureView(t *testing.T) { | ||
| store, cleanup := newTestGrpcStore(t, "test_project") | ||
| defer cleanup() | ||
|
|
||
| result, err := store.getSortedFeatureView("test_sorted_view", true) | ||
|
|
||
| assert.Nil(t, err) | ||
| assert.Equal(t, "test_sorted_view", result.Spec.Name) | ||
| } | ||
|
|
||
| func TestGrpcGetOnDemandFeatureView(t *testing.T) { | ||
| store, cleanup := newTestGrpcStore(t, "test_project") | ||
| defer cleanup() | ||
|
|
||
| result, err := store.getOnDemandFeatureView("test_odfv", true) | ||
|
|
||
| assert.Nil(t, err) | ||
| assert.Equal(t, "test_odfv", result.Spec.Name) | ||
| } | ||
|
|
||
| func TestGrpcGetFeatureService(t *testing.T) { | ||
| store, cleanup := newTestGrpcStore(t, "test_project") | ||
| defer cleanup() | ||
|
|
||
| result, err := store.getFeatureService("test_feature_service", true) | ||
|
|
||
| assert.Nil(t, err) | ||
| assert.Equal(t, "test_feature_service", result.Spec.Name) | ||
| } | ||
|
|
||
| func TestGrpcGetRegistryProto(t *testing.T) { | ||
| store, cleanup := newTestGrpcStore(t, "test_project") | ||
| defer cleanup() | ||
|
|
||
| registry, err := store.GetRegistryProto() | ||
|
|
||
| assert.Nil(t, err) | ||
| assert.IsType(t, &core.Registry{}, registry) | ||
| } | ||
|
|
||
| func TestGrpcUpdateRegistryProtoNotImplemented(t *testing.T) { | ||
| store, cleanup := newTestGrpcStore(t, "test_project") | ||
| defer cleanup() | ||
|
|
||
| err := store.UpdateRegistryProto(&core.Registry{}) | ||
|
|
||
| assert.NotNil(t, err) | ||
| assert.IsType(t, &NotImplementedError{}, err) | ||
| assert.Equal(t, "UpdateRegistryProto", err.(*NotImplementedError).FunctionName) | ||
| } | ||
|
|
||
| func TestGrpcHasFallback(t *testing.T) { | ||
| store, cleanup := newTestGrpcStore(t, "test_project") | ||
| defer cleanup() | ||
|
|
||
| assert.True(t, store.HasFallback()) | ||
| } | ||
|
|
||
| func TestGrpcTeardown(t *testing.T) { | ||
| store, cleanup := newTestGrpcStore(t, "test_project") | ||
| defer cleanup() | ||
|
|
||
| err := store.Teardown() | ||
|
|
||
| assert.Nil(t, err) | ||
| assert.Equal(t, connectivity.Shutdown, store.conn.GetState()) | ||
| } | ||
|
|
||
| func TestParseGrpcTarget(t *testing.T) { | ||
| cases := []struct { | ||
| input string | ||
| target string | ||
| useTLS bool | ||
| }{ | ||
| {"http://host:50051", "host:50051", false}, | ||
| {"https://host:50051", "host:50051", true}, | ||
| {"host:50051", "host:50051", false}, | ||
| } | ||
| for _, c := range cases { | ||
| target, useTLS := parseGrpcTarget(c.input) | ||
| assert.Equal(t, c.target, target, "input: %s", c.input) | ||
| assert.Equal(t, c.useTLS, useTLS, "input: %s", c.input) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
HttpRegistryStore has a 5 second timeout applied here.
It looks like there's no timeouts set for this.
I think we can do it with this:
https://pkg.go.dev/google.golang.org/grpc#WithDefaultServiceConfig
https://github.com/grpc/grpc/blob/master/doc/service_config.md