mongreldb-cluster 0.64.4

MongrelDB cluster runtime: node identity, meta group, tablets, placement (Stages 2-3).
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
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
//! Node runtime integration tests (spec sections 11.1, 12.1-12.4, 12.7).
//!
//! Real multi-node clusters over loopback TCP (plaintext
//! [`TransportSecurity::PlaintextForTesting`]): a three-node runtime cluster
//! bootstraps the meta group, joins nodes 2/3 through
//! [`MetaGroup::add_member`], creates a three-replica tablet group spanning
//! the nodes, proposes writes through the tablet leader, reads on followers
//! with read-your-writes tokens (spec section 11.4), restarts one node into
//! rejoin + catch-up, and shuts one node down gracefully while quorum writes
//! continue. A second test drives the section 12.7 replica-join workflow
//! (add learner, catch up, promote) onto a fourth node.

use std::path::{Path, PathBuf};
use std::time::Duration;

use mongreldb_cluster::bootstrap::{
    cluster_init, cluster_join, InitRequest, JoinInvite, TrustConfig,
};
use mongreldb_cluster::merge::{MergeError, MergePhase};
use mongreldb_cluster::meta::{
    DatabaseDescriptor, DatabaseState, MetaCommand, MetaError, MetaRejectionReason,
    TableSchemaRecord,
};
use mongreldb_cluster::network::{TcpTransport, TransportConfig, TransportSecurity};
use mongreldb_cluster::node::{
    BuildVersion, Locality, NodeCapacity, NodeDescriptor, NodeIdentity, NodeState, VersionInfo,
};
use mongreldb_cluster::runtime::{
    GroupTiming, MetaMembership, NodeRuntime, NodeRuntimeConfig, RuntimeError,
};
use mongreldb_cluster::split::{retry_guidance, RetryGuidance, SplitError, SplitPhase};
use mongreldb_cluster::tablet::{
    check_generation, find_tablet_for_key, Bound, ColumnId, Key, PartitionBounds,
    ReplicaDescriptor, ReplicaRole, RoutingError, TablePartitioningRecord, TabletDescriptor,
    TabletState,
};
use mongreldb_consensus::error::ConsensusError;
use mongreldb_consensus::group::{ConsensusGroup, GroupCommitReceipt};
use mongreldb_consensus::identity::{raft_node_id, CommandKind, RaftNodeId};
use mongreldb_consensus::read::{ReadConsistency, SessionToken};
use mongreldb_log::commit_log::ExecutionControl;
use mongreldb_log::envelope::CommandEnvelope;
use mongreldb_types::hlc::HlcTimestamp;
use mongreldb_types::ids::{
    ClusterId, DatabaseId, MetadataVersion, NodeId, RaftGroupId, SchemaVersion, TableId, TabletId,
};
use std::collections::{BTreeMap, BTreeSet};
use tempfile::TempDir;

const LEADER_TIMEOUT: Duration = Duration::from_secs(15);
/// Maintenance envelopes ride `CommandKind::Maintenance` (envelope command
/// type 3 per `mongreldb-core` `replicated_apply`); the engine apply sink
/// treats them as documented no-ops, so they exercise the quorum write path
/// (propose, replicate, commit, apply, watermark) without needing core row
/// payloads.
const COMMAND_TYPE_MAINTENANCE: u32 = 3;

// Dummy PEM armor: `cluster init` / `cluster join` validate trust material
// structurally, and these tests run plaintext transport, so the content is
// inert (mirrors `bootstrap.rs`'s own tests).
const CA_PEM: &str = "-----BEGIN CERTIFICATE-----\nY2E=\n-----END CERTIFICATE-----\n";
const CERT_PEM: &str = "-----BEGIN CERTIFICATE-----\nbm9kZQ==\n-----END CERTIFICATE-----\n";
const KEY_PEM: &str = "-----BEGIN PRIVATE KEY-----\nc2VjcmV0\n-----END PRIVATE KEY-----\n";

fn trust() -> TrustConfig {
    TrustConfig::from_pems(
        CA_PEM.to_owned(),
        CERT_PEM.to_owned(),
        KEY_PEM.to_owned(),
        vec![NodeId::from_bytes([0xFF; 16])],
    )
    .unwrap()
}

fn csprng() -> impl FnMut(&mut [u8]) -> Result<(), getrandom::Error> {
    getrandom::getrandom
}

/// A free loopback address (`127.0.0.1:<port>`), pre-allocated by binding
/// and releasing port 0.
fn free_addr() -> String {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    listener.local_addr().unwrap().to_string()
}

fn fast_timing() -> GroupTiming {
    GroupTiming {
        heartbeat_interval: Duration::from_millis(100),
        election_timeout_min: Duration::from_millis(300),
        election_timeout_max: Duration::from_millis(600),
        install_snapshot_timeout: Duration::from_millis(2_000),
    }
}

fn transport_config() -> TransportConfig {
    TransportConfig {
        connect_timeout: Duration::from_millis(500),
        rpc_timeout: Duration::from_secs(2),
        snapshot_timeout: Duration::from_secs(5),
        connect_attempts: 5,
        reconnect_backoff: Duration::from_millis(10),
        max_frame_bytes: 16 * 1024 * 1024,
        max_connections: 64,
        handshake_timeout: Duration::from_secs(2),
        shutdown_grace: Duration::from_secs(2),
    }
}

fn descriptor(node_id: NodeId, address: &str) -> NodeDescriptor {
    NodeDescriptor {
        node_id,
        rpc_address: address.to_owned(),
        locality: Locality::default(),
        capacity: NodeCapacity::default(),
        state: NodeState::Up,
        version: BuildVersion::current(),
        version_info: VersionInfo::current(),
    }
}

fn runtime_config(
    node_data: PathBuf,
    address: &str,
    peers: &[(NodeId, String)],
    meta: Option<MetaMembership>,
) -> NodeRuntimeConfig {
    NodeRuntimeConfig {
        node_data,
        security: TransportSecurity::PlaintextForTesting,
        transport: transport_config(),
        listen_address: address.to_owned(),
        rpc_address: Some(address.to_owned()),
        peers: peers.to_vec(),
        meta,
        timing: Some(fast_timing()),
    }
}

/// Provisions one node directory: `cluster init` when `join` is `None`,
/// `cluster join` into `(cluster_id, endpoints)` otherwise.
fn provision_node(
    node_data: &Path,
    address: &str,
    join: Option<&(ClusterId, Vec<String>)>,
) -> NodeIdentity {
    match join {
        None => {
            let report = cluster_init(
                node_data,
                &InitRequest {
                    rpc_address: address.to_owned(),
                    locality: "region=test,zone=a".parse().unwrap(),
                    capacity: NodeCapacity::default(),
                    trust: trust(),
                },
                &mut csprng(),
            )
            .unwrap();
            report.identity
        }
        Some((cluster_id, endpoints)) => {
            let report = cluster_join(
                node_data,
                &JoinInvite {
                    cluster_id: *cluster_id,
                    member_endpoints: endpoints.clone(),
                    trust: trust(),
                },
                &mut csprng(),
            )
            .unwrap();
            report.identity
        }
    }
}

fn command_id(seq: u64) -> [u8; 16] {
    let mut id = [0u8; 16];
    id[..8].copy_from_slice(&seq.to_le_bytes());
    id
}

fn tablet_descriptor(
    tablet_id: TabletId,
    raft_group_id: RaftGroupId,
    table_id: TableId,
    replicas: Vec<ReplicaDescriptor>,
    generation: u64,
) -> TabletDescriptor {
    TabletDescriptor {
        tablet_id,
        table_id,
        database_id: DatabaseId::ZERO,
        raft_group_id,
        partition: PartitionBounds::unbounded(),
        replicas,
        leader_hint: None,
        generation,
        state: TabletState::Active,
    }
}

fn voter(node_id: NodeId, raft_node_id: RaftNodeId) -> ReplicaDescriptor {
    ReplicaDescriptor {
        node_id,
        role: ReplicaRole::Voter,
        raft_node_id,
    }
}

async fn propose_write(group: &ConsensusGroup<TcpTransport>, seq: u64) -> GroupCommitReceipt {
    let envelope = CommandEnvelope::new(
        COMMAND_TYPE_MAINTENANCE,
        command_id(seq),
        format!("write-{seq}").into_bytes(),
    );
    group
        .propose(
            CommandKind::Maintenance,
            envelope,
            &ExecutionControl::default(),
        )
        .await
        .unwrap()
}

fn session_token(
    group: &ConsensusGroup<TcpTransport>,
    receipt: &GroupCommitReceipt,
) -> SessionToken {
    SessionToken {
        group_id: group.group_id().to_owned(),
        commit_index: receipt.position.index,
        commit_ts: receipt.commit_ts,
    }
}

