lnc-client 0.2.8

LANCE client library - Rust client for the LANCE streaming platform
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
//! Grouped Consumer Mode
//!
//! Provides Kafka-like consumer group coordination at the client level.
//! In grouped mode, a coordinator (main client) manages work distribution
//! among multiple worker consumers, enabling horizontal scaling.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────┐
//! │                  GroupCoordinator                       │
//! │  - Discovers topics from server                         │
//! │  - Tracks workers via heartbeat                         │
//! │  - Assigns topics using configurable strategy           │
//! │  - Rebalances on worker join/leave                      │
//! └─────────────────────────────────────────────────────────┘
//!          │              │              │
//!          ▼              ▼              ▼
//!     ┌─────────┐   ┌─────────┐   ┌─────────┐
//!     │ Worker  │   │ Worker  │   │ Worker  │
//!     │   A     │   │   B     │   │   C     │
//!     │ [T1,T2] │   │ [T3,T4] │   │ [T5,T6] │
//!     └─────────┘   └─────────┘   └─────────┘
//! ```
//!
//! # Client-Side Offset Management
//!
//! Unlike Kafka where the broker manages consumer group offsets, LANCE
//! uses client-side offset management. This:
//! - Reduces server load (no offset commit replication)
//! - Enables exactly-once semantics with local transactions
//! - Allows stateless broker scaling
//!
//! # Example
//!
//! ```rust,no_run
//! use lnc_client::{AssignmentStrategy, GroupCoordinator, GroupConfig, GroupedConsumer, WorkerConfig};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Start coordinator (typically one per consumer group)
//!     let coordinator = GroupCoordinator::start(
//!         "127.0.0.1:1992",
//!         GroupConfig::new("my-group")
//!             .with_topics(vec![1, 2, 3, 4])
//!             .with_assignment_strategy(AssignmentStrategy::RoundRobin),
//!     ).await?;
//!
//!     // Workers join the group (server_addr, coordinator_addr, config)
//!     let mut worker = GroupedConsumer::join(
//!         "127.0.0.1:1992",
//!         coordinator.join_address(),
//!         WorkerConfig::new("worker-1"),
//!     ).await?;
//!
//!     // Worker receives assignments and processes
//!     loop {
//!         // Copy assignments to avoid borrow issues
//!         let topics: Vec<u32> = worker.assignments().to_vec();
//!         for topic_id in topics {
//!             if let Some(_records) = worker.next_batch(topic_id).await? {
//!                 // Process records
//!                 worker.commit(topic_id).await?;
//!             }
//!         }
//!     }
//! }
//! ```

use std::collections::{HashMap, HashSet};
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::{Duration, Instant};

use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{RwLock, broadcast};

use crate::client::{ClientConfig, LanceClient};
use crate::consumer::{PollResult, SeekPosition};
use crate::error::{ClientError, Result};
use crate::offset::{LockFileOffsetStore, MemoryOffsetStore, OffsetStore};
use crate::standalone::{StandaloneConfig, StandaloneConsumer};

/// Assignment strategy for distributing topics among workers
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AssignmentStrategy {
    /// Distribute topics evenly across workers in round-robin fashion
    #[default]
    RoundRobin,
    /// Assign contiguous ranges of topics to each worker
    Range,
    /// Minimize reassignment when workers join/leave (sticky)
    Sticky,
}

/// Configuration for the group coordinator
#[derive(Debug, Clone)]
pub struct GroupConfig {
    /// Unique identifier for this consumer group
    pub group_id: String,
    /// Topics to consume (if empty, discovers from server)
    pub topics: Vec<u32>,
    /// Assignment strategy
    pub assignment_strategy: AssignmentStrategy,
    /// How often workers must send heartbeat
    pub heartbeat_interval: Duration,
    /// How long before a worker is considered dead
    pub session_timeout: Duration,
    /// Address for the coordinator to listen on
    pub coordinator_addr: SocketAddr,
    /// LANCE server address for topic discovery
    pub server_addr: String,
    /// Offset storage directory
    pub offset_dir: Option<PathBuf>,
}

impl GroupConfig {
    /// Create a new group configuration
    pub fn new(group_id: impl Into<String>) -> Self {
        Self {
            group_id: group_id.into(),
            topics: Vec::new(),
            assignment_strategy: AssignmentStrategy::RoundRobin,
            heartbeat_interval: Duration::from_secs(3),
            session_timeout: Duration::from_secs(30),
            coordinator_addr: "127.0.0.1:19920"
                .parse()
                .unwrap_or_else(|_| SocketAddr::from(([127, 0, 0, 1], 19920))),
            server_addr: "127.0.0.1:1992".to_string(),
            offset_dir: None,
        }
    }

