Skip to content

Commit 835f78e

Browse files
Leavrthti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#59719
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 26912fc commit 835f78e

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

br/pkg/restore/prealloc_table_id/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ go_test(
2020
deps = [
2121
":prealloc_table_id",
2222
"//br/pkg/metautil",
23+
<<<<<<< HEAD:br/pkg/restore/prealloc_table_id/BUILD.bazel
2324
"//pkg/parser/model",
25+
=======
26+
"//br/pkg/utiltest",
27+
"//pkg/kv",
28+
"//pkg/meta",
29+
"//pkg/meta/model",
30+
"//pkg/testkit",
31+
>>>>>>> c08679bccfa (br: fix pre allocate id exceeds bound (#59719)):br/pkg/restore/internal/prealloc_table_id/BUILD.bazel
2432
"@com_github_stretchr_testify//require",
2533
],
2634
)

br/pkg/restore/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/prealloc_table_id/alloc_test.go

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@
33
package prealloctableid_test
44

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

910
"github.com/pingcap/tidb/br/pkg/metautil"
11+
<<<<<<< HEAD:br/pkg/restore/prealloc_table_id/alloc_test.go
1012
prealloctableid "github.com/pingcap/tidb/br/pkg/restore/prealloc_table_id"
1113
"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
1222
"github.com/stretchr/testify/require"
1323
)
1424

@@ -37,14 +47,22 @@ func TestAllocator(t *testing.T) {
3747
{
3848
tableIDs: []int64{1, 2, 5, 6, 7},
3949
hasAllocatedTo: 6,
40-
successfullyAllocated: []int64{6, 7},
50+
successfullyAllocated: []int64{7},
4151
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
4256
},
4357
{
4458
tableIDs: []int64{4, 6, 9, 2},
4559
hasAllocatedTo: 1,
4660
successfullyAllocated: []int64{2, 4, 6, 9},
4761
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
4866
},
4967
{
5068
tableIDs: []int64{1, 2, 3, 4},
@@ -57,15 +75,23 @@ func TestAllocator(t *testing.T) {
5775
hasAllocatedTo: 3,
5876
successfullyAllocated: []int64{5, 6},
5977
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
6082
},
6183
{
6284
tableIDs: []int64{1, 2, 5, 6, 7},
6385
hasAllocatedTo: 6,
64-
successfullyAllocated: []int64{6, 7},
86+
successfullyAllocated: []int64{7},
6587
shouldAllocatedTo: 13,
6688
partitions: map[int64][]int64{
6789
7: {8, 9, 10, 11, 12},
6890
},
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
6995
},
7096
{
7197
tableIDs: []int64{1, 2, 5, 6, 7, 13},
@@ -75,6 +101,10 @@ func TestAllocator(t *testing.T) {
75101
partitions: map[int64][]int64{
76102
7: {8, 9, 10, 11, 12},
77103
},
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
78108
},
79109
}
80110

@@ -115,3 +145,41 @@ func TestAllocator(t *testing.T) {
115145
})
116146
}
117147
}
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

Comments
 (0)