Skip to content
New issue

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

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

Already on GitHub? # to your account

fix(submit loop): add more logging around skew calculation #1000

Merged
merged 14 commits into from
Aug 8, 2024

Conversation

danwt
Copy link
Contributor

@danwt danwt commented Aug 7, 2024

Adds more visibility to try to fix #999 in a followup PR.

  • Immediately log any errors from worker routines
  • More logs and log information around pending bytes management
  • More explicit error when trying to create a batch but last submitted height is equal to last block height
  • Avoid a race condition on startup when calculation bytes produced and sending to channel
  • Consolidate call sites of last height set/get

Summary by CodeRabbit

  • New Features

    • Introduced encapsulated methods for setting and retrieving block height, improving state management.
    • Added a new utility function for atomic subtraction to enhance thread safety.
    • Implemented a logging function for better error handling in concurrent operations.
  • Bug Fixes

    • Enhanced error handling and logging within the Manager class's execution loops.
  • Tests

    • Refined tests to align with the new block height management methods.
    • Introduced property-based testing for atomic operations.
  • Chores

    • Updated project dependencies to include a new library for property-based testing.

Copy link
Contributor

coderabbitai bot commented Aug 7, 2024

Walkthrough

This update enhances error handling, state management, and overall code clarity across the codebase. Key improvements include encapsulating block height management through dedicated methods, refining logging in concurrent operations, and updating tests for better alignment with these changes. Collectively, these modifications aim to bolster maintainability and robustness while improving the visibility of errors during execution.

Changes

Files Change Summary
block/executor_test.go, block/state.go, store/store_test.go, test/loadtime/cmd/report/main.go, testutil/types.go, types/serialization.go Encapsulated block height management using SetHeight and Height methods for improved clarity and maintainability.
block/manager.go Enhanced error handling and logging in the Start method using uerrors.ErrGroupGoLog.
block/submit.go, block/submit_loop_test.go Improved error handling and logging in SubmitLoop and CreateAndSubmitBatch functions; streamlined test code.
go.mod Added new dependency pgregory.net/rapid for property-based testing.
utils/atomic/funcs.go, utils/atomic/funcs_test.go Introduced Uint64Sub function for atomic subtraction along with unit tests.
utils/errors/err_group.go Implemented ErrGroupGoLog for immediate error logging in goroutines.

Sequence Diagram(s)

sequenceDiagram
    participant M as Manager
    participant S as State
    participant L as Logger
    participant E as ErrorGroup

    M->>E: Start
    E->>M: Goroutines for SubmitLoop, ProduceBlockLoop, etc.
    M->>S: SetHeight(0)
    S-->>M: Height set
    E->>L: Log errors if any
Loading

🐇 "In the code where bunnies hop,
With heights and logs, we never stop.
A setter here, a logger there,
Together we improve with care!
Through every change, we leap with glee,
A brighter code for you and me!" 🐇

Assessment against linked issues

