Skip to content

Commit

Permalink
fix autoid (pingcap#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
holys authored and huachaohuang committed Apr 17, 2018
1 parent 7e0e5ca commit d151f35
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
11 changes: 5 additions & 6 deletions lightning/kv/sql2kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/juju/errors"
sqltool "github.com/pingcap/tidb-lightning/lightning/sql"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/plan"
kvec "github.com/pingcap/tidb/util/kvencoder"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -41,7 +40,7 @@ type TableKVEncoder struct {
bufValues []interface{}

encoder kvec.KvEncoder
idAllocator autoid.Allocator
idAllocator *kvec.Allocator
}

func NewTableKVEncoder(
Expand All @@ -50,7 +49,7 @@ func NewTableKVEncoder(
columns int, tableSchema string, sqlMode string) (*TableKVEncoder, error) {

idAllocator := kvec.NewAllocator()
idAllocator.Rebase(tableID, 0, false)
idAllocator.Reset(0)
kvEncoder, err := kvec.New(db, idAllocator)
if err != nil {
log.Errorf("[sql2kv] kv encoder create failed : %v", err)
Expand Down Expand Up @@ -117,16 +116,16 @@ func (kvcodec *TableKVEncoder) makeStatments(maxRows int) ([]uint32, error) {
return stmtIds, nil
}

func (kvcodec *TableKVEncoder) RebaseRowID(rowID int64) {
kvcodec.idAllocator.Rebase(kvcodec.tableID, rowID, false)
func (kvcodec *TableKVEncoder) ResetRowID(rowID int64) {
kvcodec.idAllocator.Reset(rowID)
}

func (kvcodec *TableKVEncoder) Close() error {
return kvcodec.encoder.Close()
}

func (kvcodec *TableKVEncoder) NextRowID() int64 {
return kvcodec.idAllocator.Base()
return kvcodec.idAllocator.Base() + 1
}

func (kvcodec *TableKVEncoder) BuildMetaKvs(rowID int64) ([]kvec.KvPair, error) {
Expand Down
17 changes: 5 additions & 12 deletions lightning/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,18 +680,11 @@ func (tr *TableRestore) restoreTableMeta(rowID int64) error {
kvDeliver := tr.deliversMgr.AcquireClient(tr.dbInfo.Name, table)
defer tr.deliversMgr.RecycleClient(kvDeliver)

kvs, err := encoder.BuildMetaKvs(rowID)
if err != nil {
log.Errorf("[%s] failed to generate meta key (row_id = %d) : %s", table, rowID, err.Error())
return errors.Trace(err)
}

if err = kvDeliver.Put(kvs); err != nil {
log.Errorf("[%s] meta key deliver failed : %s", table, err.Error())
return errors.Trace(err)
}
dsn := tr.cfg.TiDB
db := common.ConnectDB(dsn.Host, dsn.Port, dsn.User, dsn.Psw)
defer db.Close()

return nil
return errors.Trace(AlterAutoIncrement(db, tr.tableMeta.DB, tr.tableMeta.Name, rowID))
}

func (tr *TableRestore) importKV() error {
Expand Down Expand Up @@ -875,7 +868,7 @@ func (exc *RegionRestoreExectuor) Run(
*/

if region.BeginRowID >= 0 {
kvEncoder.RebaseRowID(region.BeginRowID)
kvEncoder.ResetRowID(region.BeginRowID)
}
for {
select {
Expand Down
6 changes: 6 additions & 0 deletions lightning/restore/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,9 @@ func UpdateGCLifeTime(db *sql.DB, gcLifeTime string) error {
"UPDATE mysql.tidb SET VARIABLE_VALUE = '%s' WHERE VARIABLE_NAME = 'tikv_gc_life_time'", gcLifeTime))
return errors.Annotatef(err, "update tikv_gc_life_time=%s", gcLifeTime)
}

func AlterAutoIncrement(db *sql.DB, schema string, table string, incr int64) error {
query := fmt.Sprintf("ALTER TABLE `%s`.`%s` AUTO_INCREMENT=%d", schema, table, incr)
_, err := db.Exec(query)
return errors.Annotatef(err, "alter table %s.%s auto_increment=%d", schema, table, incr)
}

0 comments on commit d151f35

Please sign in to comment.