Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion br/pkg/restore/internal/prealloc_table_id/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (p *PreallocIDs) Alloc(m Allocator) error {
if err != nil {
return err
}
p.allocedFrom = alloced
p.allocedFrom = alloced + 1
return nil
}

Expand Down
43 changes: 43 additions & 0 deletions br/pkg/restore/internal/prealloc_table_id/alloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
package prealloctableid_test

import (
"context"
"fmt"
"testing"

"github.com/pingcap/tidb/br/pkg/metautil"
prealloctableid "github.com/pingcap/tidb/br/pkg/restore/internal/prealloc_table_id"
"github.com/pingcap/tidb/br/pkg/utiltest"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/meta"
"github.com/pingcap/tidb/pkg/meta/model"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -123,3 +128,41 @@ func TestAllocator(t *testing.T) {
})
}
}

func TestAllocatorBound(t *testing.T) {
s := utiltest.CreateRestoreSchemaSuite(t)
tk := testkit.NewTestKit(t, s.Mock.Storage)
tk.MustExec("CREATE TABLE test.t1 (id int);")
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBR)
currentGlobalID := int64(0)
err := kv.RunInNewTxn(ctx, s.Mock.Store(), true, func(_ context.Context, txn kv.Transaction) (err error) {
allocator := meta.NewMutator(txn)
currentGlobalID, err = allocator.GetGlobalID()
return err
})
require.NoError(t, err)
rows := tk.MustQuery("ADMIN SHOW DDL JOBS WHERE JOB_ID = ?", currentGlobalID).Rows()
// The current global ID is used, so it cannot use anymore.
require.Len(t, rows, 1)
tableInfos := []*metautil.Table{
{Info: &model.TableInfo{ID: currentGlobalID}},
{Info: &model.TableInfo{ID: currentGlobalID + 2}},
{Info: &model.TableInfo{ID: currentGlobalID + 4}},
}
ids := prealloctableid.New(tableInfos)
lastGlobalID := currentGlobalID
err = kv.RunInNewTxn(ctx, s.Mock.Store(), true, func(_ context.Context, txn kv.Transaction) error {
allocator := meta.NewMutator(txn)
if err := ids.Alloc(allocator); err != nil {
return err
}
currentGlobalID, err = allocator.GetGlobalID()
return err
})
require.NoError(t, err)
require.Equal(t, fmt.Sprintf("ID:[%d,%d)", lastGlobalID+1, currentGlobalID), ids.String())
require.False(t, ids.Prealloced(tableInfos[0].Info.ID))
require.True(t, ids.Prealloced(tableInfos[1].Info.ID))
require.True(t, ids.Prealloced(tableInfos[2].Info.ID))
require.True(t, ids.Prealloced(currentGlobalID-1))
}