Skip to content

Added BlockCache to HFile reader. - #13427

Open
pacificleo wants to merge 2 commits into
apache:release-0.14.2from
pacificleo:pw_blockcache_hfile
Open

pacificleo wants to merge 2 commits into
apache:release-0.14.2from
pacificleo:pw_blockcache_hfile

Conversation

@pacificleo

Copy link
Copy Markdown

Change Logs

HBase 2.4+ does not allocate a block cache automatically within the CacheConfig
but requires a BlockCache instance to be passed in. This is different from HBase 1.x
where CacheConfig allocated and used a static global BlockCache instance.

BlockCache speeds up lookup from the HFile. To emulate the behavior of
HBase 1.x, we will allocate a global static global block cache here if
it is enabled. The BlockCache can be controlle via hfile configs passed
via hadoop configuration to the process.
https://github.com/apache/hbase/blob/master/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCacheFactory.java

Logs which show BlockCache was allocated

[Driver] BlockCacheFactory: Allocating BlockCache size=3.20 GB, blockSize=64 KB
[Driver] HoodieAvroHFileReader: Allocated a new global block cache for hfile readers LruBlockCache{blockCount=0, currentSize=2.40 MB, freeSize=3.20 GB, maxSize=3.20 GB, heapSize=2.40 MB, minSize=3.04 GB, minFactor=0.95, multiSize=1.52 GB, multiFactor=0.5, singleSize=778.24 MB, 

singleFactor=0.25}

Test Plan:
Tested using a perf tool to lookup 10K keys from RI. With BlockCache enabled, the time is 4x less.

With BlockCache:
[Driver] HoodieRecordIndexPerf: Time to lookup 10000 existing keys: 204462
[Driver] HoodieRecordIndexPerf: Time to lookup 10000 missing keys:   95169

Without BlockCache:
[Driver] HoodieRecordIndexPerf: Time to lookup 10000 existing keys: 817908
[Driver] HoodieRecordIndexPerf: Time to lookup 10000 missing keys: 93470

Impact

Lookups from MDT partitions like RI can be faster.

Risk level (write none, low medium or high below)

If medium or high, explain what verification was done to mitigate the risks.

Documentation Update

Describe any necessary documentation update if there is any new feature, config, or user-facing change. If not, put "none".

  • The config description must be updated if new configs are added or the default value of the configs are changed
  • Any new feature or user-facing change requires updating the Hudi website. Please create a Jira ticket, attach the
    ticket number here and follow the instruction to make
    changes to the website.

Contributor's checklist

  • Read through contributor's guide
  • Change Logs and Impact were stated clearly
  • Adequate tests were added if applicable
  • CI passed

Summary:
HBase 2.4+ does not allocate a block cache automatically within the CacheConfig
but requires a BlockCache instance to be passed in. This is different from HBase 1.x
where CacheConfig allocated and used a static global BlockCache instance.

BlockCache speeds up lookup from the HFile. To emulate the behavior of
HBase 1.x, we will allocate a global static global block cache here if
it is enabled. The BlockCache can be controlle via hfile configs passed
via hadoop configuration to the process.
https://github.com/apache/hbase/blob/master/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCacheFactory.java

Logs which show BlockCache was allocated

[Driver] BlockCacheFactory: Allocating BlockCache size=3.20 GB, blockSize=64 KB
[Driver] HoodieAvroHFileReader: Allocated a new global block cache for hfile readers LruBlockCache{blockCount=0, currentSize=2.40 MB, freeSize=3.20 GB, maxSize=3.20 GB, heapSize=2.40 MB, minSize=3.04 GB, minFactor=0.95, multiSize=1.52 GB, multiFactor=0.5, singleSize=778.24 MB, singleFactor=0.25}

Test Plan:
Tested using a perf tool to lookup 10K keys from RI. With BlockCache enabled, the time is 4x less.

With BlockCache:
[Driver] HoodieRecordIndexPerf: Time to lookup 10000 existing keys: 204462
[Driver] HoodieRecordIndexPerf: Time to lookup 10000 missing keys:   95169

Without BlockCache:
[Driver] HoodieRecordIndexPerf: Time to lookup 10000 existing keys: 817908
[Driver] HoodieRecordIndexPerf: Time to lookup 10000 missing keys: 93470

Reviewers: syalla, O955 Project Hoodie Project Reviewer: Add blocking reviewers, #hoodie_blocking_reviewers, balajee

Reviewed By: O955 Project Hoodie Project Reviewer: Add blocking reviewers, #hoodie_blocking_reviewers, balajee

Subscribers: balajee

Tags: #hudi_0.14, #has_java

JIRA Issues: HUDI-6311

Differential Revision: https://code.uberinternal.com/D16768071
Summary: This library is required for enabling hfile Block Cache.

Reviewers: syalla, O955 Project Hoodie Project Reviewer: Add blocking reviewers, #hoodie_blocking_reviewers, jingli

Reviewed By: O955 Project Hoodie Project Reviewer: Add blocking reviewers, #hoodie_blocking_reviewers, jingli

Tags: #hudi_0.14

JIRA Issues: HUDI-6594

Differential Revision: https://code.uberinternal.com/D17294457
@github-actions github-actions Bot added the size:M PR with lines of changes in (100, 300] label Jun 12, 2025
@hudi-bot

Copy link
Copy Markdown
Collaborator

CI report:

Bot commands @hudi-bot supports the following commands:
  • @hudi-bot run azure re-run the last Azure build


// HFile BlockCache key is based on the name of the file. The inlinefs path has the format
// inlinefs://<path_to_outer_file>/<outer_file_scheme>/?start_offset=start_offset>&length=<length> whose filename
// component is parsed as "?start_offset=start_offset>&length=<length>" ignoring the actual outer file itself.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"start_offset>" ==> "<start_offset>"

blockContentLoc.getBlockSize());

// HFile BlockCache key is based on the name of the file. The inlinefs path has the format
// inlinefs://<path_to_outer_file>/<outer_file_scheme>/?start_offset=start_offset>&length=<length> whose filename

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"start_offset>" ==> "<start_offset>"

// inlinefs://<path_to_outer_file>/<outer_file_scheme>/?start_offset=start_offset>&length=<length> whose filename
// component is parsed as "?start_offset=start_offset>&length=<length>" ignoring the actual outer file itself.
// To keep this unique (as cache keys should be unique for each file), we add a hash which is calculated from the
// entire filename. So the cache key would now be ?start_offset=start_offset>&length=<length>&pathHash=<hash>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same typo

}

// The BlockCache keys are based on the name of the files being cached. Within HUDI, there is a non-zero chance that
// two different files can have the same name. Also, when multiple datasets are being accessed in the same JVM, there

@linliu-code linliu-code Jun 16, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. When do two files have the same name? Same file name in different paths?

private static final Logger LOG = LoggerFactory.getLogger(HoodieAvroHFileReader.class);

// The global block cache instance used across all HFile readers
private static BlockCache GLOBAL_HFILE_BLOCK_CACHE;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Why don't we just make it per file? After a file has been read, why do we need to keep its content in the cache?
  2. We have removed all the hbase dependencies, does it require us to bring some dependency back? If so, we probably cannot do it. Or just implement one by ourselves.

Comment thread pom.xml
<include>org.apache.hbase:hbase-server</include>
<include>org.apache.hbase.thirdparty:hbase-shaded-miscellaneous</include>
<include>org.apache.hbase.thirdparty:hbase-shaded-netty</include>
<include>org.apache.hbase.thirdparty:hbase-shaded-protobuf</include>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These Hbase dependencies have been removed from HUDI.

@nsivabalan

Copy link
Copy Markdown
Contributor

hey @rmahindra : can you take a first stab on reviewing this patch

@hudi-agent hudi-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 This review was generated by an AI agent and may contain mistakes. Please verify any suggestions before applying.

Thanks for working on this! The PR re-enables BlockCache for HFile readers (which HBase 2.4+ no longer does automatically via CacheConfig) and shows a solid 4x speedup on RI lookups. A few things worth a closer look in the inline comments: an unused local variable hinting at an incomplete refactor, the unbounded growth of FILE_NAME_TO_PATH_MAP, and a couple of robustness concerns around the new regex-based InlineFS path parsing. Please take a look at any inline comments, and this should be ready for a Hudi committer or PMC member to take it from here. A few naming and dead-code nits in the new HoodieAvroHFileReader constructor logic, plus a typo in HoodieHFileDataBlock.

