forked from marsleezm/basho_bench
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbasho_bench_driver_2i.erl
400 lines (369 loc) · 14.1 KB
/
basho_bench_driver_2i.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
%% -------------------------------------------------------------------
%%
%% basho_bench_driver_2i_pb: Driver for Secondary Indices (via PB)
%%
%% Copyright (c) 2009 Basho Techonologies
%%
%% 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.
%%
%% -------------------------------------------------------------------
-module(basho_bench_driver_2i).
-export([new/1,
run/4]).
-include("basho_bench.hrl").
-record(state, {
pb_pid,
http_host,
http_port,
bucket,
max_key,
pb_timeout,
http_timeout
}).
%% ====================================================================
%% API
%% ====================================================================
new(Id) ->
%% Ensure that ibrowse is started...
application:start(ibrowse),
%% Ensure that riakc library is in the path...
ensure_module(riakc_pb_socket),
ensure_module(mochijson2),
%% Read config settings...
PBIPs = basho_bench_config:get(pb_ips, ["127.0.0.1"]),
PBPort = basho_bench_config:get(pb_port, 8087),
HTTPIPs = basho_bench_config:get(http_ips, ["127.0.0.1"]),
HTTPPort = basho_bench_config:get(http_port, 8098),
Bucket = basho_bench_config:get(riakc_pb_bucket, <<"mybucket">>),
MaxKey = basho_bench_config:get(enforce_keyrange, undefined),
PBTimeout = basho_bench_config:get(pb_timeout_general, 30*1000),
HTTPTimeout = basho_bench_config:get(http_timeout_general, 30*1000),
%% Choose the target node using our ID as a modulus
HTTPTargets = basho_bench_config:normalize_ips(HTTPIPs, HTTPPort),
{HTTPTargetIp, HTTPTargetPort} = lists:nth((Id rem length(HTTPTargets)+1), HTTPTargets),
?INFO("Using http target ~p:~p for worker ~p\n", [HTTPTargetIp, HTTPTargetPort, Id]),
%% Choose the target node using our ID as a modulus
PBTargets = basho_bench_config:normalize_ips(PBIPs, PBPort),
{PBTargetIp, PBTargetPort} = lists:nth((Id rem length(PBTargets)+1), PBTargets),
?INFO("Using pb target ~p:~p for worker ~p\n", [PBTargetIp, PBTargetPort, Id]),
case riakc_pb_socket:start_link(PBTargetIp, PBTargetPort) of
{ok, Pid} ->
{ok, #state {
pb_pid = Pid,
http_host = HTTPTargetIp,
http_port = HTTPTargetPort,
bucket = Bucket,
max_key = MaxKey,
pb_timeout = PBTimeout,
http_timeout = HTTPTimeout}};
{error, Reason2} ->
?FAIL_MSG("Failed to connect riakc_pb_socket to ~p port ~p: ~p\n",
[PBTargetIp, PBTargetPort, Reason2])
end.
%% Get a single object.
run(get_pb, KeyGen, _ValueGen, State) ->
Pid = State#state.pb_pid,
Bucket = State#state.bucket,
Key = to_binary(KeyGen()),
case riakc_pb_socket:get(Pid, Bucket, Key, State#state.pb_timeout) of
{ok, _Obj} ->
{ok, State};
{error, notfound} ->
{ok, State};
{error, Reason} ->
{error, Reason, State}
end;
%% Put an object with N indices.
run({put_pb, N}, KeyGen, ValueGen, State) ->
Pid = State#state.pb_pid,
Bucket = State#state.bucket,
Key = to_integer(KeyGen()),
Value = ValueGen(),
Indexes = generate_integer_indexes_for_key(Key, N),
MetaData = dict:from_list([{<<"index">>, Indexes}]),
%% Create the object...
Robj0 = riakc_obj:new(Bucket, to_binary(Key)),
Robj1 = riakc_obj:update_value(Robj0, Value),
Robj2 = riakc_obj:update_metadata(Robj1, MetaData),
%% Write the object...
case riakc_pb_socket:put(Pid, Robj2, State#state.pb_timeout) of
ok ->
{ok, State};
{error, Reason} ->
{error, Reason, State}
end;
%% Query results via the HTTP interface.
run({query_http, MaxN}, KeyGen, _ValueGen, State) ->
Host = State#state.http_host,
Port = State#state.http_port,
Bucket = State#state.bucket,
{StartKey, EndKey, MaxKey, N} = expected_n(to_integer(KeyGen()), State#state.max_key, MaxN),
URL = io_lib:format("http://~s:~p/buckets/~s/index/field1_int/~p/~p",
[Host, Port, Bucket, StartKey, EndKey]),
case json_get(URL, State) of
{ok, {struct, Proplist}} ->
case {proplists:get_value(<<"keys">>, Proplist), MaxKey} of
{Results, _} when length(Results) == N ->
{ok, State};
{Results, undefined} ->
io:format("Not enough results for query_http: ~p/~p/~p~n", [StartKey, EndKey, Results]),
{ok, State};
{Results, _} ->
%% MaxKey was set, so we're assuming sequential_int from 0-MaxKey, so all values should be there.
{error,
binary_to_list(iolist_to_binary(
io_lib:format("Not enough results for query_http: ~p/~p/~p~n", [StartKey, EndKey, Results]))),
State}
end;
{error, Reason} ->
io:format("[~s:~p] ERROR - Reason: ~p~n", [?MODULE, ?LINE, Reason]),
{error, Reason, State}
end;
%% Query results via the M/R interface.
run({query_mr, 1}, KeyGen, _ValueGen, State) ->
Host = State#state.http_host,
Port = State#state.http_port,
Bucket = State#state.bucket,
Key = to_integer(KeyGen()),
URL = io_lib:format("http://~s:~p/mapred", [Host, Port]),
Body = ["
{
\"inputs\":{
\"bucket\":\"", to_list(Bucket), "\",
\"index\":\"field1_int\",
\"key\":\"", to_list(Key), "\"
},
\"query\":[
{
\"reduce\":{
\"language\":\"erlang\",
\"module\":\"riak_kv_mapreduce\",
\"function\":\"reduce_identity\",
\"keep\":true
}
}
]
}
"],
case json_post(URL, Body, State) of
{ok, Results} when length(Results) == 1 ->
{ok, State};
{ok, Results} ->
io:format("Not enough results for query_mr: ~p/~p~n", [Key, Results]),
{ok, State};
{error, Reason} ->
io:format("[~s:~p] ERROR - Reason: ~p~n", [?MODULE, ?LINE, Reason]),
{error, Reason, State}
end;
run({query_mr, MaxN}, KeyGen, _ValueGen, State) ->
Host = State#state.http_host,
Port = State#state.http_port,
Bucket = State#state.bucket,
{StartKey, EndKey, MaxKey, N} = expected_n(to_integer(KeyGen()), State#state.max_key, MaxN),
URL = io_lib:format("http://~s:~p/mapred", [Host, Port]),
Body = ["
{
\"inputs\":{
\"bucket\":\"", to_list(Bucket), "\",
\"index\":\"field1_int\",
\"start\":\"",to_list(StartKey), "\",
\"end\":\"", to_list(EndKey), "\"
},
\"query\":[
{
\"reduce\":{
\"language\":\"erlang\",
\"module\":\"riak_kv_mapreduce\",
\"function\":\"reduce_identity\",
\"keep\":true
}
}
]
}
"],
case {json_post(URL, Body, State), MaxKey} of
{{ok, Results}, _} when length(Results) == N ->
{ok, State};
{{ok, Results}, undefined} ->
io:format("Not enough results for query_mr: ~p/~p/~p~n", [StartKey, EndKey, Results]),
{ok, State};
{{ok, Results}, _} ->
{error,
binary_to_list(iolist_to_binary(
io_lib:format("Not enough results for query_mr: ~p/~p/~p~n", [StartKey, EndKey, Results]))),
State};
{{error, Reason}, _} ->
io:format("[~s:~p] ERROR - Reason: ~p~n", [?MODULE, ?LINE, Reason]),
{error, Reason, State}
end;
run({query_mr2, 1}, KeyGen, _ValueGen, State) ->
Host = State#state.http_host,
Port = State#state.http_port,
Bucket = State#state.bucket,
Key = to_integer(KeyGen()),
URL = io_lib:format("http://~s:~p/mapred", [Host, Port]),
Body = ["
{
\"inputs\":{
\"bucket\":\"", to_list(Bucket), "\",
\"index\":\"field1_int\",
\"key\":\"", to_list(Key), "\"
},
\"query\":[]
}
"],
case json_post(URL, Body, State) of
{ok, Results} when length(Results) == 1 ->
{ok, State};
{ok, Results} ->
io:format("Not enough results for query_mr: ~p/~p~n", [Key, Results]),
{ok, State};
{error, Reason} ->
io:format("[~s:~p] ERROR - Reason: ~p~n", [?MODULE, ?LINE, Reason]),
{error, Reason, State}
end;
run({query_mr2, MaxN}, KeyGen, _ValueGen, State) ->
Host = State#state.http_host,
Port = State#state.http_port,
Bucket = State#state.bucket,
{StartKey, EndKey, MaxKey, N} = expected_n(to_integer(KeyGen()), State#state.max_key, MaxN),
URL = io_lib:format("http://~s:~p/mapred", [Host, Port]),
Body = ["
{
\"inputs\":{
\"bucket\":\"", to_list(Bucket), "\",
\"index\":\"field1_int\",
\"start\":\"",to_list(StartKey), "\",
\"end\":\"", to_list(EndKey), "\"
},
\"query\":[]
}
"],
case {json_post(URL, Body, State), MaxKey} of
{{ok, Results}, _} when length(Results) == N ->
{ok, State};
{{ok, Results}, undefined} ->
io:format("Not enough results for query_mr: ~p/~p/~p~n", [StartKey, EndKey, Results]),
{ok, State};
{{ok, Results}, _} ->
{error,
binary_to_list(iolist_to_binary(
io_lib:format("Not enough results for query_mr: ~p/~p/~p~n", [StartKey, EndKey, Results]))),
State};
{error, Reason} ->
io:format("[~s:~p] ERROR - Reason: ~p~n", [?MODULE, ?LINE, Reason]),
{error, Reason, State}
end;
%% Query results via the PB interface.
run({query_pb, 1}, KeyGen, _ValueGen, State) ->
Pid = State#state.pb_pid,
Bucket = State#state.bucket,
Key = to_integer(KeyGen()),
case riakc_pb_socket:get_index(Pid, Bucket, <<"field1_int">>,
to_binary(Key), State#state.pb_timeout) of
{ok, Results} when length(Results) == 1 ->
{ok, State};
{ok, Results} ->
io:format("Not enough results for query_pb: ~p/~p~n", [Key, Results]),
{ok, State};
{error, Reason} ->
io:format("[~s:~p] ERROR - Reason: ~p~n", [?MODULE, ?LINE, Reason]),
{error, Reason, State}
end;
run({query_pb, MaxN}, KeyGen, _ValueGen, State) ->
Pid = State#state.pb_pid,
Bucket = State#state.bucket,
{StartKey, EndKey, MaxKey, N} = expected_n(to_integer(KeyGen()), State#state.max_key, MaxN),
case {riakc_pb_socket:get_index(Pid, Bucket, <<"field1_int">>,
to_binary(StartKey), to_binary(EndKey),
State#state.pb_timeout, State#state.pb_timeout), MaxKey} of
{{ok, Results}, _} when length(Results) == N ->
{ok, State};
{{ok, Results}, undefined} ->
io:format("Not enough results for query_pb: ~p/~p/~p~n", [StartKey, EndKey, Results]),
{ok, State};
{{ok, Results}, _} ->
{ok,
binary_to_list(iolist_to_binary(
io_lib:format("Not enough results for query_pb: ~p/~p/~p~n", [StartKey, EndKey, Results]))),
State};
{{error, Reason}, _} ->
io:format("[~s:~p] ERROR - Reason: ~p~n", [?MODULE, ?LINE, Reason]),
{error, Reason, State}
end;
run(Other, _, _, _) ->
throw({unknown_operation, Other}).
%% ====================================================================
%% Internal functions
%% ====================================================================
generate_integer_indexes_for_key(Key, N) ->
F = fun(X) ->
{"field" ++ to_list(X) ++ "_int", Key}
end,
[F(X) || X <- lists:seq(1, N)].
to_binary(B) when is_binary(B) ->
B;
to_binary(I) when is_integer(I) ->
list_to_binary(integer_to_list(I));
to_binary(L) when is_list(L) ->
list_to_binary(L).
to_integer(I) when is_integer(I) ->
I;
to_integer(B) when is_binary(B) ->
list_to_integer(binary_to_list(B));
to_integer(L) when is_list(L) ->
list_to_integer(L).
to_list(L) when is_list(L) ->
L;
to_list(B) when is_binary(B) ->
binary_to_list(B);
to_list(I) when is_integer(I) ->
integer_to_list(I).
json_get(Url, State) ->
Response = ibrowse:send_req(lists:flatten(Url), [], get,
[], [], State#state.pb_timeout),
case Response of
{ok, "200", _, Body} ->
{ok, mochijson2:decode(Body)};
Other ->
{error, Other}
end.
json_post(Url, Payload, State) ->
Headers = [{"Content-Type", "application/json"}],
Response = ibrowse:send_req(lists:flatten(Url), Headers,
post, lists:flatten(Payload),
[], State#state.pb_timeout),
case Response of
{ok, "200", _, Body} ->
{ok, mochijson2:decode(Body)};
Other ->
{error, Other}
end.
ensure_module(Module) ->
case code:which(Module) of
non_existing ->
?FAIL_MSG("~s requires " ++ atom_to_list(Module) ++ " module to be available on code path.\n", [?MODULE]);
_ ->
ok
end.
expected_n(StartKey, undefined, N) ->
{StartKey, StartKey + N - 1, undefined, N};
expected_n(StartKey, MaxKey, N) ->
EndKey = StartKey + N - 1,
NewN = case EndKey > MaxKey of
true -> MaxKey - StartKey + 1;
_ -> N
end,
{StartKey, EndKey, MaxKey, NewN}.