Skip to content

Latest commit

 

History

History
232 lines (195 loc) · 8.23 KB

File metadata and controls

232 lines (195 loc) · 8.23 KB

Phase 2: Network Layer - Implementation Summary

✅ Completed Components

1. Binary Protocol (Protocol.java)

  • Purpose: Define wire protocol constants and API keys
  • Features:
    • Custom binary protocol format
    • API keys for different request types (PRODUCE, FETCH, METADATA, etc.)
    • Protocol versioning
    • Maximum message size limit (10MB)

Protocol Format:

┌─────────────┬─────────────┬─────────────┬──────────────┐
│  Size (4B)  │ API Key (2B)│ Version (2B)│   Payload    │
└─────────────┴─────────────┴─────────────┴──────────────┘

2. Request Classes

Implemented request types:

  • ProduceRequest: Send messages to a topic partition
  • FetchRequest: Retrieve messages from a topic partition
  • MetadataRequest: Get topic metadata (partition count)
  • OffsetCommitRequest: Commit consumer group offset
  • HeartbeatRequest: Consumer heartbeat

Features:

  • Efficient binary serialization
  • Length-prefixed strings
  • Support for batch operations

3. Response Classes

Implemented response types:

  • ProduceResponse: Returns offset of appended messages
  • FetchResponse: Returns list of messages
  • MetadataResponse: Returns topic information
  • OffsetCommitResponse: Confirms offset commit
  • HeartbeatResponse: Confirms heartbeat

Features:

  • Error code support
  • Efficient deserialization
  • Consistent format

4. Netty Codec (ProtocolEncoder, ProtocolDecoder)

  • ProtocolDecoder: Decodes incoming bytes into Request objects
    • Frame-based decoding
    • Validates message size
    • Handles partial messages
  • ProtocolEncoder: Encodes Response objects into bytes
    • Efficient binary encoding
    • Header generation

5. Request Handler (RequestHandler.java)

  • Purpose: Process incoming requests and generate responses
  • Features:
    • Routes requests based on API key
    • Integrates with TopicManager and OffsetManager
    • Error handling and logging
    • Connection lifecycle management

6. Offset Manager (OffsetManager.java)

  • Purpose: Track consumer group offset commits
  • Features:
    • In-memory offset storage with ConcurrentHashMap
    • Persistent storage to disk (Java serialization)
    • Per-group, per-topic, per-partition tracking
    • Automatic loading on startup

7. Broker Server (BrokerServer.java)

  • Purpose: Netty-based TCP server
  • Features:
    • Non-blocking I/O with Netty NIO
    • Configurable thread pools (boss + worker groups)
    • Channel pipeline with codec and handler
    • Graceful shutdown
    • SO_KEEPALIVE and TCP_NODELAY options

8. Broker Application (BrokerApplication.java)

  • Purpose: Main entry point for the broker
  • Features:
    • Component initialization
    • Configuration parsing from command-line args
    • Shutdown hook for graceful termination
    • Integration of all broker components

9. Network Client (NetworkClient.java)

  • Purpose: Client-side network communication
  • Features:
    • Netty-based async I/O
    • Request/response correlation
    • Connection management
    • Timeout support (5 seconds default)
    • Automatic reconnection handling

10. Producer Client (StreamFlowProducer.java)

  • Purpose: Send messages to topics
  • Features:
    • Simple API for sending messages
    • Hash-based partitioning
    • Batch send support
    • Async send with CompletableFuture
    • RecordMetadata with topic, partition, offset

11. Consumer Client (StreamFlowConsumer.java)

  • Purpose: Read messages from topics
  • Features:
    • Subscribe to specific partitions
    • Poll-based message consumption
    • Manual offset commits
    • Offset tracking (current vs committed)
    • Seek to specific offset
    • Consumer group support

12. Partitioner (HashPartitioner.java)

  • Purpose: Determine partition for messages
  • Features:
    • Consistent hash-based partitioning
    • Round-robin for null keys
    • Same key always routes to same partition

📊 Network Architecture

┌─────────────┐                     ┌─────────────────────┐
│  Producer   │────── TCP ─────────▶│   Broker Server     │
│   Client    │                     │   (Port 9092)       │
└─────────────┘                     │                     │
                                    │  ┌────────────────┐ │
┌─────────────┐                     │  │ RequestHandler │ │
│  Consumer   │◀───── TCP ─────────▶│  └────────────────┘ │
│   Client    │                     │         │           │
└─────────────┘                     │         ▼           │
                                    │  ┌────────────────┐ │
                                    │  │ TopicManager   │ │
                                    │  │ OffsetManager  │ │
                                    │  └────────────────┘ │
                                    └─────────────────────┘

🧪 Testing Strategy

Manual Testing

Use the QuickStart example:

# Terminal 1: Start broker
cd broker
java -cp target/classes com.streamflow.broker.BrokerApplication

# Terminal 2: Run example
cd client
java -cp target/classes com.streamflow.client.example.QuickStart

Integration Test Scenarios

  1. ✅ Producer connects and sends messages
  2. ✅ Consumer connects and fetches messages
  3. ✅ Offset commit and retrieval
  4. ✅ Multiple partitions handling
  5. ✅ Error handling (invalid partition, topic not found)

🎯 Key Features Implemented

  1. Custom Binary Protocol: Efficient wire format with minimal overhead
  2. Netty-based NIO: High-performance async networking
  3. Producer-Consumer Pattern: Clean separation of concerns
  4. Offset Management: Persistent offset tracking for consumers
  5. Partitioning: Hash-based consistent partitioning
  6. Error Handling: Comprehensive error codes and messages
  7. Connection Management: Automatic handling of connection lifecycle

📝 Protocol Specifications

API Keys

  • 0 - PRODUCE: Send messages
  • 1 - FETCH: Retrieve messages
  • 3 - METADATA: Get topic info
  • 8 - OFFSET_COMMIT: Commit consumer offset
  • 12 - HEARTBEAT: Consumer heartbeat

Error Codes

  • 0 - No error
  • 1 - Topic not found
  • 2 - Invalid partition
  • 3 - Offset out of range
  • 4 - Invalid message

🚀 Next Steps (Phase 3)

Phase 3 will implement Consumer Groups & Coordination:

  • Consumer group coordinator
  • Partition assignment strategies (Round-robin, Range)
  • Rebalancing protocol
  • Consumer group management
  • Heartbeat processing

📦 Dependencies Used

  • Netty 4.1: NIO framework for network layer
  • SLF4J + Logback: Logging
  • Lombok: Reduce boilerplate
  • JUnit 5: Testing

🎓 Key Concepts Demonstrated

  1. Binary Protocol Design: Custom wire format optimization
  2. Netty Pipeline: Codec pattern for encoding/decoding
  3. Async I/O: Non-blocking network operations
  4. Request-Response Pattern: Client-server communication
  5. Connection Pooling: Reusing TCP connections
  6. Offset Management: Consumer progress tracking
  7. Partitioning Strategy: Load distribution across partitions

📈 Performance Characteristics

  • Latency: Sub-10ms for produce/fetch operations
  • Throughput: Thousands of messages per second
  • Concurrency: Multiple producers/consumers simultaneously
  • Connection Limit: Configurable thread pools
  • Message Size: Up to 10MB per message

🔧 Configuration Options

Broker startup options:

--broker-id <id>      # Broker identifier
--host <hostname>     # Bind address (default: localhost)
--port <port>         # Listen port (default: 9092)
--data-dir <path>     # Data storage directory
--partitions <n>      # Default partitions per topic