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

infoschema: add a simply store for DM's SchemaTracker #35954

Merged
merged 10 commits into from
Jul 6, 2022
155 changes: 155 additions & 0 deletions ddl/dm_schema_tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright 2022 PingCAP, Inc.
//
// 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 ddl

import (
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
)

// infoStore is a simple structure that stores DBInfo and TableInfo. It's not thread-safe.
type infoStore struct {
lowerCaseTableNames int // same as variable lower_case_table_names
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that this is not needed at present. Can you remove it?

Copy link
Contributor Author

@lance6716 lance6716 Jul 6, 2022

Choose a reason for hiding this comment

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

it's needed in ciStr2Key. In DM's use case, upstream is MySQL so we should set lowerCaseTableNames to MySQL's value.

Copy link
Contributor

@zimulala zimulala Jul 6, 2022

Choose a reason for hiding this comment

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

But I see lower_case_table_names is read-only.

Copy link
Contributor Author

@lance6716 lance6716 Jul 6, 2022

Choose a reason for hiding this comment

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

Sorry, in the big PR #35867, I use struct literal to build infoStore in this package. I can write a newInfoStore.

newInfoStore is already here. The usage is

// NewSchemaTracker creates a SchemaTracker. lowerCaseTableNames has the same meaning as MySQL variable lower_case_table_names.
func NewSchemaTracker(lowerCaseTableNames int) SchemaTracker {
	return SchemaTracker{
		infoStore: newInfoStore(lowerCaseTableNames),
	}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it. DM needs use it


dbs map[string]*model.DBInfo
tables map[string]map[string]*model.TableInfo
}

func newInfoStore(lowerCaseTableNames int) *infoStore {
return &infoStore{
lowerCaseTableNames: lowerCaseTableNames,
dbs: map[string]*model.DBInfo{},
tables: map[string]map[string]*model.TableInfo{},
}
}

func (i *infoStore) ciStr2Key(name model.CIStr) string {
if i.lowerCaseTableNames == 0 {
return name.O
}
return name.L
}

// SchemaByName is exported to be used when infoStore is embedded into another public struct.
func (i *infoStore) SchemaByName(name model.CIStr) *model.DBInfo {
key := i.ciStr2Key(name)
return i.dbs[key]
}

func (i *infoStore) putSchema(dbInfo *model.DBInfo) {
key := i.ciStr2Key(dbInfo.Name)
i.dbs[key] = dbInfo
if i.tables[key] == nil {
i.tables[key] = map[string]*model.TableInfo{}
}
}

func (i *infoStore) deleteSchema(name model.CIStr) bool {
key := i.ciStr2Key(name)
_, ok := i.dbs[key]
if !ok {
return false
}
delete(i.dbs, key)
delete(i.tables, key)
return true
}

// TableByName is exported to be used when infoStore is embedded into another public struct.
func (i *infoStore) TableByName(schema, table model.CIStr) (*model.TableInfo, error) {
schemaKey := i.ciStr2Key(schema)
tables, ok := i.tables[schemaKey]
if !ok {
return nil, infoschema.ErrDatabaseNotExists.GenWithStackByArgs(schema)
}

tableKey := i.ciStr2Key(table)
tbl, ok := tables[tableKey]
if !ok {
return nil, infoschema.ErrTableNotExists.GenWithStackByArgs(schema, table)
}
return tbl, nil
}

func (i *infoStore) putTable(schemaName model.CIStr, tblInfo *model.TableInfo) error {
schemaKey := i.ciStr2Key(schemaName)
tables, ok := i.tables[schemaKey]
if !ok {
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(schemaName)
}
tableKey := i.ciStr2Key(tblInfo.Name)
tables[tableKey] = tblInfo
return nil
}

func (i *infoStore) deleteTable(schema, table model.CIStr) error {
schemaKey := i.ciStr2Key(schema)
tables, ok := i.tables[schemaKey]
if !ok {
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(schema)
}

tableKey := i.ciStr2Key(table)
_, ok = tables[tableKey]
if !ok {
return infoschema.ErrTableNotExists.GenWithStackByArgs(schema, table)
}
delete(tables, tableKey)
return nil
}

// infoSchemaAdaptor convert infoStore to infoschema.InfoSchema, it only implements a part of InfoSchema interface to be
// used by DDL interface.
// nolint:unused
zimulala marked this conversation as resolved.
Show resolved Hide resolved
type infoSchemaAdaptor struct {
infoschema.InfoSchema
inner *infoStore
}

// SchemaByName implements the InfoSchema interface.
// nolint:unused
func (i infoSchemaAdaptor) SchemaByName(schema model.CIStr) (*model.DBInfo, bool) {
dbInfo := i.inner.SchemaByName(schema)
return dbInfo, dbInfo != nil
}

// TableExists implements the InfoSchema interface.
// nolint:unused
func (i infoSchemaAdaptor) TableExists(schema, table model.CIStr) bool {
tableInfo, _ := i.inner.TableByName(schema, table)
return tableInfo != nil
}

// TableIsView implements the InfoSchema interface.
// nolint:unused
func (i infoSchemaAdaptor) TableIsView(schema, table model.CIStr) bool {
tableInfo, _ := i.inner.TableByName(schema, table)
if tableInfo == nil {
return false
}
return tableInfo.IsView()
}

// TableByName implements the InfoSchema interface.
// nolint:unused
func (i infoSchemaAdaptor) TableByName(schema, table model.CIStr) (t table.Table, err error) {
tableInfo, err := i.inner.TableByName(schema, table)
if err != nil {
return nil, err
}
return tables.MockTableFromMeta(tableInfo), nil
}
116 changes: 116 additions & 0 deletions ddl/dm_schema_tracker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright 2022 PingCAP, Inc.
//
// 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 ddl

import (
"testing"

"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser/model"
"github.com/stretchr/testify/require"
)

func TestInfoStoreLowerCaseTableNames(t *testing.T) {
dbName := model.NewCIStr("DBName")
lowerDBName := model.NewCIStr("dbname")
tableName := model.NewCIStr("TableName")
lowerTableName := model.NewCIStr("tablename")
dbInfo := &model.DBInfo{Name: dbName}
tableInfo := &model.TableInfo{Name: tableName}

// case-sensitive

is := newInfoStore(0)
is.putSchema(dbInfo)
got := is.SchemaByName(dbName)
require.NotNil(t, got)
got = is.SchemaByName(lowerDBName)
require.Nil(t, got)

err := is.putTable(lowerDBName, tableInfo)
require.True(t, infoschema.ErrDatabaseNotExists.Equal(err))
err = is.putTable(dbName, tableInfo)
require.NoError(t, err)
got2, err := is.TableByName(dbName, tableName)
require.NoError(t, err)
require.NotNil(t, got2)
got2, err = is.TableByName(lowerTableName, tableName)
require.True(t, infoschema.ErrDatabaseNotExists.Equal(err))
require.Nil(t, got2)
got2, err = is.TableByName(dbName, lowerTableName)
require.True(t, infoschema.ErrTableNotExists.Equal(err))
require.Nil(t, got2)

// compare-insensitive

is = newInfoStore(2)
is.putSchema(dbInfo)
got = is.SchemaByName(dbName)
require.NotNil(t, got)
got = is.SchemaByName(lowerDBName)
require.NotNil(t, got)
require.Equal(t, dbName, got.Name)

err = is.putTable(lowerDBName, tableInfo)
require.NoError(t, err)
got2, err = is.TableByName(dbName, tableName)
require.NoError(t, err)
require.NotNil(t, got2)
got2, err = is.TableByName(dbName, lowerTableName)
require.NoError(t, err)
require.NotNil(t, got2)
require.Equal(t, tableName, got2.Name)
}

func TestInfoStoreDeleteTables(t *testing.T) {
is := newInfoStore(0)
dbName1 := model.NewCIStr("DBName1")
dbName2 := model.NewCIStr("DBName2")
tableName1 := model.NewCIStr("TableName1")
tableName2 := model.NewCIStr("TableName2")
dbInfo1 := &model.DBInfo{Name: dbName1}
dbInfo2 := &model.DBInfo{Name: dbName2}
tableInfo1 := &model.TableInfo{Name: tableName1}
tableInfo2 := &model.TableInfo{Name: tableName2}

is.putSchema(dbInfo1)
err := is.putTable(dbName1, tableInfo1)
require.NoError(t, err)
err = is.putTable(dbName1, tableInfo2)
require.NoError(t, err)

// db2 not created
ok := is.deleteSchema(dbName2)
require.False(t, ok)
err = is.putTable(dbName2, tableInfo1)
require.True(t, infoschema.ErrDatabaseNotExists.Equal(err))
err = is.deleteTable(dbName2, tableName1)
require.True(t, infoschema.ErrDatabaseNotExists.Equal(err))

is.putSchema(dbInfo2)
err = is.putTable(dbName2, tableInfo1)
require.NoError(t, err)

err = is.deleteTable(dbName2, tableName2)
require.True(t, infoschema.ErrTableNotExists.Equal(err))
err = is.deleteTable(dbName2, tableName1)
require.NoError(t, err)

// delete db will remove its tables
ok = is.deleteSchema(dbName1)
require.True(t, ok)
_, err = is.TableByName(dbName1, tableName1)
require.True(t, infoschema.ErrDatabaseNotExists.Equal(err))
}