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

tabletserver: more resilient wait for schema changes #7684

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go/vt/vttablet/endtoend/config_test.go
Expand Up @@ -191,7 +191,7 @@ func testQueryPlanCache(t *testing.T, cachedPlanSize, cachePlanSize2 int) {
t.Helper()

//sleep to avoid race between SchemaChanged event clearing out the plans cache which breaks this test
time.Sleep(1 * time.Second)
framework.Server.WaitForSchemaReset(2 * time.Second)

defer framework.Server.SetQueryPlanCacheCap(framework.Server.QueryPlanCacheCap())
framework.Server.SetQueryPlanCacheCap(cachedPlanSize)
Expand Down
26 changes: 26 additions & 0 deletions go/vt/vttablet/tabletserver/tabletserver.go
Expand Up @@ -399,6 +399,32 @@ func (tsv *TabletServer) ReloadSchema(ctx context.Context) error {
return tsv.se.Reload(ctx)
}

// WaitForSchemaReset blocks the TabletServer until there's been at least `timeout` duration without
// any schema changes. This is useful for tests that need to wait for all the currently existing schema
// changes to finish being applied.
func (tsv *TabletServer) WaitForSchemaReset(timeout time.Duration) {
onSchemaChange := make(chan struct{}, 1)
tsv.se.RegisterNotifier("_tsv_wait", func(_ map[string]*schema.Table, _, _, _ []string) {
onSchemaChange <- struct{}{}
})
defer tsv.se.UnregisterNotifier("_tsv_wait")

after := time.NewTimer(timeout)
defer after.Stop()

for {
select {
case <-after.C:
return
case <-onSchemaChange:
if !after.Stop() {
<-after.C
}
after.Reset(timeout)
}
}
}

// ClearQueryPlanCache clears internal query plan cache
func (tsv *TabletServer) ClearQueryPlanCache() {
// We should ideally bracket this with start & endErequest,
Expand Down