Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 76f0dbb

Browse files
committed
rand_compat: New module to transition from random to rand
Not all `rand` functions are implemented because there is no equivalent in `random`. References rabbitmq/rabbitmq-server#860. [#122335241]
1 parent 3885ca1 commit 76f0dbb

File tree

3 files changed

+112
-12
lines changed

3 files changed

+112
-12
lines changed

src/gen_server2.erl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,7 @@ extend_backoff(undefined) ->
610610
undefined;
611611
extend_backoff({backoff, InitialTimeout, MinimumTimeout, DesiredHibPeriod}) ->
612612
{backoff, InitialTimeout, MinimumTimeout, DesiredHibPeriod,
613-
{erlang:phash2([node()]),
614-
time_compat:monotonic_time(),
615-
time_compat:unique_integer()}}.
613+
rand_compat:seed(exsplus)}.
616614

617615
%%%========================================================================
618616
%%% Internal functions
@@ -752,7 +750,7 @@ adjust_timeout_state(SleptAt, AwokeAt, {backoff, CurrentTO, MinimumTO,
752750
true -> lists:max([MinimumTO, CurrentTO div 2]);
753751
false -> CurrentTO
754752
end,
755-
{Extra, RandomState1} = random:uniform_s(Base, RandomState),
753+
{Extra, RandomState1} = rand_compat:uniform_s(Base, RandomState),
756754
CurrentTO1 = Base + Extra,
757755
{backoff, CurrentTO1, MinimumTO, DesiredHibPeriod, RandomState1}.
758756

src/rabbit_misc.erl

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,14 +1156,7 @@ moving_average(Time, HalfLife, Next, Current) ->
11561156
Next * (1 - Weight) + Current * Weight.
11571157

11581158
random(N) ->
1159-
case get(random_seed) of
1160-
undefined ->
1161-
random:seed(erlang:phash2([node()]),
1162-
time_compat:monotonic_time(),
1163-
time_compat:unique_integer());
1164-
_ -> ok
1165-
end,
1166-
random:uniform(N).
1159+
rand_compat:uniform(N).
11671160

11681161
%% Moved from rabbit/src/rabbit_cli.erl
11691162
%% If the server we are talking to has non-standard net_ticktime, and

src/rand_compat.erl

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
%% The contents of this file are subject to the Mozilla Public License
2+
%% Version 1.1 (the "License"); you may not use this file except in
3+
%% compliance with the License. You may obtain a copy of the License
4+
%% at http://www.mozilla.org/MPL/
5+
%%
6+
%% Software distributed under the License is distributed on an "AS IS"
7+
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
8+
%% the License for the specific language governing rights and
9+
%% limitations under the License.
10+
%%
11+
%% The Original Code is RabbitMQ.
12+
%%
13+
%% The Initial Developer of the Original Code is GoPivotal, Inc.
14+
%% Copyright (c) 2016 Pivotal Software, Inc. All rights reserved.
15+
%%
16+
17+
-module(rand_compat).
18+
19+
%% We don't want warnings about the use of erlang:now/0 in
20+
%% this module.
21+
-compile(nowarn_deprecated_function).
22+
23+
%% Declare versioned functions to allow dynamic code loading,
24+
%% depending on the Erlang version running. See 'code_version.erl' for details
25+
-erlang_version_support([
26+
{18, [
27+
{seed, 1, seed_pre_18, seed_post_18},
28+
{seed, 2, seed_pre_18, seed_post_18},
29+
{uniform, 0, uniform_pre_18, uniform_post_18},
30+
{uniform, 1, uniform_pre_18, uniform_post_18},
31+
{uniform_s, 1, uniform_s_pre_18, uniform_s_post_18},
32+
{uniform_s, 2, uniform_s_pre_18, uniform_s_post_18}
33+
]}
34+
]).
35+
36+
-export([
37+
seed/1, seed_pre_18/1, seed_post_18/1,
38+
seed/2, seed_pre_18/2, seed_post_18/2,
39+
uniform/0, uniform_pre_18/0, uniform_post_18/0,
40+
uniform/1, uniform_pre_18/1, uniform_post_18/1,
41+
uniform_s/1, uniform_s_pre_18/1, uniform_s_post_18/1,
42+
uniform_s/2, uniform_s_pre_18/2, uniform_s_post_18/2
43+
]).
44+
45+
-define(IS_ALG(A), (A =:= exs64 orelse A =:= exsplus orelse A =:= exs1024)).
46+
47+
%% export_seed_s/1 can't be implemented with `random`.
48+
%% export_seed_s/2. can't be implemented with `random`.
49+
50+
%% normal_s/1 can't be implemented with `random`.
51+
%% normal_s/2. can't be implemented with `random`.
52+
53+
%% seed/1.
54+
55+
seed(AlgOrExpState) ->
56+
code_version:update(?MODULE),
57+
?MODULE:seed(AlgOrExpState).
58+
59+
seed_pre_18(Alg) when ?IS_ALG(Alg) -> random:seed();
60+
seed_pre_18(ExpState) -> random:seed(ExpState).
61+
seed_post_18(AlgOrExpState) -> rand:seed(AlgOrExpState).
62+
63+
%% seed/2.
64+
65+
seed(Alg, ExpState) ->
66+
code_version:update(?MODULE),
67+
?MODULE:seed(Alg, ExpState).
68+
69+
seed_pre_18(_Alg, ExpState) -> random:seed(ExpState).
70+
seed_post_18(Alg, ExpState) -> rand:seed(Alg, ExpState).
71+
72+
%% seed_s/1 can't be implemented with `random`.
73+
%% seed_s/2. can't be implemented with `random`.
74+
75+
%% uniform/0.
76+
77+
uniform() ->
78+
code_version:update(?MODULE),
79+
?MODULE:uniform().
80+
81+
uniform_pre_18() -> random:uniform().
82+
uniform_post_18() -> rand:uniform().
83+
84+
%% uniform/1.
85+
86+
uniform(N) ->
87+
code_version:update(?MODULE),
88+
?MODULE:uniform(N).
89+
90+
uniform_pre_18(N) -> random:uniform(N).
91+
uniform_post_18(N) -> rand:uniform(N).
92+
93+
%% uniform_s/1.
94+
95+
uniform_s(State) ->
96+
code_version:update(?MODULE),
97+
?MODULE:uniform_s(State).
98+
99+
uniform_s_pre_18(State) -> random:uniform_s(State).
100+
uniform_s_post_18(State) -> rand:uniform_s(State).
101+
102+
%% uniform_s/2.
103+
104+
uniform_s(N, State) ->
105+
code_version:update(?MODULE),
106+
?MODULE:uniform_s(N, State).
107+
108+
uniform_s_pre_18(N, State) -> random:uniform_s(N, State).
109+
uniform_s_post_18(N, State) -> rand:uniform_s(N, State).

0 commit comments

Comments
 (0)