Skip to content

fix thread Local metrics (#1621)#1647

Merged
singhravidutt merged 2 commits intobranch-3.1.xfrom
threadLocal
Mar 5, 2026
Merged

fix thread Local metrics (#1621)#1647
singhravidutt merged 2 commits intobranch-3.1.xfrom
threadLocal

Conversation

@singhravidutt
Copy link
Contributor

No description provided.

@singhravidutt
Copy link
Contributor Author

/gcbrun

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where thread-local metrics were not being properly accounted for in scenarios involving multi-threaded operations, specifically during vectored reads. The changes introduce a robust system for collecting and consolidating these metrics from individual worker threads, ensuring that the overall statistics accurately reflect the complete I/O activity. This enhancement improves the reliability and completeness of performance monitoring for concurrent file system operations.

Highlights

  • Thread-Local Metric Aggregation: Implemented a mechanism to correctly aggregate thread-local metrics from worker threads involved in vectored read operations into the main thread's statistics. This ensures accurate reporting of metrics for concurrent I/O.
  • Enhanced Metric Management: Added functionality to GhfsThreadLocalStatistics to increment metrics by their string name and to look up Metric enum values by name, improving flexibility in metric handling.
  • Vectored Read Statistics Integration: Introduced a ConcurrentHashMap in GoogleHadoopFSInputStream to temporarily store thread-local statistics during vectored reads, which are then merged into the global statistics upon stream closure.
  • Worker Thread Statistic Control: Added explicit calls within VectoredIOImpl to reset thread-local statistics before a worker thread processes a new range and to capture/merge these statistics after the range processing is complete (both success and failure paths).
  • New Integration Test: A new integration test was added to GoogleHadoopFSInputStreamIntegrationTest to validate the correct aggregation of thread-local metrics during vectored read operations.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • gcs/src/main/java/com/google/cloud/hadoop/fs/gcs/GhfsThreadLocalStatistics.java
    • Added a new increment method that accepts a metric name as a string.
    • Implemented a static getMetricByName method within the Metric enum to retrieve metrics by their string label.
    • Introduced a BY_LABEL map in the Metric enum for efficient lookup of metrics by name.
  • gcs/src/main/java/com/google/cloud/hadoop/fs/gcs/GoogleHadoopFSInputStream.java
    • Declared a ConcurrentHashMap named rangeReadThreadStats to store thread-local statistics for vectored reads.
    • Modified the readVectored method to pass the rangeReadThreadStats map to the VectoredIOImpl.
    • Added calls to mergeRangeReadThreadStats() and rangeReadThreadStats.clear() within the close() method to aggregate and reset statistics.
    • Implemented the mergeRangeReadThreadStats() private method to transfer collected range read statistics to the main thread's local statistics.
  • gcs/src/main/java/com/google/cloud/hadoop/fs/gcs/VectoredIOImpl.java
    • Updated the readVectored method signature and VectoredReadChannel constructor to accept a ConcurrentHashMap for range read statistics.
    • Added captureAndResetThreadLocalStats() to collect and merge thread-local statistics from worker threads into the shared map.
    • Introduced resetThreadLocalStats() to clear thread-local statistics for worker threads before processing new ranges.
    • Integrated calls to resetThreadLocalStats() and captureAndResetThreadLocalStats() at appropriate points within the readVectored and readCombinedRange methods.
  • gcs/src/test/java/com/google/cloud/hadoop/fs/gcs/GhfsThreadLocalStatisticsTest.java
    • Updated map initialization in getInitMetrics to use Map.ofEntries for clarity and consistency.
  • gcs/src/test/java/com/google/cloud/hadoop/fs/gcs/GoogleHadoopFSInputStreamIntegrationTest.java
    • Added a new integration test fs_operation_threadLocalMetric_tests to verify the correct aggregation of thread-local metrics during vectored reads.
  • gcs/src/test/java/com/google/cloud/hadoop/fs/gcs/VectoredIOImplTest.java
    • Modified test methods to pass and manage the new rangeReadThreadStats ConcurrentHashMap.
Activity
  • No specific activity (comments, reviews, or progress updates) was provided in the context for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@singhravidutt singhravidutt requested a review from mudit-97 March 5, 2026 04:35
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 5, 2026

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request aims to fix thread-local metrics collection for vectored reads by introducing a mechanism to aggregate stats from worker threads and merge them into the main thread's statistics. While the overall approach is sound and correctly handles single-range vectored reads, I've identified a critical issue in how statistics are collected for combined (merged) ranges. This bug will result in incorrect metrics being reported.

storageStatistics.incrementCounter(
GhfsStatistic.STREAM_READ_VECTORED_EXTRA_READ_BYTES, discardedBytes);

captureAndResetThreadLocalStats();

Choose a reason for hiding this comment

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

critical

The call to captureAndResetThreadLocalStats() is incorrectly placed inside the loop over child ranges and, more importantly, before updateBytesRead(). This will lead to incorrect metrics.

Specifically:

  1. The statistics from the GCS API call (e.g., GCS_GET_MEDIA_REQUEST) will only be captured for the first child range and then reset, being lost for subsequent child ranges in the same combined block.
  2. The STREAM_READ_BYTES statistic, which is updated in updateBytesRead(), will not be captured at all because captureAndResetThreadLocalStats() is called before it.

To fix this, captureAndResetThreadLocalStats() should be moved out of the for loop and placed after it, but before combinedFileRange.getData().complete(readContent). This ensures all statistics for the combined read are accumulated before being captured.

@codecov
Copy link

codecov bot commented Mar 5, 2026

Codecov Report

❌ Patch coverage is 97.43590% with 1 line in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (branch-3.1.x@4d5f49a). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...cloud/hadoop/fs/gcs/GhfsThreadLocalStatistics.java 88.88% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@               Coverage Diff               @@
##             branch-3.1.x    #1647   +/-   ##
===============================================
  Coverage                ?   82.65%           
  Complexity              ?     2344           
===============================================
  Files                   ?      124           
  Lines                   ?    10359           
  Branches                ?     1256           
===============================================
  Hits                    ?     8562           
  Misses                  ?     1277           
  Partials                ?      520           
Flag Coverage Δ
integrationtest 68.10% <94.87%> (?)
unittest 73.19% <97.43%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@mudit-97 mudit-97 left a comment

Choose a reason for hiding this comment

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

LGTM (cherry pick)

@singhravidutt singhravidutt merged commit 054584b into branch-3.1.x Mar 5, 2026
6 checks passed
@singhravidutt singhravidutt deleted the threadLocal branch March 5, 2026 05:19
singhravidutt added a commit that referenced this pull request Mar 5, 2026
This was referenced Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants