-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[closes #4378] Add keyspace visibility filtering to vtgates #4420
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
Merged
demmer
merged 14 commits into
vitessio:master
from
tinyspeck:setassociative-#4378-add-keyspace-filtering
Dec 14, 2018
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3eb4014
[closes #4378] Adds keyspaces_to_watch flag to vtgate
e821613
move log message into vtgate impl
44b5ded
swap to a different context so that the linter is happy
7dcd85e
Clean up reference to selectedKeyspaces which is not in scope from vt…
b96d2f1
Merge branch 'master' into setassociative-#4378-add-keyspace-filtering
893ca57
PR feedback
461e7a0
minor unit test cleanups, add test coverage for nil schema & error pa…
1110a8c
Rename error to meet lint rules
744c6b5
add super basic srvtopo.Server impl for use in tests and switch to th…
bef3eb5
Shift filter.KeyspacesToWatch into gateway package per chat w/ demmer
7938800
This blocks use of the underlying topo server when using a filtering …
944d406
Updated error mesasge because the previous was awfully specific despi…
fe3499c
Document the error behavior
a72b957
Test that everything gets filtered and the return is empty, but not nil
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
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,116 @@ | ||
| /* | ||
| Copyright 2018 The Vitess Authors. | ||
|
|
||
| Licensed 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 srvtopo | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "golang.org/x/net/context" | ||
|
|
||
| topodatapb "vitess.io/vitess/go/vt/proto/topodata" | ||
| vschemapb "vitess.io/vitess/go/vt/proto/vschema" | ||
| "vitess.io/vitess/go/vt/topo" | ||
| ) | ||
|
|
||
| var ( | ||
| // ErrNilUnderlyingServer is returned when attempting to create a new keyspace | ||
| // filtering server if a nil underlying server implementation is provided. | ||
| ErrNilUnderlyingServer = fmt.Errorf("Unable to construct filtering server without an underlying server") | ||
|
|
||
| // ErrTopoServerNotAvailable is returned if a caller tries to access the | ||
| // topo.Server supporting this srvtopo.Server. | ||
| ErrTopoServerNotAvailable = fmt.Errorf("Cannot access underlying topology server when keyspace filtering is enabled") | ||
| ) | ||
|
|
||
| // NewKeyspaceFilteringServer constructs a new server based on the provided | ||
| // implementation that prevents the specified keyspaces from being exposed | ||
| // to consumers of the new Server. | ||
| // | ||
| // A filtering server will not allow access to the topo.Server to prevent | ||
| // updates that may corrupt the global VSchema keyspace. | ||
| func NewKeyspaceFilteringServer(underlying Server, selectedKeyspaces []string) (Server, error) { | ||
| if underlying == nil { | ||
| return nil, ErrNilUnderlyingServer | ||
| } | ||
|
|
||
| keyspaces := map[string]bool{} | ||
| for _, ks := range selectedKeyspaces { | ||
| keyspaces[ks] = true | ||
| } | ||
|
|
||
| return keyspaceFilteringServer{ | ||
| server: underlying, | ||
| selectKeyspaces: keyspaces, | ||
| }, nil | ||
| } | ||
|
|
||
| type keyspaceFilteringServer struct { | ||
| server Server | ||
| selectKeyspaces map[string]bool | ||
| } | ||
|
|
||
| // GetTopoServer returns an error; filtering srvtopo.Server consumers may not | ||
| // access the underlying topo.Server. | ||
| func (ksf keyspaceFilteringServer) GetTopoServer() (*topo.Server, error) { | ||
| return nil, ErrTopoServerNotAvailable | ||
| } | ||
|
|
||
| func (ksf keyspaceFilteringServer) GetSrvKeyspaceNames( | ||
| ctx context.Context, | ||
| cell string, | ||
| ) ([]string, error) { | ||
| keyspaces, err := ksf.server.GetSrvKeyspaceNames(ctx, cell) | ||
| ret := make([]string, 0, len(keyspaces)) | ||
| for _, ks := range keyspaces { | ||
| if ksf.selectKeyspaces[ks] { | ||
| ret = append(ret, ks) | ||
| } | ||
| } | ||
| return ret, err | ||
| } | ||
|
|
||
| func (ksf keyspaceFilteringServer) GetSrvKeyspace( | ||
| ctx context.Context, | ||
| cell, | ||
| keyspace string, | ||
| ) (*topodatapb.SrvKeyspace, error) { | ||
| if !ksf.selectKeyspaces[keyspace] { | ||
| return nil, topo.NewError(topo.NoNode, keyspace) | ||
| } | ||
|
|
||
| return ksf.server.GetSrvKeyspace(ctx, cell, keyspace) | ||
| } | ||
|
|
||
| func (ksf keyspaceFilteringServer) WatchSrvVSchema( | ||
| ctx context.Context, | ||
| cell string, | ||
| callback func(*vschemapb.SrvVSchema, error), | ||
| ) { | ||
| filteringCallback := func(schema *vschemapb.SrvVSchema, err error) { | ||
| if schema != nil { | ||
| for ks := range schema.Keyspaces { | ||
| if !ksf.selectKeyspaces[ks] { | ||
| delete(schema.Keyspaces, ks) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| callback(schema, err) | ||
| } | ||
|
|
||
| ksf.server.WatchSrvVSchema(ctx, cell, filteringCallback) | ||
| } |
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,229 @@ | ||
| /* | ||
| Copyright 2018 The Vitess Authors. | ||
|
|
||
| Licensed 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 srvtopo | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "reflect" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "golang.org/x/net/context" | ||
|
|
||
| topodatapb "vitess.io/vitess/go/vt/proto/topodata" | ||
| vschemapb "vitess.io/vitess/go/vt/proto/vschema" | ||
| "vitess.io/vitess/go/vt/srvtopo/srvtopotest" | ||
| "vitess.io/vitess/go/vt/topo" | ||
| "vitess.io/vitess/go/vt/topo/memorytopo" | ||
| ) | ||
|
|
||
| var ( | ||
| stockCell = "some-cell" | ||
| stockCtx = context.Background() | ||
| stockFilters = []string{"bar", "baz"} | ||
| stockKeyspaces = map[string]*topodatapb.SrvKeyspace{ | ||
| "foo": &topodatapb.SrvKeyspace{ShardingColumnName: "foo"}, | ||
| "bar": &topodatapb.SrvKeyspace{ShardingColumnName: "bar"}, | ||
| "baz": &topodatapb.SrvKeyspace{ShardingColumnName: "baz"}, | ||
| } | ||
| stockVSchema = &vschemapb.SrvVSchema{ | ||
| Keyspaces: map[string]*vschemapb.Keyspace{ | ||
| "foo": &vschemapb.Keyspace{Sharded: true}, | ||
| "bar": &vschemapb.Keyspace{Sharded: true}, | ||
| "baz": &vschemapb.Keyspace{Sharded: false}, | ||
| }, | ||
| } | ||
| ) | ||
|
|
||
| func newFiltering(filter []string) (*topo.Server, *srvtopotest.PassthroughSrvTopoServer, Server) { | ||
| testServer := srvtopotest.NewPassthroughSrvTopoServer() | ||
|
|
||
| testServer.TopoServer = memorytopo.NewServer(stockCell) | ||
| testServer.SrvKeyspaceNames = []string{"foo", "bar", "baz"} | ||
| testServer.SrvKeyspace = &topodatapb.SrvKeyspace{ShardingColumnName: "test-column"} | ||
| testServer.WatchedSrvVSchema = stockVSchema | ||
|
|
||
| filtering, _ := NewKeyspaceFilteringServer(testServer, filter) | ||
| return testServer.TopoServer, testServer, filtering | ||
| } | ||
|
|
||
| func TestFilteringServerHandlesNilUnderlying(t *testing.T) { | ||
| got, gotErr := NewKeyspaceFilteringServer(nil, []string{}) | ||
| if got != nil { | ||
| t.Errorf("got: %v, wanted: nil server", got) | ||
| } | ||
| if gotErr != ErrNilUnderlyingServer { | ||
| t.Errorf("Bad error returned: got %v wanted %v", gotErr, ErrNilUnderlyingServer) | ||
| } | ||
| } | ||
|
|
||
| func TestFilteringServerReturnsUnderlyingServer(t *testing.T) { | ||
| _, _, f := newFiltering(nil) | ||
| got, gotErr := f.GetTopoServer() | ||
| if got != nil { | ||
| t.Errorf("Got non-nil topo.Server from FilteringServer") | ||
| } | ||
| if gotErr != ErrTopoServerNotAvailable { | ||
| t.Errorf("Unexpected error from GetTopoServer; wanted %v but got %v", ErrTopoServerNotAvailable, gotErr) | ||
| } | ||
| } | ||
|
|
||
| func doTestGetSrvKeyspaceNames( | ||
| t *testing.T, | ||
| f Server, | ||
| cell string, | ||
| want []string, | ||
| wantErr error, | ||
| ) { | ||
| got, gotErr := f.GetSrvKeyspaceNames(stockCtx, cell) | ||
|
|
||
| if got == nil { | ||
| t.Errorf("GetSrvKeyspaceNames failed: should not return nil") | ||
| } | ||
| if !reflect.DeepEqual(got, want) { | ||
| t.Errorf("GetSrvKeyspaceNames failed: want %v, got %v", want, got) | ||
| } | ||
| if wantErr != gotErr { | ||
| t.Errorf("GetSrvKeyspaceNames returned incorrect error: want %v, got %v", wantErr, gotErr) | ||
| } | ||
| } | ||
|
|
||
| func TestFilteringServerGetSrvKeyspameNamesFiltersEverythingOut(t *testing.T) { | ||
| _, _, f := newFiltering(nil) | ||
| doTestGetSrvKeyspaceNames(t, f, stockCell, []string{}, nil) | ||
| } | ||
|
|
||
| func TestFilteringServerGetSrvKeyspaceNamesFiltersKeyspaces(t *testing.T) { | ||
| _, _, f := newFiltering(stockFilters) | ||
| doTestGetSrvKeyspaceNames(t, f, stockCell, stockFilters, nil) | ||
| } | ||
|
|
||
| func TestFilteringServerGetSrvKeyspaceNamesPassesThroughErrors(t *testing.T) { | ||
| _, mock, f := newFiltering(stockFilters) | ||
| wantErr := fmt.Errorf("some badcell error") | ||
| mock.SrvKeyspaceNamesError = wantErr | ||
| doTestGetSrvKeyspaceNames(t, f, "badcell", stockFilters, wantErr) | ||
| } | ||
|
|
||
| func doTestGetSrvKeyspace( | ||
| t *testing.T, | ||
| f Server, | ||
| cell, | ||
| ksName string, | ||
| want *topodatapb.SrvKeyspace, | ||
| wantErr error, | ||
| ) { | ||
| got, gotErr := f.GetSrvKeyspace(stockCtx, cell, ksName) | ||
|
|
||
| gotColumnName := "" | ||
| wantColumnName := "" | ||
| if got != nil { | ||
| gotColumnName = got.ShardingColumnName | ||
| } | ||
| if want != nil { | ||
| wantColumnName = want.ShardingColumnName | ||
| } | ||
|
|
||
| // a different pointer comes back so compare the expected return by proxy | ||
| // of a field we know the value of | ||
| if gotColumnName != wantColumnName { | ||
| t.Errorf("keyspace incorrect: got %v, want %v", got, want) | ||
| } | ||
|
|
||
| if wantErr != gotErr { | ||
| t.Errorf("returned error incorrect: got %v, want %v", gotErr, wantErr) | ||
| } | ||
| } | ||
|
|
||
| func TestFilteringServerGetSrvKeyspaceReturnsSelectedKeyspaces(t *testing.T) { | ||
| _, mock, f := newFiltering(stockFilters) | ||
| mock.SrvKeyspace = stockKeyspaces["bar"] | ||
| doTestGetSrvKeyspace(t, f, stockCell, "bar", stockKeyspaces["bar"], nil) | ||
| } | ||
|
|
||
| func TestFilteringServerGetSrvKeyspaceErrorPassthrough(t *testing.T) { | ||
| wantErr := fmt.Errorf("some error") | ||
| _, mock, f := newFiltering(stockFilters) | ||
| mock.SrvKeyspace = stockKeyspaces["bar"] | ||
| mock.SrvKeyspaceError = wantErr | ||
| doTestGetSrvKeyspace(t, f, "badcell", "bar", stockKeyspaces["bar"], wantErr) | ||
| } | ||
|
|
||
| func TestFilteringServerGetSrvKeyspaceFilters(t *testing.T) { | ||
| wantErr := topo.NewError(topo.NoNode, "foo") | ||
| _, mock, f := newFiltering(stockFilters) | ||
| mock.SrvKeyspaceError = wantErr | ||
| doTestGetSrvKeyspace(t, f, stockCell, "foo", nil, wantErr) | ||
| } | ||
|
|
||
| func TestFilteringServerWatchSrvVSchemaFiltersPassthroughSrvVSchema(t *testing.T) { | ||
| _, mock, f := newFiltering(stockFilters) | ||
|
|
||
| allowed := map[string]bool{} | ||
| for _, ks := range stockFilters { | ||
| allowed[ks] = true | ||
| } | ||
|
|
||
| // we need to verify that the nested callback actually gets called | ||
| wg := sync.WaitGroup{} | ||
| wg.Add(1) | ||
|
|
||
| cb := func(gotSchema *vschemapb.SrvVSchema, gotErr error) { | ||
| // ensure that only selected keyspaces made it into the callback | ||
| for name, ks := range gotSchema.Keyspaces { | ||
| if !allowed[name] { | ||
| t.Errorf("Unexpected keyspace found in callback: %v", ks) | ||
| } | ||
| wantKS := mock.WatchedSrvVSchema.Keyspaces[name] | ||
| if !reflect.DeepEqual(ks, wantKS) { | ||
| t.Errorf( | ||
| "Expected keyspace to be passed through unmodified: want %#v got %#v", | ||
| wantKS, | ||
| ks, | ||
| ) | ||
| } | ||
| } | ||
| wg.Done() | ||
| } | ||
|
|
||
| f.WatchSrvVSchema(stockCtx, stockCell, cb) | ||
| wg.Wait() | ||
| } | ||
|
|
||
| func TestFilteringServerWatchSrvVSchemaHandlesNilSchema(t *testing.T) { | ||
| wantErr := fmt.Errorf("some err") | ||
| _, mock, f := newFiltering(stockFilters) | ||
| mock.WatchedSrvVSchema = nil | ||
| mock.WatchedSrvVSchemaError = wantErr | ||
|
|
||
| // we need to verify that the nested callback actually gets called | ||
| wg := sync.WaitGroup{} | ||
| wg.Add(1) | ||
|
|
||
| cb := func(gotSchema *vschemapb.SrvVSchema, gotErr error) { | ||
| if gotSchema != nil { | ||
| t.Errorf("Expected nil gotSchema: got %#v", gotSchema) | ||
| } | ||
| if gotErr != wantErr { | ||
| t.Errorf("Unexpected error: want %v got %v", wantErr, gotErr) | ||
| } | ||
| wg.Done() | ||
| } | ||
|
|
||
| f.WatchSrvVSchema(stockCtx, "other-cell", cb) | ||
| wg.Wait() | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.