// To avoid conflicts, we will only enable BlockCache if the name of the file is unique and has not been
// opened before.
final String cacheKey = path.getName();
boolean enableBlockCache = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 The local enableBlockCache is declared but never read or assigned after initialization. Looks like leftover from a previous iteration of the logic — could you remove it (or wire it into the actual branch decision below if that was the intent)?

- AI-generated; verify before applying. React 👍/👎 to flag quality.

// Map of filename to the filepath for opened files
private static Map<String, String> FILE_NAME_TO_PATH_MAP = new HashMap<>();

private final Path path;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 FILE_NAME_TO_PATH_MAP is a process-lifetime static map with no eviction — every unique HFile name ever opened (including per-log-block inline paths from lookupRecords now keyed by pathHash) stays here forever. For long-running drivers/executors that touch many MDT files, log blocks, and multi-table workloads, this can grow into the millions of entries. Have you considered bounding it (e.g., a Guava Cache with size cap) or scoping it to a session?

- AI-generated; verify before applying. React 👍/👎 to flag quality.

}

if (FILE_NAME_TO_PATH_MAP.get(cacheKey).equals(path.toString())) {
this.config = new CacheConfig(hadoopConf, GLOBAL_HFILE_BLOCK_CACHE);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 The first-wins behavior here makes caching non-deterministic: whichever file with name X was opened first in the JVM gets to use the BlockCache; any subsequent file with the same name but a different path silently has its cache disabled (with a WARN). For workloads that open multiple datasets in one JVM (e.g., metaserver, multi-table services), this means a file's caching behavior depends on JVM warm-up order rather than its own attributes. Is that intentional? Would it be more predictable to either always-disable on collision, or to use a path-derived cache key (like the pathHash trick used in HoodieHFileDataBlock.lookupRecords) for all readers?

- AI-generated; verify before applying. React 👍/👎 to flag quality.

String[] slices = inlineFSPath.toString().split("[?&=]");
return Long.parseLong(slices[slices.length - 3]);
final Matcher matcher = START_OFFSET_PATTERN.matcher(inlineFSPath.toString());
matcher.find();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 matcher.find() return value isn't checked here (and the same below on line 123 for length). If the path doesn't contain ?start_offset=<digits> for any reason, matcher.group(1) will throw IllegalStateException("No match found") with no path context — making diagnosis harder than the previous split-based failure. Could you check find() and throw a HoodieException with the offending path instead?

- AI-generated; verify before applying. React 👍/👎 to flag quality.

// The global block cache instance used across all HFile readers
private static BlockCache GLOBAL_HFILE_BLOCK_CACHE;
// Map of filename to the filepath for opened files
private static Map<String, String> FILE_NAME_TO_PATH_MAP = new HashMap<>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 nit: could you add final here? private static final Map<String, String> FILE_NAME_TO_PATH_MAP = new HashMap<>(); — without it, the field can be accidentally reassigned, which would silently drop all the cache-conflict tracking.

- AI-generated; verify before applying. React 👍/👎 to flag quality.

new HoodieAvroHFileReader(inlineConf, inlinePath, new CacheConfig(inlineConf), inlinePath.getFileSystem(inlineConf),
Option.of(getSchemaFromHeader()))) {
new HoodieAvroHFileReader(inlineConf, cachableInlinePath, inlinePath.getFileSystem(inlineConf), Option.of(getSchemaFromHeader()))) {
// Get writer's schema from the header

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 nit: small typo — cachableInlinePath should be cacheableInlinePath (the correct English spelling is "cacheable").

- AI-generated; verify before applying. React 👍/👎 to flag quality.

@voonhous

voonhous commented Nov 7, 2026

Copy link
Copy Markdown
Member

Temporarily closing this PR while we debug a GitHub settings error on master (#20471); it will be reopened shortly, no action needed.

@voonhous voonhous closed this Nov 7, 2026
@voonhous voonhous reopened this Nov 7, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M PR with lines of changes in (100, 300]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants