3
3
package prealloctableid_test
4
4
5
5
import (
6
+ "context"
6
7
"fmt"
7
8
"testing"
8
9
9
10
"github.com/pingcap/tidb/br/pkg/metautil"
11
+ << << << < HEAD :br / pkg / restore / prealloc_table_id / alloc_test .go
10
12
prealloctableid "github.com/pingcap/tidb/br/pkg/restore/prealloc_table_id"
11
13
"github.com/pingcap/tidb/pkg/parser/model"
14
+ == == == =
15
+ prealloctableid "github.com/pingcap/tidb/br/pkg/restore/internal/prealloc_table_id"
16
+ "github.com/pingcap/tidb/br/pkg/utiltest"
17
+ "github.com/pingcap/tidb/pkg/kv"
18
+ "github.com/pingcap/tidb/pkg/meta"
19
+ "github.com/pingcap/tidb/pkg/meta/model"
20
+ "github.com/pingcap/tidb/pkg/testkit"
21
+ >> >> >> > c08679bccfa (br : fix pre allocate id exceeds bound (#59719 )):br / pkg / restore / internal / prealloc_table_id / alloc_test .go
12
22
"github.com/stretchr/testify/require"
13
23
)
14
24
@@ -37,14 +47,22 @@ func TestAllocator(t *testing.T) {
37
47
{
38
48
tableIDs : []int64 {1 , 2 , 5 , 6 , 7 },
39
49
hasAllocatedTo : 6 ,
40
- successfullyAllocated : []int64 {6 , 7 },
50
+ successfullyAllocated : []int64 {7 },
41
51
shouldAllocatedTo : 8 ,
52
+ << << << < HEAD :br / pkg / restore / prealloc_table_id / alloc_test .go
53
+ == == == =
54
+ msg : "ID:[7,8)" ,
55
+ >> >> >> > c08679bccfa (br : fix pre allocate id exceeds bound (#59719 )):br / pkg / restore / internal / prealloc_table_id / alloc_test .go
42
56
},
43
57
{
44
58
tableIDs : []int64 {4 , 6 , 9 , 2 },
45
59
hasAllocatedTo : 1 ,
46
60
successfullyAllocated : []int64 {2 , 4 , 6 , 9 },
47
61
shouldAllocatedTo : 10 ,
62
+ << << << < HEAD :br / pkg / restore / prealloc_table_id / alloc_test .go
63
+ == == == =
64
+ msg: "ID:[2,10)" ,
65
+ >> >> >> > c08679bccfa (br : fix pre allocate id exceeds bound (#59719 )):br / pkg / restore / internal / prealloc_table_id / alloc_test.go
48
66
},
49
67
{
50
68
tableIDs : []int64 {1 , 2 , 3 , 4 },
@@ -57,15 +75,23 @@ func TestAllocator(t *testing.T) {
57
75
hasAllocatedTo : 3 ,
58
76
successfullyAllocated : []int64 {5 , 6 },
59
77
shouldAllocatedTo : 7 ,
78
+ << << << < HEAD :br / pkg / restore / prealloc_table_id / alloc_test .go
79
+ == == == =
80
+ msg : "ID:[4,7)" ,
81
+ >> >> >> > c08679bccfa (br : fix pre allocate id exceeds bound (#59719 )):br / pkg / restore / internal / prealloc_table_id / alloc_test.go
60
82
},
61
83
{
62
84
tableIDs : []int64 {1 , 2 , 5 , 6 , 7 },
63
85
hasAllocatedTo : 6 ,
64
- successfullyAllocated : []int64 {6 , 7 },
86
+ successfullyAllocated : []int64 {7 },
65
87
shouldAllocatedTo : 13 ,
66
88
partitions : map [int64 ][]int64 {
67
89
7 : {8 , 9 , 10 , 11 , 12 },
68
90
},
91
+ << << << < HEAD :br / pkg / restore / prealloc_table_id / alloc_test .go
92
+ == == == =
93
+ msg : "ID:[7,13)" ,
94
+ >> >> >> > c08679bccfa (br : fix pre allocate id exceeds bound (#59719 )):br / pkg / restore / internal / prealloc_table_id / alloc_test .go
69
95
},
70
96
{
71
97
tableIDs : []int64 {1 , 2 , 5 , 6 , 7 , 13 },
@@ -75,6 +101,10 @@ func TestAllocator(t *testing.T) {
75
101
partitions : map [int64 ][]int64 {
76
102
7 : {8 , 9 , 10 , 11 , 12 },
77
103
},
104
+ << << << < HEAD :br / pkg / restore / prealloc_table_id / alloc_test .go
105
+ == == == =
106
+ msg : "ID:[10,14)" ,
107
+ >> >> >> > c08679bccfa (br : fix pre allocate id exceeds bound (#59719 )):br / pkg / restore / internal / prealloc_table_id / alloc_test.go
78
108
},
79
109
}
80
110
@@ -115,3 +145,41 @@ func TestAllocator(t *testing.T) {
115
145
})
116
146
}
117
147
}
148
+
149
+ func TestAllocatorBound (t * testing.T ) {
150
+ s := utiltest .CreateRestoreSchemaSuite (t )
151
+ tk := testkit .NewTestKit (t , s .Mock .Storage )
152
+ tk .MustExec ("CREATE TABLE test.t1 (id int);" )
153
+ ctx := kv .WithInternalSourceType (context .Background (), kv .InternalTxnBR )
154
+ currentGlobalID := int64 (0 )
155
+ err := kv .RunInNewTxn (ctx , s .Mock .Store (), true , func (_ context.Context , txn kv.Transaction ) (err error ) {
156
+ allocator := meta .NewMutator (txn )
157
+ currentGlobalID , err = allocator .GetGlobalID ()
158
+ return err
159
+ })
160
+ require .NoError (t , err )
161
+ rows := tk .MustQuery ("ADMIN SHOW DDL JOBS WHERE JOB_ID = ?" , currentGlobalID ).Rows ()
162
+ // The current global ID is used, so it cannot use anymore.
163
+ require .Len (t , rows , 1 )
164
+ tableInfos := []* metautil.Table {
165
+ {Info : & model.TableInfo {ID : currentGlobalID }},
166
+ {Info : & model.TableInfo {ID : currentGlobalID + 2 }},
167
+ {Info : & model.TableInfo {ID : currentGlobalID + 4 }},
168
+ }
169
+ ids := prealloctableid .New (tableInfos )
170
+ lastGlobalID := currentGlobalID
171
+ err = kv .RunInNewTxn (ctx , s .Mock .Store (), true , func (_ context.Context , txn kv.Transaction ) error {
172
+ allocator := meta .NewMutator (txn )
173
+ if err := ids .Alloc (allocator ); err != nil {
174
+ return err
175
+ }
176
+ currentGlobalID , err = allocator .GetGlobalID ()
177
+ return err
178
+ })
179
+ require .NoError (t , err )
180
+ require .Equal (t , fmt .Sprintf ("ID:[%d,%d)" , lastGlobalID + 1 , currentGlobalID ), ids .String ())
181
+ require .False (t , ids .Prealloced (tableInfos [0 ].Info .ID ))
182
+ require .True (t , ids .Prealloced (tableInfos [1 ].Info .ID ))
183
+ require .True (t , ids .Prealloced (tableInfos [2 ].Info .ID ))
184
+ require .True (t , ids .Prealloced (currentGlobalID - 1 ))
185
+ }
0 commit comments