{"action":"created","comment":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/30599673","id":30599673,"diff_hunk":"@@ -0,0 +1,182 @@\n+// Copyright 2014 The Cockroach Authors.\n+//\n+// Licensed under the Apache License, Version 2.0 (the \"License\");\n+// you may not use this file except in compliance with the License.\n+// You may obtain a copy of the License at\n+//\n+//     http://www.apache.org/licenses/LICENSE-2.0\n+//\n+// Unless required by applicable law or agreed to in writing, software\n+// distributed under the License is distributed on an \"AS IS\" BASIS,\n+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n+// implied. See the License for the specific language governing\n+// permissions and limitations under the License. See the AUTHORS file\n+// for names of contributors.\n+//\n+// Author: yananzhi (zac.zhiyanan@gmail.com)\n+\n+package storage\n+\n+import (\n+\t\"sync\"\n+\n+\t\"github.com/cockroachdb/cockroach/proto\"\n+\t\"github.com/cockroachdb/cockroach/util/cache\"\n+\n+\tgogoproto \"github.com/gogo/protobuf/proto\"\n+)\n+\n+const (\n+\t// The default size of the txn cache.\n+\tdefaultTxnCacheSize = 1 << 20\n+)\n+\n+// TxnCache maintains txn which was pushed recently to avoid\n+// repushing further intents after a txn has already been aborted,\n+// comiitted or its timestamp moved forward.\n+type TxnCache struct {\n+\t// Use a LRU un-ordered cache, key is txn's ID, value is txn\n+\ttxnCache *cache.UnorderedCache\n+\n+\t// cacheMu protects txnCache for concurrent access\n+\tcacheMu sync.Mutex\n+}\n+\n+// NewTxnCache returns a new txn cache\n+func NewTxnCache(maxSize int) *TxnCache {\n+\n+\treturn &TxnCache{\n+\t\ttxnCache: cache.NewUnorderedCache(cache.Config{\n+\t\t\tPolicy: cache.CacheLRU,\n+\t\t\tShouldEvict: func(n int, k, v interface{}) bool {\n+\t\t\t\treturn n > maxSize\n+\t\t\t},\n+\t\t}),\n+\t}\n+}\n+\n+// processPushedTxn return true if the cache txn can process a push txn request, and vice versa\n+func (tc *TxnCache) processPushedTxn(args proto.Request, reply proto.Response) bool {\n+\n+\tpushTxnReq, ok := args.(*proto.InternalPushTxnRequest)\n+\tif !ok {\n+\t\treturn false\n+\t}\n+\n+\ttc.cacheMu.Lock()\n+\tdefer tc.cacheMu.Unlock()\n+\n+\tvalue, ok := tc.txnCache.Get(string(pushTxnReq.PusheeTxn.ID))\n+\n+\tif !ok {\n+\t\treturn false\n+\t}\n+\n+\t// Compare cached txn and pushee txn\n+\tcachedTxn := value.(*proto.Transaction)\n+\tif sufficientPushTxnRequest(cachedTxn, &pushTxnReq.PusheeTxn, pushTxnReq.PushType) {\n+\t\tpushTxnReply := reply.(*proto.InternalPushTxnResponse)\n+\n+\t\tpushTxnReply.PusheeTxn = gogoproto.Clone(cachedTxn).(*proto.Transaction)\n+\n+\t\t// Upgrade the epoch and priority as necessary.\n+\t\tif pushTxnReply.PusheeTxn.Epoch < pushTxnReq.PusheeTxn.Epoch {\n+\t\t\tpushTxnReply.PusheeTxn.Epoch = pushTxnReq.PusheeTxn.Epoch\n+\t\t\t// Also upgrade the cached txn's epoch\n+\t\t\tcachedTxn.Epoch = pushTxnReply.PusheeTxn.Epoch\n+\t\t}\n+\t\tif pushTxnReply.PusheeTxn.Priority < pushTxnReq.PusheeTxn.Priority {\n+\t\t\tpushTxnReply.PusheeTxn.Priority = pushTxnReq.PusheeTxn.Priority\n+\t\t\t// Also upgrade the cached txn's priority\n+\t\t\tcachedTxn.Priority = pushTxnReq.PusheeTxn.Priority\n+\t\t}\n+\n+\t\treturn true\n+\t}\n+\n+\treturn false\n+}\n+\n+// update check and add a succeed pushed txn to the cache.\n+func (tc *TxnCache) update(reply proto.Response) {\n+\n+\tpushTxnReply, ok := reply.(*proto.InternalPushTxnResponse)\n+\tif !ok || reply.Header().GoError() != nil {\n+\t\treturn\n+\t}\n+\n+\tpusheeTxn := pushTxnReply.PusheeTxn\n+\ttc.cacheMu.Lock()\n+\tdefer tc.cacheMu.Unlock()\n+\n+\tvalue, ok := tc.txnCache.Get(string(pusheeTxn.ID))\n+\tif !ok {\n+\t\ttc.txnCache.Add(string(pusheeTxn.ID), gogoproto.Clone(pusheeTxn).(*proto.Transaction))\n+\t} else {","path":"storage/txn_cache.go","position":115,"original_position":115,"commit_id":"f2854d983e90cab16efec016c50a6dc1e3872e7b","original_commit_id":"f2854d983e90cab16efec016c50a6dc1e3872e7b","user":{"login":"yananzhi","id":11538969,"avatar_url":"https://avatars.githubusercontent.com/u/11538969?v=3","gravatar_id":"","url":"https://api.github.com/users/yananzhi","html_url":"https://github.com/yananzhi","followers_url":"https://api.github.com/users/yananzhi/followers","following_url":"https://api.github.com/users/yananzhi/following{/other_user}","gists_url":"https://api.github.com/users/yananzhi/gists{/gist_id}","starred_url":"https://api.github.com/users/yananzhi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yananzhi/subscriptions","organizations_url":"https://api.github.com/users/yananzhi/orgs","repos_url":"https://api.github.com/users/yananzhi/repos","events_url":"https://api.github.com/users/yananzhi/events{/privacy}","received_events_url":"https://api.github.com/users/yananzhi/received_events","type":"User","site_admin":false},"body":"@bdarnell The cached txn may have a higner epoch and priority than pushee txn.","created_at":"2015-05-19T13:45:19Z","updated_at":"2015-05-19T13:45:19Z","html_url":"https://github.com/cockroachdb/cockroach/pull/1041#discussion_r30599673","pull_request_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/1041","_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/30599673"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/1041#discussion_r30599673"},"pull_request":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/1041"}}},"pull_request":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/1041","id":35515194,"html_url":"https://github.com/cockroachdb/cockroach/pull/1041","diff_url":"https://github.com/cockroachdb/cockroach/pull/1041.diff","patch_url":"https://github.com/cockroachdb/cockroach/pull/1041.patch","issue_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/1041","number":1041,"state":"open","locked":false,"title":"Keep a cache of pushed transactions on the store","user":{"login":"yananzhi","id":11538969,"avatar_url":"https://avatars.githubusercontent.com/u/11538969?v=3","gravatar_id":"","url":"https://api.github.com/users/yananzhi","html_url":"https://github.com/yananzhi","followers_url":"https://api.github.com/users/yananzhi/followers","following_url":"https://api.github.com/users/yananzhi/following{/other_user}","gists_url":"https://api.github.com/users/yananzhi/gists{/gist_id}","starred_url":"https://api.github.com/users/yananzhi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yananzhi/subscriptions","organizations_url":"https://api.github.com/users/yananzhi/orgs","repos_url":"https://api.github.com/users/yananzhi/repos","events_url":"https://api.github.com/users/yananzhi/events{/privacy}","received_events_url":"https://api.github.com/users/yananzhi/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-05-15T13:58:38Z","updated_at":"2015-05-19T13:45:19Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1f0ed17a57c71fcc1e7258ba7dd05a500dfcf8c9","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/1041/commits","review_comments_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/1041/comments","review_comment_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/1041/comments","statuses_url":"https://api.github.com/repos/cockroachdb/cockroach/statuses/f2854d983e90cab16efec016c50a6dc1e3872e7b","head":{"label":"yananzhi:push_txn_cache","ref":"push_txn_cache","sha":"f2854d983e90cab16efec016c50a6dc1e3872e7b","user":{"login":"yananzhi","id":11538969,"avatar_url":"https://avatars.githubusercontent.com/u/11538969?v=3","gravatar_id":"","url":"https://api.github.com/users/yananzhi","html_url":"https://github.com/yananzhi","followers_url":"https://api.github.com/users/yananzhi/followers","following_url":"https://api.github.com/users/yananzhi/following{/other_user}","gists_url":"https://api.github.com/users/yananzhi/gists{/gist_id}","starred_url":"https://api.github.com/users/yananzhi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yananzhi/subscriptions","organizations_url":"https://api.github.com/users/yananzhi/orgs","repos_url":"https://api.github.com/users/yananzhi/repos","events_url":"https://api.github.com/users/yananzhi/events{/privacy}","received_events_url":"https://api.github.com/users/yananzhi/received_events","type":"User","site_admin":false},"repo":{"id":34190198,"name":"cockroach","full_name":"yananzhi/cockroach","owner":{"login":"yananzhi","id":11538969,"avatar_url":"https://avatars.githubusercontent.com/u/11538969?v=3","gravatar_id":"","url":"https://api.github.com/users/yananzhi","html_url":"https://github.com/yananzhi","followers_url":"https://api.github.com/users/yananzhi/followers","following_url":"https://api.github.com/users/yananzhi/following{/other_user}","gists_url":"https://api.github.com/users/yananzhi/gists{/gist_id}","starred_url":"https://api.github.com/users/yananzhi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yananzhi/subscriptions","organizations_url":"https://api.github.com/users/yananzhi/orgs","repos_url":"https://api.github.com/users/yananzhi/repos","events_url":"https://api.github.com/users/yananzhi/events{/privacy}","received_events_url":"https://api.github.com/users/yananzhi/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/yananzhi/cockroach","description":"A Scalable, Geo-Replicated, Transactional Datastore","fork":true,"url":"https://api.github.com/repos/yananzhi/cockroach","forks_url":"https://api.github.com/repos/yananzhi/cockroach/forks","keys_url":"https://api.github.com/repos/yananzhi/cockroach/keys{/key_id}","collaborators_url":"https://api.github.com/repos/yananzhi/cockroach/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/yananzhi/cockroach/teams","hooks_url":"https://api.github.com/repos/yananzhi/cockroach/hooks","issue_events_url":"https://api.github.com/repos/yananzhi/cockroach/issues/events{/number}","events_url":"https://api.github.com/repos/yananzhi/cockroach/events","assignees_url":"https://api.github.com/repos/yananzhi/cockroach/assignees{/user}","branches_url":"https://api.github.com/repos/yananzhi/cockroach/branches{/branch}","tags_url":"https://api.github.com/repos/yananzhi/cockroach/tags","blobs_url":"https://api.github.com/repos/yananzhi/cockroach/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/yananzhi/cockroach/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/yananzhi/cockroach/git/refs{/sha}","trees_url":"https://api.github.com/repos/yananzhi/cockroach/git/trees{/sha}","statuses_url":"https://api.github.com/repos/yananzhi/cockroach/statuses/{sha}","languages_url":"https://api.github.com/repos/yananzhi/cockroach/languages","stargazers_url":"https://api.github.com/repos/yananzhi/cockroach/stargazers","contributors_url":"https://api.github.com/repos/yananzhi/cockroach/contributors","subscribers_url":"https://api.github.com/repos/yananzhi/cockroach/subscribers","subscription_url":"https://api.github.com/repos/yananzhi/cockroach/subscription","commits_url":"https://api.github.com/repos/yananzhi/cockroach/commits{/sha}","git_commits_url":"https://api.github.com/repos/yananzhi/cockroach/git/commits{/sha}","comments_url":"https://api.github.com/repos/yananzhi/cockroach/comments{/number}","issue_comment_url":"https://api.github.com/repos/yananzhi/cockroach/issues/comments{/number}","contents_url":"https://api.github.com/repos/yananzhi/cockroach/contents/{+path}","compare_url":"https://api.github.com/repos/yananzhi/cockroach/compare/{base}...{head}","merges_url":"https://api.github.com/repos/yananzhi/cockroach/merges","archive_url":"https://api.github.com/repos/yananzhi/cockroach/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/yananzhi/cockroach/downloads","issues_url":"https://api.github.com/repos/yananzhi/cockroach/issues{/number}","pulls_url":"https://api.github.com/repos/yananzhi/cockroach/pulls{/number}","milestones_url":"https://api.github.com/repos/yananzhi/cockroach/milestones{/number}","notifications_url":"https://api.github.com/repos/yananzhi/cockroach/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/yananzhi/cockroach/labels{/name}","releases_url":"https://api.github.com/repos/yananzhi/cockroach/releases{/id}","created_at":"2015-04-19T02:08:47Z","updated_at":"2015-05-01T02:36:15Z","pushed_at":"2015-05-18T15:58:56Z","git_url":"git://github.com/yananzhi/cockroach.git","ssh_url":"git@github.com:yananzhi/cockroach.git","clone_url":"https://github.com/yananzhi/cockroach.git","svn_url":"https://github.com/yananzhi/cockroach","homepage":null,"size":16419,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"cockroachdb:master","ref":"master","sha":"12f264d53f716feef14942e4fbadb4a5b0e1ca77","user":{"login":"cockroachdb","id":6748139,"avatar_url":"https://avatars.githubusercontent.com/u/6748139?v=3","gravatar_id":"","url":"https://api.github.com/users/cockroachdb","html_url":"https://github.com/cockroachdb","followers_url":"https://api.github.com/users/cockroachdb/followers","following_url":"https://api.github.com/users/cockroachdb/following{/other_user}","gists_url":"https://api.github.com/users/cockroachdb/gists{/gist_id}","starred_url":"https://api.github.com/users/cockroachdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cockroachdb/subscriptions","organizations_url":"https://api.github.com/users/cockroachdb/orgs","repos_url":"https://api.github.com/users/cockroachdb/repos","events_url":"https://api.github.com/users/cockroachdb/events{/privacy}","received_events_url":"https://api.github.com/users/cockroachdb/received_events","type":"Organization","site_admin":false},"repo":{"id":16563587,"name":"cockroach","full_name":"cockroachdb/cockroach","owner":{"login":"cockroachdb","id":6748139,"avatar_url":"https://avatars.githubusercontent.com/u/6748139?v=3","gravatar_id":"","url":"https://api.github.com/users/cockroachdb","html_url":"https://github.com/cockroachdb","followers_url":"https://api.github.com/users/cockroachdb/followers","following_url":"https://api.github.com/users/cockroachdb/following{/other_user}","gists_url":"https://api.github.com/users/cockroachdb/gists{/gist_id}","starred_url":"https://api.github.com/users/cockroachdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cockroachdb/subscriptions","organizations_url":"https://api.github.com/users/cockroachdb/orgs","repos_url":"https://api.github.com/users/cockroachdb/repos","events_url":"https://api.github.com/users/cockroachdb/events{/privacy}","received_events_url":"https://api.github.com/users/cockroachdb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cockroachdb/cockroach","description":"A Scalable, Geo-Replicated, Transactional Datastore","fork":false,"url":"https://api.github.com/repos/cockroachdb/cockroach","forks_url":"https://api.github.com/repos/cockroachdb/cockroach/forks","keys_url":"https://api.github.com/repos/cockroachdb/cockroach/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cockroachdb/cockroach/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cockroachdb/cockroach/teams","hooks_url":"https://api.github.com/repos/cockroachdb/cockroach/hooks","issue_events_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/events{/number}","events_url":"https://api.github.com/repos/cockroachdb/cockroach/events","assignees_url":"https://api.github.com/repos/cockroachdb/cockroach/assignees{/user}","branches_url":"https://api.github.com/repos/cockroachdb/cockroach/branches{/branch}","tags_url":"https://api.github.com/repos/cockroachdb/cockroach/tags","blobs_url":"https://api.github.com/repos/cockroachdb/cockroach/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cockroachdb/cockroach/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cockroachdb/cockroach/git/refs{/sha}","trees_url":"https://api.github.com/repos/cockroachdb/cockroach/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cockroachdb/cockroach/statuses/{sha}","languages_url":"https://api.github.com/repos/cockroachdb/cockroach/languages","stargazers_url":"https://api.github.com/repos/cockroachdb/cockroach/stargazers","contributors_url":"https://api.github.com/repos/cockroachdb/cockroach/contributors","subscribers_url":"https://api.github.com/repos/cockroachdb/cockroach/subscribers","subscription_url":"https://api.github.com/repos/cockroachdb/cockroach/subscription","commits_url":"https://api.github.com/repos/cockroachdb/cockroach/commits{/sha}","git_commits_url":"https://api.github.com/repos/cockroachdb/cockroach/git/commits{/sha}","comments_url":"https://api.github.com/repos/cockroachdb/cockroach/comments{/number}","issue_comment_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/comments{/number}","contents_url":"https://api.github.com/repos/cockroachdb/cockroach/contents/{+path}","compare_url":"https://api.github.com/repos/cockroachdb/cockroach/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cockroachdb/cockroach/merges","archive_url":"https://api.github.com/repos/cockroachdb/cockroach/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cockroachdb/cockroach/downloads","issues_url":"https://api.github.com/repos/cockroachdb/cockroach/issues{/number}","pulls_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls{/number}","milestones_url":"https://api.github.com/repos/cockroachdb/cockroach/milestones{/number}","notifications_url":"https://api.github.com/repos/cockroachdb/cockroach/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cockroachdb/cockroach/labels{/name}","releases_url":"https://api.github.com/repos/cockroachdb/cockroach/releases{/id}","created_at":"2014-02-06T00:18:47Z","updated_at":"2015-05-19T06:33:52Z","pushed_at":"2015-05-19T13:39:14Z","git_url":"git://github.com/cockroachdb/cockroach.git","ssh_url":"git@github.com:cockroachdb/cockroach.git","clone_url":"https://github.com/cockroachdb/cockroach.git","svn_url":"https://github.com/cockroachdb/cockroach","homepage":null,"size":36653,"stargazers_count":3444,"watchers_count":3444,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":276,"mirror_url":null,"open_issues_count":74,"forks":276,"open_issues":74,"watchers":3444,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/1041"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/1041"},"issue":{"href":"https://api.github.com/repos/cockroachdb/cockroach/issues/1041"},"comments":{"href":"https://api.github.com/repos/cockroachdb/cockroach/issues/1041/comments"},"review_comments":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/1041/comments"},"review_comment":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/1041/commits"},"statuses":{"href":"https://api.github.com/repos/cockroachdb/cockroach/statuses/f2854d983e90cab16efec016c50a6dc1e3872e7b"}}}}