{"action":"created","comment":{"url":"https://api.github.com/repos/m3db/m3cluster/pulls/comments/117542289","pull_request_review_id":39263787,"id":117542289,"diff_hunk":"@@ -0,0 +1,206 @@\n+// Copyright (c) 2017 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 placement\n+\n+import (\n+\t\"errors\"\n+\t\"sort\"\n+\t\"sync\"\n+\t\"sync/atomic\"\n+\n+\tplacementproto \"github.com/m3db/m3cluster/generated/proto/placement\"\n+\t\"github.com/m3db/m3cluster/services\"\n+\t\"github.com/m3db/m3x/clock\"\n+)\n+\n+var (\n+\terrNilPlacementSnapshotsProto  = errors.New(\"nil placement snapshots proto\")\n+\terrNoApplicablePlacement       = errors.New(\"no applicable placement found\")\n+\terrActiveStagedPlacementClosed = errors.New(\"active staged placement is closed\")\n+)\n+\n+type activeStagedPlacement struct {\n+\tsync.RWMutex\n+\n+\tplacements            []services.ServicePlacement\n+\tnowFn                 clock.NowFn\n+\tonPlacementsAddedFn   services.OnPlacementsAddedFn\n+\tonPlacementsRemovedFn services.OnPlacementsRemovedFn\n+\n+\texpiring int32\n+\tclosed   bool\n+\tdoneFn   services.DoneFn\n+}\n+\n+func newActiveStagedPlacement(\n+\tplacements []services.ServicePlacement,\n+\topts services.ActiveStagedPlacementOptions,\n+) services.ActiveStagedPlacement {\n+\tp := &activeStagedPlacement{\n+\t\tplacements:            placements,\n+\t\tnowFn:                 opts.ClockOptions().NowFn(),\n+\t\tonPlacementsAddedFn:   opts.OnPlacementsAddedFn(),\n+\t\tonPlacementsRemovedFn: opts.OnPlacementsRemovedFn(),\n+\t}\n+\tp.doneFn = p.onPlacementDone\n+\n+\tif p.onPlacementsAddedFn != nil {\n+\t\tp.onPlacementsAddedFn(placements)\n+\t}\n+\n+\treturn p\n+}\n+\n+func (p *activeStagedPlacement) ActivePlacement() (services.ServicePlacement, services.DoneFn, error) {\n+\tp.RLock()\n+\tplacement, err := p.activePlacementWithLock(p.nowFn().UnixNano())\n+\tif err != nil {\n+\t\tp.RUnlock()\n+\t\treturn nil, nil, err\n+\t}\n+\treturn placement, p.doneFn, nil\n+}\n+\n+func (p *activeStagedPlacement) Close() error {\n+\tp.Lock()\n+\tdefer p.Unlock()\n+\n+\tif p.closed {\n+\t\treturn errActiveStagedPlacementClosed\n+\t}\n+\tif p.onPlacementsRemovedFn != nil {\n+\t\tp.onPlacementsRemovedFn(p.placements)\n+\t}\n+\tp.placements = nil\n+\treturn nil\n+}\n+\n+func (p *activeStagedPlacement) onPlacementDone() { p.RUnlock() }\n+\n+func (p *activeStagedPlacement) activePlacementWithLock(timeNanos int64) (services.ServicePlacement, error) {\n+\tif p.closed {\n+\t\treturn nil, errActiveStagedPlacementClosed\n+\t}\n+\tidx := p.activeIndexWithLock(timeNanos)\n+\tif idx < 0 {\n+\t\treturn nil, errNoApplicablePlacement\n+\t}\n+\tplacement := p.placements[idx]\n+\t// If the placement that's in effect is not the first placment, expire the stale ones.\n+\tif idx > 0 && atomic.CompareAndSwapInt32(&p.expiring, 0, 1) {\n+\t\tgo p.expire()\n+\t}\n+\treturn placement, nil\n+}\n+\n+// activeIndexWithLock finds the index of the last placement whose cutover time is no\n+// later than t (a.k.a. the active placement). The cutover times of the placements are\n+// sorted in ascending order (i.e., earliest time first).\n+func (p *activeStagedPlacement) activeIndexWithLock(timeNanos int64) int {\n+\tidx := 0\n+\tfor idx < len(p.placements) && p.placements[idx].CutoverNanos() <= timeNanos {","path":"services/placement/staged_placement.go","position":119,"original_position":119,"commit_id":"e6a3959f9154608c108485ce2e0efd2fd59d978d","original_commit_id":"e6a3959f9154608c108485ce2e0efd2fd59d978d","user":{"login":"cw9","id":6710599,"avatar_url":"https://avatars3.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":"iterate from len to 0?","created_at":"2017-05-19T18:12:01Z","updated_at":"2017-05-19T19:00:48Z","html_url":"https://github.com/m3db/m3cluster/pull/124#discussion_r117542289","pull_request_url":"https://api.github.com/repos/m3db/m3cluster/pulls/124","_links":{"self":{"href":"https://api.github.com/repos/m3db/m3cluster/pulls/comments/117542289"},"html":{"href":"https://github.com/m3db/m3cluster/pull/124#discussion_r117542289"},"pull_request":{"href":"https://api.github.com/repos/m3db/m3cluster/pulls/124"}}},"pull_request":{"url":"https://api.github.com/repos/m3db/m3cluster/pulls/124","id":120967232,"html_url":"https://github.com/m3db/m3cluster/pull/124","diff_url":"https://github.com/m3db/m3cluster/pull/124.diff","patch_url":"https://github.com/m3db/m3cluster/pull/124.patch","issue_url":"https://api.github.com/repos/m3db/m3cluster/issues/124","number":124,"state":"open","locked":false,"title":"Add CutoverNanos property and InstancesForShard method for ServicePlacement","user":{"login":"xichen2020","id":12239421,"avatar_url":"https://avatars2.githubusercontent.com/u/12239421?v=3","gravatar_id":"","url":"https://api.github.com/users/xichen2020","html_url":"https://github.com/xichen2020","followers_url":"https://api.github.com/users/xichen2020/followers","following_url":"https://api.github.com/users/xichen2020/following{/other_user}","gists_url":"https://api.github.com/users/xichen2020/gists{/gist_id}","starred_url":"https://api.github.com/users/xichen2020/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xichen2020/subscriptions","organizations_url":"https://api.github.com/users/xichen2020/orgs","repos_url":"https://api.github.com/users/xichen2020/repos","events_url":"https://api.github.com/users/xichen2020/events{/privacy}","received_events_url":"https://api.github.com/users/xichen2020/received_events","type":"User","site_admin":false},"body":"cc @cw9 \r\n\r\nThis PR adds `CutoverNanos()` and `InstancesForShard` to the `ServicePlacement` interface so they can consumed by users outside m3cluster.","created_at":"2017-05-17T03:17:13Z","updated_at":"2017-05-19T19:00:48Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ed560c6d545a33ec911a4f59042797a531e84d1c","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/m3db/m3cluster/pulls/124/commits","review_comments_url":"https://api.github.com/repos/m3db/m3cluster/pulls/124/comments","review_comment_url":"https://api.github.com/repos/m3db/m3cluster/pulls/comments{/number}","comments_url":"https://api.github.com/repos/m3db/m3cluster/issues/124/comments","statuses_url":"https://api.github.com/repos/m3db/m3cluster/statuses/e6a3959f9154608c108485ce2e0efd2fd59d978d","head":{"label":"m3db:xichen-placement-utility","ref":"xichen-placement-utility","sha":"e6a3959f9154608c108485ce2e0efd2fd59d978d","user":{"login":"m3db","id":19937171,"avatar_url":"https://avatars3.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://avatars3.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-04-26T01:27:02Z","pushed_at":"2017-05-19T17:52:44Z","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":426,"stargazers_count":7,"watchers_count":7,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":8,"forks":0,"open_issues":8,"watchers":7,"default_branch":"master"}},"base":{"label":"m3db:master","ref":"master","sha":"7605d30b5d979e7828dad2d242daa9980161a927","user":{"login":"m3db","id":19937171,"avatar_url":"https://avatars3.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://avatars3.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-04-26T01:27:02Z","pushed_at":"2017-05-19T17:52:44Z","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":426,"stargazers_count":7,"watchers_count":7,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":8,"forks":0,"open_issues":8,"watchers":7,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/m3db/m3cluster/pulls/124"},"html":{"href":"https://github.com/m3db/m3cluster/pull/124"},"issue":{"href":"https://api.github.com/repos/m3db/m3cluster/issues/124"},"comments":{"href":"https://api.github.com/repos/m3db/m3cluster/issues/124/comments"},"review_comments":{"href":"https://api.github.com/repos/m3db/m3cluster/pulls/124/comments"},"review_comment":{"href":"https://api.github.com/repos/m3db/m3cluster/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/m3db/m3cluster/pulls/124/commits"},"statuses":{"href":"https://api.github.com/repos/m3db/m3cluster/statuses/e6a3959f9154608c108485ce2e0efd2fd59d978d"}}}}
{"actor":{"display_login":"cw9"}}