{"action":"created","comment":{"url":"https://api.github.com/repos/pingcap/tikv/pulls/comments/59680154","id":59680154,"diff_hunk":"@@ -0,0 +1,105 @@\n+use kvproto::raft_serverpb::RaftMessage;\n+use tikv::raftstore::Result;\n+use tikv::raftstore::store::Transport;\n+use rand;\n+use std::sync::{Arc, RwLock};\n+\n+use super::util::*;\n+use self::Strategy::*;\n+\n+#[derive(Clone)]\n+pub enum Strategy {\n+    LossPacket(u32),\n+    Latency(u64),\n+    OutOrder,\n+}\n+\n+trait Filter: Send + Sync {\n+    // in a SimulateTransport, if any filter's before return true, msg will be discard\n+    fn before(&self, msg: &RaftMessage) -> bool;\n+    // with after provided, one can change the return value arbitrarily\n+    fn after(&self, Result<()>) -> Result<()>;\n+}\n+\n+struct FilterLossPacket(u32);\n+struct FilterLatency(u64);\n+struct FilterOutOrder;\n+\n+impl Filter for FilterLossPacket {\n+    fn before(&self, _: &RaftMessage) -> bool {\n+        rand::random::<u32>() % 100u32 < self.0\n+    }\n+    fn after(&self, x: Result<()>) -> Result<()> {\n+        x\n+    }\n+}\n+\n+impl Filter for FilterLatency {\n+    fn before(&self, _: &RaftMessage) -> bool {\n+        sleep_ms(self.0);\n+        false\n+    }\n+    fn after(&self, x: Result<()>) -> Result<()> {\n+        x\n+    }\n+}\n+\n+impl Filter for FilterOutOrder {\n+    fn before(&self, _: &RaftMessage) -> bool {\n+        unimplemented!()\n+    }\n+    fn after(&self, _: Result<()>) -> Result<()> {\n+        unimplemented!()\n+    }\n+}\n+\n+pub struct SimulateTransport<T: Transport> {\n+    filters: Vec<Box<Filter>>,\n+    trans: Arc<RwLock<T>>,\n+}\n+\n+impl<T: Transport> SimulateTransport<T> {\n+    pub fn new(strategy: Vec<Strategy>, trans: Arc<RwLock<T>>) -> SimulateTransport<T> {\n+        let mut filters: Vec<Box<Filter>> = vec![];\n+        for s in strategy {\n+            match s {\n+                LossPacket(rate) => {\n+                    filters.push(Box::new(FilterLossPacket(rate)));\n+                }\n+                Latency(latency) => {\n+                    filters.push(Box::new(FilterLatency(latency)));\n+                }\n+                OutOrder => {\n+                    filters.push(Box::new(FilterOutOrder));\n+                }\n+            }\n+        }\n+\n+        SimulateTransport {\n+            filters: filters,\n+            trans: trans,\n+        }\n+    }\n+}\n+\n+impl<T: Transport> Transport for SimulateTransport<T> {\n+    fn send(&self, msg: RaftMessage) -> Result<()> {\n+        let mut discard = false;\n+        for strategy in &self.filters {\n+            if strategy.before(&msg) {\n+                discard = true;","path":"tests/raftstore/transport_simulate.rs","position":90,"original_position":90,"commit_id":"9f92130e1dccd75e68845b452844c84bf28205a7","original_commit_id":"9f92130e1dccd75e68845b452844c84bf28205a7","user":{"login":"siddontang","id":1080370,"avatar_url":"https://avatars.githubusercontent.com/u/1080370?v=3","gravatar_id":"","url":"https://api.github.com/users/siddontang","html_url":"https://github.com/siddontang","followers_url":"https://api.github.com/users/siddontang/followers","following_url":"https://api.github.com/users/siddontang/following{/other_user}","gists_url":"https://api.github.com/users/siddontang/gists{/gist_id}","starred_url":"https://api.github.com/users/siddontang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/siddontang/subscriptions","organizations_url":"https://api.github.com/users/siddontang/orgs","repos_url":"https://api.github.com/users/siddontang/repos","events_url":"https://api.github.com/users/siddontang/events{/privacy}","received_events_url":"https://api.github.com/users/siddontang/received_events","type":"User","site_admin":false},"body":"should we break the following filter here? \r\n@ngaut @BusyJay ","created_at":"2016-04-14T08:34:57Z","updated_at":"2016-04-14T08:34:57Z","html_url":"https://github.com/pingcap/tikv/pull/414#discussion_r59680154","pull_request_url":"https://api.github.com/repos/pingcap/tikv/pulls/414","_links":{"self":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/comments/59680154"},"html":{"href":"https://github.com/pingcap/tikv/pull/414#discussion_r59680154"},"pull_request":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/414"}}},"pull_request":{"url":"https://api.github.com/repos/pingcap/tikv/pulls/414","id":66327725,"html_url":"https://github.com/pingcap/tikv/pull/414","diff_url":"https://github.com/pingcap/tikv/pull/414.diff","patch_url":"https://github.com/pingcap/tikv/pull/414.patch","issue_url":"https://api.github.com/repos/pingcap/tikv/issues/414","number":414,"state":"open","locked":false,"title":"tests/raftstore: add SimulateTransport for testing bad network traffic","user":{"login":"tiancaiamao","id":1420062,"avatar_url":"https://avatars.githubusercontent.com/u/1420062?v=3","gravatar_id":"","url":"https://api.github.com/users/tiancaiamao","html_url":"https://github.com/tiancaiamao","followers_url":"https://api.github.com/users/tiancaiamao/followers","following_url":"https://api.github.com/users/tiancaiamao/following{/other_user}","gists_url":"https://api.github.com/users/tiancaiamao/gists{/gist_id}","starred_url":"https://api.github.com/users/tiancaiamao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tiancaiamao/subscriptions","organizations_url":"https://api.github.com/users/tiancaiamao/orgs","repos_url":"https://api.github.com/users/tiancaiamao/repos","events_url":"https://api.github.com/users/tiancaiamao/events{/privacy}","received_events_url":"https://api.github.com/users/tiancaiamao/received_events","type":"User","site_admin":false},"body":"SimulateTransport has a bundle of filters, while will do some addition work\r\nbefore call the the underlying Transport to simulate bad network traffic,\r\nsuch as Loss packet, Timeout, and so on.","created_at":"2016-04-13T14:10:54Z","updated_at":"2016-04-14T08:34:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fc31b64b8b841cb7456bf576ce147e2ceb6c6d41","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/pingcap/tikv/pulls/414/commits","review_comments_url":"https://api.github.com/repos/pingcap/tikv/pulls/414/comments","review_comment_url":"https://api.github.com/repos/pingcap/tikv/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pingcap/tikv/issues/414/comments","statuses_url":"https://api.github.com/repos/pingcap/tikv/statuses/9f92130e1dccd75e68845b452844c84bf28205a7","head":{"label":"pingcap:tiancaiamao/raftstore-bad-transport-1","ref":"tiancaiamao/raftstore-bad-transport-1","sha":"9f92130e1dccd75e68845b452844c84bf28205a7","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","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-13T22:24:53Z","pushed_at":"2016-04-14T06:52:07Z","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":1891,"stargazers_count":510,"watchers_count":510,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":28,"mirror_url":null,"open_issues_count":23,"forks":28,"open_issues":23,"watchers":510,"default_branch":"master"}},"base":{"label":"pingcap:master","ref":"master","sha":"ec4cbeadbd176bf9f1aec1361afd8b8b3dc409ef","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","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-13T22:24:53Z","pushed_at":"2016-04-14T06:52:07Z","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":1891,"stargazers_count":510,"watchers_count":510,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":28,"mirror_url":null,"open_issues_count":23,"forks":28,"open_issues":23,"watchers":510,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/414"},"html":{"href":"https://github.com/pingcap/tikv/pull/414"},"issue":{"href":"https://api.github.com/repos/pingcap/tikv/issues/414"},"comments":{"href":"https://api.github.com/repos/pingcap/tikv/issues/414/comments"},"review_comments":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/414/comments"},"review_comment":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pingcap/tikv/pulls/414/commits"},"statuses":{"href":"https://api.github.com/repos/pingcap/tikv/statuses/9f92130e1dccd75e68845b452844c84bf28205a7"}}}}