Skip to content

Commit

Permalink
Add dbname to mango exec stats (#4990)
Browse files Browse the repository at this point in the history
* WIP: include dbname in mango exec stats

* Don't output dbname in exec stats http response

* Make sure mango nouveau logs reports

* Test that mango reports get the dbname in the stats

* With compliant formatting

* Switch to adding exec stats dbname during create

* Add stats to explain cursor

* Add stats to mango cursor special

* Fix mango special cursor test
  • Loading branch information
chewbranca authored Mar 5, 2024
1 parent 4643fec commit ab2cf16
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 33 deletions.
4 changes: 3 additions & 1 deletion src/mango/src/mango_cursor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ create_cursor(Db, {[], Trace0}, Selector, Opts) ->
Skip = couch_util:get_value(skip, Opts, 0),
Fields = couch_util:get_value(fields, Opts, all_fields),
Bookmark = couch_util:get_value(bookmark, Opts),
Stats = mango_execution_stats:stats_init(couch_db:name(Db)),
{ok, #cursor{
db = Db,
index = none,
Expand All @@ -433,7 +434,8 @@ create_cursor(Db, {[], Trace0}, Selector, Opts) ->
limit = Limit,
skip = Skip,
fields = Fields,
bookmark = Bookmark
bookmark = Bookmark,
execution_stats = Stats
}};
create_cursor(Db, {Indexes, Trace0}, Selector, Opts) ->
Trace1 = maps:merge(Trace0, #{filtered_indexes => sets:from_list(Indexes)}),
Expand Down
2 changes: 1 addition & 1 deletion src/mango/src/mango_cursor.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
fields = undefined,
user_fun,
user_acc,
execution_stats = #execution_stats{},
execution_stats,
bookmark,
bookmark_docid,
bookmark_key
Expand Down
7 changes: 6 additions & 1 deletion src/mango/src/mango_cursor_nouveau.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ create(Db, {Indexes, Trace}, Selector, Opts) ->
Skip = couch_util:get_value(skip, Opts, 0),
Fields = couch_util:get_value(fields, Opts, all_fields),

Stats = mango_execution_stats:stats_init(couch_db:name(Db)),

{ok, #cursor{
db = Db,
index = Index,
Expand All @@ -62,7 +64,8 @@ create(Db, {Indexes, Trace}, Selector, Opts) ->
opts = Opts,
limit = Limit,
skip = Skip,
fields = Fields
fields = Fields,
execution_stats = Stats
}}.

explain(Cursor) ->
Expand Down Expand Up @@ -128,6 +131,8 @@ execute(Cursor, UserFun, UserAcc) ->
{FinalUserAcc0, Stats1} = mango_execution_stats:maybe_add_stats(
Opts, UserFun, Stats0, FinalUserAcc
),
%% This needs Stats1 as log_end is called in maybe_add_stats
mango_execution_stats:log_stats(Stats1),
FinalUserAcc1 = mango_cursor:maybe_add_warning(UserFun, Cursor, Stats1, FinalUserAcc0),
{ok, FinalUserAcc1}
end.
Expand Down
22 changes: 19 additions & 3 deletions src/mango/src/mango_cursor_special.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ create(Db, {Indexes, Trace0}, Selector, Opts) ->
Skip = couch_util:get_value(skip, Opts, 0),
Fields = couch_util:get_value(fields, Opts, all_fields),
Bookmark = couch_util:get_value(bookmark, Opts),
Stats = mango_execution_stats:stats_init(couch_db:name(Db)),

IndexRanges1 = mango_cursor:maybe_noop_range(Selector, IndexRanges),
Trace = maps:merge(Trace0, #{sorted_index_ranges => SortedIndexRanges}),
Expand All @@ -59,7 +60,8 @@ create(Db, {Indexes, Trace0}, Selector, Opts) ->
limit = Limit,
skip = Skip,
fields = Fields,
bookmark = Bookmark
bookmark = Bookmark,
execution_stats = Stats
}}.

explain(Cursor) ->
Expand All @@ -74,13 +76,26 @@ handle_message(Msg, Cursor) ->
-ifdef(TEST).
-include_lib("couch/include/couch_eunit.hrl").