Objective Addressed Explanation
Avoided empty batch creation causing submit loop to get stuck (#999)

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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@danwt danwt marked this pull request as ready for review August 7, 2024 12:32
@danwt danwt requested a review from a team as a code owner August 7, 2024 12:32
omritoptix
omritoptix previously approved these changes Aug 7, 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, codebase verification and nitpick comments (3)
utils/atomic/funcs.go (1)

1-5: Consider moving to sdk-utils.

The TODO comment suggests moving this code to sdk-utils. Ensure this is tracked properly.

utils/atomic/funcs_test.go (1)

1-10: Consider moving to sdk-utils.

The TODO comment in the implementation file suggests moving this code to sdk-utils. Ensure this is tracked properly.

utils/errors/err_group.go (1)

1-6: Consider moving to sdk-utils.

The TODO comment suggests moving this code to sdk-utils. Ensure this is tracked properly.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 93905ee and bff8909.

Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
Files selected for processing (16)
  • block/executor_test.go (1 hunks)
  • block/manager.go (2 hunks)
  • block/manager_test.go (2 hunks)
  • block/produce.go (1 hunks)
  • block/state.go (1 hunks)
  • block/submit.go (8 hunks)
  • block/submit_loop_test.go (1 hunks)
  • go.mod (1 hunks)
  • store/store_test.go (1 hunks)
  • test/loadtime/cmd/report/main.go (1 hunks)
  • testutil/types.go (1 hunks)
  • types/serialization.go (2 hunks)
  • types/serialization_test.go (1 hunks)
  • utils/atomic/funcs.go (1 hunks)
  • utils/atomic/funcs_test.go (1 hunks)
  • utils/errors/err_group.go (1 hunks)
Files skipped from review due to trivial changes (2)
  • block/produce.go
  • block/submit_loop_test.go
Additional comments not posted (25)
utils/atomic/funcs.go (1)

11-12: Ensure correctness of the subtraction logic.

The function Uint64Sub uses atomic operations to subtract a value from an atomic uint64. The use of x.Add(^(y - 1)) is a bitwise trick to perform subtraction. Ensure this approach is well-documented and tested thoroughly to avoid potential issues.

utils/atomic/funcs_test.go (1)

12-35: Ensure comprehensive test coverage.

The test function TestUint64Sub uses property-based testing to validate the Uint64Sub function. Ensure that the test cases cover a wide range of scenarios, including edge cases, to thoroughly validate the functionality.

utils/errors/err_group.go (1)

12-23: Ensure proper error handling and logging.

The function ErrGroupGoLog enhances error logging within an errgroup. Ensure that the error messages are informative and that the logging mechanism is efficient and does not introduce significant overhead.

test/loadtime/cmd/report/main.go (1)

56-56: LGTM! But verify the usage of state.Height().

The change enhances encapsulation by using a method to access the block height.

However, ensure that the state.Height() method is correctly implemented and used throughout the codebase.

Verification successful

LGTM! The state.Height() method is correctly implemented and used throughout the codebase.

The change enhances encapsulation by using a method to access the block height. The method is consistently used to retrieve the height of the highest block saved in the store.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `state.Height()` method.

# Test: Search for the method usage. Expect: Correct and consistent usage.
rg --type go -A 5 $'state.Height()'

Length of output: 1706


Script:

#!/bin/bash
# Find the definition of the `Height` method in Go files.
ast-grep --lang go --pattern 'func (state $_) Height() $_' --json

# Check the context of `state.Height` usage in `da/celestia/celestia.go`.
rg --type go -A 10 -B 10 'state.Height' da/celestia/celestia.go

Length of output: 585


Script:

#!/bin/bash
# Search for the definition of the `Height` field in Go files.
rg --type go 'Height' -A 5 -B 5

Length of output: 329168

types/serialization_test.go (1)

157-157: LGTM! But verify the usage of SetHeight.

The change enhances encapsulation by using a setter method to set the block height.

However, ensure that the SetHeight method is correctly implemented and used throughout the codebase.

Verification successful

LGTM! But verify the usage of SetHeight.

The change enhances encapsulation by using a setter method to set the block height.

  • The SetHeight method is correctly implemented in types/state.go.
  • It is used consistently across various files in the codebase.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `SetHeight` method.

# Test: Search for the method usage. Expect: Correct and consistent usage.
rg --type go -A 5 $'SetHeight'

Length of output: 3546

store/store_test.go (2)

101-101: LGTM! But verify the usage of SetHeight.

The change enhances encapsulation by using a setter method to set the block height.

However, ensure that the SetHeight method is correctly implemented and used throughout the codebase.

Verification successful

LGTM! The SetHeight method is correctly implemented and used throughout the codebase.

The method is utilized consistently across various files, ensuring proper encapsulation and state management.

  • types/serialization.go
  • types/state.go
  • testutil/mocks.go
  • testutil/types.go
  • store/store_test.go
  • rpc/client/client_test.go
  • block/state.go
  • block/executor_test.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `SetHeight` method.

# Test: Search for the method usage. Expect: Correct and consistent usage.
rg --type go -A 5 $'SetHeight'

Length of output: 3546


109-109: LGTM! But verify the usage of Height.

The change enhances encapsulation by using a method to access the block height.

However, ensure that the Height method is correctly implemented and used throughout the codebase.

Verification successful

Verification Successful: Height Method Implementation and Usage

The Height method is correctly implemented in types/state.go and is used consistently throughout the codebase.

  • Implementation: The method returns the value of s.LastBlockHeight.Load(), ensuring thread-safe access to the block height.
  • Usage: Verified across various files and contexts, confirming appropriate and consistent usage.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `Height` method.

# Test: Search for the method usage. Expect: Correct and consistent usage.
rg --type go -A 5 $'Height()'

Length of output: 216904


Script:

#!/bin/bash
# Description: Find the implementation and usage of the `Height` method.

# Search for the implementation of the `Height` method in Go files.
ast-grep --lang go --pattern 'func (s *$_) Height() $_' -A 5

Length of output: 434

block/state.go (1)

69-69: Encapsulate block height management.

The direct assignment of LastBlockHeight has been replaced with a call to SetHeight, which likely encapsulates additional logic. This change improves maintainability and consistency in managing the block height.

block/manager.go (5)

168-168: Improve error handling and logging.

The introduction of uerrors.ErrGroupGoLog enhances error handling and logging within the SubmitLoop function, centralizing error management.


171-172: Enhance logging for ProduceBlockLoop.

The use of uerrors.ErrGroupGoLog for the ProduceBlockLoop function ensures that errors are properly logged, improving maintainability.


176-176: Centralize error handling for RetrieveLoop.

The uerrors.ErrGroupGoLog function centralizes error handling for the RetrieveLoop, enhancing the robustness of the code.


179-179: Improve error logging for SyncToTargetHeightLoop.

The uerrors.ErrGroupGoLog function improves error logging for the SyncToTargetHeightLoop, ensuring better visibility of errors.


185-186: Suppress and log errors from the error group.

The errors from the error group are suppressed and logged elsewhere, streamlining the logging process and reducing clutter.

testutil/types.go (1)

232-232: Encapsulate state height management.

The direct assignment of lastBlockHeight has been replaced with a call to SetHeight, which likely encapsulates additional logic. This change improves maintainability and consistency in managing the state height.

block/executor_test.go (1)

145-145: Encapsulate height-setting logic.

The change from state.LastBlockHeight.Store(0) to state.SetHeight(0) encapsulates the height-setting logic, improving maintainability and clarity.

Verify that the SetHeight method correctly handles the height-setting logic.

Verification successful

Encapsulate height-setting logic.

The change from state.LastBlockHeight.Store(0) to state.SetHeight(0) encapsulates the height-setting logic, improving maintainability and clarity. The SetHeight method correctly handles the height-setting logic by storing the height in s.LastBlockHeight.

  • types/state.go:
    • Line 48: func (s *State) SetHeight(height uint64) {
    • Line 49: s.LastBlockHeight.Store(height)
    • Line 50: }
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of the `SetHeight` method in the `State` struct.

# Test: Search for the `SetHeight` method implementation. Expect: Correct handling of the height-setting logic.
ast-grep --lang go --pattern $'func (s *State) SetHeight(height uint64) {
  $$$
}'

