Blob Blame History Raw
From: Peter Lemenkov <lemenkov@gmail.com>
Date: Fri, 18 Aug 2017 15:42:01 +0300
Subject: [PATCH] Don't use deprecated functions

Signed-off-by: Peter Lemenkov <lemenkov@gmail.com>

diff --git a/src/bloom.erl b/src/bloom.erl
index 59e331c7..4e51bee1 100644
--- a/src/bloom.erl
+++ b/src/bloom.erl
@@ -194,7 +194,7 @@ simple_shuffle(L, N) ->
     lists:sublist(simple_shuffle(L), 1, N).
 simple_shuffle(L) ->
     N = 1000 * length(L),
-    L2 = [{random:uniform(N), E} || E <- L],
+    L2 = [{rand:uniform(N), E} || E <- L],
     {_, L3} = lists:unzip(lists:keysort(1, L2)),
     L3.
 
diff --git a/src/chash.erl b/src/chash.erl
index 265be1c4..b7764937 100644
--- a/src/chash.erl
+++ b/src/chash.erl
@@ -221,7 +221,7 @@ max_n(N, {NumPartitions, _Nodes}) ->
 %% @private
 -spec random_node(NodeA :: chash_node(), NodeB :: chash_node()) -> chash_node().
 random_node(NodeA,NodeA) -> NodeA;
-random_node(NodeA,NodeB) -> lists:nth(random:uniform(2),[NodeA,NodeB]).
+random_node(NodeA,NodeB) -> lists:nth(rand:uniform(2),[NodeA,NodeB]).
 
 %% ===================================================================
 %% EUnit tests
diff --git a/src/hashtree.erl b/src/hashtree.erl
index 3ffa9bc5..9993832b 100644
--- a/src/hashtree.erl
+++ b/src/hashtree.erl
@@ -716,7 +716,7 @@ new_segment_store(Opts, State) ->
     %% flushed to disk at once when under a heavy uniform load.
     WriteBufferMin = proplists:get_value(write_buffer_size_min, Config, DefaultWriteBufferMin),
     WriteBufferMax = proplists:get_value(write_buffer_size_max, Config, DefaultWriteBufferMax),
-    {Offset, _} = random:uniform_s(1 + WriteBufferMax - WriteBufferMin, erlang:timestamp()),
+    {Offset, _} = rand:uniform_s(1 + WriteBufferMax - WriteBufferMin, rand:seed_s(exsplus)),
     WriteBufferSize = WriteBufferMin + Offset,
     Config2 = orddict:store(write_buffer_size, WriteBufferSize, Config),
     Config3 = orddict:erase(write_buffer_size_min, Config2),
diff --git a/src/riak_core_broadcast.erl b/src/riak_core_broadcast.erl
index 038a5af6..ef54f720 100644
--- a/src/riak_core_broadcast.erl
+++ b/src/riak_core_broadcast.erl
@@ -490,7 +490,7 @@ random_other_node(OrdSet) ->
     case Size of
         0 -> undefined;
         _ ->
