Skip to content

Commit

Permalink
Clear keyspace schema in vtgate before re-loading it
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord committed Dec 22, 2021
1 parent a2fc06a commit 4a3225d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
17 changes: 16 additions & 1 deletion go/vt/vtgate/schema/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,14 @@ func (t *Tracker) LoadKeyspace(conn queryservice.QueryService, target *querypb.T
}
t.mu.Lock()
defer t.mu.Unlock()
// We must clear out any previous schema before loading it here as this is called
// whenever a shard's primary tablet starts and sends the initial signal. Without
// clearing out the previous schema we can end up with duplicate entries when the
// tablet is simply restarted or potentially when we elect a new primary.
t.clearKeyspaceTables(target.Keyspace)
t.updateTables(target.Keyspace, res)
t.tracked[target.Keyspace].setLoaded(true)
log.Infof("finished loading schema for keyspace %s. Found %d tables", target.Keyspace, len(res.Rows))
log.Infof("finished loading schema for keyspace %s. Found %d columns in total across the tables", target.Keyspace, len(res.Rows))
return nil
}

Expand Down Expand Up @@ -128,6 +133,7 @@ func (t *Tracker) newUpdateController() *updateController {
}

func (t *Tracker) initKeyspace(th *discovery.TabletHealth) error {

err := t.LoadKeyspace(th.Conn, th.Target)
if err != nil {
log.Warningf("Unable to add keyspace to tracker: %v", err)
Expand Down Expand Up @@ -255,3 +261,12 @@ func (tm *tableMap) delete(ks, tbl string) {
}
delete(m, tbl)
}

// This empties out any previous schema for for all tables in a keyspace
// You should call this before initializing/loading a keyspace of the same
// name in the cache
func (t *Tracker) clearKeyspaceTables(ks string) {
if t.tables != nil {
delete(t.tables.m, ks)
}
}
File renamed without changes.
10 changes: 5 additions & 5 deletions go/vt/vtgate/schema/update_controller_flaky_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestMultipleUpdatesFromDifferentShards(t *testing.T) {
signal := func() {
signalNb++
}
kUpdate := updateController{
updateCont := updateController{
update: update,
signal: signal,
consumeDelay: 5 * time.Millisecond,
Expand Down Expand Up @@ -159,13 +159,13 @@ func TestMultipleUpdatesFromDifferentShards(t *testing.T) {
if test.delay > 0 {
time.Sleep(test.delay)
}
kUpdate.add(d)
updateCont.add(d)
}

for {
kUpdate.mu.Lock()
done := kUpdate.queue == nil
kUpdate.mu.Unlock()
updateCont.mu.Lock()
done := updateCont.queue == nil
updateCont.mu.Unlock()
if done {
break
}
Expand Down

0 comments on commit 4a3225d

Please sign in to comment.