This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mem3.erl
204 lines (175 loc) · 7.19 KB
/
mem3.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
% Copyright 2010 Cloudant
%
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.
-module(mem3).
-export([start/0, stop/0, restart/0, nodes/0, node_info/2, shards/1, shards/2,
choose_shards/2, n/1, dbname/1, ushards/1]).
-export([get_shard/3, local_shards/1, fold_shards/2]).
-export([sync_security/0, sync_security/1]).
-export([compare_nodelists/0, compare_shards/1]).
-export([group_by_proximity/1]).
-include("mem3.hrl").
start() ->
application:start(mem3).
stop() ->
application:stop(mem3).
restart() ->
stop(),
start().
%% @doc Detailed report of cluster-wide membership state. Queries the state
%% on all member nodes and builds a dictionary with unique states as the
%% key and the nodes holding that state as the value. Also reports member
%% nodes which fail to respond and nodes which are connected but are not
%% cluster members. Useful for debugging.
-spec compare_nodelists() -> [{{cluster_nodes, [node()]} | bad_nodes
| non_member_nodes, [node()]}].
compare_nodelists() ->
Nodes = mem3:nodes(),
AllNodes = erlang:nodes([this, visible]),
{Replies, BadNodes} = gen_server:multi_call(Nodes, mem3_nodes, get_nodelist),
Dict = lists:foldl(fun({Node, Nodelist}, D) ->
orddict:append({cluster_nodes, Nodelist}, Node, D)
end, orddict:new(), Replies),
[{non_member_nodes, AllNodes -- Nodes}, {bad_nodes, BadNodes} | Dict].
-spec compare_shards(DbName::iodata()) -> [{bad_nodes | [#shard{}], [node()]}].
compare_shards(DbName) when is_list(DbName) ->
compare_shards(list_to_binary(DbName));
compare_shards(DbName) ->
Nodes = mem3:nodes(),
{Replies, BadNodes} = rpc:multicall(mem3, shards, [DbName]),
GoodNodes = [N || N <- Nodes, not lists:member(N, BadNodes)],
Dict = lists:foldl(fun({Shards, Node}, D) ->
orddict:append(Shards, Node, D)
end, orddict:new(), lists:zip(Replies, GoodNodes)),
[{bad_nodes, BadNodes} | Dict].
-spec n(DbName::iodata()) -> integer().
n(DbName) ->
length(mem3:shards(DbName, <<"foo">>)).
-spec nodes() -> [node()].
nodes() ->
mem3_nodes:get_nodelist().
node_info(Node, Key) ->
mem3_nodes:get_node_info(Node, Key).
-spec shards(DbName::iodata()) -> [#shard{}].
shards(DbName) when is_list(DbName) ->
shards(list_to_binary(DbName));
shards(DbName) ->
ShardDbName =
list_to_binary(couch_config:get("mem3", "shard_db", "dbs")),
case DbName of
ShardDbName ->
%% shard_db is treated as a single sharded db to support calls to db_info
%% and view_all_docs
[#shard{
node = node(),
name = ShardDbName,
dbname = ShardDbName,
range = [0, 2 bsl 31]}];
_ ->
mem3_shards:for_db(DbName)
end.
-spec shards(DbName::iodata(), DocId::binary()) -> [#shard{}].
shards(DbName, DocId) when is_list(DbName) ->
shards(list_to_binary(DbName), DocId);
shards(DbName, DocId) when is_list(DocId) ->
shards(DbName, list_to_binary(DocId));
shards(DbName, DocId) ->
mem3_shards:for_docid(DbName, DocId).
ushards(DbName) ->
{L,S,D} = group_by_proximity(live_shards(DbName)),
% Prefer shards in the local zone over shards in a different zone,
% but sort each group separately to ensure a consistent choice between
% nodes in the same zone.
Shards = lists:sort(L ++ S) ++ lists:sort(D),
lists:usort(fun(#shard{name=A}, #shard{name=B}) ->
A =< B
end, Shards).
live_shards(DbName) ->
Nodes = [node()|erlang:nodes()],
[S || #shard{node=Node} = S <- shards(DbName), lists:member(Node, Nodes)].
group_by_proximity(Shards) ->
{Local, Remote} = lists:partition(fun(S) -> S#shard.node =:= node() end,
Shards),
LocalZone = mem3:node_info(node(), <<"zone">>),
Fun = fun(S) -> mem3:node_info(S#shard.node, <<"zone">>) =:= LocalZone end,
{SameZone, DifferentZone} = lists:partition(Fun, Remote),
{Local, SameZone, DifferentZone}.
get_shard(DbName, Node, Range) ->
mem3_shards:get(DbName, Node, Range).
local_shards(DbName) ->
mem3_shards:local(DbName).
fold_shards(Fun, Acc) ->
mem3_shards:fold(Fun, Acc).
sync_security() ->
mem3_sync_security:go().
sync_security(Db) ->
mem3_sync_security:go(dbname(Db)).
-spec choose_shards(DbName::iodata(), Options::list()) -> [#shard{}].
choose_shards(DbName, Options) when is_list(DbName) ->
choose_shards(list_to_binary(DbName), Options);
choose_shards(DbName, Options) ->
try shards(DbName)
catch error:E when E==database_does_not_exist; E==badarg ->
Nodes = mem3:nodes(),
NodeCount = length(Nodes),
Zones = zones(Nodes),
ZoneCount = length(Zones),
if ZoneCount =:= 0 -> erlang:error(no_available_zones); true -> ok end,
N = mem3_util:n_val(couch_util:get_value(n, Options), NodeCount),
Q = mem3_util:to_integer(couch_util:get_value(q, Options,
couch_config:get("cluster", "q", "8"))),
Z = mem3_util:z_val(couch_util:get_value(z, Options), NodeCount, ZoneCount),
Suffix = couch_util:get_value(shard_suffix, Options, ""),
ChosenZones = lists:sublist(shuffle(Zones), Z),
lists:flatmap(
fun({Zone, N1}) ->
Nodes1 = nodes_in_zone(Nodes, Zone),
{A, B} = lists:split(crypto:rand_uniform(1,length(Nodes1)+1), Nodes1),
RotatedNodes = B ++ A,
mem3_util:create_partition_map(DbName, erlang:min(N1,length(Nodes1)),
Q, RotatedNodes, Suffix)
end,
lists:zip(ChosenZones, apportion(N, Z)))
end.
-spec dbname(#shard{} | iodata()) -> binary().
dbname(#shard{dbname = DbName}) ->
DbName;
dbname(<<"shards/", _:8/binary, "-", _:8/binary, "/", DbName/binary>>) ->
list_to_binary(filename:rootname(binary_to_list(DbName)));
dbname(DbName) when is_list(DbName) ->
dbname(list_to_binary(DbName));
dbname(DbName) when is_binary(DbName) ->
DbName;
dbname(_) ->
erlang:error(badarg).
zones(Nodes) ->
BlacklistStr = couch_config:get("mem3", "blacklisted_zones", "[]"),
{ok, Blacklist0} = couch_util:parse_term(BlacklistStr),
Blacklist = [list_to_binary(Z) || Z <- Blacklist0],
lists:usort([mem3:node_info(Node, <<"zone">>) || Node <- Nodes]) --
Blacklist.
nodes_in_zone(Nodes, Zone) ->
[Node || Node <- Nodes, Zone == mem3:node_info(Node, <<"zone">>)].
shuffle(List) ->
List1 = [{crypto:rand_uniform(1, 1000), Item} || Item <- List],
List2 = lists:sort(List1),
{_, Result} = lists:unzip(List2),
Result.
apportion(Shares, Ways) ->
apportion(Shares, lists:duplicate(Ways, 0), Shares).
apportion(_Shares, Acc, 0) ->
Acc;
apportion(Shares, Acc, Remaining) ->
N = Remaining rem length(Acc),
[H|T] = lists:nthtail(N, Acc),
apportion(Shares, lists:sublist(Acc, N) ++ [H+1|T], Remaining - 1).