-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathbasho_bench_driver_riakclient.erl
More file actions
143 lines (128 loc) · 4.73 KB
/
Copy pathbasho_bench_driver_riakclient.erl
File metadata and controls
143 lines (128 loc) · 4.73 KB
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
%% -------------------------------------------------------------------
%%
%% basho_bench: Benchmarking Suite
%%
%% Copyright (c) 2009-2010 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_riakclient).
-export([new/1,
run/4]).
-include("basho_bench.hrl").
-record(state, { client,
bucket,
replies }).
%% ====================================================================
%% API
%% ====================================================================
new(Id) ->
%% Make sure the path is setup such that we can get at riak_client
case code:which(riak_client) of
non_existing ->
?FAIL_MSG("~s requires riak_client module to be available on code path.\n",
[?MODULE]);
_ ->
ok
end,
Nodes = basho_bench_config:get(riakclient_nodes),
Cookie = basho_bench_config:get(riakclient_cookie, 'riak'),
MyNode = basho_bench_config:get(riakclient_mynode, [basho_bench, longnames]),
Replies = basho_bench_config:get(riakclient_replies, 2),
Bucket = basho_bench_config:get(riakclient_bucket, <<"test">>),
%% Try to spin up net_kernel
case net_kernel:start(MyNode) of
{ok, _} ->
?INFO("Net kernel started as ~p\n", [node()]);
{error, {already_started, _}} ->
ok;
{error, Reason} ->
?FAIL_MSG("Failed to start net_kernel for ~p: ~p\n", [?MODULE, Reason])
end,
%% Initialize cookie for each of the nodes
[true = erlang:set_cookie(N, Cookie) || N <- Nodes],
%% Try to ping each of the nodes
ping_each(Nodes),
%% Choose the node using our ID as a modulus
TargetNode = lists:nth((Id rem length(Nodes)+1), Nodes),
?INFO("Using target node ~p for worker ~p\n", [TargetNode, Id]),
case riak:client_connect(TargetNode) of
{ok, Client} ->
{ok, #state { client = Client,
bucket = Bucket,
replies = Replies }};
{error, Reason2} ->
?FAIL_MSG("Failed get a riak:client_connect to ~p: ~p\n", [TargetNode, Reason2])
end.
run(get, KeyGen, _ValueGen, State) ->
Key = KeyGen(),
case (State#state.client):get(State#state.bucket, Key, State#state.replies) of
{ok, _} ->
{ok, State};
{error, notfound} ->
{ok, State};
{error, Reason} ->
{error, Reason, State}
end;
run(put, KeyGen, ValueGen, State) ->
Robj = riak_object:new(State#state.bucket, KeyGen(), ValueGen()),
case (State#state.client):put(Robj, State#state.replies) of
ok ->
{ok, State};
{error, Reason} ->
{error, Reason, State}
end;
run(update, KeyGen, ValueGen, State) ->
Key = KeyGen(),
case (State#state.client):get(State#state.bucket, Key, State#state.replies) of
{ok, Robj} ->
Robj2 = riak_object:update_value(Robj, ValueGen()),
case (State#state.client):put(Robj2, State#state.replies) of
ok ->
{ok, State};
{error, Reason} ->
{error, Reason, State}
end;
{error, notfound} ->
Robj = riak_object:new(State#state.bucket, Key, ValueGen()),
case (State#state.client):put(Robj, State#state.replies) of
ok ->
{ok, State};
{error, Reason} ->
{error, Reason, State}
end
end;
run(delete, KeyGen, _ValueGen, State) ->
case (State#state.client):delete(State#state.bucket, KeyGen(), State#state.replies) of
ok ->
{ok, State};
{error, notfound} ->
{ok, State};
{error, Reason} ->
{error, Reason, State}
end.
%% ====================================================================
%% Internal functions
%% ====================================================================
ping_each([]) ->
ok;
ping_each([Node | Rest]) ->
case net_adm:ping(Node) of
pong ->
ping_each(Rest);
pang ->
?FAIL_MSG("Failed to ping node ~p\n", [Node])
end.