You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by wi...@apache.org on 2020/01/07 08:01:41 UTC

[couchdb] 01/04: Refactor Mango warning generation

This is an automated email from the ASF dual-hosted git repository.

willholley pushed a commit to branch mango_metrics
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 2b69e489aa52834e1ce2e54d7294f188805fdf72
Author: Will Holley <wi...@gmail.com>
AuthorDate: Mon Jan 6 09:22:56 2020 +0000

    Refactor Mango warning generation
    
    Refactor the code generating Mango warnings to (hopefully!)
    improve readability.
    
    This also moves the metric counter for unindexed queries such that it
    gets incremented only when no index is used. Previously, it would
    increment if *any* warning was generated (e.g. when Mango warned that
    the user-specified index was not found/valid but fell back to a
    different index).
    
    The warning string returned now also contains all warnings generated
    for the query, delimited via newlines - not just the first one.
---
 src/mango/src/mango_cursor.erl      | 60 +++++++++++++++++++++++--------------
 src/mango/src/mango_cursor_view.erl | 10 +++----
 2 files changed, 43 insertions(+), 27 deletions(-)

diff --git a/src/mango/src/mango_cursor.erl b/src/mango/src/mango_cursor.erl
index dc2ee74..03be4ee 100644
--- a/src/mango/src/mango_cursor.erl
+++ b/src/mango/src/mango_cursor.erl
@@ -114,7 +114,7 @@ filter_indexes(Indexes0, DesignId, ViewName) ->
 
 
 remove_indexes_with_partial_filter_selector(Indexes) ->
-    FiltFun = fun(Idx) -> 
+    FiltFun = fun(Idx) ->
         case mango_idx:get_partial_filter_selector(Idx) of
             undefined -> true;
             _ -> false
@@ -146,44 +146,60 @@ group_indexes_by_type(Indexes) ->
     end, ?CURSOR_MODULES).
 
 
-maybe_add_warning(UserFun, #cursor{index = Index, opts = Opts}, UserAcc) ->
-    NoIndexWarning = case Index#idx.type of
+% warn if the _all_docs index was used to fulfil a query
+no_index_warning(Index) ->
+    case Index#idx.type of
         <<"special">> ->
-            <<"no matching index found, create an index to optimize query time">>;
+            couch_stats:increment_counter([mango, unindexed_queries]),
+            [<<"no matching index found, create an index to optimize query time.">>];
         _ ->
-            ok
-    end,
+            []
+    end.
 
-    UseIndexInvalidWarning = case lists:keyfind(use_index, 1, Opts) of
-        {use_index, []} ->
-            NoIndexWarning;
+
+% warn if user specified an index which doesn't exist or isn't valid
+% for the selector.
+% In this scenario, Mango will ignore the index hint and auto-select an index.
+invalid_index_warning(Index, Opts) ->
+    case lists:keyfind(use_index, 1, Opts) of
         {use_index, [DesignId]} ->
             case filter_indexes([Index], DesignId) of
                 [] ->
-                    fmt("_design/~s was not used because it does not contain a valid index for this query.", 
-                        [ddoc_name(DesignId)]);
+                    couch_stats:increment_counter([mango, query_invalid_index]),
+                    Reason = fmt("_design/~s was not used because it does not contain a valid index for this query.",
+                        [ddoc_name(DesignId)]),
+                    [Reason];
                 _ ->
-                    NoIndexWarning
+                    []
             end;
         {use_index, [DesignId, ViewName]} ->
             case filter_indexes([Index], DesignId, ViewName) of
                 [] ->
-                    fmt("_design/~s, ~s was not used because it is not a valid index for this query.", 
-                        [ddoc_name(DesignId), ViewName]);
+                    couch_stats:increment_counter([mango, query_invalid_index]),
+                    Reason = fmt("_design/~s, ~s was not used because it is not a valid index for this query.",
+                        [ddoc_name(DesignId), ViewName]),
+                    [Reason];
                 _ ->
-                    NoIndexWarning
-            end
-    end,
+                    []
+            end;
+        _ ->
+            []
+    end.
+
 
-    maybe_add_warning_int(UseIndexInvalidWarning, UserFun, UserAcc).
+maybe_add_warning(UserFun, #cursor{index = Index, opts = Opts}, Stats, UserAcc) ->
+    W0 = invalid_index_warning(Index, Opts),
+    W1 = no_index_warning(Index),
+    Warnings = lists:append([W0, W1]),
+    maybe_add_warning_int(Warnings, UserFun, UserAcc).
 
 
-maybe_add_warning_int(ok, _, UserAcc) ->
+maybe_add_warning_int([], _, UserAcc) ->
    UserAcc;
 
-maybe_add_warning_int(Warning, UserFun, UserAcc) ->
-    couch_stats:increment_counter([mango, unindexed_queries]),
-    Arg = {add_key, warning, Warning},
+maybe_add_warning_int(Warnings, UserFun, UserAcc) ->
+    WarningStr = lists:join(<<"\n">>, Warnings),
+    Arg = {add_key, warning, WarningStr},
     {_Go, UserAcc0} = UserFun(Arg, UserAcc),
     UserAcc0.
 
diff --git a/src/mango/src/mango_cursor_view.erl b/src/mango/src/mango_cursor_view.erl
index 1c4b342..9f5e6ec 100644
--- a/src/mango/src/mango_cursor_view.erl
+++ b/src/mango/src/mango_cursor_view.erl
@@ -44,7 +44,7 @@ create(Db, Indexes, Selector, Opts) ->
     Limit = couch_util:get_value(limit, Opts, mango_opts:default_limit()),
     Skip = couch_util:get_value(skip, Opts, 0),
     Fields = couch_util:get_value(fields, Opts, all_fields),
-    Bookmark = couch_util:get_value(bookmark, Opts), 
+    Bookmark = couch_util:get_value(bookmark, Opts),
 
     {ok, #cursor{
         db = Db,
@@ -124,7 +124,7 @@ execute(#cursor{db = Db, index = Idx, execution_stats = Stats} = Cursor0, UserFu
             BaseArgs = base_args(Cursor),
             #cursor{opts = Opts, bookmark = Bookmark} = Cursor,
             Args0 = apply_opts(Opts, BaseArgs),
-            Args = mango_json_bookmark:update_args(Bookmark, Args0), 
+            Args = mango_json_bookmark:update_args(Bookmark, Args0),
             UserCtx = couch_util:get_value(user_ctx, Opts, #user_ctx{}),
             DbOpts = [{user_ctx, UserCtx}],
             Result = case mango_idx:def(Idx) of
@@ -410,7 +410,7 @@ apply_opts([{_, _} | Rest], Args) ->
 
 
 doc_member(Cursor, RowProps) ->
-    Db = Cursor#cursor.db, 
+    Db = Cursor#cursor.db,
     Opts = Cursor#cursor.opts,
     ExecutionStats = Cursor#cursor.execution_stats,
     Selector = Cursor#cursor.selector,
@@ -460,8 +460,8 @@ is_design_doc(RowProps) ->
 
 
 update_bookmark_keys(#cursor{limit = Limit} = Cursor, Props) when Limit > 0 ->
-    Id = couch_util:get_value(id, Props), 
-    Key = couch_util:get_value(key, Props), 
+    Id = couch_util:get_value(id, Props),
+    Key = couch_util:get_value(key, Props),
     Cursor#cursor {
         bookmark_docid = Id,
         bookmark_key = Key