Repository navigation
refactor(cmd): replace global initializers with lazy PersistentPreRunE - #3994
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3994 +/- ##
==========================================
- Coverage 83.11% 83.09% -0.03%
==========================================
Files 148 148
Lines 18187 18181 -6
==========================================
- Hits 15116 15107 -9
- Misses 2520 2522 +2
- Partials 551 552 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
/gemini summary |
|
/gemini review |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request is an excellent refactoring that addresses a significant performance degradation in unit tests by replacing the global cobra.OnInitialize with the instance-specific PersistentPreRunE hook. The changes are well-implemented, follow Cobra best practices, and make the command initialization logic more robust and easier to reason about. The performance improvements demonstrated in the description are substantial. The code is clean, and the logic has been correctly migrated. I have no further comments; great work!
|
/gemini review |
There was a problem hiding this comment.
Code Review
This is an excellent refactoring that addresses a significant performance issue in the test suite by replacing the cobra.OnInitialize anti-pattern with the recommended PersistentPreRunE hook. The change is well-documented in the pull request description, including clear performance metrics that demonstrate the improvement. The new implementation is cleaner, more idiomatic, and correctly isolates command initialization, which is a great improvement for testability and maintainability. I have one minor suggestion to improve error handling consistency, but overall, this is a solid contribution.
|
Great find! |
#5046) * build(deps): upgrade Go to 1.26.7 and dependencies for CVE remediation * upgrade golang to 1.26.7 (CVE fixes) * test(monitoring): use NewTextParser with LegacyValidation for prometheus/common v0.70.1 * fix(ci): update golangci-lint args in CI workflow * refactor(cmd): replace global initializers with lazy PersistentPreRunE (#3994) * fix(test): use 4095 for FUSE max name length (#4810) On newer Linux kernels, FUSE_NAME_MAX is set to PATH_MAX - 1 (4095). Setting fuseMaxNameLen to 4095 allows us to test illegal filename lengths with a value that consistently triggers a kernel-level ENAMETOOLONG error across different kernel versions. Use a separate gcsMaxNameLen (1024) for the longest legal name test, matching the GCS limit. TAG=agy CONV=cb2ad2c9-cd41-4085-af5c-0f82aa999dca * fix(test): exclude unicode.Cn category in interestingNames and interestingLegalNames (#4456) * test(cache): remove unaligned writer offset test case in CopyUsingMemoryAlignedBuffer * fix(test): allow EPERM or ENOSYS in DirectoryTest.CreateHardLink for newer kernels (#4354) * revert: fix(test): use 4095 for FUSE max name length (#4810) * Revert "revert: fix(test): use 4095 for FUSE max name length (#4810)" This reverts commit 2b19ee5. --------- Co-authored-by: Geert Jansen <geertj@google.com>
…ypto for CVE remediation (#5252) * fix(deps): upgrade Go to 1.27.4, go-jose/v4 to v4.1.6, x/net and x/crypto for CVE remediation * refactor(cmd): replace global initializers with lazy PersistentPreRunE (#3994) * remove global initializers from cobra * review fixes: wrap error based on failures (cherry picked from commit e557052) * updated otel version to 1.47.0 because of https://osv.dev/GHSA-q7gw-24fr-5p69 --------- Co-authored-by: Mohit Kumar Yadav <mohitkyadav@google.com>
Description
This pull request refactors the command initialization and configuration loading logic to fix a severe performance degradation in our unit tests.
The previous implementation used
cobra.OnInitializeto register configuration functions. This is an anti-pattern for testable applications becauseOnInitializeappends to a global list of initializers, causing repeated test runs to become progressively slower as the list grows.This change replaces the global initializer pattern with the recommended
PersistentPreRunElifecycle hook. All configuration loading, validation, and rationalization now happens withinPersistentPreRunE. This isolates test cases and restores fast, consistent test performance.Old Behavior
Previously, the
newRootCmdfunction usedcobra.OnInitializeto register initialization functions. These functions were responsible for reading the config file, validating flags, and rationalizing the final configuration.The
cobra.OnInitializefunction works by appending the provided functions to a global list of initializers within the Cobra library. In our unit tests (e.g., inroot_test.go), we callnewRootCmdinside a loop for each test case. This led to the following problem:cmd.Execute()was called for a given test, Cobra would execute all functions in the global list, including duplicates from all previous tests in the same suite.New Behavior
To resolve this issue, the configuration logic has been moved out of the global initialization path and into the
PersistentPreRunEfunction of the root command.PersistentPreRunEis a Cobra lifecycle hook that runs exactly once per command execution, right before the mainRunEfunction. By placing the setup logic here, we achieve the following:newRootCmdin a test creates a fresh command. Whencmd.Execute()is called, only thePersistentPreRunEfor that specific instance is run.This refactoring makes our command-line handling more robust, easier to reason about, and significantly improves the performance and reliability of our test suite.
Link to the issue in case of a bug fix.
b/459118811
Run time improvement in tests.
.../gcsfuse/v3/cmdPerf results
Testing details
Any backward incompatible change? If so, please explain.