-
Notifications
You must be signed in to change notification settings - Fork 286
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
fix: apache/incubator-seata#6678 #718
Open
jay-li-csck
wants to merge
5
commits into
apache:master
Choose a base branch
from
jay-li-csck:fix_tablename
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
"context" | ||
"database/sql" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -92,8 +93,9 @@ func (c *BaseTableMetaCache) refresh(ctx context.Context) { | |
|
||
for i := range v { | ||
tm := v[i] | ||
if _, ok := c.cache[tm.TableName]; !ok { | ||
c.cache[tm.TableName] = &entry{ | ||
var upperTableName = strings.ToUpper(tm.TableName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个ToUpper仅在refresh的加不行,需要在GetTableMeta的时,保证都是一致的 |
||
if _, ok := c.cache[upperTableName]; !ok { | ||
c.cache[upperTableName] = &entry{ | ||
value: tm, | ||
} | ||
} | ||
|
@@ -140,15 +142,17 @@ func (c *BaseTableMetaCache) GetTableMeta(ctx context.Context, dbName, tableName | |
|
||
defer conn.Close() | ||
|
||
v, ok := c.cache[tableName] | ||
var upperTableName = strings.ToUpper(tableName) | ||
|
||
v, ok := c.cache[upperTableName] | ||
if !ok { | ||
meta, err := c.trigger.LoadOne(ctx, dbName, tableName, conn) | ||
meta, err := c.trigger.LoadOne(ctx, dbName, upperTableName, conn) | ||
if err != nil { | ||
return types.TableMeta{}, err | ||
} | ||
|
||
if meta != nil && !meta.IsEmpty() { | ||
c.cache[tableName] = &entry{ | ||
c.cache[upperTableName] = &entry{ | ||
value: *meta, | ||
lastAccess: time.Now(), | ||
} | ||
|
@@ -160,7 +164,7 @@ func (c *BaseTableMetaCache) GetTableMeta(ctx context.Context, dbName, tableName | |
} | ||
|
||
v.lastAccess = time.Now() | ||
c.cache[tableName] = v | ||
c.cache[upperTableName] = v | ||
|
||
return v.value, nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 base | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"sync" | ||
|
||
"seata.apache.org/seata-go/pkg/datasource/sql/types" | ||
|
||
"testing" | ||
"time" | ||
) | ||
|
||
type mockTrigger struct { | ||
data []types.TableMeta | ||
err error | ||
} | ||
|
||
func (mt *mockTrigger) LoadAll() ([]types.TableMeta, error) { | ||
return mt.data, mt.err | ||
} | ||
|
||
func (mt *mockTrigger) LoadOne(ctx context.Context, dbName string, table string, conn *sql.Conn) (*types.TableMeta, error) { | ||
if mt.err != nil { | ||
return nil, mt.err | ||
} | ||
for _, meta := range mt.data { | ||
if meta.TableName == table { | ||
return &meta, nil | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func TestBaseTableMetaCache_refresh(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
trigger *mockTrigger | ||
cache map[string]*entry | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "正常情况", | ||
trigger: &mockTrigger{ | ||
data: []types.TableMeta{ | ||
{TableName: "test1"}, | ||
{TableName: "test2"}, | ||
}, | ||
err: nil, | ||
}, | ||
cache: make(map[string]*entry), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "缓存中已存在", | ||
trigger: &mockTrigger{ | ||
data: []types.TableMeta{ | ||
{TableName: "test1"}, | ||
}, | ||
err: nil, | ||
}, | ||
cache: map[string]*entry{ | ||
"TEST1": {value: types.TableMeta{TableName: "test1"}}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := &BaseTableMetaCache{ | ||
trigger: tt.trigger, | ||
cache: tt.cache, | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) | ||
defer cancel() | ||
|
||
go c.refresh(ctx) | ||
|
||
time.Sleep(1 * time.Second) | ||
|
||
c.lock.Lock() | ||
if len(c.cache) != len(tt.trigger.data) { | ||
t.Errorf("cache size = %v, want %v", len(c.cache), len(tt.trigger.data)) | ||
} | ||
c.lock.Unlock() | ||
}) | ||
} | ||
} | ||
|
||
func TestBaseTableMetaCache_GetTableMeta(t *testing.T) { | ||
var ( | ||
tableMeta1 types.TableMeta | ||
tableMeta2 types.TableMeta | ||
columns = make(map[string]types.ColumnMeta) | ||
index = make(map[string]types.IndexMeta) | ||
index2 = make(map[string]types.IndexMeta) | ||
columnMeta1 []types.ColumnMeta | ||
columnMeta2 []types.ColumnMeta | ||
ColumnNames []string | ||
) | ||
columnId := types.ColumnMeta{ | ||
ColumnDef: nil, | ||
ColumnName: "id", | ||
} | ||
columnName := types.ColumnMeta{ | ||
ColumnDef: nil, | ||
ColumnName: "name", | ||
} | ||
columnAge := types.ColumnMeta{ | ||
ColumnDef: nil, | ||
ColumnName: "age", | ||
} | ||
columns["id"] = columnId | ||
columns["name"] = columnName | ||
columns["age"] = columnAge | ||
columnMeta1 = append(columnMeta1, columnId) | ||
columnMeta2 = append(columnMeta2, columnName, columnAge) | ||
index["id"] = types.IndexMeta{ | ||
Name: "PRIMARY", | ||
IType: types.IndexTypePrimaryKey, | ||
Columns: columnMeta1, | ||
} | ||
index["id_name_age"] = types.IndexMeta{ | ||
Name: "name_age_idx", | ||
IType: types.IndexUnique, | ||
Columns: columnMeta2, | ||
} | ||
|
||
ColumnNames = []string{"id", "name", "age"} | ||
tableMeta1 = types.TableMeta{ | ||
TableName: "T_USER1", | ||
Columns: columns, | ||
Indexs: index, | ||
ColumnNames: ColumnNames, | ||
} | ||
|
||
index2["id_name_age"] = types.IndexMeta{ | ||
Name: "name_age_idx", | ||
IType: types.IndexUnique, | ||
Columns: columnMeta2, | ||
} | ||
|
||
tableMeta2 = types.TableMeta{ | ||
TableName: "T_USER2", | ||
Columns: columns, | ||
Indexs: index2, | ||
ColumnNames: ColumnNames, | ||
} | ||
tests := []types.TableMeta{tableMeta1, tableMeta2} | ||
for _, tt := range tests { | ||
t.Run(tt.TableName, func(t *testing.T) { | ||
mockTrigger := &mockTrigger{ | ||
data: tests, | ||
err: nil, | ||
} | ||
|
||
cache := &BaseTableMetaCache{ | ||
trigger: mockTrigger, | ||
cache: map[string]*entry{ | ||
"T_USER": { | ||
value: tableMeta2, | ||
lastAccess: time.Now(), | ||
}, | ||
"T_USER1": { | ||
value: tableMeta1, | ||
lastAccess: time.Now(), | ||
}, | ||
}, | ||
lock: sync.RWMutex{}, | ||
} | ||
mockConn := &sql.Conn{} | ||
|
||
meta, _ := cache.GetTableMeta(context.Background(), "db", tt.TableName, mockConn) | ||
|
||
if meta.TableName != tt.TableName { | ||
t.Errorf("GetTableMeta() got TableName = %v, want %v", meta.TableName, tt.TableName) | ||
} | ||
|
||
cache.lock.RLock() | ||
_, cached := cache.cache[tt.TableName] | ||
cache.lock.RUnlock() | ||
|
||
if !cached { | ||
t.Errorf("GetTableMeta() got TableName = %v, want %v", meta.TableName, tt.TableName) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
问题不大,补一个单测