    /// Set topics to consume
    pub fn with_topics(mut self, topics: Vec<u32>) -> Self {
        self.topics = topics;
        self
    }

    /// Set the assignment strategy
    pub fn with_assignment_strategy(mut self, strategy: AssignmentStrategy) -> Self {
        self.assignment_strategy = strategy;
        self
    }

    /// Set heartbeat interval
    pub fn with_heartbeat_interval(mut self, interval: Duration) -> Self {
        self.heartbeat_interval = interval;
        self
    }

    /// Set session timeout
    pub fn with_session_timeout(mut self, timeout: Duration) -> Self {
        self.session_timeout = timeout;
        self
    }

    /// Set coordinator listen address
    pub fn with_coordinator_addr(mut self, addr: SocketAddr) -> Self {
        self.coordinator_addr = addr;
        self
    }

    /// Set LANCE server address
    pub fn with_server_addr(mut self, addr: impl Into<String>) -> Self {
        self.server_addr = addr.into();
        self
    }

    /// Set offset storage directory
    pub fn with_offset_dir(mut self, dir: &Path) -> Self {
        self.offset_dir = Some(dir.to_path_buf());
        self
    }
}

/// Internal message types for coordinator-worker communication
#[derive(Debug, Clone)]
#[allow(dead_code)]
enum CoordinatorMessage {
    /// Worker joining the group
    Join { worker_id: String },
    /// Worker leaving the group
    Leave { worker_id: String },
    /// Heartbeat from worker
    Heartbeat { worker_id: String },
    /// Request current assignments
    GetAssignments { worker_id: String },
}

/// Internal response types
#[derive(Debug, Clone)]
#[allow(dead_code)]
enum CoordinatorResponse {
    /// Join accepted with initial assignments
    JoinAccepted {
        worker_id: String,
        generation: u64,
        assignments: Vec<u32>,
    },
    /// Leave acknowledged
    LeaveAcknowledged,
    /// Heartbeat acknowledged
    HeartbeatAck { generation: u64 },
    /// Current assignments
    Assignments {
        generation: u64,
        assignments: Vec<u32>,
    },
    /// Rebalance notification
    Rebalance {
        generation: u64,
        assignments: Vec<u32>,
    },
    /// Error response
    Error { message: String },
}

/// State of a worker in the group
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct WorkerState {
    worker_id: String,
    last_heartbeat: Instant,
    assignments: Vec<u32>,
    generation: u64,
}

/// Group coordinator that manages worker assignment
///
/// The coordinator:
/// - Listens for worker connections
/// - Tracks worker liveness via heartbeats
/// - Assigns topics to workers using the configured strategy
/// - Triggers rebalance when workers join/leave
pub struct GroupCoordinator {
    #[allow(dead_code)]
    config: GroupConfig,
    workers: Arc<RwLock<HashMap<String, WorkerState>>>,
    generation: Arc<AtomicU64>,
    running: Arc<AtomicBool>,
    shutdown_tx: broadcast::Sender<()>,
    join_addr: SocketAddr,
}

impl GroupCoordinator {
    /// Start the coordinator and begin listening for workers
    pub async fn start(server_addr: &str, mut config: GroupConfig) -> Result<Self> {
        config.server_addr = server_addr.to_string();

        // Discover topics if not specified
        if config.topics.is_empty() {
            let client_config = ClientConfig::new(&config.server_addr);
            let mut client = LanceClient::connect(client_config).await?;
            let topics = client.list_topics().await?;
            config.topics = topics.iter().map(|t| t.id).collect();
        }

        let workers = Arc::new(RwLock::new(HashMap::new()));
        let generation = Arc::new(AtomicU64::new(0));
        let running = Arc::new(AtomicBool::new(true));
        let (shutdown_tx, _) = broadcast::channel(1);

        let listener = TcpListener::bind(config.coordinator_addr)
            .await
            .map_err(ClientError::ConnectionFailed)?;

        let join_addr = listener.local_addr().map_err(ClientError::IoError)?;

        let coordinator = Self {
            config: config.clone(),
            workers: workers.clone(),
            generation: generation.clone(),
            running: running.clone(),
            shutdown_tx: shutdown_tx.clone(),
            join_addr,
        };

        // Spawn coordinator tasks
        let workers_clone = workers.clone();
        let generation_clone = generation.clone();
        let _running_clone = running.clone();
        let config_clone = config.clone();
        let mut shutdown_rx = shutdown_tx.subscribe();

        // Accept worker connections
        tokio::spawn(async move {
            loop {
                tokio::select! {
                    accept_result = listener.accept() => {
                        match accept_result {
                            Ok((stream, addr)) => {
                                let workers = workers_clone.clone();
                                let generation = generation_clone.clone();
                                let config = config_clone.clone();
                                tokio::spawn(async move {
                                    if let Err(e) = Self::handle_worker_connection(
                                        stream, addr, workers, generation, config
                                    ).await {
                                        tracing::warn!("Worker connection error: {}", e);
                                    }
                                });
                            }
                            Err(e) => {
                                tracing::error!("Accept error: {}", e);
                            }
                        }
                    }
                    _ = shutdown_rx.recv() => {
                        tracing::info!("Coordinator shutting down");
                        break;
                    }
                }
            }
        });

        // Spawn heartbeat checker
        let workers_checker = workers.clone();
        let generation_checker = generation.clone();
        let running_checker = running.clone();
        let session_timeout = config.session_timeout;
        let mut shutdown_rx2 = shutdown_tx.subscribe();

        tokio::spawn(async move {
            let mut interval = tokio::time::interval(Duration::from_secs(1));
            loop {
                tokio::select! {
                    _ = interval.tick() => {
                        if !running_checker.load(Ordering::Relaxed) {
                            break;
                        }
                        Self::check_worker_health(
                            &workers_checker,
                            &generation_checker,
                            session_timeout,
                        ).await;
                    }
                    _ = shutdown_rx2.recv() => {
                        break;
                    }
                }
            }
        });

        Ok(coordinator)
    }

    /// Get the address workers should connect to
    pub fn join_address(&self) -> SocketAddr {
        self.join_addr
    }

    /// Get current generation (increments on rebalance)
    pub fn generation(&self) -> u64 {
        self.generation.load(Ordering::Relaxed)
    }

    /// Get current worker count
    pub async fn worker_count(&self) -> usize {
        self.workers.read().await.len()
    }

    /// Get current assignments for all workers
    pub async fn get_assignments(&self) -> HashMap<String, Vec<u32>> {
        self.workers
            .read()
            .await
            .iter()
            .map(|(id, state)| (id.clone(), state.assignments.clone()))
            .collect()
    }

    /// Stop the coordinator
    pub fn stop(&self) {
        self.running.store(false, Ordering::Relaxed);
        let _ = self.shutdown_tx.send(());
    }

    /// Handle a worker connection
    async fn handle_worker_connection(
        stream: TcpStream,
        _addr: SocketAddr,
        workers: Arc<RwLock<HashMap<String, WorkerState>>>,
        generation: Arc<AtomicU64>,
        config: GroupConfig,
    ) -> Result<()> {
        // Simple protocol: length-prefixed JSON messages
        // In production, use proper framing like LWP
        use tokio::io::{AsyncReadExt, AsyncWriteExt};

        let (mut reader, mut writer) = stream.into_split();
        let mut buf = vec![0u8; 4096];

        loop {
            // Read message length
            let n = reader.read(&mut buf).await?;
            if n == 0 {
                break; // Connection closed
            }

            // Parse message (simplified - in production use proper serialization)
            let msg_str = std::str::from_utf8(&buf[..n])
                .map_err(|e| ClientError::ProtocolError(e.to_string()))?;

            let response = Self::process_message(msg_str, &workers, &generation, &config).await;

            // Send response
            let response_bytes = format!("{:?}", response);
            writer.write_all(response_bytes.as_bytes()).await?;
        }

        Ok(())
    }

    /// Process a coordinator message
    async fn process_message(
        msg: &str,
        workers: &Arc<RwLock<HashMap<String, WorkerState>>>,
        generation: &Arc<AtomicU64>,
        config: &GroupConfig,
    ) -> CoordinatorResponse {
        // Parse message (simplified)
        if msg.starts_with("JOIN:") {
            let worker_id = msg
                .strip_prefix("JOIN:")
                .unwrap_or("unknown")
                .trim()
                .to_string();
            Self::handle_join(worker_id, workers, generation, config).await
        } else if msg.starts_with("LEAVE:") {
            let worker_id = msg
                .strip_prefix("LEAVE:")
                .unwrap_or("unknown")
                .trim()
                .to_string();
            Self::handle_leave(worker_id, workers, generation, config).await
        } else if msg.starts_with("HEARTBEAT:") {
            let worker_id = msg
                .strip_prefix("HEARTBEAT:")
                .unwrap_or("unknown")
                .trim()
                .to_string();
            Self::handle_heartbeat(worker_id, workers, generation).await
        } else if msg.starts_with("GET_ASSIGNMENTS:") {
            let worker_id = msg
                .strip_prefix("GET_ASSIGNMENTS:")
                .unwrap_or("unknown")
                .trim()
                .to_string();
            Self::handle_get_assignments(worker_id, workers, generation).await
        } else {
            CoordinatorResponse::Error {
                message: format!("Unknown message: {}", msg),
            }
        }
    }

    /// Handle worker join
    async fn handle_join(
        worker_id: String,
        workers: &Arc<RwLock<HashMap<String, WorkerState>>>,
        generation: &Arc<AtomicU64>,
        config: &GroupConfig,
    ) -> CoordinatorResponse {
        let new_gen = generation.fetch_add(1, Ordering::SeqCst) + 1;

        {
            let mut workers_lock = workers.write().await;
            workers_lock.insert(
                worker_id.clone(),
                WorkerState {
                    worker_id: worker_id.clone(),
                    last_heartbeat: Instant::now(),
                    assignments: Vec::new(),
                    generation: new_gen,
                },
            );
        }

        // Rebalance assignments
        let _assignments = Self::rebalance(workers, config).await;

        // Get this worker's assignments
        let worker_assignments = {
            let workers_lock = workers.read().await;
            workers_lock
                .get(&worker_id)
                .map(|w| w.assignments.clone())
                .unwrap_or_default()
        };

        CoordinatorResponse::JoinAccepted {
            worker_id,
            generation: new_gen,
            assignments: worker_assignments,
        }
    }

    /// Handle worker leave
    async fn handle_leave(
        worker_id: String,
        workers: &Arc<RwLock<HashMap<String, WorkerState>>>,
        generation: &Arc<AtomicU64>,
        config: &GroupConfig,
    ) -> CoordinatorResponse {
        {
            let mut workers_lock = workers.write().await;
            workers_lock.remove(&worker_id);
        }

        generation.fetch_add(1, Ordering::SeqCst);

        // Rebalance remaining workers
        Self::rebalance(workers, config).await;

        CoordinatorResponse::LeaveAcknowledged
    }

    /// Handle worker heartbeat
    async fn handle_heartbeat(
        worker_id: String,
        workers: &Arc<RwLock<HashMap<String, WorkerState>>>,
        generation: &Arc<AtomicU64>,
    ) -> CoordinatorResponse {
        let current_gen = generation.load(Ordering::Relaxed);

        let mut workers_lock = workers.write().await;
        if let Some(worker) = workers_lock.get_mut(&worker_id) {
            worker.last_heartbeat = Instant::now();
            CoordinatorResponse::HeartbeatAck {
                generation: current_gen,
            }
        } else {
            CoordinatorResponse::Error {
                message: "Worker not found".to_string(),
            }
        }
    }

    /// Handle get assignments request
    async fn handle_get_assignments(
        worker_id: String,
        workers: &Arc<RwLock<HashMap<String, WorkerState>>>,
        generation: &Arc<AtomicU64>,
    ) -> CoordinatorResponse {
        let current_gen = generation.load(Ordering::Relaxed);
        let workers_lock = workers.read().await;

        if let Some(worker) = workers_lock.get(&worker_id) {
            CoordinatorResponse::Assignments {
                generation: current_gen,
                assignments: worker.assignments.clone(),
            }
        } else {
            CoordinatorResponse::Error {
                message: "Worker not found".to_string(),
            }
        }
    }

    /// Check worker health and trigger rebalance if needed
    async fn check_worker_health(
        workers: &Arc<RwLock<HashMap<String, WorkerState>>>,
        generation: &Arc<AtomicU64>,
        session_timeout: Duration,
    ) {
        let now = Instant::now();
        let mut dead_workers = Vec::new();

        {
            let workers_lock = workers.read().await;
            for (id, state) in workers_lock.iter() {
                if now.duration_since(state.last_heartbeat) > session_timeout {
                    dead_workers.push(id.clone());
                }
            }
        }

        if !dead_workers.is_empty() {
            let mut workers_lock = workers.write().await;
            for id in dead_workers {
                tracing::warn!("Worker {} timed out, removing from group", id);
                workers_lock.remove(&id);
            }
            generation.fetch_add(1, Ordering::SeqCst);
            // Rebalance will happen on next assignment request
        }
    }

