diff options
author | Damien F. Katz <damien@apache.org> | 2010-04-02 20:01:11 +0000 |
---|---|---|
committer | Damien F. Katz <damien@apache.org> | 2010-04-02 20:01:11 +0000 |
commit | c1ba70c38adef885ee50687210732c3999951a08 (patch) | |
tree | c96c961c0f08cde85d990151937a6aa58b43e294 /src/couchdb/couch_server.erl | |
parent | 3a30ebac70a7baef18719357c8b7ff00edbbfa83 (diff) |
Deterministic/synchronous shutdown code.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@930363 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/couchdb/couch_server.erl')
-rw-r--r-- | src/couchdb/couch_server.erl | 44 |
1 files changed, 19 insertions, 25 deletions
diff --git a/src/couchdb/couch_server.erl b/src/couchdb/couch_server.erl index e52bacfa..28d9024a 100644 --- a/src/couchdb/couch_server.erl +++ b/src/couchdb/couch_server.erl @@ -147,8 +147,9 @@ init([]) -> max_dbs_open=MaxDbsOpen, start_time=httpd_util:rfc1123_date()}}. -terminate(Reason, _Srv) -> - couch_util:terminate_linked(Reason), +terminate(_Reason, _Srv) -> + [couch_util:shutdown_sync(Pid) || {_, {Pid, _LruTime}} <- + ets:tab2list(couch_dbs_by_name)], ok. all_databases() -> @@ -189,8 +190,7 @@ try_close_lru(StartTime) -> [{_, {opened, MainPid, LruTime}}] = ets:lookup(couch_dbs_by_name, DbName), case couch_db:is_idle(MainPid) of true -> - exit(MainPid, kill), - receive {'EXIT', MainPid, _Reason} -> ok end, + couch_util:shutdown_sync(MainPid), true = ets:delete(couch_dbs_by_lru, LruTime), true = ets:delete(couch_dbs_by_name, DbName), true = ets:delete(couch_dbs_by_pid, MainPid), @@ -212,7 +212,13 @@ open_async(Server, From, DbName, Filepath, Options) -> Opener = spawn_link(fun() -> Res = couch_db:start_link(DbName, Filepath, Options), gen_server:call(Parent, {open_result, DbName, Res}, infinity), - unlink(Parent) + unlink(Parent), + case Res of + {ok, DbReader} -> + unlink(DbReader); + _ -> + ok + end end), true = ets:insert(couch_dbs_by_name, {DbName, {opening, Opener, [From]}}), true = ets:insert(couch_dbs_by_pid, {Opener, DbName}), @@ -299,15 +305,13 @@ handle_call({delete, DbName, _Options}, _From, Server) -> case ets:lookup(couch_dbs_by_name, DbName) of [] -> Server; [{_, {opening, Pid, Froms}}] -> - exit(Pid, kill), - receive {'EXIT', Pid, _Reason} -> ok end, + couch_util:shutdown_sync(Pid), true = ets:delete(couch_dbs_by_name, DbName), true = ets:delete(couch_dbs_by_pid, Pid), [gen_server:send_result(F, not_found) || F <- Froms], Server#server{dbs_open=Server#server.dbs_open - 1}; [{_, {opened, Pid, LruTime}}] -> - exit(Pid, kill), - receive {'EXIT', Pid, _Reason} -> ok end, + couch_util:shutdown_sync(Pid), true = ets:delete(couch_dbs_by_name, DbName), true = ets:delete(couch_dbs_by_pid, Pid), true = ets:delete(couch_dbs_by_lru, LruTime), @@ -336,20 +340,10 @@ handle_cast(Msg, _Server) -> code_change(_OldVsn, State, _Extra) -> {ok, State}. - -handle_info({'EXIT', _Pid, config_change}, _Server) -> - exit(kill); -handle_info({'EXIT', Pid, Reason}, #server{dbs_open=DbsOpen}=Server) -> - [{Pid, DbName}] = ets:lookup(couch_dbs_by_pid, Pid), - case ets:lookup(couch_dbs_by_name, DbName) of - [{DbName, {opened, Pid, LruTime}}] -> - true = ets:delete(couch_dbs_by_lru, LruTime); - [{DbName, {opening, Pid, Froms}}] -> - [gen_server:reply(From, Reason) || From <- Froms] - end, - true = ets:delete(couch_dbs_by_pid, Pid), - true = ets:delete(couch_dbs_by_name, DbName), - {noreply, Server#server{dbs_open=DbsOpen - 1}}; -handle_info(Info, _Server) -> - exit({unknown_message, Info}). + +handle_info({'EXIT', _Pid, config_change}, Server) -> + {noreply, shutdown, Server}; +handle_info(Error, _Server) -> + ?LOG_ERROR("Unexpected message, restarting couch_server: ~p", [Error]), + exit(kill). |