Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(server/v2): kill viper from server components #21663

Merged
merged 3 commits into from
Sep 12, 2024

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Sep 11, 2024

Description

@kocubinski rightfully pointed viper may not be needed as well in server components.
Follow-up of #21635

This gives us:

Main server, reads the app.toml and config.toml using viper, and pass down the whole config to server components as a map[string]any. The server components are able to unmarshal their own config from that map.

The app still needs viper, but this is unrelated to this PR.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Enhanced flexibility in configuration handling by transitioning from a specific library to a generic map structure across multiple server components.
  • Bug Fixes

    • Improved initialization processes for various server components by refining the configuration data source.
  • Chores

    • Removed direct dependency on the Viper library, streamlining the project's dependency management.

Copy link
Contributor

coderabbitai bot commented Sep 11, 2024

Walkthrough

Walkthrough

This pull request introduces a significant change in configuration management across several components of the server. The primary modification involves replacing the *viper.Viper type with a map[string]any type for configuration parameters in various Init methods. This shift indicates a move away from the Viper configuration library towards a more generic map structure, simplifying the configuration handling process throughout the application.

Changes

Files Change Summary
server/v2/api/grpc/server.go, server/v2/api/grpcgateway/server.go, server/v2/cometbft/server.go, server/v2/store/server.go, server/v2/server.go, server/v2/server_mock_test.go Modified Init method signatures to replace *viper.Viper with map[string]any for configuration handling.
server/v2/cometbft/config.go Removed the getConfigTomlFromViper function and eliminated the viper import statement.
server/v2/cometbft/go.mod Removed direct dependency on github.com/spf13/viper v1.19.0 and added it as an indirect dependency.
server/v2/config.go Changed UnmarshalSubConfig function signature to accept map[string]any instead of *viper.Viper.
server/v2/commands.go Updated the argument passed to server.Init to use v.AllSettings() instead of v.
server/v2/config_test.go, server/v2/server_test.go Modified test cases to reflect changes in configuration handling, using cfg instead of v.

Possibly related PRs

Suggested labels

C:CLI, C:x/auth, C:x/genutil, C:x/accounts, C:x/bank, C:x/gov, C:x/feegrant, C:x/group, C:x/staking


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Sep 11, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
server/v2/server_mock_test.go (1)

34-36: LGTM! Update the method documentation.

The method signature change aligns with the PR objective of removing Viper from the server components.

If the empty method body is a placeholder, consider adding a TODO comment to track the pending implementation.

Update the method documentation to reflect the change in the configuration parameter type from *viper.Viper to map[string]any.

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 72620a5 and 2cca86d.

Files selected for processing (12)
  • server/v2/api/grpc/server.go (3 hunks)
  • server/v2/api/grpcgateway/server.go (3 hunks)
  • server/v2/cometbft/config.go (2 hunks)
  • server/v2/cometbft/go.mod (2 hunks)
  • server/v2/cometbft/server.go (2 hunks)
  • server/v2/commands.go (1 hunks)
  • server/v2/config.go (1 hunks)
  • server/v2/config_test.go (1 hunks)
  • server/v2/server.go (3 hunks)
  • server/v2/server_mock_test.go (2 hunks)
  • server/v2/server_test.go (1 hunks)
  • server/v2/store/server.go (2 hunks)
Files skipped from review due to trivial changes (1)
  • server/v2/cometbft/go.mod
Additional context used
Path-based instructions (11)
server/v2/server_mock_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

server/v2/config_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

server/v2/store/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/config.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/server_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

server/v2/cometbft/config.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/api/grpcgateway/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/api/grpc/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/commands.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/cometbft/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (15)
server/v2/config_test.go (2)

Line range hint 14-25:
This test function has no changes in the provided diff. Skipping the review.


34-34: LGTM!

The changes simplify the code by using the cfg variable that represents all settings, instead of directly using the v variable. This does not alter the behavior of the test.

The code changes are approved.

Also applies to: 37-37, 44-44

server/v2/store/server.go (1)

26-33: LGTM!

The changes to the Init method are approved. The shift from using the Viper library to a more generic map-based configuration approach simplifies the configuration handling mechanism and reduces dependencies on external libraries. The changes are correctly implemented and there are no apparent issues.

server/v2/config.go (1)

42-54: LGTM!

The changes to the UnmarshalSubConfig function look good:

  • The function signature update improves flexibility by allowing it to work with a generic map instead of being coupled to the Viper library.
  • The updated logic correctly handles the case when the sub-configuration name is empty by unmarshaling the entire map.
  • The function uses the mapstructure library to decode the configuration into the target struct, which provides additional decoding options.
  • Error handling is done correctly by returning descriptive errors.

The code changes are approved.

server/v2/server_test.go (3)

59-59: LGTM!

The change to derive a configuration map from the Viper instance using AllSettings() is approved. This seems to be part of a larger effort to move away from direct usage of Viper.


63-63: LGTM!

The change to initialize the grpcServer using the derived configuration map cfg instead of the Viper instance is approved. This change is consistent with the previous change to use a configuration map.


67-67: LGTM!

The change to initialize the storeServer using the derived configuration map cfg instead of the Viper instance is approved. This change is consistent with the previous changes to use a configuration map.

server/v2/cometbft/config.go (1)

Line range hint 1-50: Removal of Viper dependency for configuration management

The removal of the getConfigTomlFromViper function and the viper import statement indicates a shift away from using Viper for configuration management within this context. This change aligns with the PR objective of killing Viper from server components and may simplify the configuration process or indicate a move towards a different configuration handling strategy.

