Skip to content
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

Tablet picker: Handle the case where a primary tablet is not setup for a shard #17573

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions go/vt/discovery/tablet_picker.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,13 @@ func (tp *TabletPicker) GetMatchingTablets(ctx context.Context) []*topo.TabletIn
log.Errorf("Error getting shard %s/%s: %v", tp.keyspace, tp.shard, err)
return nil
}
if _, ignore := tp.ignoreTablets[si.PrimaryAlias.String()]; !ignore {
aliases = append(aliases, si.PrimaryAlias)

// It is possible that there is a cluster event (ERS/PRS, for example) due to which
// there is no primary elected for the shard at the moment.
if si.PrimaryAlias != nil {
if _, ignore := tp.ignoreTablets[si.PrimaryAlias.String()]; !ignore {
aliases = append(aliases, si.PrimaryAlias)
}
}
} else {
actualCells := make([]string, 0)
Expand Down Expand Up @@ -425,6 +430,9 @@ func (tp *TabletPicker) GetMatchingTablets(ctx context.Context) []*topo.TabletIn
continue
}
for _, node := range sri.Nodes {
if node.TabletAlias == nil {
continue
}
if _, ignore := tp.ignoreTablets[node.TabletAlias.String()]; !ignore {
aliases = append(aliases, node.TabletAlias)
}
Expand Down
27 changes: 27 additions & 0 deletions go/vt/discovery/tablet_picker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ func TestPickPrimary(t *testing.T) {
assert.True(t, proto.Equal(want, tablet), "Pick: %v, want %v", tablet, want)
}

// TestPickNoPrimary confirms that if the picker was setup only for primary tablets but
// there is no primary setup for the shard we correctly return an error.
func TestPickNoPrimary(t *testing.T) {
defer utils.EnsureNoLeaks(t)
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
Copy link
Contributor

@mattlord mattlord Jan 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use multiple contexts, and why with such short timeouts? Won't this make it flaky in the CI? I get why we use the short context for PickForStreaming() — to cut short the search — but I don't see why we use the other two contexts (instead of one) with such short timeouts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a cut/paste of a previous test and I had only changed one line, so didn't focus on that. Changed to use a single context.

defer cancel()

te := newPickerTestEnv(t, ctx, []string{"cell", "otherCell"})
want := addTablet(ctx, te, 100, topodatapb.TabletType_PRIMARY, "cell", true, true)
defer deleteTablet(t, te, want)
ctx, cancel = context.WithTimeout(ctx, 200*time.Millisecond)
defer cancel()
_, err := te.topoServ.UpdateShardFields(ctx, te.keyspace, te.shard, func(si *topo.ShardInfo) error {
si.PrimaryAlias = nil // force a missing primary
return nil
})
require.NoError(t, err)

tp, err := NewTabletPicker(ctx, te.topoServ, []string{"otherCell"}, "cell", te.keyspace, te.shard, "primary", TabletPickerOptions{})
require.NoError(t, err)

ctx2, cancel2 := context.WithTimeout(ctx, 200*time.Millisecond)
defer cancel2()
_, err = tp.PickForStreaming(ctx2)
require.Errorf(t, err, "No healthy serving tablet")
}

func TestPickLocalPreferences(t *testing.T) {
defer utils.EnsureNoLeaks(t)
type tablet struct {
Expand Down
3 changes: 3 additions & 0 deletions go/vt/topo/tablet.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@ func (ts *Server) GetTabletMap(ctx context.Context, tabletAliases []*topodatapb.
)

for _, tabletAlias := range tabletAliases {
if tabletAlias == nil {
return nil, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "nil tablet alias in list")
}
wg.Add(1)
go func(tabletAlias *topodatapb.TabletAlias) {
defer wg.Done()
Expand Down
Loading