-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Conversation
meta/autoid/autoid.go
Outdated
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
return m.SetSequenceCycle(dbID, tableID, round) | ||
} | ||
|
||
// getSequenceCycleRound is used to get whether the sequence is already in cycle. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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. |
meta/autoid/autoid_test.go
Outdated
Name: model.NewCIStr("seq"), | ||
Sequence: seq, | ||
} | ||
if seq.Increment >= 0 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/run-unit-test |
// base < end when increment > 0. | ||
// base > end when increment < 0. | ||
end int64 | ||
base int64 |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
wg.Add(1) | ||
go func(num int) { | ||
defer wg.Done() | ||
time.Sleep(time.Duration(num%10) * time.Microsecond) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
if _, ok := m[i]; ok { | ||
errCh <- fmt.Errorf("duplicate id:%v", i) | ||
errFlag = true | ||
mu.Unlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated unlock here?
There was a problem hiding this comment.
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")) | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice suggestion.
/build |
/run-all-tests |
/run-all-tests |
/run-unit-test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
/merge |
/run-all-tests |
@AilinKid merge failed. |
/build |
/merge |
/run-all-tests |
What problem does this PR solve?
implement the sequence allocator logic
this PR implements the complete functionality of TiDB sequence.
For example:
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
Code changes
Related changes
Release note