However, the overall control flow of the configuration management has been altered by this removal. Any previous reliance on the getConfigTomlFromViper function for setting up application configuration will now need to be addressed elsewhere in the codebase.

Please ensure that the configuration management is properly handled after this change. Verify that any parts of the application that previously depended on the getConfigTomlFromViper function for configuration retrieval have been updated accordingly.

You can use the following script to search for potential usage of the removed function across the codebase:

server/v2/api/grpcgateway/server.go (2)

Line range hint 85-97: LGTM! The changes improve configuration flexibility and maintainability.

The modifications to the Init method signature and internal logic enhance the flexibility of the configuration handling process by decoupling it from the Viper library. The method now accepts a more generic map[string]any structure for configuration management, making it easier to integrate with different configuration sources.

The added check for the presence of entries in the cfg map ensures that the configuration is only unmarshaled when necessary, preventing potential errors.

The renaming of the cfg variable to serverCfg improves code clarity, making it more apparent that the variable represents the unmarshaled server configuration.

Overall, these changes contribute to better maintainability and adaptability of the codebase.


Line range hint 85-97: Code conforms to the Uber Go Style Guide.

The reviewed code segment adheres to the conventions outlined in the Uber Go Style Guide. The naming, formatting, and structure of the code are consistent with the guidelines, promoting readability and maintainability.

The utilization of generics with the GRPCGatewayServer[T] struct aligns with the recommendations for leveraging Go's type system effectively, enabling type-safe and reusable code.

No deviations from the style guide were observed in this code segment.

server/v2/api/grpc/server.go (2)

49-61: LGTM!

The changes to the Init method look good. The shift from using the Viper library to a more generic map structure for configuration management enhances flexibility and simplifies the initialization process. The updated logic correctly handles the new configuration type and ensures that the server's configuration is set up based on the provided input.

The changes also include proper checks to handle cases where the cfg map is empty and updates to how the maximum send and receive message sizes are accessed using the serverCfg.

Overall, the modifications improve the configuration handling within the gRPC server initialization process.


49-61: Code style looks good!

The Init method follows the Uber Golang style guide:

  • The method signature and parameters adhere to the recommended naming conventions.
  • The error handling is in line with the recommended practices.
  • The method length is within the suggested limit.

No deviations from the style guide were found.

server/v2/commands.go (1)

115-115: LGTM!

The change aligns with the PR objective of removing the Viper library from the server components. The server.Init function now receives a comprehensive set of settings via v.AllSettings(), instead of the raw viper.Viper instance.

The change is localized and does not introduce any new errors or edge cases.

server/v2/server.go (1)

191-211: LGTM!

The changes to the Init method signature and implementation look good. The move from the Viper library to a more generic map structure for configuration enhances the modularity and adaptability of the server's initialization process. The updated logic to unmarshal configuration and the new variable names accurately reflect the new configuration type. The overall flow of the method remains intact, and the code changes conform to the Uber Go Style Guide.

Great work on this refactor! The changes are approved.

server/v2/cometbft/server.go (1)

60-71: Approve: The changes effectively remove the dependency on the Viper library and introduce a more flexible configuration management approach.

The modifications to the Init method signature and the corresponding updates to the configuration retrieval logic align well with the objective of eliminating the reliance on the Viper library. By accepting a generic map[string]any instead of a viper.Viper instance, the code becomes more adaptable to different configuration sources and management strategies.

The updated logic for retrieving the home directory, unmarshaling configuration, and obtaining the chainID demonstrates a consistent and coherent approach to working with the new configuration map. The fallback mechanism for reading the genesis file remains intact, ensuring backward compatibility.

Overall, the changes are well-structured, maintain the functionality of the Init method, and provide a solid foundation for more flexible configuration management in the future.

Also applies to: 81-94

Copy link
Contributor

@hieuvubk hieuvubk left a comment

Choose a reason for hiding this comment

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

ack

@julienrbrt julienrbrt added this pull request to the merge queue Sep 12, 2024
Merged via the queue into main with commit 88cfebe Sep 12, 2024
77 of 78 checks passed
@julienrbrt julienrbrt deleted the julien/remove-viper-server-components branch September 12, 2024 06:29
mergify bot pushed a commit that referenced this pull request Sep 12, 2024
(cherry picked from commit 88cfebe)

# Conflicts:
#	server/v2/api/grpc/server.go
#	server/v2/api/grpcgateway/server.go
#	server/v2/commands.go
#	server/v2/config.go
#	server/v2/config_test.go
#	server/v2/server.go
#	server/v2/server_mock_test.go
#	server/v2/server_test.go
#	server/v2/store/server.go
alessio pushed a commit to alessio/cosmos-sdk that referenced this pull request Sep 12, 2024
alpe added a commit that referenced this pull request Sep 12, 2024
* main:
  docs(client/debug): correct `debug raw-bytes` command example (#21671)
  build: don't reinstall golangci-lint if already installed (#21662)
  refactor(server/v2): kill viper from server components (#21663)
  chore: sync changelog with latest releases (#21658)
  refactor: remove viper as a direct dependency (#21635)
  ci: centralized job for rocksdb libaries cache (#21657)
  fix: remove stray fmt.Println (#21661)
julienrbrt added a commit that referenced this pull request Sep 12, 2024
…) (#21673)

Co-authored-by: Julien Robert <julien@rbrt.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release C:server/v2 api C:server/v2 cometbft C:server/v2 Issues related to server/v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants