Riak pre-commit hook crashing unexpectedly

David Rogers predictivestatmech at gmail.com
Sun Dec 6 22:34:43 EST 2015


I'm using riak {release,"riak","2.1.0","5.10.3",
"$HOME/src/riak-2.1.1/rel/riak/lib/kernel-2.16.3"} ...}
on a cluster with 1 Linux 3.16.0-38 and 4 OSX machines.

I have installed the following pre-commit hook (see end of email) to 
validate protobuf-formatted values whose key should be the sha1-hash of 
part of their payload.

     props = 

I attached the failed data element.  I'm using the riak-c-client to 

   Although almost everything I've added to the bucket this way has 
worked so far, a few (like the sample I sent) fail, with riak_put 
returning only the unhelpful ERIAK_SERVER_ERROR ("An error was returned 
from the server").  This happens even though manually running the 
validation works fine.  It is consistently reproducible on this input.

   In my attempt to track down the error, I added a test at the end of 
riak_sync_request (below) that will log all server errors.  The only 
problem is that cfg->log_fn is always NULL when riak_log_error is 
called, rather than what I set it to initially with 
riak_config_set_logging!  Gdb can't seem to catch any writes there, so 
maybe cfg gets incompletely copied somewhere?

   Anyway, I can get out the error message from the debugger.

riak_sync_request (rop_target=rop_target at entry=0x7fffffffe0b0, 
response=response at entry=0x7fffffffe130)
     at src/riak.c:81
81            riak_log_error(cxn, "%.*s\n", (int)msg->len, msg->data);
(gdb) print msg->data
$13 = (riak_uint8_t *) 0x670a40 

so the server error shows only that the precommit hook 
(validate_hash:validate/1) crashes.  Again, manually running doesn't 
crash it...


   First, I don't understand why riak-c-client doesn't call the function 
I supplied to riak_config_set_logging.  Second, I don't know where to 
look for more details on why the pre-commit hook crashes only when I 
actually try to add this particular key/value. Can anyone spot a problem 
in its error handling or suggest a way to debug?

Note: I get slightly more info. when posting from curl,
$ curl -XPOST$hash -H 
'Content-Type: application/octet-stream' --data-binary @"$hash"
<html><head><title>500 Internal Server 
Error</title></head><body><h1>Internal Server Error</h1>The server 
encountered an error while processing this request:<br><pre>{error,
web server</ADDRESS></body></html>

~ David M. Rogers

message sign {
     required bytes  signer  = 1;
     required uint32 dig_alg = 2;
     required bytes  sign    = 3;
     required uint64 ctime   = 4;
     optional uint32 flags   = 5;

     optional bytes  obj     = 10;

-export([validate/1, ck_hash/2, start/0]).

-author("David M. Rogers <predictivestatmech at gmail.com>").

%load_proto() ->
%    protobuffs_compile:scan_file("signed.proto").

validate(Object) ->
     dict:is_key(<<"X-Riak-Deleted">>, riak_object:get_metadata(Object)) of
         true -> Object;
         false -> correct_hash(Object)
     error:Error ->
       {fail, "Invalid Commit: " ++ 
binary_to_list(list_to_binary(io_lib:format("~p", [Error])))}

correct_hash(Object) ->
     Msg = term_to_binary(riak_object:get_value(Object)),
     Hash = binary_to_list(riak_object:key(Object)),
     case ck_hash(Hash, Msg) of
         true  -> Object;
         false -> {fail, "Invalid Commit: Bad hash value."}

%% Hash : string of 40 hex chars
%% Msg : protocol buffer sign message (signed.proto)
%%  returns bool
ck_hash(Hash, Msg) ->
     Obj = signed_pb:decode_sign(Msg), %% signed and sobject come from 
file naming
     Digest = crypto:hash(sha, element(7,Obj)),
     bin_to_hexstr(Digest) == Hash

bin_to_hexstr(Bin) ->
       lists:flatten([io_lib:format("~2.16.0b", [X]) ||
                          X <- binary_to_list(Bin)]).

hexstr_to_bin(S) ->
       hexstr_to_bin(S, []).
hexstr_to_bin([], Acc) ->
hexstr_to_bin([X,Y|T], Acc) ->
       {ok, [V], []} = io_lib:fread("~16u", [X,Y]),
         hexstr_to_bin(T, [V | Acc]).

start() ->
     Hash = "fc144f6728c994a10309a922d0fd9758b9999cc4",
     {ok, Msg} = file:read_file(Hash),
     Ret = ck_hash(Hash, Msg),
     io:fwrite( "Returned: ~p.~n", [Ret] ).

-------------- next part --------------
A non-text attachment was scrubbed...
Name: fc144f6728c994a10309a922d0fd9758b9999cc4
Type: application/octet-stream
Size: 747 bytes
Desc: not available
URL: <http://lists.basho.com/pipermail/riak-users_lists.basho.com/attachments/20151206/8c3ce183/attachment.obj>

More information about the riak-users mailing list