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 reverse iterator #9564

Closed
wants to merge 18 commits into from
Closed

Implement reverse iterator #9564

wants to merge 18 commits into from

Conversation

arthurkiller
Copy link

What problem does this PR solve?

Request IterReverse for long.

See also #7429

What is changed and how it works?

IIRC we asking for reverse iterator since last summer, and the job was block on the dependency last year (#7429).

Finally I implemented reverseIterator for tikv SDK by add a new member for tikv.Scanner.

  • getRegion on reverse will return the key from start to end in descend order
  • on region border, set nextStartKey to the StartKey.Next(), cause the region will not contains StartKey.
  • Once StartKey is blank, we met the very first region stop the iterator.

Check List

Tests

  • Manual test (add detailed scripts or steps below)
1. write keys into tikv
2. split the region
3. find any region start key, and reverseIter it
4. check if things going right

Side effects
rely on these functions already implemented

  • regionCache.LocateEndKey
  • pb.ScanRequest.Reverse = true

and changed this internal function

func newScanner(snapshot *tikvSnapshot, startKey []byte, endKey []byte, batchSize int)
func newScanner(snapshot *tikvSnapshot, startKey []byte, endKey []byte, batchSize int, reverse bool)

Related changes

  • Need to update the documentation
  • Need to be included in the release note

@shenli
Copy link
Member

shenli commented Mar 5, 2019

@arthurkiller Manual test is not enough. Could you add some unit tests?

@arthurkiller
Copy link
Author

@shenli thx for reply and I will fix the unit test then. But I'm not sure if tikv mock is easy to implement.

@shenli
Copy link
Member

shenli commented Mar 5, 2019

@arthurkiller Thanks for your contribution! If you need any help, please let us know. :)

@siddontang siddontang requested a review from disksing March 5, 2019 15:05
store/tikv/scan.go Outdated Show resolved Hide resolved
@arthurkiller
Copy link
Author

arthurkiller commented Mar 6, 2019 via email

@disksing
Copy link
Contributor

disksing commented Mar 6, 2019

@arthurkiller I think it's ok to update kv.IterReverse later. But as TiKV already supports it, it is better to support in the scanner.

@arthurkiller
Copy link
Author

arthurkiller commented Mar 6, 2019

@shenli Hey, I have added unit test for scanner scan reverse.
@disksing and endKey support for scan reverse now.

@shenli
Copy link
Member

shenli commented Mar 6, 2019

@arthurkiller Cool!

@arthurkiller
Copy link
Author

----------------------------------------------------------------------
FAIL: scan_mock_test.go:29: testScanMockSuite.TestScanMultipleRegions

scan_mock_test.go:56:
    c.Assert([]byte{ch}, BytesEquals, []byte(scanner.Key()))
... bytes_one []uint8 = []byte{0x7a}
... bytes_two []uint8 = []byte(nil)

PASS: scan_test.go:66: testScanSuite.TestScan   0.064s

----------------------------------------------------------------------
FAIL: scan_test.go:141: testScanSuite.TestScanReverse

scan_test.go:187:
    check(c, scan, rowNum, false)
scan_test.go:145:
    c.Assert([]byte(k), BytesEquals, encodeKey(s.prefix, s08d("key", i)))
... bytes_one []uint8 = []byte{0x73, 0x65, 0x65, 0x6b, 0x5f, 0x31, 0x35, 0x35, 0xff, 0x31, 0x38, 0x37, 0x34, 0x35, 0x35, 0x31, 0x5f, 0xff, 0x6b, 0x65, 0x79, 0x30, 0x30, 0x30, 0x30, 0x30, 0xff, 0x30, 0x30, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0xfa}
... bytes_two []uint8 = []byte{0x73, 0x65, 0x65, 0x6b, 0x5f, 0x31, 0x35, 0x35, 0xff, 0x31, 0x38, 0x37, 0x34, 0x35, 0x35, 0x31, 0x5f, 0xff, 0x6b, 0x65, 0x79, 0x30, 0x30, 0x30, 0x30, 0x30, 0xff, 0x30, 0x30, 0x31, 0x0, 0x0, 0x0, 0x0, 0x0, 0xfa}

