Skip to content

Commit c91a8f5

Browse files
MinatoWuCode-Fight
andauthored
Fix : del metadatacache uppertablenamekey and increase tablemeta field uppertablename (#857)
* fix: uppertable metacache todo:Fix lockkey case difference #846 * increase uppertablename and Fix lockkey case difference #846 * Fix test case difference #846 * Fix test case difference #846 * reduce uppertablename col #846 * reduce uppertablename #846 * fix test #846 --------- Co-authored-by: FengZhang <[email protected]>
1 parent 7139e4e commit c91a8f5

File tree

7 files changed

+65
-38
lines changed

7 files changed

+65
-38
lines changed

pkg/datasource/sql/datasource/base/meta_cache.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"context"
2222
"database/sql"
2323
"fmt"
24-
"strings"
2524
"sync"
2625
"time"
2726

@@ -111,9 +110,8 @@ func (c *BaseTableMetaCache) refresh(ctx context.Context) {
111110

112111
for i := range v {
113112
tm := v[i]
114-
upperTableName := strings.ToUpper(tm.TableName)
115-
if _, ok := c.cache[upperTableName]; ok {
116-
c.cache[upperTableName] = &entry{
113+
if _, ok := c.cache[tm.TableName]; ok {
114+
c.cache[tm.TableName] = &entry{
117115
value: tm,
118116
}
119117
}
@@ -159,16 +157,15 @@ func (c *BaseTableMetaCache) GetTableMeta(ctx context.Context, dbName, tableName
159157
defer c.lock.Unlock()
160158

161159
defer conn.Close()
162-
upperTableName := strings.ToUpper(tableName)
163-
v, ok := c.cache[upperTableName]
160+
v, ok := c.cache[tableName]
164161
if !ok {
165-
meta, err := c.trigger.LoadOne(ctx, dbName, upperTableName, conn)
162+
meta, err := c.trigger.LoadOne(ctx, dbName, tableName, conn)
166163
if err != nil {
167164
return types.TableMeta{}, err
168165
}
169166

170167
if meta != nil && !meta.IsEmpty() {
171-
c.cache[upperTableName] = &entry{
168+
c.cache[tableName] = &entry{
172169
value: *meta,
173170
lastAccess: time.Now(),
174171
}
@@ -180,7 +177,7 @@ func (c *BaseTableMetaCache) GetTableMeta(ctx context.Context, dbName, tableName
180177
}
181178

182179
v.lastAccess = time.Now()
183-
c.cache[upperTableName] = v
180+
c.cache[tableName] = v
184181

185182
return v.value, nil
186183
}

pkg/datasource/sql/datasource/base/meta_cache_test.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,29 @@ func TestBaseTableMetaCache_refresh(t *testing.T) {
9393
args args
9494
want types.TableMeta
9595
}{
96-
{name: "test-1",
96+
{
97+
name: "test1",
98+
fields: fields{
99+
lock: sync.RWMutex{},
100+
capity: capacity,
101+
size: 0,
102+
expireDuration: EexpireTime,
103+
cache: map[string]*entry{
104+
"test": {
105+
value: types.TableMeta{},
106+
lastAccess: time.Now(),
107+
},
108+
},
109+
cancel: cancel,
110+
trigger: &mockTrigger{},
111+
cfg: &mysql.Config{},
112+
db: &sql.DB{},
113+
},
114+
args: args{ctx: ctx},
115+
want: testdata.MockWantTypesMeta("test"),
116+
},
117+
{
118+
name: "test2",
97119
fields: fields{
98120
lock: sync.RWMutex{},
99121
capity: capacity,
@@ -109,8 +131,10 @@ func TestBaseTableMetaCache_refresh(t *testing.T) {
109131
trigger: &mockTrigger{},
110132
cfg: &mysql.Config{},
111133
db: &sql.DB{},
112-
}, args: args{ctx: ctx},
113-
want: testdata.MockWantTypesMeta("test")},
134+
},
135+
args: args{ctx: ctx},
136+
want: testdata.MockWantTypesMeta("TEST"),
137+
},
114138
}
115139
for _, tt := range tests {
116140
t.Run(tt.name, func(t *testing.T) {
@@ -144,7 +168,12 @@ func TestBaseTableMetaCache_refresh(t *testing.T) {
144168
time.Sleep(time.Second * 3)
145169
c.lock.RLock()
146170
defer c.lock.RUnlock()
147-
assert.Equal(t, c.cache["TEST"].value, tt.want)
171+
assert.Equal(t, c.cache[func() string {
172+
if tt.name == "test2" {
173+
return "TEST"
174+
}
175+
return "test"
176+
}()].value, tt.want)
148177
})
149178
}
150179
}
@@ -190,7 +219,7 @@ func TestBaseTableMetaCache_GetTableMeta(t *testing.T) {
190219

191220
ColumnNames = []string{"id", "name", "age"}
192221
tableMeta1 = types.TableMeta{
193-
TableName: "T_USER1",
222+
TableName: "t_user1",
194223
Columns: columns,
195224
Indexs: index,
196225
ColumnNames: ColumnNames,
@@ -229,12 +258,12 @@ func TestBaseTableMetaCache_GetTableMeta(t *testing.T) {
229258
cache := &BaseTableMetaCache{
230259
trigger: mockTrigger,
231260
cache: map[string]*entry{
232-
"T_USER": {
233-
value: tableMeta2,
261+
"t_user1": {
262+
value: tableMeta1,
234263
lastAccess: time.Now(),
235264
},
236-
"T_USER1": {
237-
value: tableMeta1,
265+
"T_USER2": {
266+
value: tableMeta2,
238267
lastAccess: time.Now(),
239268
},
240269
},

pkg/datasource/sql/exec/at/base_executor_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestBaseExecBuildLockKey(t *testing.T) {
7171
{[]types.ColumnImage{getColumnImage("id", 2), getColumnImage("userId", "user2")}},
7272
},
7373
},
74-
"test_name:1_user1,2_user2",
74+
"TEST_NAME:1_user1,2_user2",
7575
},
7676
{
7777
"Three Primary Keys",
@@ -89,7 +89,7 @@ func TestBaseExecBuildLockKey(t *testing.T) {
8989
{[]types.ColumnImage{getColumnImage("id", 3), getColumnImage("userId", "three"), getColumnImage("age", "33")}},
9090
},
9191
},
92-
"test2_name:1_one_11,2_two_22,3_three_33",
92+
"TEST2_NAME:1_one_11,2_two_22,3_three_33",
9393
},
9494
{
9595
name: "Single Primary Key",
@@ -105,7 +105,7 @@ func TestBaseExecBuildLockKey(t *testing.T) {
105105
{Columns: []types.ColumnImage{getColumnImage("id", 100)}},
106106
},
107107
},
108-
expected: "single_key:100",
108+
expected: "SINGLE_KEY:100",
109109
},
110110
{
111111
name: "Mixed Type Keys",
@@ -121,7 +121,7 @@ func TestBaseExecBuildLockKey(t *testing.T) {
121121
{Columns: []types.ColumnImage{getColumnImage("name", "mike"), getColumnImage("age", 25)}},
122122
},
123123
},
124-
expected: "mixed_key:mike_25",
124+
expected: "MIXED_KEY:mike_25",
125125
},
126126
{
127127
name: "Empty Records",
@@ -132,7 +132,7 @@ func TestBaseExecBuildLockKey(t *testing.T) {
132132
},
133133
},
134134
records: types.RecordImage{TableName: "empty"},
135-
expected: "empty:",
135+
expected: "EMPTY:",
136136
},
137137
{
138138
name: "Special Characters",
@@ -148,7 +148,7 @@ func TestBaseExecBuildLockKey(t *testing.T) {
148148
{Columns: []types.ColumnImage{getColumnImage("id", "A,b_c")}},
149149
},
150150
},
151-
expected: "special:A,b_c",
151+
expected: "SPECIAL:A,b_c",
152152
},
153153
{
154154
name: "Non-existent Key Name",
@@ -164,7 +164,7 @@ func TestBaseExecBuildLockKey(t *testing.T) {
164164
{Columns: []types.ColumnImage{getColumnImage("id", 1)}},
165165
},
166166
},
167-
expected: "error_key:",
167+
expected: "ERROR_KEY:",
168168
},
169169
{
170170
name: "Multiple Rows With Nil PK Value",
@@ -182,7 +182,7 @@ func TestBaseExecBuildLockKey(t *testing.T) {
182182
{Columns: []types.ColumnImage{getColumnImage("id", nil)}},
183183
},
184184
},
185-
expected: "nil_pk:,123,",
185+
expected: "NIL_PK:,123,",
186186
},
187187
{
188188
name: "PK As Bool And Float",
@@ -199,10 +199,9 @@ func TestBaseExecBuildLockKey(t *testing.T) {
199199
{Columns: []types.ColumnImage{getColumnImage("name", false), getColumnImage("age", 0.0)}},
200200
},
201201
},
202-
expected: "type_pk:true_3.14,false_0",
202+
expected: "TYPE_PK:true_3.14,false_0",
203203
},
204204
}
205-
206205
for _, tt := range tests {
207206
t.Run(tt.name, func(t *testing.T) {
208207
lockKeys := exec.buildLockKey(&tt.records, tt.metaData)

pkg/datasource/sql/undo/builder/basic_undo_log_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func (b *BasicUndoLogBuilder) buildLockKey(rows driver.Rows, meta types.TableMet
247247
lockKeys bytes.Buffer
248248
filedSequence int
249249
)
250-
lockKeys.WriteString(meta.TableName)
250+
lockKeys.WriteString(strings.ToUpper(meta.TableName))
251251
lockKeys.WriteString(":")
252252

253253
pks := b.GetScanSlice(meta.GetPrimaryKeyOnlyName(), &meta)

pkg/datasource/sql/undo/builder/basic_undo_log_builder_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestBuildLockKey(t *testing.T) {
9797
{[]types.ColumnImage{getColumnImage("id", 2), getColumnImage("userId", "two")}},
9898
},
9999
},
100-
"test_name:1_one,2_two",
100+
"TEST_NAME:1_one,2_two",
101101
},
102102
{
103103
"Three Primary Keys",
@@ -115,7 +115,7 @@ func TestBuildLockKey(t *testing.T) {
115115
{[]types.ColumnImage{getColumnImage("id", 3), getColumnImage("userId", "three"), getColumnImage("age", "33")}},
116116
},
117117
},
118-
"test2_name:1_one_11,2_two_22,3_three_33",
118+
"TEST2_NAME:1_one_11,2_two_22,3_three_33",
119119
},
120120
{
121121
name: "Single Primary Key",
@@ -131,7 +131,7 @@ func TestBuildLockKey(t *testing.T) {
131131
{Columns: []types.ColumnImage{getColumnImage("id", 100)}},
132132
},
133133
},
134-
expected: "single_key:100",
134+
expected: "SINGLE_KEY:100",
135135
},
136136
{
137137
name: "Mixed Type Keys",
@@ -147,7 +147,7 @@ func TestBuildLockKey(t *testing.T) {
147147
{Columns: []types.ColumnImage{getColumnImage("name", "Alice"), getColumnImage("age", 25)}},
148148
},
149149
},
150-
expected: "mixed_key:Alice_25",
150+
expected: "MIXED_KEY:Alice_25",
151151
},
152152
{
153153
name: "Empty Records",
@@ -158,7 +158,7 @@ func TestBuildLockKey(t *testing.T) {
158158
},
159159
},
160160
records: types.RecordImage{TableName: "empty"},
161-
expected: "empty:",
161+
expected: "EMPTY:",
162162
},
163163
{
164164
name: "Special Characters",
@@ -174,7 +174,7 @@ func TestBuildLockKey(t *testing.T) {
174174
{Columns: []types.ColumnImage{getColumnImage("id", "a,b_c")}},
175175
},
176176
},
177-
expected: "special:a,b_c",
177+
expected: "SPECIAL:a,b_c",
178178
},
179179
{
180180
name: "Non-existent Key Name",
@@ -190,7 +190,7 @@ func TestBuildLockKey(t *testing.T) {
190190
{Columns: []types.ColumnImage{getColumnImage("id", 1)}},
191191
},
192192
},
193-
expected: "error_key:",
193+
expected: "ERROR_KEY:",
194194
},
195195
}
196196

pkg/datasource/sql/util/lockkey.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func BuildLockKey(records *types.RecordImage, meta types.TableMeta) string {
3434
colIndex int
3535
}
3636

37-
lockKeys.WriteString(meta.TableName)
37+
lockKeys.WriteString(strings.ToUpper(meta.TableName))
3838
lockKeys.WriteString(":")
3939

4040
keys := meta.GetPrimaryKeyOnlyName()

testdata/meta_cache.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
package testdata
1919

20-
import "seata.apache.org/seata-go/pkg/datasource/sql/types"
20+
import (
21+
"seata.apache.org/seata-go/pkg/datasource/sql/types"
22+
)
2123

2224
func MockWantTypesMeta(tableName string) types.TableMeta {
2325
return types.TableMeta{

0 commit comments

Comments
 (0)