-            lists:nth(random:uniform(Size),
+            lists:nth(rand:uniform(Size),
                      ordsets:to_list(OrdSet))
     end.
 
@@ -608,19 +608,19 @@ init_peers(Members) ->
             %% with cycles. it will be adjusted as needed
             Tree = riak_core_util:build_tree(1, Members, [cycles]),
             InitEagers = orddict:fetch(node(), Tree),
-            InitLazys  = [lists:nth(random:uniform(N - 2), Members -- [node() | InitEagers])];
+            InitLazys  = [lists:nth(rand:uniform(N - 2), Members -- [node() | InitEagers])];
         N when N < 10 ->
             %% 5 to 9 members, start with gossip tree used by
             %% riak_core_gossip. it will be adjusted as needed
             Tree = riak_core_util:build_tree(2, Members, [cycles]),
             InitEagers = orddict:fetch(node(), Tree),
-            InitLazys  = [lists:nth(random:uniform(N - 3), Members -- [node() | InitEagers])];
+            InitLazys  = [lists:nth(rand:uniform(N - 3), Members -- [node() | InitEagers])];
         N ->
             %% 10 or more members, use a tree similar to riak_core_gossip
             %% but with higher fanout (larger initial eager set size)
             NEagers = round(math:log(N) + 1),
             Tree = riak_core_util:build_tree(NEagers, Members, [cycles]),
             InitEagers = orddict:fetch(node(), Tree),
-            InitLazys  = [lists:nth(random:uniform(N - (NEagers + 1)), Members -- [node() | InitEagers])]
+            InitLazys  = [lists:nth(rand:uniform(N - (NEagers + 1)), Members -- [node() | InitEagers])]
     end,
     {InitEagers, InitLazys}.
diff --git a/src/riak_core_claim.erl b/src/riak_core_claim.erl
index aaad2d19..4ed90ddf 100644
--- a/src/riak_core_claim.erl
+++ b/src/riak_core_claim.erl
@@ -390,13 +390,13 @@ choose_claim_v3(Ring, _ClaimNode, Params) ->
 
     %% Seed the random number generator for predictable results
     %% run the claim, then put it back if possible
-    OldSeed = random:seed(proplists:get_value(seed, Params, {1,2,3})),
+    OldSeed = rand:seed(proplists:get_value(seed, Params, {1,2,3})),
     {NewOwners, NewMetrics} = claim_v3(Wants, Owners, Params),
     case OldSeed of
         undefined ->
             ok;
         _ ->
-            {_,_,_} = random:seed(OldSeed),
+            {_,_,_} = rand:seed(OldSeed),
             ok
     end,
 
@@ -1027,7 +1027,7 @@ urand(High) ->
     urand(1, High).
 
 urand(Low, High) ->
-    Low + random:uniform(High - Low + 1) - 1.
+    Low + rand:uniform(High - Low + 1) - 1.
 
 %% @private
 %% return all the indices within TN of Indices
diff --git a/src/riak_core_claimant.erl b/src/riak_core_claimant.erl
index 33966b41..7357c7f7 100644
--- a/src/riak_core_claimant.erl
+++ b/src/riak_core_claimant.erl
@@ -1413,7 +1413,7 @@ handle_down_nodes(CState, Next) ->
                  case (OwnerLeaving and NextDown) of
                      true ->
                          Active = riak_core_ring:active_members(CState) -- [O],
-                         RNode = lists:nth(random:uniform(length(Active)),
+                         RNode = lists:nth(rand:uniform(length(Active)),
                                            Active),
                          {Idx, O, RNode, Mods, Status};
                      _ ->
diff --git a/src/riak_core_gossip.erl b/src/riak_core_gossip.erl
index 3567897f..e7d2bce4 100644
--- a/src/riak_core_gossip.erl
+++ b/src/riak_core_gossip.erl
@@ -115,7 +115,7 @@ recursive_gossip(Ring) ->
 
 random_recursive_gossip(Ring) ->
     Active = riak_core_ring:active_members(Ring),
-    RNode = lists:nth(random:uniform(length(Active)), Active),
+    RNode = lists:nth(rand:uniform(length(Active)), Active),
     recursive_gossip(Ring, RNode).
 
 %% ===================================================================
@@ -357,7 +357,7 @@ log_node_removed(Node, Old) ->
     lager:info("'~s' removed from cluster (previously: '~s')~n", [Node, Old]).
 
 remove_from_cluster(Ring, ExitingNode) ->
-    remove_from_cluster(Ring, ExitingNode, erlang:timestamp()).
+    remove_from_cluster(Ring, ExitingNode, rand:seed_s(exsplus)).
 
 remove_from_cluster(Ring, ExitingNode, Seed) ->
     % Get a list of indices owned by the ExitingNode...
@@ -414,7 +414,7 @@ attempt_simple_transfer(Seed, Ring, [{P, Exit}|Rest], TargetN, Exit, Idx, Last)
                     target_n_fail;
                 Qualifiers ->
                     %% these nodes don't violate target_n forward
-                    {Rand, Seed2} = random:uniform_s(length(Qualifiers), Seed),
+                    {Rand, Seed2} = rand:uniform_s(length(Qualifiers), Seed),
                     Chosen = lists:nth(Rand, Qualifiers),
                     %% choose one, and do the rest of the ring
                     attempt_simple_transfer(
diff --git a/src/riak_core_net_ticktime.erl b/src/riak_core_net_ticktime.erl
index 4dfa6a6b..cbbd2989 100644
--- a/src/riak_core_net_ticktime.erl
+++ b/src/riak_core_net_ticktime.erl
@@ -62,7 +62,7 @@ start_set_net_ticktime_daemon(Node, Time, true) ->
                             lager:info("start_set_net_ticktime_daemon: started "
                                        "changing net_ticktime on ~p to ~p\n",
                                  [Node, Time]),
-                            _ = random:seed(os:timestamp()),
+                            _ = rand:seed(os:timestamp()),
                             set_net_ticktime_daemon_loop(Time, 1)
                         catch _:_ ->
                                 ok
@@ -116,7 +116,7 @@ set_net_ticktime_daemon_loop(Time, Count) ->
                        "changing net_ticktime on ~p to ~p\n", [node(), Time]),
             exit(normal);
         _ ->
-            timer:sleep(random:uniform(1*1000)),
+            timer:sleep(rand:uniform(1*1000)),
             %% Here is an uncommon use the erlang:nodes/1 BIF.
             %% Hidden nodes (e.g. administrative escripts) may have
             %% connected to us.  Force them to change their tick time,
diff --git a/src/riak_core_pw_auth.erl b/src/riak_core_pw_auth.erl
index 8fb15590..523bd67e 100644
--- a/src/riak_core_pw_auth.erl
+++ b/src/riak_core_pw_auth.erl
@@ -33,7 +33,7 @@
 hash_password(BinaryPass) when is_binary(BinaryPass) ->
     % TODO: Do something more with the salt?
     % Generate salt the simple way
-    Salt = crypto:rand_bytes(?SALT_LENGTH),
+    Salt = crypto:strong_rand_bytes(?SALT_LENGTH),
 
     % Hash the original password and store as hex
     {ok, HashedPass} = pbkdf2:pbkdf2(?HASH_FUNCTION, BinaryPass, Salt, ?HASH_ITERATIONS),
diff --git a/src/riak_core_ring.erl b/src/riak_core_ring.erl
index b5a2f3ed..74d46a93 100644
--- a/src/riak_core_ring.erl
+++ b/src/riak_core_ring.erl
@@ -443,7 +443,7 @@ preflist(Key, State) -> chash:successors(Key, State?CHSTATE.chring).
 -spec random_node(State :: chstate()) -> Node :: term().
 random_node(State) ->
     L = all_members(State),
-    lists:nth(random:uniform(length(L)), L).
+    lists:nth(rand:uniform(length(L)), L).
 
 %% @doc Return a partition index not owned by the node executing this function.
 %%      If this node owns all partitions, return any index.
@@ -452,7 +452,7 @@ random_other_index(State) ->
     L = [I || {I,Owner} <- ?MODULE:all_owners(State), Owner =/= node()],
     case L of
         [] -> hd(my_indices(State));
-        _ -> lists:nth(random:uniform(length(L)), L)
+        _ -> lists:nth(rand:uniform(length(L)), L)
     end.
 
 -spec random_other_index(State :: chstate(), Exclude :: [term()]) -> chash:index_as_int() | no_indices.
@@ -462,7 +462,7 @@ random_other_index(State, Exclude) when is_list(Exclude) ->
               not lists:member(I, Exclude)],
     case L of
         [] -> no_indices;
-        _ -> lists:nth(random:uniform(length(L)), L)
+        _ -> lists:nth(rand:uniform(length(L)), L)
     end.
 
 %% @doc Return a randomly-chosen node from amongst the owners other than this one.
@@ -472,7 +472,7 @@ random_other_node(State) ->
         [] ->
             no_node;
         L ->
-            lists:nth(random:uniform(length(L)), L)
+            lists:nth(rand:uniform(length(L)), L)
     end.
 
 %% @doc Return a randomly-chosen active node other than this one.
@@ -482,7 +482,7 @@ random_other_active_node(State) ->
         [] ->
             no_node;
         L ->
-            lists:nth(random:uniform(length(L)), L)
+            lists:nth(rand:uniform(length(L)), L)
     end.
 
 %% @doc Incorporate another node's state into our view of the Riak world.
diff --git a/src/riak_core_util.erl b/src/riak_core_util.erl
index c799975f..472608e4 100644
--- a/src/riak_core_util.erl
+++ b/src/riak_core_util.erl
@@ -132,7 +132,7 @@ rfc1123_to_now(String) when is_list(String) ->
 %%      to the new directory.
 make_tmp_dir() ->
     TmpId = io_lib:format("riptemp.~p",
-                          [erlang:phash2({random:uniform(),self()})]),
+                          [erlang:phash2({rand:uniform(),self()})]),
     TempDir = filename:join("/tmp", TmpId),
     case filelib:is_dir(TempDir) of
         true -> make_tmp_dir();