    /// Rebalance topic assignments among workers
    async fn rebalance(
        workers: &Arc<RwLock<HashMap<String, WorkerState>>>,
        config: &GroupConfig,
    ) -> HashMap<String, Vec<u32>> {
        let mut workers_lock = workers.write().await;
        let worker_ids: Vec<String> = workers_lock.keys().cloned().collect();

        if worker_ids.is_empty() {
            return HashMap::new();
        }

        let assignments = match config.assignment_strategy {
            AssignmentStrategy::RoundRobin => Self::assign_round_robin(&config.topics, &worker_ids),
            AssignmentStrategy::Range => Self::assign_range(&config.topics, &worker_ids),
            AssignmentStrategy::Sticky => {
                // For sticky, we try to maintain existing assignments
                let existing: HashMap<String, Vec<u32>> = workers_lock
                    .iter()
                    .map(|(id, state)| (id.clone(), state.assignments.clone()))
                    .collect();
                Self::assign_sticky(&config.topics, &worker_ids, &existing)
            },
        };

        // Update worker states
        for (worker_id, topics) in &assignments {
            if let Some(worker) = workers_lock.get_mut(worker_id) {
                worker.assignments = topics.clone();
            }
        }

        assignments
    }

    /// Round-robin assignment strategy
    fn assign_round_robin(topics: &[u32], workers: &[String]) -> HashMap<String, Vec<u32>> {
        let mut assignments: HashMap<String, Vec<u32>> =
            workers.iter().map(|w| (w.clone(), Vec::new())).collect();

        for (i, topic) in topics.iter().enumerate() {
            let worker = &workers[i % workers.len()];
            if let Some(topics) = assignments.get_mut(worker) {
                topics.push(*topic);
            }
        }

        assignments
    }

    /// Range assignment strategy
    fn assign_range(topics: &[u32], workers: &[String]) -> HashMap<String, Vec<u32>> {
        let mut assignments: HashMap<String, Vec<u32>> =
            workers.iter().map(|w| (w.clone(), Vec::new())).collect();

        let topics_per_worker = topics.len() / workers.len();
        let remainder = topics.len() % workers.len();

        let mut topic_idx = 0;
        for (worker_idx, worker) in workers.iter().enumerate() {
            let extra = if worker_idx < remainder { 1 } else { 0 };
            let count = topics_per_worker + extra;

            if let Some(worker_topics) = assignments.get_mut(worker) {
                for _ in 0..count {
                    if topic_idx < topics.len() {
                        worker_topics.push(topics[topic_idx]);
                        topic_idx += 1;
                    }
                }
            }
        }

        assignments
    }

    /// Sticky assignment strategy (minimizes movement)
    fn assign_sticky(
        topics: &[u32],
        workers: &[String],
        existing: &HashMap<String, Vec<u32>>,
    ) -> HashMap<String, Vec<u32>> {
        let mut assignments: HashMap<String, Vec<u32>> =
            workers.iter().map(|w| (w.clone(), Vec::new())).collect();

        let topic_set: HashSet<u32> = topics.iter().copied().collect();
        let mut assigned: HashSet<u32> = HashSet::new();

        // First, keep existing assignments that are still valid
        for (worker, old_topics) in existing {
            if assignments.contains_key(worker) {
                for topic in old_topics {
                    if topic_set.contains(topic) && !assigned.contains(topic) {
                        if let Some(worker_topics) = assignments.get_mut(worker) {
                            worker_topics.push(*topic);
                            assigned.insert(*topic);
                        }
                    }
                }
            }
        }

        // Then distribute unassigned topics
        let unassigned: Vec<u32> = topics
            .iter()
            .filter(|t| !assigned.contains(t))
            .copied()
            .collect();

        // Find worker with fewest assignments for each unassigned topic
        for topic in unassigned {
            let min_worker = assignments
                .iter()
                .min_by_key(|(_, topics)| topics.len())
                .map(|(w, _)| w.clone());

            if let Some(worker) = min_worker {
                if let Some(worker_topics) = assignments.get_mut(&worker) {
                    worker_topics.push(topic);
                }
            }
        }

        assignments
    }
}

impl Drop for GroupCoordinator {
    fn drop(&mut self) {
        self.stop();
    }
}

