{"action":"created","comment":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/25691100","id":25691100,"diff_hunk":"@@ -1759,27 +1760,64 @@ func (r *Range) AdminMerge(args *proto.AdminMergeRequest, reply *proto.AdminMerg\n \t}\n }\n \n-// InternalChangeReplicas is called after a raft configuration change has committed.\n-func (r *Range) InternalChangeReplicas(batch engine.Engine,\n-\treq *proto.InternalChangeReplicasRequest,\n-\treply *proto.InternalChangeReplicasResponse) {\n-\tr.Lock()\n-\tdefer r.Unlock()\n-\t// Apply the committed membership change to r.Desc.\n-\t// TODO(bdarnell): update the range addressing records too (atomically).\n-\t// Need to either combine this with EndTransaction or queue up a transaction on\n-\t// the meta records.\n-\tr.Desc.Replicas = r.Desc.Replicas[:0]\n-\tfor _, n := range req.Nodes {\n-\t\tnodeID, storeID := decodeRaftNodeID(multiraft.NodeID(n))\n-\t\tr.Desc.Replicas = append(r.Desc.Replicas, proto.Replica{\n-\t\t\tNodeID:  nodeID,\n-\t\t\tStoreID: storeID,\n-\t\t\t// TODO(bdarnell): Set attributes. Copy from gossip or pass through request?\n-\t\t})\n+// ChangeReplicas adds or removes a replica of a range. The change is performed\n+// in a distributed transaction and takes effect when that transaction is committed.\n+// When removing a replica, only the NodeID and StoreID fields of the Replica are used.\n+func (r *Range) ChangeReplicas(changeType proto.ReplicaChangeType, replica proto.Replica) error {\n+\t// Only allow a single change per range at a time.\n+\tif !atomic.CompareAndSwapInt32(&r.metaLock, int32(0), int32(1)) {\n+\t\treturn util.Errorf(\"range %d metadata locked\", r.Desc.RaftID)\n+\t}\n+\tdefer func() { atomic.StoreInt32(&r.metaLock, int32(0)) }()\n+\n+\t// Validate the request and prepare the new descriptor.\n+\tupdatedDesc := *r.Desc\n+\tfound := -1\n+\tfor i, existingRep := range r.Desc.Replicas {\n+\t\tif existingRep.NodeID == replica.NodeID && existingRep.StoreID == replica.StoreID {\n+\t\t\tfound = i\n+\t\t\tbreak\n+\t\t}\n+\t}\n+\tif changeType == proto.ADD_REPLICA {","path":"storage/range.go","position":114,"original_position":114,"commit_id":"e07efc62620bb23a97e2337677008fd1d944e9ed","original_commit_id":"e07efc62620bb23a97e2337677008fd1d944e9ed","user":{"login":"bdarnell","id":160562,"avatar_url":"https://avatars.githubusercontent.com/u/160562?v=3","gravatar_id":"","url":"https://api.github.com/users/bdarnell","html_url":"https://github.com/bdarnell","followers_url":"https://api.github.com/users/bdarnell/followers","following_url":"https://api.github.com/users/bdarnell/following{/other_user}","gists_url":"https://api.github.com/users/bdarnell/gists{/gist_id}","starred_url":"https://api.github.com/users/bdarnell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bdarnell/subscriptions","organizations_url":"https://api.github.com/users/bdarnell/orgs","repos_url":"https://api.github.com/users/bdarnell/repos","events_url":"https://api.github.com/users/bdarnell/events{/privacy}","received_events_url":"https://api.github.com/users/bdarnell/received_events","type":"User","site_admin":false},"body":"Really? Tying configs to replica order seems fragile; I assumed that the zone configs specified attributes of the set as a whole. (my previous commit on this topic reordered Desc.Replicas arbitrarily on any change). This algorithm can work for add-then-remove (assuming we maintain some sort of lock so there are no other changes in between), but it breaks down for remove-then-add, and if we're changing from 3 to 5 replicas or vice versa it gets complicated to maintain the desired ordering without causing unnecessary churn at the raft level (if we add the replica at index 5 before the one at index 4 do we need to create a gap in the list?)","created_at":"2015-03-03T14:34:57Z","updated_at":"2015-03-03T14:34:57Z","html_url":"https://github.com/cockroachdb/cockroach/pull/364#discussion_r25691100","pull_request_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/364","_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/25691100"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/364#discussion_r25691100"},"pull_request":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/364"}}},"pull_request":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/364","id":30338675,"html_url":"https://github.com/cockroachdb/cockroach/pull/364","diff_url":"https://github.com/cockroachdb/cockroach/pull/364.diff","patch_url":"https://github.com/cockroachdb/cockroach/pull/364.patch","issue_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/364","number":364,"state":"open","locked":false,"title":"Replace the InternalChangeReplicas raft command with a distributed txn.","user":{"login":"bdarnell","id":160562,"avatar_url":"https://avatars.githubusercontent.com/u/160562?v=3","gravatar_id":"","url":"https://api.github.com/users/bdarnell","html_url":"https://github.com/bdarnell","followers_url":"https://api.github.com/users/bdarnell/followers","following_url":"https://api.github.com/users/bdarnell/following{/other_user}","gists_url":"https://api.github.com/users/bdarnell/gists{/gist_id}","starred_url":"https://api.github.com/users/bdarnell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bdarnell/subscriptions","organizations_url":"https://api.github.com/users/bdarnell/orgs","repos_url":"https://api.github.com/users/bdarnell/repos","events_url":"https://api.github.com/users/bdarnell/events{/privacy}","received_events_url":"https://api.github.com/users/bdarnell/received_events","type":"User","site_admin":false},"body":"This allows us to update the addressing records as well, but I'm leaving\r\nthat for a subsequent commit.\r\n\r\nmultiraft.ChangeGroupMembership can now smuggle a command to be executed\r\natomically with the membership change; we do this to attach the\r\nmembership change to the EndTransaction call.\r\n\r\nstorage/raft_test.go has been moved to client_test.go and\r\nclient_raft_test.go now that it needs to use distributed transactions.","created_at":"2015-03-03T01:25:06Z","updated_at":"2015-03-03T14:34:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"954bf5a28e48523b134c4cb7a30b9d58d5acb5da","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/364/commits","review_comments_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/364/comments","review_comment_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/364/comments","statuses_url":"https://api.github.com/repos/cockroachdb/cockroach/statuses/e07efc62620bb23a97e2337677008fd1d944e9ed","head":{"label":"bdarnell:rewrite-change-replicas","ref":"rewrite-change-replicas","sha":"e07efc62620bb23a97e2337677008fd1d944e9ed","user":{"login":"bdarnell","id":160562,"avatar_url":"https://avatars.githubusercontent.com/u/160562?v=3","gravatar_id":"","url":"https://api.github.com/users/bdarnell","html_url":"https://github.com/bdarnell","followers_url":"https://api.github.com/users/bdarnell/followers","following_url":"https://api.github.com/users/bdarnell/following{/other_user}","gists_url":"https://api.github.com/users/bdarnell/gists{/gist_id}","starred_url":"https://api.github.com/users/bdarnell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bdarnell/subscriptions","organizations_url":"https://api.github.com/users/bdarnell/orgs","repos_url":"https://api.github.com/users/bdarnell/repos","events_url":"https://api.github.com/users/bdarnell/events{/privacy}","received_events_url":"https://api.github.com/users/bdarnell/received_events","type":"User","site_admin":false},"repo":{"id":23885381,"name":"cockroach","full_name":"bdarnell/cockroach","owner":{"login":"bdarnell","id":160562,"avatar_url":"https://avatars.githubusercontent.com/u/160562?v=3","gravatar_id":"","url":"https://api.github.com/users/bdarnell","html_url":"https://github.com/bdarnell","followers_url":"https://api.github.com/users/bdarnell/followers","following_url":"https://api.github.com/users/bdarnell/following{/other_user}","gists_url":"https://api.github.com/users/bdarnell/gists{/gist_id}","starred_url":"https://api.github.com/users/bdarnell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bdarnell/subscriptions","organizations_url":"https://api.github.com/users/bdarnell/orgs","repos_url":"https://api.github.com/users/bdarnell/repos","events_url":"https://api.github.com/users/bdarnell/events{/privacy}","received_events_url":"https://api.github.com/users/bdarnell/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bdarnell/cockroach","description":"A Scalable, Geo-Replicated, Transactional Datastore","fork":true,"url":"https://api.github.com/repos/bdarnell/cockroach","forks_url":"https://api.github.com/repos/bdarnell/cockroach/forks","keys_url":"https://api.github.com/repos/bdarnell/cockroach/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bdarnell/cockroach/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bdarnell/cockroach/teams","hooks_url":"https://api.github.com/repos/bdarnell/cockroach/hooks","issue_events_url":"https://api.github.com/repos/bdarnell/cockroach/issues/events{/number}","events_url":"https://api.github.com/repos/bdarnell/cockroach/events","assignees_url":"https://api.github.com/repos/bdarnell/cockroach/assignees{/user}","branches_url":"https://api.github.com/repos/bdarnell/cockroach/branches{/branch}","tags_url":"https://api.github.com/repos/bdarnell/cockroach/tags","blobs_url":"https://api.github.com/repos/bdarnell/cockroach/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bdarnell/cockroach/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bdarnell/cockroach/git/refs{/sha}","trees_url":"https://api.github.com/repos/bdarnell/cockroach/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bdarnell/cockroach/statuses/{sha}","languages_url":"https://api.github.com/repos/bdarnell/cockroach/languages","stargazers_url":"https://api.github.com/repos/bdarnell/cockroach/stargazers","contributors_url":"https://api.github.com/repos/bdarnell/cockroach/contributors","subscribers_url":"https://api.github.com/repos/bdarnell/cockroach/subscribers","subscription_url":"https://api.github.com/repos/bdarnell/cockroach/subscription","commits_url":"https://api.github.com/repos/bdarnell/cockroach/commits{/sha}","git_commits_url":"https://api.github.com/repos/bdarnell/cockroach/git/commits{/sha}","comments_url":"https://api.github.com/repos/bdarnell/cockroach/comments{/number}","issue_comment_url":"https://api.github.com/repos/bdarnell/cockroach/issues/comments{/number}","contents_url":"https://api.github.com/repos/bdarnell/cockroach/contents/{+path}","compare_url":"https://api.github.com/repos/bdarnell/cockroach/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bdarnell/cockroach/merges","archive_url":"https://api.github.com/repos/bdarnell/cockroach/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bdarnell/cockroach/downloads","issues_url":"https://api.github.com/repos/bdarnell/cockroach/issues{/number}","pulls_url":"https://api.github.com/repos/bdarnell/cockroach/pulls{/number}","milestones_url":"https://api.github.com/repos/bdarnell/cockroach/milestones{/number}","notifications_url":"https://api.github.com/repos/bdarnell/cockroach/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bdarnell/cockroach/labels{/name}","releases_url":"https://api.github.com/repos/bdarnell/cockroach/releases{/id}","created_at":"2014-09-10T17:55:56Z","updated_at":"2014-09-10T07:15:42Z","pushed_at":"2015-03-03T01:24:48Z","git_url":"git://github.com/bdarnell/cockroach.git","ssh_url":"git@github.com:bdarnell/cockroach.git","clone_url":"https://github.com/bdarnell/cockroach.git","svn_url":"https://github.com/bdarnell/cockroach","homepage":null,"size":9947,"stargazers_count":0,"watchers_count":0,"language":null,"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":"cockroachdb:master","ref":"master","sha":"2ba0d21de876906fb47690c4082d26c727517ca2","user":{"login":"cockroachdb","id":6748139,"avatar_url":"https://avatars.githubusercontent.com/u/6748139?v=3","gravatar_id":"","url":"https://api.github.com/users/cockroachdb","html_url":"https://github.com/cockroachdb","followers_url":"https://api.github.com/users/cockroachdb/followers","following_url":"https://api.github.com/users/cockroachdb/following{/other_user}","gists_url":"https://api.github.com/users/cockroachdb/gists{/gist_id}","starred_url":"https://api.github.com/users/cockroachdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cockroachdb/subscriptions","organizations_url":"https://api.github.com/users/cockroachdb/orgs","repos_url":"https://api.github.com/users/cockroachdb/repos","events_url":"https://api.github.com/users/cockroachdb/events{/privacy}","received_events_url":"https://api.github.com/users/cockroachdb/received_events","type":"Organization","site_admin":false},"repo":{"id":16563587,"name":"cockroach","full_name":"cockroachdb/cockroach","owner":{"login":"cockroachdb","id":6748139,"avatar_url":"https://avatars.githubusercontent.com/u/6748139?v=3","gravatar_id":"","url":"https://api.github.com/users/cockroachdb","html_url":"https://github.com/cockroachdb","followers_url":"https://api.github.com/users/cockroachdb/followers","following_url":"https://api.github.com/users/cockroachdb/following{/other_user}","gists_url":"https://api.github.com/users/cockroachdb/gists{/gist_id}","starred_url":"https://api.github.com/users/cockroachdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cockroachdb/subscriptions","organizations_url":"https://api.github.com/users/cockroachdb/orgs","repos_url":"https://api.github.com/users/cockroachdb/repos","events_url":"https://api.github.com/users/cockroachdb/events{/privacy}","received_events_url":"https://api.github.com/users/cockroachdb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cockroachdb/cockroach","description":"A Scalable, Geo-Replicated, Transactional Datastore","fork":false,"url":"https://api.github.com/repos/cockroachdb/cockroach","forks_url":"https://api.github.com/repos/cockroachdb/cockroach/forks","keys_url":"https://api.github.com/repos/cockroachdb/cockroach/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cockroachdb/cockroach/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cockroachdb/cockroach/teams","hooks_url":"https://api.github.com/repos/cockroachdb/cockroach/hooks","issue_events_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/events{/number}","events_url":"https://api.github.com/repos/cockroachdb/cockroach/events","assignees_url":"https://api.github.com/repos/cockroachdb/cockroach/assignees{/user}","branches_url":"https://api.github.com/repos/cockroachdb/cockroach/branches{/branch}","tags_url":"https://api.github.com/repos/cockroachdb/cockroach/tags","blobs_url":"https://api.github.com/repos/cockroachdb/cockroach/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cockroachdb/cockroach/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cockroachdb/cockroach/git/refs{/sha}","trees_url":"https://api.github.com/repos/cockroachdb/cockroach/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cockroachdb/cockroach/statuses/{sha}","languages_url":"https://api.github.com/repos/cockroachdb/cockroach/languages","stargazers_url":"https://api.github.com/repos/cockroachdb/cockroach/stargazers","contributors_url":"https://api.github.com/repos/cockroachdb/cockroach/contributors","subscribers_url":"https://api.github.com/repos/cockroachdb/cockroach/subscribers","subscription_url":"https://api.github.com/repos/cockroachdb/cockroach/subscription","commits_url":"https://api.github.com/repos/cockroachdb/cockroach/commits{/sha}","git_commits_url":"https://api.github.com/repos/cockroachdb/cockroach/git/commits{/sha}","comments_url":"https://api.github.com/repos/cockroachdb/cockroach/comments{/number}","issue_comment_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/comments{/number}","contents_url":"https://api.github.com/repos/cockroachdb/cockroach/contents/{+path}","compare_url":"https://api.github.com/repos/cockroachdb/cockroach/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cockroachdb/cockroach/merges","archive_url":"https://api.github.com/repos/cockroachdb/cockroach/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cockroachdb/cockroach/downloads","issues_url":"https://api.github.com/repos/cockroachdb/cockroach/issues{/number}","pulls_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls{/number}","milestones_url":"https://api.github.com/repos/cockroachdb/cockroach/milestones{/number}","notifications_url":"https://api.github.com/repos/cockroachdb/cockroach/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cockroachdb/cockroach/labels{/name}","releases_url":"https://api.github.com/repos/cockroachdb/cockroach/releases{/id}","created_at":"2014-02-06T00:18:47Z","updated_at":"2015-03-03T11:07:43Z","pushed_at":"2015-03-02T22:21:19Z","git_url":"git://github.com/cockroachdb/cockroach.git","ssh_url":"git@github.com:cockroachdb/cockroach.git","clone_url":"https://github.com/cockroachdb/cockroach.git","svn_url":"https://github.com/cockroachdb/cockroach","homepage":null,"size":15295,"stargazers_count":2980,"watchers_count":2980,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":208,"mirror_url":null,"open_issues_count":26,"forks":208,"open_issues":26,"watchers":2980,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/364"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/364"},"issue":{"href":"https://api.github.com/repos/cockroachdb/cockroach/issues/364"},"comments":{"href":"https://api.github.com/repos/cockroachdb/cockroach/issues/364/comments"},"review_comments":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/364/comments"},"review_comment":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/364/commits"},"statuses":{"href":"https://api.github.com/repos/cockroachdb/cockroach/statuses/e07efc62620bb23a97e2337677008fd1d944e9ed"}}}}