Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve validation of database creation parameters #1582

Merged
merged 1 commit into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mem3/src/mem3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ choose_shards(DbName, Nodes, Options) ->
if N =:= 0 -> erlang:error(no_nodes_in_zone);
true -> ok
end,
Q = mem3_util:to_integer(couch_util:get_value(q, Options,
Q = mem3_util:q_val(couch_util:get_value(q, Options,
config:get("cluster", "q", "8"))),
%% rotate to a random entry in the nodelist for even distribution
{A, B} = lists:split(crypto:rand_uniform(1,length(Nodes)+1), Nodes),
Expand Down
15 changes: 11 additions & 4 deletions src/mem3/src/mem3_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
-module(mem3_util).

-export([hash/1, name_shard/2, create_partition_map/5, build_shards/2,
n_val/2, to_atom/1, to_integer/1, write_db_doc/1, delete_db_doc/1,
n_val/2, q_val/1, to_atom/1, to_integer/1, write_db_doc/1, delete_db_doc/1,
shard_info/1, ensure_exists/1, open_db_doc/1]).
-export([is_deleted/1, rotate_list/2]).

Expand Down Expand Up @@ -52,15 +52,15 @@ make_name(DbName, [B,E], Suffix) ->
create_partition_map(DbName, N, Q, Nodes) ->
create_partition_map(DbName, N, Q, Nodes, "").

create_partition_map(DbName, N, Q, Nodes, Suffix) ->
create_partition_map(DbName, N, Q, Nodes, Suffix) when Q > 0 ->
UniqueShards = make_key_ranges((?RINGTOP) div Q, 0, []),
Shards0 = lists:flatten([lists:duplicate(N, S) || S <- UniqueShards]),
Shards1 = attach_nodes(Shards0, [], Nodes, []),
[name_shard(S#shard{dbname=DbName}, Suffix) || S <- Shards1].

make_key_ranges(_, CurrentPos, Acc) when CurrentPos >= ?RINGTOP ->
make_key_ranges(I, CurrentPos, Acc) when I > 0, CurrentPos >= ?RINGTOP ->
Acc;
make_key_ranges(Increment, Start, Acc) ->
make_key_ranges(Increment, Start, Acc) when Increment > 0 ->
case Start + 2*Increment of
X when X > ?RINGTOP ->
End = ?RINGTOP - 1;
Expand Down Expand Up @@ -217,6 +217,13 @@ n_val(N, _) when N < 1 ->
n_val(N, _) ->
N.

q_val(Q) when is_list(Q) ->
q_val(list_to_integer(Q));
q_val(Q) when Q > 0 ->
Q;
q_val(_) ->
throw({error, invalid_q_value}).

shard_info(DbName) ->
[{n, mem3:n(DbName)},
{q, length(mem3:shards(DbName)) div mem3:n(DbName)}].
Expand Down