Skip to content

Commit

Permalink
Remove compat couch_rand module
Browse files Browse the repository at this point in the history
It's not needed any longer as `rand` is in all the currently supported Erlang
releases.
  • Loading branch information
nickva committed Aug 19, 2024
1 parent b85d4bb commit aca60dd
Show file tree
Hide file tree
Showing 16 changed files with 22 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/couch/src/couch_debug.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ random_processes(Acc, Depth) ->
end.

oneof(Options) ->
lists:nth(couch_rand:uniform(length(Options)), Options).
lists:nth(rand:uniform(length(Options)), Options).

tree() ->
[InitialPid | _] = Processes = random_processes(5),
Expand Down
2 changes: 1 addition & 1 deletion src/couch/src/couch_multidb_changes.erl
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ notify_fold(DbName, {Server, DbSuffix, Count}) ->
% number of shards back to back during startup.
jitter(N) ->
Range = min(2 * N * ?AVG_DELAY_MSEC, ?MAX_DELAY_MSEC),
couch_rand:uniform(Range).
rand:uniform(Range).

scan_local_db(Server, DbSuffix) when is_pid(Server) ->
case couch_server:exists(DbSuffix) of
Expand Down
24 changes: 0 additions & 24 deletions src/couch/src/couch_rand.erl

This file was deleted.

2 changes: 1 addition & 1 deletion src/couch/src/couch_uuids.erl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ new_prefix() ->
couch_util:to_hex((crypto:strong_rand_bytes(13))).

