Skip to content

Commit

Permalink
address Paul's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangphcn committed Mar 24, 2020
1 parent ee295f3 commit 88cf83e
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 57 deletions.
32 changes: 0 additions & 32 deletions src/chttpd/src/chttpd_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ handle_request(#httpd{path_parts=[DbName|RestParts],method=Method}=Req)->
end;
{_, []} ->
do_db_req(Req, fun db_req/2);
{_, [<<"_restore">>|_]} ->
handle_restore_db_req(Req, DbName);
{_, [SecondPart|_]} ->
Handler = chttpd_handlers:db_handler(SecondPart, fun db_req/2),
do_db_req(Req, Handler)
Expand Down Expand Up @@ -277,12 +275,6 @@ handle_view_cleanup_req(Req, Db) ->
ok = fabric:cleanup_index_files_all_nodes(Db),
send_json(Req, 202, {[{ok, true}]}).

handle_restore_db_req(#httpd{method='POST'}=Req, DbName) ->
restore_db_req(Req, DbName);

handle_restore_db_req(Req, _DbName) ->
send_method_not_allowed(Req, "POST").

handle_partition_req(#httpd{path_parts=[_,_]}=_Req, _Db) ->
throw({bad_request, invalid_partition_req});

Expand Down Expand Up @@ -405,30 +397,6 @@ delete_db_req(#httpd{user_ctx=Ctx}=Req, DbName) ->
throw(Error)
end.

restore_db_req(#httpd{user_ctx=Ctx}=Req, DbName) ->
couch_httpd:verify_is_server_admin(Req),
chttpd:validate_ctype(Req, "application/json"),
{JsonProps} = chttpd:json_body_obj(Req),
DesDbName = case couch_util:get_value(<<"destination">>, JsonProps) of
undefined -> DbName;
Name -> Name
end,
case couch_util:get_value(<<"sourceTS">>, JsonProps) of
undefined ->
throw({bad_request,
<<"POST body must include `sourceTS` parameter.">>});
Timestamp ->
case fabric2_db:restore(DbName, DesDbName, Timestamp,
[{user_ctx, Ctx}]) of
ok ->
send_json(Req, 200, {[{ok, true}]});
{error, file_exists} ->
chttpd:send_error(Req, file_exists);
Error ->
throw(Error)
end
end.

do_db_req(#httpd{path_parts=[DbName|_], user_ctx=Ctx}=Req, Fun) ->
{ok, Db} = fabric2_db:open(DbName, [{user_ctx, Ctx}]),
Fun(Req, Db).
Expand Down
1 change: 1 addition & 0 deletions src/chttpd/src/chttpd_httpd_handlers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ url_handler(<<"favicon.ico">>) -> fun chttpd_misc:handle_favicon_req/1;
url_handler(<<"_utils">>) -> fun chttpd_misc:handle_utils_dir_req/1;
url_handler(<<"_all_dbs">>) -> fun chttpd_misc:handle_all_dbs_req/1;
url_handler(<<"_deleted_dbs">>) -> fun chttpd_misc:handle_deleted_dbs_req/1;
url_handler(<<"_undelete">>) -> fun chttpd_misc:handle_undelete_db_req/1;
url_handler(<<"_dbs_info">>) -> fun chttpd_misc:handle_dbs_info_req/1;
url_handler(<<"_active_tasks">>) -> fun chttpd_misc:handle_task_status_req/1;
url_handler(<<"_scheduler">>) -> fun couch_replicator_httpd:handle_scheduler_req/1;
Expand Down
30 changes: 30 additions & 0 deletions src/chttpd/src/chttpd_misc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-export([
handle_all_dbs_req/1,
handle_deleted_dbs_req/1,
handle_undelete_db_req/1,
handle_dbs_info_req/1,
handle_favicon_req/1,
handle_favicon_req/2,
Expand Down Expand Up @@ -206,6 +207,35 @@ deleted_dbs_info_req(#httpd{user_ctx=Ctx}=Req, DbName) ->
deleted_dbs_info_req(Req, _DbName) ->
send_method_not_allowed(Req, "GET,HEAD").

handle_undelete_db_req(#httpd{path_parts=[DbName| _], method='POST'}=Req) ->
undelete_db_req(Req, DbName);
handle_undelete_db_req(Req) ->
send_method_not_allowed(Req, "POST").

undelete_db_req(#httpd{user_ctx=Ctx}=Req, DbName) ->
couch_httpd:verify_is_server_admin(Req),
chttpd:validate_ctype(Req, "application/json"),
{JsonProps} = chttpd:json_body_obj(Req),
DestDbName = case couch_util:get_value(<<"destination">>, JsonProps) of
undefined -> DbName;
Name -> Name
end,
case couch_util:get_value(<<"sourceTS">>, JsonProps) of
undefined ->
throw({bad_request,
<<"POST body must include `sourceTS` parameter.">>});
Timestamp ->
case fabric2_db:undelete(
DbName, DestDbName, Timestamp, [{user_ctx, Ctx}]) of
ok ->
send_json(Req, 200, {[{ok, true}]});
{error, file_exists} ->
chttpd:send_error(Req, file_exists);
Error ->
throw(Error)
end
end.

all_dbs_callback({meta, _Meta}, #vacc{resp=Resp0}=Acc) ->
{ok, Resp1} = chttpd:send_delayed_chunk(Resp0, "["),
{ok, Acc#vacc{resp=Resp1}};
Expand Down
5 changes: 2 additions & 3 deletions src/fabric/include/fabric2.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@

% Prefix Definitions

-define(DEFAULT_DB_PREFIX, <<16#FD>>).

% Layer Level: (LayerPrefix, X, ...)

-define(CLUSTER_CONFIG, 0).
-define(ALL_DBS, 1).
-define(DELETED_DBS, 2).
-define(DB_HCA, 2).
-define(DELETED_DBS, 3).
-define(DBS, 15).
-define(TX_IDS, 255).

Expand Down
14 changes: 6 additions & 8 deletions src/fabric/src/fabric2_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
open/2,
delete/2,
deleted_dbs_info/2,
restore/4,
undelete/4,

list_dbs/0,
list_dbs/1,
Expand Down Expand Up @@ -220,19 +220,17 @@ delete(DbName, Options) ->


deleted_dbs_info(DbName, Options) ->
Options1 = lists:keystore(user_ctx, 1, Options, ?ADMIN_CTX),
Result = fabric2_fdb:transactional(DbName, Options1, fun(TxDb) ->
Result = fabric2_fdb:transactional(DbName, Options, fun(TxDb) ->
fabric2_fdb:deleted_dbs_info(TxDb)
end),
{ok, lists:reverse(Result)}.


restore(DbName, DesDbName, TimeStamp, Options) ->
case validate_dbname(DesDbName) of
undelete(DbName, DestDbName, TimeStamp, Options) ->
case validate_dbname(DestDbName) of
ok ->
Options1 = lists:keystore(user_ctx, 1, Options, ?ADMIN_CTX),
fabric2_fdb:transactional(DbName, Options1, fun(TxDb) ->
fabric2_fdb:restore(TxDb, DesDbName, TimeStamp)
fabric2_fdb:transactional(DbName, Options, fun(TxDb) ->
fabric2_fdb:undelete(TxDb, DestDbName, TimeStamp)
end);
Error ->
Error
Expand Down
19 changes: 10 additions & 9 deletions src/fabric/src/fabric2_fdb.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
delete/1,
exists/1,
deleted_dbs_info/1,
restore/3,
undelete/3,

get_dir/1,

Expand Down Expand Up @@ -182,11 +182,8 @@ create(#{} = Db0, Options) ->
} = Db = ensure_current(Db0, false),

DbKey = erlfdb_tuple:pack({?ALL_DBS, DbName}, LayerPrefix),
DefDbPref = ?DEFAULT_DB_PREFIX,
AllDbPrefix = erlfdb_util:get(Options, db_prefix, DefDbPref),
DbId = erlfdb_tuple:pack({AllDbPrefix}, AllDbPrefix),
DbPrefixAllocator = erlfdb_hca:create(erlfdb_tuple:pack({DbId}, <<"hca">>)),
AllocPrefix = erlfdb_hca:allocate(DbPrefixAllocator, Tx),
HCA = erlfdb_hca:create(erlfdb_tuple:pack({?DB_HCA}, LayerPrefix)),
AllocPrefix = erlfdb_hca:allocate(HCA, Tx),
DbPrefix = erlfdb_tuple:pack({?DBS, AllocPrefix}, LayerPrefix),
erlfdb:set(Tx, DbKey, DbPrefix),

Expand Down Expand Up @@ -386,7 +383,7 @@ deleted_dbs_info(#{} = Db0) ->
end, [], DeletedDbs).


restore(#{} = Db0, DesDbName, Timestamp) ->
undelete(#{} = Db0, DesDbName, TimeStamp) ->
#{
name := DbName,
tx := Tx,
Expand All @@ -397,8 +394,12 @@ restore(#{} = Db0, DesDbName, Timestamp) ->
Bin when is_binary(Bin) ->
{error, file_exists};
not_found ->
DeleteDbKey = erlfdb_tuple:pack({?DELETED_DBS, DbName,
Timestamp}, LayerPrefix),
DeletedDbTupleKey = {
?DELETED_DBS,
DbName,
TimeStamp
},
DeleteDbKey = erlfdb_tuple:pack(DeletedDbTupleKey, LayerPrefix),
case erlfdb:wait(erlfdb:get(Tx, DeleteDbKey)) of
not_found ->
erlang:error({not_found, invalid_timestamp});
Expand Down
4 changes: 2 additions & 2 deletions src/fabric/src/fabric2_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ pmap_exec(Fun, Arg) ->

iso8601_timestamp() ->
Now = os:timestamp(),
{{Year, Month, Date}, {Hour, Minute,
Second}} = calendar:now_to_datetime(Now),
{{Year, Month, Date}, {Hour, Minute, Second}} =
calendar:now_to_datetime(Now),
Format = "~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0BZ",
io_lib:format(Format, [Year, Month, Date, Hour, Minute, Second]).

Expand Down
7 changes: 4 additions & 3 deletions src/fabric/test/fabric2_db_crud_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ crud_test_() ->
?TDEF_FE(create_db),
?TDEF_FE(open_db),
?TDEF_FE(delete_db),
?TDEF_FE(recreate_db),
?TDEF_FE(undelete_db),
?TDEF_FE(restore_db),
?TDEF_FE(list_dbs),
?TDEF_FE(list_dbs_user_fun),
Expand Down Expand Up @@ -73,6 +73,7 @@ setup() ->


cleanup(_) ->
ok = config:set("couchdb", "enable_database_recovery", "false", false),
fabric2_test_util:tx_too_old_reset_errors(),
reset_fail_erfdb_wait(),
meck:reset([erlfdb]).
Expand Down Expand Up @@ -144,7 +145,7 @@ recreate_db(_) ->
?assertError(database_does_not_exist, fabric2_db:open(DbName, BadOpts)).


restore_db(_) ->
undelete_db(_) ->
DbName = ?tempdb(),
?assertError(database_does_not_exist, fabric2_db:delete(DbName, [])),

Expand All @@ -157,7 +158,7 @@ restore_db(_) ->
?assertEqual(false, ets:member(fabric2_server, DbName)),

{ok, [{Timestamp, _Info}]} = fabric2_db:deleted_dbs_info(DbName, []),
ok = fabric2_db:restore(DbName, DbName, Timestamp, []),
ok = fabric2_db:undelete(DbName, DbName, Timestamp, []),

{ok, AllDbInfos} = fabric2_db:list_dbs_info(),
?assert(is_db_info_member(DbName, AllDbInfos)).
Expand Down

0 comments on commit 88cf83e

Please sign in to comment.