You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2018/07/14 15:01:13 UTC

[GitHub] janl closed pull request #1089: Extract helper to execute a function in a new process

janl closed pull request #1089: Extract helper to execute a function in a new process
URL: https://github.com/apache/couchdb/pull/1089
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl
index dbbb454cb3..9e9f896477 100644
--- a/src/chttpd/src/chttpd_db.erl
+++ b/src/chttpd/src/chttpd_db.erl
@@ -984,35 +984,11 @@ http_code_from_status(Status) ->
     end.
 
 update_doc(Db, DocId, #doc{deleted=Deleted, body=DocBody}=Doc, Options) ->
-    {_, Ref} = spawn_monitor(fun() ->
-        try fabric:update_doc(Db, Doc, Options) of
-            Resp ->
-                exit({exit_ok, Resp})
-        catch
-            throw:Reason ->
-                exit({exit_throw, Reason});
-            error:Reason ->
-                exit({exit_error, Reason});
-            exit:Reason ->
-                exit({exit_exit, Reason})
-        end
-    end),
-    Result = receive
-        {'DOWN', Ref, _, _, {exit_ok, Ret}} ->
-            Ret;
-        {'DOWN', Ref, _, _, {exit_throw, Reason}} ->
-            throw(Reason);
-        {'DOWN', Ref, _, _, {exit_error, Reason}} ->
-            erlang:error(Reason);
-        {'DOWN', Ref, _, _, {exit_exit, Reason}} ->
-            erlang:exit(Reason)
-    end,
-
-    case Result of
-    {ok, NewRev} ->
-        Accepted = false;
-    {accepted, NewRev} ->
-        Accepted = true
+    Accepted = case fabric_util:defer(fabric, update_doc, [Db, Doc, Options]) of
+        {ok, NewRev} ->
+            false;
+        {accepted, NewRev} ->
+            true
     end,
     Etag = couch_httpd:doc_etag(DocId, DocBody, NewRev),
     Status = case {Accepted, Deleted} of
diff --git a/src/fabric/src/fabric_util.erl b/src/fabric/src/fabric_util.erl
index 49f4c8913f..f61e22acca 100644
--- a/src/fabric/src/fabric_util.erl
+++ b/src/fabric/src/fabric_util.erl
@@ -20,6 +20,7 @@
 -export([log_timeout/2, remove_done_workers/2]).
 -export([is_users_db/1, is_replicator_db/1, fake_db/2]).
 -export([upgrade_mrargs/1]).
+-export([defer/3]).
 
 -compile({inline, [{doc_id_and_rev,1}]}).
 
@@ -264,6 +265,29 @@ create_monitors(Shards) ->
     ]),
     rexi_monitor:start(MonRefs).
 
+
+defer(Mod, Fun, Args) ->
+    {Pid, Ref} = erlang:spawn_monitor(?MODULE, do_defer, [Mod, Fun, Args]),
+    receive
+        {'DOWN', Ref, process, Pid, {defer_ok, Value}} ->
+            Value;
+        {'DOWN', Ref, process, Pid, {defer_failed, Class, Reason, Stack}} ->
+            erlang:raise(Class, Reason, Stack)
+    end.
+
+
+do_defer(Mod, Fun, Args) ->
+    try erlang:apply(Mod, Fun, Args) of
+        Resp ->
+            erlang:exit({defer_ok, Resp})
+    catch
+        Class:Reason ->
+            Stack = erlang:get_stacktrace(),
+            couch_log:error("Defered error: ~w~n    ~p", [{Class, Reason}, Stack]),
+            erlang:exit({defer_failed, Class, Reason, Stack})
+    end.
+
+
 %% verify only id and rev are used in key.
 update_counter_test() ->
     Reply = {ok, #doc{id = <<"id">>, revs = <<"rev">>,
diff --git a/src/mango/src/mango_cursor_view.erl b/src/mango/src/mango_cursor_view.erl
index 1e2108b7d7..6eff5cb3d9 100644
--- a/src/mango/src/mango_cursor_view.erl
+++ b/src/mango/src/mango_cursor_view.erl
@@ -340,7 +340,7 @@ doc_member(Db, RowProps, Opts, ExecutionStats) ->
         undefined ->
             ExecutionStats1 = mango_execution_stats:incr_quorum_docs_examined(ExecutionStats),
             Id = couch_util:get_value(id, RowProps),
-            case mango_util:defer(fabric, open_doc, [Db, Id, Opts]) of
+            case fabric_util:defer(fabric, open_doc, [Db, Id, Opts]) of
                 {ok, #doc{}=Doc} ->
                     {ok, couch_doc:to_json_obj(Doc, []), {execution_stats, ExecutionStats1}};
                 Else ->
diff --git a/src/mango/src/mango_util.erl b/src/mango/src/mango_util.erl
index a7347178e9..25a6c369f7 100644
--- a/src/mango/src/mango_util.erl
+++ b/src/mango/src/mango_util.erl
@@ -19,9 +19,6 @@
     load_ddoc/2,
     load_ddoc/3,
 
-    defer/3,
-    do_defer/3,
-
     assert_ejson/1,
 
     to_lower/1,
@@ -85,7 +82,7 @@ open_doc(Db, DocId) ->
 
 
 open_doc(Db, DocId, Options) ->
-    case mango_util:defer(fabric, open_doc, [Db, DocId, Options]) of
+    case fabric_util:defer(fabric, open_doc, [Db, DocId, Options]) of
         {ok, Doc} ->
             {ok, Doc};
         {not_found, _} ->
@@ -96,7 +93,7 @@ open_doc(Db, DocId, Options) ->
 
 
 open_ddocs(Db) ->
-    case mango_util:defer(fabric, design_docs, [Db]) of
+    case fabric_util:defer(fabric, design_docs, [Db]) of
         {ok, Docs} ->
             {ok, Docs};
         _ ->
@@ -119,40 +116,6 @@ load_ddoc(Db, DDocId, DbOpts) ->
     end.
 
 
-defer(Mod, Fun, Args) ->
-    {Pid, Ref} = erlang:spawn_monitor(?MODULE, do_defer, [Mod, Fun, Args]),
-    receive
-        {'DOWN', Ref, process, Pid, {mango_defer_ok, Value}} ->
-            Value;
-        {'DOWN', Ref, process, Pid, {mango_defer_throw, Value}} ->
-            erlang:throw(Value);
-        {'DOWN', Ref, process, Pid, {mango_defer_error, Value}} ->
-            erlang:error(Value);
-        {'DOWN', Ref, process, Pid, {mango_defer_exit, Value}} ->
-            erlang:exit(Value)
-    end.
-
-
-do_defer(Mod, Fun, Args) ->
-    try erlang:apply(Mod, Fun, Args) of
-        Resp ->
-            erlang:exit({mango_defer_ok, Resp})
-    catch
-        throw:Error ->
-            Stack = erlang:get_stacktrace(),
-            couch_log:error("Defered error: ~w~n    ~p", [{throw, Error}, Stack]),
-            erlang:exit({mango_defer_throw, Error});
-        error:Error ->
-            Stack = erlang:get_stacktrace(),
-            couch_log:error("Defered error: ~w~n    ~p", [{error, Error}, Stack]),
-            erlang:exit({mango_defer_error, Error});
-        exit:Error ->
-            Stack = erlang:get_stacktrace(),
-            couch_log:error("Defered error: ~w~n    ~p", [{exit, Error}, Stack]),
-            erlang:exit({mango_defer_exit, Error})
-    end.
-
-
 assert_ejson({Props}) ->
     assert_ejson_obj(Props);
 assert_ejson(Vals) when is_list(Vals) ->


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services