seems SDK did not mock the reverse yet

@arthurkiller
Copy link
Author

arthurkiller commented Mar 6, 2019

anyway I have added the test case in this pr

@shenli have you got any plan to add this mock?

store/tikv/scan.go Outdated Show resolved Hide resolved
store/tikv/scan.go Outdated Show resolved Hide resolved
@disksing
Copy link
Contributor

disksing commented Mar 8, 2019

Seems the tests are failing. @arthurkiller

@arthurkiller
Copy link
Author

----------------------------------------------------------------------
FAIL: scan_mock_test.go:29: testScanMockSuite.TestScanMultipleRegions

scan_mock_test.go:56:
    c.Assert([]byte{ch}, BytesEquals, []byte(scanner.Key()))
... bytes_one []uint8 = []byte{0x7a}
... bytes_two []uint8 = []byte(nil)

PASS: scan_test.go:66: testScanSuite.TestScan   0.064s

----------------------------------------------------------------------
FAIL: scan_test.go:141: testScanSuite.TestScanReverse

scan_test.go:187:
    check(c, scan, rowNum, false)
scan_test.go:145:
    c.Assert([]byte(k), BytesEquals, encodeKey(s.prefix, s08d("key", i)))
... bytes_one []uint8 = []byte{0x73, 0x65, 0x65, 0x6b, 0x5f, 0x31, 0x35, 0x35, 0xff, 0x31, 0x38, 0x37, 0x34, 0x35, 0x35, 0x31, 0x5f, 0xff, 0x6b, 0x65, 0x79, 0x30, 0x30, 0x30, 0x30, 0x30, 0xff, 0x30, 0x30, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0xfa}
... bytes_two []uint8 = []byte{0x73, 0x65, 0x65, 0x6b, 0x5f, 0x31, 0x35, 0x35, 0xff, 0x31, 0x38, 0x37, 0x34, 0x35, 0x35, 0x31, 0x5f, 0xff, 0x6b, 0x65, 0x79, 0x30, 0x30, 0x30, 0x30, 0x30, 0xff, 0x30, 0x30, 0x31, 0x0, 0x0, 0x0, 0x0, 0x0, 0xfa}

seems SDK did not mock the reverse yet @disksing

@zz-jason
Copy link
Member

zz-jason commented Aug 3, 2019

@arthurkiller Could update the master branch and resolve conflicts?

@sre-bot
Copy link
Contributor

sre-bot commented Aug 3, 2019

Hi contributor, thanks for your PR.

This patch needs to be approved by someone of admins. They should reply with "/ok-to-test" to accept this PR for running test automatically.

@zz-jason zz-jason added contribution This PR is from a community contributor. status/ReqChange component/tikv labels Aug 3, 2019
@zz-jason
Copy link
Member

zz-jason commented Aug 3, 2019

please follow CONTRIBUTING.md to refine the PR title.

@arthurkiller
Copy link
Author

arthurkiller commented Aug 5, 2019

@zz-jason okay, I will finish this later this week

@arthurkiller
Copy link
Author

@zz-jason @disksing

Do you guys have already implemented reverse seek? I found the code has been added at 2019-05-31 23:40:09 +0800

crazycs           2019-05-31 23:40:09 +0800)|  216         if !s.reverse {

@arthurkiller
Copy link
Author

@crazycs520 Seems you have added the IterReverse already, is that right?

@arthurkiller
Copy link
Author

@disksing @shenli @zz-jason ping

@crazycs520
Copy link
Contributor

@arthurkiller, Sorry for that, I don't know this pr before, I write PR #10152 to speed up admin show ddl jobs and implement the reverse scan.

@arthurkiller
Copy link
Author

nvm, so we can use IterReverse now, and this pr can be closed

CC @shafreeck

@crazycs520
Copy link
Contributor

@arthurkiller, Thanks for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/tikv contribution This PR is from a community contributor.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants