reddb-io-server 1.12.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
use super::*;
use crate::replication::cdc::ChangeRecord;
use crate::replication::logical::{ApplyMode, LogicalChangeApplier};

fn catchup_mode_from_wire(
    catchup: Option<&reddb_wire::replication::CatchupModeReply>,
) -> reddb_file::ReplicaCatchupMode {
    match catchup.map(|reply| reply.mode) {
        Some(reddb_wire::replication::CatchupMode::Reclone) => {
            reddb_file::ReplicaCatchupMode::Reclone
        }
        Some(reddb_wire::replication::CatchupMode::Wal) => reddb_file::ReplicaCatchupMode::WalOnly,
        _ => reddb_file::ReplicaCatchupMode::BaseBackupThenWal,
    }
}

fn rebootstrap_ready_message(
    catchup_mode: reddb_file::ReplicaCatchupMode,
    checkpoint_lsn: u64,
) -> String {
    format!(
        "{} staged at checkpoint_lsn={checkpoint_lsn}; restart or store swap required",
        if catchup_mode == reddb_file::ReplicaCatchupMode::Reclone {
            "reclone basebackup"
        } else {
            "basebackup"
        }
    )
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum ReplicaRejoinStartup {
    Continue {
        since_lsn: u64,
        force_reclone: bool,
    },
    Blocked {
        state: &'static str,
        message: String,
    },
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::RedDBOptions;

    #[test]
    fn catchup_mode_from_wire_defaults_to_basebackup_then_wal() {
        assert_eq!(
            catchup_mode_from_wire(None),
            reddb_file::ReplicaCatchupMode::BaseBackupThenWal
        );
        assert_eq!(
            catchup_mode_from_wire(Some(&reddb_wire::replication::CatchupModeReply {
                mode: reddb_wire::replication::CatchupMode::BaseBackupThenWal,
                available_from_lsn: Some(10),
                replica_lsn: Some(3),
                reason: None,
            })),
            reddb_file::ReplicaCatchupMode::BaseBackupThenWal
        );
        assert_eq!(
            catchup_mode_from_wire(Some(&reddb_wire::replication::CatchupModeReply {
                mode: reddb_wire::replication::CatchupMode::Reclone,
                available_from_lsn: None,
                replica_lsn: None,
                reason: Some("slot-invalidated".to_string()),
            })),
            reddb_file::ReplicaCatchupMode::Reclone
        );
    }

    #[test]
    fn replication_health_persists_catchup_mode_for_operator_state() {
        let runtime = RedDBRuntime::with_options(RedDBOptions::in_memory()).expect("runtime boots");
        runtime.persist_replication_health_with_catchup_mode(
            "reclone_required",
            "no usable basebackup is available",
            Some(10),
            Some(3),
            Some(reddb_file::ReplicaCatchupMode::Reclone),
        );

        assert_eq!(
            runtime.config_string("red.replication.state", ""),
            "reclone_required"
        );
        assert_eq!(
            runtime.config_string("red.replication.catchup_mode", ""),
            "reclone"
        );
        assert_eq!(
            runtime.config_string("red.replication.last_error", ""),
            "no usable basebackup is available"
        );
    }

    #[test]
    fn rebootstrap_ready_message_treats_reclone_as_executed_reclone_basebackup() {
        assert_eq!(
            rebootstrap_ready_message(reddb_file::ReplicaCatchupMode::Reclone, 42),
            "reclone basebackup staged at checkpoint_lsn=42; restart or store swap required"
        );
        assert_eq!(
            rebootstrap_ready_message(reddb_file::ReplicaCatchupMode::BaseBackupThenWal, 42),
            "basebackup staged at checkpoint_lsn=42; restart or store swap required"
        );
    }

    #[test]
    fn rejoin_startup_consumes_follow_wal_plan() {
        let runtime = RedDBRuntime::with_options(RedDBOptions::in_memory()).expect("runtime boots");
        runtime.inner.db.store().set_config_tree(
            "red.replication",
            &crate::json!({
                "state": "rejoin_follow_wal",
                "rejoin_start_lsn": 950,
            }),
        );

        assert_eq!(
            runtime.replica_rejoin_startup(1_200),
            ReplicaRejoinStartup::Continue {
                since_lsn: 950,
                force_reclone: false,
            }
        );
        assert_eq!(
            runtime.config_u64("red.replication.last_applied_lsn", 0),
            950
        );
        assert_eq!(
            runtime.config_string("red.replication.state", ""),
            "rejoining"
        );
    }

    #[test]
    fn rejoin_startup_blocks_until_rewind_or_reclone_is_executed() {
        let runtime = RedDBRuntime::with_options(RedDBOptions::in_memory()).expect("runtime boots");
        runtime.inner.db.store().set_config_tree(
            "red.replication",
            &crate::json!({
                "state": "rejoin_rewind_required",
                "rejoin_target_timeline": 3,
                "rejoin_rewind_to_lsn": 42,
            }),
        );

        assert_eq!(
            runtime.replica_rejoin_startup(77),
            ReplicaRejoinStartup::Blocked {
                state: "rejoin_rewind_required",
                message:
                    "timeline rejoin requires confirmed physical rewind to timeline 3 at LSN 42 before WAL apply can resume"
                        .to_string(),
            }
        );
    }

    #[test]
    fn rejoin_startup_does_not_trust_last_applied_lsn_without_rewind_confirmation() {
        let runtime = RedDBRuntime::with_options(RedDBOptions::in_memory()).expect("runtime boots");
        runtime.inner.db.store().set_config_tree(
            "red.replication",
            &crate::json!({
                "state": "rejoin_rewind_required",
                "rejoin_target_timeline": 3,
                "rejoin_rewind_to_lsn": 42,
                "last_applied_lsn": 42,
            }),
        );

        assert_eq!(
            runtime.replica_rejoin_startup(77),
            ReplicaRejoinStartup::Blocked {
                state: "rejoin_rewind_required",
                message:
                    "timeline rejoin requires confirmed physical rewind to timeline 3 at LSN 42 before WAL apply can resume"
                        .to_string(),
            }
        );
        assert_eq!(
            runtime.config_string("red.replication.state", ""),
            "rejoin_rewind_required"
        );
    }

    #[test]
    fn rejoin_startup_consumes_rewind_plan_after_confirmed_physical_rewind() {
        let runtime = RedDBRuntime::with_options(RedDBOptions::in_memory()).expect("runtime boots");
        runtime.inner.db.store().set_config_tree(
            "red.replication",
            &crate::json!({
                "state": "rejoin_rewind_required",
                "rejoin_target_timeline": 3,
                "rejoin_rewind_to_lsn": 42,
                "last_applied_lsn": 77,
            }),
        );
        runtime.mark_replica_rejoin_rewind_confirmed(3, 42);

        assert_eq!(
            runtime.replica_rejoin_startup(77),
            ReplicaRejoinStartup::Continue {
                since_lsn: 42,
                force_reclone: false,
            }
        );
        assert_eq!(
            runtime.config_string("red.replication.state", ""),
            "rejoining"
        );
        assert_eq!(
            runtime.config_u64("red.replication.last_applied_lsn", 0),
            42
        );
    }

    #[test]
    fn rejoin_startup_schedules_reclone_without_wal_apply() {
        let runtime = RedDBRuntime::with_options(RedDBOptions::in_memory()).expect("runtime boots");
        runtime.inner.db.store().set_config_tree(
            "red.replication",
            &crate::json!({
                "state": "reclone_required",
                "last_applied_lsn": 77,
            }),
        );

        assert_eq!(
            runtime.replica_rejoin_startup(77),
            ReplicaRejoinStartup::Continue {
                since_lsn: 0,
                force_reclone: true,
            }
        );
        assert_eq!(
            runtime.config_string("red.replication.state", ""),
            "reclone_required"
        );
        assert_eq!(
            runtime.config_u64("red.replication.last_applied_lsn", 0),
            77
        );
    }
}

impl RedDBRuntime {
    fn persist_replica_lsn(&self, lsn: u64) {
        self.inner.db.store().set_config_tree(
            "red.replication",
            &crate::json!({
                "last_applied_lsn": lsn
            }),
        );
    }

    /// Resolve this replica's stable identity (issue #812). The primary keys
    /// per-replica progress off this id, so it MUST be stable across reboots
    /// — a changing id would make the primary treat every restart as a brand
    /// new replica. Honours an operator-configured `red.replication.replica_id`
    /// first; otherwise generates one once and persists it so the next boot
    /// reuses the same value.
    pub(crate) fn resolve_replica_id(&self) -> String {
        let configured = self.config_string("red.replication.replica_id", "");
        if !configured.is_empty() {
            return configured;
        }
        let generated = crate::crypto::uuid::Uuid::new_v4().to_string();
        self.inner.db.store().set_config_tree(
            "red.replication",
            &crate::json!({
                "replica_id": generated.clone()
            }),
        );
        generated
    }

    fn persist_replication_health(
        &self,
        state: &str,
        last_error: &str,
        primary_lsn: Option<u64>,
        oldest_available_lsn: Option<u64>,
    ) {
        self.persist_replication_health_with_catchup_mode(
            state,
            last_error,
            primary_lsn,
            oldest_available_lsn,
            None,
        );
    }

    fn persist_replication_health_with_catchup_mode(
        &self,
        state: &str,
        last_error: &str,
        primary_lsn: Option<u64>,
        oldest_available_lsn: Option<u64>,
        catchup_mode: Option<reddb_file::ReplicaCatchupMode>,
    ) {
        self.inner.db.store().set_config_tree(
            "red.replication",
            &crate::json!({
                "state": state,
                "last_error": last_error,
                "catchup_mode": catchup_mode
                    .map(reddb_file::ReplicaCatchupMode::as_str)
                    .unwrap_or(""),
                "last_seen_primary_lsn": primary_lsn.unwrap_or(0),
                "last_seen_oldest_lsn": oldest_available_lsn.unwrap_or(0),
                "updated_at_unix_ms": SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_millis() as u64
            }),
        );
    }

    pub(crate) fn mark_replica_rejoin_rewind_confirmed(
        &self,
        target_timeline: u64,
        rewind_to_lsn: u64,
    ) {
        self.inner.db.store().set_config_tree(
            "red.replication",
            &crate::json!({
                "rejoin_rewind_confirmed_timeline": target_timeline,
                "rejoin_rewind_confirmed_lsn": rewind_to_lsn,
                "last_applied_lsn": rewind_to_lsn,
            }),
        );
    }

    fn replica_rejoin_startup(&self, since_lsn: u64) -> ReplicaRejoinStartup {
        match self.config_string("red.replication.state", "").as_str() {
            "rejoin_follow_wal" => {
                let start_lsn = self.config_u64("red.replication.rejoin_start_lsn", since_lsn);
                self.inner.db.store().set_config_tree(
                    "red.replication",
                    &crate::json!({
                        "state": "rejoining",
                        "last_applied_lsn": start_lsn,
                    }),
                );
                ReplicaRejoinStartup::Continue {
                    since_lsn: start_lsn,
                    force_reclone: false,
                }
            }
            "rejoin_rewind_required" => {
                let rewind_to_lsn = self.config_u64("red.replication.rejoin_rewind_to_lsn", 0);
                let target_timeline = self.config_u64("red.replication.rejoin_target_timeline", 0);
                let confirmed_lsn =
                    self.config_u64("red.replication.rejoin_rewind_confirmed_lsn", 0);
                let confirmed_timeline =
                    self.config_u64("red.replication.rejoin_rewind_confirmed_timeline", 0);
                if confirmed_lsn == rewind_to_lsn
                    && confirmed_timeline == target_timeline
                    && rewind_to_lsn > 0
                {
                    self.inner.db.store().set_config_tree(
                        "red.replication",
                        &crate::json!({
                            "state": "rejoining",
                            "last_applied_lsn": rewind_to_lsn,
                        }),
                    );
                    return ReplicaRejoinStartup::Continue {
                        since_lsn: rewind_to_lsn,
                        force_reclone: false,
                    };
                }
                ReplicaRejoinStartup::Blocked {
                    state: "rejoin_rewind_required",
                    message: format!(
                        "timeline rejoin requires confirmed physical rewind to timeline {target_timeline} at LSN {rewind_to_lsn} before WAL apply can resume"
                    ),
                }
            }
            "reclone_required" => ReplicaRejoinStartup::Continue {
                since_lsn: 0,
                force_reclone: true,
            },
            _ => ReplicaRejoinStartup::Continue {
                since_lsn,
                force_reclone: false,
            },
        }
    }

    pub(crate) fn run_replica_loop(&self, primary_addr: String) {
        let endpoint = if primary_addr.starts_with("http") {
            primary_addr
        } else {
            format!("http://{primary_addr}")
        };
        let poll_ms = self.inner.db.options().replication.poll_interval_ms;
        let max_count = self.inner.db.options().replication.max_batch_size;
        let mut since_lsn = self.config_u64("red.replication.last_applied_lsn", 0);
        let force_reclone;
        match self.replica_rejoin_startup(since_lsn) {
            ReplicaRejoinStartup::Continue {
                since_lsn: planned,
                force_reclone: planned_reclone,
            } => {
                since_lsn = planned;
                force_reclone = planned_reclone;
            }
            ReplicaRejoinStartup::Blocked { state, message } => {
                self.persist_replication_health(state, &message, None, None);
                return;
            }
        }
        // Issue #812 — stable identity sent on every WAL pull so the primary
        // can self-register this replica and attribute pulls to it.
        let replica_id = self.resolve_replica_id();

        let runtime = match tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
        {
            Ok(runtime) => runtime,
            Err(_) => return,
        };

        runtime.block_on(async move {
            use crate::grpc::proto::red_db_client::RedDbClient;
            use crate::grpc::proto::JsonPayloadRequest;

            let mut client = loop {
                match RedDbClient::connect(endpoint.clone()).await {
                    Ok(client) => {
                        self.persist_replication_health("connecting", "", None, None);
                        break client;
                    }
                    Err(_) => {
                        self.persist_replication_health(
                            "connecting",
                            "waiting for primary connection",
                            None,
                            None,
                        );
                        std::thread::sleep(std::time::Duration::from_millis(poll_ms.max(250)))
                    }
                }
            };

            if force_reclone {
                match self
                    .stage_primary_replica_rebootstrap_from_snapshot(&mut client, 4 * 1024 * 1024)
                    .await
                {
                    Ok(Some(checkpoint_lsn)) => {
                        self.persist_replication_health_with_catchup_mode(
                            "rebootstrap_ready",
                            &rebootstrap_ready_message(
                                reddb_file::ReplicaCatchupMode::Reclone,
                                checkpoint_lsn,
                            ),
                            None,
                            None,
                            Some(reddb_file::ReplicaCatchupMode::Reclone),
                        );
                    }
                    Ok(None) => {
                        self.persist_replication_health_with_catchup_mode(
                            "reclone_required",
                            "timeline rejoin requires reclone but no primary snapshot is available",
                            None,
                            None,
                            Some(reddb_file::ReplicaCatchupMode::Reclone),
                        );
                    }
                    Err(err) => {
                        self.persist_replication_health_with_catchup_mode(
                            "rebootstrap_error",
                            &format!("failed to stage reclone basebackup: {err}"),
                            None,
                            None,
                            Some(reddb_file::ReplicaCatchupMode::Reclone),
                        );
                    }
                }
                return;
            }

            // PLAN.md Phase 11.5 — stateful applier guards LSN
            // monotonicity across pulls. Seed with the persisted
            // `last_applied_lsn` so reboots don't lose the chain
            // pointer.
            let applier = LogicalChangeApplier::with_metrics(
                since_lsn,
                self.inner.replica_apply_metrics.clone(),
            );

            loop {
                let payload = reddb_wire::replication::WalStreamOpen {
                    since_lsn,
                    max_count,
                    replica_id: Some(replica_id.clone()),
                    await_data: true,
                    await_timeout_ms: 30_000,
                };
                let request = tonic::Request::new(JsonPayloadRequest {
                    payload_json: String::from_utf8(payload.encode_json())
                        .unwrap_or_else(|_| "{}".to_string()),
                });

                if let Ok(response) = client.pull_wal_records(request).await {
                    if let Ok(chunk) = reddb_wire::replication::WalStreamChunk::decode_json(
                        response.into_inner().payload.as_bytes(),
                    )
                    {
                        let current_lsn = Some(chunk.current_lsn);
                        let oldest_available_lsn = chunk.oldest_available_lsn;
                        if chunk.needs_rebootstrap {
                            let reason = chunk.invalidation_reason.as_deref().unwrap_or("unknown");
                            let catchup_mode = catchup_mode_from_wire(chunk.catchup.as_ref());
                            if self.config_string("red.replication.state", "")
                                == "rebootstrap_ready"
                            {
                                std::thread::sleep(std::time::Duration::from_millis(
                                    poll_ms.max(250),
                                ));
                                continue;
                            }
                            {
                                match self
                                    .stage_primary_replica_rebootstrap_from_snapshot(
                                        &mut client,
                                        4 * 1024 * 1024,
                                    )
                                    .await
                                {
                                    Ok(Some(checkpoint_lsn)) => {
                                        self.persist_replication_health_with_catchup_mode(
                                            "rebootstrap_ready",
                                            &rebootstrap_ready_message(
                                                catchup_mode,
                                                checkpoint_lsn,
                                            ),
                                            current_lsn,
                                            oldest_available_lsn,
                                            Some(catchup_mode),
                                        );
                                        std::thread::sleep(std::time::Duration::from_millis(
                                            poll_ms.max(250),
                                        ));
                                        continue;
                                    }
                                    Ok(None) => {}
                                    Err(err) => {
                                        self.persist_replication_health_with_catchup_mode(
                                            "rebootstrap_error",
                                            &format!("failed to stage basebackup rebootstrap: {err}"),
                                            current_lsn,
                                            oldest_available_lsn,
                                            Some(catchup_mode),
                                        );
                                        std::thread::sleep(std::time::Duration::from_millis(
                                            poll_ms.max(250),
                                        ));
                                        continue;
                                    }
                                }
                            }
                            self.persist_replication_health_with_catchup_mode(
                                "rebootstrap_required",
                                &format!("replication slot invalidated ({reason}); re-bootstrap required"),
                                current_lsn,
                                oldest_available_lsn,
                                Some(catchup_mode),
                            );
                            std::thread::sleep(std::time::Duration::from_millis(poll_ms.max(250)));
                            continue;
                        }
                        if since_lsn > 0
                            && oldest_available_lsn
                                .map(|oldest| oldest > since_lsn.saturating_add(1))
                                .unwrap_or(false)
                        {
                            if self.config_string("red.replication.state", "")
                                == "rebootstrap_ready"
                            {
                                std::thread::sleep(std::time::Duration::from_millis(
                                    poll_ms.max(250),
                                ));
                                continue;
                            }
                            {
                                match self
                                    .stage_primary_replica_rebootstrap_from_snapshot(
                                        &mut client,
                                        4 * 1024 * 1024,
                                    )
                                    .await
                                {
                                    Ok(Some(checkpoint_lsn)) => {
                                        self.persist_replication_health(
                                            "rebootstrap_ready",
                                            &format!(
                                                "basebackup staged at checkpoint_lsn={checkpoint_lsn}; restart or store swap required"
                                            ),
                                            current_lsn,
                                            oldest_available_lsn,
                                        );
                                        std::thread::sleep(std::time::Duration::from_millis(
                                            poll_ms.max(250),
                                        ));
                                        continue;
                                    }
                                    Ok(None) => {}
                                    Err(err) => {
                                        self.persist_replication_health(
                                            "rebootstrap_error",
                                            &format!("failed to stage basebackup rebootstrap: {err}"),
                                            current_lsn,
                                            oldest_available_lsn,
                                        );
                                        std::thread::sleep(std::time::Duration::from_millis(
                                            poll_ms.max(250),
                                        ));
                                        continue;
                                    }
                                }
                            }
                            self.persist_replication_health(
                                "rebootstrap_required",
                                "replica is behind the oldest logical WAL available on primary; re-bootstrap required",
                                current_lsn,
                                oldest_available_lsn,
                            );
                            std::thread::sleep(std::time::Duration::from_millis(poll_ms.max(250)));
                            continue;
                        }
                        {
                            let mut batch_applied_lsn = None;
                            let mut applied_relay_records: Vec<(u64, Vec<u8>)> = Vec::new();
                            let mut ack_failed = false;
                            for record in &chunk.records {
                                let Ok(change) = ChangeRecord::decode(&record.data) else {
                                    self.inner.replica_apply_metrics.record(
                                        crate::replication::logical::ApplyErrorKind::Decode,
                                    );
                                    self.persist_replication_health(
                                        "apply_error",
                                        "failed to decode logical WAL record",
                                        current_lsn,
                                        oldest_available_lsn,
                                    );
                                    continue;
                                };
                                match applier.apply(
                                    self.inner.db.as_ref(),
                                    &change,
                                    ApplyMode::Replica,
                                ) {
                                    Ok(crate::replication::logical::ApplyOutcome::Applied) => {
                                        self.invalidate_result_cache_for_table(&change.collection);
                                        since_lsn = since_lsn.max(change.lsn);
                                        self.persist_replica_lsn(since_lsn);
                                        batch_applied_lsn = Some(since_lsn);
                                        applied_relay_records.push((change.lsn, record.data.clone()));
                                    }
                                    Ok(_) => {
                                        // Idempotent / Skipped: no advance, no error.
                                    }
                                    Err(err) => {
                                        self.inner.replica_apply_metrics.record(err.kind());
                                        // Issue #205 — emit operator-grade event
                                        // for the two replication-fatal kinds. `Gap`
                                        // / `Apply` / `Decode` already persist via
                                        // `persist_replication_health`; the
                                        // OperatorEvent variants only cover the
                                        // two "stream is broken" / "follower
                                        // diverged" conditions an operator must act
                                        // on out-of-band.
                                        match &err {
                                            crate::replication::logical::LogicalApplyError::Divergence { lsn, expected: _, got: _, .. } => {
                                                crate::telemetry::operator_event::OperatorEvent::Divergence {
                                                    peer: "primary".to_string(),
                                                    leader_lsn: *lsn,
                                                    follower_lsn: since_lsn,
                                                }
                                                .emit_global();
                                            }
                                            crate::replication::logical::LogicalApplyError::Gap { last, next } => {
                                                crate::telemetry::operator_event::OperatorEvent::ReplicationBroken {
                                                    peer: "primary".to_string(),
                                                    reason: format!("stalled gap last={last} next={next}"),
                                                }
                                                .emit_global();
                                            }
                                            _ => {}
                                        }
                                        let kind = match &err {
                                            crate::replication::logical::LogicalApplyError::Gap { .. } => "stalled_gap",
                                            crate::replication::logical::LogicalApplyError::Divergence { .. } => "divergence",
                                            // Issue #835 — a stale-term record from a
                                            // returning ex-primary was fenced. The
                                            // replica stays put (no apply, no watermark
                                            // advance) until the legitimate primary's
                                            // current-term stream resumes.
                                            crate::replication::logical::LogicalApplyError::StaleTermFenced { .. } => "stale_term_fenced",
                                            _ => "apply_error",
                                        };
                                        self.persist_replication_health(
                                            kind,
                                            &format!("replica apply rejected: {err}"),
                                            current_lsn,
                                            oldest_available_lsn,
                                        );
                                        // Stop applying this batch. The
                                        // outer loop will retry on next
                                        // pull, which on a real Gap will
                                        // not magically heal — operator
                                        // must rebootstrap. For
                                        // Divergence, we explicitly do
                                        // not advance; this keeps the
                                        // replica visibly unhealthy
                                        // instead of silently swallowing
                                        // corruption.
                                        break;
                                    }
                                }
                            }
                            if let Some(applied_lsn) = batch_applied_lsn {
                                if let Err(err) = self.record_replica_relay_batch(
                                    &replica_id,
                                    &applied_relay_records,
                                    applied_lsn,
                                ) {
                                    ack_failed = true;
                                    self.persist_replication_health(
                                        "relay_error",
                                        &format!("failed to persist replica relay manifest: {err}"),
                                        current_lsn,
                                        oldest_available_lsn,
                                    );
                                }
                                if !ack_failed {
                                    let apply_errors = self.replica_apply_error_counts();
                                    let apply_errors_total =
                                        apply_errors.iter().map(|(_, count)| *count).sum::<u64>();
                                    let divergence_total = apply_errors
                                        .iter()
                                        .find(|(kind, _)| {
                                            matches!(
                                                kind,
                                                crate::replication::logical::ApplyErrorKind::Divergence
                                            )
                                        })
                                        .map(|(_, count)| *count)
                                        .unwrap_or(0);
                                    let ack_payload = reddb_wire::replication::WalStreamAck {
                                        replica_id: replica_id.clone(),
                                        applied_lsn,
                                        durable_lsn: applied_lsn,
                                        apply_errors_total,
                                        divergence_total,
                                    };
                                    let ack_request = tonic::Request::new(JsonPayloadRequest {
                                        payload_json: String::from_utf8(ack_payload.encode_json())
                                            .unwrap_or_else(|_| "{}".to_string()),
                                    });
                                    if client.ack_replica_lsn(ack_request).await.is_err() {
                                        ack_failed = true;
                                        self.persist_replication_health(
                                            "ack_error",
                                            "primary ack_replica_lsn request failed",
                                            current_lsn,
                                            oldest_available_lsn,
                                        );
                                    }
                                }
                            }
                            if ack_failed {
                                std::thread::sleep(std::time::Duration::from_millis(poll_ms));
                                continue;
                            }
                        }
                        self.persist_replication_health(
                            "healthy",
                            "",
                            current_lsn,
                            oldest_available_lsn,
                        );
                    } else {
                        self.persist_replication_health(
                            "apply_error",
                            "failed to parse pull_wal_records response",
                            None,
                            None,
                        );
                    }
                } else {
                    self.persist_replication_health(
                        "connecting",
                        "primary pull_wal_records request failed",
                        None,
                        None,
                    );
                    std::thread::sleep(std::time::Duration::from_millis(poll_ms.max(250)));
                }
            }
        });
    }
}