forked from marsleezm/basho_bench
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbasho_bench_driver_cassandra.erl
125 lines (112 loc) · 4.11 KB
/
basho_bench_driver_cassandra.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
%% -------------------------------------------------------------------
%%
%% 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_cassandra).
-export([new/1,
run/4]).
-include("basho_bench.hrl").
-include_lib("casbench/include/cassandra_thrift.hrl").
-record(state, { client,
keyspace,
colpath,
conlevel
}).
%% ====================================================================
%% API
%% ====================================================================
new(Id) ->
%% Make sure the path is setup such that we can get at riak_client
case code:which(cassandra_thrift) of
non_existing ->
?FAIL_MSG("~s requires cassandra_thrift module to be available on code path.\n",
[?MODULE]);
_ ->
ok
end,
Hosts = basho_bench_config:get(cassandra_hosts, ["localhost"]),
Port = basho_bench_config:get(cassandra_port, 9160),
Keyspace = basho_bench_config:get(cassandra_keyspace, "Keyspace1"),
ColPath = #columnPath { column_family = "Standard1", column = "col1" },
ConLevel = basho_bench_config:get(cassandra_consistencylevel, 1),
%% Choose the node using our ID as a modulus
TargetHost = lists:nth((Id rem length(Hosts)+1), Hosts),
?INFO("Using target ~s:~p for worker ~p\n", [TargetHost, Port, Id]),
case thrift_client:start_link(TargetHost, Port, cassandra_thrift) of
{ok, Client} ->
{ok, #state { client = Client,
keyspace = Keyspace,
colpath = ColPath,
conlevel = ConLevel }};
{error, Reason} ->
?FAIL_MSG("Failed to get a thrift_client for ~p: ~p\n", [TargetHost, Reason])
end.
call(State, Op, Args) ->
(catch thrift_client:call(State#state.client, Op, Args)).
tstamp() ->
{Mega, Sec, _Micro} = now(),
(Mega * 1000000) + Sec.
run(get, KeyGen, _ValueGen,
#state{keyspace=KeySpace, colpath=ColPath, conlevel=ConLevel}=State) ->
Key = KeyGen(),
Args = [KeySpace, Key, ColPath, ConLevel],
case call(State, get, Args) of
{ok, _} ->
{ok, State};
{notFoundException} ->
%% DEBUG io:format("g(~p)",[Key]),
io:format("g"),
{ok, State};
{'EXIT', {timeout, _}} ->
{error, timeout, State};
Error ->
{error, Error, State}
end;
run(put, KeyGen, ValueGen,
#state{keyspace=KeySpace, colpath=ColPath, conlevel=ConLevel}=State) ->
Key = KeyGen(),
Val = ValueGen(),
TS = tstamp(),
Args = [KeySpace, Key, ColPath, Val, TS, ConLevel],
case call(State, insert, Args) of
{ok, ok} ->
{ok, State};
{'EXIT', {timeout, _}} ->
{error, timeout, State};
Error ->
{error, Error, State}
end;
run(delete, KeyGen, _ValueGen,
#state{keyspace=KeySpace, colpath=ColPath, conlevel=ConLevel}=State) ->
Key = KeyGen(),
TS = 0, %% TBD: cannot specify a "known" timestamp value?
Args = [KeySpace, Key, ColPath, TS, ConLevel],
case call(State, remove, Args) of
{ok, _} ->
{ok, State};
{notFoundException} ->
%% DEBUG io:format("d(~p)",[Key]),
io:format("d"),
{ok, State};
{'EXIT', {timeout, _}} ->
{error, timeout, State};
Error ->
{error, Error, State}
end.