Skip to content

Commit

Permalink
fix: lock when vscham delete and add
Browse files Browse the repository at this point in the history
  • Loading branch information
newborn22 committed Dec 15, 2024
1 parent b3600cd commit b020b41
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
6 changes: 5 additions & 1 deletion go/vt/vtgate/engine/dbddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ func (c *DBDDL) createDatabase(ctx context.Context, vcursor VCursor, plugin DBDD
}

newKSSchema := &vindexes.KeyspaceSchema{Keyspace: &vindexes.Keyspace{Name: c.name, Sharded: false}, Tables: make(map[string]*vindexes.Table)}
vcursor.GetExecutorVSchema().Keyspaces[c.name] = newKSSchema
vcursor.VSchemaAddKeyspaceIfNotExists(c.name, newKSSchema)
//vcursor.GetExecutorVSchema().Keyspaces[c.name] = newKSSchema
//vcursor.GetVSchema().Keyspaces[c.name] = newKSSchema

return &sqltypes.Result{RowsAffected: 1}, nil
Expand All @@ -183,6 +184,9 @@ func (c *DBDDL) dropDatabase(ctx context.Context, vcursor VCursor, plugin DBDDLP
}
}

vcursor.VSchemaDeleteKeyspace(c.name)
//delete(vcursor.GetExecutorVSchema().Keyspaces, c.name)

return &sqltypes.Result{StatusFlags: sqltypes.ServerStatusDbDropped}, nil
}

Expand Down
16 changes: 16 additions & 0 deletions go/vt/vtgate/engine/fake_vcursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ func (t *noopVCursor) GetExecutorVSchema() *vindexes.VSchema {
panic("implement me")
}

func (t *noopVCursor) VSchemaAddKeyspaceIfNotExists(name string, KeyspaceSchema *vindexes.KeyspaceSchema) {
panic("implement me")
}

func (t *noopVCursor) VSchemaDeleteKeyspace(name string) {
panic("implement me")
}

func (t *noopVCursor) GetVSchema() *vindexes.VSchema {
// TODO implement me
panic("implement me")
Expand Down Expand Up @@ -836,6 +844,14 @@ func (f *loggingVCursor) GetExecutorVSchema() *vindexes.VSchema {
panic("implement me")
}

func (f *loggingVCursor) VSchemaAddKeyspaceIfNotExists(name string, KeyspaceSchema *vindexes.KeyspaceSchema) {
panic("implement me")
}

func (f *loggingVCursor) VSchemaDeleteKeyspace(name string) {
panic("implement me")
}

func (f *loggingVCursor) GetVSchema() {
panic("implement me")
}
Expand Down
8 changes: 6 additions & 2 deletions go/vt/vtgate/engine/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,13 @@ type (

FindHealthyPrimaryTablet() (*discovery.TabletHealth, error)

GetExecutorVSchema() *vindexes.VSchema
//GetExecutorVSchema() *vindexes.VSchema

GetVSchema() *vindexes.VSchema
VSchemaAddKeyspaceIfNotExists(name string, KeyspaceSchema *vindexes.KeyspaceSchema)

VSchemaDeleteKeyspace(name string)

//GetVSchema() *vindexes.VSchema
}

// SessionActions gives primitives ability to interact with the session state
Expand Down
14 changes: 14 additions & 0 deletions go/vt/vtgate/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,20 @@ func (e *Executor) VSchema() *vindexes.VSchema {
return e.vschema
}

func (e *Executor) VSchemaAddKeyspaceIfNotExists(name string, KeyspaceSchema *vindexes.KeyspaceSchema) {
e.mu.Lock()
defer e.mu.Unlock()
if _, ok := e.vschema.Keyspaces[name]; !ok {
e.vschema.Keyspaces[name] = KeyspaceSchema
}
}

func (e *Executor) VSchemaDeleteKeyspace(name string) {
e.mu.Lock()
defer e.mu.Unlock()
delete(e.vschema.Keyspaces, name)
}

// SaveVSchema updates the vschema and stats
func (e *Executor) SaveVSchema(vschema *vindexes.VSchema, stats *VSchemaStats) {
e.mu.Lock()
Expand Down
10 changes: 10 additions & 0 deletions go/vt/vtgate/vcursor_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type iExecute interface {
ParseDestinationTarget(targetString string) (string, topodatapb.TabletType, key.Destination, error)
reloadExec(ctx context.Context, reloadType *sqlparser.ReloadType) error
VSchema() *vindexes.VSchema
VSchemaAddKeyspaceIfNotExists(name string, KeyspaceSchema *vindexes.KeyspaceSchema)
VSchemaDeleteKeyspace(name string)
SetFailPoint(command string, key string, value string) error
SubmitDMLJob(command, sql, uuid, tableSchema, timePeriodStart, timePeriodEnd, timePeriodTimeZone string, timeGapInMs, batchSize int64, postponeLaunch bool, failPolicy, throttleDuration, throttleRatio string) (*sqltypes.Result, error)
ShowDMLJob(uuid string, showDetail bool) (*sqltypes.Result, error)
Expand Down Expand Up @@ -1220,6 +1222,14 @@ func (vc *vcursorImpl) GetExecutorVSchema() *vindexes.VSchema {
return vc.executor.VSchema()
}

func (vc *vcursorImpl) VSchemaAddKeyspaceIfNotExists(name string, KeyspaceSchema *vindexes.KeyspaceSchema) {
vc.executor.VSchemaAddKeyspaceIfNotExists(name, KeyspaceSchema)
}

func (vc *vcursorImpl) VSchemaDeleteKeyspace(name string) {
vc.executor.VSchemaDeleteKeyspace(name)
}

func (vc *vcursorImpl) GetVSchema() *vindexes.VSchema {
return vc.vschema
}
Expand Down

0 comments on commit b020b41

Please sign in to comment.