inc() ->
couch_rand:uniform(16#ffd).
rand:uniform(16#ffd).

state() ->
AlgoStr = config:get("uuids", "algorithm", "sequential"),
Expand Down
2 changes: 1 addition & 1 deletion src/couch/src/test_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ random_rev() ->
couch_util:to_hex_bin(crypto:strong_rand_bytes(16)).

shuffle(List) ->
Paired = [{couch_rand:uniform(), I} || I <- List],
Paired = [{rand:uniform(), I} || I <- List],
Sorted = lists:sort(Paired),
[I || {_, I} <- Sorted].

Expand Down
8 changes: 4 additions & 4 deletions src/couch/test/eunit/couch_btree_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ btree_open_test_() ->

sorted_kvs_test_() ->
Funs = kvs_test_funs(),
Sorted = [{Seq, couch_rand:uniform()} || Seq <- lists:seq(1, ?ROWS)],
Sorted = [{Seq, rand:uniform()} || Seq <- lists:seq(1, ?ROWS)],
{
"BTree with sorted keys",
{
Expand All @@ -103,7 +103,7 @@ sorted_kvs_test_() ->
}.

rsorted_kvs_test_() ->
Sorted = [{Seq, couch_rand:uniform()} || Seq <- lists:seq(1, ?ROWS)],
Sorted = [{Seq, rand:uniform()} || Seq <- lists:seq(1, ?ROWS)],
Funs = kvs_test_funs(),
Reversed = Sorted,
{
Expand All @@ -123,7 +123,7 @@ rsorted_kvs_test_() ->

shuffled_kvs_test_() ->
Funs = kvs_test_funs(),
Sorted = [{Seq, couch_rand:uniform()} || Seq <- lists:seq(1, ?ROWS)],
Sorted = [{Seq, rand:uniform()} || Seq <- lists:seq(1, ?ROWS)],
Shuffled = shuffle(Sorted),
{
"BTree with shuffled keys",
Expand Down Expand Up @@ -579,7 +579,7 @@ randomize(T, List) ->
).

randomize(List) ->
D = lists:map(fun(A) -> {couch_rand:uniform(), A} end, List),
D = lists:map(fun(A) -> {rand:uniform(), A} end, List),
{_, D1} = lists:unzip(lists:keysort(1, D)),
D1.

Expand Down
6 changes: 3 additions & 3 deletions src/couch/test/eunit/couch_file_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,14 @@ check_header_recovery(CheckFun) ->
ok.

write_random_data(Fd) ->
write_random_data(Fd, 100 + couch_rand:uniform(1000)).
write_random_data(Fd, 100 + rand:uniform(1000)).

write_random_data(Fd, 0) ->
{ok, Bytes} = couch_file:bytes(Fd),
{ok, (1 + Bytes div ?BLOCK_SIZE) * ?BLOCK_SIZE};
write_random_data(Fd, N) ->
Choices = [foo, bar, <<"bizzingle">>, "bank", ["rough", stuff]],
Term = lists:nth(couch_rand:uniform(4) + 1, Choices),
Term = lists:nth(rand:uniform(4) + 1, Choices),
{ok, _, _} = couch_file:append_term(Fd, Term),
write_random_data(Fd, N - 1).

Expand Down Expand Up @@ -466,7 +466,7 @@ nuke_dir_test_() ->
File0 = ?tempfile() ++ ".couch",
RootDir = filename:dirname(File0),
BaseName = filename:basename(File0),
Seed = couch_rand:uniform(8999999999) + 999999999,
Seed = rand:uniform(8999999999) + 999999999,
DDocDir = io_lib:format("db.~b_design", [Seed]),
ViewDir = filename:join([RootDir, DDocDir]),
file:make_dir(ViewDir),
Expand Down
2 changes: 1 addition & 1 deletion src/couch/test/eunit/couch_key_tree_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ should_find_missing_leaves_large_test() ->
Seed = {1647, 841737, 351137},
{_, Tree} = test_util:revtree_generate(1000, 0.2, 5, Seed),
Seq = lists:seq(1, 1000),
Revs1 = [{couch_rand:uniform(1000), test_util:random_rev()} || _ <- Seq],
Revs1 = [{rand:uniform(1000), test_util:random_rev()} || _ <- Seq],
Revs2 = couch_key_tree:find_missing(Tree, Revs1),
?assertEqual(lists:sort(Revs1), lists:sort(Revs2))
end).
Expand Down
2 changes: 1 addition & 1 deletion src/couch/test/eunit/json_stream_parse_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ single_byte_data_fun([H | T]) ->
multiple_bytes_data_fun([]) ->
done;
multiple_bytes_data_fun(L) ->
N = couch_rand:uniform(7) - 1,
N = rand:uniform(7) - 1,
{Part, Rest} = split(L, N),
{list_to_binary(Part), fun() -> multiple_bytes_data_fun(Rest) end}.

Expand Down
2 changes: 1 addition & 1 deletion src/couch_log/test/eunit/couch_log_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ check_levels(TestLevel, [CfgLevel | RestLevels]) ->
check_levels(TestLevel, RestLevels).

new_msg() ->
Bin = list_to_binary([couch_rand:uniform(255) || _ <- lists:seq(1, 16)]),
Bin = list_to_binary([rand:uniform(255) || _ <- lists:seq(1, 16)]),
couch_util:to_hex(Bin).
6 changes: 3 additions & 3 deletions src/couch_replicator/src/couch_replicator_doc_processor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ error_backoff(ErrCnt) ->
% ErrCnt is the exponent here. The reason 64 is used is to start at
% 64 (about a minute) max range. Then first backoff would be 30 sec
% on average. Then 1 minute and so on.
couch_rand:uniform(?INITIAL_BACKOFF_EXPONENT bsl Exp).
rand:uniform(?INITIAL_BACKOFF_EXPONENT bsl Exp).

-spec filter_backoff() -> seconds().
filter_backoff() ->
Expand All @@ -419,10 +419,10 @@ filter_backoff() ->
% wait is longer, but not more than a day (?TS_DAY_SEC). If there are just
% few, wait is shorter, starting at about 30 seconds. `2 *` is used since
% the expected wait would then be 0.5 * Range so it is easier to see the
% average wait. `1 +` is used because couch_rand:uniform only
% average wait. `1 +` is used because rand:uniform only
% accepts >= 1 values and crashes otherwise.
Range = 1 + min(2 * (Total / 10), ?TS_DAY_SEC),
?MIN_FILTER_DELAY_SEC + couch_rand:uniform(round(Range)).
?MIN_FILTER_DELAY_SEC + rand:uniform(round(Range)).

% Replication was usurped - move it to the not_owner state
% and try to kill running jobs if there are any.
Expand Down
2 changes: 1 addition & 1 deletion src/couch_replicator/src/couch_replicator_docs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ update_rep_doc(RepDbName, RepDocId, KVs, Wait) when is_binary(RepDocId) ->
throw:conflict ->
Msg = "Conflict when updating replication doc `~s`. Retrying.",
couch_log:error(Msg, [RepDocId]),
ok = timer:sleep(couch_rand:uniform(erlang:min(128, Wait)) * 100),
ok = timer:sleep(rand:uniform(erlang:min(128, Wait)) * 100),
update_rep_doc(RepDbName, RepDocId, KVs, Wait * 2)
end;
update_rep_doc(RepDbName, #doc{body = {RepDocBody}} = RepDoc, KVs, _Try) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ startup_jitter() ->
"startup_jitter",
?STARTUP_JITTER_DEFAULT
),
couch_rand:uniform(erlang:max(1, Jitter)).
rand:uniform(erlang:max(1, Jitter)).

headers_strip_creds([], Acc) ->
lists:reverse(Acc);
Expand Down
2 changes: 1 addition & 1 deletion src/ddoc_cache/test/eunit/ddoc_cache_lru_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ recover(<<"pause", _/binary>>) ->
end,
{ok, paused};
recover(<<"big", _/binary>>) ->
{ok, [couch_rand:uniform() || _ <- lists:seq(1, 8192)]};
{ok, [rand:uniform() || _ <- lists:seq(1, 8192)]};
recover(DbName) ->
{ok, DbName}.

Expand Down
2 changes: 1 addition & 1 deletion src/ioq/src/ioq.erl
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ make_next_request(#state{} = State) ->
{false, true} ->
choose_next_request(#state.background, State);
{false, false} ->
case couch_rand:uniform() < State#state.ratio of
case rand:uniform() < State#state.ratio of
true ->
choose_next_request(#state.background, State);
false ->
Expand Down
2 changes: 1 addition & 1 deletion src/mem3/src/mem3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ choose_shards(DbName, Nodes, Options) ->
mem3_util:create_partition_map(DbName, N, Q, RotatedNodes, Suffix).

rotate_rand(Nodes) ->
{A, B} = lists:split(couch_rand:uniform(length(Nodes)), Nodes),
{A, B} = lists:split(rand:uniform(length(Nodes)), Nodes),
B ++ A.

get_placement(Options) ->
Expand Down

0 comments on commit aca60dd

Please sign in to comment.