Skip to content

Commit

Permalink
s/erlcassa/erlcql/
Browse files Browse the repository at this point in the history
erlcassa's thrift implementation clashes with casbench's.
erlcassa is nominally used as a cql client but was set up using thrift
(which casbench does already)
it was clearly labelled as alpha, and remains untouched in 2 years.
erlcql is a real cql client, under active development and does not clash
with casbench
  • Loading branch information
sanmiguel committed Dec 11, 2013
1 parent 5fa4fee commit 80a802b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 43 deletions.
22 changes: 16 additions & 6 deletions examples/cassandra_cql.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
%% Run the following queries in a cqlsh session:
%% CREATE KEYSPACE DEMO WITH strategy_class = SimpleStrategy AND strategy_options:replication_factor = 1;
%% CREATE COLUMNFAMILY test (KEY varchar PRIMARY KEY, val blob);

%% Cassandra v2.0.1
%% DROP KEYSPACE DEMO;
%% CREATE KEYSPACE DEMO WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1' };
%% USE DEMO;
%% CREATE COLUMNFAMILY test (KEY varchar PRIMARY KEY, val blob);

{mode, max}.

{duration, 10}.
{duration, 1}.

{concurrent, 1}.
{concurrent, 5}.

{driver, basho_bench_driver_cassandra_cql}.

Expand All @@ -19,13 +26,16 @@
{value_generator, {fixed_bin, 100}}.

{cassandra_host, "localhost"}.
{cassandra_port, 9160}.
{cassandra_port, 9042}.

{cassandra_keyspace, "DEMO"}.
{cassandra_columnfamily, "test"}.
{cassandra_column, "val"}.

%% {operations, [{insert, 1},{put, 1},{get, 1},{delete, 1}]}.
{operations, [{insert, 1}]}.
{operations, [{insert, 1},{put, 1},{get, 1},{delete, 1}]}.
%%{operations, [{insert, 1}]}.
%%{operations, [{put, 1}]}.
%%{operations, [{get, 1}]}.
%%{operations, [{delete, 1}]}.