/// Configuration for a grouped consumer (worker)
#[derive(Debug, Clone)]
pub struct WorkerConfig {
    /// Unique identifier for this worker
    pub worker_id: String,
    /// Maximum bytes to fetch per poll
    pub max_fetch_bytes: u32,
    /// Heartbeat interval
    pub heartbeat_interval: Duration,
    /// Offset storage directory
    pub offset_dir: Option<PathBuf>,
    /// Starting position for new topics
    pub start_position: SeekPosition,
}

impl WorkerConfig {
    /// Create a new worker configuration
    pub fn new(worker_id: impl Into<String>) -> Self {
        Self {
            worker_id: worker_id.into(),
            max_fetch_bytes: 1_048_576,
            heartbeat_interval: Duration::from_secs(3),
            offset_dir: None,
            start_position: SeekPosition::Beginning,
        }
    }

    /// Set max fetch bytes
    pub fn with_max_fetch_bytes(mut self, bytes: u32) -> Self {
        self.max_fetch_bytes = bytes;
        self
    }

    /// Set heartbeat interval
    pub fn with_heartbeat_interval(mut self, interval: Duration) -> Self {
        self.heartbeat_interval = interval;
        self
    }

    /// Set offset storage directory
    pub fn with_offset_dir(mut self, dir: &Path) -> Self {
        self.offset_dir = Some(dir.to_path_buf());
        self
    }

    /// Set starting position
    pub fn with_start_position(mut self, position: SeekPosition) -> Self {
        self.start_position = position;
        self
    }
}

/// Grouped consumer (worker) that receives assignments from a coordinator
pub struct GroupedConsumer {
    config: WorkerConfig,
    server_addr: String,
    coordinator_addr: SocketAddr,
    assignments: Vec<u32>,
    generation: u64,
    consumers: HashMap<u32, StandaloneConsumer>,
    #[allow(dead_code)]
    offset_store: Arc<dyn OffsetStore>,
    running: bool,
}

impl GroupedConsumer {
    /// Join a consumer group by connecting to the coordinator
    pub async fn join(
        server_addr: &str,
        coordinator_addr: SocketAddr,
        config: WorkerConfig,
    ) -> Result<Self> {
        // Connect to coordinator
        let mut stream = TcpStream::connect(coordinator_addr)
            .await
            .map_err(ClientError::ConnectionFailed)?;

        use tokio::io::{AsyncReadExt, AsyncWriteExt};

        // Send join message
        let join_msg = format!("JOIN:{}", config.worker_id);
        stream.write_all(join_msg.as_bytes()).await?;

        // Receive response
        let mut buf = vec![0u8; 4096];
        let n = stream.read(&mut buf).await?;
        let response = std::str::from_utf8(&buf[..n])
            .map_err(|e| ClientError::ProtocolError(e.to_string()))?;

        // Parse assignments from response (simplified)
        let (generation, assignments) = Self::parse_join_response(response)?;

        // Initialize offset store
        let offset_store: Arc<dyn OffsetStore> = if let Some(ref dir) = config.offset_dir {
            Arc::new(LockFileOffsetStore::open(dir, &config.worker_id)?)
        } else {
            Arc::new(MemoryOffsetStore::new())
        };

        // Create consumers for assigned topics
        let mut consumers = HashMap::new();
        for topic_id in &assignments {
            let standalone_config = StandaloneConfig::new(&config.worker_id, *topic_id)
                .with_max_fetch_bytes(config.max_fetch_bytes)
                .with_start_position(config.start_position);

            if let Some(ref dir) = config.offset_dir {
                let consumer = StandaloneConsumer::connect(
                    server_addr,
                    standalone_config.with_offset_dir(dir),
                )
                .await?;
                consumers.insert(*topic_id, consumer);
            } else {
                let consumer = StandaloneConsumer::connect(server_addr, standalone_config).await?;
                consumers.insert(*topic_id, consumer);
            }
        }

        Ok(Self {
            config,
            server_addr: server_addr.to_string(),
            coordinator_addr,
            assignments,
            generation,
            consumers,
            offset_store,
            running: true,
        })
    }

    /// Get current topic assignments
    pub fn assignments(&self) -> &[u32] {
        &self.assignments
    }

    /// Get current generation
    pub fn generation(&self) -> u64 {
        self.generation
    }

