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"
10
11
prealloctableid "github.com/pingcap/tidb/br/pkg/restore/internal/prealloc_table_id"
12
+ "github.com/pingcap/tidb/br/pkg/utiltest"
13
+ "github.com/pingcap/tidb/pkg/kv"
14
+ "github.com/pingcap/tidb/pkg/meta"
11
15
"github.com/pingcap/tidb/pkg/meta/model"
16
+ "github.com/pingcap/tidb/pkg/testkit"
12
17
"github.com/stretchr/testify/require"
13
18
)
14
19
@@ -38,16 +43,16 @@ func TestAllocator(t *testing.T) {
38
43
{
39
44
tableIDs : []int64 {1 , 2 , 5 , 6 , 7 },
40
45
hasAllocatedTo : 6 ,
41
- successfullyAllocated : []int64 {6 , 7 },
46
+ successfullyAllocated : []int64 {7 },
42
47
shouldAllocatedTo : 8 ,
43
- msg : "ID:[6 ,8)" ,
48
+ msg : "ID:[7 ,8)" ,
44
49
},
45
50
{
46
51
tableIDs : []int64 {4 , 6 , 9 , 2 },
47
52
hasAllocatedTo : 1 ,
48
53
successfullyAllocated : []int64 {2 , 4 , 6 , 9 },
49
54
shouldAllocatedTo : 10 ,
50
- msg : "ID:[1 ,10)" ,
55
+ msg : "ID:[2 ,10)" ,
51
56
},
52
57
{
53
58
tableIDs : []int64 {1 , 2 , 3 , 4 },
@@ -61,17 +66,17 @@ func TestAllocator(t *testing.T) {
61
66
hasAllocatedTo : 3 ,
62
67
successfullyAllocated : []int64 {5 , 6 },
63
68
shouldAllocatedTo : 7 ,
64
- msg : "ID:[3 ,7)" ,
69
+ msg : "ID:[4 ,7)" ,
65
70
},
66
71
{
67
72
tableIDs : []int64 {1 , 2 , 5 , 6 , 7 },
68
73
hasAllocatedTo : 6 ,
69
- successfullyAllocated : []int64 {6 , 7 },
74
+ successfullyAllocated : []int64 {7 },
70
75
shouldAllocatedTo : 13 ,
71
76
partitions : map [int64 ][]int64 {
72
77
7 : {8 , 9 , 10 , 11 , 12 },
73
78
},
74
- msg : "ID:[6 ,13)" ,
79
+ msg : "ID:[7 ,13)" ,
75
80
},
76
81
{
77
82
tableIDs : []int64 {1 , 2 , 5 , 6 , 7 , 13 },
@@ -81,7 +86,7 @@ func TestAllocator(t *testing.T) {
81
86
partitions : map [int64 ][]int64 {
82
87
7 : {8 , 9 , 10 , 11 , 12 },
83
88
},
84
- msg : "ID:[9 ,14)" ,
89
+ msg : "ID:[10 ,14)" ,
85
90
},
86
91
}
87
92
@@ -123,3 +128,41 @@ func TestAllocator(t *testing.T) {
123
128
})
124
129
}
125
130
}
131
+
132
+ func TestAllocatorBound (t * testing.T ) {
133
+ s := utiltest .CreateRestoreSchemaSuite (t )
134
+ tk := testkit .NewTestKit (t , s .Mock .Storage )
135
+ tk .MustExec ("CREATE TABLE test.t1 (id int);" )
136
+ ctx := kv .WithInternalSourceType (context .Background (), kv .InternalTxnBR )
137
+ currentGlobalID := int64 (0 )
138
+ err := kv .RunInNewTxn (ctx , s .Mock .Store (), true , func (_ context.Context , txn kv.Transaction ) (err error ) {
139
+ allocator := meta .NewMutator (txn )
140
+ currentGlobalID , err = allocator .GetGlobalID ()
141
+ return err
142
+ })
143
+ require .NoError (t , err )
144
+ rows := tk .MustQuery ("ADMIN SHOW DDL JOBS WHERE JOB_ID = ?" , currentGlobalID ).Rows ()
145
+ // The current global ID is used, so it cannot use anymore.
146
+ require .Len (t , rows , 1 )
147
+ tableInfos := []* metautil.Table {
148
+ {Info : & model.TableInfo {ID : currentGlobalID }},
149
+ {Info : & model.TableInfo {ID : currentGlobalID + 2 }},
150
+ {Info : & model.TableInfo {ID : currentGlobalID + 4 }},
151
+ }
152
+ ids := prealloctableid .New (tableInfos )
153
+ lastGlobalID := currentGlobalID
154
+ err = kv .RunInNewTxn (ctx , s .Mock .Store (), true , func (_ context.Context , txn kv.Transaction ) error {
155
+ allocator := meta .NewMutator (txn )
156
+ if err := ids .Alloc (allocator ); err != nil {
157
+ return err
158
+ }
159
+ currentGlobalID , err = allocator .GetGlobalID ()
160
+ return err
161
+ })
162
+ require .NoError (t , err )
163
+ require .Equal (t , fmt .Sprintf ("ID:[%d,%d)" , lastGlobalID + 1 , currentGlobalID ), ids .String ())
164
+ require .False (t , ids .Prealloced (tableInfos [0 ].Info .ID ))
165
+ require .True (t , ids .Prealloced (tableInfos [1 ].Info .ID ))
166
+ require .True (t , ids .Prealloced (tableInfos [2 ].Info .ID ))
167
+ require .True (t , ids .Prealloced (currentGlobalID - 1 ))
168
+ }
0 commit comments