Length of output: 215

block/submit.go (6)

80-80: Enhance logging for bytes produced.

The addition of the logger statement improves traceability of the bytes produced.


112-117: Improve error handling and diagnostics.

The refined error handling logic ensures that internal errors during batch creation and submission are logged and diagnosed clearly.


120-121: Ensure thread safety with atomic operations.

The use of uatomic.Uint64Sub for managing the pending bytes ensures thread safety in concurrent environments.


144-154: Add validation for height checks.

The added checks ensure that startHeight is not greater than endHeightInclusive, preventing potential logical errors in batch creation.


162-162: Log batch creation details.

The logger statement enhances visibility into the batch creation process, indicating the start and end heights.


157-157: Ensure correct batch size calculations.

The logic for creating a batch ensures that the batch size does not exceed the maximum size, preventing potential errors.

types/serialization.go (2)

282-282: Encapsulate height-setting logic.

The change from s.LastBlockHeight.Store(uint64(other.LastBlockHeight)) to s.SetHeight(uint64(other.LastBlockHeight)) encapsulates the height-setting logic, improving maintainability and clarity.


264-264: Encapsulate height retrieval logic.

The change from s.LastBlockHeight.Load() to s.Height() encapsulates the height retrieval logic, improving maintainability and clarity.

Verify that the Height method correctly handles the height retrieval logic.

Verification successful

Encapsulate height retrieval logic.

The change from s.LastBlockHeight.Load() to s.Height() encapsulates the height retrieval logic, improving maintainability and clarity. The Height method correctly returns the height of the highest block saved in the store by calling s.LastBlockHeight.Load().

  • types/state.go:
    • func (s *State) Height() uint64 { return s.LastBlockHeight.Load() }
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of the `Height` method in the `State` struct.

# Test: Search for the `Height` method implementation. Expect: Correct handling of the height retrieval logic.
ast-grep --lang go --pattern $'func (s *State) Height() int64 {
  $$$
}'

Length of output: 73


Script:

#!/bin/bash
# Search for the definition of the `Height` method in the codebase.
rg 'func (s *State) Height() int64' -A 10

# Search for the definition of the `State` struct to understand its structure.
rg 'type State struct' -A 20

Length of output: 5743


Script:

#!/bin/bash
# Search for methods in the types/state.go file that interact with the LastBlockHeight field.
rg 'LastBlockHeight' types/state.go -A 10

Length of output: 845

go.mod (1)

275-275: Dependency Addition Approved

The addition of pgregory.net/rapid v1.1.0 as an indirect dependency is approved. This library is useful for property-based testing in Go.

block/manager_test.go (1)

92-92: Improved State Management in Tests

The changes in TestInitialState to use sampleState.Height() instead of Load() improve clarity and maintainability by standardizing the method of obtaining the last block height.

Also applies to: 106-106

@danwt
Copy link
Contributor Author

danwt commented Aug 7, 2024

Had to fix test logger

@danwt danwt requested a review from zale144 August 7, 2024 16:01
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bff8909 and 51866e4.

Files selected for processing (2)
  • block/submit_loop_test.go (2 hunks)
  • utils/atomic/funcs_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • block/submit_loop_test.go
  • utils/atomic/funcs_test.go

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 51866e4 and 461b4e4.

Files selected for processing (1)
  • utils/atomic/funcs.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • utils/atomic/funcs.go

@danwt danwt merged commit 0bbe5be into main Aug 8, 2024
6 checks passed
@danwt danwt deleted the danwt/999-fix-empty-batch-creation branch August 8, 2024 10:36
omritoptix pushed a commit that referenced this pull request Aug 13, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creating empty batches causes submit loop to get stuck
3 participants