{"action":"created","comment":{"url":"https://api.github.com/repos/m3db/m3cluster/pulls/comments/99470887","pull_request_review_id":20152488,"id":99470887,"diff_hunk":"@@ -0,0 +1,148 @@\n+// Copyright (c) 2016 Uber Technologies, Inc.\n+//\n+// Permission is hereby granted, free of charge, to any person obtaining a copy\n+// of this software and associated documentation files (the \"Software\"), to deal\n+// in the Software without restriction, including without limitation the rights\n+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n+// copies of the Software, and to permit persons to whom the Software is\n+// furnished to do so, subject to the following conditions:\n+//\n+// The above copyright notice and this permission notice shall be included in\n+// all copies or substantial portions of the Software.\n+//\n+// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n+// THE SOFTWARE.\n+\n+package watchmanager\n+\n+import (\n+\t\"context\"\n+\t\"fmt\"\n+\t\"time\"\n+\n+\t\"github.com/m3db/m3x/log\"\n+\n+\t\"github.com/coreos/etcd/clientv3\"\n+\t\"github.com/uber-go/tally\"\n+)\n+\n+// NewWatchManager creates a new watch manager\n+func NewWatchManager(opts Options) (WatchManager, error) {\n+\tif err := opts.Validate(); err != nil {\n+\t\treturn nil, err\n+\t}\n+\n+\tscope := opts.InstrumentsOptions().MetricsScope()\n+\treturn &manager{\n+\t\topts:   opts,\n+\t\tlogger: opts.InstrumentsOptions().Logger(),\n+\t\tm: metrics{\n+\t\t\tetcdWatchCreate: scope.Counter(\"etcd-watch-create\"),\n+\t\t\tetcdWatchError:  scope.Counter(\"etcd-watch-error\"),\n+\t\t\tetcdWatchReset:  scope.Counter(\"etcd-watch-reset\"),\n+\t\t},\n+\t\tupdateFn:      opts.UpdateFn(),\n+\t\ttickAndStopFn: opts.TickAndStopFn(),\n+\t}, nil\n+}\n+\n+type manager struct {\n+\topts   Options\n+\tlogger xlog.Logger\n+\tm      metrics\n+\n+\tupdateFn      UpdateFn\n+\ttickAndStopFn TickAndStopFn\n+}\n+\n+type metrics struct {\n+\tetcdWatchCreate tally.Counter\n+\tetcdWatchError  tally.Counter\n+\tetcdWatchReset  tally.Counter\n+}\n+\n+func (w *manager) watchChanWithTimeout(key string) (clientv3.WatchChan, error) {\n+\tdoneCh := make(chan struct{})\n+\n+\tvar watchChan clientv3.WatchChan\n+\tgo func() {\n+\t\twatchChan = w.opts.Watcher().Watch(\n+\t\t\tclientv3.WithRequireLeader(context.Background()),\n+\t\t\tkey,\n+\t\t\tw.opts.WatchOptions()...,\n+\t\t)\n+\t\tclose(doneCh)\n+\t}()\n+\n+\tselect {\n+\tcase <-doneCh:\n+\t\treturn watchChan, nil\n+\tcase <-time.After(w.opts.WatchChanInitTimeout()):\n+\t\treturn nil, fmt.Errorf(\"etcd watch chan creation timed-out on key: %s\", key)\n+\t}\n+}\n+\n+func (w *manager) Watch(key string) {\n+\tticker := time.Tick(w.opts.WatchChanCheckInterval())\n+\n+\tvar (\n+\t\twatchChan clientv3.WatchChan\n+\t\terr       error\n+\t)\n+\tfor {\n+\t\tif watchChan == nil {\n+\t\t\tw.m.etcdWatchCreate.Inc(1)\n+\t\t\twatchChan, err = w.watchChanWithTimeout(key)\n+\t\t\tif err != nil {\n+\t\t\t\tw.logger.Errorf(\"could not create etcd watch, %v\", err)\n+\n+\t\t\t\t// NB(cw) when we failed to create a etcd watch channel\n+\t\t\t\t// we do a get for now and will try to recreate the watch chan later\n+\t\t\t\tif err = w.updateFn(key); err != nil {\n+\t\t\t\t\tw.logger.Errorf(\"failed to get value for key %s: %v\", key, err)\n+\t\t\t\t}\n+\t\t\t\t// avoid recreating watch channel too frequently\n+\t\t\t\ttime.Sleep(w.opts.WatchChanResetInterval())\n+\t\t\t\tcontinue\n+\t\t\t}\n+\t\t}\n+\n+\t\tselect {\n+\t\tcase r, ok := <-watchChan:\n+\t\t\tif !ok {\n+\t\t\t\t// the watch chan is closed, set it to nil so it will be recreated\n+\t\t\t\t// this is unlikely to happen but just to be defensive\n+\t\t\t\twatchChan = nil\n+\t\t\t\tw.logger.Warnf(\"etcd watch channel closed on key %s, recreating a watch channel\", key)\n+\n+\t\t\t\t// avoid recreating watch channel too frequently\n+\t\t\t\ttime.Sleep(w.opts.WatchChanResetInterval())\n+\t\t\t\tw.m.etcdWatchReset.Inc(1)\n+\n+\t\t\t\tcontinue\n+\t\t\t}\n+\n+\t\t\t// handle the update\n+\t\t\tif err = r.Err(); err != nil {\n+\t\t\t\tw.logger.Errorf(\"received error on watch channel: %v\", err)\n+\t\t\t\tw.m.etcdWatchError.Inc(1)\n+\t\t\t\t// do not stop here, even though the update contains an error\n+\t\t\t\t// we still take this chance to attemp a Get() for the latest value\n+\t\t\t}\n+\n+\t\t\tif err = w.updateFn(key); err != nil {\n+\t\t\t\tw.logger.Errorf(\"received notification for key %s, but failed to get value: %v\", key, err)\n+\t\t\t}\n+\t\tcase <-ticker:\n+\t\t\tif w.tickAndStopFn(key) {","path":"etcd/watchmanager/manager.go","position":142,"original_position":142,"commit_id":"4b426ff15d1d6699bd98b9561d18759a123da9ca","original_commit_id":"4b426ff15d1d6699bd98b9561d18759a123da9ca","user":{"login":"robskillington","id":339615,"avatar_url":"https://avatars.githubusercontent.com/u/339615?v=3","gravatar_id":"","url":"https://api.github.com/users/robskillington","html_url":"https://github.com/robskillington","followers_url":"https://api.github.com/users/robskillington/followers","following_url":"https://api.github.com/users/robskillington/following{/other_user}","gists_url":"https://api.github.com/users/robskillington/gists{/gist_id}","starred_url":"https://api.github.com/users/robskillington/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/robskillington/subscriptions","organizations_url":"https://api.github.com/users/robskillington/orgs","repos_url":"https://api.github.com/users/robskillington/repos","events_url":"https://api.github.com/users/robskillington/events{/privacy}","received_events_url":"https://api.github.com/users/robskillington/received_events","type":"User","site_admin":false},"body":"Instead of reading a ticker and check if it should stop, wouldn't it be better to immediately signal stop through some channel?  Otherwise we're going to be waking up goroutines that don't necessarily need to be woken.","created_at":"2017-02-04T17:25:25Z","updated_at":"2017-02-04T17:25:25Z","html_url":"https://github.com/m3db/m3cluster/pull/77#discussion_r99470887","pull_request_url":"https://api.github.com/repos/m3db/m3cluster/pulls/77","_links":{"self":{"href":"https://api.github.com/repos/m3db/m3cluster/pulls/comments/99470887"},"html":{"href":"https://github.com/m3db/m3cluster/pull/77#discussion_r99470887"},"pull_request":{"href":"https://api.github.com/repos/m3db/m3cluster/pulls/77"}}},"pull_request":{"url":"https://api.github.com/repos/m3db/m3cluster/pulls/77","id":103843545,"html_url":"https://github.com/m3db/m3cluster/pull/77","diff_url":"https://github.com/m3db/m3cluster/pull/77.diff","patch_url":"https://github.com/m3db/m3cluster/pull/77.patch","issue_url":"https://api.github.com/repos/m3db/m3cluster/issues/77","number":77,"state":"open","locked":false,"title":"Make watch() non-blocking","user":{"login":"cw9","id":6710599,"avatar_url":"https://avatars.githubusercontent.com/u/6710599?v=3","gravatar_id":"","url":"https://api.github.com/users/cw9","html_url":"https://github.com/cw9","followers_url":"https://api.github.com/users/cw9/followers","following_url":"https://api.github.com/users/cw9/following{/other_user}","gists_url":"https://api.github.com/users/cw9/gists{/gist_id}","starred_url":"https://api.github.com/users/cw9/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cw9/subscriptions","organizations_url":"https://api.github.com/users/cw9/orgs","repos_url":"https://api.github.com/users/cw9/repos","events_url":"https://api.github.com/users/cw9/events{/privacy}","received_events_url":"https://api.github.com/users/cw9/received_events","type":"User","site_admin":false},"body":"@xichen2020 @robskillington ","created_at":"2017-01-30T23:29:46Z","updated_at":"2017-02-04T17:25:25Z","closed_at":null,"merged_at":null,"merge_commit_sha":"810dbf351d90a6ecaa5cc1169764c72452f29ef0","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/m3db/m3cluster/pulls/77/commits","review_comments_url":"https://api.github.com/repos/m3db/m3cluster/pulls/77/comments","review_comment_url":"https://api.github.com/repos/m3db/m3cluster/pulls/comments{/number}","comments_url":"https://api.github.com/repos/m3db/m3cluster/issues/77/comments","statuses_url":"https://api.github.com/repos/m3db/m3cluster/statuses/4b426ff15d1d6699bd98b9561d18759a123da9ca","head":{"label":"m3db:chao-etcd-version","ref":"chao-etcd-version","sha":"4b426ff15d1d6699bd98b9561d18759a123da9ca","user":{"login":"m3db","id":19937171,"avatar_url":"https://avatars.githubusercontent.com/u/19937171?v=3","gravatar_id":"","url":"https://api.github.com/users/m3db","html_url":"https://github.com/m3db","followers_url":"https://api.github.com/users/m3db/followers","following_url":"https://api.github.com/users/m3db/following{/other_user}","gists_url":"https://api.github.com/users/m3db/gists{/gist_id}","starred_url":"https://api.github.com/users/m3db/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m3db/subscriptions","organizations_url":"https://api.github.com/users/m3db/orgs","repos_url":"https://api.github.com/users/m3db/repos","events_url":"https://api.github.com/users/m3db/events{/privacy}","received_events_url":"https://api.github.com/users/m3db/received_events","type":"Organization","site_admin":false},"repo":{"id":64314919,"name":"m3cluster","full_name":"m3db/m3cluster","owner":{"login":"m3db","id":19937171,"avatar_url":"https://avatars.githubusercontent.com/u/19937171?v=3","gravatar_id":"","url":"https://api.github.com/users/m3db","html_url":"https://github.com/m3db","followers_url":"https://api.github.com/users/m3db/followers","following_url":"https://api.github.com/users/m3db/following{/other_user}","gists_url":"https://api.github.com/users/m3db/gists{/gist_id}","starred_url":"https://api.github.com/users/m3db/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m3db/subscriptions","organizations_url":"https://api.github.com/users/m3db/orgs","repos_url":"https://api.github.com/users/m3db/repos","events_url":"https://api.github.com/users/m3db/events{/privacy}","received_events_url":"https://api.github.com/users/m3db/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/m3db/m3cluster","description":"Cluster management interfaces (service discovery, config distribution) supporting pluggable backends","fork":false,"url":"https://api.github.com/repos/m3db/m3cluster","forks_url":"https://api.github.com/repos/m3db/m3cluster/forks","keys_url":"https://api.github.com/repos/m3db/m3cluster/keys{/key_id}","collaborators_url":"https://api.github.com/repos/m3db/m3cluster/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/m3db/m3cluster/teams","hooks_url":"https://api.github.com/repos/m3db/m3cluster/hooks","issue_events_url":"https://api.github.com/repos/m3db/m3cluster/issues/events{/number}","events_url":"https://api.github.com/repos/m3db/m3cluster/events","assignees_url":"https://api.github.com/repos/m3db/m3cluster/assignees{/user}","branches_url":"https://api.github.com/repos/m3db/m3cluster/branches{/branch}","tags_url":"https://api.github.com/repos/m3db/m3cluster/tags","blobs_url":"https://api.github.com/repos/m3db/m3cluster/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/m3db/m3cluster/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/m3db/m3cluster/git/refs{/sha}","trees_url":"https://api.github.com/repos/m3db/m3cluster/git/trees{/sha}","statuses_url":"https://api.github.com/repos/m3db/m3cluster/statuses/{sha}","languages_url":"https://api.github.com/repos/m3db/m3cluster/languages","stargazers_url":"https://api.github.com/repos/m3db/m3cluster/stargazers","contributors_url":"https://api.github.com/repos/m3db/m3cluster/contributors","subscribers_url":"https://api.github.com/repos/m3db/m3cluster/subscribers","subscription_url":"https://api.github.com/repos/m3db/m3cluster/subscription","commits_url":"https://api.github.com/repos/m3db/m3cluster/commits{/sha}","git_commits_url":"https://api.github.com/repos/m3db/m3cluster/git/commits{/sha}","comments_url":"https://api.github.com/repos/m3db/m3cluster/comments{/number}","issue_comment_url":"https://api.github.com/repos/m3db/m3cluster/issues/comments{/number}","contents_url":"https://api.github.com/repos/m3db/m3cluster/contents/{+path}","compare_url":"https://api.github.com/repos/m3db/m3cluster/compare/{base}...{head}","merges_url":"https://api.github.com/repos/m3db/m3cluster/merges","archive_url":"https://api.github.com/repos/m3db/m3cluster/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/m3db/m3cluster/downloads","issues_url":"https://api.github.com/repos/m3db/m3cluster/issues{/number}","pulls_url":"https://api.github.com/repos/m3db/m3cluster/pulls{/number}","milestones_url":"https://api.github.com/repos/m3db/m3cluster/milestones{/number}","notifications_url":"https://api.github.com/repos/m3db/m3cluster/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/m3db/m3cluster/labels{/name}","releases_url":"https://api.github.com/repos/m3db/m3cluster/releases{/id}","deployments_url":"https://api.github.com/repos/m3db/m3cluster/deployments","created_at":"2016-07-27T14:22:38Z","updated_at":"2017-01-25T06:11:33Z","pushed_at":"2017-02-03T21:46:47Z","git_url":"git://github.com/m3db/m3cluster.git","ssh_url":"git@github.com:m3db/m3cluster.git","clone_url":"https://github.com/m3db/m3cluster.git","svn_url":"https://github.com/m3db/m3cluster","homepage":"","size":290,"stargazers_count":6,"watchers_count":6,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":4,"forks":0,"open_issues":4,"watchers":6,"default_branch":"master"}},"base":{"label":"m3db:master","ref":"master","sha":"41253a12011658b9a20f91bdd245b79e8c0f5800","user":{"login":"m3db","id":19937171,"avatar_url":"https://avatars.githubusercontent.com/u/19937171?v=3","gravatar_id":"","url":"https://api.github.com/users/m3db","html_url":"https://github.com/m3db","followers_url":"https://api.github.com/users/m3db/followers","following_url":"https://api.github.com/users/m3db/following{/other_user}","gists_url":"https://api.github.com/users/m3db/gists{/gist_id}","starred_url":"https://api.github.com/users/m3db/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m3db/subscriptions","organizations_url":"https://api.github.com/users/m3db/orgs","repos_url":"https://api.github.com/users/m3db/repos","events_url":"https://api.github.com/users/m3db/events{/privacy}","received_events_url":"https://api.github.com/users/m3db/received_events","type":"Organization","site_admin":false},"repo":{"id":64314919,"name":"m3cluster","full_name":"m3db/m3cluster","owner":{"login":"m3db","id":19937171,"avatar_url":"https://avatars.githubusercontent.com/u/19937171?v=3","gravatar_id":"","url":"https://api.github.com/users/m3db","html_url":"https://github.com/m3db","followers_url":"https://api.github.com/users/m3db/followers","following_url":"https://api.github.com/users/m3db/following{/other_user}","gists_url":"https://api.github.com/users/m3db/gists{/gist_id}","starred_url":"https://api.github.com/users/m3db/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m3db/subscriptions","organizations_url":"https://api.github.com/users/m3db/orgs","repos_url":"https://api.github.com/users/m3db/repos","events_url":"https://api.github.com/users/m3db/events{/privacy}","received_events_url":"https://api.github.com/users/m3db/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/m3db/m3cluster","description":"Cluster management interfaces (service discovery, config distribution) supporting pluggable backends","fork":false,"url":"https://api.github.com/repos/m3db/m3cluster","forks_url":"https://api.github.com/repos/m3db/m3cluster/forks","keys_url":"https://api.github.com/repos/m3db/m3cluster/keys{/key_id}","collaborators_url":"https://api.github.com/repos/m3db/m3cluster/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/m3db/m3cluster/teams","hooks_url":"https://api.github.com/repos/m3db/m3cluster/hooks","issue_events_url":"https://api.github.com/repos/m3db/m3cluster/issues/events{/number}","events_url":"https://api.github.com/repos/m3db/m3cluster/events","assignees_url":"https://api.github.com/repos/m3db/m3cluster/assignees{/user}","branches_url":"https://api.github.com/repos/m3db/m3cluster/branches{/branch}","tags_url":"https://api.github.com/repos/m3db/m3cluster/tags","blobs_url":"https://api.github.com/repos/m3db/m3cluster/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/m3db/m3cluster/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/m3db/m3cluster/git/refs{/sha}","trees_url":"https://api.github.com/repos/m3db/m3cluster/git/trees{/sha}","statuses_url":"https://api.github.com/repos/m3db/m3cluster/statuses/{sha}","languages_url":"https://api.github.com/repos/m3db/m3cluster/languages","stargazers_url":"https://api.github.com/repos/m3db/m3cluster/stargazers","contributors_url":"https://api.github.com/repos/m3db/m3cluster/contributors","subscribers_url":"https://api.github.com/repos/m3db/m3cluster/subscribers","subscription_url":"https://api.github.com/repos/m3db/m3cluster/subscription","commits_url":"https://api.github.com/repos/m3db/m3cluster/commits{/sha}","git_commits_url":"https://api.github.com/repos/m3db/m3cluster/git/commits{/sha}","comments_url":"https://api.github.com/repos/m3db/m3cluster/comments{/number}","issue_comment_url":"https://api.github.com/repos/m3db/m3cluster/issues/comments{/number}","contents_url":"https://api.github.com/repos/m3db/m3cluster/contents/{+path}","compare_url":"https://api.github.com/repos/m3db/m3cluster/compare/{base}...{head}","merges_url":"https://api.github.com/repos/m3db/m3cluster/merges","archive_url":"https://api.github.com/repos/m3db/m3cluster/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/m3db/m3cluster/downloads","issues_url":"https://api.github.com/repos/m3db/m3cluster/issues{/number}","pulls_url":"https://api.github.com/repos/m3db/m3cluster/pulls{/number}","milestones_url":"https://api.github.com/repos/m3db/m3cluster/milestones{/number}","notifications_url":"https://api.github.com/repos/m3db/m3cluster/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/m3db/m3cluster/labels{/name}","releases_url":"https://api.github.com/repos/m3db/m3cluster/releases{/id}","deployments_url":"https://api.github.com/repos/m3db/m3cluster/deployments","created_at":"2016-07-27T14:22:38Z","updated_at":"2017-01-25T06:11:33Z","pushed_at":"2017-02-03T21:46:47Z","git_url":"git://github.com/m3db/m3cluster.git","ssh_url":"git@github.com:m3db/m3cluster.git","clone_url":"https://github.com/m3db/m3cluster.git","svn_url":"https://github.com/m3db/m3cluster","homepage":"","size":290,"stargazers_count":6,"watchers_count":6,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":4,"forks":0,"open_issues":4,"watchers":6,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/m3db/m3cluster/pulls/77"},"html":{"href":"https://github.com/m3db/m3cluster/pull/77"},"issue":{"href":"https://api.github.com/repos/m3db/m3cluster/issues/77"},"comments":{"href":"https://api.github.com/repos/m3db/m3cluster/issues/77/comments"},"review_comments":{"href":"https://api.github.com/repos/m3db/m3cluster/pulls/77/comments"},"review_comment":{"href":"https://api.github.com/repos/m3db/m3cluster/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/m3db/m3cluster/pulls/77/commits"},"statuses":{"href":"https://api.github.com/repos/m3db/m3cluster/statuses/4b426ff15d1d6699bd98b9561d18759a123da9ca"}}}}
{"actor":{"display_login":"robskillington"}}