From 41a03a2a0be67ab9e293ddbe631f11f703ef4242 Mon Sep 17 00:00:00 2001 From: Jan Lehnardt Date: Wed, 14 Apr 2021 11:39:29 +0200 Subject: 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 b1e355518..2948d685b 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, <>} = file:pread(Fd, Pos, TotalBytes), - {remove_block_prefixes(Pos rem ?SIZE_BLOCK, RawBin), Pos + TotalBytes}. - + case catch file:pread(Fd, Pos, TotalBytes) of + {ok, <>} -> + {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) -- cgit v1.2.1