Skip to content

Commit

Permalink
bugfix: fix nil pointer in vtgate on topo connection error (#13010)
Browse files Browse the repository at this point in the history
* fix nil pointer in vtgate on topo connection error

Signed-off-by: Olga Shestopalova <[email protected]>

* add test

Signed-off-by: Olga Shestopalova <[email protected]>

* fix year

Signed-off-by: Olga Shestopalova <[email protected]>

---------

Signed-off-by: Olga Shestopalova <[email protected]>
Co-authored-by: Olga Shestopalova <[email protected]>
  • Loading branch information
olyazavr and Olga Shestopalova authored May 10, 2023
1 parent 013ab3d commit 2cd5cac
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
100 changes: 100 additions & 0 deletions go/vt/discovery/keyspace_events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
Copyright 2023 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 discovery

import (
"context"
"encoding/hex"
"testing"
"time"

"github.com/stretchr/testify/require"

topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/faketopo"
)

func TestSrvKeyspaceWithNilNewKeyspace(t *testing.T) {
cell := "cell"
keyspace := "testks"
factory := faketopo.NewFakeTopoFactory()
factory.AddCell(cell)
ts := faketopo.NewFakeTopoServer(factory)
ts2 := &fakeTopoServer{}
hc := NewHealthCheck(context.Background(), 1*time.Millisecond, time.Hour, ts, cell, "")
kew := NewKeyspaceEventWatcher(context.Background(), ts2, hc, cell)
kss := &keyspaceState{
kew: kew,
keyspace: keyspace,
shards: make(map[string]*shardState),
}
kss.lastKeyspace = &topodatapb.SrvKeyspace{
ServedFrom: []*topodatapb.SrvKeyspace_ServedFrom{
{
TabletType: topodatapb.TabletType_PRIMARY,
Keyspace: keyspace,
},
},
}
require.True(t, kss.onSrvKeyspace(nil, nil))
}

type fakeTopoServer struct {
}

// GetTopoServer returns the full topo.Server instance.
func (f *fakeTopoServer) GetTopoServer() (*topo.Server, error) {
return nil, nil
}

// GetSrvKeyspaceNames returns the list of keyspaces served in
// the provided cell.
func (f *fakeTopoServer) GetSrvKeyspaceNames(ctx context.Context, cell string, staleOK bool) ([]string, error) {
return []string{"ks1"}, nil
}

// GetSrvKeyspace returns the SrvKeyspace for a cell/keyspace.
func (f *fakeTopoServer) GetSrvKeyspace(ctx context.Context, cell, keyspace string) (*topodatapb.SrvKeyspace, error) {
zeroHexBytes, _ := hex.DecodeString("")
eightyHexBytes, _ := hex.DecodeString("80")
ks := &topodatapb.SrvKeyspace{
Partitions: []*topodatapb.SrvKeyspace_KeyspacePartition{
{
ServedType: topodatapb.TabletType_PRIMARY,
ShardReferences: []*topodatapb.ShardReference{
{Name: "-80", KeyRange: &topodatapb.KeyRange{Start: zeroHexBytes, End: eightyHexBytes}},
{Name: "80-", KeyRange: &topodatapb.KeyRange{Start: eightyHexBytes, End: zeroHexBytes}},
},
},
},
}
return ks, nil
}

func (f *fakeTopoServer) WatchSrvKeyspace(ctx context.Context, cell, keyspace string, callback func(*topodatapb.SrvKeyspace, error) bool) {
ks, err := f.GetSrvKeyspace(ctx, cell, keyspace)
callback(ks, err)
}

// WatchSrvVSchema starts watching the SrvVSchema object for
// the provided cell. It will call the callback when
// a new value or an error occurs.
func (f *fakeTopoServer) WatchSrvVSchema(ctx context.Context, cell string, callback func(*vschemapb.SrvVSchema, error) bool) {

}
3 changes: 3 additions & 0 deletions go/vt/topo/topoproto/srvkeyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func (sra ShardReferenceArray) Sort() { sort.Sort(sra) }
// SrvKeyspaceGetPartition returns a Partition for the given tablet type,
// or nil if it's not there.
func SrvKeyspaceGetPartition(sk *topodatapb.SrvKeyspace, tabletType topodatapb.TabletType) *topodatapb.SrvKeyspace_KeyspacePartition {
if sk == nil {
return nil
}
for _, p := range sk.Partitions {
if p.ServedType == tabletType {
return p
Expand Down

0 comments on commit 2cd5cac

Please sign in to comment.