create_test() ->
create_test_() ->
{
foreach,
fun() ->
meck:expect(couch_db, name, fun(A) when is_atom(A) -> atom_to_binary(A) end)
end,
fun(_) -> meck:unload() end,
[
?TDEF_FE(t_create)
]
}.

t_create(_) ->
Index = #idx{type = <<"special">>, def = all_docs},
Indexes = [Index],
Ranges = [{'$gt', null, '$lt', mango_json_max}],
Trace = #{},
Selector = {[]},
Options = [{limit, limit}, {skip, skip}, {fields, fields}, {bookmark, bookmark}],
Stats = mango_execution_stats:stats_init(couch_db:name(db)),
Cursor =
#cursor{
db = db,
Expand All @@ -92,7 +107,8 @@ create_test() ->
skip = skip,
fields = fields,
bookmark = bookmark,
trace = #{sorted_index_ranges => [{Index, Ranges, 0}]}
trace = #{sorted_index_ranges => [{Index, Ranges, 0}]},
execution_stats = Stats
},
?assertEqual({ok, Cursor}, create(db, {Indexes, Trace}, Selector, Options)).

Expand Down
25 changes: 17 additions & 8 deletions src/mango/src/mango_cursor_text.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ create(Db, {Indexes, Trace}, Selector, Opts0) ->
?MANGO_ERROR(multiple_text_indexes)
end,

Opts = unpack_bookmark(couch_db:name(Db), Opts0),
DbName = couch_db:name(Db),
Opts = unpack_bookmark(DbName, Opts0),
Stats = mango_execution_stats:stats_init(DbName),

DreyfusLimit = get_dreyfus_limit(),
Limit = erlang:min(DreyfusLimit, couch_util:get_value(limit, Opts, mango_opts:default_limit())),
Expand All @@ -66,7 +68,8 @@ create(Db, {Indexes, Trace}, Selector, Opts0) ->
opts = Opts,
limit = Limit,
skip = Skip,
fields = Fields
fields = Fields,
execution_stats = Stats
}}.

explain(Cursor) ->
Expand Down Expand Up @@ -389,36 +392,42 @@ t_create_regular(_) ->
Limit = 10,
Options = [{limit, Limit}, {skip, skip}, {fields, fields}, {bookmark, bookmark}],
Options1 = [{limit, Limit}, {skip, skip}, {fields, fields}, {bookmark, unpacked_bookmark}],
Db = db,
Stats = mango_execution_stats:stats_init(couch_db:name(Db)),
Cursor = #cursor{
db = db,
db = Db,
index = Index,
ranges = null,
trace = Trace,
selector = selector,
opts = Options1,
limit = Limit,
skip = skip,
fields = fields
fields = fields,
execution_stats = Stats
},
meck:expect(dreyfus_bookmark, unpack, [db_name, bookmark], meck:val(unpacked_bookmark)),
?assertEqual({ok, Cursor}, create(db, {Indexes, Trace}, selector, Options)).
?assertEqual({ok, Cursor}, create(Db, {Indexes, Trace}, selector, Options)).

t_create_no_bookmark(_) ->
Limit = 99,
Options = [{limit, Limit}, {skip, skip}, {fields, fields}, {bookmark, nil}],
Options1 = [{limit, Limit}, {skip, skip}, {fields, fields}, {bookmark, []}],
Db = db,
Stats = mango_execution_stats:stats_init(couch_db:name(Db)),
Cursor = #cursor{
db = db,
db = Db,
index = index,
ranges = null,
trace = trace,
selector = selector,
opts = Options1,
limit = Limit,
skip = skip,
fields = fields
fields = fields,
execution_stats = Stats
},
?assertEqual({ok, Cursor}, create(db, {[index], trace}, selector, Options)).
?assertEqual({ok, Cursor}, create(Db, {[index], trace}, selector, Options)).

t_create_invalid_bookmark(_) ->
Options = [{bookmark, invalid}],
Expand Down
Loading

0 comments on commit ab2cf16

Please sign in to comment.