-
Notifications
You must be signed in to change notification settings - Fork 392
/
riak_core_util.erl
242 lines (213 loc) · 7.96 KB
/
riak_core_util.erl
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
%% -------------------------------------------------------------------
%%
%% riak_core: Core Riak Application
%%
%% Copyright (c) 2007-2010 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
%% @doc Various functions that are useful throughout Riak.
-module(riak_core_util).
-export([moment/0,
make_tmp_dir/0,
compare_dates/2,
reload_all/1,
integer_to_list/2,
unique_id_62/0,
str_to_node/1,
chash_key/1,
chash_std_keyfun/1,
chash_bucketonly_keyfun/1,
mkclientid/1,
start_app_deps/1,
rpc_every_member/4]).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
%% R14 Compatibility
-compile({no_auto_import,[integer_to_list/2]}).
%% ===================================================================
%% Public API
%% ===================================================================
%% @spec moment() -> integer()
%% @doc Get the current "moment". Current implementation is the
%% number of seconds from year 0 to now, universal time, in
%% the gregorian calendar.
moment() -> calendar:datetime_to_gregorian_seconds(calendar:universal_time()).
%% @spec compare_dates(string(), string()) -> boolean()
%% @doc Compare two RFC1123 date strings or two now() tuples (or one
%% of each). Return true if date A is later than date B.
compare_dates(A={_,_,_}, B={_,_,_}) ->
%% assume 3-tuples are now() times
A > B;
compare_dates(A, B) when is_list(A) ->
%% assume lists are rfc1123 date strings
compare_dates(rfc1123_to_now(A), B);
compare_dates(A, B) when is_list(B) ->
compare_dates(A, rfc1123_to_now(B)).
%% 719528 days from Jan 1, 0 to Jan 1, 1970
%% *86400 seconds/day
-define(SEC_TO_EPOCH, 62167219200).
rfc1123_to_now(String) when is_list(String) ->
GSec = calendar:datetime_to_gregorian_seconds(
httpd_util:convert_request_date(String)),
ESec = GSec-?SEC_TO_EPOCH,
Sec = ESec rem 1000000,
MSec = ESec div 1000000,
{MSec, Sec, 0}.
%% @spec make_tmp_dir() -> string()
%% @doc Create a unique directory in /tmp. Returns the path
%% to the new directory.
make_tmp_dir() ->
TmpId = io_lib:format("riptemp.~p",
[erlang:phash2({random:uniform(),self()})]),
TempDir = filename:join("/tmp", TmpId),
case filelib:is_dir(TempDir) of
true -> make_tmp_dir();
false ->
ok = file:make_dir(TempDir),
TempDir
end.
%% @spec integer_to_list(Integer :: integer(), Base :: integer()) ->
%% string()
%% @doc Convert an integer to its string representation in the given
%% base. Bases 2-62 are supported.
integer_to_list(I, 10) ->
erlang:integer_to_list(I);
integer_to_list(I, Base)
when is_integer(I), is_integer(Base),Base >= 2, Base =< 1+$Z-$A+10+1+$z-$a ->
if I < 0 ->
[$-|integer_to_list(-I, Base, [])];
true ->
integer_to_list(I, Base, [])
end;
integer_to_list(I, Base) ->
erlang:error(badarg, [I, Base]).
%% @spec integer_to_list(integer(), integer(), string()) -> string()
integer_to_list(I0, Base, R0) ->
D = I0 rem Base,
I1 = I0 div Base,
R1 = if D >= 36 ->
[D-36+$a|R0];
D >= 10 ->
[D-10+$A|R0];
true ->
[D+$0|R0]
end,
if I1 =:= 0 ->
R1;
true ->
integer_to_list(I1, Base, R1)
end.
%% @spec unique_id_62() -> string()
%% @doc Create a random identifying integer, returning its string
%% representation in base 62.
unique_id_62() ->
Rand = crypto:sha(term_to_binary({make_ref(), now()})),
<<I:160/integer>> = Rand,
integer_to_list(I, 62).
%% @spec reload_all(Module :: atom()) ->
%% [{purge_response(), load_file_response()}]
%% @type purge_response() = boolean()
%% @type load_file_response() = {module, Module :: atom()}|
%% {error, term()}
%% @doc Ask each member node of the riak ring to reload the given
%% Module. Return is a list of the results of code:purge/1
%% and code:load_file/1 on each node.
reload_all(Module) ->
{ok, Ring} = riak_core_ring_manager:get_my_ring(),
[{rpc:call(Node, code, purge, [Module]),
rpc:call(Node, code, load_file, [Module])} ||
Node <- riak_core_ring:all_members(Ring)].
%% @spec mkclientid(RemoteNode :: term()) -> ClientID :: list()
%% @doc Create a unique-enough id for vclock clients.
mkclientid(RemoteNode) ->
{{Y,Mo,D},{H,Mi,S}} = erlang:universaltime(),
{_,_,NowPart} = now(),
Id = erlang:phash2([Y,Mo,D,H,Mi,S,node(),RemoteNode,NowPart]),
<<Id:32>>.
%% @spec chash_key(BKey :: riak_object:bkey()) -> chash:index()
%% @doc Create a binary used for determining replica placement.
chash_key({Bucket,Key}) ->
BucketProps = riak_core_bucket:get_bucket(Bucket),
{chash_keyfun, {M, F}} = proplists:lookup(chash_keyfun, BucketProps),
M:F({Bucket,Key}).
%% @spec chash_std_keyfun(BKey :: riak_object:bkey()) -> chash:index()
%% @doc Default object/ring hashing fun, direct passthrough of bkey.
chash_std_keyfun({Bucket, Key}) -> chash:key_of({Bucket, Key}).
%% @spec chash_bucketonly_keyfun(BKey :: riak_object:bkey()) -> chash:index()
%% @doc Object/ring hashing fun that ignores Key, only uses Bucket.
chash_bucketonly_keyfun({Bucket, _Key}) -> chash:key_of(Bucket).
str_to_node(Node) when is_atom(Node) ->
str_to_node(atom_to_list(Node));
str_to_node(NodeStr) ->
case string:tokens(NodeStr, "@") of
[NodeName] ->
%% Node name only; no host name. If the local node has a hostname,
%% append it
case node_hostname() of
[] ->
list_to_atom(NodeName);
Hostname ->
list_to_atom(NodeName ++ "@" ++ Hostname)
end;
_ ->
list_to_atom(NodeStr)
end.
node_hostname() ->
NodeStr = atom_to_list(node()),
case string:tokens(NodeStr, "@") of
[_NodeName, Hostname] ->
Hostname;
_ ->
[]
end.
%% @spec start_app_deps(App :: atom()) -> ok
%% @doc Start depedent applications of App.
start_app_deps(App) ->
{ok, DepApps} = application:get_key(App, applications),
[ensure_started(A) || A <- DepApps],
ok.
%% @spec ensure_started(Application :: atom()) -> ok
%% @doc Start the named application if not already started.
ensure_started(App) ->
case application:start(App) of
ok ->
ok;
{error, {already_started, App}} ->
ok
end.
%% @spec rpc_every_member(atom(), atom(), [term()], integer()|infinity)
%% -> {Results::[term()], BadNodes::[node()]}
%% @doc Make an RPC call to the given module and function on each
%% member of the cluster. See rpc:multicall/5 for a description
%% of the return value.
rpc_every_member(Module, Function, Args, Timeout) ->
{ok, MyRing} = riak_core_ring_manager:get_my_ring(),
Nodes = riak_core_ring:all_members(MyRing),
rpc:multicall(Nodes, Module, Function, Args, Timeout).
%% ===================================================================
%% EUnit tests
%% ===================================================================
-ifdef(TEST).
moment_test() ->
M1 = riak_core_util:moment(),
M2 = riak_core_util:moment(),
?assert(M2 >= M1).
clientid_uniqueness_test() ->
ClientIds = [mkclientid('somenode@somehost') || _I <- lists:seq(0, 10000)],
length(ClientIds) =:= length(sets:to_list(sets:from_list(ClientIds))).
-endif.