{"action":"created","comment":{"url":"https://api.github.com/repos/pingcap/tikv/pulls/comments/60521244","id":60521244,"diff_hunk":"@@ -12,59 +12,49 @@\n // limitations under the License.\n \n use super::{Coprocessor, RegionObserver, ObserverContext, Result as CopResult};\n+use util::codec::table;\n \n use kvproto::raft_cmdpb::{SplitRequest, AdminRequest, Request, AdminResponse, Response,\n                           AdminCmdType};\n use protobuf::RepeatedField;\n-use util::codec::bytes;\n use std::result::Result as StdResult;\n \n /// `SplitObserver` adjusts the split key so that it won't seperate\n-/// the data of a row into two region.\n+/// the data of a row into two region. It adjusts the key according\n+/// to the key format of `TiDB`.\n pub struct SplitObserver;\n \n-pub const TABLE_PREFIX: &'static [u8] = b\"t\";\n-pub const TABLE_ROW_MARK: &'static [u8] = b\"_r\";\n+const VERSION_LENGTH: usize = 8;\n \n type Result<T> = StdResult<T, String>;\n \n impl SplitObserver {\n-    fn extract_sql_key(&self, split: &SplitRequest) -> Result<Vec<u8>> {\n+    fn on_split(&mut self, _: &mut ObserverContext, split: &mut SplitRequest) -> Result<()> {\n         if !split.has_split_key() {\n             return Err(\"split key is expected!\".to_owned());\n         }\n-        let split_key = split.get_split_key();\n-        bytes::decode_bytes(split_key)\n-            .map_err(|e| format!(\"invalid key {:?}: {}\", split_key, e))\n-            .map(|r| r.0)\n-    }\n-\n-    fn on_split(&mut self, _: &mut ObserverContext, split: &mut SplitRequest) -> Result<()> {\n-        let key = try!(self.extract_sql_key(split));\n-\n-        // format of a key is TABLE_PREFIX + table_id + TABLE_ROW_MARK + handle or\n-        // TABLE_PREFIX + table_id + TABLE_INDEX_MARK\n+        let mut key = split.get_split_key().to_vec();\n \n-        if !key.starts_with(TABLE_PREFIX) {\n-            // if it's not a data key, just split.\n+        if key.len() < VERSION_LENGTH {\n+            // bypass non-mvcc key.\n             return Ok(());\n         }\n-        // table id is a u64 and we only care about row data.\n-        let mark_index = TABLE_PREFIX.len() + 8;\n-        let new_key: &[u8];\n-        if key.len() > mark_index && key[mark_index..].starts_with(TABLE_ROW_MARK) {\n-            // handle is a u64 too.\n-            let expected_len = TABLE_PREFIX.len() + 8 + TABLE_ROW_MARK.len() + 8;\n-            if key.len() < expected_len {\n-                return Err(format!(\"invalid key {:?}; len should be at least {}\",\n-                                   key,\n-                                   expected_len));\n-            }\n-            new_key = &key[..expected_len];\n+\n+        // format of a key is TABLE_PREFIX + table_id + TABLE_ROW_MARK + handle + column_id\n+        // + version or TABLE_PREFIX + table_id + TABLE_INDEX_MARK + index_id + values + version\n+        // or meta_key + version\n+\n+        let table_prefix_len = table::TABLE_PREFIX.len() + table::ID_LEN;\n+        if key.starts_with(table::TABLE_PREFIX) && key.len() > table::PREFIX_LEN + table::ID_LEN &&\n+           key[table_prefix_len..].starts_with(table::RECORD_PREFIX_SEP) {\n+            // row key, truncate to handle\n+            key.truncate(table::PREFIX_LEN + table::ID_LEN);","path":"src/raftstore/coprocessor/split_observer.rs","position":70,"original_position":70,"commit_id":"c6f2ec7997f3ad93d92d3904f91953b2cfa2a484","original_commit_id":"c6f2ec7997f3ad93d92d3904f91953b2cfa2a484","user":{"login":"coocood","id":891222,"avatar_url":"https://avatars.githubusercontent.com/u/891222?v=3","gravatar_id":"","url":"https://api.github.com/users/coocood","html_url":"https://github.com/coocood","followers_url":"https://api.github.com/users/coocood/followers","following_url":"https://api.github.com/users/coocood/following{/other_user}","gists_url":"https://api.github.com/users/coocood/gists{/gist_id}","starred_url":"https://api.github.com/users/coocood/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coocood/subscriptions","organizations_url":"https://api.github.com/users/coocood/orgs","repos_url":"https://api.github.com/users/coocood/repos","events_url":"https://api.github.com/users/coocood/events{/privacy}","received_events_url":"https://api.github.com/users/coocood/received_events","type":"User","site_admin":false},"body":"It's OK to truncate to a certain length for now, but in the future, we will use string handle, the prefix length will change.\r\n\r\nWe can append row key a column ID 0, so column key and row key would have the same length, then truncate suffix will ensure a row will not be split.\r\n","created_at":"2016-04-21T03:14:43Z","updated_at":"2016-04-21T03:14:43Z","html_url":"https://github.com/pingcap/tikv/pull/446#discussion_r60521244","pull_request_url":"https://api.github.com/repos/pingcap/tikv/pulls/446","_links":{"self":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/comments/60521244"},"html":{"href":"https://github.com/pingcap/tikv/pull/446#discussion_r60521244"},"pull_request":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/446"}}},"pull_request":{"url":"https://api.github.com/repos/pingcap/tikv/pulls/446","id":67173452,"html_url":"https://github.com/pingcap/tikv/pull/446","diff_url":"https://github.com/pingcap/tikv/pull/446.diff","patch_url":"https://github.com/pingcap/tikv/pull/446.patch","issue_url":"https://api.github.com/repos/pingcap/tikv/issues/446","number":446,"state":"open","locked":false,"title":"observer: fix split error","user":{"login":"BusyJay","id":1701473,"avatar_url":"https://avatars.githubusercontent.com/u/1701473?v=3","gravatar_id":"","url":"https://api.github.com/users/BusyJay","html_url":"https://github.com/BusyJay","followers_url":"https://api.github.com/users/BusyJay/followers","following_url":"https://api.github.com/users/BusyJay/following{/other_user}","gists_url":"https://api.github.com/users/BusyJay/gists{/gist_id}","starred_url":"https://api.github.com/users/BusyJay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BusyJay/subscriptions","organizations_url":"https://api.github.com/users/BusyJay/orgs","repos_url":"https://api.github.com/users/BusyJay/repos","events_url":"https://api.github.com/users/BusyJay/events{/privacy}","received_events_url":"https://api.github.com/users/BusyJay/received_events","type":"User","site_admin":false},"body":"@ngaut @siddontang @coocood PTAL","created_at":"2016-04-20T11:54:00Z","updated_at":"2016-04-21T03:14:43Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2cb997d4877f2fa627aa78c4c31d3429c1ede156","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/pingcap/tikv/pulls/446/commits","review_comments_url":"https://api.github.com/repos/pingcap/tikv/pulls/446/comments","review_comment_url":"https://api.github.com/repos/pingcap/tikv/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pingcap/tikv/issues/446/comments","statuses_url":"https://api.github.com/repos/pingcap/tikv/statuses/c6f2ec7997f3ad93d92d3904f91953b2cfa2a484","head":{"label":"pingcap:busyjay/fix-split","ref":"busyjay/fix-split","sha":"c6f2ec7997f3ad93d92d3904f91953b2cfa2a484","user":{"login":"pingcap","id":11855343,"avatar_url":"https://avatars.githubusercontent.com/u/11855343?v=3","gravatar_id":"","url":"https://api.github.com/users/pingcap","html_url":"https://github.com/pingcap","followers_url":"https://api.github.com/users/pingcap/followers","following_url":"https://api.github.com/users/pingcap/following{/other_user}","gists_url":"https://api.github.com/users/pingcap/gists{/gist_id}","starred_url":"https://api.github.com/users/pingcap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pingcap/subscriptions","organizations_url":"https://api.github.com/users/pingcap/orgs","repos_url":"https://api.github.com/users/pingcap/repos","events_url":"https://api.github.com/users/pingcap/events{/privacy}","received_events_url":"https://api.github.com/users/pingcap/received_events","type":"Organization","site_admin":false},"repo":{"id":48833910,"name":"tikv","full_name":"pingcap/tikv","owner":{"login":"pingcap","id":11855343,"avatar_url":"https://avatars.githubusercontent.com/u/11855343?v=3","gravatar_id":"","url":"https://api.github.com/users/pingcap","html_url":"https://github.com/pingcap","followers_url":"https://api.github.com/users/pingcap/followers","following_url":"https://api.github.com/users/pingcap/following{/other_user}","gists_url":"https://api.github.com/users/pingcap/gists{/gist_id}","starred_url":"https://api.github.com/users/pingcap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pingcap/subscriptions","organizations_url":"https://api.github.com/users/pingcap/orgs","repos_url":"https://api.github.com/users/pingcap/repos","events_url":"https://api.github.com/users/pingcap/events{/privacy}","received_events_url":"https://api.github.com/users/pingcap/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/pingcap/tikv","description":"Distributed transactional key value database powered by Rust and Raft","fork":false,"url":"https://api.github.com/repos/pingcap/tikv","forks_url":"https://api.github.com/repos/pingcap/tikv/forks","keys_url":"https://api.github.com/repos/pingcap/tikv/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pingcap/tikv/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pingcap/tikv/teams","hooks_url":"https://api.github.com/repos/pingcap/tikv/hooks","issue_events_url":"https://api.github.com/repos/pingcap/tikv/issues/events{/number}","events_url":"https://api.github.com/repos/pingcap/tikv/events","assignees_url":"https://api.github.com/repos/pingcap/tikv/assignees{/user}","branches_url":"https://api.github.com/repos/pingcap/tikv/branches{/branch}","tags_url":"https://api.github.com/repos/pingcap/tikv/tags","blobs_url":"https://api.github.com/repos/pingcap/tikv/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pingcap/tikv/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pingcap/tikv/git/refs{/sha}","trees_url":"https://api.github.com/repos/pingcap/tikv/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pingcap/tikv/statuses/{sha}","languages_url":"https://api.github.com/repos/pingcap/tikv/languages","stargazers_url":"https://api.github.com/repos/pingcap/tikv/stargazers","contributors_url":"https://api.github.com/repos/pingcap/tikv/contributors","subscribers_url":"https://api.github.com/repos/pingcap/tikv/subscribers","subscription_url":"https://api.github.com/repos/pingcap/tikv/subscription","commits_url":"https://api.github.com/repos/pingcap/tikv/commits{/sha}","git_commits_url":"https://api.github.com/repos/pingcap/tikv/git/commits{/sha}","comments_url":"https://api.github.com/repos/pingcap/tikv/comments{/number}","issue_comment_url":"https://api.github.com/repos/pingcap/tikv/issues/comments{/number}","contents_url":"https://api.github.com/repos/pingcap/tikv/contents/{+path}","compare_url":"https://api.github.com/repos/pingcap/tikv/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pingcap/tikv/merges","archive_url":"https://api.github.com/repos/pingcap/tikv/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pingcap/tikv/downloads","issues_url":"https://api.github.com/repos/pingcap/tikv/issues{/number}","pulls_url":"https://api.github.com/repos/pingcap/tikv/pulls{/number}","milestones_url":"https://api.github.com/repos/pingcap/tikv/milestones{/number}","notifications_url":"https://api.github.com/repos/pingcap/tikv/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pingcap/tikv/labels{/name}","releases_url":"https://api.github.com/repos/pingcap/tikv/releases{/id}","deployments_url":"https://api.github.com/repos/pingcap/tikv/deployments","created_at":"2015-12-31T04:51:32Z","updated_at":"2016-04-20T23:41:44Z","pushed_at":"2016-04-21T03:05:27Z","git_url":"git://github.com/pingcap/tikv.git","ssh_url":"git@github.com:pingcap/tikv.git","clone_url":"https://github.com/pingcap/tikv.git","svn_url":"https://github.com/pingcap/tikv","homepage":"","size":2019,"stargazers_count":552,"watchers_count":552,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":32,"mirror_url":null,"open_issues_count":33,"forks":32,"open_issues":33,"watchers":552,"default_branch":"master"}},"base":{"label":"pingcap:master","ref":"master","sha":"d2ad10de7086cef1fe6667eb932723e9137131d1","user":{"login":"pingcap","id":11855343,"avatar_url":"https://avatars.githubusercontent.com/u/11855343?v=3","gravatar_id":"","url":"https://api.github.com/users/pingcap","html_url":"https://github.com/pingcap","followers_url":"https://api.github.com/users/pingcap/followers","following_url":"https://api.github.com/users/pingcap/following{/other_user}","gists_url":"https://api.github.com/users/pingcap/gists{/gist_id}","starred_url":"https://api.github.com/users/pingcap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pingcap/subscriptions","organizations_url":"https://api.github.com/users/pingcap/orgs","repos_url":"https://api.github.com/users/pingcap/repos","events_url":"https://api.github.com/users/pingcap/events{/privacy}","received_events_url":"https://api.github.com/users/pingcap/received_events","type":"Organization","site_admin":false},"repo":{"id":48833910,"name":"tikv","full_name":"pingcap/tikv","owner":{"login":"pingcap","id":11855343,"avatar_url":"https://avatars.githubusercontent.com/u/11855343?v=3","gravatar_id":"","url":"https://api.github.com/users/pingcap","html_url":"https://github.com/pingcap","followers_url":"https://api.github.com/users/pingcap/followers","following_url":"https://api.github.com/users/pingcap/following{/other_user}","gists_url":"https://api.github.com/users/pingcap/gists{/gist_id}","starred_url":"https://api.github.com/users/pingcap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pingcap/subscriptions","organizations_url":"https://api.github.com/users/pingcap/orgs","repos_url":"https://api.github.com/users/pingcap/repos","events_url":"https://api.github.com/users/pingcap/events{/privacy}","received_events_url":"https://api.github.com/users/pingcap/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/pingcap/tikv","description":"Distributed transactional key value database powered by Rust and Raft","fork":false,"url":"https://api.github.com/repos/pingcap/tikv","forks_url":"https://api.github.com/repos/pingcap/tikv/forks","keys_url":"https://api.github.com/repos/pingcap/tikv/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pingcap/tikv/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pingcap/tikv/teams","hooks_url":"https://api.github.com/repos/pingcap/tikv/hooks","issue_events_url":"https://api.github.com/repos/pingcap/tikv/issues/events{/number}","events_url":"https://api.github.com/repos/pingcap/tikv/events","assignees_url":"https://api.github.com/repos/pingcap/tikv/assignees{/user}","branches_url":"https://api.github.com/repos/pingcap/tikv/branches{/branch}","tags_url":"https://api.github.com/repos/pingcap/tikv/tags","blobs_url":"https://api.github.com/repos/pingcap/tikv/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pingcap/tikv/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pingcap/tikv/git/refs{/sha}","trees_url":"https://api.github.com/repos/pingcap/tikv/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pingcap/tikv/statuses/{sha}","languages_url":"https://api.github.com/repos/pingcap/tikv/languages","stargazers_url":"https://api.github.com/repos/pingcap/tikv/stargazers","contributors_url":"https://api.github.com/repos/pingcap/tikv/contributors","subscribers_url":"https://api.github.com/repos/pingcap/tikv/subscribers","subscription_url":"https://api.github.com/repos/pingcap/tikv/subscription","commits_url":"https://api.github.com/repos/pingcap/tikv/commits{/sha}","git_commits_url":"https://api.github.com/repos/pingcap/tikv/git/commits{/sha}","comments_url":"https://api.github.com/repos/pingcap/tikv/comments{/number}","issue_comment_url":"https://api.github.com/repos/pingcap/tikv/issues/comments{/number}","contents_url":"https://api.github.com/repos/pingcap/tikv/contents/{+path}","compare_url":"https://api.github.com/repos/pingcap/tikv/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pingcap/tikv/merges","archive_url":"https://api.github.com/repos/pingcap/tikv/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pingcap/tikv/downloads","issues_url":"https://api.github.com/repos/pingcap/tikv/issues{/number}","pulls_url":"https://api.github.com/repos/pingcap/tikv/pulls{/number}","milestones_url":"https://api.github.com/repos/pingcap/tikv/milestones{/number}","notifications_url":"https://api.github.com/repos/pingcap/tikv/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pingcap/tikv/labels{/name}","releases_url":"https://api.github.com/repos/pingcap/tikv/releases{/id}","deployments_url":"https://api.github.com/repos/pingcap/tikv/deployments","created_at":"2015-12-31T04:51:32Z","updated_at":"2016-04-20T23:41:44Z","pushed_at":"2016-04-21T03:05:27Z","git_url":"git://github.com/pingcap/tikv.git","ssh_url":"git@github.com:pingcap/tikv.git","clone_url":"https://github.com/pingcap/tikv.git","svn_url":"https://github.com/pingcap/tikv","homepage":"","size":2019,"stargazers_count":552,"watchers_count":552,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":32,"mirror_url":null,"open_issues_count":33,"forks":32,"open_issues":33,"watchers":552,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/446"},"html":{"href":"https://github.com/pingcap/tikv/pull/446"},"issue":{"href":"https://api.github.com/repos/pingcap/tikv/issues/446"},"comments":{"href":"https://api.github.com/repos/pingcap/tikv/issues/446/comments"},"review_comments":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/446/comments"},"review_comment":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/446/commits"},"statuses":{"href":"https://api.github.com/repos/pingcap/tikv/statuses/c6f2ec7997f3ad93d92d3904f91953b2cfa2a484"}}}}