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 2022/11/13 08:13:27 UTC

[couchdb] branch main updated: Update active db size calculation to use only leaf nodes

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

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


The following commit(s) were added to refs/heads/main by this push:
     new eddb6365b Update active db size calculation to use only leaf nodes
eddb6365b is described below

commit eddb6365bc871ba072951361422d98ef69320f52
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Sat Nov 12 22:29:49 2022 -0500

    Update active db size calculation to use only leaf nodes
    
    Previously it used both leaf and intermediate nodes.
    
    With this PR active db size should decrease when users delete documents. This,
    in turn, should make smoosh enqueue those dbs for compaction to recover the
    disk space used by the now deleted document bodies. Previously, it was possible
    for users to delete gigabytes worth of document bodies and smoosh never
    noticing and never triggering the compaction.
    
    Original idea and patch provided by Robert Newson in CouchDB dev Slack
    discussion channel.
    
    Co-authored-by: Robert Newson <rn...@apache.org>
    
    Fixes: https://github.com/apache/couchdb/issues/4263
---
 src/couch/src/couch_db_updater.erl | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/src/couch/src/couch_db_updater.erl b/src/couch/src/couch_db_updater.erl
index 0248c21ec..fac5aee10 100644
--- a/src/couch/src/couch_db_updater.erl
+++ b/src/couch/src/couch_db_updater.erl
@@ -440,7 +440,7 @@ check_doc_atts(Db, Doc) ->
             end
     end.
 
-add_sizes(Type, #leaf{sizes = Sizes, atts = AttSizes}, Acc) ->
+add_sizes(leaf, #leaf{sizes = Sizes, atts = AttSizes}, Acc) ->
     % Maybe upgrade from disk_size only
     #size_info{
         active = ActiveSize,
@@ -448,14 +448,13 @@ add_sizes(Type, #leaf{sizes = Sizes, atts = AttSizes}, Acc) ->
     } = upgrade_sizes(Sizes),
     {ASAcc, ESAcc, AttsAcc} = Acc,
     NewASAcc = ActiveSize + ASAcc,
-    NewESAcc =
-        ESAcc +
-            if
-                Type == leaf -> ExternalSize;
-                true -> 0
-            end,
+    NewESAcc = ExternalSize + ESAcc,
     NewAttsAcc = lists:umerge(AttSizes, AttsAcc),
-    {NewASAcc, NewESAcc, NewAttsAcc}.
+    {NewASAcc, NewESAcc, NewAttsAcc};
+add_sizes(_, #leaf{atts = AttSizes}, Acc) ->
+    % For intermediate nodes external and active contribution is 0
+    {ASAcc, ESAcc, AttsAcc} = Acc,
+    {ASAcc, ESAcc, lists:umerge(AttSizes, AttsAcc)}.
 
 upgrade_sizes(#size_info{} = SI) ->
     SI;