Skip to content

Add block fetch prefetch pipeline and improve shutdown consistency#46

Closed
freemans13 wants to merge 2 commits into
bsv-blockchain:mainfrom
freemans13:fix/improved-shutdown-consistency-for-grpc-and-kafka-consumer
Closed

Add block fetch prefetch pipeline and improve shutdown consistency#46
freemans13 wants to merge 2 commits into
bsv-blockchain:mainfrom
freemans13:fix/improved-shutdown-consistency-for-grpc-and-kafka-consumer

Conversation

@freemans13

Copy link
Copy Markdown
Collaborator

Summary

This PR introduces two major improvements:

1. Block Fetch Prefetch Pipeline

Adds a configurable prefetch mechanism that decouples block fetching from validation processing, allowing the network pipeline to stay full while validation processes earlier batches. This significantly improves catchup throughput.

Key changes:

  • New FetchPrefetchDepth setting (default: 5 batches) to control how far ahead fetching runs
  • Implements batchFetchWithPrefetch() and distributeBatchesToWorkers() for pipeline architecture
  • Uses buffered channel to naturally throttle fetches when validation falls behind
  • Falls back to original synchronous behavior when prefetchDepth=0
  • Adds detailed logging for pipeline monitoring

2. Improved Shutdown Consistency

Enhances graceful shutdown behavior across GRPC and Kafka consumer to prevent hangs and race conditions.

GRPC improvements (util/grpc.go):

  • Adds 5-second timeout for graceful shutdown
  • Falls back to forced Stop() if graceful shutdown hangs
  • Better logging for shutdown states

Kafka consumer improvements (util/kafka/kafka_consumer.go):

  • Fixes race condition where signal handler could attempt cancellation after context already done
  • Properly checks context state before cancelling

Subtree validation improvements (services/subtreevalidation/check_block_subtrees.go):

  • Adds context cancellation checking between merkle tree levels
  • Allows faster shutdown during long-running block validation

Test Plan

  • Run catchup performance tests to verify improved throughput with prefetch enabled
  • Test graceful shutdown behavior under various load conditions
  • Verify no regressions in block validation correctness
  • Test with prefetchDepth=0 to ensure backward compatibility
  • Verify GRPC shutdown doesn't hang with stuck connections
  • Test Kafka consumer shutdown under signal interrupts

🤖 Generated with Claude Code

freemans13 and others added 2 commits October 23, 2025 11:33
Decouple network block fetching from worker distribution using a two-goroutine
pipeline. This keeps the network connection saturated even when validation
is slower than fetch, improving overall catchup performance.

Changes:
- Add batchFetchWithPrefetch and distributeBatchesToWorkers functions
- Introduce FetchPrefetchDepth setting (default: 5 batches ahead)
- Add context cancellation check in subtree validation level processing

The fetcher can now run up to prefetchDepth batches ahead of distribution,
maximizing network throughput during block catchup operations.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
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.

1 participant