/// The runtime (of `nodes`) whose tablet group replica currently leads.
async fn tablet_leader<'a>(nodes: &[&'a NodeRuntime], tablet_id: TabletId) -> &'a NodeRuntime {
    let leader = nodes
        .iter()
        .find_map(|node| node.tablet_group(tablet_id))
        .expect("a tablet group")
        .wait_leader(LEADER_TIMEOUT)
        .await
        .unwrap();
    nodes
        .iter()
        .find(|node| {
            node.tablet_group(tablet_id)
                .is_some_and(|group| group.node_id() == leader)
        })
        .copied()
        .expect("the leader is one of the runtimes")
}

async fn create_bootstrapped_tablet(
    nodes: &mut [&mut NodeRuntime],
    tablet: &TabletDescriptor,
    partitioning: &TablePartitioningRecord,
    peers: &[(NodeId, String)],
    control: &ExecutionControl,
) {
    let deadline = std::time::Instant::now() + LEADER_TIMEOUT;
    loop {
        let leader = nodes[0]
            .meta_group()
            .unwrap()
            .group()
            .wait_leader(LEADER_TIMEOUT)
            .await
            .unwrap();
        let runtime = nodes
            .iter()
            .position(|node| raft_node_id(&node.identity().node_id) == leader)
            .expect("the meta leader is one of the runtimes");
        match nodes[runtime]
            .create_tablet(tablet, partitioning, Some(peers), true, control)
            .await
        {
            Ok(()) => return,
            Err(error) if is_meta_not_leader(&error) && std::time::Instant::now() < deadline => {
                tokio::time::sleep(Duration::from_millis(20)).await;
            }
            Err(error) => panic!("create_tablet failed: {error}"),
        }
    }
}

/// Publishes the database and table schema the tablet descriptor references
/// (meta apply enforces the reference).
async fn publish_schema(node: &NodeRuntime, database_id: DatabaseId, table_id: TableId) {
    let control = ExecutionControl::default();
    let meta = node.meta_group().unwrap();
    meta.propose(
        command_id(90),
        MetaCommand::CreateDatabase {
            descriptor: DatabaseDescriptor {
                database_id,
                name: "app".to_owned(),
                created_at: HlcTimestamp::ZERO,
                state: DatabaseState::Online,
                metadata_version: MetadataVersion::ZERO,
            },
        },
        &control,
    )
    .await
    .unwrap();
    meta.propose(
        command_id(91),
        MetaCommand::SetTableSchema {
            record: TableSchemaRecord {
                table_id,
                database_id,
                schema_version: SchemaVersion::new(1),
                schema: serde_json::json!({"columns": [{"name": "pk", "type": "u64"}]}),
                metadata_version: MetadataVersion::ZERO,
            },
        },
        &control,
    )
    .await
    .unwrap();
}

async fn publish_tablet_descriptor_on_meta_leader(
    nodes: &[&NodeRuntime],
    descriptor: &TabletDescriptor,
    control: &ExecutionControl,
) {
    let deadline = std::time::Instant::now() + LEADER_TIMEOUT;
    loop {
        let leader = nodes[0]
            .meta_group()
            .unwrap()
            .group()
            .wait_leader(LEADER_TIMEOUT)
            .await
            .unwrap();
        let runtime = nodes
            .iter()
            .find(|node| raft_node_id(&node.identity().node_id) == leader)
            .expect("the meta leader is one of the runtimes");
        match runtime.publish_tablet_descriptor(descriptor, control).await {
            Ok(_) => return,
            Err(error) if is_meta_not_leader(&error) && std::time::Instant::now() < deadline => {
                tokio::time::sleep(Duration::from_millis(20)).await;
            }
            Err(error) => panic!("publish_tablet_descriptor failed: {error}"),
        }
    }
}

async fn wait_for_tablet_descriptor(node: &NodeRuntime, tablet_id: TabletId) -> TabletDescriptor {
    let deadline = std::time::Instant::now() + LEADER_TIMEOUT;
    loop {
        if let Some(descriptor) = node
            .meta_group()
            .unwrap()
            .state()
            .tablet(tablet_id)
            .cloned()
        {
            return descriptor;
        }
        assert!(
            std::time::Instant::now() < deadline,
            "tablet descriptor did not reach node {}",
            node.identity().node_id
        );
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
}

/// The heavy multi-node TCP tests run one at a time: parallel 3-node
/// clusters with sub-second election timers starve each other's heartbeats
/// under full-suite load and churn leadership, which the protocols tolerate
/// but these fixed-driver flows should not have to absorb.
static SERIAL_CLUSTER_TESTS: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

/// Acquires the multi-node-cluster test lock (see [`SERIAL_CLUSTER_TESTS`]).
async fn serial_cluster_lock() -> tokio::sync::MutexGuard<'static, ()> {
    SERIAL_CLUSTER_TESTS.lock().await
}

#[tokio::test]
async fn three_node_runtime_cluster_end_to_end() {
    let _serial = serial_cluster_lock().await;
    let tmp = TempDir::new().unwrap();
    let dirs: Vec<PathBuf> = (1..=3)
        .map(|index| tmp.path().join(format!("node-{index}")))
        .collect();
    let addrs: Vec<String> = (1..=3).map(|_| free_addr()).collect();

    // Provision identities: node 1 inits the cluster; nodes 2/3 join it.
    let identity1 = provision_node(&dirs[0], &addrs[0], None);
    let invite = (identity1.cluster_id, addrs.clone());
    let identity2 = provision_node(&dirs[1], &addrs[1], Some(&invite));
    let identity3 = provision_node(&dirs[2], &addrs[2], Some(&invite));
    let identities = [identity1, identity2, identity3];
    let peers: Vec<(NodeId, String)> = identities
        .iter()
        .zip(&addrs)
        .map(|(identity, addr)| (identity.node_id, addr.clone()))
        .collect();

    // Start the runtimes: node 1 bootstraps the meta group; nodes 2/3 run
    // pristine meta members awaiting `add_member`.
    let meta_group_id = RaftGroupId::new_random();
    let mut node1 = NodeRuntime::start(runtime_config(
        dirs[0].clone(),
        &addrs[0],
        &peers,
        Some(MetaMembership {
            meta_group_id,
            bootstrap_voters: Some(vec![(identities[0].node_id, addrs[0].clone())]),
        }),
    ))
    .await
    .unwrap();
    let mut node2 = NodeRuntime::start(runtime_config(
        dirs[1].clone(),
        &addrs[1],
        &peers,
        Some(MetaMembership {
            meta_group_id,
            bootstrap_voters: None,
        }),
    ))
    .await
    .unwrap();
    let mut node3 = NodeRuntime::start(runtime_config(
        dirs[2].clone(),
        &addrs[2],
        &peers,
        Some(MetaMembership {
            meta_group_id,
            bootstrap_voters: None,
        }),
    ))
    .await
    .unwrap();

    let control = ExecutionControl::default();
    {
        let meta = node1.meta_group().unwrap();
        meta.group().wait_leader(LEADER_TIMEOUT).await.unwrap();
        // Node 1 registers itself; nodes 2/3 join through the membership
        // workflow (learner, catch-up, promote, descriptor registration).
        meta.propose(
            command_id(1),
            MetaCommand::RegisterNode {
                descriptor: descriptor(identities[0].node_id, &addrs[0]),
            },
            &control,
        )
        .await
        .unwrap();
        meta.add_member(&descriptor(identities[1].node_id, &addrs[1]), &control)
            .await
            .unwrap();
        meta.add_member(&descriptor(identities[2].node_id, &addrs[2]), &control)
            .await
            .unwrap();
        assert_eq!(meta.state().nodes.len(), 3);
    }
    publish_schema(&node1, DatabaseId::new_random(), TableId::new(1)).await;
    let table_id = TableId::new(1);

    // One tablet group spanning all three nodes (raft ids 1..=3).
    let tablet_id = TabletId::new_random();
    let tablet = tablet_descriptor(
        tablet_id,
        RaftGroupId::new_random(),
        table_id,
        vec![
            voter(identities[0].node_id, 1),
            voter(identities[1].node_id, 2),
            voter(identities[2].node_id, 3),
        ],
        1,
    );
    let partitioning =
        TablePartitioningRecord::automatic_default(table_id, vec![ColumnId::new(1)], 16);
    // Replicas first; the bootstrapping node last.
    node2
        .create_tablet(&tablet, &partitioning, None, false, &control)
        .await
        .unwrap();
    node3
        .create_tablet(&tablet, &partitioning, None, false, &control)
        .await
        .unwrap();
    create_bootstrapped_tablet(
        &mut [&mut node1, &mut node2, &mut node3],
        &tablet,
        &partitioning,
        peers.as_slice(),
        &control,
    )
    .await;

    // The meta descriptor reflects the tablet and its replicas.
    let published = wait_for_tablet_descriptor(&node1, tablet_id).await;
    assert_eq!(published, tablet);
    assert_eq!(published.replicas.len(), 3);

    // Status: identity, groups with roles, applied watermarks.
    let status = node1.status();
    assert_eq!(status.identity, identities[0]);
    assert_eq!(status.rpc_address, addrs[0]);
    assert!(status.meta.is_some());
    assert_eq!(status.tablets.len(), 1);
    assert_eq!(status.tablets[0].replicas.len(), 3);

    // Writes through the tablet group leader; read-your-writes on both
    // followers (spec section 11.4).
    let nodes: Vec<&NodeRuntime> = vec![&node1, &node2, &node3];
    let leader = tablet_leader(&nodes, tablet_id).await;
    let leader_group = leader.tablet_group(tablet_id).unwrap();
    let receipt = propose_write(leader_group, 1).await;
    let token = session_token(leader_group, &receipt);
    for node in &nodes {
        let group = node.tablet_group(tablet_id).unwrap();
        let watermark = group
            .consistent_read(
                &ReadConsistency::ReadYourWrites {
                    token: token.clone(),
                },
                &control,
            )
            .await
            .unwrap();
        assert!(watermark.position.index >= token.commit_index);
    }

    // Restart node 3: it rejoins from its durable state and catches up.
    node3.shutdown().await.unwrap();
    let node3 = NodeRuntime::start(runtime_config(
        dirs[2].clone(),
        &addrs[2],
        &peers,
        Some(MetaMembership {
            meta_group_id,
            bootstrap_voters: None,
        }),
    ))
    .await
    .unwrap();
    assert!(node3.meta_group().is_some());
    let restarted_group = node3.tablet_group(tablet_id).unwrap();
    restarted_group
        .wait_applied_index(receipt.position.index, LEADER_TIMEOUT)
        .await
        .unwrap();

    // A fresh write lands on the restarted node too (read-your-writes).
    let nodes: Vec<&NodeRuntime> = vec![&node1, &node2, &node3];
    let leader = tablet_leader(&nodes, tablet_id).await;
    let leader_group = leader.tablet_group(tablet_id).unwrap();
    let receipt = propose_write(leader_group, 2).await;
    let token = session_token(leader_group, &receipt);
    let watermark = restarted_group
        .consistent_read(
            &ReadConsistency::ReadYourWrites {
                token: token.clone(),
            },
            &control,
        )
        .await
        .unwrap();
    assert!(watermark.position.index >= token.commit_index);

    // Graceful shutdown of a non-leader node leaves quorum writes working.
    let nodes: Vec<&NodeRuntime> = vec![&node1, &node2, &node3];
    let leader = tablet_leader(&nodes, tablet_id).await;
    let victim_is_node2 = leader.identity().node_id != identities[1].node_id;
    let survivor = if victim_is_node2 {
        node2.shutdown().await.unwrap();
        node3
    } else {
        node3.shutdown().await.unwrap();
        node2
    };
    let survivors: Vec<&NodeRuntime> = vec![&node1, &survivor];
    let leader = tablet_leader(&survivors, tablet_id).await;
    let receipt = propose_write(leader.tablet_group(tablet_id).unwrap(), 3).await;
    assert!(receipt.position.index > 0);

    node1.shutdown().await.unwrap();
    survivor.shutdown().await.unwrap();
}

#[tokio::test]
async fn replica_join_workflow_promotes_a_new_voter() {
    let _serial = serial_cluster_lock().await;
    let tmp = TempDir::new().unwrap();
    let dirs: Vec<PathBuf> = (1..=4)
        .map(|index| tmp.path().join(format!("node-{index}")))
        .collect();
    let addrs: Vec<String> = (1..=4).map(|_| free_addr()).collect();

    let identity1 = provision_node(&dirs[0], &addrs[0], None);
    let invite = (identity1.cluster_id, addrs[..3].to_vec());
    let identity2 = provision_node(&dirs[1], &addrs[1], Some(&invite));
    let identity3 = provision_node(&dirs[2], &addrs[2], Some(&invite));
    let identity4 = provision_node(&dirs[3], &addrs[3], Some(&invite));
    let identities = [identity1, identity2, identity3, identity4];
    let peers: Vec<(NodeId, String)> = identities
        .iter()
        .zip(&addrs)
        .map(|(identity, addr)| (identity.node_id, addr.clone()))
        .collect();

    // Nodes 1-3 run the meta group and (shortly) a three-voter tablet; node
    // 4 runs no meta group and no tablet yet.
    let meta_group_id = RaftGroupId::new_random();
    let membership = |bootstrap_voters| {
        Some(MetaMembership {
            meta_group_id,
            bootstrap_voters,
        })
    };
    let mut node1 = NodeRuntime::start(runtime_config(
        dirs[0].clone(),
        &addrs[0],
        &peers,
        membership(Some(vec![(identities[0].node_id, addrs[0].clone())])),
    ))
    .await
    .unwrap();
    let mut node2 = NodeRuntime::start(runtime_config(
        dirs[1].clone(),
        &addrs[1],
        &peers,
        membership(None),
    ))
    .await
    .unwrap();
    let mut node3 = NodeRuntime::start(runtime_config(
        dirs[2].clone(),
        &addrs[2],
        &peers,
        membership(None),
    ))
    .await
    .unwrap();
    let mut node4 = NodeRuntime::start(runtime_config(dirs[3].clone(), &addrs[3], &peers, None))
        .await
        .unwrap();

    let control = ExecutionControl::default();
    {
        let meta = node1.meta_group().unwrap();
        meta.group().wait_leader(LEADER_TIMEOUT).await.unwrap();
        meta.propose(
            command_id(1),
            MetaCommand::RegisterNode {
                descriptor: descriptor(identities[0].node_id, &addrs[0]),
            },
            &control,
        )
        .await
        .unwrap();
        for (identity, addr) in identities[1..3].iter().zip(&addrs[1..3]) {
            meta.add_member(&descriptor(identity.node_id, addr), &control)
                .await
                .unwrap();
        }
        // Register node 4's descriptor so its locality is known to placement.
        meta.propose(
            command_id(2),
            MetaCommand::RegisterNode {
                descriptor: descriptor(identities[3].node_id, &addrs[3]),
            },
            &control,
        )
        .await
        .unwrap();
    }
    publish_schema(&node1, DatabaseId::new_random(), TableId::new(1)).await;
    let table_id = TableId::new(1);

    let tablet_id = TabletId::new_random();
    let tablet = tablet_descriptor(
        tablet_id,
        RaftGroupId::new_random(),
        table_id,
        vec![
            voter(identities[0].node_id, 1),
            voter(identities[1].node_id, 2),
            voter(identities[2].node_id, 3),
        ],
        1,
    );
    let partitioning =
        TablePartitioningRecord::automatic_default(table_id, vec![ColumnId::new(1)], 16);
    node2
        .create_tablet(&tablet, &partitioning, None, false, &control)
        .await
        .unwrap();
    node3
        .create_tablet(&tablet, &partitioning, None, false, &control)
        .await
        .unwrap();
    create_bootstrapped_tablet(
        &mut [&mut node1, &mut node2, &mut node3],
        &tablet,
        &partitioning,
        &peers[..3],
        &control,
    )
    .await;

    // Node 4 creates its local replica as a learner (descriptor generation 2
    // naming it), then the tablet leader drives the section 12.7 movement
    // protocol: add learner, snapshot/catch-up, promote.
    let learner = ReplicaDescriptor {
        node_id: identities[3].node_id,
        role: ReplicaRole::Learner,
        raft_node_id: 4,
    };
    let mut joined = tablet.clone();
    joined.generation = 2;
    joined.replicas.push(learner);
    node4
        .create_tablet(&joined, &partitioning, None, false, &control)
        .await
        .unwrap();
    let nodes: Vec<&NodeRuntime> = vec![&node1, &node2, &node3];
    let mut leader = tablet_leader(&nodes, tablet_id).await;
    while let Err(error) = leader
        .add_tablet_replica(tablet_id, learner, &addrs[3])
        .await
    {
        if !is_meta_not_leader(&error) {
            panic!("add_tablet_replica failed: {error}");
        }
        leader = tablet_leader(&nodes, tablet_id).await;
    }

    // Publish the promoted descriptor (node 4 a voter, generation 2) and
    // confirm the meta state reflects it.
    let mut promoted = joined.clone();
    promoted
        .replicas
        .iter_mut()
        .find(|replica| replica.node_id == identities[3].node_id)
        .unwrap()
        .role = ReplicaRole::Voter;
    publish_tablet_descriptor_on_meta_leader(&nodes, &promoted, &control).await;
    let published = wait_for_tablet_descriptor(&node1, tablet_id).await;
    assert_eq!(published, promoted);
    assert_eq!(published.replicas.len(), 4);
    assert_eq!(published.voter_count(), 4);

    // The group serves writes, and the promoted replica answers
    // read-your-writes.
    let leader_group = leader.tablet_group(tablet_id).unwrap();
    let receipt = propose_write(leader_group, 1).await;
    let token = session_token(leader_group, &receipt);
    let watermark = node4
        .tablet_group(tablet_id)
        .unwrap()
        .consistent_read(&ReadConsistency::ReadYourWrites { token }, &control)
        .await
        .unwrap();
    assert!(watermark.position.index >= receipt.position.index);

    node4.shutdown().await.unwrap();
    node3.shutdown().await.unwrap();
    node2.shutdown().await.unwrap();
    node1.shutdown().await.unwrap();
}

#[tokio::test]
async fn runtime_failure_modes_fail_closed() {
    let _serial = serial_cluster_lock().await;
    let tmp = TempDir::new().unwrap();
    let control = ExecutionControl::default();

    // Starting on an unprovisioned directory fails closed (S2A-001).
    let config = NodeRuntimeConfig {
        node_data: tmp.path().join("unprovisioned"),
        security: TransportSecurity::PlaintextForTesting,
        transport: transport_config(),
        listen_address: free_addr(),
        rpc_address: None,
        peers: Vec::new(),
        meta: None,
        timing: Some(fast_timing()),
    };
    assert!(matches!(
        NodeRuntime::start(config).await,
        Err(RuntimeError::Cluster(
            mongreldb_cluster::node::ClusterError::NotInitialized
        ))
    ));

    // A one-node runtime (no meta membership).
    let dir = tmp.path().join("node-1");
    let addr = free_addr();
    let identity = provision_node(&dir, &addr, None);
    let peers = vec![(identity.node_id, addr.clone())];
    let mut node = NodeRuntime::start(runtime_config(dir.clone(), &addr, &peers, None))
        .await
        .unwrap();

    let tablet_id = TabletId::new_random();
    let table_id = TableId::new(1);
    let tablet = tablet_descriptor(
        tablet_id,
        RaftGroupId::new_random(),
        table_id,
        vec![voter(identity.node_id, 1)],
        1,
    );
    let partitioning =
        TablePartitioningRecord::automatic_default(table_id, vec![ColumnId::new(1)], 16);

    // The partitioning record must name the descriptor's table.
    let wrong =
        TablePartitioningRecord::automatic_default(TableId::new(2), vec![ColumnId::new(1)], 16);
    assert!(matches!(
        node.create_tablet(&tablet, &wrong, None, false, &control)
            .await,
        Err(RuntimeError::InvalidRequest(_))
    ));
    // A descriptor that does not list this node as a replica is refused
    // (before any layout is allocated).
    let mut foreign = tablet.clone();
    foreign.replicas = vec![voter(NodeId::new_random(), 7)];
    assert!(matches!(
        node.create_tablet(&foreign, &partitioning, None, false, &control)
            .await,
        Err(RuntimeError::InvalidRequest(_))
    ));
    // Publishing without a meta group is refused.
    assert!(matches!(
        node.publish_tablet_descriptor(&tablet, &control).await,
        Err(RuntimeError::InvalidRequest(_))
    ));

    // Create the single-replica tablet; an identical repeat is a no-op, a
    // different descriptor for the same tablet id fails closed.
    let bootstrap = vec![(identity.node_id, addr.clone())];
    node.create_tablet(
        &tablet,
        &partitioning,
        Some(bootstrap.as_slice()),
        false,
        &control,
    )
    .await
    .unwrap();
    node.create_tablet(&tablet, &partitioning, None, false, &control)
        .await
        .unwrap();
    let mut changed = tablet.clone();
    changed.generation = 2;
    assert!(matches!(
        node.create_tablet(&changed, &partitioning, None, false, &control)
            .await,
        Err(RuntimeError::InvalidRequest(_))
    ));
    let status = node.status();
    assert_eq!(status.tablets.len(), 1);
    assert!(status.meta.is_none());

    // A second runtime on the same directory cannot own the same tablet
    // storage core (spec section 12.3, process-local half).
    assert!(matches!(
        NodeRuntime::start(runtime_config(dir.clone(), &free_addr(), &peers, None)).await,
        Err(RuntimeError::Tablet(
            mongreldb_cluster::tablet::TabletError::AlreadyOwned { .. }
        ))
    ));

    node.shutdown().await.unwrap();

    // After shutdown the ownership guard is released: the node reopens.
    let node = NodeRuntime::start(runtime_config(dir, &addr, &peers, None))
        .await
        .unwrap();
    assert_eq!(node.tablet_ids(), vec![tablet_id]);
    node.shutdown().await.unwrap();
}

// ---------------------------------------------------------------------------
// Split/merge integration (spec sections 12.5-12.6)
// ---------------------------------------------------------------------------

fn key(bytes: &[u8]) -> Key {
    Key::from_bytes(bytes.to_vec())
}

fn bounds(low: &[u8], high: &[u8]) -> PartitionBounds {
    PartitionBounds::new(Bound::Included(key(low)), Bound::Excluded(key(high))).unwrap()
}

/// A provisioned multi-node cluster directory set plus the wiring to start
/// and restart runtimes on it.
struct ClusterFixture {
    _tmp: TempDir,
    dirs: Vec<PathBuf>,
    addrs: Vec<String>,
    identities: Vec<NodeIdentity>,
    peers: Vec<(NodeId, String)>,
    meta_group_id: RaftGroupId,
}

impl ClusterFixture {
    fn new(count: usize) -> Self {
        let tmp = TempDir::new().unwrap();
        let dirs: Vec<PathBuf> = (1..=count)
            .map(|index| tmp.path().join(format!("node-{index}")))
            .collect();
        let addrs: Vec<String> = (1..=count).map(|_| free_addr()).collect();
        let identity1 = provision_node(&dirs[0], &addrs[0], None);
        let invite = (identity1.cluster_id, addrs.clone());
        let mut identities = vec![identity1];
        for index in 1..count {
            identities.push(provision_node(&dirs[index], &addrs[index], Some(&invite)));
        }
        let peers: Vec<(NodeId, String)> = identities
            .iter()
            .zip(&addrs)
            .map(|(identity, address)| (identity.node_id, address.clone()))
            .collect();
        Self {
            _tmp: tmp,
            dirs,
            addrs,
            identities,
            peers,
            meta_group_id: RaftGroupId::new_random(),
        }
    }

    fn config(&self, index: usize, bootstrap: bool) -> NodeRuntimeConfig {
        runtime_config(
            self.dirs[index].clone(),
            &self.addrs[index],
            &self.peers,
            Some(MetaMembership {
                meta_group_id: self.meta_group_id,
                bootstrap_voters: bootstrap
                    .then(|| vec![(self.identities[index].node_id, self.addrs[index].clone())]),
            }),
        )
    }

    /// Starts every node: node 1 bootstraps the meta group, the rest join
    /// through the membership workflow.
    async fn start_all(&self) -> Vec<NodeRuntime> {
        let mut nodes = Vec::new();
        for index in 0..self.dirs.len() {
            nodes.push(
                NodeRuntime::start(self.config(index, index == 0))
                    .await
                    .unwrap(),
            );
        }
        let control = ExecutionControl::default();
        let meta = nodes[0].meta_group().unwrap();
        meta.group().wait_leader(LEADER_TIMEOUT).await.unwrap();
        meta.propose(
            command_id(1),
            MetaCommand::RegisterNode {
                descriptor: descriptor(self.identities[0].node_id, &self.addrs[0]),
            },
            &control,
        )
        .await
        .unwrap();
        for index in 1..self.dirs.len() {
            meta.add_member(
                &descriptor(self.identities[index].node_id, &self.addrs[index]),
                &control,
            )
            .await
            .unwrap();
        }
        nodes
    }

    /// Restarts one node from its durable state (no bootstrap).
    async fn restart(&self, index: usize) -> NodeRuntime {
        NodeRuntime::start(self.config(index, false)).await.unwrap()
    }
}

/// The index of the node currently leading the meta group (all nodes agree).
async fn meta_leader_index(nodes: &[NodeRuntime]) -> usize {
    let deadline = std::time::Instant::now() + LEADER_TIMEOUT;
    loop {
        let mut votes = BTreeMap::<RaftNodeId, usize>::new();
        for node in nodes {
            if let Some(leader) = node.meta_group().unwrap().group().metrics().current_leader {
                *votes.entry(leader).or_default() += 1;
            }
        }
        if let Some((leader, count)) = votes.iter().next_back() {
            if *count == nodes.len() {
                return nodes
                    .iter()
                    .position(|node| raft_node_id(&node.identity().node_id) == *leader)
                    .expect("the meta leader is one of the runtimes");
            }
        }
        assert!(std::time::Instant::now() < deadline, "no meta leader");
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
}

/// Transfers meta leadership onto `index` when it is not already the leader
/// (a restarted split driver must lead the meta group to propose).
async fn ensure_meta_leader(nodes: &[NodeRuntime], index: usize) {
    let target = raft_node_id(&nodes[index].identity().node_id);
    let deadline = std::time::Instant::now() + LEADER_TIMEOUT;
    loop {
        let leader = nodes[index]
            .meta_group()
            .unwrap()
            .group()
            .wait_leader(LEADER_TIMEOUT)
            .await
            .unwrap();
        if leader == target {
            return;
        }
        let leader_index = nodes
            .iter()
            .position(|node| raft_node_id(&node.identity().node_id) == leader)
            .expect("the meta leader is one of the runtimes");
        match nodes[leader_index]
            .meta_group()
            .unwrap()
            .group()
            .transfer_leader(target, LEADER_TIMEOUT)
            .await
        {
            Ok(()) => return,
            Err(ConsensusError::NotLeader { .. }) if std::time::Instant::now() < deadline => {
                tokio::time::sleep(Duration::from_millis(20)).await;
            }
            Err(error) => panic!("meta leadership transfer failed: {error}"),
        }
    }
}

/// Transfers a tablet group's leadership onto `index` when needed (child
/// state installs propose through the driver's local group).
async fn ensure_tablet_leader(nodes: &[NodeRuntime], tablet_id: TabletId, index: usize) {
    let target = nodes[index].tablet_group(tablet_id).unwrap().node_id();
    let deadline = std::time::Instant::now() + LEADER_TIMEOUT;
    loop {
        let leader = nodes[index]
            .tablet_group(tablet_id)
            .unwrap()
            .wait_leader(LEADER_TIMEOUT)
            .await
            .unwrap();
        if leader == target {
            return;
        }
        let leader_index = nodes
            .iter()
            .position(|node| {
                node.tablet_group(tablet_id)
                    .is_some_and(|group| group.node_id() == leader)
            })
            .expect("the tablet leader is one of the runtimes");
        match nodes[leader_index]
            .tablet_group(tablet_id)
            .unwrap()
            .transfer_leader(target, LEADER_TIMEOUT)
            .await
        {
            Ok(()) => return,
            Err(ConsensusError::NotLeader { .. }) if std::time::Instant::now() < deadline => {
                tokio::time::sleep(Duration::from_millis(20)).await;
            }
            Err(error) => panic!("tablet leadership transfer failed: {error}"),
        }
    }
}

/// Creates one tablet spanning every node: replica raft ids from the
/// meta-owned allocator, local replicas everywhere, bootstrap + publish on
/// the meta leader.
async fn create_tablet_on_cluster(
    nodes: &mut [NodeRuntime],
    table_id: TableId,
    partition: PartitionBounds,
) -> TabletDescriptor {
    let control = ExecutionControl::default();
    let leader = meta_leader_index(nodes).await;
    let meta = nodes[leader].meta_group().unwrap();
    let raft_ids = meta.allocate_raft_node_ids(3, &control).await.unwrap();
    let tablet = TabletDescriptor {
        tablet_id: TabletId::new_random(),
        database_id: mongreldb_types::ids::DatabaseId::ZERO,
        table_id,
        raft_group_id: RaftGroupId::new_random(),
        partition,
        replicas: nodes
            .iter()
            .enumerate()
            .map(|(index, node)| ReplicaDescriptor {
                node_id: node.identity().node_id,
                role: ReplicaRole::Voter,
                raft_node_id: raft_ids[index],
            })
            .collect(),
        leader_hint: None,
        generation: 1,
        state: TabletState::Active,
    };
    let partitioning =
        TablePartitioningRecord::automatic_default(table_id, vec![ColumnId::new(1)], 16);
    // Replicas first; the bootstrapping node last.
    for node in nodes.iter_mut().skip(1) {
        node.create_tablet(&tablet, &partitioning, None, false, &control)
            .await
            .unwrap();
    }
    let peers: Vec<(NodeId, String)> = tablet
        .replicas
        .iter()
        .map(|replica| {
            let address = nodes
                .iter()
                .find(|node| node.identity().node_id == replica.node_id)
                .unwrap()
                .rpc_address()
                .to_owned();
            (replica.node_id, address)
        })
        .collect();
    let mut runtimes: Vec<&mut NodeRuntime> = nodes.iter_mut().collect();
    create_bootstrapped_tablet(
        &mut runtimes,
        &tablet,
        &partitioning,
        peers.as_slice(),
        &control,
    )
    .await;
    nodes[0]
        .tablet_group(tablet.tablet_id)
        .unwrap()
        .wait_leader(LEADER_TIMEOUT)
        .await
        .unwrap();
    tablet
}

/// Writes rows through the tablet group's current leader, returning the
/// commit receipt (its position gates replica catch-up waits).
async fn write_rows(
    nodes: &[NodeRuntime],
    tablet_id: TabletId,
    rows: &[(Key, Vec<u8>)],
) -> GroupCommitReceipt {
    let deadline = std::time::Instant::now() + LEADER_TIMEOUT;
    loop {
        let refs: Vec<&NodeRuntime> = nodes.iter().collect();
        let leader = tablet_leader(&refs, tablet_id).await;
        match leader
            .write_tablet_rows(tablet_id, rows, &ExecutionControl::default())
            .await
        {
            Ok(receipt) => return receipt,
            Err(_) if std::time::Instant::now() < deadline => {
                tokio::time::sleep(Duration::from_millis(20)).await;
            }
            Err(error) => panic!("write to tablet {tablet_id} failed: {error}"),
        }
    }
}

/// Polls until `node`'s local ledger of `tablet_id` holds exactly `expected`
/// (replication and apply are asynchronous on followers).
async fn wait_rows(node: &NodeRuntime, tablet_id: TabletId, expected: &BTreeMap<Key, Vec<u8>>) {
    let deadline = std::time::Instant::now() + LEADER_TIMEOUT;
    loop {
        let rows = node.tablet_rows(tablet_id).unwrap();
        if rows == *expected {
            return;
        }
        assert!(
            std::time::Instant::now() < deadline,
            "tablet {tablet_id} rows did not settle: have {rows:?}, want {expected:?}"
        );
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
}

/// The meta state's tablet descriptors, in tablet-id order.
fn meta_descriptors(node: &NodeRuntime) -> Vec<TabletDescriptor> {
    node.meta_group()
        .unwrap()
        .state()
        .tablets
        .values()
        .map(|record| record.descriptor.clone())
        .collect()
}

/// The local meta apply watermark.
fn meta_version(node: &NodeRuntime) -> MetadataVersion {
    node.meta_group().unwrap().metadata_version()
}

/// Polls until `node`'s meta apply watermark reaches `version` (meta
/// replication to a follower is asynchronous; the driver's propose returns
/// once ITS local apply landed).
async fn wait_meta_caught_up(node: &NodeRuntime, version: MetadataVersion) {
    let deadline = std::time::Instant::now() + LEADER_TIMEOUT;
    loop {
        if meta_version(node) >= version {
            return;
        }
        assert!(
            std::time::Instant::now() < deadline,
            "meta state did not catch up to {version:?} (at {:?})",
            meta_version(node)
        );
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
}

/// Sort key for a partition's low endpoint (Unbounded sorts first).
fn low_key(partition: &PartitionBounds) -> Vec<u8> {
    match &partition.low {
        Bound::Included(key) | Bound::Excluded(key) => key.as_bytes().to_vec(),
        Bound::Unbounded => Vec::new(),
    }
}

fn row_map(rows: &[(&[u8], &[u8])]) -> BTreeMap<Key, Vec<u8>> {
    rows.iter()
        .map(|(key, value)| (Key::from_bytes(key.to_vec()), value.to_vec()))
        .collect()
}

#[tokio::test]
async fn split_end_to_end_on_three_node_cluster() {
    let _serial = serial_cluster_lock().await;
    let fixture = ClusterFixture::new(3);
    let mut nodes = fixture.start_all().await;
    let control = ExecutionControl::default();
    let database_id = DatabaseId::new_random();
    let table_id = TableId::new(1);
    publish_schema(&nodes[0], database_id, table_id).await;
    let source = create_tablet_on_cluster(&mut nodes, table_id, bounds(b"a", b"z")).await;
    let g = source.generation;

    // Data written before the split.
    let pre_split = row_map(&[
        (b"apple", b"v-apple"),
        (b"fig", b"v-fig"),
        (b"mango", b"v-mango"),
        (b"yam", b"v-yam"),
    ]);
    write_rows(
        &nodes,
        source.tablet_id,
        &pre_split.clone().into_iter().collect::<Vec<_>>(),
    )
    .await;
    for node in &nodes {
        wait_rows(node, source.tablet_id, &pre_split).await;
    }

    let mut driver = meta_leader_index(&nodes).await;
    // Step 1: the source is marked Splitting at g + 1; requests holding g
    // redirect (spec section 12.5 step 9).
    let (phase, _) = split_step_retry(
        &mut nodes,
        &mut driver,
        source.tablet_id,
        Some(key(b"m")),
        &control,
    )
    .await;
    assert_eq!(phase, SplitPhase::MarkedSplitting);
    let splitting = nodes[driver]
        .meta_group()
        .unwrap()
        .state()
        .tablet(source.tablet_id)
        .unwrap()
        .clone();
    assert_eq!(splitting.state, TabletState::Splitting);
    assert_eq!(splitting.generation, g + 1);
    let error = check_generation(&splitting, g).unwrap_err();
    assert_eq!(
        error,
        RoutingError::TabletSplit {
            tablet_id: source.tablet_id,
            used_generation: g,
            current_generation: g + 1,
        }
    );
    assert_eq!(
        retry_guidance(&error),
        RetryGuidance::AwaitSplitPublish {
            tablet_id: source.tablet_id,
        }
    );

    // Step 2: the children are created — Creating, never routable.
    let (phase, _) =
        split_step_retry(&mut nodes, &mut driver, source.tablet_id, None, &control).await;
    assert_eq!(phase, SplitPhase::ChildrenCreated);
    let descriptors = meta_descriptors(&nodes[driver]);
    assert_eq!(
        find_tablet_for_key(&descriptors, table_id, &key(b"fig"))
            .unwrap()
            .tablet_id,
        source.tablet_id
    );
    // The peers adopt the child replicas; the child groups elect. (Meta
    // replication to the peers is asynchronous: wait for the children to
    // arrive before the sync asserts the exact outcome.)
    let version = meta_version(&nodes[driver]);
    for (index, node) in nodes.iter_mut().enumerate() {
        if index == driver {
            continue;
        }
        wait_meta_caught_up(node, version).await;
        let report = node.sync_hosted_tablets(&control).await.unwrap();
        assert_eq!(report.created.len(), 2);
    }
    let children: Vec<TabletDescriptor> = {
        let mut creating: Vec<TabletDescriptor> = meta_descriptors(&nodes[driver])
            .into_iter()
            .filter(|descriptor| descriptor.state == TabletState::Creating)
            .collect();
        creating.sort_by_key(|descriptor| low_key(&descriptor.partition));
        creating
    };
    assert_eq!(children.len(), 2);
    for child in &children {
        ensure_tablet_leader(&nodes, child.tablet_id, driver).await;
    }

    // Steps 3-5: pin, build, catch up. A write committed mid-split (after
    // the pin timestamp) streams to its child as a delta.
    let (phase, _) =
        split_step_retry(&mut nodes, &mut driver, source.tablet_id, None, &control).await;
    assert_eq!(phase, SplitPhase::SnapshotPinned);
    let mid_split = row_map(&[(b"banana", b"v-banana@mid")]);
    let receipt = write_rows(
        &nodes,
        source.tablet_id,
        &mid_split.clone().into_iter().collect::<Vec<_>>(),
    )
    .await;
    // The driver's local ledger must apply it before the catch-up reads deltas.
    nodes[driver]
        .tablet_group(source.tablet_id)
        .unwrap()
        .wait_applied_index(receipt.position.index, LEADER_TIMEOUT)
        .await
        .unwrap();
    let (phase, _) =
        split_step_retry(&mut nodes, &mut driver, source.tablet_id, None, &control).await;
    assert_eq!(phase, SplitPhase::ChildrenBuilt);
    let (phase, _) =
        split_step_retry(&mut nodes, &mut driver, source.tablet_id, None, &control).await;
    assert_eq!(phase, SplitPhase::CaughtUp);

    // Step 6: the atomic publication — one meta command flips the children
    // Active and the source Retiring at one generation.
    let (phase, published) =
        split_step_retry(&mut nodes, &mut driver, source.tablet_id, None, &control).await;
    assert_eq!(phase, SplitPhase::Published);
    let command = published.expect("the publish step yields its command");
    assert_eq!(command.publish_generation(), g + 2);
    let meta_state = nodes[driver].meta_group().unwrap().state();
    let retiring = meta_state.tablet(source.tablet_id).unwrap();
    assert_eq!(retiring.state, TabletState::Retiring);
    assert_eq!(retiring.generation, g + 2);
    for child in &command.children {
        let stored = meta_state.tablet(child.tablet_id).unwrap();
        assert_eq!(stored.state, TabletState::Active);
        assert_eq!(stored.generation, g + 2);
        assert!(stored
            .replicas
            .iter()
            .all(|replica| replica.role == ReplicaRole::Voter));
    }
    // Routing resolves every key to its child; stale generations redirect.
    let descriptors = meta_descriptors(&nodes[driver]);
    assert_eq!(
        find_tablet_for_key(&descriptors, table_id, &key(b"banana"))
            .unwrap()
            .tablet_id,
        command.children[0].tablet_id
    );
    assert_eq!(
        find_tablet_for_key(&descriptors, table_id, &key(b"yam"))
            .unwrap()
            .tablet_id,
        command.children[1].tablet_id
    );
    assert!(matches!(
        check_generation(retiring, g),
        Err(RoutingError::TabletMoved { .. })
    ));
    // The child replica raft ids came from the meta-owned allocator: fresh
    // and distinct from the source's (and from each other).
    let mut raft_ids: BTreeSet<u64> = source
        .replicas
        .iter()
        .map(|replica| replica.raft_node_id)
        .collect();
    for child in &command.children {
        for replica in &child.replicas {
            assert!(
                raft_ids.insert(replica.raft_node_id),
                "raft id {} reused",
                replica.raft_node_id
            );
        }
    }

    // Step 7: the source is retired and removed; peers tear their replicas
    // down on the next sync.
    let (phase, _) =
        split_step_retry(&mut nodes, &mut driver, source.tablet_id, None, &control).await;
    assert_eq!(phase, SplitPhase::SourceRetired);
    assert!(nodes[driver]
        .meta_group()
        .unwrap()
        .state()
        .tablet(source.tablet_id)
        .is_none());
    assert!(!fixture.dirs[driver]
        .join("tablets")
        .join(source.tablet_id.to_hex())
        .exists());
    // The source's removal must replicate to each peer's meta before its
    // sync tears the local replica down.
    let version = meta_version(&nodes[driver]);
    for (index, node) in nodes.iter_mut().enumerate() {
        if index == driver {
            continue;
        }
        wait_meta_caught_up(node, version).await;
        let report = node.sync_hosted_tablets(&control).await.unwrap();
        assert_eq!(report.torn_down, vec![source.tablet_id]);
    }
    for dir in &fixture.dirs {
        assert!(!dir.join("tablets").join(source.tablet_id.to_hex()).exists());
    }

    // Zero loss, zero duplication: the pre-split data and the mid-split
    // delta are fully readable from the children on EVERY replica.
    let expected_lower = row_map(&[
        (b"apple", b"v-apple"),
        (b"banana", b"v-banana@mid"),
        (b"fig", b"v-fig"),
    ]);
    let expected_upper = row_map(&[(b"mango", b"v-mango"), (b"yam", b"v-yam")]);
    let all: BTreeSet<Key> = pre_split.keys().chain(mid_split.keys()).cloned().collect();
    assert_eq!(
        expected_lower
            .keys()
            .chain(expected_upper.keys())
            .cloned()
            .collect::<BTreeSet<_>>(),
        all
    );
    for node in &nodes {
        wait_rows(node, command.children[0].tablet_id, &expected_lower).await;
        wait_rows(node, command.children[1].tablet_id, &expected_upper).await;
    }

    for node in nodes {
        node.shutdown().await.unwrap();
    }
}

/// Drives the split on the meta leader from the current persisted phase to
/// `target`, syncing the peers' hosted tablets at the children-created and
/// published boundaries (the points the protocol needs them).
///
/// True when a runtime error is the meta plane reporting this node is not the
/// leader (spec section 11.7): proposals must be retried on the current
/// leader, never unwrapped — elections churn under parallel load.
fn is_meta_not_leader(error: &RuntimeError) -> bool {
    matches!(
        error,
        RuntimeError::Split(SplitError::MetaPlane(MetaRejectionReason::NotLeader { .. }))
            | RuntimeError::Merge(MergeError::MetaPlane(MetaRejectionReason::NotLeader { .. }))
            | RuntimeError::Meta(MetaError::Consensus(
                mongreldb_consensus::error::ConsensusError::NotLeader { .. },
            ))
            | RuntimeError::Consensus(mongreldb_consensus::error::ConsensusError::NotLeader { .. })
    )
}

/// One split step with leader-stabilizing retry: the split executor's
/// progress lives on the driver node's local disk, so on `NotLeader` the test
/// re-elects leadership onto the SAME driver (never hops nodes) and resumes —
/// the executor's persisted phases make the resume exact.
async fn split_step_retry(
    nodes: &mut [NodeRuntime],
    driver: &mut usize,
    tablet_id: TabletId,
    split_key: Option<Key>,
    control: &ExecutionControl,
) -> (
    SplitPhase,
    Option<mongreldb_cluster::split::SplitPublishCommand>,
) {
    loop {
        match nodes[*driver]
            .split_step(tablet_id, split_key.clone(), control)
            .await
        {
            Ok(step) => return step,
            Err(error) if is_meta_not_leader(&error) => {
                ensure_meta_leader(nodes, *driver).await;
            }
            Err(error) => panic!("split_step failed: {error}"),
        }
    }
}

/// One merge step with leader-following retry (idempotent, like the split).
async fn merge_step_retry(
    nodes: &mut [NodeRuntime],
    driver: &mut usize,
    first: TabletId,
    second: TabletId,
    control: &ExecutionControl,
) -> (
    MergePhase,
    Option<mongreldb_cluster::merge::MergePublishCommand>,
) {
    loop {
        match nodes[*driver].merge_step(first, second, control).await {
            Ok(step) => return step,
            Err(error) if is_meta_not_leader(&error) => {
                ensure_meta_leader(nodes, *driver).await;
            }
            Err(error) => panic!("merge_step failed: {error}"),
        }
    }
}

/// One abort step with leader-following retry.
async fn abort_split_retry(
    nodes: &mut [NodeRuntime],
    driver: &mut usize,
    tablet_id: TabletId,
    control: &ExecutionControl,
) -> mongreldb_cluster::split::SplitAbortReport {
    loop {
        match nodes[*driver].abort_split(tablet_id, control).await {
            Ok(report) => return report,
            Err(error) if is_meta_not_leader(&error) => {
                ensure_meta_leader(nodes, *driver).await;
            }
            Err(error) => panic!("abort_split failed: {error}"),
        }
    }
}

async fn drive_split_to(
    nodes: &mut [NodeRuntime],
    driver: usize,
    source: &TabletDescriptor,
    split_key: Option<Key>,
    target: SplitPhase,
) -> Option<mongreldb_cluster::split::SplitPublishCommand> {
    let mut driver = driver;
    let control = ExecutionControl::default();
    let mut published = None;
    loop {
        // Child state installs and catch-up deltas propose through the
        // driver's local child groups. Pull leadership onto the driver
        // before the first step after a restart, not after that step has
        // already tried to propose.
        for child in active_or_creating(nodes, driver, source) {
            ensure_tablet_leader(nodes, child, driver).await;
        }
        let (phase, command) = split_step_retry(
            nodes,
            &mut driver,
            source.tablet_id,
            split_key.clone(),
            &control,
        )
        .await;
        if command.is_some() {
            published = command;
        }
        match phase {
            SplitPhase::ChildrenCreated | SplitPhase::Published => {
                let version = meta_version(&nodes[driver]);
                for (index, node) in nodes.iter_mut().enumerate() {
                    if index == driver {
                        continue;
                    }
                    wait_meta_caught_up(node, version).await;
                    node.sync_hosted_tablets(&control).await.unwrap();
                }
            }
            _ => {}
        }
        if phase == target {
            return published;
        }
    }
}

/// The not-yet-removed tablets derived from `source`'s table (Creating or
/// Active), as tablet ids, lower bounds first.
fn active_or_creating(
    nodes: &[NodeRuntime],
    driver: usize,
    source: &TabletDescriptor,
) -> Vec<TabletId> {
    let mut derived: Vec<TabletDescriptor> = meta_descriptors(&nodes[driver])
        .into_iter()
        .filter(|descriptor| {
            descriptor.table_id == source.table_id && descriptor.tablet_id != source.tablet_id
        })
        .collect();
    derived.sort_by_key(|descriptor| low_key(&descriptor.partition));
    derived
        .into_iter()
        .map(|descriptor| descriptor.tablet_id)
        .collect()
}

#[tokio::test]
async fn split_crash_at_each_phase_resumes_via_the_runtime() {
    let _serial = serial_cluster_lock().await;
    // Crash points: every durable phase with remaining work. The final
    // phase's teardown removes the record itself, so a "crash" there is the
    // completed split — covered by the end-to-end test.
    for crash_after in [
        SplitPhase::MarkedSplitting,
        SplitPhase::ChildrenCreated,
        SplitPhase::SnapshotPinned,
        SplitPhase::ChildrenBuilt,
        SplitPhase::CaughtUp,
        SplitPhase::Published,
    ] {
        let fixture = ClusterFixture::new(3);
        let mut nodes = fixture.start_all().await;
        let control = ExecutionControl::default();
        let database_id = DatabaseId::new_random();
        let table_id = TableId::new(1);
        publish_schema(&nodes[0], database_id, table_id).await;
        let source = create_tablet_on_cluster(&mut nodes, table_id, bounds(b"a", b"z")).await;
        let rows = row_map(&[(b"apple", b"v-apple"), (b"yam", b"v-yam")]);
        write_rows(
            &nodes,
            source.tablet_id,
            &rows.clone().into_iter().collect::<Vec<_>>(),
        )
        .await;

        let mut driver = meta_leader_index(&nodes).await;
        // Begin the split (records Started), then drive to the crash point.
        let (phase, _) = split_step_retry(
            &mut nodes,
            &mut driver,
            source.tablet_id,
            Some(key(b"m")),
            &control,
        )
        .await;
        assert_eq!(phase, SplitPhase::MarkedSplitting);
        let pre_crash = if crash_after != SplitPhase::MarkedSplitting {
            drive_split_to(&mut nodes, driver, &source, None, crash_after).await
        } else {
            None
        };

        // The crash: the driver stops WITHOUT a graceful shutdown (raft
        // tasks die in place; only fsynced state survives) and restarts
        // from its durable state.
        let crashed = nodes.remove(driver);
        crashed.crash().await;
        let restarted = fixture.restart(driver).await;
        nodes.insert(driver, restarted);
        ensure_meta_leader(&nodes, driver).await;

        // The runtime resumes the split from the persisted phase and drives
        // it to completion.
        let published =
            drive_split_to(&mut nodes, driver, &source, None, SplitPhase::SourceRetired)
                .await
                .or(pre_crash)
                .expect("the resumed split re-publishes its command");
        assert!(
            nodes[driver]
                .meta_group()
                .unwrap()
                .state()
                .tablet(source.tablet_id)
                .is_none(),
            "crash after {crash_after}: source still in meta"
        );
        // Data written before the split is fully readable from the children
        // on every replica.
        let expected_lower = row_map(&[(b"apple", b"v-apple")]);
        let expected_upper = row_map(&[(b"yam", b"v-yam")]);
        for node in nodes.iter() {
            wait_rows(node, published.children[0].tablet_id, &expected_lower).await;
            wait_rows(node, published.children[1].tablet_id, &expected_upper).await;
        }
        for node in nodes {
            node.shutdown().await.unwrap();
        }
    }
}

#[tokio::test]
async fn split_abort_mid_split_restores_the_source() {
    let _serial = serial_cluster_lock().await;
    let fixture = ClusterFixture::new(3);
    let mut nodes = fixture.start_all().await;
    let control = ExecutionControl::default();
    let database_id = DatabaseId::new_random();
    let table_id = TableId::new(1);
    publish_schema(&nodes[0], database_id, table_id).await;
    let source = create_tablet_on_cluster(&mut nodes, table_id, bounds(b"a", b"z")).await;
    let rows = row_map(&[(b"apple", b"v-apple"), (b"yam", b"v-yam")]);
    write_rows(
        &nodes,
        source.tablet_id,
        &rows.clone().into_iter().collect::<Vec<_>>(),
    )
    .await;

    let mut driver = meta_leader_index(&nodes).await;
    // Mark and create the children, then abort mid-split.
    drive_split_to(
        &mut nodes,
        driver,
        &source,
        Some(key(b"m")),
        SplitPhase::ChildrenCreated,
    )
    .await;
    let report = abort_split_retry(&mut nodes, &mut driver, source.tablet_id, &control).await;
    assert_eq!(report.phase, Some(SplitPhase::ChildrenCreated));
    assert_eq!(report.children_removed.len(), 2);
    // The source is Active again one generation above the mark; the children
    // are gone from the meta state and the local disk.
    let meta_state = nodes[driver].meta_group().unwrap().state();
    let restored = meta_state.tablet(source.tablet_id).unwrap();
    assert_eq!(restored.state, TabletState::Active);
    assert_eq!(restored.generation, source.generation + 2);
    assert_eq!(meta_state.tablets.len(), 1);
    assert_eq!(nodes[driver].tablet_ids(), vec![source.tablet_id]);
    assert_eq!(
        nodes[driver].tablet_descriptor(source.tablet_id).unwrap(),
        restored
    );
    for child in &report.children_removed {
        assert!(!fixture.dirs[driver]
            .join("tablets")
            .join(child.to_hex())
            .exists());
    }
    // The persisted progress record is cleared.
    assert!(!fixture.dirs[driver]
        .join("tablets")
        .join(source.tablet_id.to_hex())
        .join("split.json")
        .exists());
    // The abort is idempotent: a second drive is a no-op.
    let second = abort_split_retry(&mut nodes, &mut driver, source.tablet_id, &control).await;
    assert_eq!(second.phase, None);
    assert!(second.children_removed.is_empty());
    // The source keeps serving: writes and reads work through the abort.
    let more = row_map(&[(b"fig", b"v-fig@after-abort")]);
    write_rows(
        &nodes,
        source.tablet_id,
        &more.clone().into_iter().collect::<Vec<_>>(),
    )
    .await;
    let mut expected = rows.clone();
    expected.extend(more);
    for node in &nodes {
        wait_rows(node, source.tablet_id, &expected).await;
    }
    // The peers' sync tears down the orphaned child replicas (hosted but no
    // longer in the meta state); nothing is created or refreshed. The
    // children's removal must replicate to each peer's meta first.
    let version = meta_version(&nodes[driver]);
    for (index, node) in nodes.iter_mut().enumerate() {
        if index == driver {
            continue;
        }
        wait_meta_caught_up(node, version).await;
        let mut torn_down = node.sync_hosted_tablets(&control).await.unwrap().torn_down;
        torn_down.sort();
        let mut expected_orphans = report.children_removed.clone();
        expected_orphans.sort();
        assert_eq!(torn_down, expected_orphans);
        assert!(node.tablet_ids().contains(&source.tablet_id));
    }

    // A fresh split of the restored source runs to completion.
    let published = drive_split_to(
        &mut nodes,
        driver,
        &source,
        Some(key(b"m")),
        SplitPhase::SourceRetired,
    )
    .await
    .expect("the second split publishes");
    let expected_lower = row_map(&[(b"apple", b"v-apple"), (b"fig", b"v-fig@after-abort")]);
    let expected_upper = row_map(&[(b"yam", b"v-yam")]);
    for node in &nodes {
        wait_rows(node, published.children[0].tablet_id, &expected_lower).await;
        wait_rows(node, published.children[1].tablet_id, &expected_upper).await;
    }

    // Once a split has published, aborting fails closed.
    let another = create_tablet_on_cluster(&mut nodes, table_id, bounds(b"A", b"Z")).await;
    drive_split_to(
        &mut nodes,
        driver,
        &another,
        Some(key(b"M")),
        SplitPhase::Published,
    )
    .await;
    assert!(matches!(
        nodes[driver].abort_split(another.tablet_id, &control).await,
        Err(RuntimeError::Split(
            mongreldb_cluster::split::SplitError::CannotAbort { .. }
        ))
    ));

    for node in nodes {
        node.shutdown().await.unwrap();
    }
}

#[tokio::test]
async fn merge_end_to_end_on_three_node_cluster() {
    let _serial = serial_cluster_lock().await;
    let fixture = ClusterFixture::new(3);
    let mut nodes = fixture.start_all().await;
    let control = ExecutionControl::default();
    let database_id = DatabaseId::new_random();
    let table_id = TableId::new(1);
    publish_schema(&nodes[0], database_id, table_id).await;
    // Two adjacent tablets, each spanning the cluster.
    let first = create_tablet_on_cluster(&mut nodes, table_id, bounds(b"a", b"m")).await;
    let second = create_tablet_on_cluster(&mut nodes, table_id, bounds(b"m", b"z")).await;
    let first_rows = row_map(&[(b"apple", b"v-apple"), (b"fig", b"v-fig")]);
    let second_rows = row_map(&[(b"mango", b"v-mango"), (b"yam", b"v-yam")]);
    write_rows(
        &nodes,
        first.tablet_id,
        &first_rows.clone().into_iter().collect::<Vec<_>>(),
    )
    .await;
    write_rows(
        &nodes,
        second.tablet_id,
        &second_rows.clone().into_iter().collect::<Vec<_>>(),
    )
    .await;
    for node in &nodes {
        wait_rows(node, first.tablet_id, &first_rows).await;
        wait_rows(node, second.tablet_id, &second_rows).await;
    }

    let mut driver = meta_leader_index(&nodes).await;
    // Mark both sources Merging; create the hidden replacement.
    let (phase, _) = merge_step_retry(
        &mut nodes,
        &mut driver,
        first.tablet_id,
        second.tablet_id,
        &control,
    )
    .await;
    assert_eq!(phase, MergePhase::MarkedMerging);
    let (phase, _) = merge_step_retry(
        &mut nodes,
        &mut driver,
        first.tablet_id,
        second.tablet_id,
        &control,
    )
    .await;
    assert_eq!(phase, MergePhase::ReplacementCreated);
    // The peers adopt the replacement; its group elects.
    let version = meta_version(&nodes[driver]);
    for (index, node) in nodes.iter_mut().enumerate() {
        if index == driver {
            continue;
        }
        wait_meta_caught_up(node, version).await;
        let report = node.sync_hosted_tablets(&control).await.unwrap();
        assert_eq!(report.created.len(), 1);
    }
    let replacement_id = {
        let creating: Vec<TabletDescriptor> = meta_descriptors(&nodes[driver])
            .into_iter()
            .filter(|descriptor| descriptor.state == TabletState::Creating)
            .collect();
        assert_eq!(creating.len(), 1);
        creating[0].tablet_id
    };
    ensure_tablet_leader(&nodes, replacement_id, driver).await;
    // Pin, build, catch up, publish.
    for expected in [
        MergePhase::SnapshotsPinned,
        MergePhase::ReplacementBuilt,
        MergePhase::CaughtUp,
    ] {
        let (phase, _) = merge_step_retry(
            &mut nodes,
            &mut driver,
            first.tablet_id,
            second.tablet_id,
            &control,
        )
        .await;
        assert_eq!(phase, expected);
    }
    let (phase, published) = merge_step_retry(
        &mut nodes,
        &mut driver,
        first.tablet_id,
        second.tablet_id,
        &control,
    )
    .await;
    assert_eq!(phase, MergePhase::Published);
    let command = published.expect("the publish step yields its command");
    // Both sources started at generation 1: p = max(1, 1) + 2 = 3.
    assert_eq!(command.publish_generation(), 3);
    let meta_state = nodes[driver].meta_group().unwrap().state();
    let replacement = meta_state.tablet(command.replacement.tablet_id).unwrap();
    assert_eq!(replacement.state, TabletState::Active);
    assert_eq!(replacement.generation, 3);
    assert_eq!(replacement.partition, bounds(b"a", b"z"));
    for source in &command.sources {
        let stored = meta_state.tablet(source.tablet_id).unwrap();
        assert_eq!(stored.state, TabletState::Retiring);
        assert_eq!(stored.generation, 3);
        assert!(matches!(
            check_generation(stored, 1),
            Err(RoutingError::TabletMoved { .. })
        ));
    }
    // Routing resolves every key to the replacement.
    let descriptors = meta_descriptors(&nodes[driver]);
    for probe in ["apple", "fig", "mango", "yam"] {
        assert_eq!(
            find_tablet_for_key(&descriptors, table_id, &key(probe.as_bytes()))
                .unwrap()
                .tablet_id,
            command.replacement.tablet_id
        );
    }
    // Retire the sources; the peers tear their replicas down on sync.
    let (phase, _) = merge_step_retry(
        &mut nodes,
        &mut driver,
        first.tablet_id,
        second.tablet_id,
        &control,
    )
    .await;
    assert_eq!(phase, MergePhase::SourcesRetired);
    for source in &command.sources {
        assert!(nodes[driver]
            .meta_group()
            .unwrap()
            .state()
            .tablet(source.tablet_id)
            .is_none());
    }
    let version = meta_version(&nodes[driver]);
    for (index, node) in nodes.iter_mut().enumerate() {
        if index == driver {
            continue;
        }
        wait_meta_caught_up(node, version).await;
        let mut torn_down = node.sync_hosted_tablets(&control).await.unwrap().torn_down;
        torn_down.sort();
        let mut sources: Vec<TabletId> = command
            .sources
            .iter()
            .map(|source| source.tablet_id)
            .collect();
        sources.sort();
        assert_eq!(torn_down, sources);
    }
    // No data loss: the replacement holds both sources' rows on every replica.
    let mut expected = first_rows.clone();
    expected.extend(second_rows.clone());
    for node in &nodes {
        wait_rows(node, command.replacement.tablet_id, &expected).await;
    }

    for node in nodes {
        node.shutdown().await.unwrap();
    }
}