Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: shard implicit row ID #5513

Merged
merged 13 commits into from
Jan 4, 2018
Merged

*: shard implicit row ID #5513

merged 13 commits into from
Jan 4, 2018

Conversation

coocood
Copy link
Member

@coocood coocood commented Dec 27, 2017

Shard the implicit row ID to avoid hot spot.

@coocood coocood added the DNM label Dec 27, 2017
ddl/ddl_api.go Outdated
@@ -828,8 +828,22 @@ func handleTableOptions(options []*ast.TableOption, tbInfo *model.TableInfo) {
tbInfo.Charset = op.StrValue
case ast.TableOptionCollate:
tbInfo.Collate = op.StrValue
case ast.TableOptionShardRowID:
if !hasAutoIncrementColumn(tbInfo) {
//tbInfo.ShardRowID = op.UintValue != 0
Copy link
Contributor

Choose a reason for hiding this comment

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

remove this?

executor/show.go Outdated
@@ -623,6 +623,10 @@ func (e *ShowExec) fetchShowCreateTable() error {
}
}

if tb.Meta().ShardRowID {
buf.WriteString(" SHARD_ROW_ID=1")
Copy link
Contributor

Choose a reason for hiding this comment

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

if we enable shard_row_id by SHARD_ROW_ID=2?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's enabled too.

parser/parser.y Outdated
@@ -5272,6 +5273,10 @@ TableOption:
{
$$ = &ast.TableOption{Tp: ast.TableOptionStatsPersistent}
}
| "SHARD_ROW_ID" EqOpt LengthNum
Copy link
Contributor

Choose a reason for hiding this comment

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

is EqOpt LengthNum necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

So we can disable it after enabled.

@coocood coocood removed the DNM label Jan 2, 2018
@coocood
Copy link
Member Author

coocood commented Jan 2, 2018

@ngaut @shenli PTAL

@coocood
Copy link
Member Author

coocood commented Jan 2, 2018

@zhangjinpeng1987 PTAL

ddl/ddl_api.go Outdated
@@ -932,6 +956,36 @@ func (d *ddl) RebaseAutoID(ctx context.Context, ident ast.Ident, newBase int64)
return errors.Trace(err)
}

func (d *ddl) ShardRowID(ctx context.Context, ident ast.Ident, uVal uint64) error {
Copy link
Member

Choose a reason for hiding this comment

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

add comment for this function

ddl/ddl_api.go Outdated
return errors.Trace(err)
}

func (d *ddl) createJobByIdent(ident ast.Ident) (*model.Job, error) {
Copy link
Member

Choose a reason for hiding this comment

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

createJobByName is a better name?

Copy link
Member Author

Choose a reason for hiding this comment

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

ident has both Schema and TableName, it's not a single name.

Copy link
Member

Choose a reason for hiding this comment

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

createJobForTable

Copy link
Member

Choose a reason for hiding this comment

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

createJobForTable is a better name.

var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], startTS)
hashVal := int64(murmur3.Sum32(buf[:]))
return (hashVal & (1<<t.meta.ShardRowIDBits - 1)) << (64 - t.meta.ShardRowIDBits - 1)
Copy link
Member

Choose a reason for hiding this comment

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

Could we make the (1<<t.meta.ShardRowIDBits - 1)) << (64 - t.meta.ShardRowIDBits - 1) a const?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, t.meta.ShardRowIDBits is different for each table.

ddl/ddl_api.go Outdated
}
t, err := is.TableByName(ident.Schema, ident.Name)
if err != nil {
return nil, errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ident.Schema, ident.Name))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this trace ?

ddl/ddl_api.go Outdated
switch opt.Tp {
case ast.TableOptionShardRowID:
if opt.UintValue > 15 {
opt.UintValue = 15
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we define a constant as 15? We use it in multiple places.

@coocood
Copy link
Member Author

coocood commented Jan 3, 2018

@zimulala @shenli PTAL

if (h >> 56) > 0 {
hasShardedID = true
}
count++
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we put this line to line 355? Then we can make sure all IDs are sharded ID.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, sharded ID still include shard 0, then (h >> 56) == 0.

c.Assert(tk.Se.NewTxn(), IsNil)
err = tbl.IterRecords(tk.Se, tbl.FirstKey(), nil, func(h int64, rec []types.Datum, cols []*table.Column) (more bool, err error) {
c.Assert(h, GreaterEqual, int64(0))
if (h >> 56) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why there is 56? Can we use a temporary variable to replace it? Then make it more readable.

@coocood
Copy link
Member Author

coocood commented Jan 3, 2018

@shenli @zimulala PTAL

ddl/ddl_api.go Outdated
return errors.Trace(err)
}

func (d *ddl) createJobByIdent(ident ast.Ident) (*model.Job, error) {
Copy link
Member

Choose a reason for hiding this comment

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

createJobForTable is a better name.

@@ -27,7 +27,7 @@ import (
)

// Test needs to change it, so it's a variable.
var step = int64(5000)
var step = int64(30000)
Copy link
Member

Choose a reason for hiding this comment

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

We should add a metrics for auto-id allocation latter.

executor/show.go Outdated
@@ -623,6 +623,10 @@ func (e *ShowExec) fetchShowCreateTable() error {
}
}

if tb.Meta().ShardRowIDBits > 0 {
buf.WriteString(fmt.Sprintf(" SHARD_ROW_ID_BITS=%d", tb.Meta().ShardRowIDBits))
Copy link
Member

Choose a reason for hiding this comment

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

We should also use special-comment for show create table. Or users will meet error when migrating data from TiDB to MySQL.

}
upperQuery := strings.ToUpper(ddlQuery)
reg, err := regexp.Compile(`SHARD_ROW_ID_BITS\s*=\s*\d+`)
terror.Log(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

If err isn't nil, reg may be nil.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's just added to pass errcheck, it never returns nil

@zimulala
Copy link
Contributor

zimulala commented Jan 4, 2018

LGTM

@zimulala zimulala added the status/LGT1 Indicates that a PR has LGTM 1. label Jan 4, 2018
@shenli
Copy link
Member

shenli commented Jan 4, 2018

LGTM
Please resolve the conflicts.

@coocood
Copy link
Member Author

coocood commented Jan 4, 2018

/run-all-tests

@coocood coocood added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jan 4, 2018
@coocood coocood merged commit 04ef7d7 into pingcap:master Jan 4, 2018
@coocood coocood deleted the shard-row-id branch January 4, 2018 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants