You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2021/04/14 09:41:46 UTC

[GitHub] [couchdb] janl opened a new pull request #3507: log file truncation

janl opened a new pull request #3507:
URL: https://github.com/apache/couchdb/pull/3507


   Helps find corrupted files on servers with lots of shards.
   
   test script:
   ```sh
   #!/bin/sh
   
   COUCH=http://a:a@127.0.0.1:15984
   
   DB=x-test-goop
   CURL="curl -s"
   
   # file does not exist
   ls ~/Work/asf/couchdb/dev/lib/node1/data/shards/00000000-ffffffff/$DB*.couch
   
   # reset db
   $CURL -X DELETE $COUCH/$DB
   $CURL -X PUT $COUCH/$DB?q=1
   
   # add docs so we have something to truncate
   $CURL -X PUT $COUCH/$DB/doc1 -d '{"foo":"baaaaaah1"}'
   $CURL -X PUT $COUCH/$DB/doc2 -d '{"foo":"baaaaaah2"}'
   $CURL -X PUT $COUCH/$DB/doc3 -d '{"foo":"baaaaaah3"}'
   
   # show that we can read from the db
   $CURL $COUCH/$DB/_changes?include_docs=true
   
   # truncate
   FILE_NAME=`ls ~/Work/asf/couchdb/dev/lib/node1/data/shards/00000000-ffffffff/$DB*.couch`
   FILE_SIZE=`ls -l $FILE_NAME | awk '{print $5}'`
   NEW_FILE_SIZE=`echo "$FILE_SIZE-5000" | bc`
   truncate -s $NEW_FILE_SIZE $FILE_NAME
   # rm $FILE_NAME
   REAL_NEW_FILE_SIZE=`ls -l $FILE_NAME | awk '{print $5}'`
   
   echo "$FILE_NAME"
   echo "fs: $FILE_SIZE, nfs: $NEW_FILE_SIZE, rnfs: $REAL_NEW_FILE_SIZE"
   
   # show error
   $CURL $COUCH/$DB/_changes?include_docs=true
   ```
   
   Response without the patch:
   
   ```erlang
   {"error":"error","reason":"{{{badmatch,eof},\n  [{couch_file,read_raw_iolist_int,3,[{file,\"src/couch_file.erl\"},{line,714}]},\n   {couch_file,handle_call,3,[{file,\"src/couch_file.erl\"},{line,493}]},\n   {gen_server,try_handle_call,4,[{file,\"gen_server.erl\"},{line,636}]},\n   {gen_server,handle_msg,6,[{file,\"gen_server.erl\"},{line,665}]},\n   {proc_lib,init_p_do_apply,3,[{file,\"proc_lib.erl\"},{line,247}]}]},\n {gen_server,call,[<0.784.0>,{pread_iolist,16830},infinity]},\n [{gen_server,call,3,[{file,\"gen_server.erl\"},{line,214}]},\n  {couch_file,pread_iolist,2,[{file,\"src/couch_file.erl\"},{line,177}]},\n  {couch_file,pread_binary,2,[{file,\"src/couch_file.erl\"},{line,172}]},\n  {couch_file,pread_term,2,[{file,\"src/couch_file.erl\"},{line,160}]},\n  {couch_btree,get_node,2,[{file,\"src/couch_btree.erl\"},{line,435}]},\n  {couch_btree,reduce_stream_node,11,\n               [{file,\"src/couch_btree.erl\"},{line,622}]},\n  {couch_btree,fold_reduce,4,[{file,\"src/couch_bt
 ree.erl\"},{line,81}]},\n  {couch_bt_engine,count_changes_since,2,\n                   [{file,\"src/couch_bt_engine.erl\"},{line,646}]}]}"}
   ```
   
   Response with the patch:
   
   ```erlang
   {"error":"error","reason":"{{bad_return_value,\n     {file_truncate_error,eof,\n         \"/Users/jan/Work/asf/couchdb/dev/lib/node1/data/shards/00000000-ffffffff/x-test-goop.1618391080.couch\"}},\n {gen_server,call,[<0.6608.1>,{pread_iolist,16830},infinity]},\n [{gen_server,call,3,[{file,\"gen_server.erl\"},{line,214}]},\n  {couch_file,pread_iolist,2,[{file,\"src/couch_file.erl\"},{line,177}]},\n  {couch_file,pread_binary,2,[{file,\"src/couch_file.erl\"},{line,172}]},\n  {couch_file,pread_term,2,[{file,\"src/couch_file.erl\"},{line,160}]},\n  {couch_btree,get_node,2,[{file,\"src/couch_btree.erl\"},{line,435}]},\n  {couch_btree,reduce_stream_node,11,\n      [{file,\"src/couch_btree.erl\"},{line,622}]},\n  {couch_btree,fold_reduce,4,[{file,\"src/couch_btree.erl\"},{line,81}]},\n  {couch_bt_engine,count_changes_since,2,\n      [{file,\"src/couch_bt_engine.erl\"},{line,646}]}]}"}
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] bessbd commented on pull request #3507: log file truncation

Posted by GitBox <gi...@apache.org>.
bessbd commented on pull request #3507:
URL: https://github.com/apache/couchdb/pull/3507#issuecomment-819456907


   Should we open a follow-up issue / PR?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] janl merged pull request #3507: log file truncation

Posted by GitBox <gi...@apache.org>.
janl merged pull request #3507:
URL: https://github.com/apache/couchdb/pull/3507


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] rnewson commented on pull request #3507: log file truncation

Posted by GitBox <gi...@apache.org>.
rnewson commented on pull request #3507:
URL: https://github.com/apache/couchdb/pull/3507#issuecomment-879229350


   Yes, sorry for delay. +1.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] janl commented on pull request #3507: log file truncation

Posted by GitBox <gi...@apache.org>.
janl commented on pull request #3507:
URL: https://github.com/apache/couchdb/pull/3507#issuecomment-877755965


   @rnewson is this more in line with what you were looking for here?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] bessbd commented on pull request #3507: log file truncation

Posted by GitBox <gi...@apache.org>.
bessbd commented on pull request #3507:
URL: https://github.com/apache/couchdb/pull/3507#issuecomment-819456737


   The error message improvement seems awesome, but we still shouldn't get a stack trace on HTTP, right?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [couchdb] rnewson commented on a change in pull request #3507: log file truncation

Posted by GitBox <gi...@apache.org>.
rnewson commented on a change in pull request #3507:
URL: https://github.com/apache/couchdb/pull/3507#discussion_r613406164



##########
File path: 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})

Review comment:
       The original behaviour caused an `exit` which we're now softening to a `throw`. I think that's a mistake.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org