Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

backend: dynamically calculate the maximum auto-inc ID #227

Merged
merged 2 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion lightning/backend/sql2kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,13 @@ func (kvcodec *tableKVEncoder) Encode(

for i, col := range cols {
j := columnPermutation[i]
isAutoIncCol := mysql.HasAutoIncrementFlag(col.Flag)
if j >= 0 && j < len(row) {
value, err = table.CastValue(kvcodec.se, row[j], col.ToInfo())
if err == nil {
value, err = col.HandleBadNull(value, kvcodec.se.vars.StmtCtx)
}
} else if mysql.HasAutoIncrementFlag(col.Flag) {
} else if isAutoIncCol {
// we still need a conversion, e.g. to catch overflow with a TINYINT column.
value, err = table.CastValue(kvcodec.se, types.NewIntDatum(rowID), col.ToInfo())
} else {
Expand All @@ -179,6 +180,9 @@ func (kvcodec *tableKVEncoder) Encode(
return nil, logKVConvertFailed(logger, row, j, col.ToInfo(), err)
}
record = append(record, value)
if isAutoIncCol {
kvcodec.tbl.RebaseAutoID(kvcodec.se, value.GetInt64(), false)
Copy link
Contributor

@lance6716 lance6716 Aug 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This RebaseAutoID is called at every row in my understanding, why could this
rebase-per-row gives tighter upper bound than rebase-chunk-max, I think they will eventually reach almost same auto-id(rebase4Unsigned, rebase4Signed, Rebase) and now it's more expensive.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change allowing a tighter bound is deleting

for _, engine := range cp.Engines {
for _, chunk := range engine.Chunks {
cp.AllocBase = mathutil.MaxInt64(cp.AllocBase, chunk.Chunk.RowIDMax)
}
}

since RowIDMax is normally much larger than the actual row number.

Yes this could become more expensive, but we should perform a measurement to ensure. I bet the CAS loop is still much cheaper than the actual KV encoding.

Copy link
Contributor

@lance6716 lance6716 Aug 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now i see rebased value is cast from rowId, which is smaller or equal than row's content. And I agree CAS is lightweight. I don't know if we could move this rebase-per-row outside, such as on chunk's EOF, so there's fewer repeat.

case io.EOF:
break outside

Copy link
Contributor

@lance6716 lance6716 Aug 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CAS is lightweight so maybe we shouldn't bother moving it outside ⬆️

}
}

if !kvcodec.tbl.Meta().PKIsHandle {
Expand All @@ -192,6 +196,7 @@ func (kvcodec *tableKVEncoder) Encode(
return nil, logKVConvertFailed(logger, row, j, extraHandleColumnInfo, err)
}
record = append(record, value)
kvcodec.tbl.RebaseAutoID(kvcodec.se, value.GetInt64(), false)
}

_, err = kvcodec.tbl.AddRecord(kvcodec.se, record)
Expand Down
5 changes: 3 additions & 2 deletions lightning/backend/sql2kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ func (mockTable) AddRecord(ctx sessionctx.Context, r []types.Datum, opts ...*tab
func (s *kvSuite) TestEncode(c *C) {
c1 := &model.ColumnInfo{ID: 1, Name: model.NewCIStr("c1"), State: model.StatePublic, Offset: 0, FieldType: *types.NewFieldType(mysql.TypeTiny)}
cols := []*model.ColumnInfo{c1}
tblInfo := &model.TableInfo{ID: 1, Columns: cols, PKIsHandle: false}
tbl := tables.MockTableFromMeta(tblInfo)
tblInfo := &model.TableInfo{ID: 1, Columns: cols, PKIsHandle: false, State: model.StatePublic}
tbl, err := tables.TableFromMeta(NewPanickingAllocator(0), tblInfo)
c.Assert(err, IsNil)

logger := log.Logger{Logger: zap.NewNop()}
rows := []types.Datum{
Expand Down
7 changes: 1 addition & 6 deletions lightning/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ import (
"go.uber.org/zap"
"modernc.org/mathutil"

kv "github.com/pingcap/tidb-lightning/lightning/backend"
. "github.com/pingcap/tidb-lightning/lightning/checkpoints"
"github.com/pingcap/tidb-lightning/lightning/common"
"github.com/pingcap/tidb-lightning/lightning/config"
kv "github.com/pingcap/tidb-lightning/lightning/backend"
"github.com/pingcap/tidb-lightning/lightning/log"
"github.com/pingcap/tidb-lightning/lightning/metric"
"github.com/pingcap/tidb-lightning/lightning/mydump"
Expand Down Expand Up @@ -646,11 +646,6 @@ func (t *TableRestore) restoreTable(

// rebase the allocator so it exceeds the number of rows.
cp.AllocBase = mathutil.MaxInt64(cp.AllocBase, t.tableInfo.Core.AutoIncID)
for _, engine := range cp.Engines {
for _, chunk := range engine.Chunks {
cp.AllocBase = mathutil.MaxInt64(cp.AllocBase, chunk.Chunk.RowIDMax)
}
}
t.alloc.Rebase(t.tableInfo.ID, cp.AllocBase, false)
rc.saveCpCh <- saveCp{
tableName: t.tableName,
Expand Down
16 changes: 16 additions & 0 deletions tests/tool_1472/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[lightning]
file = "/tmp/lightning_test_result/lightning.log"

[tikv-importer]
addr = "127.0.0.1:8808"

[mydumper]
data-source-dir = "tests/tool_1472/data"

[tidb]
host = "127.0.0.1"
port = 4000
user = "root"
status-port = 10080
pd-addr = "127.0.0.1:2379"
log-level = "error"
1 change: 1 addition & 0 deletions tests/tool_1472/data/EE1472-schema-create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create database `EE1472`;
5 changes: 5 additions & 0 deletions tests/tool_1472/data/EE1472.notpk-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create table `notpk` (
a int primary key,
b tinyint auto_increment,
key(a)
);
8 changes: 8 additions & 0 deletions tests/tool_1472/data/EE1472.notpk.1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
insert into `notpk` values (1111, 6);
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
8 changes: 8 additions & 0 deletions tests/tool_1472/data/EE1472.notpk.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
insert into `notpk` values (2222, 9);
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
3 changes: 3 additions & 0 deletions tests/tool_1472/data/EE1472.pk-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
create table `pk` (
a tinyint primary key auto_increment
);
8 changes: 8 additions & 0 deletions tests/tool_1472/data/EE1472.pk.1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
insert into `pk` values (3);
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
8 changes: 8 additions & 0 deletions tests/tool_1472/data/EE1472.pk.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
insert into `pk` values (4);
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
-- include some comments to inflate the file size.
31 changes: 31 additions & 0 deletions tests/tool_1472/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh
#
# Copyright 2019 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

# This test verifies if TOOL-1420 is fixed.
# It involves column names not in lower-case.

set -eu

run_sql 'drop database if exists EE1472;'
run_lightning

run_sql 'insert into EE1472.pk values ();'
run_sql 'select count(a), max(a) from EE1472.pk;'
check_contains 'count(a): 3'
check_contains 'max(a): 5'

run_sql 'insert into EE1472.notpk (a) values (3333);'
run_sql 'select b from EE1472.notpk where a = 3333;'
check_contains 'b: 10'