summaryrefslogtreecommitdiff
path: root/src/cluster_ops.erl
blob: 72bba92f88086ffb11758a0b67e53989a5ee9abb (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
%%%-------------------------------------------------------------------
%%% File:      cluster_ops.erl
%%% @author    Brad Anderson <brad@cloudant.com> [http://cloudant.com]
%%% @copyright 2009 Brad Anderson
%%% @doc
%%%
%%% @end
%%%
%%% @since 2009-07-21 by Brad Anderson
%%%-------------------------------------------------------------------
-module(cluster_ops).
-author('brad@cloudant.com').

%% API
-export([key_lookup/3, key_lookup/5,
  all_parts/4,
  some_parts/4, some_parts/5,
  quorum_from_each_part/3]).

-include("../include/common.hrl").
-include("../include/config.hrl").

-include("../include/profile.hrl").


%%====================================================================
%% API
%%====================================================================

%% @doc Get to the proper shard on N nodes by key lookup
%%
%%      This fun uses quorum constants from config
key_lookup(Key, {M,F,A}, Access) ->
    N = list_to_integer(couch_config:get("cluster", "n", "3")),
    key_lookup(Key, {M,F,A}, Access, get_const(Access), N).


%% @doc Get to the proper shard on N nodes by key lookup
%%
%%      This fun uses a provided quorum constant, possibly from request,
%%      possibly from config
key_lookup(Key, {M,F,A}, Access, Const, N) ->
    NodeParts = membership2:nodeparts_for_key(Key),
    {ResolveFun, NotFoundFun} = case Access of
    r -> {fun resolve_read/1, fun resolve_not_found/2};
    w -> {fun resolve_write/1, fun(_,_) -> {false, notused, []} end}
    end,
    MapFun = fun({Node,Part}) ->
        try
            rpc:call(Node, M, F, [[Part | A]])
        catch Class:Exception ->
            {error, Class, Exception}
        end
    end,
    {GoodReplies, Bad} = pcall(MapFun, NodeParts, N),
    if length(Bad) > 0 -> ?LOG_DEBUG("~nBad: ~p~n", [Bad]); true -> ok end,
    Good = lists:map(fun strip_ok/1, GoodReplies),
    final_key_lookup(Good, Bad, N, Const, ResolveFun, NotFoundFun, Access).


%% @doc Do op on all shards (and maybe even replication partners)
all_parts({M,F,A}, Access, AndPartners, ResolveFun) ->
    NodePartList = membership2:all_nodes_parts(AndPartners),
    MapFun = fun({Node, Part}) ->
        try
            rpc:call(Node, M, F, [[Part | A]])
        catch Class:Exception ->
            {error, Class, Exception}
        end
    end,
    Replies = ?PMAP(MapFun, NodePartList),
    {Good, Bad} = lists:partition(fun valid/1, Replies),
    final_all_parts(Good, Bad, length(NodePartList), ResolveFun, Access).


%% @doc Do op on some shards, depending on list of keys sent in.
%%
%%      This fun uses quorum constants from config
some_parts(KeyFun, SeqsKVPairs, {M,F,A}, Access) ->
    some_parts(KeyFun, SeqsKVPairs, {M,F,A}, Access, get_const(Access)).


%% @doc Do op on some shards, depending on list of keys sent in.
%%
%%      This fun uses a provided quorum constant, possibly from request,
%%      possibly from config
some_parts(KeyFun, SeqsKVPairs, {M,F,A}, _Access, Const) ->
    TaskFun = fun({{Node,Part}, Values}) ->
        try
            rpc:call(Node, M, F, [[Part | [Values | A]]])
        catch Class:Exception ->
            {error, Class, Exception}
        end
    end,

    % get tasks per node that are part / values for that partition
    DistTasks = get_dist_tasks(KeyFun, SeqsKVPairs),

    % With the distributed tasklist in hand, do the tasks per partition.
    % For each partition, do the work on all nodes/parts.
    TaskReplies = ?PMAP(TaskFun, DistTasks),
    {GoodReplies, Bad} = lists:partition(fun valid/1, TaskReplies),
    if length(Bad) > 0 -> ?LOG_DEBUG("~nBad: ~p~n", [Bad]); true -> ok end,
    Good = lists:map(fun strip_ok/1, GoodReplies),
    final_some_parts(Good, Bad, Const).


quorum_from_each_part({M,F,A}, Access, ResolveFun) ->
    Const = get_const(Access),
    {_, Parts} = lists:unzip(membership2:partitions()),
    PartsMapFun = fun(Part) ->
        Nodes = membership2:nodes_for_part(Part),
        NodesMapFun = fun(Node) -> rpc:call(Node, M, F, [[Part | A]]) end,
        {GoodReplies,BadReplies} = pcall(NodesMapFun, Nodes, Const),
        Good1 = lists:map(fun strip_ok/1, GoodReplies),
        Bad1 = case length(Good1) >= Const of
        true -> [];
        false -> BadReplies
        end,
        {Good1,Bad1}
    end,
    Results1 = ?PMAP(PartsMapFun, Parts),
    {Good,Bad} = lists:foldl(fun({G,B}, {GAcc,BAcc}) ->
        {lists:append(G,GAcc),lists:append(B,BAcc)}
    end, {[],[]}, Results1),
    if length(Bad) > 0 -> ?LOG_DEBUG("~nBad: ~p~n", [Bad]); true -> ok end,
    final_quorum_from_each_part(Good, Bad, length(Parts), ResolveFun, Access).


%%--------------------------------------------------------------------
%% Internal functions
%%--------------------------------------------------------------------

final_key_lookup(Good, Bad, N, Const, ResolveFun, NotFoundFun, Access) ->
  {NotFound, Return, Reasons} = NotFoundFun(Bad, Const),
  if
    length(Good) >= Const -> {ok, ResolveFun(Good)};
    NotFound -> {ok, Return, Reasons};
    true -> error_message(Good, Bad, N, Const, Access)
  end.


final_all_parts(Good, Bad, Total, ResolveFun, Access) ->
  case length(Good) =:= Total of
    true -> {ok, ResolveFun(Good)};
    _ -> error_message(Good, Bad, Total, Total, Access)
  end.


final_some_parts(Good, _Bad, Const) ->
  Good1 = lists:flatten(Good),
  {Seqs, _} = lists:unzip(Good1),
  {ResG,ResB} =
    lists:foldl(
      fun(Seq, {AccG,AccB}) ->
          Vals = proplists:get_all_values(Seq, Good1),
          case length(Vals) >= Const of
            true -> {[{Seq, Vals}|AccG],AccB};
            _ -> {AccG, [{Seq, Vals}|AccB]}
          end
      end, {[],[]}, lists:usort(Seqs)),
  case length(ResB) of
    0 -> {ok, ResG};
    _ -> {error, ResB}
  end.


final_quorum_from_each_part(Good, Bad, Total, ResolveFun, Access) ->
    case length(Good) =:= Total of
    true -> {ok, ResolveFun(Good)};
    _ -> error_message(Good, Bad, Total, Total, Access)
    end.


resolve_read([First|Responses]) ->
  case First of
    not_found -> not_found;
    _ -> lists:foldr(fun vector_clock:resolve/2, First, Responses)
  end.


resolve_write([First|Responses]) ->
  case First of
    not_found -> not_found;
    _ -> lists:foldr(fun vector_clock:resolve/2, First, Responses)
  end.


resolve_not_found(Bad, R) ->
  {NotFoundCnt, DeletedCnt, OtherReasons} =
    lists:foldl(fun({Error,Reason}, {NotFoundAcc, DeletedAcc, ReasonAcc}) ->
      case {Error,Reason} of
        {not_found, {_Clock, [missing|_Rest]}} ->
          {NotFoundAcc+1, DeletedAcc, ReasonAcc};
        {not_found, {_Clock, [deleted|_Rest]}} ->
          {NotFoundAcc, DeletedAcc+1, ReasonAcc};
        _ ->
          {NotFoundAcc, DeletedAcc, [Reason|ReasonAcc]}
      end
    end, {0, 0, []}, Bad),
  % TODO: is the comparison to R good here, or should it be N-R?
  if
    NotFoundCnt >= R -> {true, {not_found, missing}, OtherReasons};
    DeletedCnt >= R -> {true, {not_found, deleted}, OtherReasons};
    true -> {false, other, OtherReasons}
  end.


error_message(Good, Bad, N, T, Access) ->
  Msg = list_to_atom(lists:concat([atom_to_list(Access), "_quorum_not_met"])),
  ?LOG_ERROR("~p~nSuccess on ~p of ~p servers. Needed ~p. Errors: ~w"
             , [Msg, length(Good), N, T, Bad]),
  [{error, Msg}, {good, Good}, {bad, Bad}].


pcall(MapFun, Servers, Const) ->
  Replies = lib_misc:pmap(MapFun, Servers, Const),
  lists:partition(fun valid/1, Replies).


valid({ok, _}) -> true;
valid(ok) -> true;
valid(_) -> false.


strip_ok({ok, Val}) -> Val;
strip_ok(Val) -> Val.


%% @spec get_dist_tasks(KeyFun::function(), KVPairs::list()) ->
%%           [{{Node::node(), Part::integer()}, SeqVals}]
%%       Type     - ordered | ??
%%       SeqVals   - [{Seq, Val}]
%% @doc builds a distributed task list of nodes with a list of shard/values.
%%      This looks like a dict structure
%%      but is a list so we can use ?PMAP with the results
%% @end
get_dist_tasks(KeyFun, SeqsKVPairs) ->
    NPSV = lists:flatmap(fun({_,KVPair} = Elem) ->
        [{NP, Elem} || NP <- membership2:nodeparts_for_key(KeyFun(KVPair))]
    end, SeqsKVPairs),
    group_by_key(NPSV).

group_by_key([]) ->
    [];
group_by_key(List) ->
    [{FirstK,FirstV} | Rest] = lists:keysort(1,List),
    Acc0 = {FirstK, [FirstV], []},
    FoldFun = fun({K,V}, {K,Vs,Acc}) ->
        {K, [V|Vs], Acc};
    ({NewKey,V}, {OldKey,Vs,Acc}) ->
        {NewKey, [V], [{OldKey,Vs}|Acc]}
    end,
    {LastK, LastVs, Acc} = lists:foldl(FoldFun, Acc0, Rest),
    [{LastK, LastVs} | Acc].

get_const(r) ->
    list_to_integer(couch_config:get("cluster", "r", "2"));
get_const(w) ->
    list_to_integer(couch_config:get("cluster", "w", "2"));
get_const(r1) ->
    1;
get_const(Other) ->
    throw({bad_access_term, Other}).