Page MenuHomePhabricator

HDFS usage dashboard is quadruple counting file counts and file sizes
Closed, ResolvedPublic

Description

Indeed the definition of the Superset virtual dataset seems to be double (or triple?) counting:

Dataset definition: https://superset.wikimedia.org/explore/?form_data_key=AdGJRebFr1bvqbReSZziBs8UOCGMWkgC9F7FKNvX__LVy1YQ3bfymfztr-XX08JZ&datasource_type=table&datasource_id=1682

Running the associated query:

with base_directories as (
    select id, parent_id, path, files_count, blocks_size, path_depth
    from wmf.hdfs_usage 
    where type = 'DIRECTORY'
       and path_depth <= 6
       and date(snapshot) >= (current_date - interval '14' day)
       and case when day_of_week(current_date) = 1 then date(snapshot) < current_date else true end
), base_directories_with_parents as (
    select b.id, b.path, b.files_count, b.blocks_size, b.path_depth, p.path as parent_path
    from base_directories b
    inner join base_directories p
        on p.id = b.parent_id
)
select path, parent_path, files_count,  blocks_size
from base_directories_with_parents
where ((path = '/tmp' or path = '/var')
    or (parent_path = '/user')
    or (parent_path = '/wmf' and path <> '/wmf/data')
    or (parent_path = '/wmf/data' and path <> '/wmf/data/raw' and path <> '/wmf/data/wmf')
    or (parent_path = '/wmf/data/raw')
    or (parent_path = '/wmf/data/wmf'))
    and (blocks_size >= 10000000000 or files_count > 1000)
limit 1000

Returns 4 rows of each path:

...
/wmf/data/wmf_contributors	/wmf/data	20347	140193636567
/wmf/data/wmf_contributors	/wmf/data	20347	140193636567
/wmf/data/wmf_contributors	/wmf/data	20305	140004135096
/wmf/data/wmf_contributors	/wmf/data	20305	140004135096
/wmf/data/wmf_content	/wmf/data	2945164	156098555325320
/wmf/data/wmf_content	/wmf/data	2945164	156098555325320
/wmf/data/wmf_content	/wmf/data	2908070	155424626278535
/wmf/data/wmf_content	/wmf/data	2908070	155424626278535
/wmf/data/wme	/wmf/data	23878	58067764561
/wmf/data/wme	/wmf/data	23878	58067764561
/wmf/data/wme	/wmf/data	20711	51681910602
/wmf/data/wme	/wmf/data	20711	51681910602
...

And if we multiply 136.3 T * 4 = 136.3 = 545.2 which is close enough to what is being advertised in the dashboard.

In this task we should :

  • Investigate why the query quadruple counts, looks related to the snapshot logic?
  • Fix and deploy a new version of the dashboard.

Event Timeline

xcollazo triaged this task as High priority.

Context: this dashboard was put together as part of T381707: Low available space on Hadoop / HDFS.

Root cause: The base_directories CTE in the Superset virtual dataset was filtering for 14 days of snapshot partitions. Since snapshot is a Hive partition key, each directory has one row per snapshot day. The self-join (b JOIN p ON p.id = b.parent_id) had no constraint on matching snapshot dates, so each child row joined to all matching parent rows across all 14 days — producing 4 rows per path (2 snapshot dates × 2 parent matches after filtering).

Fix: Add a latest_base_directories CTE that narrows the already partition-pruned base_directories down to the single most recent snapshot, then use that in the join instead:

 with base_directories as (                                                                                                                                                               
-    select id, parent_id, path, files_count, blocks_size, path_depth
+    select id, parent_id, path, files_count, blocks_size, path_depth, snapshot                                                                                                           
     from wmf.hdfs_usage                                                                                                                                                                  
     where type = 'DIRECTORY'                                                                                                                                                             
        and path_depth <= 6                                                                                                                                                               
        and date(snapshot) >= (current_date - interval '14' day)                                                                                                                          
        and case when day_of_week(current_date) = 1 then date(snapshot) < current_date else true end                                                                                      
+), latest_base_directories as (                                                                                                                                                          
+    select id, parent_id, path, files_count, blocks_size, path_depth                                                                                                                     
+    from base_directories                                                                                                                                                                
+    where snapshot = (select max(snapshot) from base_directories)                                                                                                                        
 ), base_directories_with_parents as (                                                                                                                                                    
     select b.id, b.path, b.files_count, b.blocks_size, b.path_depth, p.path as parent_path                                                                                               
-    from base_directories b                                                                                                                                                              
+    from latest_base_directories b
-    inner join base_directories p                                                                                                                                                        
+    inner join latest_base_directories p
         on p.id = b.parent_id                                                                                                                                                            
 )

The 14-day window and Monday exclusion are preserved intentionally — they enable Presto partition pruning (removing them caused 3+ min timeouts), and the Monday exclusion avoids a potentially incomplete same-day snapshot. The max(snapshot) subquery runs against the already-pruned CTE rather than the full table, so it resolves cheaply.

Full updated query:

-- base_directories uses a 14-day window for partition pruning (performance).                                                                                                             
-- The Monday exclusion avoids using a potentially incomplete same-day snapshot.
-- latest_base_directories then narrows down to the single most recent snapshot.
with base_directories as (
    select id, parent_id, path, files_count, blocks_size, path_depth, snapshot                                                                                                            
    from wmf.hdfs_usage                                                                                                                                                                   
    where type = 'DIRECTORY'                                                                                                                                                              
       and path_depth <= 6                                                                                                                                                                
       and date(snapshot) >= (current_date - interval '14' day)                                                                                                                           
       and case when day_of_week(current_date) = 1 then date(snapshot) < current_date else true end                                                                                       
), latest_base_directories as (
    select id, parent_id, path, files_count, blocks_size, path_depth                                                                                                                      
    from base_directories                                                                                                                                                                 
    where snapshot = (select max(snapshot) from base_directories)
), base_directories_with_parents as (                                                                                                                                                     
    select b.id, b.path, b.files_count, b.blocks_size, b.path_depth, p.path as parent_path
    from latest_base_directories b                                                                                                                                                        
    inner join latest_base_directories p
        on p.id = b.parent_id                                                                                                                                                             
)               
select path, parent_path, files_count, blocks_size
from base_directories_with_parents
where ((path = '/tmp' or path = '/var')                                                                                                                                                   
    or (parent_path = '/user')
    or (parent_path = '/wmf' and path <> '/wmf/data')                                                                                                                                     
    or (parent_path = '/wmf/data' and path <> '/wmf/data/raw' and path <> '/wmf/data/wmf')
    or (parent_path = '/wmf/data/raw')                                                                                                                                                    
    or (parent_path = '/wmf/data/wmf'))
    and (blocks_size >= 10000000000 or files_count > 1000)                                                                                                                                
limit 1000

@Antoine_Quhen: Can you confirm the intent of the 14-day window and Monday exclusion? We want to make sure we're not inadvertently changing behavior that was there for a reason before applying the fix.

Next step: @Antoine_Quhen if you agree with changes, please apply to your virtual dataset.

👍 Dataset updated. Data looks good. Checked with hdfs dfs -count ...