Skip to content

Commit

Permalink
Add RelationExists interface to txnDatabase 2.0 (#20403)
Browse files Browse the repository at this point in the history
- Add RelationExists to prevent too many logs like table not found

Approved by: @daviszhen, @XuPeng-SH, @m-schen, @qingxinhome, @reusee, @triump2020, @sukki37
  • Loading branch information
aptend authored Nov 28, 2024
1 parent 1f3926e commit 02ab661
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 9 deletions.
15 changes: 15 additions & 0 deletions pkg/frontend/test/engine_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/testutil/testengine/testengine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (

"github.com/matrixorigin/matrixone/pkg/catalog"
"github.com/matrixorigin/matrixone/pkg/common/runtime"
"github.com/matrixorigin/matrixone/pkg/defines"
"github.com/stretchr/testify/require"
)

func TestTestEngine(t *testing.T) {
Expand All @@ -40,3 +42,20 @@ func BenchmarkNew(b *testing.B) {
New(context.Background())
}
}

func TestRelationExists(t *testing.T) {
catalog.SetupDefines("")
ctx := context.Background()
ctx = defines.AttachAccountId(ctx, 0)
eng, _, _ := New(ctx)
db, err := eng.Database(ctx, "test", nil)
require.NoError(t, err)

exist, err := db.RelationExists(ctx, "NotExist", nil)
require.NoError(t, err)
require.False(t, exist)

exist, err = db.RelationExists(ctx, "r", nil)
require.NoError(t, err)
require.True(t, exist)
}
40 changes: 32 additions & 8 deletions pkg/vm/engine/disttae/txn_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (db *txnDatabase) Relations(ctx context.Context) ([]string, error) {
return rels, nil
}

func (db *txnDatabase) Relation(ctx context.Context, name string, proc any) (engine.Relation, error) {
func (db *txnDatabase) relation(ctx context.Context, name string, proc any) (engine.Relation, error) {
common.DoIfDebugEnabled(func() {
logutil.Debug(
"Transaction.Relation",
Expand Down Expand Up @@ -117,7 +117,8 @@ func (db *txnDatabase) Relation(ctx context.Context, name string, proc any) (eng

// check the table is deleted or not
if !openSys && txn.tableOps.existAndDeleted(key) {
return nil, moerr.NewParseErrorf(ctx, "table %q does not exist", name)
logutil.Info("[relation] deleted in txn", zap.String("table", name))
return nil, nil
}

// get relation from the txn created tables cache: created by this txn
Expand All @@ -143,10 +144,13 @@ func (db *txnDatabase) Relation(ctx context.Context, name string, proc any) (eng
if err != nil {
return nil, err
}
if item == nil {
return nil, nil
}

tbl, err := newTxnTable(
db,
item,
*item,
p,
shardservice.GetService(p.GetService()),
txn.engine,
Expand All @@ -159,6 +163,25 @@ func (db *txnDatabase) Relation(ctx context.Context, name string, proc any) (eng
return tbl, nil
}

func (db *txnDatabase) Relation(ctx context.Context, name string, proc any) (engine.Relation, error) {
rel, err := db.relation(ctx, name, proc)
if err != nil {
return nil, err
}
if rel == nil {
return nil, moerr.NewParseErrorf(ctx, "table %q does not exist", name)
}
return rel, nil
}

func (db *txnDatabase) RelationExists(ctx context.Context, name string, proc any) (bool, error) {
rel, err := db.relation(ctx, name, proc)
if err != nil {
return false, err
}
return rel != nil, nil
}

func (db *txnDatabase) Delete(ctx context.Context, name string) error {
_, err := db.deleteTable(ctx, name, false, false)
return err
Expand Down Expand Up @@ -584,7 +607,7 @@ func (db *txnDatabase) getTableItem(
accountID uint32,
name string,
engine *Engine,
) (cache.TableItem, error) {
) (*cache.TableItem, error) {
item := cache.TableItem{
Name: name,
DatabaseId: db.databaseId,
Expand All @@ -596,14 +619,15 @@ func (db *txnDatabase) getTableItem(
if ok := c.GetTable(&item); !ok {
var tableitem *cache.TableItem
if !c.CanServe(types.TimestampToTS(db.op.SnapshotTS())) {
logutil.Info("FIND_TABLE getTableItem cache cannot serve", zap.String("table", name), zap.Uint32("accountID", accountID), zap.String("timestamp", db.op.SnapshotTS().DebugString()))
if tableitem, err = db.loadTableFromStorage(ctx, accountID, name); err != nil {
return cache.TableItem{}, err
return nil, err
}
}
if tableitem == nil {
return cache.TableItem{}, moerr.NewParseErrorf(ctx, "table %q does not exist", name)
return nil, nil
}
return *tableitem, nil
return tableitem, nil
}
return item, nil
return &item, nil
}
5 changes: 4 additions & 1 deletion pkg/vm/engine/disttae/txn_table_sharding_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,10 +681,13 @@ func getTxnTable(
if err != nil {
return nil, err
}
if item == nil {
return nil, moerr.NewParseErrorf(ctx, "table %q does not exist", param.TxnTable.TableName)
}

tbl := newTxnTableWithItem(
db,
item,
*item,
proc,
engine.(*Engine),
)
Expand Down
11 changes: 11 additions & 0 deletions pkg/vm/engine/memoryengine/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ func (d *Database) Relation(ctx context.Context, relName string, _ any) (engine.
}

}
func (d *Database) RelationExists(ctx context.Context, relName string, _ any) (bool, error) {
rel, err := d.Relation(ctx, relName, nil)
if err != nil {
if moerr.IsMoErrCode(err, moerr.ErrNoSuchTable) {
return false, nil
} else {
return false, err
}
}
return rel != nil, nil
}

func (d *Database) Relations(ctx context.Context) ([]string, error) {

Expand Down
31 changes: 31 additions & 0 deletions pkg/vm/engine/test/disttae_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,3 +1207,34 @@ func TestCache3Tables(t *testing.T) {
require.Equal(t, catalog.MO_COLUMNS, tname)
require.NotNil(t, rel)
}

func TestRelationExists(t *testing.T) {
opts := config.WithLongScanAndCKPOpts(nil)
p := testutil.InitEnginePack(testutil.TestOptions{TaeEngineOptions: opts}, t)
defer p.Close()
txnop := p.StartCNTxn()
schema := catalog2.MockSchemaAll(2, 0)
schema.Name = "test"
p.CreateDBAndTable(txnop, "db", schema)

db, err := p.D.Engine.Database(p.Ctx, "db", txnop)
require.NoError(t, err)

exist, err := db.RelationExists(p.Ctx, "test", nil)
require.NoError(t, err)
require.True(t, exist)

exist, err = db.RelationExists(p.Ctx, "testxx", nil)
require.NoError(t, err)
require.False(t, exist)

// craft no-account-id error
ctx := context.WithValue(p.Ctx, defines.TenantIDKey{}, nil)
exist, err = db.RelationExists(ctx, "testxx", nil)
require.Error(t, err)
require.False(t, exist)

rel, err := db.Relation(ctx, "test", nil)
require.Error(t, err)
require.Nil(t, rel)
}
1 change: 1 addition & 0 deletions pkg/vm/engine/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ type Reader interface {
type Database interface {
Relations(context.Context) ([]string, error)
Relation(context.Context, string, any) (Relation, error)
RelationExists(context.Context, string, any) (bool, error)

Delete(context.Context, string) error
Create(context.Context, string, []TableDef) error // Create Table - (name, table define)
Expand Down

0 comments on commit 02ab661

Please sign in to comment.