{code_paths, ["./deps/erlcassa/ebin", "./deps/thrift/ebin"]}.
{code_paths, ["./deps/erlcql/ebin"]}.
4 changes: 2 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
{git, "git://github.com/basho/casbench",
"95ed55b494551577870984aeb1e0f683631a326f"}},
{thrift, ".*", {git, "git://github.com/lpgauth/thrift-erlang.git", {branch, "master"}}},
{erlcassa, ".*",
{git, "git://github.com/ostinelli/erlcassa.git",
{erlcql, ".*",
{git, "git://github.com/rpt/erlcql.git",
{branch, "master"}}},
{riakc, ".*",
{git, "git://github.com/basho/riak-erlang-client", {tag, "1.4.1"}}},
Expand Down
88 changes: 53 additions & 35 deletions src/basho_bench_driver_cassandra_cql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
-export([new/1,
run/4]).

-include("basho_bench.hrl").
-include_lib("erlcassa/include/erlcassa.hrl").
-include_lib("basho_bench/include/basho_bench.hrl").

-record(state, { client,
keyspace,
Expand All @@ -40,34 +39,48 @@

new(Id) ->
Host = basho_bench_config:get(cassandra_host, "localhost"),
Port = basho_bench_config:get(cassandra_port, 9160),
Port = basho_bench_config:get(cassandra_port, 9042),
Keyspace = basho_bench_config:get(cassandra_keyspace, "Keyspace1"),
ColumnFamily = basho_bench_config:get(cassandra_columnfamily, "ColumnFamily1"),
Column = basho_bench_config:get(cassandra_column, "Column"),

% connect to client
{ok, C} = erlcassa_client:connect(Host, Port),
?INFO("Id: ~p, Connected to Cassandra at Host ~p and Port ~p\n", [Id, Host, Port]),

% use keyspace
{result, ok} = erlcassa_client:cql_execute(C, lists:concat(["USE ", Keyspace, ";"])),

case erlcassa_client:cql_execute(C, lists:concat(["USE ", Keyspace, ";"])) of
{result, ok} ->
{ok, C} = erlcql:start_link(Host, [{port, Port}]),
error_logger:info_msg("Id: ~p, "
"Connected to Cassandra at Host ~p and Port ~p\n", [Id, Host, Port]),


case ksbarrier(C, Keyspace) of
ok ->
{ok, #state { client = C,
keyspace = Keyspace,
columnfamily = ColumnFamily,
column = Column}};
{error, Reason} ->
?FAIL_MSG("Failed to get a thrift_client for ~p: ~p\n", [Host, Reason])
{error, Reason} ->
error_logger:error_msg("Failed to get a erlcql client for ~p: ~p\n",
[Host, Reason])
end.


ksbarrier(C, Keyspace) ->
case erlcql:q(C, lists:concat(["USE ", Keyspace, ";"])) of
{ok, _KSBin} -> ok;
{error, not_ready} ->
%% Not ready yet, try again
timer:sleep(100),
ksbarrier(C, Keyspace);
{error, _} = Error ->
Error
end.

run(get, KeyGen, _ValueGen,
run(get, KeyGen, _ValueGen,
#state{client=C, columnfamily=ColumnFamily, column=Column}=State) ->
Key = KeyGen(),
Query = lists:concat(["SELECT ", Column ," FROM ", ColumnFamily ," where KEY = '", Key ,"';"]),
case erlcassa_client:cql_execute(C, Query, proplist) of
{result, {rows, _Rows}} ->
Query = ["SELECT ", Column ," FROM ", ColumnFamily ," where KEY = '", Key ,"';"],
case erlcql:q(C, Query, one) of
{ok,void} ->
{ok, State};
{ok, {_Rows, _Cols}} ->
%% [Row|_] = Rows,
%% KeyColumn = erlcassa_client:get_column("KEY", Row),
{ok, State};
Expand All @@ -78,9 +91,14 @@ run(insert, KeyGen, ValueGen,
#state{client=C, columnfamily=ColumnFamily, column=Column}=State) ->
Key = KeyGen(),
Val = ValueGen(),
Query = lists:concat(["INSERT INTO ", ColumnFamily , " (KEY, ", Column, ") VALUES ('", Key ,"', ", bin_to_hexstr(Val) ,");"]),
case erlcassa_client:cql_execute(C, Query) of
{result, ok} ->
Query = ["INSERT INTO ", ColumnFamily ,
" (KEY, ", Column, ") VALUES "
"('", Key ,"', ", bin_to_hexstr(Val) ,");"],

case erlcql:q(C, Query, any) of
{ok,void} ->
{ok, State};
{ok, {_Rows, _Cols}} ->
{ok, State};
Error ->
{error, Error, State}
Expand All @@ -89,19 +107,26 @@ run(put, KeyGen, ValueGen,
#state{client=C, columnfamily=ColumnFamily, column=Column}=State) ->
Key = KeyGen(),
Val = ValueGen(),
Query = lists:concat(["UPDATE ", ColumnFamily, " SET '", Column, "' = ", bin_to_hexstr(Val), " WHERE KEY = '", Key, "';"]),
case erlcassa_client:cql_execute(C, Query, proplist) of
{result,ok} ->
Query = ["UPDATE ", ColumnFamily,
" SET ", Column, " = ", bin_to_hexstr(Val),
" WHERE KEY = '", Key, "';"],

case erlcql:q(C, Query, any) of
{ok,void} ->
{ok, State};
{ok, {_Rows, _Cols}} ->
{ok, State};
Error ->
{error, Error, State}
end;
run(delete, KeyGen, _ValueGen,
#state{client=C, columnfamily=ColumnFamily}=State) ->
Key = KeyGen(),
Query = lists:concat(["DELETE FROM ", ColumnFamily ," where KEY = '", Key ,"';"]),
case erlcassa_client:cql_execute(C, Query) of
{result, ok} ->
Query = ["DELETE FROM ", ColumnFamily ," WHERE KEY = '", Key ,"';"],
case erlcql:q(C, Query, any) of
{ok,void} ->
{ok, State};
{ok, {_Rows, _Cols}} ->
{ok, State};
Error ->
{error, Error, State}
Expand All @@ -113,14 +138,7 @@ hex(N) when N < 10 ->
$0+N;
hex(N) when N >= 10, N < 16 ->
$a+(N-10).

to_hex(N) when N < 256 ->
[hex(N div 16), hex(N rem 16)].

list_to_hexstr([]) ->
[];
list_to_hexstr([H|T]) ->
to_hex(H) ++ list_to_hexstr(T).

bin_to_hexstr(Bin) ->
lists:concat(["abcdef0123",list_to_hexstr(binary_to_list(Bin))]).
List = binary_to_list(Bin),
["0x", [ [hex(N div 16), hex(N rem 16)] || N <- List, N < 256 ] ].

0 comments on commit 80a802b

Please sign in to comment.