Skip to content

Instantly share code, notes, and snippets.

@angrycub
Last active October 17, 2017 10:22
Show Gist options
  • Save angrycub/97ac6bed08c3ceba2588 to your computer and use it in GitHub Desktop.
Save angrycub/97ac6bed08c3ceba2588 to your computer and use it in GitHub Desktop.
Helper Shell Funs for Riak CS Cleanup
%% Shell functions that can be run in `riak-cs attach`
%% Peek({S3Bucket, Key}) will fetch an object and display the sibling count and
%% the number of manifests in the object.
Peek = fun({Bucket, Key}) ->
io:format("Peeking at ~p/~p.~n", [Bucket, Key]),
{ok, RcPid} = riak_cs_riak_client:checkout(),
ManifestBucket = riak_cs_utils:to_bucket_name(objects, Bucket),
ok = riak_cs_riak_client:set_bucket_name(RcPid, Bucket),
{ok, ManifestPbc} = riak_cs_riak_client:manifest_pbc(RcPid),
case riakc_pb_socket:get(ManifestPbc, ManifestBucket, Key, [{r, all}]) of
{ok, RiakObject} ->
Manifests = riak_cs_manifest:manifests_from_riak_object(RiakObject),
io:format("Value Count: ~p. Length : ~p.~n", [riakc_obj:value_count(RiakObject), length(Manifests)]);
{error, notfound} ->
io:format("Not found ~p/~p.~n", [Bucket, Key]);
{error, Reason} ->
io:format("Unhandled error in ~p/~p. [~p]~n", [Bucket, Key, Reason])
end,
riak_cs_riak_client:checkin(RcPid)
end.
%% GC_all_manifests({S3Bucket, Key}) will go through all manifests with in the object and
%% set them to be garbage collected.
GC_all_manifests=fun({Bucket, Key}) ->
{ok, RcPid} = riak_cs_riak_client:checkout(),
{ok, Obj, _} = riak_cs_manifest:get_manifests(RcPid, Bucket, Key),
List = [ B || {B,M} <- lists:flatten([ binary_to_term(V) || V <- riakc_obj:get_values(Obj)])],
Res = riak_cs_gc:gc_specific_manifests(List, Obj, Bucket, Key, RcPid),
riak_cs_riak_client:checkin(RcPid),
Res
end.
%% Expunge_manifest({Bucket, Key}) will forceably remove a RiakCS Manifest object
%% from the supportive Riak backend.
Expunge_manifest = fun({Bucket, Key}) ->
io:format("Killing ~p/~p.~n", [Bucket, Key]),
{ok, RcPid} = riak_cs_riak_client:checkout(),
ManifestBucket = riak_cs_utils:to_bucket_name(objects, Bucket),
ok = riak_cs_riak_client:set_bucket_name(RcPid, Bucket),
{ok, ManifestPbc} = riak_cs_riak_client:manifest_pbc(RcPid),
case riakc_pb_socket:delete(ManifestPbc, ManifestBucket, Key) of
ok ->
io:format("Killed dead.~n", []);
{error, notfound} ->
io:format("Not found ~p/~p.~n", [Bucket, Key]);
{error, Reason} ->
io:format("Unhandled error in ~p/~p. [~p]~n", [Bucket, Key, Reason])
end,
riak_cs_riak_client:checkin(RcPid)
end.
%% Force_remove({Bucket, Key}) will inspect the object, gc all of the manifests,
%% delete the manifest object from Riak, and verify that it is gone.
Force_remove = fun(BKey = {Bucket, Key}) ->
Peek(BKey),
GC_all_manifests(BKey),
Expunge_manifest(BKey),
Peek(BKey)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment