{"action":"created","comment":{"url":"https://api.github.com/repos/juju/juju/pulls/comments/99259805","pull_request_review_id":19931845,"id":99259805,"diff_hunk":"@@ -405,22 +405,35 @@ func (u *Unit) destroyOps() ([]txn.Op, error) {\n \n \t// See if the unit agent has started running.\n \t// If so then we can't set directly to dead.\n+\tisAssigned := u.doc.MachineId != \"\"\n \tagentStatusDocId := u.globalAgentKey()\n \tagentStatusInfo, agentErr := getStatus(u.st, agentStatusDocId, \"agent\")\n \tif errors.IsNotFound(agentErr) {\n \t\treturn nil, errAlreadyDying\n \t} else if agentErr != nil {\n \t\treturn nil, errors.Trace(agentErr)\n \t}\n-\tif agentStatusInfo.Status != status.Allocating {\n+\tif isAssigned && agentStatusInfo.Status != status.Allocating {\n \t\treturn setDyingOps, nil\n \t}\n+\tif agentStatusInfo.Status != status.Error && agentStatusInfo.Status != status.Allocating {\n+\t\treturn nil, errors.Errorf(\"unexpected unit state - unit with status %v is not assigned to a machine\", agentStatusInfo.Status)\n+\t}\n \n-\tops := []txn.Op{{\n-\t\tC:      statusesC,\n-\t\tId:     u.st.docID(agentStatusDocId),\n-\t\tAssert: bson.D{{\"status\", status.Allocating}},\n-\t}, minUnitsOp}\n+\t// TODO(wallyworld) - we really want to assert on status==Error && machineid==\"\"\n+\t// but we can't do that across collections. For now, if a unit cannot be assigned,\n+\t// it will stay in that state and status and machineid will not change.\n+\tstatusOp := txn.Op{\n+\t\tC:  statusesC,\n+\t\tId: u.st.docID(agentStatusDocId),\n+\t}\n+\tif isAssigned {\n+\t\tstatusOp.Assert = bson.D{{\"status\", status.Allocating}}\n+\t} else {\n+\t\tstatusOp.Assert = bson.D{{\"$or\", []bson.D{{{\"status\", status.Allocating}}, {{\"status\", status.Error}}}}}","path":"state/unit.go","position":35,"original_position":35,"commit_id":"ec1e626de9865cd3f6ba9c1aa290e75365264c94","original_commit_id":"ec1e626de9865cd3f6ba9c1aa290e75365264c94","user":{"login":"axw","id":843579,"avatar_url":"https://avatars.githubusercontent.com/u/843579?v=3","gravatar_id":"","url":"https://api.github.com/users/axw","html_url":"https://github.com/axw","followers_url":"https://api.github.com/users/axw/followers","following_url":"https://api.github.com/users/axw/following{/other_user}","gists_url":"https://api.github.com/users/axw/gists{/gist_id}","starred_url":"https://api.github.com/users/axw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/axw/subscriptions","organizations_url":"https://api.github.com/users/axw/orgs","repos_url":"https://api.github.com/users/axw/repos","events_url":"https://api.github.com/users/axw/events{/privacy}","received_events_url":"https://api.github.com/users/axw/received_events","type":"User","site_admin":false},"body":"I don't think this is good enough. A unit could be concurrently assigned to a machine when it's in error state (esp. once the assigner retries automatically).\r\n\r\nWe already do multi-collection assertions (statusOp, minUnitsOp, and isAliveDoc all assert on different collections). Please check machineid is empty when status is error.","created_at":"2017-02-03T01:16:27Z","updated_at":"2017-02-03T01:19:16Z","html_url":"https://github.com/juju/juju/pull/6906#discussion_r99259805","pull_request_url":"https://api.github.com/repos/juju/juju/pulls/6906","_links":{"self":{"href":"https://api.github.com/repos/juju/juju/pulls/comments/99259805"},"html":{"href":"https://github.com/juju/juju/pull/6906#discussion_r99259805"},"pull_request":{"href":"https://api.github.com/repos/juju/juju/pulls/6906"}}},"pull_request":{"url":"https://api.github.com/repos/juju/juju/pulls/6906","id":104339968,"html_url":"https://github.com/juju/juju/pull/6906","diff_url":"https://github.com/juju/juju/pull/6906.diff","patch_url":"https://github.com/juju/juju/pull/6906.patch","issue_url":"https://api.github.com/repos/juju/juju/issues/6906","number":6906,"state":"open","locked":false,"title":"Units which are unassigned to  a machine due to error can be removed","user":{"login":"wallyworld","id":5808968,"avatar_url":"https://avatars.githubusercontent.com/u/5808968?v=3","gravatar_id":"","url":"https://api.github.com/users/wallyworld","html_url":"https://github.com/wallyworld","followers_url":"https://api.github.com/users/wallyworld/followers","following_url":"https://api.github.com/users/wallyworld/following{/other_user}","gists_url":"https://api.github.com/users/wallyworld/gists{/gist_id}","starred_url":"https://api.github.com/users/wallyworld/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wallyworld/subscriptions","organizations_url":"https://api.github.com/users/wallyworld/orgs","repos_url":"https://api.github.com/users/wallyworld/repos","events_url":"https://api.github.com/users/wallyworld/events{/privacy}","received_events_url":"https://api.github.com/users/wallyworld/received_events","type":"User","site_admin":false},"body":"## Description of change\r\n\r\nThere could be an error assigning units to a machine. The error is recorded against the agent, but this prevents juju remove-unit from actually removing the unit, and model cleanup doesn't work - same root cause. This PR fixes the issue. Many tests falsely set up units which had a non allocating status but were not assigned to machines. Such an error is now reported so tests were changed.\r\n\r\n## QA steps\r\n\r\nI hacked the code which assigns a unit to return an error. Without this PR, juju remove-unit would not work. With the PR, juju remove-unit works as expected.\r\n\r\n## Bug reference\r\n\r\nhttps://bugs.launchpad.net/juju/+bug/1643430\r\n","created_at":"2017-02-02T13:45:01Z","updated_at":"2017-02-03T01:19:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"52a8ee118e13699c13cf892334e07dad376282d4","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/juju/juju/pulls/6906/commits","review_comments_url":"https://api.github.com/repos/juju/juju/pulls/6906/comments","review_comment_url":"https://api.github.com/repos/juju/juju/pulls/comments{/number}","comments_url":"https://api.github.com/repos/juju/juju/issues/6906/comments","statuses_url":"https://api.github.com/repos/juju/juju/statuses/ec1e626de9865cd3f6ba9c1aa290e75365264c94","head":{"label":"wallyworld:unassigned-unit-remove","ref":"unassigned-unit-remove","sha":"ec1e626de9865cd3f6ba9c1aa290e75365264c94","user":{"login":"wallyworld","id":5808968,"avatar_url":"https://avatars.githubusercontent.com/u/5808968?v=3","gravatar_id":"","url":"https://api.github.com/users/wallyworld","html_url":"https://github.com/wallyworld","followers_url":"https://api.github.com/users/wallyworld/followers","following_url":"https://api.github.com/users/wallyworld/following{/other_user}","gists_url":"https://api.github.com/users/wallyworld/gists{/gist_id}","starred_url":"https://api.github.com/users/wallyworld/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wallyworld/subscriptions","organizations_url":"https://api.github.com/users/wallyworld/orgs","repos_url":"https://api.github.com/users/wallyworld/repos","events_url":"https://api.github.com/users/wallyworld/events{/privacy}","received_events_url":"https://api.github.com/users/wallyworld/received_events","type":"User","site_admin":false},"repo":{"id":20465218,"name":"juju","full_name":"wallyworld/juju","owner":{"login":"wallyworld","id":5808968,"avatar_url":"https://avatars.githubusercontent.com/u/5808968?v=3","gravatar_id":"","url":"https://api.github.com/users/wallyworld","html_url":"https://github.com/wallyworld","followers_url":"https://api.github.com/users/wallyworld/followers","following_url":"https://api.github.com/users/wallyworld/following{/other_user}","gists_url":"https://api.github.com/users/wallyworld/gists{/gist_id}","starred_url":"https://api.github.com/users/wallyworld/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wallyworld/subscriptions","organizations_url":"https://api.github.com/users/wallyworld/orgs","repos_url":"https://api.github.com/users/wallyworld/repos","events_url":"https://api.github.com/users/wallyworld/events{/privacy}","received_events_url":"https://api.github.com/users/wallyworld/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/wallyworld/juju","description":"juju is devops distilled","fork":true,"url":"https://api.github.com/repos/wallyworld/juju","forks_url":"https://api.github.com/repos/wallyworld/juju/forks","keys_url":"https://api.github.com/repos/wallyworld/juju/keys{/key_id}","collaborators_url":"https://api.github.com/repos/wallyworld/juju/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/wallyworld/juju/teams","hooks_url":"https://api.github.com/repos/wallyworld/juju/hooks","issue_events_url":"https://api.github.com/repos/wallyworld/juju/issues/events{/number}","events_url":"https://api.github.com/repos/wallyworld/juju/events","assignees_url":"https://api.github.com/repos/wallyworld/juju/assignees{/user}","branches_url":"https://api.github.com/repos/wallyworld/juju/branches{/branch}","tags_url":"https://api.github.com/repos/wallyworld/juju/tags","blobs_url":"https://api.github.com/repos/wallyworld/juju/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/wallyworld/juju/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/wallyworld/juju/git/refs{/sha}","trees_url":"https://api.github.com/repos/wallyworld/juju/git/trees{/sha}","statuses_url":"https://api.github.com/repos/wallyworld/juju/statuses/{sha}","languages_url":"https://api.github.com/repos/wallyworld/juju/languages","stargazers_url":"https://api.github.com/repos/wallyworld/juju/stargazers","contributors_url":"https://api.github.com/repos/wallyworld/juju/contributors","subscribers_url":"https://api.github.com/repos/wallyworld/juju/subscribers","subscription_url":"https://api.github.com/repos/wallyworld/juju/subscription","commits_url":"https://api.github.com/repos/wallyworld/juju/commits{/sha}","git_commits_url":"https://api.github.com/repos/wallyworld/juju/git/commits{/sha}","comments_url":"https://api.github.com/repos/wallyworld/juju/comments{/number}","issue_comment_url":"https://api.github.com/repos/wallyworld/juju/issues/comments{/number}","contents_url":"https://api.github.com/repos/wallyworld/juju/contents/{+path}","compare_url":"https://api.github.com/repos/wallyworld/juju/compare/{base}...{head}","merges_url":"https://api.github.com/repos/wallyworld/juju/merges","archive_url":"https://api.github.com/repos/wallyworld/juju/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/wallyworld/juju/downloads","issues_url":"https://api.github.com/repos/wallyworld/juju/issues{/number}","pulls_url":"https://api.github.com/repos/wallyworld/juju/pulls{/number}","milestones_url":"https://api.github.com/repos/wallyworld/juju/milestones{/number}","notifications_url":"https://api.github.com/repos/wallyworld/juju/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/wallyworld/juju/labels{/name}","releases_url":"https://api.github.com/repos/wallyworld/juju/releases{/id}","deployments_url":"https://api.github.com/repos/wallyworld/juju/deployments","created_at":"2014-06-04T00:56:12Z","updated_at":"2016-01-11T16:57:01Z","pushed_at":"2017-02-02T13:35:53Z","git_url":"git://github.com/wallyworld/juju.git","ssh_url":"git@github.com:wallyworld/juju.git","clone_url":"https://github.com/wallyworld/juju.git","svn_url":"https://github.com/wallyworld/juju","homepage":null,"size":100312,"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":"develop"}},"base":{"label":"juju:2.1","ref":"2.1","sha":"64be732874b42953415bf7ed8add6edd8e45cf54","user":{"login":"juju","id":4604548,"avatar_url":"https://avatars.githubusercontent.com/u/4604548?v=3","gravatar_id":"","url":"https://api.github.com/users/juju","html_url":"https://github.com/juju","followers_url":"https://api.github.com/users/juju/followers","following_url":"https://api.github.com/users/juju/following{/other_user}","gists_url":"https://api.github.com/users/juju/gists{/gist_id}","starred_url":"https://api.github.com/users/juju/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juju/subscriptions","organizations_url":"https://api.github.com/users/juju/orgs","repos_url":"https://api.github.com/users/juju/repos","events_url":"https://api.github.com/users/juju/events{/privacy}","received_events_url":"https://api.github.com/users/juju/received_events","type":"Organization","site_admin":false},"repo":{"id":20445947,"name":"juju","full_name":"juju/juju","owner":{"login":"juju","id":4604548,"avatar_url":"https://avatars.githubusercontent.com/u/4604548?v=3","gravatar_id":"","url":"https://api.github.com/users/juju","html_url":"https://github.com/juju","followers_url":"https://api.github.com/users/juju/followers","following_url":"https://api.github.com/users/juju/following{/other_user}","gists_url":"https://api.github.com/users/juju/gists{/gist_id}","starred_url":"https://api.github.com/users/juju/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juju/subscriptions","organizations_url":"https://api.github.com/users/juju/orgs","repos_url":"https://api.github.com/users/juju/repos","events_url":"https://api.github.com/users/juju/events{/privacy}","received_events_url":"https://api.github.com/users/juju/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/juju/juju","description":"juju is devops distilled","fork":false,"url":"https://api.github.com/repos/juju/juju","forks_url":"https://api.github.com/repos/juju/juju/forks","keys_url":"https://api.github.com/repos/juju/juju/keys{/key_id}","collaborators_url":"https://api.github.com/repos/juju/juju/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/juju/juju/teams","hooks_url":"https://api.github.com/repos/juju/juju/hooks","issue_events_url":"https://api.github.com/repos/juju/juju/issues/events{/number}","events_url":"https://api.github.com/repos/juju/juju/events","assignees_url":"https://api.github.com/repos/juju/juju/assignees{/user}","branches_url":"https://api.github.com/repos/juju/juju/branches{/branch}","tags_url":"https://api.github.com/repos/juju/juju/tags","blobs_url":"https://api.github.com/repos/juju/juju/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/juju/juju/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/juju/juju/git/refs{/sha}","trees_url":"https://api.github.com/repos/juju/juju/git/trees{/sha}","statuses_url":"https://api.github.com/repos/juju/juju/statuses/{sha}","languages_url":"https://api.github.com/repos/juju/juju/languages","stargazers_url":"https://api.github.com/repos/juju/juju/stargazers","contributors_url":"https://api.github.com/repos/juju/juju/contributors","subscribers_url":"https://api.github.com/repos/juju/juju/subscribers","subscription_url":"https://api.github.com/repos/juju/juju/subscription","commits_url":"https://api.github.com/repos/juju/juju/commits{/sha}","git_commits_url":"https://api.github.com/repos/juju/juju/git/commits{/sha}","comments_url":"https://api.github.com/repos/juju/juju/comments{/number}","issue_comment_url":"https://api.github.com/repos/juju/juju/issues/comments{/number}","contents_url":"https://api.github.com/repos/juju/juju/contents/{+path}","compare_url":"https://api.github.com/repos/juju/juju/compare/{base}...{head}","merges_url":"https://api.github.com/repos/juju/juju/merges","archive_url":"https://api.github.com/repos/juju/juju/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/juju/juju/downloads","issues_url":"https://api.github.com/repos/juju/juju/issues{/number}","pulls_url":"https://api.github.com/repos/juju/juju/pulls{/number}","milestones_url":"https://api.github.com/repos/juju/juju/milestones{/number}","notifications_url":"https://api.github.com/repos/juju/juju/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/juju/juju/labels{/name}","releases_url":"https://api.github.com/repos/juju/juju/releases{/id}","deployments_url":"https://api.github.com/repos/juju/juju/deployments","created_at":"2014-06-03T14:28:10Z","updated_at":"2017-02-01T21:02:36Z","pushed_at":"2017-02-03T01:09:53Z","git_url":"git://github.com/juju/juju.git","ssh_url":"git@github.com:juju/juju.git","clone_url":"https://github.com/juju/juju.git","svn_url":"https://github.com/juju/juju","homepage":"juju.ubuntu.com","size":100458,"stargazers_count":570,"watchers_count":570,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":180,"mirror_url":null,"open_issues_count":26,"forks":180,"open_issues":26,"watchers":570,"default_branch":"staging"}},"_links":{"self":{"href":"https://api.github.com/repos/juju/juju/pulls/6906"},"html":{"href":"https://github.com/juju/juju/pull/6906"},"issue":{"href":"https://api.github.com/repos/juju/juju/issues/6906"},"comments":{"href":"https://api.github.com/repos/juju/juju/issues/6906/comments"},"review_comments":{"href":"https://api.github.com/repos/juju/juju/pulls/6906/comments"},"review_comment":{"href":"https://api.github.com/repos/juju/juju/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/juju/juju/pulls/6906/commits"},"statuses":{"href":"https://api.github.com/repos/juju/juju/statuses/ec1e626de9865cd3f6ba9c1aa290e75365264c94"}}}}
{"actor":{"display_login":"axw"}}