From cdf43ab5a1d5ea21e42302c848fe4f07150e6947 Mon Sep 17 00:00:00 2001 From: "Damien F. Katz" Date: Thu, 9 Apr 2009 21:37:23 +0000 Subject: Fix for attachment sparseness bug COUCHDB-220 by giving each attachment it's own stream and calling set_min_buffer instead of ensure_buffer. Also fixed spurious couch_file crash messages by putting the statistics decrement code into a seperate monitoring process. git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@763816 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_file.erl | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src/couchdb/couch_file.erl') diff --git a/src/couchdb/couch_file.erl b/src/couchdb/couch_file.erl index ecab455b..93c43434 100644 --- a/src/couchdb/couch_file.erl +++ b/src/couchdb/couch_file.erl @@ -269,7 +269,6 @@ init_status_error(ReturnPid, Ref, Error) -> % server functions init({Filepath, Options, ReturnPid, Ref}) -> - process_flag(trap_exit, true), case lists:member(create, Options) of true -> filelib:ensure_dir(Filepath), @@ -285,14 +284,14 @@ init({Filepath, Options, ReturnPid, Ref}) -> true -> {ok, 0} = file:position(Fd, 0), ok = file:truncate(Fd), - catch couch_stats_collector:increment({couchdb, open_os_files}), + track_stats(), {ok, Fd}; false -> ok = file:close(Fd), init_status_error(ReturnPid, Ref, file_exists) end; false -> - catch couch_stats_collector:increment({couchdb, open_os_files}), + track_stats(), {ok, Fd} end; Error -> @@ -304,7 +303,7 @@ init({Filepath, Options, ReturnPid, Ref}) -> {ok, Fd_Read} -> {ok, Fd} = file:open(Filepath, [read, write, raw, binary]), ok = file:close(Fd_Read), - catch couch_stats_collector:increment({couchdb, open_os_files}), + track_stats(), {ok, Fd}; Error -> init_status_error(ReturnPid, Ref, Error) @@ -312,10 +311,21 @@ init({Filepath, Options, ReturnPid, Ref}) -> end. -terminate(_Reason, _Fd) -> - catch couch_stats_collector:decrement({couchdb, open_os_files}), +terminate(_Reason, _Fd) -> ok. +track_stats() -> + try couch_stats_collector:increment({couchdb, open_os_files}) of + ok -> + Self = self(), + spawn( + fun() -> + erlang:monitor(process, Self), + receive {'DOWN', _, _, _, _} -> ok end, + couch_stats_collector:decrement({couchdb, open_os_files}) + end) + catch _ -> ok + end. handle_call({pread, Pos, Bytes}, _From, Fd) -> {reply, file:pread(Fd, Pos, Bytes), Fd}; @@ -349,5 +359,5 @@ handle_cast(close, Fd) -> code_change(_OldVsn, State, _Extra) -> {ok, State}. -handle_info({'EXIT', _, Reason}, Fd) -> - {stop, Reason, Fd}. +handle_info(foo, _Fd) -> + ok. -- cgit v1.2.3