{"action":"created","comment":{"url":"https://api.github.com/repos/square/p2/pulls/comments/41206743","id":41206743,"diff_hunk":"@@ -0,0 +1,183 @@\n+package rc\n+\n+import (\n+\t\"github.com/square/p2/Godeps/_workspace/src/github.com/Sirupsen/logrus\"\n+\n+\t\"github.com/square/p2/pkg/kp/rcstore\"\n+\t\"github.com/square/p2/pkg/labels\"\n+\t\"github.com/square/p2/pkg/logging\"\n+\t\"github.com/square/p2/pkg/rc/fields\"\n+)\n+\n+// The RCManager is responsible for spawning and reaping replication controllers\n+// as they are added to and deleted from Consul. Multiple managers can exist\n+// simultaneously, but each one must hold a different Consul session. This\n+// ensures that the managers do not instantiate the same replication controller\n+// multiple times.\n+type RCManager struct {\n+\t// constructor arguments for rcs created by this manager\n+\tkpStore   kpStore\n+\trcStore   rcstore.Store\n+\tscheduler Scheduler\n+\tlabeler   labels.Applicator\n+\n+\t// session stream for the rcs locked by this manager\n+\tsessions <-chan string\n+\n+\t// rcs managed by this manager\n+\tchildren map[fields.ID]managedRC\n+\n+\tlogger logging.Logger\n+}\n+\n+type managedRC struct {\n+\trc   ReplicationController\n+\tquit chan<- struct{}\n+}\n+\n+func NewManager(\n+\tkpStore kpStore,\n+\trcs rcstore.Store,\n+\tscheduler Scheduler,\n+\tlabeler labels.Applicator,\n+\tsessions <-chan string,\n+\tlogger logging.Logger,\n+) *RCManager {\n+\treturn &RCManager{\n+\t\tkpStore:   kpStore,\n+\t\trcStore:   rcs,\n+\t\tscheduler: scheduler,\n+\t\tlabeler:   labeler,\n+\t\tsessions:  sessions,\n+\t\tlogger:    logger,\n+\t\tchildren:  make(map[fields.ID]managedRC),\n+\t}\n+}\n+\n+// Start is a blocking function that monitors Consul for replication controllers.\n+// The RCManager will attempt to claim replication controllers as they appear\n+// and, if successful, will start goroutines for those replication controllers\n+// to do their job. Closing the quit channel will cause this function to\n+// return, but does not release locks.\n+//\n+// Start is not safe for concurrent execution. Do not execute multiple\n+// concurrent instances of Start.\n+func (rcm *RCManager) Start(quit <-chan struct{}) {\n+\tsubQuit := make(chan struct{})\n+\tdefer close(subQuit)\n+\trcWatch, rcErr := rcm.rcStore.WatchNew(subQuit)\n+\tvar session string\n+\n+\tfor {\n+\t\tselect {\n+\t\tcase <-quit:\n+\t\t\t// TODO: we have to release these locks as well to make sure others\n+\t\t\t// can capture them, otherwise the lock will persist until it expires\n+\t\t\t// on its own. in practice, we will also kill the consul session\n+\t\t\t// manager, so is this really a problem?\n+\t\t\trcm.logger.WithField(\"session\", session).Infoln(\"Halt requested, releasing replication controllers\")\n+\t\t\trcm.releaseChildren()\n+\t\t\treturn\n+\t\tcase session = <-rcm.sessions:\n+\t\t\tif session == \"\" {\n+\t\t\t\t// our session has expired, we must assume our locked children\n+\t\t\t\t// have all been released and that someone else may have\n+\t\t\t\t// claimed them by now\n+\t\t\t\trcm.logger.NoFields().Errorln(\"Session expired, releasing replication controllers\")\n+\t\t\t\trcm.releaseChildren()\n+\t\t\t} else {\n+\t\t\t\t// a new session has been acquired - only happens after an\n+\t\t\t\t// expiration message, so len(children)==0. right now this is a\n+\t\t\t\t// noop, since the session has been set to its new value, but in\n+\t\t\t\t// the future we should use a refactored lock API and instantiate\n+\t\t\t\t// it here\n+\t\t\t\trcm.logger.WithField(\"session\", session).Infoln(\"Acquired new session\")\n+\t\t\t\t// TODO: restart the watch so that you get updates right away?\n+\t\t\t}\n+\t\tcase err := <-rcErr:\n+\t\t\trcm.logger.WithError(err).Errorln(\"Could not read consul replication controllers\")\n+\t\tcase rcFields := <-rcWatch:\n+\t\t\trcm.logger.WithField(\"n\", len(rcFields)).Debugln(\"Received replication controller update\")\n+\t\t\tif session == \"\" {\n+\t\t\t\t// we can't claim new nodes because our session is invalidated.\n+\t\t\t\t// raise an error and ignore this update\n+\t\t\t\trcm.logger.NoFields().Infoln(\"Received replication controller update, but do not have session to acquire locks\")\n+\t\t\t\tcontinue","path":"pkg/rc/manager.go","position":105,"original_position":105,"commit_id":"6488b01c3621fd432f783e4296aae401765d8d26","original_commit_id":"6488b01c3621fd432f783e4296aae401765d8d26","user":{"login":"anthonybishopric","id":540783,"avatar_url":"https://avatars.githubusercontent.com/u/540783?v=3","gravatar_id":"","url":"https://api.github.com/users/anthonybishopric","html_url":"https://github.com/anthonybishopric","followers_url":"https://api.github.com/users/anthonybishopric/followers","following_url":"https://api.github.com/users/anthonybishopric/following{/other_user}","gists_url":"https://api.github.com/users/anthonybishopric/gists{/gist_id}","starred_url":"https://api.github.com/users/anthonybishopric/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/anthonybishopric/subscriptions","organizations_url":"https://api.github.com/users/anthonybishopric/orgs","repos_url":"https://api.github.com/users/anthonybishopric/repos","events_url":"https://api.github.com/users/anthonybishopric/events{/privacy}","received_events_url":"https://api.github.com/users/anthonybishopric/received_events","type":"User","site_admin":false},"body":"Can we recover if we throw away results from `rcWatch` when we don't have a session at the time when they're received? What if we get a session on the next iteration of this loop?","created_at":"2015-10-05T22:35:38Z","updated_at":"2015-10-05T22:35:38Z","html_url":"https://github.com/square/p2/pull/281#discussion_r41206743","pull_request_url":"https://api.github.com/repos/square/p2/pulls/281","_links":{"self":{"href":"https://api.github.com/repos/square/p2/pulls/comments/41206743"},"html":{"href":"https://github.com/square/p2/pull/281#discussion_r41206743"},"pull_request":{"href":"https://api.github.com/repos/square/p2/pulls/281"}}},"pull_request":{"url":"https://api.github.com/repos/square/p2/pulls/281","id":46839680,"html_url":"https://github.com/square/p2/pull/281","diff_url":"https://github.com/square/p2/pull/281.diff","patch_url":"https://github.com/square/p2/pull/281.patch","issue_url":"https://api.github.com/repos/square/p2/issues/281","number":281,"state":"open","locked":false,"title":"Add RC manager","user":{"login":"tummychow","id":4285049,"avatar_url":"https://avatars.githubusercontent.com/u/4285049?v=3","gravatar_id":"","url":"https://api.github.com/users/tummychow","html_url":"https://github.com/tummychow","followers_url":"https://api.github.com/users/tummychow/followers","following_url":"https://api.github.com/users/tummychow/following{/other_user}","gists_url":"https://api.github.com/users/tummychow/gists{/gist_id}","starred_url":"https://api.github.com/users/tummychow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tummychow/subscriptions","organizations_url":"https://api.github.com/users/tummychow/orgs","repos_url":"https://api.github.com/users/tummychow/repos","events_url":"https://api.github.com/users/tummychow/events{/privacy}","received_events_url":"https://api.github.com/users/tummychow/received_events","type":"User","site_admin":false},"body":"code mostly speaks for itself","created_at":"2015-10-05T21:59:42Z","updated_at":"2015-10-05T22:35:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f410dd48382e5c0a5e2a2188bb74737f2b195f69","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/square/p2/pulls/281/commits","review_comments_url":"https://api.github.com/repos/square/p2/pulls/281/comments","review_comment_url":"https://api.github.com/repos/square/p2/pulls/comments{/number}","comments_url":"https://api.github.com/repos/square/p2/issues/281/comments","statuses_url":"https://api.github.com/repos/square/p2/statuses/6488b01c3621fd432f783e4296aae401765d8d26","head":{"label":"tummychow:orch-rcfarm","ref":"orch-rcfarm","sha":"6488b01c3621fd432f783e4296aae401765d8d26","user":{"login":"tummychow","id":4285049,"avatar_url":"https://avatars.githubusercontent.com/u/4285049?v=3","gravatar_id":"","url":"https://api.github.com/users/tummychow","html_url":"https://github.com/tummychow","followers_url":"https://api.github.com/users/tummychow/followers","following_url":"https://api.github.com/users/tummychow/following{/other_user}","gists_url":"https://api.github.com/users/tummychow/gists{/gist_id}","starred_url":"https://api.github.com/users/tummychow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tummychow/subscriptions","organizations_url":"https://api.github.com/users/tummychow/orgs","repos_url":"https://api.github.com/users/tummychow/repos","events_url":"https://api.github.com/users/tummychow/events{/privacy}","received_events_url":"https://api.github.com/users/tummychow/received_events","type":"User","site_admin":false},"repo":{"id":29206040,"name":"p2","full_name":"tummychow/p2","owner":{"login":"tummychow","id":4285049,"avatar_url":"https://avatars.githubusercontent.com/u/4285049?v=3","gravatar_id":"","url":"https://api.github.com/users/tummychow","html_url":"https://github.com/tummychow","followers_url":"https://api.github.com/users/tummychow/followers","following_url":"https://api.github.com/users/tummychow/following{/other_user}","gists_url":"https://api.github.com/users/tummychow/gists{/gist_id}","starred_url":"https://api.github.com/users/tummychow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tummychow/subscriptions","organizations_url":"https://api.github.com/users/tummychow/orgs","repos_url":"https://api.github.com/users/tummychow/repos","events_url":"https://api.github.com/users/tummychow/events{/privacy}","received_events_url":"https://api.github.com/users/tummychow/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/tummychow/p2","description":"Platypus Platform: Tools for Scalable Deployment","fork":true,"url":"https://api.github.com/repos/tummychow/p2","forks_url":"https://api.github.com/repos/tummychow/p2/forks","keys_url":"https://api.github.com/repos/tummychow/p2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tummychow/p2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tummychow/p2/teams","hooks_url":"https://api.github.com/repos/tummychow/p2/hooks","issue_events_url":"https://api.github.com/repos/tummychow/p2/issues/events{/number}","events_url":"https://api.github.com/repos/tummychow/p2/events","assignees_url":"https://api.github.com/repos/tummychow/p2/assignees{/user}","branches_url":"https://api.github.com/repos/tummychow/p2/branches{/branch}","tags_url":"https://api.github.com/repos/tummychow/p2/tags","blobs_url":"https://api.github.com/repos/tummychow/p2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tummychow/p2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tummychow/p2/git/refs{/sha}","trees_url":"https://api.github.com/repos/tummychow/p2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tummychow/p2/statuses/{sha}","languages_url":"https://api.github.com/repos/tummychow/p2/languages","stargazers_url":"https://api.github.com/repos/tummychow/p2/stargazers","contributors_url":"https://api.github.com/repos/tummychow/p2/contributors","subscribers_url":"https://api.github.com/repos/tummychow/p2/subscribers","subscription_url":"https://api.github.com/repos/tummychow/p2/subscription","commits_url":"https://api.github.com/repos/tummychow/p2/commits{/sha}","git_commits_url":"https://api.github.com/repos/tummychow/p2/git/commits{/sha}","comments_url":"https://api.github.com/repos/tummychow/p2/comments{/number}","issue_comment_url":"https://api.github.com/repos/tummychow/p2/issues/comments{/number}","contents_url":"https://api.github.com/repos/tummychow/p2/contents/{+path}","compare_url":"https://api.github.com/repos/tummychow/p2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tummychow/p2/merges","archive_url":"https://api.github.com/repos/tummychow/p2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tummychow/p2/downloads","issues_url":"https://api.github.com/repos/tummychow/p2/issues{/number}","pulls_url":"https://api.github.com/repos/tummychow/p2/pulls{/number}","milestones_url":"https://api.github.com/repos/tummychow/p2/milestones{/number}","notifications_url":"https://api.github.com/repos/tummychow/p2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tummychow/p2/labels{/name}","releases_url":"https://api.github.com/repos/tummychow/p2/releases{/id}","created_at":"2015-01-13T19:07:54Z","updated_at":"2015-05-08T18:50:42Z","pushed_at":"2015-10-05T21:59:07Z","git_url":"git://github.com/tummychow/p2.git","ssh_url":"git@github.com:tummychow/p2.git","clone_url":"https://github.com/tummychow/p2.git","svn_url":"https://github.com/tummychow/p2","homepage":"","size":31794,"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":"square:master","ref":"master","sha":"8aa6a5ce5bcd6f6e44890f6ad2f38a406d6570fd","user":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"repo":{"id":25320424,"name":"p2","full_name":"square/p2","owner":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/square/p2","description":"Platypus Platform: Tools for Scalable Deployment","fork":false,"url":"https://api.github.com/repos/square/p2","forks_url":"https://api.github.com/repos/square/p2/forks","keys_url":"https://api.github.com/repos/square/p2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/square/p2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/square/p2/teams","hooks_url":"https://api.github.com/repos/square/p2/hooks","issue_events_url":"https://api.github.com/repos/square/p2/issues/events{/number}","events_url":"https://api.github.com/repos/square/p2/events","assignees_url":"https://api.github.com/repos/square/p2/assignees{/user}","branches_url":"https://api.github.com/repos/square/p2/branches{/branch}","tags_url":"https://api.github.com/repos/square/p2/tags","blobs_url":"https://api.github.com/repos/square/p2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/square/p2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/square/p2/git/refs{/sha}","trees_url":"https://api.github.com/repos/square/p2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/square/p2/statuses/{sha}","languages_url":"https://api.github.com/repos/square/p2/languages","stargazers_url":"https://api.github.com/repos/square/p2/stargazers","contributors_url":"https://api.github.com/repos/square/p2/contributors","subscribers_url":"https://api.github.com/repos/square/p2/subscribers","subscription_url":"https://api.github.com/repos/square/p2/subscription","commits_url":"https://api.github.com/repos/square/p2/commits{/sha}","git_commits_url":"https://api.github.com/repos/square/p2/git/commits{/sha}","comments_url":"https://api.github.com/repos/square/p2/comments{/number}","issue_comment_url":"https://api.github.com/repos/square/p2/issues/comments{/number}","contents_url":"https://api.github.com/repos/square/p2/contents/{+path}","compare_url":"https://api.github.com/repos/square/p2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/square/p2/merges","archive_url":"https://api.github.com/repos/square/p2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/square/p2/downloads","issues_url":"https://api.github.com/repos/square/p2/issues{/number}","pulls_url":"https://api.github.com/repos/square/p2/pulls{/number}","milestones_url":"https://api.github.com/repos/square/p2/milestones{/number}","notifications_url":"https://api.github.com/repos/square/p2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/square/p2/labels{/name}","releases_url":"https://api.github.com/repos/square/p2/releases{/id}","created_at":"2014-10-16T20:31:12Z","updated_at":"2015-10-01T19:33:30Z","pushed_at":"2015-10-05T21:59:42Z","git_url":"git://github.com/square/p2.git","ssh_url":"git@github.com:square/p2.git","clone_url":"https://github.com/square/p2.git","svn_url":"https://github.com/square/p2","homepage":"","size":33215,"stargazers_count":64,"watchers_count":64,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":12,"mirror_url":null,"open_issues_count":3,"forks":12,"open_issues":3,"watchers":64,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/square/p2/pulls/281"},"html":{"href":"https://github.com/square/p2/pull/281"},"issue":{"href":"https://api.github.com/repos/square/p2/issues/281"},"comments":{"href":"https://api.github.com/repos/square/p2/issues/281/comments"},"review_comments":{"href":"https://api.github.com/repos/square/p2/pulls/281/comments"},"review_comment":{"href":"https://api.github.com/repos/square/p2/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/square/p2/pulls/281/commits"},"statuses":{"href":"https://api.github.com/repos/square/p2/statuses/6488b01c3621fd432f783e4296aae401765d8d26"}}}}