    /// Receive the next batch for a specific assigned topic.
    pub async fn next_batch(&mut self, topic_id: u32) -> Result<Option<PollResult>> {
        if let Some(consumer) = self.consumers.get_mut(&topic_id) {
            consumer.next_batch().await
        } else {
            Err(ClientError::InvalidResponse(format!(
                "Topic {} not assigned to this worker",
                topic_id
            )))
        }
    }

    /// Primary consume interface alias for a specific assigned topic.
    #[inline]
    pub async fn consume(&mut self, topic_id: u32) -> Result<Option<PollResult>> {
        self.next_batch(topic_id).await
    }

    /// Compatibility wrapper for callers still using polling terminology.
    #[inline]
    pub async fn poll(&mut self, topic_id: u32) -> Result<Option<PollResult>> {
        self.next_batch(topic_id).await
    }

    /// Commit offset for a specific topic
    pub async fn commit(&mut self, topic_id: u32) -> Result<()> {
        if let Some(consumer) = self.consumers.get_mut(&topic_id) {
            consumer.commit().await
        } else {
            Err(ClientError::InvalidResponse(format!(
                "Topic {} not assigned to this worker",
                topic_id
            )))
        }
    }

    /// Commit all topics
    pub async fn commit_all(&mut self) -> Result<()> {
        for consumer in self.consumers.values_mut() {
            consumer.commit().await?;
        }
        Ok(())
    }

    /// Send heartbeat to coordinator
    pub async fn heartbeat(&mut self) -> Result<u64> {
        let mut stream = TcpStream::connect(self.coordinator_addr)
            .await
            .map_err(ClientError::ConnectionFailed)?;

        use tokio::io::{AsyncReadExt, AsyncWriteExt};

        let msg = format!("HEARTBEAT:{}", self.config.worker_id);
        stream.write_all(msg.as_bytes()).await?;

        let mut buf = vec![0u8; 1024];
        let n = stream.read(&mut buf).await?;
        let response = std::str::from_utf8(&buf[..n])
            .map_err(|e| ClientError::ProtocolError(e.to_string()))?;

        // Check if generation changed (rebalance needed)
        let new_gen = Self::parse_heartbeat_response(response)?;

        if new_gen > self.generation {
            // Fetch new assignments
            self.refresh_assignments().await?;
        }

        Ok(self.generation)
    }

    /// Refresh assignments from coordinator
    async fn refresh_assignments(&mut self) -> Result<()> {
        let mut stream = TcpStream::connect(self.coordinator_addr)
            .await
            .map_err(ClientError::ConnectionFailed)?;

        use tokio::io::{AsyncReadExt, AsyncWriteExt};

        let msg = format!("GET_ASSIGNMENTS:{}", self.config.worker_id);
        stream.write_all(msg.as_bytes()).await?;

        let mut buf = vec![0u8; 4096];
        let n = stream.read(&mut buf).await?;
        let response = std::str::from_utf8(&buf[..n])
            .map_err(|e| ClientError::ProtocolError(e.to_string()))?;

        let (generation, new_assignments) = Self::parse_assignments_response(response)?;

        // Update assignments
        let old_set: HashSet<u32> = self.assignments.iter().copied().collect();
        let new_set: HashSet<u32> = new_assignments.iter().copied().collect();

        // Remove consumers for revoked topics
        for topic_id in old_set.difference(&new_set) {
            if let Some(consumer) = self.consumers.remove(topic_id) {
                let _ = consumer.close().await;
            }
        }

        // Add consumers for new topics
        for topic_id in new_set.difference(&old_set) {
            let standalone_config = StandaloneConfig::new(&self.config.worker_id, *topic_id)
                .with_max_fetch_bytes(self.config.max_fetch_bytes)
                .with_start_position(self.config.start_position);

            let consumer = if let Some(ref dir) = self.config.offset_dir {
                StandaloneConsumer::connect(
                    &self.server_addr,
                    standalone_config.with_offset_dir(dir),
                )
                .await?
            } else {
                StandaloneConsumer::connect(&self.server_addr, standalone_config).await?
            };

            self.consumers.insert(*topic_id, consumer);
        }

        self.assignments = new_assignments;
        self.generation = generation;

        Ok(())
    }

    /// Leave the consumer group
    pub async fn leave(mut self) -> Result<()> {
        // Commit all offsets
        self.commit_all().await?;

        // Notify coordinator
        let mut stream = TcpStream::connect(self.coordinator_addr)
            .await
            .map_err(ClientError::ConnectionFailed)?;

        use tokio::io::AsyncWriteExt;

        let msg = format!("LEAVE:{}", self.config.worker_id);
        stream.write_all(msg.as_bytes()).await?;

        // Close all consumers
        for (_, consumer) in self.consumers.drain() {
            let _ = consumer.close().await;
        }

        self.running = false;
        Ok(())
    }

