Skip to content

Commit c92e2fb

Browse files
committed
Revert Cassandra drivers
1 parent 02da072 commit c92e2fb

File tree

3 files changed

+275
-1
lines changed

3 files changed

+275
-1
lines changed

rebar.config

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@
1919
{mochiweb, "2.9.*",
2020
{git, "git://github.com/basho/mochiweb", {tag, "v2.9.0"}}},
2121
{getopt, ".*",
22-
{git, "git://github.com/jcomellas/getopt", {tag, "v0.8.2"}}}
22+
{git, "git://github.com/jcomellas/getopt", {tag, "v0.8.2"}}},
23+
24+
{casbench, "0.1",
25+
{git, "git://github.com/basho/casbench",
26+
"95ed55b494551577870984aeb1e0f683631a326f"}},
27+
{erlcql, ".*",
28+
{git, "git://github.com/rpt/erlcql.git",
29+
{branch, "master"}}}
2330
]}.
2431

2532
{erl_opts, [{src_dirs, [src]},
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
%% -------------------------------------------------------------------
2+
%%
3+
%% basho_bench: Benchmarking Suite
4+
%%
5+
%% Copyright (c) 2009-2010 Basho Techonologies
6+
%%
7+
%% This file is provided to you under the Apache License,
8+
%% Version 2.0 (the "License"); you may not use this file
9+
%% except in compliance with the License. You may obtain
10+
%% a copy of the License at
11+
%%
12+
%% http://www.apache.org/licenses/LICENSE-2.0
13+
%%
14+
%% Unless required by applicable law or agreed to in writing,
15+
%% software distributed under the License is distributed on an
16+
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
%% KIND, either express or implied. See the License for the
18+
%% specific language governing permissions and limitations
19+
%% under the License.
20+
%%
21+
%% -------------------------------------------------------------------
22+
-module(basho_bench_driver_cassandra).
23+
24+
-export([new/1,
25+
run/4]).
26+
27+
-include("basho_bench.hrl").
28+
-include_lib("casbench/include/cassandra_thrift.hrl").
29+
30+
-record(state, { client,
31+
keyspace,
32+
colpath,
33+
conlevel
34+
}).
35+
36+
37+
%% ====================================================================
38+
%% API
39+
%% ====================================================================
40+
41+
new(Id) ->
42+
%% Make sure the path is setup such that we can get at riak_client
43+
case code:which(cassandra_thrift) of
44+
non_existing ->
45+
?FAIL_MSG("~s requires cassandra_thrift module to be available on code path.\n",
46+
[?MODULE]);
47+
_ ->
48+
ok
49+
end,
50+
51+
Hosts = basho_bench_config:get(cassandra_hosts, ["localhost"]),
52+
Port = basho_bench_config:get(cassandra_port, 9160),
53+
Keyspace = basho_bench_config:get(cassandra_keyspace, "Keyspace1"),
54+
ColPath = #columnPath { column_family = "Standard1", column = "col1" },
55+
ConLevel = basho_bench_config:get(cassandra_consistencylevel, 1),
56+
57+
%% Choose the node using our ID as a modulus
58+
TargetHost = lists:nth((Id rem length(Hosts)+1), Hosts),
59+
?INFO("Using target ~s:~p for worker ~p\n", [TargetHost, Port, Id]),
60+
61+
case thrift_client:start_link(TargetHost, Port, cassandra_thrift) of
62+
{ok, Client} ->
63+
{ok, #state { client = Client,
64+
keyspace = Keyspace,
65+
colpath = ColPath,
66+
conlevel = ConLevel }};
67+
{error, Reason} ->
68+
?FAIL_MSG("Failed to get a thrift_client for ~p: ~p\n", [TargetHost, Reason])
69+
end.
70+
71+
call(State, Op, Args) ->
72+
(catch thrift_client:call(State#state.client, Op, Args)).
73+
74+
tstamp() ->
75+
{Mega, Sec, _Micro} = now(),
76+
(Mega * 1000000) + Sec.
77+
78+
79+
run(get, KeyGen, _ValueGen,
80+
#state{keyspace=KeySpace, colpath=ColPath, conlevel=ConLevel}=State) ->
81+
Key = KeyGen(),
82+
Args = [KeySpace, Key, ColPath, ConLevel],
83+
case call(State, get, Args) of
84+
{ok, _} ->
85+
{ok, State};
86+
{notFoundException} ->
87+
%% DEBUG io:format("g(~p)",[Key]),
88+
io:format("g"),
89+
{ok, State};
90+
{'EXIT', {timeout, _}} ->
91+
{error, timeout, State};
92+
Error ->
93+
{error, Error, State}
94+
end;
95+
run(put, KeyGen, ValueGen,
96+
#state{keyspace=KeySpace, colpath=ColPath, conlevel=ConLevel}=State) ->
97+
Key = KeyGen(),
98+
Val = ValueGen(),
99+
TS = tstamp(),
100+
Args = [KeySpace, Key, ColPath, Val, TS, ConLevel],
101+
case call(State, insert, Args) of
102+
{ok, ok} ->
103+
{ok, State};
104+
{'EXIT', {timeout, _}} ->
105+
{error, timeout, State};
106+
Error ->
107+
{error, Error, State}
108+
end;
109+
run(delete, KeyGen, _ValueGen,
110+
#state{keyspace=KeySpace, colpath=ColPath, conlevel=ConLevel}=State) ->
111+
Key = KeyGen(),
112+
TS = 0, %% TBD: cannot specify a "known" timestamp value?
113+
Args = [KeySpace, Key, ColPath, TS, ConLevel],
114+
case call(State, remove, Args) of
115+
{ok, _} ->
116+
{ok, State};
117+
{notFoundException} ->
118+
%% DEBUG io:format("d(~p)",[Key]),
119+
io:format("d"),
120+
{ok, State};
121+
{'EXIT', {timeout, _}} ->
122+
{error, timeout, State};
123+
Error ->
124+
{error, Error, State}
125+
end.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
%% -------------------------------------------------------------------
2+
%%
3+
%% basho_bench: Benchmarking Suite
4+
%%
5+
%% Copyright (c) 2009-2012 Basho Techonologies
6+
%%
7+
%% This file is provided to you under the Apache License,
8+
%% Version 2.0 (the "License"); you may not use this file
9+
%% except in compliance with the License. You may obtain
10+
%% a copy of the License at
11+
%%
12+
%% http://www.apache.org/licenses/LICENSE-2.0
13+
%%
14+
%% Unless required by applicable law or agreed to in writing,
15+
%% software distributed under the License is distributed on an
16+
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
%% KIND, either express or implied. See the License for the
18+
%% specific language governing permissions and limitations
19+
%% under the License.
20+
%%
21+
%% -------------------------------------------------------------------
22+
-module(basho_bench_driver_cassandra_cql).
23+
24+
-export([new/1,
25+
run/4]).
26+
27+
-include("basho_bench.hrl").
28+
29+
-record(state, { client,
30+
keyspace,
31+
columnfamily,
32+
column
33+
}).
34+
35+
36+
%% ====================================================================
37+
%% API
38+
%% ====================================================================
39+
40+
new(Id) ->
41+
Host = basho_bench_config:get(cassandra_host, "localhost"),
42+
Port = basho_bench_config:get(cassandra_port, 9042),
43+
Keyspace = basho_bench_config:get(cassandra_keyspace, "Keyspace1"),
44+
ColumnFamily = basho_bench_config:get(cassandra_columnfamily, "ColumnFamily1"),
45+
Column = basho_bench_config:get(cassandra_column, "Column"),
46+
47+
% connect to client
48+
{ok, C} = erlcql:start_link(Host, [{port, Port}]),
49+
error_logger:info_msg("Id: ~p, "
50+
"Connected to Cassandra at Host ~p and Port ~p\n", [Id, Host, Port]),
51+
52+
53+
case ksbarrier(C, Keyspace) of
54+
ok ->
55+
{ok, #state { client = C,
56+
keyspace = Keyspace,
57+
columnfamily = ColumnFamily,
58+
column = Column}};
59+
{error, Reason} ->
60+
error_logger:error_msg("Failed to get a erlcql client for ~p: ~p\n",
61+
[Host, Reason])
62+
end.
63+
64+
65+
ksbarrier(C, Keyspace) ->
66+
case erlcql:q(C, lists:concat(["USE ", Keyspace, ";"])) of
67+
{ok, _KSBin} -> ok;
68+
{error, not_ready} ->
69+
%% Not ready yet, try again
70+
timer:sleep(100),
71+
ksbarrier(C, Keyspace);
72+
{error, _} = Error ->
73+
Error
74+
end.
75+
76+
run(get, KeyGen, _ValueGen,
77+
#state{client=C, columnfamily=ColumnFamily, column=Column}=State) ->
78+
Key = KeyGen(),
79+
Query = ["SELECT ", Column ," FROM ", ColumnFamily ," where KEY = '", Key ,"';"],
80+
case erlcql:q(C, Query, one) of
81+
{ok,void} ->
82+
{ok, State};
83+
{ok, {_Rows, _Cols}} ->
84+
{ok, State};
85+
Error ->
86+
{error, Error, State}
87+
end;
88+
run(insert, KeyGen, ValueGen,
89+
#state{client=C, columnfamily=ColumnFamily, column=Column}=State) ->
90+
Key = KeyGen(),
91+
Val = ValueGen(),
92+
Query = ["INSERT INTO ", ColumnFamily ,
93+
" (KEY, ", Column, ") VALUES "
94+
"('", Key ,"', ", bin_to_hexstr(Val) ,");"],
95+
96+
case erlcql:q(C, Query, any) of
97+
{ok,void} ->
98+
{ok, State};
99+
{ok, {_Rows, _Cols}} ->
100+
{ok, State};
101+
Error ->
102+
{error, Error, State}
103+
end;
104+
run(put, KeyGen, ValueGen,
105+
#state{client=C, columnfamily=ColumnFamily, column=Column}=State) ->
106+
Key = KeyGen(),
107+
Val = ValueGen(),
108+
Query = ["UPDATE ", ColumnFamily,
109+
" SET ", Column, " = ", bin_to_hexstr(Val),
110+
" WHERE KEY = '", Key, "';"],
111+
112+
case erlcql:q(C, Query, any) of
113+
{ok,void} ->
114+
{ok, State};
115+
{ok, {_Rows, _Cols}} ->
116+
{ok, State};
117+
Error ->
118+
{error, Error, State}
119+
end;
120+
run(delete, KeyGen, _ValueGen,
121+
#state{client=C, columnfamily=ColumnFamily}=State) ->
122+
Key = KeyGen(),
123+
Query = ["DELETE FROM ", ColumnFamily ," WHERE KEY = '", Key ,"';"],
124+
case erlcql:q(C, Query, any) of
125+
{ok,void} ->
126+
{ok, State};
127+
{ok, {_Rows, _Cols}} ->
128+
{ok, State};
129+
Error ->
130+
{error, Error, State}
131+
end.
132+
133+
%% Internal Functions
134+
135+
hex(N) when N < 10 ->
136+
$0+N;
137+
hex(N) when N >= 10, N < 16 ->
138+
$a+(N-10).
139+
140+
bin_to_hexstr(Bin) ->
141+
List = binary_to_list(Bin),
142+
["0x", [ [hex(N div 16), hex(N rem 16)] || N <- List, N < 256 ] ].

0 commit comments

Comments
 (0)