@@ -625,7 +625,7 @@ orddict_delta(A, B) ->
 
 shuffle(L) ->
     N = 134217727, %% Largest small integer on 32-bit Erlang
-    L2 = [{random:uniform(N), E} || E <- L],
+    L2 = [{rand:uniform(N), E} || E <- L],
     L3 = [E || {_, E} <- lists:sort(L2)],
     L3.
 
diff --git a/src/riak_core_vnode.erl b/src/riak_core_vnode.erl
index a1aa0cb6..393a60d5 100644
--- a/src/riak_core_vnode.erl
+++ b/src/riak_core_vnode.erl
@@ -243,7 +243,7 @@ do_init(State = #state{index=Index, mod=Mod, forward=Forward}) ->
             end,
             riak_core_handoff_manager:remove_exclusion(Mod, Index),
             Timeout = app_helper:get_env(riak_core, vnode_inactivity_timeout, ?DEFAULT_TIMEOUT),
-            Timeout2 = Timeout + random:uniform(Timeout),
+            Timeout2 = Timeout + rand:uniform(Timeout),
             State2 = State#state{modstate=ModState, inactivity_timeout=Timeout2,
                                  pool_pid=PoolPid, pool_config=PoolConfig},
             lager:debug("vnode :: ~p/~p :: ~p~n", [Mod, Index, Forward]),