    /// Parse join response (simplified)
    fn parse_join_response(response: &str) -> Result<(u64, Vec<u32>)> {
        // Format: "JoinAccepted { worker_id: ..., generation: N, assignments: [1, 2, 3] }"
        // This is a simplified parser - production would use proper serialization

        let generation = response
            .find("generation: ")
            .and_then(|i| {
                let start = i + 12;
                let end = response[start..].find(',')?;
                response[start..start + end].parse().ok()
            })
            .unwrap_or(0);

        let assignments = response
            .find("assignments: [")
            .map(|i| {
                let start = i + 14;
                let end = response[start..].find(']').unwrap_or(0);
                response[start..start + end]
                    .split(',')
                    .filter_map(|s| s.trim().parse().ok())
                    .collect()
            })
            .unwrap_or_default();

        Ok((generation, assignments))
    }

    /// Parse heartbeat response
    fn parse_heartbeat_response(response: &str) -> Result<u64> {
        response
            .find("generation: ")
            .and_then(|i| {
                let start = i + 12;
                let end = response[start..]
                    .find([',', ' ', '}'])
                    .unwrap_or(response.len() - start);
                response[start..start + end].parse().ok()
            })
            .ok_or_else(|| ClientError::ProtocolError("Invalid heartbeat response".to_string()))
    }

    /// Parse assignments response
    fn parse_assignments_response(response: &str) -> Result<(u64, Vec<u32>)> {
        Self::parse_join_response(response)
    }
}

impl std::fmt::Debug for GroupedConsumer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GroupedConsumer")
            .field("worker_id", &self.config.worker_id)
            .field("generation", &self.generation)
            .field("assignments", &self.assignments)
            .field("running", &self.running)
            .finish()
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn test_group_config_defaults() {
        let config = GroupConfig::new("test-group");

        assert_eq!(config.group_id, "test-group");
        assert!(config.topics.is_empty());
        assert_eq!(config.assignment_strategy, AssignmentStrategy::RoundRobin);
    }

    #[test]
    fn test_worker_config_defaults() {
        let config = WorkerConfig::new("worker-1");

        assert_eq!(config.worker_id, "worker-1");
        assert_eq!(config.max_fetch_bytes, 1_048_576);
    }

    #[test]
    fn test_round_robin_assignment() {
        let topics = vec![1, 2, 3, 4, 5, 6];
        let workers = vec!["w1".to_string(), "w2".to_string(), "w3".to_string()];

        let assignments = GroupCoordinator::assign_round_robin(&topics, &workers);

        assert_eq!(assignments.get("w1"), Some(&vec![1, 4]));
        assert_eq!(assignments.get("w2"), Some(&vec![2, 5]));
        assert_eq!(assignments.get("w3"), Some(&vec![3, 6]));
    }

    #[test]
    fn test_range_assignment() {
        let topics = vec![1, 2, 3, 4, 5, 6, 7];
        let workers = vec!["w1".to_string(), "w2".to_string(), "w3".to_string()];

        let assignments = GroupCoordinator::assign_range(&topics, &workers);

        // 7 topics / 3 workers = 2 each + 1 remainder
        // w1 gets 3, w2 gets 2, w3 gets 2
        assert_eq!(assignments.get("w1").map(|v| v.len()), Some(3));
        assert_eq!(assignments.get("w2").map(|v| v.len()), Some(2));
        assert_eq!(assignments.get("w3").map(|v| v.len()), Some(2));
    }

    #[test]
    fn test_sticky_assignment_preserves_existing() {
        let topics = vec![1, 2, 3, 4];
        let workers = vec!["w1".to_string(), "w2".to_string()];

        let mut existing = HashMap::new();
        existing.insert("w1".to_string(), vec![1, 2]);
        existing.insert("w2".to_string(), vec![3, 4]);

        let assignments = GroupCoordinator::assign_sticky(&topics, &workers, &existing);

        // Should preserve existing assignments
        assert_eq!(assignments.get("w1"), Some(&vec![1, 2]));
        assert_eq!(assignments.get("w2"), Some(&vec![3, 4]));
    }

    #[test]
    fn test_assignment_strategy_default() {
        assert_eq!(
            AssignmentStrategy::default(),
            AssignmentStrategy::RoundRobin
        );
    }
}