From 5849af54d0aa3f052e0961f71667ad8f9a5ce7e9 Mon Sep 17 00:00:00 2001 From: Suhaha Date: Thu, 19 May 2022 09:17:05 +0800 Subject: [PATCH 1/7] fix(keyviz): find nearest id if not found --- pkg/keyvisual/decorator/tidb.go | 122 ++++++++++++++++++++--- pkg/keyvisual/decorator/tidb_requests.go | 2 + pkg/keyvisual/decorator/tidb_test.go | 38 +++++++ 3 files changed, 148 insertions(+), 14 deletions(-) diff --git a/pkg/keyvisual/decorator/tidb.go b/pkg/keyvisual/decorator/tidb.go index a3458ff7c0..17ff0ce88b 100644 --- a/pkg/keyvisual/decorator/tidb.go +++ b/pkg/keyvisual/decorator/tidb.go @@ -6,6 +6,7 @@ import ( "context" "encoding/hex" "fmt" + "sort" "sync" "time" @@ -52,14 +53,95 @@ type tidbLabelStrategy struct { EtcdClient *clientv3.Client TableMap sync.Map + TableInOrder *TableInOrder tidbClient *tidb.Client SchemaVersion int64 TidbAddress []string } +type TableInOrder struct { + rwMu sync.RWMutex + tables []*tableDetail +} + +// BuildFromTableMap build order map from a table map. +func (inOrder *TableInOrder) BuildFromTableMap(m *sync.Map) { + tables := []*tableDetail{} + m.Range(func(key, value interface{}) bool { + t := value.(*tableDetail) + tables = append(tables, t) + return true + }) + + sort.Sort(&tableSorter{tables: tables}) + + inOrder.rwMu.Lock() + defer inOrder.rwMu.Unlock() + inOrder.tables = tables +} + +// FindOne will find first table detail which id is between [fromId, toId). +// Returns nil if not found any +func (inOrder *TableInOrder) FindOne(fromId, toId int64) *tableDetail { + if fromId >= toId { + return nil + } + + inOrder.rwMu.RLock() + defer inOrder.rwMu.RUnlock() + + tLen := len(inOrder.tables) + var pivot int = tLen / 2 + left := 0 + right := tLen + for pivot > left { + prevId := inOrder.tables[pivot-1].ID + id := inOrder.tables[pivot].ID + // find approaching id near the fromId + // table_1 ------- table_3 ------- table_5 + // ^ + // search table_2 + // approaching result: table_3 + if prevId < fromId && id >= fromId { + break + } + + if id < fromId { + left = pivot + } else { + right = pivot + } + pivot = (left + right) / 2 + } + + id := inOrder.tables[pivot].ID + if id < fromId || id >= toId { + return nil + } + + return inOrder.tables[pivot] +} + +type tableSorter struct { + tables []*tableDetail +} + +func (ts *tableSorter) Len() int { + return len(ts.tables) +} + +func (ts *tableSorter) Swap(i, j int) { + ts.tables[i], ts.tables[j] = ts.tables[j], ts.tables[i] +} + +func (ts *tableSorter) Less(i, j int) bool { + return ts.tables[i].ID < ts.tables[j].ID +} + type tidbLabeler struct { - TableMap *sync.Map - Buffer model.KeyInfoBuffer + TableMap *sync.Map + TableInOrder *TableInOrder + Buffer model.KeyInfoBuffer } func (s *tidbLabelStrategy) ReloadConfig(cfg *config.KeyVisualConfig) {} @@ -79,7 +161,8 @@ func (s *tidbLabelStrategy) Background(ctx context.Context) { func (s *tidbLabelStrategy) NewLabeler() Labeler { return &tidbLabeler{ - TableMap: &s.TableMap, + TableMap: &s.TableMap, + TableInOrder: s.TableInOrder, } } @@ -105,8 +188,8 @@ func (e *tidbLabeler) CrossBorder(startKey, endKey string) bool { // Label will parse the ID information of the table and index. func (e *tidbLabeler) Label(keys []string) []LabelKey { labelKeys := make([]LabelKey, len(keys)) - for i, key := range keys { - labelKeys[i] = e.label(key) + for i := 1; i < len(keys); i++ { + labelKeys[i-1] = e.label(keys[i-1], keys[i]) } if keys[0] == "" { @@ -120,30 +203,41 @@ func (e *tidbLabeler) Label(keys []string) []LabelKey { return labelKeys } -func (e *tidbLabeler) label(key string) (label LabelKey) { - keyBytes := region.Bytes(key) - label.Key = hex.EncodeToString(keyBytes) - keyInfo, _ := e.Buffer.DecodeKey(keyBytes) +func (e *tidbLabeler) label(startKey, endKey string) (label LabelKey) { + startKeyBytes := region.Bytes(startKey) + label.Key = hex.EncodeToString(startKeyBytes) + startKeyInfo, _ := e.Buffer.DecodeKey(startKeyBytes) - isMeta, tableID := keyInfo.MetaOrTable() + isMeta, startTableID := startKeyInfo.MetaOrTable() if isMeta { label.Labels = append(label.Labels, "meta") return } var detail *tableDetail - if v, ok := e.TableMap.Load(tableID); ok { + if v, ok := e.TableMap.Load(startTableID); ok { detail = v.(*tableDetail) label.Labels = append(label.Labels, detail.DB, detail.Name) } else { - label.Labels = append(label.Labels, fmt.Sprintf("table_%d", tableID)) + endKeyBytes := region.Bytes(endKey) + endKeyInfo, _ := e.Buffer.DecodeKey(endKeyBytes) + _, endTableID := endKeyInfo.MetaOrTable() + detail := e.TableInOrder.FindOne(startTableID, endTableID) + + if detail != nil { + label.Labels = append(label.Labels, detail.DB, detail.Name) + // can't find the row/index info if the table info was came from a range + return + } else { + label.Labels = append(label.Labels, fmt.Sprintf("table_%d", startTableID)) + } } - if isCommonHandle, rowID := keyInfo.RowInfo(); isCommonHandle { + if isCommonHandle, rowID := startKeyInfo.RowInfo(); isCommonHandle { label.Labels = append(label.Labels, "row") } else if rowID != 0 { label.Labels = append(label.Labels, fmt.Sprintf("row_%d", rowID)) - } else if indexID := keyInfo.IndexInfo(); indexID != 0 { + } else if indexID := startKeyInfo.IndexInfo(); indexID != 0 { if detail == nil { label.Labels = append(label.Labels, fmt.Sprintf("index_%d", indexID)) } else if name, ok := detail.Indices[indexID]; ok { diff --git a/pkg/keyvisual/decorator/tidb_requests.go b/pkg/keyvisual/decorator/tidb_requests.go index e30856cb55..0531dd226a 100644 --- a/pkg/keyvisual/decorator/tidb_requests.go +++ b/pkg/keyvisual/decorator/tidb_requests.go @@ -104,6 +104,8 @@ func (s *tidbLabelStrategy) updateMap(ctx context.Context) { } } + s.TableInOrder.BuildFromTableMap(&s.TableMap) + // update schema version if updateSuccess { s.SchemaVersion = schemaVersion diff --git a/pkg/keyvisual/decorator/tidb_test.go b/pkg/keyvisual/decorator/tidb_test.go index d41a615ffa..2b9f32a7c5 100644 --- a/pkg/keyvisual/decorator/tidb_test.go +++ b/pkg/keyvisual/decorator/tidb_test.go @@ -3,9 +3,47 @@ package decorator import ( + "sync" + "testing" + . "github.com/pingcap/check" + "github.com/stretchr/testify/require" ) var _ = Suite(&testTiDBSuite{}) type testTiDBSuite struct{} + +func TestTableInOrderBuild(t *testing.T) { + tableMap := sync.Map{} + tableInOrder := &TableInOrder{} + + tableMap.Store(8, &tableDetail{ID: 8}) + tableMap.Store(2, &tableDetail{ID: 2}) + tableMap.Store(4, &tableDetail{ID: 4}) + tableMap.Store(1, &tableDetail{ID: 1}) + + tableInOrder.BuildFromTableMap(&tableMap) + tableIds := make([]int64, 0, len(tableInOrder.tables)) + for _, table := range tableInOrder.tables { + tableIds = append(tableIds, table.ID) + } + + require.Equal(t, []int64{1, 2, 4, 8}, tableIds) +} + +func TestTableInOrderFindOne(t *testing.T) { + tableInOrder := &TableInOrder{ + tables: []*tableDetail{{ID: 1}, {ID: 2}, {ID: 4}, {ID: 8}}, + } + + require.Equal(t, tableInOrder.FindOne(1, 2).ID, int64(1)) + require.Equal(t, tableInOrder.FindOne(2, 3).ID, int64(2)) + require.Equal(t, tableInOrder.FindOne(3, 5).ID, int64(4)) + require.Equal(t, tableInOrder.FindOne(2, 8).ID, int64(2)) + require.Equal(t, tableInOrder.FindOne(8, 18).ID, int64(8)) + + require.Nil(t, tableInOrder.FindOne(3, 4)) + require.Nil(t, tableInOrder.FindOne(8, 0)) + require.Nil(t, tableInOrder.FindOne(8, 8)) +} From 52d47d411564b9a021b3d974cc0beb1aacc8f24c Mon Sep 17 00:00:00 2001 From: Suhaha Date: Thu, 19 May 2022 09:22:38 +0800 Subject: [PATCH 2/7] chore: update test & comment --- pkg/keyvisual/decorator/tidb.go | 2 +- pkg/keyvisual/decorator/tidb_test.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/keyvisual/decorator/tidb.go b/pkg/keyvisual/decorator/tidb.go index 17ff0ce88b..805e7250f8 100644 --- a/pkg/keyvisual/decorator/tidb.go +++ b/pkg/keyvisual/decorator/tidb.go @@ -64,7 +64,7 @@ type TableInOrder struct { tables []*tableDetail } -// BuildFromTableMap build order map from a table map. +// BuildFromTableMap build ordered tables from a table map. func (inOrder *TableInOrder) BuildFromTableMap(m *sync.Map) { tables := []*tableDetail{} m.Range(func(key, value interface{}) bool { diff --git a/pkg/keyvisual/decorator/tidb_test.go b/pkg/keyvisual/decorator/tidb_test.go index 2b9f32a7c5..c4ffa31d61 100644 --- a/pkg/keyvisual/decorator/tidb_test.go +++ b/pkg/keyvisual/decorator/tidb_test.go @@ -46,4 +46,5 @@ func TestTableInOrderFindOne(t *testing.T) { require.Nil(t, tableInOrder.FindOne(3, 4)) require.Nil(t, tableInOrder.FindOne(8, 0)) require.Nil(t, tableInOrder.FindOne(8, 8)) + require.Nil(t, tableInOrder.FindOne(80, 81)) } From 8eba05ece8070c4250f921b78d4d89ad9f0fa246 Mon Sep 17 00:00:00 2001 From: Suhaha Date: Thu, 19 May 2022 09:26:04 +0800 Subject: [PATCH 3/7] chore: make lint happy --- pkg/keyvisual/decorator/tidb.go | 29 ++++++++++++------------ pkg/keyvisual/decorator/tidb_requests.go | 2 +- pkg/keyvisual/decorator/tidb_test.go | 24 ++++++++++---------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/pkg/keyvisual/decorator/tidb.go b/pkg/keyvisual/decorator/tidb.go index 805e7250f8..bd48e2a2be 100644 --- a/pkg/keyvisual/decorator/tidb.go +++ b/pkg/keyvisual/decorator/tidb.go @@ -53,19 +53,19 @@ type tidbLabelStrategy struct { EtcdClient *clientv3.Client TableMap sync.Map - TableInOrder *TableInOrder + tableInOrder *tableInOrder tidbClient *tidb.Client SchemaVersion int64 TidbAddress []string } -type TableInOrder struct { +type tableInOrder struct { rwMu sync.RWMutex tables []*tableDetail } // BuildFromTableMap build ordered tables from a table map. -func (inOrder *TableInOrder) BuildFromTableMap(m *sync.Map) { +func (inOrder *tableInOrder) buildFromTableMap(m *sync.Map) { tables := []*tableDetail{} m.Range(func(key, value interface{}) bool { t := value.(*tableDetail) @@ -82,8 +82,8 @@ func (inOrder *TableInOrder) BuildFromTableMap(m *sync.Map) { // FindOne will find first table detail which id is between [fromId, toId). // Returns nil if not found any -func (inOrder *TableInOrder) FindOne(fromId, toId int64) *tableDetail { - if fromId >= toId { +func (inOrder *tableInOrder) findOne(fromID, toID int64) *tableDetail { + if fromID >= toID { return nil } @@ -91,22 +91,22 @@ func (inOrder *TableInOrder) FindOne(fromId, toId int64) *tableDetail { defer inOrder.rwMu.RUnlock() tLen := len(inOrder.tables) - var pivot int = tLen / 2 + pivot := tLen / 2 left := 0 right := tLen for pivot > left { - prevId := inOrder.tables[pivot-1].ID + prevID := inOrder.tables[pivot-1].ID id := inOrder.tables[pivot].ID // find approaching id near the fromId // table_1 ------- table_3 ------- table_5 // ^ // search table_2 // approaching result: table_3 - if prevId < fromId && id >= fromId { + if prevID < fromID && id >= fromID { break } - if id < fromId { + if id < fromID { left = pivot } else { right = pivot @@ -115,7 +115,7 @@ func (inOrder *TableInOrder) FindOne(fromId, toId int64) *tableDetail { } id := inOrder.tables[pivot].ID - if id < fromId || id >= toId { + if id < fromID || id >= toID { return nil } @@ -140,7 +140,7 @@ func (ts *tableSorter) Less(i, j int) bool { type tidbLabeler struct { TableMap *sync.Map - TableInOrder *TableInOrder + tableInOrder *tableInOrder Buffer model.KeyInfoBuffer } @@ -162,7 +162,7 @@ func (s *tidbLabelStrategy) Background(ctx context.Context) { func (s *tidbLabelStrategy) NewLabeler() Labeler { return &tidbLabeler{ TableMap: &s.TableMap, - TableInOrder: s.TableInOrder, + tableInOrder: s.tableInOrder, } } @@ -222,15 +222,14 @@ func (e *tidbLabeler) label(startKey, endKey string) (label LabelKey) { endKeyBytes := region.Bytes(endKey) endKeyInfo, _ := e.Buffer.DecodeKey(endKeyBytes) _, endTableID := endKeyInfo.MetaOrTable() - detail := e.TableInOrder.FindOne(startTableID, endTableID) + detail := e.tableInOrder.findOne(startTableID, endTableID) if detail != nil { label.Labels = append(label.Labels, detail.DB, detail.Name) // can't find the row/index info if the table info was came from a range return - } else { - label.Labels = append(label.Labels, fmt.Sprintf("table_%d", startTableID)) } + label.Labels = append(label.Labels, fmt.Sprintf("table_%d", startTableID)) } if isCommonHandle, rowID := startKeyInfo.RowInfo(); isCommonHandle { diff --git a/pkg/keyvisual/decorator/tidb_requests.go b/pkg/keyvisual/decorator/tidb_requests.go index 0531dd226a..2eca5e9e41 100644 --- a/pkg/keyvisual/decorator/tidb_requests.go +++ b/pkg/keyvisual/decorator/tidb_requests.go @@ -104,7 +104,7 @@ func (s *tidbLabelStrategy) updateMap(ctx context.Context) { } } - s.TableInOrder.BuildFromTableMap(&s.TableMap) + s.tableInOrder.buildFromTableMap(&s.TableMap) // update schema version if updateSuccess { diff --git a/pkg/keyvisual/decorator/tidb_test.go b/pkg/keyvisual/decorator/tidb_test.go index c4ffa31d61..f568e24fbe 100644 --- a/pkg/keyvisual/decorator/tidb_test.go +++ b/pkg/keyvisual/decorator/tidb_test.go @@ -16,14 +16,14 @@ type testTiDBSuite struct{} func TestTableInOrderBuild(t *testing.T) { tableMap := sync.Map{} - tableInOrder := &TableInOrder{} + tableInOrder := &tableInOrder{} tableMap.Store(8, &tableDetail{ID: 8}) tableMap.Store(2, &tableDetail{ID: 2}) tableMap.Store(4, &tableDetail{ID: 4}) tableMap.Store(1, &tableDetail{ID: 1}) - tableInOrder.BuildFromTableMap(&tableMap) + tableInOrder.buildFromTableMap(&tableMap) tableIds := make([]int64, 0, len(tableInOrder.tables)) for _, table := range tableInOrder.tables { tableIds = append(tableIds, table.ID) @@ -33,18 +33,18 @@ func TestTableInOrderBuild(t *testing.T) { } func TestTableInOrderFindOne(t *testing.T) { - tableInOrder := &TableInOrder{ + tableInOrder := &tableInOrder{ tables: []*tableDetail{{ID: 1}, {ID: 2}, {ID: 4}, {ID: 8}}, } - require.Equal(t, tableInOrder.FindOne(1, 2).ID, int64(1)) - require.Equal(t, tableInOrder.FindOne(2, 3).ID, int64(2)) - require.Equal(t, tableInOrder.FindOne(3, 5).ID, int64(4)) - require.Equal(t, tableInOrder.FindOne(2, 8).ID, int64(2)) - require.Equal(t, tableInOrder.FindOne(8, 18).ID, int64(8)) + require.Equal(t, tableInOrder.findOne(1, 2).ID, int64(1)) + require.Equal(t, tableInOrder.findOne(2, 3).ID, int64(2)) + require.Equal(t, tableInOrder.findOne(3, 5).ID, int64(4)) + require.Equal(t, tableInOrder.findOne(2, 8).ID, int64(2)) + require.Equal(t, tableInOrder.findOne(8, 18).ID, int64(8)) - require.Nil(t, tableInOrder.FindOne(3, 4)) - require.Nil(t, tableInOrder.FindOne(8, 0)) - require.Nil(t, tableInOrder.FindOne(8, 8)) - require.Nil(t, tableInOrder.FindOne(80, 81)) + require.Nil(t, tableInOrder.findOne(3, 4)) + require.Nil(t, tableInOrder.findOne(8, 0)) + require.Nil(t, tableInOrder.findOne(8, 8)) + require.Nil(t, tableInOrder.findOne(80, 81)) } From 280776433745d16cfac227b30a231c1060c72cbc Mon Sep 17 00:00:00 2001 From: Suhaha Date: Thu, 19 May 2022 09:46:49 +0800 Subject: [PATCH 4/7] chore: add lint timeout --- scripts/lint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lint.sh b/scripts/lint.sh index 846c55c5bb..e363a9f5b9 100755 --- a/scripts/lint.sh +++ b/scripts/lint.sh @@ -26,7 +26,7 @@ if [ "${NEED_DOWNLOAD}" = true ]; then fi echo "+ Run lints for Go source code" -${LINT_BIN} run --fix +${LINT_BIN} run --fix --timeout 3m echo "+ Clean up go mod" go mod tidy From 52a6f227c35a1c3a61afd1117e902a9318cfb518 Mon Sep 17 00:00:00 2001 From: Suhaha Date: Thu, 19 May 2022 09:50:27 +0800 Subject: [PATCH 5/7] fix: init --- pkg/keyvisual/decorator/tidb.go | 49 +++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/pkg/keyvisual/decorator/tidb.go b/pkg/keyvisual/decorator/tidb.go index bd48e2a2be..77b04a1df3 100644 --- a/pkg/keyvisual/decorator/tidb.go +++ b/pkg/keyvisual/decorator/tidb.go @@ -25,6 +25,7 @@ func TiDBLabelStrategy(lc fx.Lifecycle, wg *sync.WaitGroup, etcdClient *clientv3 EtcdClient: etcdClient, tidbClient: tidbClient, SchemaVersion: -1, + tableInOrder: &tableInOrder{}, } lc.Append(fx.Hook{ @@ -60,12 +61,16 @@ type tidbLabelStrategy struct { } type tableInOrder struct { - rwMu sync.RWMutex + mu sync.RWMutex tables []*tableDetail } // BuildFromTableMap build ordered tables from a table map. func (inOrder *tableInOrder) buildFromTableMap(m *sync.Map) { + if m == nil { + return + } + tables := []*tableDetail{} m.Range(func(key, value interface{}) bool { t := value.(*tableDetail) @@ -75,22 +80,26 @@ func (inOrder *tableInOrder) buildFromTableMap(m *sync.Map) { sort.Sort(&tableSorter{tables: tables}) - inOrder.rwMu.Lock() - defer inOrder.rwMu.Unlock() + inOrder.mu.Lock() + defer inOrder.mu.Unlock() inOrder.tables = tables } // FindOne will find first table detail which id is between [fromId, toId). -// Returns nil if not found any +// Returns nil if not found any. func (inOrder *tableInOrder) findOne(fromID, toID int64) *tableDetail { if fromID >= toID { return nil } - inOrder.rwMu.RLock() - defer inOrder.rwMu.RUnlock() + inOrder.mu.RLock() + defer inOrder.mu.RUnlock() tLen := len(inOrder.tables) + if tLen == 0 { + return nil + } + pivot := tLen / 2 left := 0 right := tLen @@ -115,7 +124,7 @@ func (inOrder *tableInOrder) findOne(fromID, toID int64) *tableDetail { } id := inOrder.tables[pivot].ID - if id < fromID || id >= toID { + if id < fromID || id > toID { return nil } @@ -203,40 +212,40 @@ func (e *tidbLabeler) Label(keys []string) []LabelKey { return labelKeys } -func (e *tidbLabeler) label(startKey, endKey string) (label LabelKey) { - startKeyBytes := region.Bytes(startKey) - label.Key = hex.EncodeToString(startKeyBytes) - startKeyInfo, _ := e.Buffer.DecodeKey(startKeyBytes) +func (e *tidbLabeler) label(lKey, rKey string) (label LabelKey) { + lKeyBytes := region.Bytes(lKey) + label.Key = hex.EncodeToString(lKeyBytes) + lKeyInfo, _ := e.Buffer.DecodeKey(lKeyBytes) - isMeta, startTableID := startKeyInfo.MetaOrTable() + isMeta, lTableID := lKeyInfo.MetaOrTable() if isMeta { label.Labels = append(label.Labels, "meta") return } var detail *tableDetail - if v, ok := e.TableMap.Load(startTableID); ok { + if v, ok := e.TableMap.Load(lTableID); ok { detail = v.(*tableDetail) label.Labels = append(label.Labels, detail.DB, detail.Name) } else { - endKeyBytes := region.Bytes(endKey) - endKeyInfo, _ := e.Buffer.DecodeKey(endKeyBytes) - _, endTableID := endKeyInfo.MetaOrTable() - detail := e.tableInOrder.findOne(startTableID, endTableID) + rKeyBytes := region.Bytes(rKey) + rKeyInfo, _ := e.Buffer.DecodeKey(rKeyBytes) + _, rTableID := rKeyInfo.MetaOrTable() + detail := e.tableInOrder.findOne(lTableID, rTableID) if detail != nil { label.Labels = append(label.Labels, detail.DB, detail.Name) // can't find the row/index info if the table info was came from a range return } - label.Labels = append(label.Labels, fmt.Sprintf("table_%d", startTableID)) + label.Labels = append(label.Labels, fmt.Sprintf("table_%d", lTableID)) } - if isCommonHandle, rowID := startKeyInfo.RowInfo(); isCommonHandle { + if isCommonHandle, rowID := lKeyInfo.RowInfo(); isCommonHandle { label.Labels = append(label.Labels, "row") } else if rowID != 0 { label.Labels = append(label.Labels, fmt.Sprintf("row_%d", rowID)) - } else if indexID := startKeyInfo.IndexInfo(); indexID != 0 { + } else if indexID := lKeyInfo.IndexInfo(); indexID != 0 { if detail == nil { label.Labels = append(label.Labels, fmt.Sprintf("index_%d", indexID)) } else if name, ok := detail.Indices[indexID]; ok { From 9cd2564ac227c2273d77a59f9cd1cab796cc1749 Mon Sep 17 00:00:00 2001 From: Suhaha Date: Fri, 20 May 2022 11:09:25 +0800 Subject: [PATCH 6/7] fix: test --- pkg/keyvisual/decorator/tidb_test.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/pkg/keyvisual/decorator/tidb_test.go b/pkg/keyvisual/decorator/tidb_test.go index f568e24fbe..44bffa7464 100644 --- a/pkg/keyvisual/decorator/tidb_test.go +++ b/pkg/keyvisual/decorator/tidb_test.go @@ -33,18 +33,21 @@ func TestTableInOrderBuild(t *testing.T) { } func TestTableInOrderFindOne(t *testing.T) { - tableInOrder := &tableInOrder{ + table := &tableInOrder{ tables: []*tableDetail{{ID: 1}, {ID: 2}, {ID: 4}, {ID: 8}}, } - - require.Equal(t, tableInOrder.findOne(1, 2).ID, int64(1)) - require.Equal(t, tableInOrder.findOne(2, 3).ID, int64(2)) - require.Equal(t, tableInOrder.findOne(3, 5).ID, int64(4)) - require.Equal(t, tableInOrder.findOne(2, 8).ID, int64(2)) - require.Equal(t, tableInOrder.findOne(8, 18).ID, int64(8)) - - require.Nil(t, tableInOrder.findOne(3, 4)) - require.Nil(t, tableInOrder.findOne(8, 0)) - require.Nil(t, tableInOrder.findOne(8, 8)) - require.Nil(t, tableInOrder.findOne(80, 81)) + require.Equal(t, table.findOne(1, 2).ID, int64(1)) + require.Equal(t, table.findOne(2, 3).ID, int64(2)) + require.Equal(t, table.findOne(3, 5).ID, int64(4)) + require.Equal(t, table.findOne(8, 18).ID, int64(8)) + require.Equal(t, table.findOne(3, 4).ID, int64(4)) + + require.Nil(t, table.findOne(8, 0)) + require.Nil(t, table.findOne(8, 8)) + require.Nil(t, table.findOne(80, 81)) + + emptyTable := &tableInOrder{ + tables: []*tableDetail{}, + } + require.Nil(t, emptyTable.findOne(1, 2)) } From 1149d14fa011eaff56b5db5eb2b982e3cb5a602f Mon Sep 17 00:00:00 2001 From: Suhaha Date: Fri, 20 May 2022 11:17:13 +0800 Subject: [PATCH 7/7] test: add more tests --- pkg/keyvisual/decorator/tidb_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/keyvisual/decorator/tidb_test.go b/pkg/keyvisual/decorator/tidb_test.go index 44bffa7464..3286f65faa 100644 --- a/pkg/keyvisual/decorator/tidb_test.go +++ b/pkg/keyvisual/decorator/tidb_test.go @@ -41,13 +41,19 @@ func TestTableInOrderFindOne(t *testing.T) { require.Equal(t, table.findOne(3, 5).ID, int64(4)) require.Equal(t, table.findOne(8, 18).ID, int64(8)) require.Equal(t, table.findOne(3, 4).ID, int64(4)) - require.Nil(t, table.findOne(8, 0)) require.Nil(t, table.findOne(8, 8)) require.Nil(t, table.findOne(80, 81)) - emptyTable := &tableInOrder{ + table0 := &tableInOrder{ tables: []*tableDetail{}, } - require.Nil(t, emptyTable.findOne(1, 2)) + require.Nil(t, table0.findOne(1, 2)) + + table1 := &tableInOrder{ + tables: []*tableDetail{{ID: 2}}, + } + require.Equal(t, table1.findOne(1, 2).ID, int64(2)) + require.Nil(t, table1.findOne(0, 1)) + require.Nil(t, table1.findOne(3, 4)) }