{"action":"created","comment":{"url":"https://api.github.com/repos/pingcap/tikv/pulls/comments/175972091","pull_request_review_id":105592953,"id":175972091,"diff_hunk":"@@ -14,46 +14,116 @@\n use std::fmt;\n use std::sync::Arc;\n \n-use uuid::Uuid;\n+use futures::Future;\n+use kvproto::metapb::Region;\n use kvproto::importpb::SSTMeta;\n \n+use pd::PdClient;\n use import::SSTImporter;\n+use raftstore::store::Msg;\n+use raftstore::store::util::is_epoch_stale;\n+use util::transport::SendCh;\n use util::worker::Runnable;\n \n pub enum Task {\n-    DeleteSST { sst: SSTMeta },\n+    DeleteSST { ssts: Vec<SSTMeta> },\n+    ValidateSST { ssts: Vec<SSTMeta> },\n }\n \n impl fmt::Display for Task {\n     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {\n         match *self {\n-            Task::DeleteSST { ref sst } => match Uuid::from_bytes(sst.get_uuid()) {\n-                Ok(uuid) => write!(f, \"Delete SST {}\", uuid),\n-                Err(e) => write!(f, \"Delete SST {:?}\", e),\n-            },\n+            Task::DeleteSST { ref ssts } => write!(f, \"Delete {} ssts\", ssts.len()),\n+            Task::ValidateSST { ref ssts } => write!(f, \"Validate {} ssts\", ssts.len()),\n         }\n     }\n }\n \n-pub struct Runner {\n+pub struct Runner<C> {\n+    store_id: u64,\n+    store_ch: SendCh<Msg>,\n     importer: Arc<SSTImporter>,\n+    pd_client: Arc<C>,\n }\n \n-impl Runner {\n-    pub fn new(importer: Arc<SSTImporter>) -> Runner {\n-        Runner { importer: importer }\n+impl<C: PdClient> Runner<C> {\n+    pub fn new(\n+        store_id: u64,\n+        store_ch: SendCh<Msg>,\n+        importer: Arc<SSTImporter>,\n+        pd_client: Arc<C>,\n+    ) -> Runner<C> {\n+        Runner {\n+            store_id: store_id,\n+            store_ch: store_ch,\n+            importer: importer,\n+            pd_client: pd_client,\n+        }\n     }\n \n-    fn handle_delete_sst(&self, sst: SSTMeta) {\n-        let _ = self.importer.delete(&sst);\n+    fn handle_delete_sst(&self, ssts: Vec<SSTMeta>) {\n+        for sst in &ssts {\n+            let _ = self.importer.delete(sst);\n+        }\n+    }\n+\n+    fn handle_validate_sst(&self, ssts: Vec<SSTMeta>) {\n+        let store_id = self.store_id;\n+        // Get region info from PD, then check if the SST still belongs to the store.\n+        let f = &|sst: SSTMeta, res: Result<Option<Region>, _>| {\n+            match res {\n+                Ok(Some(region)) => {\n+                    if is_epoch_stale(region.get_region_epoch(), sst.get_region_epoch()) {\n+                        // Region has not been updated.\n+                        return Ok(None);\n+                    }\n+                    if region\n+                        .get_peers()\n+                        .into_iter()\n+                        .any(|p| p.get_store_id() == store_id)\n+                    {\n+                        // The SST still belongs to the store.\n+                        return Ok(None);\n+                    }\n+                    Ok(Some(sst))\n+                }\n+                Ok(None) => {\n+                    // TODO: handle merge\n+                    Ok(None)\n+                }\n+                Err(e) => {\n+                    error!(\"get region {} failed {:?}\", sst.get_region_id(), e);\n+                    Err(e)\n+                }\n+            }\n+        };\n+\n+        let mut results = Vec::new();\n+        for sst in ssts {\n+            let sst = self.pd_client\n+                .get_region_by_id(sst.get_region_id())","path":"src/raftstore/store/worker/cleanup_sst.rs","position":103,"original_position":103,"commit_id":"85517e044d8f1ac05ed432da56994f31730d7b7b","original_commit_id":"85517e044d8f1ac05ed432da56994f31730d7b7b","user":{"login":"huachaohuang","id":7806468,"avatar_url":"https://avatars0.githubusercontent.com/u/7806468?v=4","gravatar_id":"","url":"https://api.github.com/users/huachaohuang","html_url":"https://github.com/huachaohuang","followers_url":"https://api.github.com/users/huachaohuang/followers","following_url":"https://api.github.com/users/huachaohuang/following{/other_user}","gists_url":"https://api.github.com/users/huachaohuang/gists{/gist_id}","starred_url":"https://api.github.com/users/huachaohuang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/huachaohuang/subscriptions","organizations_url":"https://api.github.com/users/huachaohuang/orgs","repos_url":"https://api.github.com/users/huachaohuang/repos","events_url":"https://api.github.com/users/huachaohuang/events{/privacy}","received_events_url":"https://api.github.com/users/huachaohuang/received_events","type":"User","site_admin":false},"body":"Not yet.","created_at":"2018-03-21T02:09:38Z","updated_at":"2018-03-21T02:09:38Z","html_url":"https://github.com/pingcap/tikv/pull/2839#discussion_r175972091","pull_request_url":"https://api.github.com/repos/pingcap/tikv/pulls/2839","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/comments/175972091"},"html":{"href":"https://github.com/pingcap/tikv/pull/2839#discussion_r175972091"},"pull_request":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/2839"}},"in_reply_to_id":175968500},"pull_request":{"url":"https://api.github.com/repos/pingcap/tikv/pulls/2839","id":175799179,"html_url":"https://github.com/pingcap/tikv/pull/2839","diff_url":"https://github.com/pingcap/tikv/pull/2839.diff","patch_url":"https://github.com/pingcap/tikv/pull/2839.patch","issue_url":"https://api.github.com/repos/pingcap/tikv/issues/2839","number":2839,"state":"open","locked":false,"title":"Cleanup import SST files periodically","user":{"login":"huachaohuang","id":7806468,"avatar_url":"https://avatars0.githubusercontent.com/u/7806468?v=4","gravatar_id":"","url":"https://api.github.com/users/huachaohuang","html_url":"https://github.com/huachaohuang","followers_url":"https://api.github.com/users/huachaohuang/followers","following_url":"https://api.github.com/users/huachaohuang/following{/other_user}","gists_url":"https://api.github.com/users/huachaohuang/gists{/gist_id}","starred_url":"https://api.github.com/users/huachaohuang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/huachaohuang/subscriptions","organizations_url":"https://api.github.com/users/huachaohuang/orgs","repos_url":"https://api.github.com/users/huachaohuang/repos","events_url":"https://api.github.com/users/huachaohuang/events{/privacy}","received_events_url":"https://api.github.com/users/huachaohuang/received_events","type":"User","site_admin":false},"body":"The import SST process is divided into two steps:\r\n\r\n  1. Upload an SST file to all peers of a region\r\n  2. Ingest the SST file to the region with a Raft command\r\n\r\nHowever, since these two steps are not atomic, a lot of reasons can\r\nstop a client from ingesting an uploaded SST file to the region. In\r\nthat case, the uploaded SST file will remain in the import\r\ndirectory. So we need a way to clean up these obsoleted SST files.\r\n\r\nBefore we can delete an uploaded SST file, we need to make sure that\r\nthe SST file will not be ingested anymore.\r\nWe need to guarantee one of these:\r\n\r\n  1. The epoch of the SST is staler than the local region peer\r\n  2. The region information from PD shows that the SST does not belong\r\n  to the store, and there is no stale peer of that region in the store too.","created_at":"2018-03-19T03:49:56Z","updated_at":"2018-03-21T02:09:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0ea3307aff555d2f0afeccbf6b80424f6a0ebefd","assignee":null,"assignees":[],"requested_reviewers":[{"login":"overvenus","id":2150711,"avatar_url":"https://avatars0.githubusercontent.com/u/2150711?v=4","gravatar_id":"","url":"https://api.github.com/users/overvenus","html_url":"https://github.com/overvenus","followers_url":"https://api.github.com/users/overvenus/followers","following_url":"https://api.github.com/users/overvenus/following{/other_user}","gists_url":"https://api.github.com/users/overvenus/gists{/gist_id}","starred_url":"https://api.github.com/users/overvenus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/overvenus/subscriptions","organizations_url":"https://api.github.com/users/overvenus/orgs","repos_url":"https://api.github.com/users/overvenus/repos","events_url":"https://api.github.com/users/overvenus/events{/privacy}","received_events_url":"https://api.github.com/users/overvenus/received_events","type":"User","site_admin":false}],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/pingcap/tikv/pulls/2839/commits","review_comments_url":"https://api.github.com/repos/pingcap/tikv/pulls/2839/comments","review_comment_url":"https://api.github.com/repos/pingcap/tikv/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pingcap/tikv/issues/2839/comments","statuses_url":"https://api.github.com/repos/pingcap/tikv/statuses/85517e044d8f1ac05ed432da56994f31730d7b7b","head":{"label":"huachaohuang:cleanup-sst","ref":"cleanup-sst","sha":"85517e044d8f1ac05ed432da56994f31730d7b7b","user":{"login":"huachaohuang","id":7806468,"avatar_url":"https://avatars0.githubusercontent.com/u/7806468?v=4","gravatar_id":"","url":"https://api.github.com/users/huachaohuang","html_url":"https://github.com/huachaohuang","followers_url":"https://api.github.com/users/huachaohuang/followers","following_url":"https://api.github.com/users/huachaohuang/following{/other_user}","gists_url":"https://api.github.com/users/huachaohuang/gists{/gist_id}","starred_url":"https://api.github.com/users/huachaohuang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/huachaohuang/subscriptions","organizations_url":"https://api.github.com/users/huachaohuang/orgs","repos_url":"https://api.github.com/users/huachaohuang/repos","events_url":"https://api.github.com/users/huachaohuang/events{/privacy}","received_events_url":"https://api.github.com/users/huachaohuang/received_events","type":"User","site_admin":false},"repo":{"id":108204393,"name":"tikv","full_name":"huachaohuang/tikv","owner":{"login":"huachaohuang","id":7806468,"avatar_url":"https://avatars0.githubusercontent.com/u/7806468?v=4","gravatar_id":"","url":"https://api.github.com/users/huachaohuang","html_url":"https://github.com/huachaohuang","followers_url":"https://api.github.com/users/huachaohuang/followers","following_url":"https://api.github.com/users/huachaohuang/following{/other_user}","gists_url":"https://api.github.com/users/huachaohuang/gists{/gist_id}","starred_url":"https://api.github.com/users/huachaohuang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/huachaohuang/subscriptions","organizations_url":"https://api.github.com/users/huachaohuang/orgs","repos_url":"https://api.github.com/users/huachaohuang/repos","events_url":"https://api.github.com/users/huachaohuang/events{/privacy}","received_events_url":"https://api.github.com/users/huachaohuang/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/huachaohuang/tikv","description":"Distributed transactional key value database powered by Rust and Raft","fork":true,"url":"https://api.github.com/repos/huachaohuang/tikv","forks_url":"https://api.github.com/repos/huachaohuang/tikv/forks","keys_url":"https://api.github.com/repos/huachaohuang/tikv/keys{/key_id}","collaborators_url":"https://api.github.com/repos/huachaohuang/tikv/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/huachaohuang/tikv/teams","hooks_url":"https://api.github.com/repos/huachaohuang/tikv/hooks","issue_events_url":"https://api.github.com/repos/huachaohuang/tikv/issues/events{/number}","events_url":"https://api.github.com/repos/huachaohuang/tikv/events","assignees_url":"https://api.github.com/repos/huachaohuang/tikv/assignees{/user}","branches_url":"https://api.github.com/repos/huachaohuang/tikv/branches{/branch}","tags_url":"https://api.github.com/repos/huachaohuang/tikv/tags","blobs_url":"https://api.github.com/repos/huachaohuang/tikv/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/huachaohuang/tikv/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/huachaohuang/tikv/git/refs{/sha}","trees_url":"https://api.github.com/repos/huachaohuang/tikv/git/trees{/sha}","statuses_url":"https://api.github.com/repos/huachaohuang/tikv/statuses/{sha}","languages_url":"https://api.github.com/repos/huachaohuang/tikv/languages","stargazers_url":"https://api.github.com/repos/huachaohuang/tikv/stargazers","contributors_url":"https://api.github.com/repos/huachaohuang/tikv/contributors","subscribers_url":"https://api.github.com/repos/huachaohuang/tikv/subscribers","subscription_url":"https://api.github.com/repos/huachaohuang/tikv/subscription","commits_url":"https://api.github.com/repos/huachaohuang/tikv/commits{/sha}","git_commits_url":"https://api.github.com/repos/huachaohuang/tikv/git/commits{/sha}","comments_url":"https://api.github.com/repos/huachaohuang/tikv/comments{/number}","issue_comment_url":"https://api.github.com/repos/huachaohuang/tikv/issues/comments{/number}","contents_url":"https://api.github.com/repos/huachaohuang/tikv/contents/{+path}","compare_url":"https://api.github.com/repos/huachaohuang/tikv/compare/{base}...{head}","merges_url":"https://api.github.com/repos/huachaohuang/tikv/merges","archive_url":"https://api.github.com/repos/huachaohuang/tikv/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/huachaohuang/tikv/downloads","issues_url":"https://api.github.com/repos/huachaohuang/tikv/issues{/number}","pulls_url":"https://api.github.com/repos/huachaohuang/tikv/pulls{/number}","milestones_url":"https://api.github.com/repos/huachaohuang/tikv/milestones{/number}","notifications_url":"https://api.github.com/repos/huachaohuang/tikv/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/huachaohuang/tikv/labels{/name}","releases_url":"https://api.github.com/repos/huachaohuang/tikv/releases{/id}","deployments_url":"https://api.github.com/repos/huachaohuang/tikv/deployments","created_at":"2017-10-25T01:34:28Z","updated_at":"2018-03-16T15:51:36Z","pushed_at":"2018-03-19T13:54:00Z","git_url":"git://github.com/huachaohuang/tikv.git","ssh_url":"git@github.com:huachaohuang/tikv.git","clone_url":"https://github.com/huachaohuang/tikv.git","svn_url":"https://github.com/huachaohuang/tikv","homepage":"https://pingcap.com","size":13563,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"pingcap:master","ref":"master","sha":"8dcc5ab06930f0c5ad8dd078a5118c03412b352d","user":{"login":"pingcap","id":11855343,"avatar_url":"https://avatars3.githubusercontent.com/u/11855343?v=4","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://avatars3.githubusercontent.com/u/11855343?v=4","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":"2018-03-21T01:22:23Z","pushed_at":"2018-03-20T13:06:31Z","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":"https://pingcap.com","size":12380,"stargazers_count":2849,"watchers_count":2849,"language":"Rust","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":351,"mirror_url":null,"archived":false,"open_issues_count":167,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":351,"open_issues":167,"watchers":2849,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/2839"},"html":{"href":"https://github.com/pingcap/tikv/pull/2839"},"issue":{"href":"https://api.github.com/repos/pingcap/tikv/issues/2839"},"comments":{"href":"https://api.github.com/repos/pingcap/tikv/issues/2839/comments"},"review_comments":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/2839/comments"},"review_comment":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/2839/commits"},"statuses":{"href":"https://api.github.com/repos/pingcap/tikv/statuses/85517e044d8f1ac05ed432da56994f31730d7b7b"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"huachaohuang"}}