Skip to content

Commit 80c7ab9

Browse files
authored
restore: Fix prealloc (#40176) (#40181)
close #40177
1 parent a4e5b4f commit 80c7ab9

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

br/pkg/restore/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func (db *DB) tableIDAllocFilter() ddl.AllocTableIDIf {
284284
if db.preallocedIDs == nil {
285285
return true
286286
}
287-
prealloced := db.preallocedIDs.Prealloced(ti.ID)
287+
prealloced := db.preallocedIDs.PreallocedFor(ti)
288288
if prealloced {
289289
log.Info("reusing table ID", zap.Stringer("table", ti.Name))
290290
}

br/pkg/restore/prealloc_table_id/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ go_library(
55
srcs = ["alloc.go"],
66
importpath = "github.com/pingcap/tidb/br/pkg/restore/prealloc_table_id",
77
visibility = ["//visibility:public"],
8-
deps = ["//br/pkg/metautil"],
8+
deps = [
9+
"//br/pkg/metautil",
10+
"//parser/model",
11+
],
912
)
1013

1114
go_test(

br/pkg/restore/prealloc_table_id/alloc.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math"
88

99
"github.com/pingcap/tidb/br/pkg/metautil"
10+
"github.com/pingcap/tidb/parser/model"
1011
)
1112

1213
const (
@@ -48,6 +49,14 @@ func New(tables []*metautil.Table) *PreallocIDs {
4849
if t.Info.ID > max && t.Info.ID < insaneTableIDThreshold {
4950
max = t.Info.ID
5051
}
52+
53+
if t.Info.Partition != nil && t.Info.Partition.Definitions != nil {
54+
for _, part := range t.Info.Partition.Definitions {
55+
if part.ID > max && part.ID < insaneTableIDThreshold {
56+
max = part.ID
57+
}
58+
}
59+
}
5160
}
5261
return &PreallocIDs{
5362
end: max + 1,
@@ -86,3 +95,17 @@ func (p *PreallocIDs) Alloc(m Allocator) error {
8695
func (p *PreallocIDs) Prealloced(tid int64) bool {
8796
return p.allocedFrom <= tid && tid < p.end
8897
}
98+
99+
func (p *PreallocIDs) PreallocedFor(ti *model.TableInfo) bool {
100+
if !p.Prealloced(ti.ID) {
101+
return false
102+
}
103+
if ti.Partition != nil && ti.Partition.Definitions != nil {
104+
for _, part := range ti.Partition.Definitions {
105+
if !p.Prealloced(part.ID) {
106+
return false
107+
}
108+
}
109+
}
110+
return true
111+
}

br/pkg/restore/prealloc_table_id/alloc_test.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func (t *testAllocator) AdvanceGlobalIDs(n int) (int64, error) {
2727
func TestAllocator(t *testing.T) {
2828
type Case struct {
2929
tableIDs []int64
30+
partitions map[int64][]int64
3031
hasAllocatedTo int64
3132
successfullyAllocated []int64
3233
shouldAllocatedTo int64
@@ -57,26 +58,51 @@ func TestAllocator(t *testing.T) {
5758
successfullyAllocated: []int64{5, 6},
5859
shouldAllocatedTo: 7,
5960
},
61+
{
62+
tableIDs: []int64{1, 2, 5, 6, 7},
63+
hasAllocatedTo: 6,
64+
successfullyAllocated: []int64{6, 7},
65+
shouldAllocatedTo: 13,
66+
partitions: map[int64][]int64{
67+
7: {8, 9, 10, 11, 12},
68+
},
69+
},
70+
{
71+
tableIDs: []int64{1, 2, 5, 6, 7, 13},
72+
hasAllocatedTo: 9,
73+
successfullyAllocated: []int64{13},
74+
shouldAllocatedTo: 14,
75+
partitions: map[int64][]int64{
76+
7: {8, 9, 10, 11, 12},
77+
},
78+
},
6079
}
6180

6281
run := func(t *testing.T, c Case) {
6382
tables := make([]*metautil.Table, 0, len(c.tableIDs))
6483
for _, id := range c.tableIDs {
65-
tables = append(tables, &metautil.Table{
84+
table := metautil.Table{
6685
Info: &model.TableInfo{
67-
ID: id,
86+
ID: id,
87+
Partition: &model.PartitionInfo{},
6888
},
69-
})
89+
}
90+
if c.partitions != nil {
91+
for _, part := range c.partitions[id] {
92+
table.Info.Partition.Definitions = append(table.Info.Partition.Definitions, model.PartitionDefinition{ID: part})
93+
}
94+
}
95+
tables = append(tables, &table)
7096
}
7197

7298
ids := prealloctableid.New(tables)
7399
allocator := testAllocator(c.hasAllocatedTo)
74100
require.NoError(t, ids.Alloc(&allocator))
75101

76102
allocated := make([]int64, 0, len(c.successfullyAllocated))
77-
for _, t := range c.tableIDs {
78-
if ids.Prealloced(t) {
79-
allocated = append(allocated, t)
103+
for _, t := range tables {
104+
if ids.PreallocedFor(t.Info) {
105+
allocated = append(allocated, t.Info.ID)
80106
}
81107
}
82108
require.ElementsMatch(t, allocated, c.successfullyAllocated)

0 commit comments

Comments
 (0)