Skip to content
This repository has been archived by the owner on Sep 19, 2019. It is now read-only.

Commit

Permalink
Merge pull request #42 from cloudant/13470-make-get_dbs-zone-aware_ma…
Browse files Browse the repository at this point in the history
…ster

Merge pull request #39 from cloudant/13470-make-get_dbs-zone-aware

BugzID: 13470
  • Loading branch information
kocolosk committed May 1, 2012
2 parents de4b7da + f3e5dec commit 6670f05
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/fabric_util.erl
Expand Up @@ -54,14 +54,14 @@ get_db(DbName) ->
get_db(DbName, []).

get_db(DbName, Options) ->
% prefer local shards
{Local, Remote} = lists:partition(fun(S) -> S#shard.node =:= node() end,
mem3:shards(DbName)),
{Local, SameZone, DifferentZone} = mem3:group_by_proximity(mem3:shards(DbName)),
% Prefer shards on the same node over other nodes, prefer shards in the same zone over
% over zones and sort each remote list by name so that we don't repeatedly try the same node.
Shards = Local ++ lists:keysort(#shard.name, SameZone) ++ lists:keysort(#shard.name, DifferentZone),
% suppress shards from down nodes
Nodes = erlang:nodes(),
Live = [S || #shard{node = N} = S <- Remote, lists:member(N, Nodes)],
% sort the live remote shards so that we don't repeatedly try the same node
get_shard(Local ++ lists:keysort(#shard.name, Live), Options, 100).
Nodes = [node()|erlang:nodes()],
Live = [S || #shard{node = N} = S <- Shards, lists:member(N, Nodes)],
get_shard(Live, Options, 100).

get_shard([], _Opts, _Timeout) ->
erlang:error({internal_server_error, "No DB shards could be opened."});
Expand Down

0 comments on commit 6670f05

Please sign in to comment.