You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2021/09/01 13:27:21 UTC

[couchdb] branch 3.x updated: feat(couch_file): log file path when a file was truncated from under us

This is an automated email from the ASF dual-hosted git repository.

jan pushed a commit to branch 3.x
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/3.x by this push:
     new aabc7ae  feat(couch_file): log file path when a file was truncated from under us
aabc7ae is described below

commit aabc7ae445c47ba57d15a4b6c6601bd0d818abb7
Author: Jan Lehnardt <ja...@apache.org>
AuthorDate: Wed Apr 14 11:39:29 2021 +0200

    feat(couch_file): log file path when a file was truncated from under us
---
 src/couch/src/couch_file.erl | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl
index b1e3555..2948d68 100644
--- a/src/couch/src/couch_file.erl
+++ b/src/couch/src/couch_file.erl
@@ -711,10 +711,21 @@ read_raw_iolist_int(Fd, {Pos, _Size}, Len) -> % 0110 UPGRADE CODE
     read_raw_iolist_int(Fd, Pos, Len);
 read_raw_iolist_int(#file{fd = Fd} = File, Pos, Len) ->
     {Pos, TotalBytes} = get_pread_locnum(File, Pos, Len),
-    {ok, <<RawBin:TotalBytes/binary>>} = file:pread(Fd, Pos, TotalBytes),
-    {remove_block_prefixes(Pos rem ?SIZE_BLOCK, RawBin), Pos + TotalBytes}.
-
+    case catch file:pread(Fd, Pos, TotalBytes) of
+        {ok, <<RawBin:TotalBytes/binary>>} ->
+            {remove_block_prefixes(Pos rem ?SIZE_BLOCK, RawBin), Pos + TotalBytes};
+        Else ->
+            % This clause matches when the file we are working with got truncated
+            % outside of CouchDB after we opened it. To find affected files, we
+            % need to log the file path.
+            %
+            % Technically, this should also go into read_multi_raw_iolists_int/2,
+            % but that doesn’t seem to be in use anywhere.
+            {_Fd, Filepath} = get(couch_file_fd),
+            throw({file_truncate_error, Else, Filepath})
+    end.
 
+% TODO: check if this is really unused
 read_multi_raw_iolists_int(#file{fd = Fd} = File, PosLens) ->
     LocNums = lists:map(fun({Pos, Len}) ->
         get_pread_locnum(File, Pos, Len)