anchor.diff:                    _:_ -> []
                 end
         end, Nodes).
  -
  -actions(Children) ->
  -    [{VBucket, Dst} || #child_id{vbuckets=VBuckets, dest_node=Dst} <- Children,
  -                       VBucket <- VBuckets].
  -
  -kill_vbuckets(Node, Bucket, VBuckets) ->
  -    {ok, States} = ns_memcached:list_vbuckets(Node, Bucket),
  -    case [X || X = {V, S} <- States, lists:member(V, VBuckets), S /= replica] of
  -        [] ->
  -            ok;
  -        RemainingVBuckets ->
  -            lists:foreach(fun ({V, dead}) ->
  -                                  ns_memcached:delete_vbucket(Node, Bucket, V);
  -                              ({V, _}) ->
  -                                  ns_memcached:set_vbucket(Node, Bucket,
  -                                                           V, dead),
  -                                  ns_memcached:delete_vbucket(Node, Bucket, V)
  -                              end, RemainingVBuckets)
  -    end.
  -
   
   set_replicas(Bucket, NodesReplicas) ->
       %% Replace with the empty list if replication is disabled
  
positive.code:                           kill_child(Node, Bucket, Child)
                    end, Children).
  
  -spec kill_runaway_children(node(), nonempty_string(),
                              [{non_neg_integer(), node()}]) ->
                                     [#child_id{}].
  kill_runaway_children(Node, Bucket, Replicas) ->
      %% Kill any children not in Replicas
      {GoodChildren, Runaways} =
          lists:partition(
            fun (#child_id{vbuckets=VBuckets, dest_node=DstNode}) ->
                    NodeReplicas = [{V, DstNode} || V <- VBuckets],
                    lists:all(fun (NR) -> lists:member(NR, Replicas) end,
                              NodeReplicas)
            end, children(Node, Bucket)),
      lists:foreach(
        fun (Runaway) ->
                ?log_info(
                  "Killing replicator ~p on node ~p",
                   [Runaway, Node]),
                kill_child(Node, Bucket, Runaway)
        end, Runaways),
      GoodChildren.
  
  
  %% supervisor callbacks
  init([]) ->
      {ok, {{one_for_one,
  
negative.code: 
  
  %% @doc Set up replication from the given source node to a list of
  %% {VBucket, DstNode} pairs.
  set_replicas(SrcNode, Bucket, Replicas) ->
      %% A dictionary mapping destination node to a sorted list of
      %% vbuckets to replicate there from SrcNode.
      DesiredReplicaDict =
          dict:map(
            fun (_, V) -> lists:usort(V) end,
            lists:foldl(
              fun ({VBucket, DstNode}, D) ->
                      dict:append(DstNode, VBucket, D)
              end, dict:new(), Replicas)),
      %% Remove any destination nodes from the dictionary that the
      %% replication is already correct for.
      NeededReplicas =
          dict:to_list(
            lists:foldl(
              fun (#child_id{dest_node=DstNode, vbuckets=VBuckets} = Id, D) ->
                      case dict:find(DstNode, DesiredReplicaDict) of
                          {ok, VBuckets} ->
                              %% Already running
                              dict:erase(DstNode, D);
                          _ ->
                              %% Either wrong vbuckets or not wanted at all
                              kill_child(SrcNode, Bucket, Id),
                              D
                      end
              end, DesiredReplicaDict, children(SrcNode, Bucket))),
      lists:foreach(
        fun ({DstNode, VBuckets}) ->
                start_child(SrcNode, Bucket, VBuckets, DstNode)
        end, NeededReplicas).
  
  
  -spec start_child(atom(), nonempty_string(), [non_neg_integer(),...], atom()) ->
                           {ok, pid()}.
  start_child(Node, Bucket, VBuckets, DstNode) ->
      Args = args(Node, Bucket, VBuckets, DstNode, false),
      ?log_info("Starting replicator with args =~n~p",
                [Args]),
      ChildSpec = {#child_id{vbuckets=VBuckets, dest_node=DstNode},
                   {ebucketmigrator_srv, start_link, Args},
                   permanent, 10, worker, [ebucketmigrator_srv]},
      supervisor:start_child({server(Bucket), Node}, ChildSpec).
  