-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into small-copr-opt
- Loading branch information
Showing
37 changed files
with
727 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2022 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package prealloctableid | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"github.com/pingcap/tidb/br/pkg/metautil" | ||
) | ||
|
||
// Allocator is the interface needed to allocate table IDs. | ||
type Allocator interface { | ||
GetGlobalID() (int64, error) | ||
AdvanceGlobalIDs(n int) (int64, error) | ||
} | ||
|
||
// PreallocIDs mantains the state of preallocated table IDs. | ||
type PreallocIDs struct { | ||
end int64 | ||
|
||
allocedFrom int64 | ||
} | ||
|
||
// New collects the requirement of prealloc IDs and return a | ||
// not-yet-allocated PreallocIDs. | ||
func New(tables []*metautil.Table) *PreallocIDs { | ||
if len(tables) == 0 { | ||
return &PreallocIDs{ | ||
allocedFrom: math.MaxInt64, | ||
} | ||
} | ||
|
||
max := int64(0) | ||
|
||
for _, t := range tables { | ||
if t.Info.ID > max { | ||
max = t.Info.ID | ||
} | ||
} | ||
return &PreallocIDs{ | ||
end: max + 1, | ||
|
||
allocedFrom: math.MaxInt64, | ||
} | ||
} | ||
|
||
// String implements fmt.Stringer. | ||
func (p *PreallocIDs) String() string { | ||
if p.allocedFrom >= p.end { | ||
return fmt.Sprintf("ID:empty(end=%d)", p.end) | ||
} | ||
return fmt.Sprintf("ID:[%d,%d)", p.allocedFrom, p.end) | ||
} | ||
|
||
// preallocTableIDs peralloc the id for [start, end) | ||
func (p *PreallocIDs) Alloc(m Allocator) error { | ||
currentId, err := m.GetGlobalID() | ||
if err != nil { | ||
return err | ||
} | ||
if currentId > p.end { | ||
return nil | ||
} | ||
|
||
alloced, err := m.AdvanceGlobalIDs(int(p.end - currentId)) | ||
if err != nil { | ||
return err | ||
} | ||
p.allocedFrom = alloced | ||
return nil | ||
} | ||
|
||
// Prealloced checks whether a table ID has been successfully allocated. | ||
func (p *PreallocIDs) Prealloced(tid int64) bool { | ||
return p.allocedFrom <= tid && tid < p.end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2022 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package prealloctableid_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/pingcap/tidb/br/pkg/metautil" | ||
prealloctableid "github.com/pingcap/tidb/br/pkg/restore/prealloc_table_id" | ||
"github.com/pingcap/tidb/parser/model" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type testAllocator int64 | ||
|
||
func (t *testAllocator) GetGlobalID() (int64, error) { | ||
return int64(*t), nil | ||
} | ||
|
||
func (t *testAllocator) AdvanceGlobalIDs(n int) (int64, error) { | ||
old := int64(*t) | ||
*t = testAllocator(int64(*t) + int64(n)) | ||
return old, nil | ||
} | ||
|
||
func TestAllocator(t *testing.T) { | ||
type Case struct { | ||
tableIDs []int64 | ||
hasAllocatedTo int64 | ||
successfullyAllocated []int64 | ||
shouldAllocatedTo int64 | ||
} | ||
|
||
cases := []Case{ | ||
{ | ||
tableIDs: []int64{1, 2, 5, 6, 7}, | ||
hasAllocatedTo: 6, | ||
successfullyAllocated: []int64{6, 7}, | ||
shouldAllocatedTo: 8, | ||
}, | ||
{ | ||
tableIDs: []int64{4, 6, 9, 2}, | ||
hasAllocatedTo: 1, | ||
successfullyAllocated: []int64{2, 4, 6, 9}, | ||
shouldAllocatedTo: 10, | ||
}, | ||
{ | ||
tableIDs: []int64{1, 2, 3, 4}, | ||
hasAllocatedTo: 5, | ||
successfullyAllocated: []int64{}, | ||
shouldAllocatedTo: 5, | ||
}, | ||
} | ||
|
||
run := func(t *testing.T, c Case) { | ||
tables := make([]*metautil.Table, 0, len(c.tableIDs)) | ||
for _, id := range c.tableIDs { | ||
tables = append(tables, &metautil.Table{ | ||
Info: &model.TableInfo{ | ||
ID: id, | ||
}, | ||
}) | ||
} | ||
|
||
ids := prealloctableid.New(tables) | ||
allocator := testAllocator(c.hasAllocatedTo) | ||
require.NoError(t, ids.Alloc(&allocator)) | ||
|
||
allocated := make([]int64, 0, len(c.successfullyAllocated)) | ||
for _, t := range c.tableIDs { | ||
if ids.Prealloced(t) { | ||
allocated = append(allocated, t) | ||
} | ||
} | ||
require.ElementsMatch(t, allocated, c.successfullyAllocated) | ||
require.Equal(t, int64(allocator), c.shouldAllocatedTo) | ||
} | ||
|
||
for i, c := range cases { | ||
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) { | ||
run(t, c) | ||
}) | ||
} | ||
} |
Oops, something went wrong.