{"action":"created","comment":{"url":"https://api.github.com/repos/coreos/etcd/pulls/comments/56195757","id":56195757,"diff_hunk":"@@ -0,0 +1,241 @@\n+// Copyright 2016 CoreOS, Inc.\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 implied.\n+// See the License for the specific language governing permissions and\n+// limitations under the License.\n+\n+package concurrency\n+\n+import (\n+\t\"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context\"\n+\tv3 \"github.com/coreos/etcd/clientv3\"\n+)\n+\n+// STM is an interface for software transactional memory.\n+type STM interface {\n+\t// Get returns the value for a given key, inserting the key into the txn's rset.\n+\t// If Get fails, it aborts the transaction with an error, never returning.\n+\tGet(key string) string\n+\t// Put adds a value for a key to the write set.\n+\tPut(key, val string, opts ...v3.OpOption)\n+\t// Rev returns the revision of a key in the read set.\n+\tRev(key string) int64\n+\t// Del deletes a key.\n+\tDel(key string)\n+\n+\t// commit attempts to apply the txn's changes to the server.\n+\tcommit() *v3.TxnResponse\n+\treset()\n+}\n+\n+// stm implements repeatable-read software transactional memory over etcd\n+type stm struct {\n+\tclient *v3.Client\n+\tctx    context.Context\n+\t// rset holds read key values and revisions\n+\trset map[string]*v3.GetResponse\n+\t// wset holds overwritten keys and their values\n+\twset map[string]stmPut\n+\t// getOpts are the opts used for gets\n+\tgetOpts []v3.OpOption\n+}\n+\n+type stmPut struct {\n+\tval string\n+\top  v3.Op\n+}\n+\n+// stmError safely passes STM errors through panic to the STM error channel.\n+type stmError struct{ err error }\n+\n+// NewSTMRepeatable initiates new repeatable-read transaction.\n+func NewSTMRepeatable(ctx context.Context, c *v3.Client, apply func(STM) error) (*v3.TxnResponse, error) {\n+\ts := &stm{client: c, ctx: ctx, getOpts: []v3.OpOption{v3.WithSerializable()}}\n+\treturn runSTM(s, apply)\n+}\n+\n+// NewSTMSerializable initiates a new serialized transaction.\n+func NewSTMSerializable(ctx context.Context, c *v3.Client, apply func(STM) error) (*v3.TxnResponse, error) {\n+\ts := &stmSerializable{\n+\t\tstm:      stm{client: c, ctx: ctx},\n+\t\tprefetch: make(map[string]*v3.GetResponse),\n+\t}\n+\treturn runSTM(s, apply)\n+}\n+\n+type stmResponse struct {\n+\tresp *v3.TxnResponse\n+\terr  error\n+}\n+\n+func runSTM(s STM, apply func(STM) error) (*v3.TxnResponse, error) {\n+\toutc := make(chan stmResponse, 1)\n+\tgo func() {\n+\t\tdefer func() {\n+\t\t\tif r := recover(); r != nil {\n+\t\t\t\te, ok := r.(stmError)\n+\t\t\t\tif !ok {\n+\t\t\t\t\tpanic(r)","path":"clientv3/concurrency/stm.go","position":86,"original_position":86,"commit_id":"0f318e5151a8c616d8000c0d235d52aa821c5009","original_commit_id":"0f318e5151a8c616d8000c0d235d52aa821c5009","user":{"login":"xiang90","id":4479947,"avatar_url":"https://avatars.githubusercontent.com/u/4479947?v=3","gravatar_id":"","url":"https://api.github.com/users/xiang90","html_url":"https://github.com/xiang90","followers_url":"https://api.github.com/users/xiang90/followers","following_url":"https://api.github.com/users/xiang90/following{/other_user}","gists_url":"https://api.github.com/users/xiang90/gists{/gist_id}","starred_url":"https://api.github.com/users/xiang90/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xiang90/subscriptions","organizations_url":"https://api.github.com/users/xiang90/orgs","repos_url":"https://api.github.com/users/xiang90/repos","events_url":"https://api.github.com/users/xiang90/events{/privacy}","received_events_url":"https://api.github.com/users/xiang90/received_events","type":"User","site_admin":false},"body":"you mean client code error? (the passed in apply)","created_at":"2016-03-15T16:37:33Z","updated_at":"2016-03-15T16:37:33Z","html_url":"https://github.com/coreos/etcd/pull/4760#discussion_r56195757","pull_request_url":"https://api.github.com/repos/coreos/etcd/pulls/4760","_links":{"self":{"href":"https://api.github.com/repos/coreos/etcd/pulls/comments/56195757"},"html":{"href":"https://github.com/coreos/etcd/pull/4760#discussion_r56195757"},"pull_request":{"href":"https://api.github.com/repos/coreos/etcd/pulls/4760"}}},"pull_request":{"url":"https://api.github.com/repos/coreos/etcd/pulls/4760","id":62794658,"html_url":"https://github.com/coreos/etcd/pull/4760","diff_url":"https://github.com/coreos/etcd/pull/4760.diff","patch_url":"https://github.com/coreos/etcd/pull/4760.patch","issue_url":"https://api.github.com/repos/coreos/etcd/issues/4760","number":4760,"state":"open","locked":false,"title":"clientv3/concurrency: STM","user":{"login":"heyitsanthony","id":16294350,"avatar_url":"https://avatars.githubusercontent.com/u/16294350?v=3","gravatar_id":"","url":"https://api.github.com/users/heyitsanthony","html_url":"https://github.com/heyitsanthony","followers_url":"https://api.github.com/users/heyitsanthony/followers","following_url":"https://api.github.com/users/heyitsanthony/following{/other_user}","gists_url":"https://api.github.com/users/heyitsanthony/gists{/gist_id}","starred_url":"https://api.github.com/users/heyitsanthony/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/heyitsanthony/subscriptions","organizations_url":"https://api.github.com/users/heyitsanthony/orgs","repos_url":"https://api.github.com/users/heyitsanthony/repos","events_url":"https://api.github.com/users/heyitsanthony/events{/privacy}","received_events_url":"https://api.github.com/users/heyitsanthony/received_events","type":"User","site_admin":false},"body":"Repeatable read and serialized read STM implementations.","created_at":"2016-03-14T18:25:01Z","updated_at":"2016-03-15T16:37:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6770ad500564d6810d1b51d4838f655d027f5257","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/coreos/etcd/pulls/4760/commits","review_comments_url":"https://api.github.com/repos/coreos/etcd/pulls/4760/comments","review_comment_url":"https://api.github.com/repos/coreos/etcd/pulls/comments{/number}","comments_url":"https://api.github.com/repos/coreos/etcd/issues/4760/comments","statuses_url":"https://api.github.com/repos/coreos/etcd/statuses/0f318e5151a8c616d8000c0d235d52aa821c5009","head":{"label":"heyitsanthony:clientv3-stm","ref":"clientv3-stm","sha":"0f318e5151a8c616d8000c0d235d52aa821c5009","user":{"login":"heyitsanthony","id":16294350,"avatar_url":"https://avatars.githubusercontent.com/u/16294350?v=3","gravatar_id":"","url":"https://api.github.com/users/heyitsanthony","html_url":"https://github.com/heyitsanthony","followers_url":"https://api.github.com/users/heyitsanthony/followers","following_url":"https://api.github.com/users/heyitsanthony/following{/other_user}","gists_url":"https://api.github.com/users/heyitsanthony/gists{/gist_id}","starred_url":"https://api.github.com/users/heyitsanthony/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/heyitsanthony/subscriptions","organizations_url":"https://api.github.com/users/heyitsanthony/orgs","repos_url":"https://api.github.com/users/heyitsanthony/repos","events_url":"https://api.github.com/users/heyitsanthony/events{/privacy}","received_events_url":"https://api.github.com/users/heyitsanthony/received_events","type":"User","site_admin":false},"repo":{"id":47994854,"name":"etcd","full_name":"heyitsanthony/etcd","owner":{"login":"heyitsanthony","id":16294350,"avatar_url":"https://avatars.githubusercontent.com/u/16294350?v=3","gravatar_id":"","url":"https://api.github.com/users/heyitsanthony","html_url":"https://github.com/heyitsanthony","followers_url":"https://api.github.com/users/heyitsanthony/followers","following_url":"https://api.github.com/users/heyitsanthony/following{/other_user}","gists_url":"https://api.github.com/users/heyitsanthony/gists{/gist_id}","starred_url":"https://api.github.com/users/heyitsanthony/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/heyitsanthony/subscriptions","organizations_url":"https://api.github.com/users/heyitsanthony/orgs","repos_url":"https://api.github.com/users/heyitsanthony/repos","events_url":"https://api.github.com/users/heyitsanthony/events{/privacy}","received_events_url":"https://api.github.com/users/heyitsanthony/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/heyitsanthony/etcd","description":"Distributed reliable key-value store for the most critical data of a distributed system","fork":true,"url":"https://api.github.com/repos/heyitsanthony/etcd","forks_url":"https://api.github.com/repos/heyitsanthony/etcd/forks","keys_url":"https://api.github.com/repos/heyitsanthony/etcd/keys{/key_id}","collaborators_url":"https://api.github.com/repos/heyitsanthony/etcd/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/heyitsanthony/etcd/teams","hooks_url":"https://api.github.com/repos/heyitsanthony/etcd/hooks","issue_events_url":"https://api.github.com/repos/heyitsanthony/etcd/issues/events{/number}","events_url":"https://api.github.com/repos/heyitsanthony/etcd/events","assignees_url":"https://api.github.com/repos/heyitsanthony/etcd/assignees{/user}","branches_url":"https://api.github.com/repos/heyitsanthony/etcd/branches{/branch}","tags_url":"https://api.github.com/repos/heyitsanthony/etcd/tags","blobs_url":"https://api.github.com/repos/heyitsanthony/etcd/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/heyitsanthony/etcd/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/heyitsanthony/etcd/git/refs{/sha}","trees_url":"https://api.github.com/repos/heyitsanthony/etcd/git/trees{/sha}","statuses_url":"https://api.github.com/repos/heyitsanthony/etcd/statuses/{sha}","languages_url":"https://api.github.com/repos/heyitsanthony/etcd/languages","stargazers_url":"https://api.github.com/repos/heyitsanthony/etcd/stargazers","contributors_url":"https://api.github.com/repos/heyitsanthony/etcd/contributors","subscribers_url":"https://api.github.com/repos/heyitsanthony/etcd/subscribers","subscription_url":"https://api.github.com/repos/heyitsanthony/etcd/subscription","commits_url":"https://api.github.com/repos/heyitsanthony/etcd/commits{/sha}","git_commits_url":"https://api.github.com/repos/heyitsanthony/etcd/git/commits{/sha}","comments_url":"https://api.github.com/repos/heyitsanthony/etcd/comments{/number}","issue_comment_url":"https://api.github.com/repos/heyitsanthony/etcd/issues/comments{/number}","contents_url":"https://api.github.com/repos/heyitsanthony/etcd/contents/{+path}","compare_url":"https://api.github.com/repos/heyitsanthony/etcd/compare/{base}...{head}","merges_url":"https://api.github.com/repos/heyitsanthony/etcd/merges","archive_url":"https://api.github.com/repos/heyitsanthony/etcd/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/heyitsanthony/etcd/downloads","issues_url":"https://api.github.com/repos/heyitsanthony/etcd/issues{/number}","pulls_url":"https://api.github.com/repos/heyitsanthony/etcd/pulls{/number}","milestones_url":"https://api.github.com/repos/heyitsanthony/etcd/milestones{/number}","notifications_url":"https://api.github.com/repos/heyitsanthony/etcd/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/heyitsanthony/etcd/labels{/name}","releases_url":"https://api.github.com/repos/heyitsanthony/etcd/releases{/id}","deployments_url":"https://api.github.com/repos/heyitsanthony/etcd/deployments","created_at":"2015-12-14T18:57:05Z","updated_at":"2016-01-11T20:59:07Z","pushed_at":"2016-03-15T00:24:07Z","git_url":"git://github.com/heyitsanthony/etcd.git","ssh_url":"git@github.com:heyitsanthony/etcd.git","clone_url":"https://github.com/heyitsanthony/etcd.git","svn_url":"https://github.com/heyitsanthony/etcd","homepage":"https://coreos.com/etcd/docs/latest/","size":21606,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":false,"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":"coreos:master","ref":"master","sha":"58c92ee41bab97d92a330e47eac7dac036a3523e","user":{"login":"coreos","id":3730757,"avatar_url":"https://avatars.githubusercontent.com/u/3730757?v=3","gravatar_id":"","url":"https://api.github.com/users/coreos","html_url":"https://github.com/coreos","followers_url":"https://api.github.com/users/coreos/followers","following_url":"https://api.github.com/users/coreos/following{/other_user}","gists_url":"https://api.github.com/users/coreos/gists{/gist_id}","starred_url":"https://api.github.com/users/coreos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coreos/subscriptions","organizations_url":"https://api.github.com/users/coreos/orgs","repos_url":"https://api.github.com/users/coreos/repos","events_url":"https://api.github.com/users/coreos/events{/privacy}","received_events_url":"https://api.github.com/users/coreos/received_events","type":"Organization","site_admin":false},"repo":{"id":11225014,"name":"etcd","full_name":"coreos/etcd","owner":{"login":"coreos","id":3730757,"avatar_url":"https://avatars.githubusercontent.com/u/3730757?v=3","gravatar_id":"","url":"https://api.github.com/users/coreos","html_url":"https://github.com/coreos","followers_url":"https://api.github.com/users/coreos/followers","following_url":"https://api.github.com/users/coreos/following{/other_user}","gists_url":"https://api.github.com/users/coreos/gists{/gist_id}","starred_url":"https://api.github.com/users/coreos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coreos/subscriptions","organizations_url":"https://api.github.com/users/coreos/orgs","repos_url":"https://api.github.com/users/coreos/repos","events_url":"https://api.github.com/users/coreos/events{/privacy}","received_events_url":"https://api.github.com/users/coreos/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/coreos/etcd","description":"Distributed reliable key-value store for the most critical data of a distributed system","fork":false,"url":"https://api.github.com/repos/coreos/etcd","forks_url":"https://api.github.com/repos/coreos/etcd/forks","keys_url":"https://api.github.com/repos/coreos/etcd/keys{/key_id}","collaborators_url":"https://api.github.com/repos/coreos/etcd/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/coreos/etcd/teams","hooks_url":"https://api.github.com/repos/coreos/etcd/hooks","issue_events_url":"https://api.github.com/repos/coreos/etcd/issues/events{/number}","events_url":"https://api.github.com/repos/coreos/etcd/events","assignees_url":"https://api.github.com/repos/coreos/etcd/assignees{/user}","branches_url":"https://api.github.com/repos/coreos/etcd/branches{/branch}","tags_url":"https://api.github.com/repos/coreos/etcd/tags","blobs_url":"https://api.github.com/repos/coreos/etcd/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/coreos/etcd/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/coreos/etcd/git/refs{/sha}","trees_url":"https://api.github.com/repos/coreos/etcd/git/trees{/sha}","statuses_url":"https://api.github.com/repos/coreos/etcd/statuses/{sha}","languages_url":"https://api.github.com/repos/coreos/etcd/languages","stargazers_url":"https://api.github.com/repos/coreos/etcd/stargazers","contributors_url":"https://api.github.com/repos/coreos/etcd/contributors","subscribers_url":"https://api.github.com/repos/coreos/etcd/subscribers","subscription_url":"https://api.github.com/repos/coreos/etcd/subscription","commits_url":"https://api.github.com/repos/coreos/etcd/commits{/sha}","git_commits_url":"https://api.github.com/repos/coreos/etcd/git/commits{/sha}","comments_url":"https://api.github.com/repos/coreos/etcd/comments{/number}","issue_comment_url":"https://api.github.com/repos/coreos/etcd/issues/comments{/number}","contents_url":"https://api.github.com/repos/coreos/etcd/contents/{+path}","compare_url":"https://api.github.com/repos/coreos/etcd/compare/{base}...{head}","merges_url":"https://api.github.com/repos/coreos/etcd/merges","archive_url":"https://api.github.com/repos/coreos/etcd/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/coreos/etcd/downloads","issues_url":"https://api.github.com/repos/coreos/etcd/issues{/number}","pulls_url":"https://api.github.com/repos/coreos/etcd/pulls{/number}","milestones_url":"https://api.github.com/repos/coreos/etcd/milestones{/number}","notifications_url":"https://api.github.com/repos/coreos/etcd/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/coreos/etcd/labels{/name}","releases_url":"https://api.github.com/repos/coreos/etcd/releases{/id}","deployments_url":"https://api.github.com/repos/coreos/etcd/deployments","created_at":"2013-07-06T21:57:21Z","updated_at":"2016-03-15T13:57:01Z","pushed_at":"2016-03-15T15:39:39Z","git_url":"git://github.com/coreos/etcd.git","ssh_url":"git@github.com:coreos/etcd.git","clone_url":"https://github.com/coreos/etcd.git","svn_url":"https://github.com/coreos/etcd","homepage":"https://coreos.com/etcd/docs/latest/","size":24370,"stargazers_count":8960,"watchers_count":8960,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1474,"mirror_url":null,"open_issues_count":139,"forks":1474,"open_issues":139,"watchers":8960,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/coreos/etcd/pulls/4760"},"html":{"href":"https://github.com/coreos/etcd/pull/4760"},"issue":{"href":"https://api.github.com/repos/coreos/etcd/issues/4760"},"comments":{"href":"https://api.github.com/repos/coreos/etcd/issues/4760/comments"},"review_comments":{"href":"https://api.github.com/repos/coreos/etcd/pulls/4760/comments"},"review_comment":{"href":"https://api.github.com/repos/coreos/etcd/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/coreos/etcd/pulls/4760/commits"},"statuses":{"href":"https://api.github.com/repos/coreos/etcd/statuses/0f318e5151a8c616d8000c0d235d52aa821c5009"}}}}