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/06 09:26:31 UTC

[couchdb] branch mango_metrics created (now 2671d20)

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

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


      at 2671d20  Refactor Mango warning generation

This branch includes the following new commits:

     new 2671d20  Refactor Mango warning generation

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[couchdb] 01/01: Refactor Mango warning generation

Posted by wi...@apache.org.
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 2671d20f5a756e2d963fd9f5f1d3ec7bb5bfbafd
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).
---
 src/mango/src/mango_cursor.erl          | 58 ++++++++++++++++++++-------------
 src/mango/src/mango_cursor_text.erl     |  2 +-
 src/mango/src/mango_cursor_view.erl     | 12 +++----
 src/mango/src/mango_execution_stats.erl |  2 +-
 src/mango/src/mango_httpd.erl           |  2 +-
 5 files changed, 45 insertions(+), 31 deletions(-)

diff --git a/src/mango/src/mango_cursor.erl b/src/mango/src/mango_cursor.erl
index dc2ee74..09b95c0 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,58 @@ group_indexes_by_type(Indexes) ->
     end, ?CURSOR_MODULES).
 
 
-maybe_add_warning(UserFun, #cursor{index = Index, opts = Opts}, UserAcc) ->
-    NoIndexWarning = case Index#idx.type of
+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]),
+            {warning, <<"no matching index found, create an index to optimize query time">>};
         _ ->
-            ok
-    end,
+            {ok, []}
+    end.
 
-    UseIndexInvalidWarning = case lists:keyfind(use_index, 1, Opts) of
-        {use_index, []} ->
-            NoIndexWarning;
+
+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)]);
+                    Reason = fmt("_design/~s was not used because it does not contain a valid index for this query.",
+                        [ddoc_name(DesignId)]),
+                    {warning, Reason};
                 _ ->
-                    NoIndexWarning
+                    {ok, []}
             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]);
+                    Reason = fmt("_design/~s, ~s was not used because it is not a valid index for this query.",
+                        [ddoc_name(DesignId), ViewName]),
+                    {warning, Reason};
                 _ ->
-                    NoIndexWarning
-            end
-    end,
+                    {ok, []}
+            end;
+        _ ->
+            {ok, []}
+    end.
+
+
+maybe_add_warning(UserFun, #cursor{index = Index, opts = Opts}, UserAcc) ->
+    UseIndexInvalidWarning = invalid_index_warning(Index, Opts),
+    NoIndexWarning = no_index_warning(Index),
 
-    maybe_add_warning_int(UseIndexInvalidWarning, UserFun, UserAcc).
+    % responses only return one warning string.
+    % UseIndexInvalidWarning trumps
+    % NoIndexWarning though both may be generated for a single request
+    UserAcc0 = maybe_add_warning_int(NoIndexWarning, UserFun, UserAcc),
+    maybe_add_warning_int(UseIndexInvalidWarning, UserFun, UserAcc0).
 
 
-maybe_add_warning_int(ok, _, UserAcc) ->
+maybe_add_warning_int({ok, _}, _, 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({warning, Reason}, UserFun, UserAcc) ->
+    % overwrite existing warning if set
+    Arg = {set_key, warning, Reason},
     {_Go, UserAcc0} = UserFun(Arg, UserAcc),
     UserAcc0.
 
diff --git a/src/mango/src/mango_cursor_text.erl b/src/mango/src/mango_cursor_text.erl
index 8938f35..2e6ce4f 100644
--- a/src/mango/src/mango_cursor_text.erl
+++ b/src/mango/src/mango_cursor_text.erl
@@ -123,7 +123,7 @@ execute(Cursor, UserFun, UserAcc) ->
                 execution_stats = Stats0
             } = FinalCAcc,
             JsonBM = dreyfus_bookmark:pack(FinalBM),
-            Arg = {add_key, bookmark, JsonBM},
+            Arg = {set_key, bookmark, JsonBM},
             {_Go, FinalUserAcc} = UserFun(Arg, LastUserAcc),
             FinalUserAcc0 = mango_execution_stats:maybe_add_stats(Opts, UserFun, Stats0, FinalUserAcc),
             FinalUserAcc1 = mango_cursor:maybe_add_warning(UserFun, Cursor, FinalUserAcc0),
diff --git a/src/mango/src/mango_cursor_view.erl b/src/mango/src/mango_cursor_view.erl
index 1c4b342..b94867d 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
@@ -141,7 +141,7 @@ execute(#cursor{db = Db, index = Idx, execution_stats = Stats} = Cursor0, UserFu
             case Result of
                 {ok, LastCursor} ->
                     NewBookmark = mango_json_bookmark:create(LastCursor),
-                    Arg = {add_key, bookmark, NewBookmark},
+                    Arg = {set_key, bookmark, NewBookmark},
                     {_Go, FinalUserAcc} = UserFun(Arg, LastCursor#cursor.user_acc),
                     Stats0 = LastCursor#cursor.execution_stats,
                     FinalUserAcc0 = mango_execution_stats:maybe_add_stats(Opts, UserFun, Stats0, FinalUserAcc),
@@ -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
diff --git a/src/mango/src/mango_execution_stats.erl b/src/mango/src/mango_execution_stats.erl
index 7e8afd7..77f917f 100644
--- a/src/mango/src/mango_execution_stats.erl
+++ b/src/mango/src/mango_execution_stats.erl
@@ -86,7 +86,7 @@ maybe_add_stats(Opts, UserFun, Stats, UserAcc) ->
         true ->
             Stats0 = log_end(Stats),
             JSONValue = to_json(Stats0),
-            Arg = {add_key, execution_stats, JSONValue},
+            Arg = {set_key, execution_stats, JSONValue},
             {_Go, FinalUserAcc} = UserFun(Arg, UserAcc),
             FinalUserAcc;
         _ ->
diff --git a/src/mango/src/mango_httpd.erl b/src/mango/src/mango_httpd.erl
index 379d2e1..425ae6f 100644
--- a/src/mango/src/mango_httpd.erl
+++ b/src/mango/src/mango_httpd.erl
@@ -282,7 +282,7 @@ run_find(Resp, Db, Sel, Opts) ->
     mango_crud:find(Db, Sel, fun handle_doc/2, Acc0, Opts).
 
 
-handle_doc({add_key, Key, Value}, Acc0) ->
+handle_doc({set_key, Key, Value}, Acc0) ->
     #vacc{kvs=KVs} = Acc0,
     NewKVs = lists:keystore(Key, 1, KVs, {Key, Value}),
     {ok, Acc0#vacc{kvs = NewKVs}};