Skip to content

Commit 3c20199

Browse files
Leavrthzeminzhou
authored andcommitted
br: fix pre allocate id exceeds bound (pingcap#59719)
close pingcap#59718
1 parent 1319f5e commit 3c20199

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

br/pkg/restore/internal/prealloc_table_id/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ go_test(
1919
deps = [
2020
":prealloc_table_id",
2121
"//br/pkg/metautil",
22+
"//br/pkg/utiltest",
23+
"//pkg/kv",
24+
"//pkg/meta",
2225
"//pkg/meta/model",
26+
"//pkg/testkit",
2327
"@com_github_stretchr_testify//require",
2428
],
2529
)

br/pkg/restore/internal/prealloc_table_id/alloc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (p *PreallocIDs) Alloc(m Allocator) error {
8787
if err != nil {
8888
return err
8989
}
90-
p.allocedFrom = alloced
90+
p.allocedFrom = alloced + 1
9191
return nil
9292
}
9393

br/pkg/restore/internal/prealloc_table_id/alloc_test.go

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
package prealloctableid_test
44

55
import (
6+
"context"
67
"fmt"
78
"testing"
89

910
"github.com/pingcap/tidb/br/pkg/metautil"
1011
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"
1115
"github.com/pingcap/tidb/pkg/meta/model"
16+
"github.com/pingcap/tidb/pkg/testkit"
1217
"github.com/stretchr/testify/require"
1318
)
1419

@@ -38,16 +43,16 @@ func TestAllocator(t *testing.T) {
3843
{
3944
tableIDs: []int64{1, 2, 5, 6, 7},
4045
hasAllocatedTo: 6,
41-
successfullyAllocated: []int64{6, 7},
46+
successfullyAllocated: []int64{7},
4247
shouldAllocatedTo: 8,
43-
msg: "ID:[6,8)",
48+
msg: "ID:[7,8)",
4449
},
4550
{
4651
tableIDs: []int64{4, 6, 9, 2},
4752
hasAllocatedTo: 1,
4853
successfullyAllocated: []int64{2, 4, 6, 9},
4954
shouldAllocatedTo: 10,
50-
msg: "ID:[1,10)",
55+
msg: "ID:[2,10)",
5156
},
5257
{
5358
tableIDs: []int64{1, 2, 3, 4},
@@ -61,17 +66,17 @@ func TestAllocator(t *testing.T) {
6166
hasAllocatedTo: 3,
6267
successfullyAllocated: []int64{5, 6},
6368
shouldAllocatedTo: 7,
64-
msg: "ID:[3,7)",
69+
msg: "ID:[4,7)",
6570
},
6671
{
6772
tableIDs: []int64{1, 2, 5, 6, 7},
6873
hasAllocatedTo: 6,
69-
successfullyAllocated: []int64{6, 7},
74+
successfullyAllocated: []int64{7},
7075
shouldAllocatedTo: 13,
7176
partitions: map[int64][]int64{
7277
7: {8, 9, 10, 11, 12},
7378
},
74-
msg: "ID:[6,13)",
79+
msg: "ID:[7,13)",
7580
},
7681
{
7782
tableIDs: []int64{1, 2, 5, 6, 7, 13},
@@ -81,7 +86,7 @@ func TestAllocator(t *testing.T) {
8186
partitions: map[int64][]int64{
8287
7: {8, 9, 10, 11, 12},
8388
},
84-
msg: "ID:[9,14)",
89+
msg: "ID:[10,14)",
8590
},
8691
}
8792

@@ -123,3 +128,41 @@ func TestAllocator(t *testing.T) {
123128
})
124129
}
125130
}
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

Comments
 (0)