Skip to content

Conversation

@iuwqyir
Copy link
Contributor

@iuwqyir iuwqyir commented Apr 16, 2025

TL;DR

Implemented atomic block data operations to improve data consistency during reorgs and commits.

What changed?

  • Created a new ReplaceBlockData method in ClickHouse storage that atomically handles both deletion and insertion of block data
  • Replaced separate DeleteBlockData and InsertBlockData calls in the reorg handler with a single atomic ReplaceBlockData call
  • Reordered the commit process to ensure data is saved to main storage before publishing to Kafka to prevent duplicate publishing
  • Added new ClickHouse tables and materialized views to support the atomic operations:
    • Created inserts_null_table to store block data in a denormalized format
    • Added materialized views to transform this data into the normalized tables

How to test?

  1. Run the application with a chain that experiences frequent reorgs
  2. Verify that block data remains consistent during reorgs
  3. Check that no data is lost when blocks are replaced during reorgs
  4. Verify that Kafka messages are only published after data is successfully committed to storage

Why make this change?

The previous implementation had a race condition where block data could be inconsistent during reorgs. If the process failed between deleting old data and inserting new data, the database would be left in an inconsistent state. This change makes the replace operation atomic, ensuring data consistency even if failures occur.

Additionally, by ensuring data is saved to storage before publishing to Kafka, we prevent situations where consumers might receive notifications about data that isn't yet available in the database.

Copy link
Contributor Author

iuwqyir commented Apr 16, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@iuwqyir iuwqyir changed the title make inserts and reorgs atomic make inserts and reorg handling atomic Apr 16, 2025
@iuwqyir iuwqyir changed the title make inserts and reorg handling atomic Make inserts and reorg handling atomic Apr 16, 2025
@iuwqyir iuwqyir requested review from AmineAfia and catalyst17 April 16, 2025 23:03
@iuwqyir iuwqyir marked this pull request as ready for review April 16, 2025 23:03
@iuwqyir iuwqyir merged commit 183efdb into main Apr 17, 2025
5 checks passed
@iuwqyir iuwqyir deleted the 04-17-make_inserts_and_reorgs_atomic branch April 17, 2025 20:05
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