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

*: implement the sequence allocator logic. #14829

Merged
merged 24 commits into from
Feb 26, 2020
Merged

Conversation

AilinKid
Copy link
Contributor

What problem does this PR solve?

implement the sequence allocator logic
this PR implements the complete functionality of TiDB sequence.
For example:

create sequence seq start 1 increment -2 minvalue -10 maxvalue 10 cache 3 cycle.
select nextval(seq) will get: 
[1, -1, -3]   [-5, -7, -9]   [10, 8, 6] ...

setval(seq, num) will change the sequence value to a certain position.
1: num previous to now base, return NULL,
2: return the num, and rebase the cache(in cache) / rebase the meta (invalid the cache).

`lastval(seq)` will show the last allocated value in your session. 
1: if `nextval` hasn't been called, return NULL, 
2: otherwise, return the last value.

What is changed and how it works?

1: do sequence cache in tables, a sequence table can be easily notified with cache empty and write the binlog.
2: implement the sequence positive-growth & negative-growth allocation under MIN, MAX limitation.
3: add the allocSeqCache interface to allocator, cause it differs from autoid a lot.
4: add some tests at SQL and KV level.

Check List

Tests

  • Unit test
  • Integration test

Code changes

  • Has interface methods change

Related changes

  • Need to update the documentation

Release note

  • implement the sequence allocator logic.

@AilinKid AilinKid requested a review from bb7133 February 18, 2020 07:30
@AilinKid AilinKid added require-LGT3 Indicates that the PR requires three LGTM. type/new-feature labels Feb 18, 2020
table/column.go Show resolved Hide resolved
startTime := time.Now()
err := kv.RunInNewTxn(alloc.store, true, func(txn kv.Transaction) error {
m := meta.NewMeta(txn)
currentEnd, err1 := getAutoIDByAllocType(m, alloc.dbID, tableID, alloc.allocType)
Copy link
Contributor

Choose a reason for hiding this comment

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

The variable name err is ok at here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it.

meta/autoid/autoid.go Outdated Show resolved Hide resolved
return m.SetSequenceCycle(dbID, tableID, round)
}

// getSequenceCycleRound is used to get whether the sequence is already in cycle.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// getSequenceCycleRound is used to get whether the sequence is already in cycle.
// getSequenceCycleRound is used to get the flag `round`, which indicates whether the sequence is already in cycle.

Name: model.NewCIStr("seq"),
Sequence: seq,
}
if seq.Increment >= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

seq.Increment >= 0 is always true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this test, it is. addressed.

Copy link
Contributor

@crazycs520 crazycs520 left a comment

Choose a reason for hiding this comment

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

image

Is this expect?

@AilinKid
Copy link
Contributor Author

image

Is this expect?

yes, in TiDB set value will not change the next valid value.
Mariadb: next value = set value + increment
TiDB: next value is still in the formula: offset + increment * n, we ganna find the next valid value.

@AilinKid
Copy link
Contributor Author

/run-unit-test

meta/autoid/autoid.go Outdated Show resolved Hide resolved
meta/autoid/autoid.go Outdated Show resolved Hide resolved
meta/autoid/autoid.go Outdated Show resolved Hide resolved
@AilinKid AilinKid removed the require-LGT3 Indicates that the PR requires three LGTM. label Feb 21, 2020
@AilinKid AilinKid added this to the v4.0.0-beta.1 milestone Feb 21, 2020
@Deardrops Deardrops self-requested a review February 21, 2020 04:00
table/tables/tables.go Outdated Show resolved Hide resolved
table/tables/tables.go Outdated Show resolved Hide resolved
table/tables/tables.go Outdated Show resolved Hide resolved
// base < end when increment > 0.
// base > end when increment < 0.
end int64
base int64
Copy link
Contributor

Choose a reason for hiding this comment

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

How about naming them low and high? high is always greater than low.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not readlly, if use low/high, that will mean low < high when increment > 0, but low > high when increment < 0, which is weird.

meta/autoid/autoid.go Outdated Show resolved Hide resolved
meta/autoid/autoid_test.go Outdated Show resolved Hide resolved
wg.Add(1)
go func(num int) {
defer wg.Done()
time.Sleep(time.Duration(num%10) * time.Microsecond)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we remove sleep here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This tried to simulate to allocate sequence cache at different start-time currently.
The sleep duration is small with a few Microsecond, won't postpone the test time.

meta/autoid/autoid_test.go Outdated Show resolved Hide resolved
if _, ok := m[i]; ok {
errCh <- fmt.Errorf("duplicate id:%v", i)
errFlag = true
mu.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

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

Duplicated unlock here?

Copy link
Contributor Author

@AilinKid AilinKid Feb 24, 2020

Choose a reason for hiding this comment

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

Not readlly, errFlag = true will break twice, which will skip the normal unlock.

s.tk.MustExec("select setval(seq, 100)")
s.tk.MustExec("insert into t values(lastval(seq)),(-1),(nextval(seq))")
s.tk.MustQuery("select * from t").Check(testkit.Rows("14", "-1", "101"))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Add some test for using nextval(), setval(), lastval() in generated columns' expression.

Copy link
Contributor

Choose a reason for hiding this comment

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

We store sequence value by int64. Could you add some tests for integer overflow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nice suggestion.

@AilinKid
Copy link
Contributor Author

/build

@AilinKid AilinKid added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Feb 25, 2020
@AilinKid
Copy link
Contributor Author

/run-all-tests

@AilinKid
Copy link
Contributor Author

/run-all-tests

@AilinKid
Copy link
Contributor Author

/run-unit-test

Copy link
Contributor

@tangenta tangenta left a comment

Choose a reason for hiding this comment

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

LGTM

@tangenta tangenta added status/LGT3 The PR has already had 3 LGTM. and removed status/LGT2 Indicates that a PR has LGTM 2. labels Feb 26, 2020
@AilinKid
Copy link
Contributor Author

/merge

@sre-bot sre-bot added the status/can-merge Indicates a PR has been approved by a committer. label Feb 26, 2020
@sre-bot
Copy link
Contributor

sre-bot commented Feb 26, 2020

/run-all-tests

@sre-bot
Copy link
Contributor

sre-bot commented Feb 26, 2020

@AilinKid merge failed.

@AilinKid
Copy link
Contributor Author

/build

@AilinKid
Copy link
Contributor Author

/merge

@sre-bot
Copy link
Contributor

sre-bot commented Feb 26, 2020

/run-all-tests

@sre-bot sre-bot merged commit 0bbf1d9 into pingcap:master Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/can-merge Indicates a PR has been approved by a committer. status/LGT3 The PR has already had 3 LGTM. type/new-feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants