Skip to content

Commit

Permalink
Merge pull request #17 from cloudant-labs/87336-add-disk_size-end-point
Browse files Browse the repository at this point in the history
Add new end point to get disk size information for search index
  • Loading branch information
brkolla authored Jun 6, 2017
2 parents 5fbbe3e + bc2f94b commit d838881
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/clouseau_rpc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
-export([await/2, commit/2, get_update_seq/1, info/1, search/6, search/2]).
-export([group1/7, group2/8, group2/2]).
-export([delete/2, update/3, cleanup/1, cleanup/2]).
-export([analyze/2, version/0]).
-export([analyze/2, version/0, disk_size/1]).

open_index(Peer, Path, Analyzer) ->
rpc({main, clouseau()}, {open, Peer, Path, Analyzer}).

disk_size(Path) ->
rpc({main, clouseau()}, {disk_size, Path}).

await(Ref, MinSeq) ->
rpc(Ref, {await, MinSeq}).

Expand Down
10 changes: 5 additions & 5 deletions src/dreyfus_fabric_info.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
-include_lib("mem3/include/mem3.hrl").
-include_lib("couch/include/couch_db.hrl").

-export([go/3]).
-export([go/4]).

go(DbName, DDocId, IndexName) when is_binary(DDocId) ->
go(DbName, DDocId, IndexName, InfoLevel) when is_binary(DDocId) ->
{ok, DDoc} = fabric:open_doc(DbName, <<"_design/", DDocId/binary>>, []),
go(DbName, DDoc, IndexName);
go(DbName, DDoc, IndexName, InfoLevel);

go(DbName, DDoc, IndexName) ->
go(DbName, DDoc, IndexName, InfoLevel) ->
Shards = mem3:shards(DbName),
Workers = fabric_util:submit_jobs(Shards, dreyfus_rpc, info, [DDoc, IndexName]),
Workers = fabric_util:submit_jobs(Shards, dreyfus_rpc, InfoLevel, [DDoc, IndexName]),
RexiMon = fabric_util:create_monitors(Shards),
Acc0 = {fabric_dict:init(Workers, nil), []},
try
Expand Down
19 changes: 17 additions & 2 deletions src/dreyfus_httpd.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

-module(dreyfus_httpd).

-export([handle_search_req/3, handle_info_req/3,
-export([handle_search_req/3, handle_info_req/3, handle_disk_size_req/3,
handle_cleanup_req/2, handle_analyze_req/1]).
-include("dreyfus.hrl").
-include_lib("couch/include/couch_db.hrl").
Expand Down Expand Up @@ -108,7 +108,7 @@ handle_search_req(Req, _Db, _DDoc, _RetryCount, _RetryPause) ->

handle_info_req(#httpd{method='GET', path_parts=[_, _, _, _, IndexName]}=Req
,#db{name=DbName}, #doc{id=Id}=DDoc) ->
case dreyfus_fabric_info:go(DbName, DDoc, IndexName) of
case dreyfus_fabric_info:go(DbName, DDoc, IndexName, info) of
{ok, IndexInfoList} ->
send_json(Req, 200, {[
{name, <<Id/binary,"/",IndexName/binary>>},
Expand All @@ -122,6 +122,21 @@ handle_info_req(#httpd{path_parts=[_, _, _, _, _]}=Req, _Db, _DDoc) ->
handle_info_req(Req, _Db, _DDoc) ->
send_error(Req, {bad_request, "path not recognized"}).

handle_disk_size_req(#httpd{method='GET', path_parts=[_, _, _, _, IndexName]}=Req, #db{name=DbName}, #doc{id=Id}=DDoc) ->
case dreyfus_fabric_info:go(DbName, DDoc, IndexName, disk_size) of
{ok, IndexInfoList} ->
send_json(Req, 200, {[
{name, <<Id/binary,"/",IndexName/binary>>},
{search_index, {IndexInfoList}}
]});
{error, Reason} ->
send_error(Req, Reason)
end;
handle_disk_size_req(#httpd{path_parts=[_, _, _, _, _]}=Req, _Db, _DDoc) ->
send_method_not_allowed(Req, "GET");
handle_disk_size_req(Req, _Db, _DDoc) ->
send_error(Req, {bad_request, "path not recognized"}).

handle_cleanup_req(#httpd{method='POST'}=Req, #db{name=DbName}) ->
ok = dreyfus_fabric_cleanup:go(DbName),
send_json(Req, 202, {[{ok, true}]});
Expand Down
1 change: 1 addition & 0 deletions src/dreyfus_httpd_handlers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ db_handler(_) -> no_match.

design_handler(<<"_search">>) -> fun dreyfus_httpd:handle_search_req/3;
design_handler(<<"_search_info">>) -> fun dreyfus_httpd:handle_info_req/3;
design_handler(<<"_search_disk_size">>) -> fun dreyfus_httpd:handle_disk_size_req/3;
design_handler(_) -> no_match.
10 changes: 9 additions & 1 deletion src/dreyfus_index_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
-define(BY_PID, dreyfus_by_pid).

% public api.
-export([start_link/0, get_index/2]).
-export([start_link/0, get_index/2, get_disk_size/2]).

% gen_server api.
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
Expand All @@ -38,6 +38,9 @@ start_link() ->
get_index(DbName, Index) ->
gen_server:call(?MODULE, {get_index, DbName, Index}, infinity).

get_disk_size(DbName, Index) ->
gen_server:call(?MODULE, {get_disk_size, DbName, Index}, infinity).

% gen_server functions.

init([]) ->
Expand All @@ -61,6 +64,11 @@ handle_call({get_index, DbName, #index{sig=Sig}=Index}, From, State) ->
{reply, {ok, ExistingPid}, State}
end;

handle_call({get_disk_size, DbName, #index{sig=Sig}=Index}, From, State) ->
Path = <<DbName/binary,"/",Sig/binary>>,
Reply = clouseau_rpc:disk_size(Path),
{reply, Reply, State};

handle_call({open_ok, DbName, Sig, NewPid}, {OpenerPid, _}, State) ->
link(NewPid),
[{_, WaitList}] = ets:lookup(?BY_SIG, {DbName, Sig}),
Expand Down
13 changes: 12 additions & 1 deletion src/dreyfus_rpc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-import(couch_query_servers, [get_os_process/1, ret_os_process/1, proc_prompt/2]).

% public api.
-export([search/4, group1/4, group2/4, info/3]).
-export([search/4, group1/4, group2/4, info/3, disk_size/3]).

% private callback
-export([call/5, info_int/3]).
Expand Down Expand Up @@ -90,6 +90,17 @@ info_int(DbName, DDoc, IndexName) ->
rexi:reply(Error)
end.

disk_size(DbName, DDoc, IndexName) ->
erlang:put(io_priority, {interactive, DbName}),
check_interactive_mode(),
case dreyfus_index:design_doc_to_index(DDoc, IndexName) of
{ok, Index} ->
Result = dreyfus_index_manager:get_disk_size(DbName, Index),
rexi:reply(Result);
Error ->
rexi:reply(Error)
end.

get_or_create_db(DbName, Options) ->
case couch_db:open_int(DbName, Options) of
{not_found, no_db_file} ->
Expand Down

0 comments on commit d838881

Please sign in to comment.