You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by to...@apache.org on 2017/02/09 19:13:02 UTC
[1/2] chttpd commit: updated refs/heads/master to 928bb2e
Repository: couchdb-chttpd
Updated Branches:
refs/heads/master 90648a2e0 -> 928bb2e4b
Handle error return clauses for fabric:open_revs
When calling fabric:open_revs, we don't account for situations where
the function returns an {error, any()} value as specified by the
function specification. This will account for errors thrown.
COUCHDB-3289
Project: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/commit/cd4c5c70
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/tree/cd4c5c70
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/diff/cd4c5c70
Branch: refs/heads/master
Commit: cd4c5c70c146e74a344b42e7e636e8e81f08495c
Parents: 90648a2
Author: Tony Sun <to...@cloudant.com>
Authored: Wed Feb 1 13:14:42 2017 -0800
Committer: Tony Sun <to...@cloudant.com>
Committed: Wed Feb 1 13:14:42 2017 -0800
----------------------------------------------------------------------
src/chttpd_db.erl | 37 ++++++++++++++++++++++---------------
1 file changed, 22 insertions(+), 15 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/cd4c5c70/src/chttpd_db.erl
----------------------------------------------------------------------
diff --git a/src/chttpd_db.erl b/src/chttpd_db.erl
index f3dd2ab..bb08db6 100644
--- a/src/chttpd_db.erl
+++ b/src/chttpd_db.erl
@@ -673,11 +673,10 @@ db_doc_req(#httpd{method='GET', mochi_req=MochiReq}=Req, Db, DocId) ->
Doc = couch_doc_open(Db, DocId, Rev, Options2),
send_doc(Req, Doc, Options2);
_ ->
- {ok, Results} = fabric:open_revs(Db, DocId, Revs, Options),
- case Results of
- [] when Revs == all ->
+ case fabric:open_revs(Db, DocId, Revs, Options) of
+ {ok, []} when Revs == all ->
chttpd:send_error(Req, {not_found, missing});
- _Else ->
+ {ok, Results} ->
case MochiReq:accepts_content_type("multipart/mixed") of
false ->
{ok, Resp} = start_json_response(Req, 200),
@@ -703,7 +702,9 @@ db_doc_req(#httpd{method='GET', mochi_req=MochiReq}=Req, Db, DocId) ->
end_json_response(Resp);
true ->
send_docs_multipart(Req, Results, Options)
- end
+ end;
+ {error, Error} ->
+ chttpd:send_error(Req, Error)
end
end;
@@ -722,7 +723,10 @@ db_doc_req(#httpd{method='POST', user_ctx=Ctx}=Req, Db, DocId) ->
Doc = couch_doc_from_req(Req, DocId, Json);
false ->
Rev = couch_doc:parse_rev(list_to_binary(couch_util:get_value("_rev", Form))),
- {ok, [{ok, Doc}]} = fabric:open_revs(Db, DocId, [Rev], [])
+ Doc = case fabric:open_revs(Db, DocId, [Rev], []) of
+ {ok, [{ok, Doc0}]} -> Doc0;
+ {error, Error} -> throw(Error)
+ end
end,
UpdatedAtts = [
couch_att:new([
@@ -1063,14 +1067,16 @@ couch_doc_open(#db{} = Db, DocId, Rev, Options0) ->
Error ->
throw(Error)
end;
- _ -> % open a specific rev (deletions come back as stubs)
- case fabric:open_revs(Db, DocId, [Rev], Options) of
- {ok, [{ok, Doc}]} ->
- Doc;
- {ok, [{{not_found, missing}, Rev}]} ->
- throw(not_found);
- {ok, [Else]} ->
- throw(Else)
+ _ -> % open a specific rev (deletions come back as stubs)
+ case fabric:open_revs(Db, DocId, [Rev], Options) of
+ {ok, [{ok, Doc}]} ->
+ Doc;
+ {ok, [{{not_found, missing}, Rev}]} ->
+ throw(not_found);
+ {ok, [Else]} ->
+ throw(Else);
+ {error, Error} ->
+ throw(Error)
end
end.
@@ -1235,7 +1241,8 @@ db_attachment_req(#httpd{method=Method, user_ctx=Ctx}=Req, Db, DocId, FileNamePa
Rev ->
case fabric:open_revs(Db, DocId, [Rev], [{user_ctx,Ctx}]) of
{ok, [{ok, Doc0}]} -> Doc0;
- {ok, [Error]} -> throw(Error)
+ {ok, [Error]} -> throw(Error);
+ {error, Error} -> throw(Error)
end
end,
[2/2] chttpd commit: updated refs/heads/master to 928bb2e
Posted by to...@apache.org.
Introduce 503 error when nodes are all unavailable.
In rare situations when all nodes are down or in maintenance mode and
no workers can service a request, we return a 503 to the user.
COUCHDB-3289
Project: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/commit/928bb2e4
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/tree/928bb2e4
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/diff/928bb2e4
Branch: refs/heads/master
Commit: 928bb2e4b9e542a8fd37202493af96a7e20f44cb
Parents: cd4c5c7
Author: Tony Sun <to...@cloudant.com>
Authored: Wed Feb 1 13:23:26 2017 -0800
Committer: Tony Sun <to...@cloudant.com>
Committed: Thu Feb 9 11:03:21 2017 -0800
----------------------------------------------------------------------
src/chttpd.erl | 3 +
test/chttpd_open_revs_error_test.erl | 105 ++++++++++++++++++++++++++++++
2 files changed, 108 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/928bb2e4/src/chttpd.erl
----------------------------------------------------------------------
diff --git a/src/chttpd.erl b/src/chttpd.erl
index e2ab054..cdf6e8d 100644
--- a/src/chttpd.erl
+++ b/src/chttpd.erl
@@ -894,6 +894,9 @@ error_info(request_entity_too_large) ->
error_info({error, security_migration_updates_disabled}) ->
{503, <<"security_migration">>, <<"Updates to security docs are disabled during "
"security migration.">>};
+error_info(all_workers_died) ->
+ {503, <<"service unvailable">>, <<"Nodes are unable to service this "
+ "request due to overloading or maintenance mode.">>};
error_info(not_implemented) ->
{501, <<"not_implemented">>, <<"this feature is not yet implemented">>};
error_info(timeout) ->
http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/928bb2e4/test/chttpd_open_revs_error_test.erl
----------------------------------------------------------------------
diff --git a/test/chttpd_open_revs_error_test.erl b/test/chttpd_open_revs_error_test.erl
new file mode 100644
index 0000000..5b26b5c
--- /dev/null
+++ b/test/chttpd_open_revs_error_test.erl
@@ -0,0 +1,105 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+% http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(chttpd_open_revs_error_test).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+-define(USER, "chttpd_db_test_admin").
+-define(PASS, "pass").
+-define(AUTH, {basic_auth, {?USER, ?PASS}}).
+-define(CONTENT_JSON, {"Content-Type", "application/json"}).
+-define(CONTENT_MULTI_FORM, {"Content-Type",
+ "multipart/form-data;boundary=\"bound\""}).
+
+setup() ->
+ ok = config:set("admins", ?USER, ?PASS, _Persist=false),
+ TmpDb = ?tempdb(),
+ Addr = config:get("chttpd", "bind_address", "127.0.0.1"),
+ Port = mochiweb_socket_server:get(chttpd, port),
+ Url = lists:concat(["http://", Addr, ":", Port, "/", ?b2l(TmpDb)]),
+ mock(fabric),
+ create_db(Url),
+ Url.
+
+teardown(Url) ->
+ delete_db(Url),
+ (catch meck:unload(fabric)),
+ ok = config:delete("admins", ?USER, _Persist=false).
+
+create_db(Url) ->
+ {ok, Status, _, _} = test_request:put(Url, [?CONTENT_JSON, ?AUTH], "{}"),
+ ?assert(Status =:= 201 orelse Status =:= 202).
+
+
+create_doc(Url, Id) ->
+ test_request:put(Url ++ "/" ++ Id,
+ [?CONTENT_JSON, ?AUTH], "{\"mr\": \"rockoartischocko\"}").
+
+delete_db(Url) ->
+ {ok, 200, _, _} = test_request:delete(Url, [?AUTH]).
+
+open_revs_error_test_() ->
+ {
+ "open revs error tests",
+ {
+ setup,
+ fun chttpd_test_util:start_couch/0, fun chttpd_test_util:stop_couch/1,
+ {
+ foreach,
+ fun setup/0, fun teardown/1,
+ [
+ fun should_return_503_error_for_open_revs_get/1,
+ fun should_return_503_error_for_open_revs_post_form/1
+ ]
+ }
+ }
+ }.
+
+should_return_503_error_for_open_revs_get(Url) ->
+ {ok, _, _, Body} = create_doc(Url, "testdoc"),
+ {Json} = ?JSON_DECODE(Body),
+ Ref = couch_util:get_value(<<"rev">>, Json, undefined),
+ mock_open_revs({error, all_workers_died}),
+ {ok, Code, _, _} = test_request:get(Url ++
+ "/testdoc?rev=" ++ ?b2l(Ref), [?AUTH]),
+ ?_assertEqual(503, Code).
+
+should_return_503_error_for_open_revs_post_form(Url) ->
+ Port = mochiweb_socket_server:get(chttpd, port),
+ Host = lists:concat([ "http://127.0.0.1:", Port]),
+ Referer = {"Referer", Host},
+ Body1 = "{\"body\":\"This is a body.\"}",
+ DocBeg = "--bound\r\nContent-Disposition: form-data; name=\"_doc\"\r\n\r\n",
+ DocRev = "--bound\r\nContent-Disposition: form-data; name=\"_rev\"\r\n\r\n",
+ DocRest = "\r\n--bound\r\nContent-Disposition:"
+ "form-data; name=\"_attachments\"; filename=\"file.txt\"\r\n"
+ "Content-Type: text/plain\r\n\r\ncontents of file.txt\r\n\r\n"
+ "--bound--",
+ Doc1 = lists:concat([DocBeg, Body1, DocRest]),
+ {ok, _, _, ResultBody} = test_request:post(Url ++ "/" ++ "RevDoc",
+ [?CONTENT_MULTI_FORM, ?AUTH, Referer], Doc1),
+ {Json} = ?JSON_DECODE(ResultBody),
+ Ref = couch_util:get_value(<<"rev">>, Json, undefined),
+ Doc2 = lists:concat([DocRev, ?b2l(Ref) , DocRest]),
+
+ mock_open_revs({error, all_workers_died}),
+ {ok, Code, _, ResultBody1} = test_request:post(Url ++ "/" ++ "RevDoc",
+ [?CONTENT_MULTI_FORM, ?AUTH, Referer], Doc2),
+ ?_assertEqual(503, Code).
+
+mock_open_revs(RevsResp) ->
+ ok = meck:expect(fabric, open_revs, fun(_, _, _, _) -> RevsResp end).
+
+mock(fabric) ->
+ ok = meck:new(fabric, [passthrough]).