scylla 1.6.0

Async CQL driver for Rust, optimized for ScyllaDB, fully compatible with Apache Cassandraâ„¢
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
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
//! `Session` is the main object used in the driver.\
//! It manages all connections to the cluster and allows to execute CQL requests.

use super::execution_profile::{ExecutionProfile, ExecutionProfileHandle, ExecutionProfileInner};
use super::pager::{PreparedPagerConfig, QueryPager};
use super::{Compression, PoolSize, SelfIdentity, WriteCoalescingDelay};
use crate::authentication::AuthenticatorProvider;
use crate::client::client_routes::ClientRoutesConfig;
use crate::cluster::node::{KnownNode, NodeRef};
use crate::cluster::{Cluster, ClusterNeatDebug, ClusterState};
use crate::errors::{
    BadQuery, BrokenConnectionError, ExecutionError, MetadataError, NewSessionError,
    PagerExecutionError, PrepareError, RequestAttemptError, RequestError, SchemaAgreementError,
    TracingError, UseKeyspaceError,
};
use crate::frame::response::result;
use crate::network::tls::TlsProvider;
use crate::network::{Connection, ConnectionConfig, PoolConfig, VerifiedKeyspaceName};
use crate::observability::driver_tracing::RequestSpan;
use crate::observability::history::{self, HistoryListener};
#[cfg(feature = "metrics")]
use crate::observability::metrics::Metrics;
use crate::observability::tracing::TracingInfo;
use crate::policies::address_translator::AddressTranslator;
use crate::policies::host_filter::HostFilter;
use crate::policies::load_balancing::{self, RoutingInfo};
use crate::policies::reconnect::ExponentialReconnectPolicy;
#[cfg(all(scylla_unstable, feature = "unstable-reconnect-policy"))]
use crate::policies::reconnect::ReconnectPolicy;
use crate::policies::retry::{RequestInfo, RetryDecision, RetrySession};
use crate::policies::speculative_execution;
use crate::policies::timestamp_generator::TimestampGenerator;
use crate::response::query_result::{MaybeFirstRowError, QueryResult, RowsError};
use crate::response::{
    Coordinator, NonErrorQueryResponse, PagingState, PagingStateResponse, QueryResponse,
};
use crate::routing::partitioner::PartitionerName;
use crate::routing::{Shard, ShardAwarePortRange};
use crate::statement::batch::batch_values;
use crate::statement::batch::{Batch, BatchStatement};
use crate::statement::prepared::{PartitionKeyError, PreparedStatement};
use crate::statement::unprepared::Statement;
use crate::statement::{Consistency, PageSize, StatementConfig};
use arc_swap::ArcSwapOption;
use futures::future::join_all;
use futures::future::try_join_all;
use itertools::Itertools;
use scylla_cql::frame::response::NonErrorResponseWithDeserializedMetadataV2 as NonErrorResponseWithDeserializedMetadata;
use scylla_cql::frame::response::error::DbError;
use scylla_cql::serialize::batch::BatchValues;
use scylla_cql::serialize::row::{SerializeRow, SerializedValues};
use std::borrow::Borrow;
use std::future::Future;
use std::net::{IpAddr, SocketAddr};
use std::num::NonZeroU32;
use std::ops::ControlFlow;
use std::sync::{Arc, OnceLock};
use std::time::Duration;
use thiserror::Error;
use tokio::time::timeout;
use tracing::{Instrument, debug, error, trace, trace_span};
use uuid::Uuid;

pub(crate) const TABLET_CHANNEL_SIZE: usize = 8192;

// Query used for schema agreement checks
const SCHEMA_VERSION_QUERY_STR: &str = "SELECT schema_version FROM system.local WHERE key='local'";

/// Statements for internal driver operations.
///
/// We would like those to be prepared, but there are issues related to
/// changing connection keyspace: https://github.com/scylladb/scylla-rust-driver/issues/1561
#[derive(Default)]
struct InternalStatements {
    /// Statement for querying tracing session info from system_traces.sessions
    tracing_session: OnceLock<Statement>,
    /// Statement for querying tracing events from system_traces.events
    tracing_events: OnceLock<Statement>,
    /// Statement for fetching schema version during schema agreement checks
    schema_version: OnceLock<Statement>,
}

/// `Session` manages connections to the cluster and allows to execute CQL requests.
pub struct Session {
    cluster: Cluster,
    default_execution_profile_handle: ExecutionProfileHandle,
    schema_agreement_interval: Duration,
    #[cfg(feature = "metrics")]
    metrics: Arc<Metrics>,
    schema_agreement_timeout: Duration,
    schema_agreement_automatic_waiting: bool,
    refresh_metadata_on_auto_schema_agreement: bool,
    keyspace_name: Arc<ArcSwapOption<String>>,
    tracing_info_fetch_attempts: NonZeroU32,
    tracing_info_fetch_interval: Duration,
    tracing_info_fetch_consistency: Consistency,
    internal_statements: InternalStatements,
}

/// This implementation deliberately omits some details from Cluster in order
/// to avoid cluttering the print with much information of little usability.
impl std::fmt::Debug for Session {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut d = f.debug_struct("Session");
        d.field("cluster", &ClusterNeatDebug(&self.cluster))
            .field(
                "default_execution_profile_handle",
                &self.default_execution_profile_handle,
            )
            .field("schema_agreement_interval", &self.schema_agreement_interval);

        #[cfg(feature = "metrics")]
        d.field("metrics", &self.metrics);

        d.field(
            "auto_await_schema_agreement_timeout",
            &self.schema_agreement_timeout,
        )
        .field(
            "schema_agreement_automatic_waiting",
            &self.schema_agreement_automatic_waiting,
        )
        .field(
            "refresh_metadata_on_auto_schema_agreement",
            &self.refresh_metadata_on_auto_schema_agreement,
        )
        .field("keyspace_name", &self.keyspace_name)
        .field(
            "tracing_info_fetch_attempts",
            &self.tracing_info_fetch_attempts,
        )
        .field(
            "tracing_info_fetch_interval",
            &self.tracing_info_fetch_interval,
        )
        .field(
            "tracing_info_fetch_consistency",
            &self.tracing_info_fetch_consistency,
        )
        .finish()
    }
}

/// Represents a TLS context used to configure TLS connections to DB nodes.
/// Abstracts over various TLS implementations, such as OpenSSL and Rustls.
#[derive(Clone)] // Cheaply clonable - reference counted.
#[non_exhaustive]
pub enum TlsContext {
    /// TLS context backed by OpenSSL 0.10.
    #[cfg(feature = "openssl-010")]
    OpenSsl010(openssl::ssl::SslContext),
    /// TLS context backed by Rustls 0.23.
    #[cfg(feature = "rustls-023")]
    Rustls023(Arc<rustls::ClientConfig>),
}

#[cfg(feature = "openssl-010")]
impl From<openssl::ssl::SslContext> for TlsContext {
    fn from(value: openssl::ssl::SslContext) -> Self {
        TlsContext::OpenSsl010(value)
    }
}

#[cfg(feature = "rustls-023")]
impl From<Arc<rustls::ClientConfig>> for TlsContext {
    fn from(value: Arc<rustls::ClientConfig>) -> Self {
        TlsContext::Rustls023(value)
    }
}

/// Configuration options for [`Session`].
/// Can be created manually, but usually it's easier to use
/// [SessionBuilder](super::session_builder::SessionBuilder)
#[derive(Clone)]
#[non_exhaustive]
pub struct SessionConfig {
    /// List of database servers known on Session startup.
    /// Session will connect to these nodes to retrieve information about other nodes in the cluster.
    /// Each node can be represented as a hostname or an IP address.
    pub known_nodes: Vec<KnownNode>,

    /// A local ip address to bind all driver's TCP sockets to.
    ///
    /// By default set to None, which is equivalent to:
    /// - `INADDR_ANY` for IPv4 ([`Ipv4Addr::UNSPECIFIED`][std::net::Ipv4Addr::UNSPECIFIED])
    /// - `in6addr_any` for IPv6 ([`Ipv6Addr::UNSPECIFIED`][std::net::Ipv6Addr::UNSPECIFIED])
    pub local_ip_address: Option<IpAddr>,

    /// Specifies the local port range used for shard-aware connections.
    ///
    /// By default set to [`ShardAwarePortRange::EPHEMERAL_PORT_RANGE`].
    pub shard_aware_local_port_range: ShardAwarePortRange,

    /// Preferred compression algorithm to use on connections.
    /// If it's not supported by database server Session will fall back to no compression.
    pub compression: Option<Compression>,

    /// Whether to set the nodelay TCP flag.
    pub tcp_nodelay: bool,

    /// TCP keepalive interval, which means how often keepalive messages
    /// are sent **on TCP layer** when a connection is idle.
    /// If `None`, no TCP keepalive messages are sent.
    pub tcp_keepalive_interval: Option<Duration>,

    /// Size of the TCP receive buffer in bytes.
    /// If `None`, the OS default is used.
    pub tcp_recv_buffer_size: Option<usize>,

    /// Size of the TCP send buffer in bytes.
    /// If `None`, the OS default is used.
    pub tcp_send_buffer_size: Option<usize>,

    /// Whether to set the `SO_REUSEADDR` socket option.
    /// If `None`, the OS default is used (typically `false`).
    pub tcp_reuse_address: Option<bool>,

    /// Linger duration for the socket.
    /// If `None`, the OS default is used (lingering disabled).
    /// Setting this to `Some(Duration::ZERO)` causes the connection to be reset (RST) on close.
    pub tcp_linger: Option<Duration>,

    /// Handle to the default execution profile, which is used
    /// for all statements that do not specify an execution profile.
    pub default_execution_profile_handle: ExecutionProfileHandle,

    /// Keyspace to be used on all connections.
    /// Each connection will send `"USE <keyspace_name>"` before sending any requests.
    /// This can be later changed with [`Session::use_keyspace`].
    pub used_keyspace: Option<String>,

    /// Whether the keyspace name is case-sensitive.
    /// This is used to determine how the keyspace name is sent to the server:
    /// - if case-insensitive, it is sent as-is,
    /// - if case-sensitive, it is enclosed in double quotes.
    pub keyspace_case_sensitive: bool,

    /// TLS context used configure TLS connections to DB nodes.
    pub tls_context: Option<TlsContext>,

    /// Custom authenticator provider to create an authenticator instance
    /// upon session creation.
    pub authenticator: Option<Arc<dyn AuthenticatorProvider>>,

    /// Timeout for establishing connections to a node.
    ///
    /// If it's higher than underlying os's default connection timeout, it won't have
    /// any effect.
    pub connect_timeout: Duration,

    /// Size of the per-node connection pool, i.e. how many connections the driver should keep to each node.
    /// The default is `PerShard(1)`, which is the recommended setting for ScyllaDB clusters.
    pub connection_pool_size: PoolSize,

    /// If true, prevents the driver from connecting to the shard-aware port, even if the node supports it.
    /// Generally, this options is best left as default (false).
    pub disallow_shard_aware_port: bool,

    /// Policy that determines how long the connection pool waits between attempts
    /// to fill connections to given host.
    #[cfg(all(scylla_unstable, feature = "unstable-reconnect-policy"))]
    pub reconnect_policy: Arc<dyn ReconnectPolicy>,

    ///  Timestamp generator used for generating timestamps on the client-side
    ///  If None, server-side timestamps are used.
    pub timestamp_generator: Option<Arc<dyn TimestampGenerator>>,

    /// If empty, fetch all keyspaces
    pub keyspaces_to_fetch: Vec<String>,

    /// If true, full schema is fetched with every metadata refresh.
    pub fetch_schema_metadata: bool,

    /// Custom timeout for requests that query metadata.
    pub metadata_request_serverside_timeout: Option<Duration>,

    /// Interval of sending keepalive requests.
    /// If `None`, keepalives are never sent, so `Self::keepalive_timeout` has no effect.
    pub keepalive_interval: Option<Duration>,

    /// Controls after what time of not receiving response to keepalives a connection is closed.
    /// If `None`, connections are never closed due to lack of response to a keepalive message.
    pub keepalive_timeout: Option<Duration>,

    /// How often the driver should ask if schema is in agreement.
    pub schema_agreement_interval: Duration,

    /// Controls the timeout for waiting for schema agreement.
    /// This works both for manual awaiting schema agreement and for
    /// automatic waiting after a schema-altering statement is sent.
    pub schema_agreement_timeout: Duration,

    /// Controls whether schema agreement is automatically awaited
    /// after sending a schema-altering statement.
    pub schema_agreement_automatic_waiting: bool,

    /// If true, full schema metadata is fetched after successfully reaching a schema agreement.
    /// It is true by default but can be disabled if successive schema-altering statements should be performed.
    pub refresh_metadata_on_auto_schema_agreement: bool,

    /// DNS hostname resolution timeout.
    /// If `None`, the driver will wait for hostname resolution indefinitely.
    pub hostname_resolution_timeout: Option<Duration>,

    /// The address translator is used to translate addresses received from ScyllaDB nodes
    /// (either with cluster metadata or with an event) to addresses that can be used to
    /// actually connect to those nodes. This may be needed e.g. when there is NAT
    /// between the nodes and the driver.
    pub address_translator: Option<Arc<dyn AddressTranslator>>,

    /// Routing configuration for Scylla Cloud. If set, Session will connect
    /// to Scylla Cloud clusters using custom routing based on `system.client_routes`.
    #[cfg(feature = "unstable-client-routes")]
    pub(crate) client_routes_config: Option<super::client_routes::ClientRoutesConfig>,

    /// The host filter decides whether any connections should be opened
    /// to the node or not. The driver will also avoid filtered out nodes when
    /// re-establishing the control connection.
    pub host_filter: Option<Arc<dyn HostFilter>>,

    #[cfg(all(scylla_unstable, feature = "unstable-host-listener"))]
    /// Optional listener for host events (ADD, REMOVE, UP, DOWN).
    pub host_listener: Option<Arc<dyn crate::policies::host_listener::HostListener>>,

    /// If true, the driver will inject a delay controlled by [`SessionConfig::write_coalescing_delay`]
    /// before flushing data to the socket.
    /// This gives the driver an opportunity to collect more write requests
    /// and write them in a single syscall, increasing the efficiency.
    ///
    /// However, this optimization may worsen latency if the rate of requests
    /// issued by the application is low, but otherwise the application is
    /// heavily loaded with other tasks on the same tokio executor.
    /// Please do performance measurements before committing to disabling
    /// this option.
    pub enable_write_coalescing: bool,

    /// Controls the write coalescing delay (if enabled).
    ///
    /// This option has no effect if [`SessionConfig::enable_write_coalescing`] is false.
    ///
    /// This option is [`WriteCoalescingDelay::SmallNondeterministic`] by default.
    pub write_coalescing_delay: WriteCoalescingDelay,

    /// Number of attempts to fetch [`TracingInfo`]
    /// in [`Session::get_tracing_info`]. Tracing info
    /// might not be available immediately on queried node - that's why
    /// the driver performs a few attempts with sleeps in between.
    pub tracing_info_fetch_attempts: NonZeroU32,

    /// Delay between attempts to fetch [`TracingInfo`]
    /// in [`Session::get_tracing_info`]. Tracing info
    /// might not be available immediately on queried node - that's why
    /// the driver performs a few attempts with sleeps in between.
    pub tracing_info_fetch_interval: Duration,

    /// Consistency level of fetching [`TracingInfo`]
    /// in [`Session::get_tracing_info`].
    pub tracing_info_fetch_consistency: Consistency,

    /// Interval between refreshing cluster metadata. This
    /// can be configured according to the traffic pattern
    /// for e.g: if they do not want unexpected traffic
    /// or they expect the topology to change frequently.
    pub cluster_metadata_refresh_interval: Duration,

    /// Driver and application self-identifying information,
    /// to be sent to server in STARTUP message.
    pub identity: SelfIdentity<'static>,
}

impl SessionConfig {
    /// Creates a [`SessionConfig`] with default configuration
    /// # Default configuration
    /// * Compression: None
    /// * Load balancing policy: Token-aware Round-robin
    ///
    /// # Example
    /// ```
    /// # use scylla::client::session::SessionConfig;
    /// let config = SessionConfig::new();
    /// ```
    pub fn new() -> Self {
        SessionConfig {
            known_nodes: Vec::new(),
            local_ip_address: None,
            shard_aware_local_port_range: ShardAwarePortRange::EPHEMERAL_PORT_RANGE,
            compression: None,
            tcp_nodelay: true,
            tcp_keepalive_interval: None,
            tcp_recv_buffer_size: None,
            tcp_send_buffer_size: None,
            tcp_reuse_address: None,
            tcp_linger: None,
            schema_agreement_interval: Duration::from_millis(200),
            default_execution_profile_handle: ExecutionProfile::new_from_inner(Default::default())
                .into_handle(),
            used_keyspace: None,
            keyspace_case_sensitive: false,
            tls_context: None,
            authenticator: None,
            connect_timeout: Duration::from_secs(5),
            hostname_resolution_timeout: Some(Duration::from_secs(5)),
            connection_pool_size: Default::default(),
            disallow_shard_aware_port: false,
            #[cfg(all(scylla_unstable, feature = "unstable-reconnect-policy"))]
            reconnect_policy: Arc::new(ExponentialReconnectPolicy::new()),
            timestamp_generator: None,
            keyspaces_to_fetch: Vec::new(),
            fetch_schema_metadata: true,
            metadata_request_serverside_timeout: Some(Duration::from_secs(2)),
            keepalive_interval: Some(Duration::from_secs(30)),
            keepalive_timeout: Some(Duration::from_secs(30)),
            schema_agreement_timeout: Duration::from_secs(60),
            schema_agreement_automatic_waiting: true,
            address_translator: None,
            host_filter: None,
            #[cfg(all(scylla_unstable, feature = "unstable-host-listener"))]
            host_listener: None,
            refresh_metadata_on_auto_schema_agreement: true,
            enable_write_coalescing: true,
            write_coalescing_delay: WriteCoalescingDelay::SmallNondeterministic,
            tracing_info_fetch_attempts: NonZeroU32::new(10).unwrap(),
            tracing_info_fetch_interval: Duration::from_millis(3),
            tracing_info_fetch_consistency: Consistency::One,
            cluster_metadata_refresh_interval: Duration::from_secs(60),
            identity: SelfIdentity::default(),
            #[cfg(feature = "unstable-client-routes")]
            client_routes_config: None,
        }
    }

    /// Adds a known database server with a hostname.
    /// If the port is not explicitly specified, 9042 is used as default
    /// # Example
    /// ```
    /// # use scylla::client::session::SessionConfig;
    /// let mut config = SessionConfig::new();
    /// config.add_known_node("127.0.0.1");
    /// config.add_known_node("db1.example.com:9042");
    /// ```
    pub fn add_known_node(&mut self, hostname: impl AsRef<str>) {
        self.known_nodes
            .push(KnownNode::Hostname(hostname.as_ref().to_string()));
    }

    /// Adds a known database server with an IP address
    /// # Example
    /// ```
    /// # use scylla::client::session::SessionConfig;
    /// # use std::net::{SocketAddr, IpAddr, Ipv4Addr};
    /// let mut config = SessionConfig::new();
    /// config.add_known_node_addr(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 9042));
    /// ```
    pub fn add_known_node_addr(&mut self, node_addr: SocketAddr) {
        self.known_nodes.push(KnownNode::Address(node_addr));
    }

    /// Adds a list of known database server with hostnames.
    /// If the port is not explicitly specified, 9042 is used as default
    /// # Example
    /// ```
    /// # use scylla::client::session::SessionConfig;
    /// # use std::net::{SocketAddr, IpAddr, Ipv4Addr};
    /// let mut config = SessionConfig::new();
    /// config.add_known_nodes(&["127.0.0.1:9042", "db1.example.com"]);
    /// ```
    pub fn add_known_nodes(&mut self, hostnames: impl IntoIterator<Item = impl AsRef<str>>) {
        for hostname in hostnames {
            self.add_known_node(hostname);
        }
    }

    /// Adds a list of known database servers with IP addresses
    /// # Example
    /// ```
    /// # use scylla::client::session::SessionConfig;
    /// # use std::net::{SocketAddr, IpAddr, Ipv4Addr};
    /// let addr1 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(172, 17, 0, 3)), 9042);
    /// let addr2 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(172, 17, 0, 4)), 9042);
    ///
    /// let mut config = SessionConfig::new();
    /// config.add_known_nodes_addr(&[addr1, addr2]);
    /// ```
    pub fn add_known_nodes_addr(
        &mut self,
        node_addrs: impl IntoIterator<Item = impl Borrow<SocketAddr>>,
    ) {
        for address in node_addrs {
            self.add_known_node_addr(*address.borrow());
        }
    }
}

/// Creates default [`SessionConfig`], same as [`SessionConfig::new`]
impl Default for SessionConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl SessionConfig {
    /// [SessionConfig] may unfortunately represent invalid configurations. We need to rule them out
    /// at runtime by running validation.
    #[expect(clippy::result_large_err)] // TODO(2.0): Make NewSessionError smaller.
    fn validate(&self) -> Result<(), NewSessionError> {
        // Ensure there is at least one known node
        if self.known_nodes.is_empty() {
            return Err(NewSessionError::EmptyKnownNodesList);
        }

        // Ensure no illegal configuration with Client Routes
        #[cfg(feature = "unstable-client-routes")]
        if self.client_routes_config.is_some() {
            if self.address_translator.is_some() {
                return Err(NewSessionError::IllegalConfig(
                    "User-provided address translator is not supported if ClientRoutesConfig is provided, \
                        because the driver uses its own custom translator for client routes".into(),
                ));
            }

            if self.tls_context.is_some() {
                return Err(NewSessionError::IllegalConfig(
                    "TLS is not (yet) supported if ClientRoutesConfig is provided, \
                        because of architectural limitations that are out of the driver's scope"
                        .into(),
                ));
            }
        }

        Ok(())
    }
}

pub(crate) enum RunRequestResult<ResT> {
    IgnoredWriteError,
    Completed(ResT),
}

/// Represents a CQL session, which can be used to communicate
/// with the database
impl Session {
    /// Sends a request to the database and receives a response.\
    /// Executes an unprepared CQL statement without paging, i.e. all results are received in a single response.
    ///
    /// This is the easiest way to execute a CQL statement, but performance is worse than that of prepared statements.
    ///
    /// It is discouraged to use this method with non-empty values argument ([`SerializeRow::is_empty()`]
    /// trait method returns false). In such case, statement first needs to be prepared (on a single connection), so
    /// driver will perform 2 round trips instead of 1. Please use [`Session::execute_unpaged()`] instead.
    ///
    /// As all results come in one response (no paging is done!), the memory footprint and latency may be huge
    /// for statements returning rows (i.e. SELECTs)! Prefer this method for non-SELECTs, and for SELECTs
    /// it is best to use paged requests:
    /// - to receive multiple pages and transparently iterate through them, use [query_iter](Session::query_iter).
    /// - to manually receive multiple pages and iterate through them, use [query_single_page](Session::query_single_page).
    ///
    /// See [the book](https://rust-driver.docs.scylladb.com/stable/statements/unprepared.html) for more information
    /// # Arguments
    /// * `statement` - statement to be executed, can be just a `&str` or the [`Statement`] struct.
    /// * `values` - values bound to the statement, the easiest way is to use a tuple of bound values.
    ///
    /// # Examples
    /// ```rust
    /// # use scylla::client::session::Session;
    /// # use std::error::Error;
    /// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
    /// // Insert an int and text into a table.
    /// session
    ///     .query_unpaged(
    ///         "INSERT INTO ks.tab (a, b) VALUES(?, ?)",
    ///         (2_i32, "some text")
    ///     )
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    /// ```rust
    /// # use scylla::client::session::Session;
    /// # use std::error::Error;
    /// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
    ///
    /// // Read rows containing an int and text.
    /// // Keep in mind that all results come in one response (no paging is done!),
    /// // so the memory footprint and latency may be huge!
    /// // To prevent that, use `Session::query_iter` or `Session::query_single_page`.
    /// let query_rows = session
    ///     .query_unpaged("SELECT a, b FROM ks.tab", &[])
    ///     .await?
    ///     .into_rows_result()?;
    ///
    /// for row in query_rows.rows()? {
    ///     // Parse row as int and text.
    ///     let (int_val, text_val): (i32, &str) = row?;
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn query_unpaged(
        &self,
        statement: impl Into<Statement>,
        values: impl SerializeRow,
    ) -> Result<QueryResult, ExecutionError> {
        self.do_query_unpaged(&statement.into(), values).await
    }

    /// Queries a single page from the database, optionally continuing from a saved point.
    ///
    /// It is discouraged to use this method with non-empty values argument ([`SerializeRow::is_empty()`]
    /// trait method returns false). In such case, CQL statement first needs to be prepared (on a single connection), so
    /// driver will perform 2 round trips instead of 1. Please use [`Session::execute_single_page()`] instead.
    ///
    /// # Arguments
    ///
    /// * `statement` - statement to be executed
    /// * `values` - values bound to the statement
    /// * `paging_state` - previously received paging state or [PagingState::start()]
    ///
    /// # Example
    ///
    /// ```rust
    /// # use scylla::client::session::Session;
    /// # use std::error::Error;
    /// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
    /// use std::ops::ControlFlow;
    /// use scylla::response::PagingState;
    ///
    /// // Manual paging in a loop, unprepared statement.
    /// let mut paging_state = PagingState::start();
    /// loop {
    ///    let (res, paging_state_response) = session
    ///        .query_single_page("SELECT a, b, c FROM ks.tbl", &[], paging_state)
    ///        .await?;
    ///
    ///    // Do something with a single page of results.
    ///    for row in res
    ///        .into_rows_result()?
    ///        .rows::<(i32, &str)>()?
    ///    {
    ///        let (a, b) = row?;
    ///    }
    ///
    ///    match paging_state_response.into_paging_control_flow() {
    ///        ControlFlow::Break(()) => {
    ///            // No more pages to be fetched.
    ///            break;
    ///        }
    ///        ControlFlow::Continue(new_paging_state) => {
    ///            // Update paging state from the response, so that query
    ///            // will be resumed from where it ended the last time.
    ///            paging_state = new_paging_state;
    ///        }
    ///    }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn query_single_page(
        &self,
        statement: impl Into<Statement>,
        values: impl SerializeRow,
        paging_state: PagingState,
    ) -> Result<(QueryResult, PagingStateResponse), ExecutionError> {
        self.do_query_single_page(&statement.into(), values, paging_state)
            .await
    }

    /// Execute an unprepared CQL statement with paging\
    /// This method will query all pages of the result\
    ///
    /// Returns an async iterator (stream) over all received rows\
    /// Page size can be specified in the [`Statement`] passed to the function
    ///
    /// It is discouraged to use this method with non-empty values argument ([`SerializeRow::is_empty()`]
    /// trait method returns false). In such case, statement first needs to be prepared (on a single connection), so
    /// driver will initially perform 2 round trips instead of 1. Please use [`Session::execute_iter()`] instead.
    ///
    /// See [the book](https://rust-driver.docs.scylladb.com/stable/statements/paged.html) for more information.
    ///
    /// # Arguments
    /// * `statement` - statement to be executed, can be just a `&str` or the [`Statement`] struct.
    /// * `values` - values bound to the statement, the easiest way is to use a tuple of bound values.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use scylla::client::session::Session;
    /// # use std::error::Error;
    /// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
    /// use futures::stream::StreamExt;
    ///
    /// let mut rows_stream = session
    ///    .query_iter("SELECT a, b FROM ks.t", &[])
    ///    .await?
    ///    .rows_stream::<(i32, i32)>()?;
    ///
    /// while let Some(next_row_res) = rows_stream.next().await {
    ///     let (a, b): (i32, i32) = next_row_res?;
    ///     println!("a, b: {}, {}", a, b);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn query_iter(
        &self,
        statement: impl Into<Statement>,
        values: impl SerializeRow,
    ) -> Result<QueryPager, PagerExecutionError> {
        self.do_query_iter(statement.into(), values).await
    }

    /// Execute a prepared statement. Requires a [PreparedStatement]
    /// generated using [`Session::prepare`](Session::prepare).\
    /// Performs an unpaged request, i.e. all results are received in a single response.
    ///
    /// As all results come in one response (no paging is done!), the memory footprint and latency may be huge
    /// for statements returning rows (i.e. SELECTs)! Prefer this method for non-SELECTs, and for SELECTs
    /// it is best to use paged requests:
    /// - to receive multiple pages and transparently iterate through them, use [execute_iter](Session::execute_iter).
    /// - to manually receive multiple pages and iterate through them, use [execute_single_page](Session::execute_single_page).
    ///
    /// Prepared statements are much faster than unprepared statements:
    /// * Database doesn't need to parse the statement string upon each execution (only once)
    /// * They are properly load balanced using token aware routing
    ///
    /// > ***Warning***\
    /// > For token/shard aware load balancing to work properly, all partition key values
    /// > must be sent as bound values
    /// > (see [performance section](https://rust-driver.docs.scylladb.com/stable/statements/prepared.html#performance)).
    ///
    /// See [the book](https://rust-driver.docs.scylladb.com/stable/statements/prepared.html) for more information.
    ///
    /// # Arguments
    /// * `prepared` - the prepared statement to execute, generated using [`Session::prepare`](Session::prepare)
    /// * `values` - values bound to the statement, the easiest way is to use a tuple of bound values
    ///
    /// # Example
    /// ```rust
    /// # use scylla::client::session::Session;
    /// # use std::error::Error;
    /// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
    /// use scylla::statement::prepared::PreparedStatement;
    ///
    /// // Prepare the statement for later execution
    /// let prepared: PreparedStatement = session
    ///     .prepare("INSERT INTO ks.tab (a) VALUES(?)")
    ///     .await?;
    ///
    /// // Execute the prepared statement with some values, just like an unprepared statement.
    /// let to_insert: i32 = 12345;
    /// session.execute_unpaged(&prepared, (to_insert,)).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn execute_unpaged(
        &self,
        prepared: &PreparedStatement,
        values: impl SerializeRow,
    ) -> Result<QueryResult, ExecutionError> {
        let serialized_values = prepared.serialize_values(&values)?;
        let (result, paging_state) = self
            .execute(prepared, &serialized_values, None, PagingState::start())
            .await?;
        if !paging_state.finished() {
            error!(
                "Unpaged prepared query returned a non-empty paging state! This is a driver-side or server-side bug."
            );
            return Err(ExecutionError::LastAttemptError(
                RequestAttemptError::NonfinishedPagingState,
            ));
        }
        Ok(result)
    }

    /// Executes a prepared statement, restricting results to single page.
    /// Optionally continues fetching results from a saved point.
    ///
    /// # Arguments
    ///
    /// * `prepared` - a statement prepared with [prepare](crate::client::session::Session::prepare)
    /// * `values` - values bound to the statement
    /// * `paging_state` - continuation based on a paging state received from a previous paged query or None
    ///
    /// # Example
    ///
    /// ```rust
    /// # use scylla::client::session::Session;
    /// # use std::error::Error;
    /// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
    /// use std::ops::ControlFlow;
    /// use scylla::statement::unprepared::Statement;
    /// use scylla::response::{PagingState, PagingStateResponse};
    ///
    /// let paged_prepared = session
    ///     .prepare(
    ///         Statement::new("SELECT a, b FROM ks.tbl")
    ///             .with_page_size(100.try_into().unwrap()),
    ///     )
    ///     .await?;
    ///
    /// // Manual paging in a loop, prepared statement.
    /// let mut paging_state = PagingState::start();
    /// loop {
    ///     let (res, paging_state_response) = session
    ///         .execute_single_page(&paged_prepared, &[], paging_state)
    ///         .await?;
    ///
    ///    // Do something with a single page of results.
    ///    for row in res
    ///        .into_rows_result()?
    ///        .rows::<(i32, &str)>()?
    ///    {
    ///        let (a, b) = row?;
    ///    }
    ///
    ///     match paging_state_response.into_paging_control_flow() {
    ///         ControlFlow::Break(()) => {
    ///             // No more pages to be fetched.
    ///             break;
    ///         }
    ///         ControlFlow::Continue(new_paging_state) => {
    ///             // Update paging continuation from the paging state, so that query
    ///             // will be resumed from where it ended the last time.
    ///             paging_state = new_paging_state;
    ///         }
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn execute_single_page(
        &self,
        prepared: &PreparedStatement,
        values: impl SerializeRow,
        paging_state: PagingState,
    ) -> Result<(QueryResult, PagingStateResponse), ExecutionError> {
        let serialized_values = prepared.serialize_values(&values)?;
        let page_size = prepared.get_validated_page_size();
        self.execute(prepared, &serialized_values, Some(page_size), paging_state)
            .await
    }

    /// Execute a prepared statement with paging.\
    /// This method will query all pages of the result.\
    ///
    /// Returns an async iterator (stream) over all received rows.\
    /// Page size can be specified in the [PreparedStatement] passed to the function.
    ///
    /// See [the book](https://rust-driver.docs.scylladb.com/stable/statements/paged.html) for more information.
    ///
    /// # Arguments
    /// * `prepared` - the prepared statement to execute, generated using [`Session::prepare`](Session::prepare)
    /// * `values` - values bound to the statement, the easiest way is to use a tuple of bound values
    ///
    /// # Example
    ///
    /// ```rust
    /// # use scylla::client::session::Session;
    /// # use futures::StreamExt as _;
    /// # use std::error::Error;
    /// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
    /// use scylla::statement::prepared::PreparedStatement;
    ///
    /// // Prepare the statement for later execution
    /// let prepared: PreparedStatement = session
    ///     .prepare("SELECT a, b FROM ks.t")
    ///     .await?;
    ///
    /// // Execute the statement and receive all pages
    /// let mut rows_stream = session
    ///    .execute_iter(prepared, &[])
    ///    .await?
    ///    .rows_stream::<(i32, i32)>()?;
    ///
    /// while let Some(next_row_res) = rows_stream.next().await {
    ///     let (a, b): (i32, i32) = next_row_res?;
    ///     println!("a, b: {}, {}", a, b);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn execute_iter(
        &self,
        prepared: impl Into<PreparedStatement>,
        values: impl SerializeRow,
    ) -> Result<QueryPager, PagerExecutionError> {
        let prepared = prepared.into();
        let serialized_values = prepared.serialize_values(&values)?;

        self.execute_iter_nongeneric(prepared, serialized_values)
            .await
    }

    /// Execute a batch statement\
    /// Batch contains many `unprepared` or `prepared` statements which are executed at once\
    /// Batch doesn't return any rows.
    ///
    /// Batch values must contain values for each of the statements.
    ///
    /// Avoid using non-empty values ([`SerializeRow::is_empty()`] return false) for unprepared statements
    /// inside the batch. Such statements will first need to be prepared, so the driver will need to
    /// send (numer_of_unprepared_statements_with_values + 1) requests instead of 1 request, severly
    /// affecting performance.
    ///
    /// See [the book](https://rust-driver.docs.scylladb.com/stable/statements/batch.html) for more information
    ///
    /// # Arguments
    /// * `batch` - [Batch] to be performed
    /// * `values` - List of values for each statement, it's the easiest to use a tuple of tuples
    ///
    /// # Example
    /// ```rust
    /// # use scylla::client::session::Session;
    /// # use std::error::Error;
    /// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
    /// use scylla::statement::batch::Batch;
    ///
    /// let mut batch: Batch = Default::default();
    ///
    /// // A statement with two bound values
    /// batch.append_statement("INSERT INTO ks.tab(a, b) VALUES(?, ?)");
    ///
    /// // A statement with one bound value
    /// batch.append_statement("INSERT INTO ks.tab(a, b) VALUES(3, ?)");
    ///
    /// // A statement with no bound values
    /// batch.append_statement("INSERT INTO ks.tab(a, b) VALUES(5, 6)");
    ///
    /// // Batch values is a tuple of 3 tuples containing values for each statement
    /// let batch_values = ((1_i32, 2_i32), // Tuple with two values for the first statement
    ///                     (4_i32,),       // Tuple with one value for the second statement
    ///                     ());            // Empty tuple/unit for the third statement
    ///
    /// // Run the batch
    /// session.batch(&batch, batch_values).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn batch(
        &self,
        batch: &Batch,
        values: impl BatchValues,
    ) -> Result<QueryResult, ExecutionError> {
        // Shard-awareness behavior for batch will be to pick shard based on first batch statement's shard
        // If users batch statements by shard, they will be rewarded with full shard awareness

        // check to ensure that we don't send a batch statement with more than u16::MAX queries
        let batch_statements_length = batch.statements.len();
        if batch_statements_length > u16::MAX as usize {
            return Err(ExecutionError::BadQuery(
                BadQuery::TooManyQueriesInBatchStatement(batch_statements_length),
            ));
        }

        let execution_profile = batch
            .get_execution_profile_handle()
            .unwrap_or_else(|| self.get_default_execution_profile_handle())
            .access();

        let consistency = batch
            .config
            .consistency
            .unwrap_or(execution_profile.consistency);

        let serial_consistency = batch
            .config
            .serial_consistency
            .unwrap_or(execution_profile.serial_consistency);

        let (first_value_token, values) =
            batch_values::peek_first_token(values, batch.statements.first())?;
        let values_ref = &values;

        let table_spec =
            if let Some(BatchStatement::PreparedStatement(ps)) = batch.statements.first() {
                ps.get_table_spec()
            } else {
                None
            };

        let statement_info = RoutingInfo {
            consistency,
            serial_consistency,
            token: first_value_token,
            table: table_spec,
            is_confirmed_lwt: false,
        };

        let span = RequestSpan::new_batch();

        let (run_request_result, coordinator): (
            RunRequestResult<NonErrorQueryResponse>,
            Coordinator,
        ) = self
            .run_request(
                statement_info,
                &batch.config,
                execution_profile,
                |connection: Arc<Connection>,
                 consistency: Consistency,
                 execution_profile: &ExecutionProfileInner| {
                    let serial_consistency = batch
                        .config
                        .serial_consistency
                        .unwrap_or(execution_profile.serial_consistency);
                    async move {
                        connection
                            .batch_with_consistency(
                                batch,
                                values_ref,
                                consistency,
                                serial_consistency,
                            )
                            .await
                            .and_then(QueryResponse::into_non_error_query_response)
                    }
                },
                &span,
            )
            .instrument(span.span().clone())
            .await?;

        let result = match run_request_result {
            RunRequestResult::IgnoredWriteError => QueryResult::mock_empty(coordinator),
            RunRequestResult::Completed(non_error_query_response) => {
                let result = non_error_query_response.into_query_result(coordinator)?;
                span.record_result_fields(&result);
                result
            }
        };

        Ok(result)
    }

    /// Estabilishes a CQL session with the database
    ///
    /// Usually it's easier to use [SessionBuilder](crate::client::session_builder::SessionBuilder)
    /// instead of calling `Session::connect` directly, because it's more convenient.
    /// # Arguments
    /// * `config` - Connection configuration - known nodes, Compression, etc.
    ///   Must contain at least one known node.
    ///
    /// # Example
    /// ```rust
    /// # use std::error::Error;
    /// # async fn check_only_compiles() -> Result<(), Box<dyn Error>> {
    /// use scylla::client::session::{Session, SessionConfig};
    /// use scylla::cluster::KnownNode;
    ///
    /// let mut config = SessionConfig::new();
    /// config.known_nodes.push(KnownNode::Hostname("127.0.0.1:9042".to_string()));
    ///
    /// let session: Session = Session::connect(config).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn connect(config: SessionConfig) -> Result<Self, NewSessionError> {
        config.validate()?;

        let known_nodes = config.known_nodes;

        let (tablet_sender, tablet_receiver) = tokio::sync::mpsc::channel(TABLET_CHANNEL_SIZE);

        let tls_provider = if let Some(tls_context) = config.tls_context {
            // To silence warnings when TlsContext is an empty enum (tls features are disabled).
            // In such case, TlsProvider is uninhabited.
            #[cfg_attr(
                not(any(feature = "openssl-010", feature = "rustls-023")),
                // TODO: make this expect() once MSRV is 1.92+.
                allow(unreachable_code, unused_variables)
            )]
            let provider = TlsProvider::new_with_global_context(tls_context);
            #[cfg_attr(
                not(any(feature = "openssl-010", feature = "rustls-023")),
                // TODO: remove this once MSRV is 1.92+.
                allow(unreachable_code)
            )]
            Some(provider)
        } else {
            None
        };

        let connection_config = ConnectionConfig {
            local_ip_address: config.local_ip_address,
            shard_aware_local_port_range: config.shard_aware_local_port_range,
            compression: config.compression,
            tcp_nodelay: config.tcp_nodelay,
            tcp_keepalive_interval: config.tcp_keepalive_interval,
            tcp_recv_buffer_size: config.tcp_recv_buffer_size,
            tcp_send_buffer_size: config.tcp_send_buffer_size,
            tcp_reuse_address: config.tcp_reuse_address,
            tcp_linger: config.tcp_linger,
            timestamp_generator: config.timestamp_generator,
            tls_provider,
            authenticator: config.authenticator,
            connect_timeout: config.connect_timeout,
            event_sender: None,
            default_consistency: Default::default(),
            address_translator: config.address_translator,
            write_coalescing_delay: config
                .enable_write_coalescing
                .then_some(config.write_coalescing_delay),
            keepalive_interval: config.keepalive_interval,
            keepalive_timeout: config.keepalive_timeout,
            tablet_sender: Some(tablet_sender),
            identity: config.identity,
        };

        let pool_config = PoolConfig {
            connection_config,
            pool_size: config.connection_pool_size,
            can_use_shard_aware_port: !config.disallow_shard_aware_port,
            #[cfg(all(scylla_unstable, feature = "unstable-reconnect-policy"))]
            reconnect_policy: config.reconnect_policy,
            #[cfg(not(all(scylla_unstable, feature = "unstable-reconnect-policy")))]
            reconnect_policy: Arc::new(ExponentialReconnectPolicy::new()),
        };

        #[cfg(feature = "metrics")]
        let metrics = Arc::new(Metrics::new());

        let host_listener = {
            #[cfg(all(scylla_unstable, feature = "unstable-host-listener"))]
            {
                config.host_listener
            }
            #[cfg(not(all(scylla_unstable, feature = "unstable-host-listener")))]
            {
                None
            }
        };

        let client_routes_config: Option<ClientRoutesConfig> = {
            #[cfg(feature = "unstable-client-routes")]
            {
                config.client_routes_config
            }
            #[cfg(not(feature = "unstable-client-routes"))]
            {
                None
            }
        };

        let cluster = Cluster::new(
            known_nodes,
            pool_config,
            config.keyspaces_to_fetch,
            config.fetch_schema_metadata,
            config.metadata_request_serverside_timeout,
            config.hostname_resolution_timeout,
            config.host_filter,
            host_listener,
            config.cluster_metadata_refresh_interval,
            tablet_receiver,
            #[cfg(feature = "metrics")]
            Arc::clone(&metrics),
            client_routes_config,
        )
        .await?;

        let default_execution_profile_handle = config.default_execution_profile_handle;

        let session = Self {
            cluster,
            default_execution_profile_handle,
            schema_agreement_interval: config.schema_agreement_interval,
            #[cfg(feature = "metrics")]
            metrics,
            schema_agreement_timeout: config.schema_agreement_timeout,
            schema_agreement_automatic_waiting: config.schema_agreement_automatic_waiting,
            refresh_metadata_on_auto_schema_agreement: config
                .refresh_metadata_on_auto_schema_agreement,
            keyspace_name: Arc::new(ArcSwapOption::default()), // will be set by use_keyspace
            tracing_info_fetch_attempts: config.tracing_info_fetch_attempts,
            tracing_info_fetch_interval: config.tracing_info_fetch_interval,
            tracing_info_fetch_consistency: config.tracing_info_fetch_consistency,
            internal_statements: InternalStatements::default(),
        };

        if let Some(keyspace_name) = config.used_keyspace {
            session
                .use_keyspace(keyspace_name, config.keyspace_case_sensitive)
                .await?;
        }

        Ok(session)
    }

    /// Creates and returns ref to a shared statement for querying tracing session info.
    fn get_tracing_session_statement(&self) -> &Statement {
        self.internal_statements.tracing_session.get_or_init(|| {
            let mut stmt = Statement::new(crate::observability::tracing::TRACES_SESSION_QUERY_STR);
            stmt.set_page_size(crate::observability::tracing::TRACING_QUERY_PAGE_SIZE);
            stmt.set_consistency(self.tracing_info_fetch_consistency);
            stmt.set_is_idempotent(true);
            stmt
        })
    }

    /// Creates and returns ref to a shared statement for querying tracing events.
    fn get_tracing_events_statement(&self) -> &Statement {
        self.internal_statements.tracing_events.get_or_init(|| {
            let mut stmt = Statement::new(crate::observability::tracing::TRACES_EVENTS_QUERY_STR);
            stmt.set_page_size(crate::observability::tracing::TRACING_QUERY_PAGE_SIZE);
            stmt.set_consistency(self.tracing_info_fetch_consistency);
            stmt.set_is_idempotent(true);
            stmt
        })
    }

    /// Creates and returns ref to a shared statement for querying schema version.
    fn get_schema_version_statement(&self) -> &Statement {
        self.internal_statements.schema_version.get_or_init(|| {
            let mut statement = Statement::new(SCHEMA_VERSION_QUERY_STR);
            // Use ONE consistency for schema version queries - this is a local query
            // that reads from system.local, so ONE is appropriate.
            statement.set_consistency(Consistency::One);
            statement.set_is_idempotent(true);
            statement
        })
    }

    async fn do_query_unpaged(
        &self,
        statement: &Statement,
        values: impl SerializeRow,
    ) -> Result<QueryResult, ExecutionError> {
        let (result, paging_state_response) = self
            .query(statement, values, None, PagingState::start())
            .await?;
        if !paging_state_response.finished() {
            error!(
                "Unpaged unprepared query returned a non-empty paging state! This is a driver-side or server-side bug."
            );
            return Err(ExecutionError::LastAttemptError(
                RequestAttemptError::NonfinishedPagingState,
            ));
        }
        Ok(result)
    }

    async fn do_query_single_page(
        &self,
        statement: &Statement,
        values: impl SerializeRow,
        paging_state: PagingState,
    ) -> Result<(QueryResult, PagingStateResponse), ExecutionError> {
        self.query(
            statement,
            values,
            Some(statement.get_validated_page_size()),
            paging_state,
        )
        .await
    }

    /// Sends a request to the database.
    /// Optionally continues fetching results from a saved point.
    ///
    /// This is now an internal method only.
    ///
    /// Tl;dr: use [Session::query_unpaged], [Session::query_single_page] or [Session::query_iter] instead.
    ///
    /// The rationale is that we believe that paging is so important concept (and it has shown to be error-prone as well)
    /// that we need to require users to make a conscious decision to use paging or not. For that, we expose
    /// the aforementioned 3 methods clearly differing in naming and API, so that no unconscious choices about paging
    /// should be made.
    async fn query(
        &self,
        statement: &Statement,
        values: impl SerializeRow,
        page_size: Option<PageSize>,
        paging_state: PagingState,
    ) -> Result<(QueryResult, PagingStateResponse), ExecutionError> {
        let execution_profile = statement
            .get_execution_profile_handle()
            .unwrap_or_else(|| self.get_default_execution_profile_handle())
            .access();

        let statement_info = RoutingInfo {
            consistency: statement
                .config
                .consistency
                .unwrap_or(execution_profile.consistency),
            serial_consistency: statement
                .config
                .serial_consistency
                .unwrap_or(execution_profile.serial_consistency),
            ..Default::default()
        };

        let span = RequestSpan::new_query(&statement.contents);
        let span_ref = &span;
        let (run_request_result, coordinator): (
            RunRequestResult<NonErrorQueryResponse>,
            Coordinator,
        ) = self
            .run_request(
                statement_info,
                &statement.config,
                execution_profile,
                |connection: Arc<Connection>,
                 consistency: Consistency,
                 execution_profile: &ExecutionProfileInner| {
                    let serial_consistency = statement
                        .config
                        .serial_consistency
                        .unwrap_or(execution_profile.serial_consistency);
                    // Needed to avoid moving query and values into async move block
                    let values_ref = &values;
                    let paging_state_ref = &paging_state;
                    async move {
                        if values_ref.is_empty() {
                            span_ref.record_request_size(0);
                            connection
                                .query_raw_with_consistency(
                                    statement,
                                    consistency,
                                    serial_consistency,
                                    page_size,
                                    paging_state_ref.clone(),
                                )
                                .await
                                .and_then(QueryResponse::into_non_error_query_response)
                        } else {
                            let prepared = connection.prepare(statement).await?;
                            let serialized = prepared.serialize_values(values_ref)?;
                            span_ref.record_request_size(serialized.buffer_size());
                            connection
                                .execute_raw_with_consistency(
                                    &prepared,
                                    &serialized,
                                    consistency,
                                    serial_consistency,
                                    page_size,
                                    paging_state_ref.clone(),
                                )
                                .await
                                .and_then(QueryResponse::into_non_error_query_response)
                        }
                    }
                },
                &span,
            )
            .instrument(span.span().clone())
            .await?;

        let response = match run_request_result {
            RunRequestResult::IgnoredWriteError => NonErrorQueryResponse {
                response: NonErrorResponseWithDeserializedMetadata::Result(
                    result::ResultWithDeserializedMetadata::Void,
                ),
                tracing_id: None,
                warnings: Vec::new(),
            },
            RunRequestResult::Completed(response) => response,
        };

        let (result, paging_state_response) =
            response.into_query_result_and_paging_state(coordinator)?;
        span.record_result_fields(&result);

        Ok((result, paging_state_response))
    }

    pub(crate) async fn handle_set_keyspace_response(
        &self,
        response: &NonErrorQueryResponse,
    ) -> Result<(), UseKeyspaceError> {
        if let Some(set_keyspace) = response.as_set_keyspace() {
            debug!(
                "Detected USE KEYSPACE query, setting session's keyspace to {}",
                set_keyspace.keyspace_name
            );
            self.use_keyspace(set_keyspace.keyspace_name.clone(), true)
                .await?;
        }

        Ok(())
    }
}

#[derive(Debug, Error)]
/// Errors that can occur during automatic awaiting of schema agreement after a schema change.
pub(crate) enum AutoSchemaAwaitingError {
    /// Schema agreement could not be reached.
    #[error("Schema agreement could not be reached: {0}")]
    SchemaAgreement(#[from] SchemaAgreementError),
    /// Metadata refresh after reaching schema agreement failed.
    #[error("Metadata refresh after reaching schema agreement failed: {0}")]
    MetadataRefresh(#[from] MetadataError),
}

impl From<AutoSchemaAwaitingError> for ExecutionError {
    fn from(err: AutoSchemaAwaitingError) -> Self {
        match err {
            AutoSchemaAwaitingError::SchemaAgreement(e) => e.into(),
            AutoSchemaAwaitingError::MetadataRefresh(e) => e.into(),
        }
    }
}

impl Session {
    pub(crate) async fn handle_auto_await_schema_agreement(
        &self,
        response: &NonErrorQueryResponse,
        coordinator_id: Uuid,
    ) -> Result<(), AutoSchemaAwaitingError> {
        if self.schema_agreement_automatic_waiting && response.as_schema_change().is_some() {
            debug!("Detected schema change, so awaiting schema agreement automatically...");
            self.await_schema_agreement_with_required_node(Some(coordinator_id))
                .await?;
            debug!("Auto schema agreement awaiting: schema agreement reached.",);

            if self.refresh_metadata_on_auto_schema_agreement {
                self.refresh_metadata().await?;
            }
        }

        Ok(())
    }

    async fn do_query_iter(
        &self,
        statement: Statement,
        values: impl SerializeRow,
    ) -> Result<QueryPager, PagerExecutionError> {
        if values.is_empty() {
            self.do_query_iter_without_values(statement).await
        } else {
            // Making QueryPager::new_for_query work with values is too hard (if even possible)
            // so instead of sending one prepare to a specific connection on each iterator query,
            // we fully prepare a statement beforehand.
            let prepared = self.prepare_nongeneric(&statement).await?;
            let values = prepared.serialize_values(&values)?;
            self.execute_iter_nongeneric(prepared, values).await
        }
    }

    /// `Session::query_iter` specialization for empty values.
    async fn do_query_iter_without_values(
        &self,
        statement: Statement,
    ) -> Result<QueryPager, PagerExecutionError> {
        let execution_profile = statement
            .get_execution_profile_handle()
            .unwrap_or_else(|| self.get_default_execution_profile_handle())
            .access();

        QueryPager::new_for_query(
            self,
            statement,
            execution_profile,
            self.cluster.get_state(),
            #[cfg(feature = "metrics")]
            Arc::clone(&self.metrics),
        )
        .await
    }

    /// Prepares a statement on the server side and returns a prepared statement,
    /// which can later be used to perform more efficient requests.
    ///
    /// The statement is prepared on all nodes. This function finishes once all nodes respond
    /// with either success or an error.
    ///
    /// Prepared statements are much faster than unprepared statements:
    /// * Database doesn't need to parse the statement string upon each execution (only once)
    /// * They are properly load balanced using token aware routing
    ///
    /// <div class="warning">
    ///
    /// **Warning!**
    ///
    /// **Prepare a statement once** (e.g., store it in a variable, static, or struct field),
    /// then **execute it multiple times** with different values.
    /// Do **NOT** call `prepare()` repeatedly for the same statement before each execution -
    /// this defeats the purpose of prepared statements and significantly degrades performance.
    ///
    /// </div>
    ///
    /// <div class="warning">
    ///
    /// **Warning!**
    ///
    /// For token/shard aware load balancing to work properly, all partition key values
    /// must be sent as bound values
    /// (see [performance section](https://rust-driver.docs.scylladb.com/stable/statements/prepared.html#performance))
    ///
    /// </div>
    ///
    /// See [the book](https://rust-driver.docs.scylladb.com/stable/statements/prepared.html) for more information.
    /// See the documentation of [`PreparedStatement`].
    ///
    /// # Arguments
    /// * `statement` - statement to prepare, can be just a `&str` or the [`Statement`] struct.
    ///
    /// # Example
    /// ```rust
    /// # use scylla::client::session::Session;
    /// # use std::error::Error;
    /// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
    /// use scylla::statement::prepared::PreparedStatement;
    ///
    /// // Prepare the statement ONCE for later execution
    /// let prepared: PreparedStatement = session
    ///     .prepare("INSERT INTO ks.tab (a) VALUES(?)")
    ///     .await?;
    ///
    /// // Execute the prepared statement multiple times
    /// let to_insert: i32 = 12345;
    /// session.execute_unpaged(&prepared, (to_insert,)).await?;
    ///
    /// let to_insert: i32 = 67890;
    /// session.execute_unpaged(&prepared, (to_insert,)).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn prepare(
        &self,
        statement: impl Into<Statement>,
    ) -> Result<PreparedStatement, PrepareError> {
        let statement = statement.into();
        self.prepare_nongeneric(&statement).await
    }

    // Introduced to avoid monomorphisation of this large function.
    async fn prepare_nongeneric(
        &self,
        statement: &Statement,
    ) -> Result<PreparedStatement, PrepareError> {
        let cluster_state = self.get_cluster_state();

        // Start by attempting preparation on a single (random) connection to every node.
        {
            let mut connections_to_nodes = cluster_state.iter_working_connections_to_nodes()?;
            let on_all_nodes_result =
                Self::prepare_on_all(statement, &cluster_state, &mut connections_to_nodes).await;
            if let Ok(prepared) = on_all_nodes_result {
                // We succeeded in preparing the statement on at least one node. We're done.
                // Other nodes could have failed to prepare the statement, but this will be handled
                // as `DbError::Unprepared` upon execution, followed by a repreparation attempt.
                return Ok(prepared);
            }
        }

        // We could have been just unlucky: we could have possibly chosen random connections all of which were defunct
        // (one possibility is that we targeted overloaded shards).
        // Let's try again, this time on connections to every shard. This is a "last call" fallback.
        {
            let mut connections_to_shards = cluster_state.iter_working_connections_to_shards()?;

            Self::prepare_on_all(statement, &cluster_state, &mut connections_to_shards).await
        }
    }

    /// Prepares the statement on all given connections.
    /// These are intended to be connections to either all nodes or all shards.
    ///
    /// ASSUMPTION: the `working_connections` Iterator is nonempty.
    ///
    /// Returns:
    /// - `Ok(PreparedStatement)`, if preparation succeeded on at least one connection;
    /// - `Err(PrepareError)`, if no connection is working or preparation failed on all attempted connections.
    // TODO: There are no timeouts here. So, just one stuck node freezes the driver here, potentially indefinitely long.
    // Also, what the driver requires to get from the cluster is the prepared statement metadata.
    // It suffices that it gets only one copy of it, just from one success response. Therefore, it's a possible
    // optimisation that the function only waits for one preparation to finish successfully, and then it returns.
    // For it to be done, other preparations must continue in the background, on a separate tokio task.
    // Describing issue: #1332.
    async fn prepare_on_all(
        statement: &Statement,
        cluster_state: &ClusterState,
        working_connections: &mut (dyn Iterator<Item = Arc<Connection>> + Send),
    ) -> Result<PreparedStatement, PrepareError> {
        // Find the first result that is Ok, or Err if all failed.
        let preparations =
            working_connections.map(|c| async move { c.prepare_raw(statement).await });
        let raw_prepared_statements_results = join_all(preparations).await;

        let mut raw_prepared_statements_results_iter = raw_prepared_statements_results.into_iter();

        // Safety: We pass a nonempty iterator, so there will be at least one result.
        let first_ok_or_error = raw_prepared_statements_results_iter
            .by_ref()
            .find_or_first(|res| res.is_ok())
            .unwrap(); // Safety: there is at least one connection.

        let mut prepared: PreparedStatement = first_ok_or_error
            .map_err(|first_attempt| PrepareError::AllAttemptsFailed { first_attempt })?
            .into_prepared_statement();

        // Validate prepared ids equality.
        for another_raw_prepared in raw_prepared_statements_results_iter.flatten() {
            if prepared.get_id() != another_raw_prepared.get_id() {
                tracing::error!(
                    "Got differing ids upon statement preparation: statement \"{}\", id1: {:?}, id2: {:?}",
                    prepared.get_statement(),
                    prepared.get_id(),
                    another_raw_prepared.get_id()
                );
                return Err(PrepareError::PreparedStatementIdsMismatch);
            }

            // Collect all tracing ids from prepare() queries in the final result
            prepared
                .prepare_tracing_ids
                .extend(another_raw_prepared.tracing_id());
        }

        // This is the first preparation that succeeded.
        // Let's return the PreparedStatement.
        prepared.set_partitioner_name(
            Self::extract_partitioner_name(&prepared, cluster_state)
                .and_then(PartitionerName::from_str)
                .unwrap_or_default(),
        );

        Ok(prepared)
    }

    fn extract_partitioner_name<'a>(
        prepared: &PreparedStatement,
        cluster_state: &'a ClusterState,
    ) -> Option<&'a str> {
        let table_spec = prepared.get_table_spec()?;
        cluster_state
            .keyspaces
            .get(table_spec.ks_name())?
            .tables
            .get(table_spec.table_name())?
            .partitioner
            .as_deref()
    }

    /// Sends a prepared request to the database, optionally continuing from a saved point.
    ///
    /// This is now an internal method only.
    ///
    /// Tl;dr: use [Session::execute_unpaged], [Session::execute_single_page] or [Session::execute_iter] instead.
    ///
    /// The rationale is that we believe that paging is so important concept (and it has shown to be error-prone as well)
    /// that we need to require users to make a conscious decision to use paging or not. For that, we expose
    /// the aforementioned 3 methods clearly differing in naming and API, so that no unconscious choices about paging
    /// should be made.
    async fn execute(
        &self,
        prepared: &PreparedStatement,
        serialized_values: &SerializedValues,
        page_size: Option<PageSize>,
        paging_state: PagingState,
    ) -> Result<(QueryResult, PagingStateResponse), ExecutionError> {
        let paging_state_ref = &paging_state;

        let (partition_key, token) = prepared
            .extract_partition_key_and_calculate_token(
                prepared.get_partitioner_name(),
                serialized_values,
            )
            .map_err(PartitionKeyError::into_execution_error)?
            .unzip();

        let execution_profile = prepared
            .get_execution_profile_handle()
            .unwrap_or_else(|| self.get_default_execution_profile_handle())
            .access();

        let table_spec = prepared.get_table_spec();

        let statement_info = RoutingInfo {
            consistency: prepared
                .config
                .consistency
                .unwrap_or(execution_profile.consistency),
            serial_consistency: prepared
                .config
                .serial_consistency
                .unwrap_or(execution_profile.serial_consistency),
            token,
            table: table_spec,
            is_confirmed_lwt: prepared.is_confirmed_lwt(),
        };

        let span = RequestSpan::new_prepared(
            partition_key.as_ref().map(|pk| pk.iter()),
            token,
            serialized_values.buffer_size(),
        );

        if !span.span().is_disabled()
            && let (Some(table_spec), Some(token)) = (statement_info.table, token)
        {
            let cluster_state = self.get_cluster_state();
            let replicas = cluster_state.get_token_endpoints_iter(table_spec, token);
            span.record_replicas(replicas)
        }

        let (run_request_result, coordinator): (
            RunRequestResult<NonErrorQueryResponse>,
            Coordinator,
        ) = self
            .run_request(
                statement_info,
                &prepared.config,
                execution_profile,
                |connection: Arc<Connection>,
                 consistency: Consistency,
                 execution_profile: &ExecutionProfileInner| {
                    let serial_consistency = prepared
                        .config
                        .serial_consistency
                        .unwrap_or(execution_profile.serial_consistency);
                    async move {
                        connection
                            .execute_raw_with_consistency(
                                prepared,
                                serialized_values,
                                consistency,
                                serial_consistency,
                                page_size,
                                paging_state_ref.clone(),
                            )
                            .await
                            .and_then(QueryResponse::into_non_error_query_response)
                    }
                },
                &span,
            )
            .instrument(span.span().clone())
            .await?;

        let response = match run_request_result {
            RunRequestResult::IgnoredWriteError => NonErrorQueryResponse {
                response: NonErrorResponseWithDeserializedMetadata::Result(
                    result::ResultWithDeserializedMetadata::Void,
                ),
                tracing_id: None,
                warnings: Vec::new(),
            },
            RunRequestResult::Completed(response) => response,
        };

        let (result, paging_state_response) =
            response.into_query_result_and_paging_state(coordinator)?;
        span.record_result_fields(&result);

        Ok((result, paging_state_response))
    }

    /// Does the same as [`Session::execute_iter`], but without generics.
    /// This is to reduce code bloat.
    ///
    /// Additionally, this function accepts only `SerializedValues`,
    /// so the caller is responsible for serializing the values beforehand.
    /// This:
    /// - on one hand introduces a risk of misusing the API (passing values
    ///   that don't match the prepared statement) - therefore this API
    ///   must not be leaked to end users;
    /// - on the other hand allows manual preserialization of values, which
    ///   we need in wrapper drivers (e.g. C#-RS Driver) - for them we expose
    ///   a dedicated API endpoint using this function, conditionally compiled
    ///   if special compile flag is passed.
    async fn execute_iter_nongeneric(
        &self,
        prepared: PreparedStatement,
        values: SerializedValues,
    ) -> Result<QueryPager, PagerExecutionError> {
        let execution_profile = prepared
            .get_execution_profile_handle()
            .unwrap_or_else(|| self.get_default_execution_profile_handle())
            .access();

        QueryPager::new_for_prepared_statement(
            self,
            PreparedPagerConfig {
                prepared,
                values,
                execution_profile,
                cluster_state: self.cluster.get_state(),
                #[cfg(feature = "metrics")]
                metrics: Arc::clone(&self.metrics),
            },
        )
        .await
    }

    /// Prepares all statements within the batch and returns a new batch where every
    /// statement is prepared.
    ///
    /// # Example
    /// ```rust
    /// # extern crate scylla;
    /// # use scylla::client::session::Session;
    /// # use std::error::Error;
    /// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
    /// use scylla::statement::batch::Batch;
    ///
    /// // Create a batch statement with unprepared statements
    /// let mut batch: Batch = Default::default();
    /// batch.append_statement("INSERT INTO ks.simple_unprepared1 VALUES(?, ?)");
    /// batch.append_statement("INSERT INTO ks.simple_unprepared2 VALUES(?, ?)");
    ///
    /// // Prepare all statements in the batch at once
    /// let prepared_batch: Batch = session.prepare_batch(&batch).await?;
    ///
    /// // Specify bound values to use with each statement
    /// let batch_values = ((1_i32, 2_i32),
    ///                     (3_i32, 4_i32));
    ///
    /// // Run the prepared batch
    /// session.batch(&prepared_batch, batch_values).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn prepare_batch(&self, batch: &Batch) -> Result<Batch, PrepareError> {
        let mut prepared_batch = batch.clone();

        try_join_all(
            prepared_batch
                .statements
                .iter_mut()
                .map(|statement| async move {
                    if let BatchStatement::Query(query) = statement {
                        let prepared = self.prepare_nongeneric(query).await?;
                        *statement = BatchStatement::PreparedStatement(prepared);
                    }
                    Ok::<(), PrepareError>(())
                }),
        )
        .await?;

        Ok(prepared_batch)
    }

    /// Sends `USE <keyspace_name>` request on all connections\
    /// This allows to write `SELECT * FROM table` instead of `SELECT * FROM keyspace.table`\
    ///
    /// Note that even failed `use_keyspace` can change currently used keyspace - the request is sent on all connections and
    /// can overwrite previously used keyspace.
    ///
    /// Call only one `use_keyspace` at a time.\
    /// Trying to do two `use_keyspace` requests simultaneously with different names
    /// can end with some connections using one keyspace and the rest using the other.
    ///
    /// See [the book](https://rust-driver.docs.scylladb.com/stable/statements/usekeyspace.html) for more information
    ///
    /// # Arguments
    ///
    /// * `keyspace_name` - keyspace name to use,
    ///   keyspace names can have up to 48 alphanumeric characters and contain underscores
    /// * `case_sensitive` - if set to true the generated statement will put keyspace name in quotes
    /// # Example
    /// ```rust
    /// # use scylla::client::session::Session;
    /// # use scylla::client::session_builder::SessionBuilder;
    /// # use scylla::client::Compression;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let session = SessionBuilder::new().known_node("127.0.0.1:9042").build().await?;
    /// session
    ///     .query_unpaged("INSERT INTO my_keyspace.tab (a) VALUES ('test1')", &[])
    ///     .await?;
    ///
    /// session.use_keyspace("my_keyspace", false).await?;
    ///
    /// // Now we can omit keyspace name in the statement
    /// session
    ///     .query_unpaged("INSERT INTO tab (a) VALUES ('test2')", &[])
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn use_keyspace(
        &self,
        keyspace_name: impl Into<String>,
        case_sensitive: bool,
    ) -> Result<(), UseKeyspaceError> {
        let keyspace_name = keyspace_name.into();
        self.keyspace_name
            .store(Some(Arc::new(keyspace_name.clone())));

        // Trying to pass keyspace as bound value in "USE ?" doesn't work
        // So we have to create a string for query: "USE " + new_keyspace
        // To avoid any possible CQL injections it's good to verify that the name is valid
        let verified_ks_name = VerifiedKeyspaceName::new(keyspace_name, case_sensitive)?;

        self.cluster.use_keyspace(verified_ks_name).await
    }

    /// Manually trigger a metadata refresh\
    /// The driver will fetch current nodes in the cluster and update its metadata
    ///
    /// Normally this is not needed,
    /// the driver should automatically detect all metadata changes in the cluster
    pub async fn refresh_metadata(&self) -> Result<(), MetadataError> {
        debug!("Session: requested metadata refresh");
        let res = self.cluster.refresh_metadata().await;
        debug!("Session: finished metadata refresh");
        res
    }

    /// Access metrics collected by the driver\
    /// Driver collects various metrics like number of queries or query latencies.
    /// They can be read using this method
    #[cfg(feature = "metrics")]
    pub fn get_metrics(&self) -> Arc<Metrics> {
        Arc::clone(&self.metrics)
    }

    /// Access cluster state visible by the driver.
    ///
    /// Driver collects various information about network topology or schema.
    /// It can be read using this method.
    pub fn get_cluster_state(&self) -> Arc<ClusterState> {
        self.cluster.get_state()
    }

    /// Get [`TracingInfo`] of a traced query performed earlier
    ///
    /// See [the book](https://rust-driver.docs.scylladb.com/stable/tracing/tracing.html)
    /// for more information about query tracing
    pub async fn get_tracing_info(&self, tracing_id: &Uuid) -> Result<TracingInfo, TracingError> {
        // tracing_info_fetch_attempts is NonZeroU32 so at least one attempt will be made
        for _ in 0..self.tracing_info_fetch_attempts.get() {
            let current_try: Option<TracingInfo> =
                self.try_getting_tracing_info(tracing_id).await?;

            match current_try {
                Some(tracing_info) => return Ok(tracing_info),
                None => tokio::time::sleep(self.tracing_info_fetch_interval).await,
            };
        }

        Err(TracingError::EmptyResults)
    }

    /// Gets the name of the keyspace that is currently set, or `None` if no
    /// keyspace was set.
    ///
    /// It will initially return the name of the keyspace that was set
    /// in the session configuration, but calling `use_keyspace` will update
    /// it.
    ///
    /// Note: the return value might be wrong if `use_keyspace` was called
    /// concurrently or it previously failed. It is also unspecified
    /// if `get_keyspace` is called concurrently with `use_keyspace`.
    #[inline]
    pub fn get_keyspace(&self) -> Option<Arc<String>> {
        self.keyspace_name.load_full()
    }

    // Tries getting the tracing info
    // If the queries return 0 rows then returns None - the information didn't reach this node yet
    // If there is some other error returns this error
    async fn try_getting_tracing_info(
        &self,
        tracing_id: &Uuid,
    ) -> Result<Option<TracingInfo>, TracingError> {
        // Get statements for tracing queries.
        // Consistency is set during construction based on session's tracing_info_fetch_consistency.
        let traces_session_stmt = self.get_tracing_session_statement();
        let traces_events_stmt = self.get_tracing_events_statement();

        // Using non-public do_query_unpaged allows us to avoid cloning.
        let (traces_session_res, traces_events_res) = tokio::try_join!(
            self.do_query_unpaged(traces_session_stmt, (tracing_id,)),
            self.do_query_unpaged(traces_events_stmt, (tracing_id,))
        )?;

        // Get tracing info
        let maybe_tracing_info: Option<TracingInfo> = traces_session_res
            .into_rows_result()
            .map_err(TracingError::TracesSessionIntoRowsResultError)?
            .maybe_first_row()
            .map_err(|err| match err {
                MaybeFirstRowError::TypeCheckFailed(e) => {
                    TracingError::TracesSessionInvalidColumnType(e)
                }
                MaybeFirstRowError::DeserializationFailed(e) => {
                    TracingError::TracesSessionDeserializationFailed(e)
                }
            })?;

        let mut tracing_info = match maybe_tracing_info {
            None => return Ok(None),
            Some(tracing_info) => tracing_info,
        };

        // Get tracing events
        let tracing_event_rows_result = traces_events_res
            .into_rows_result()
            .map_err(TracingError::TracesEventsIntoRowsResultError)?;
        let tracing_event_rows = tracing_event_rows_result.rows().map_err(|err| match err {
            RowsError::TypeCheckFailed(err) => TracingError::TracesEventsInvalidColumnType(err),
        })?;

        tracing_info.events = tracing_event_rows
            .collect::<Result<_, _>>()
            .map_err(TracingError::TracesEventsDeserializationFailed)?;

        if tracing_info.events.is_empty() {
            return Ok(None);
        }

        Ok(Some(tracing_info))
    }

    /// This method allows to easily run a request using load balancing, retry policy etc.
    /// Requires some information about the request and a closure.
    /// The closure is used to execute the request once on a chosen connection.
    /// - query will use connection.query()
    /// - execute will use connection.execute()
    ///
    /// If this closure fails with some errors, retry policy is used to perform retries.
    /// On success, this request's result is returned.
    // I tried to make this closures take a reference instead of an Arc but failed
    // maybe once async closures get stabilized this can be fixed
    async fn run_request<'a, QueryFut>(
        &'a self,
        statement_info: RoutingInfo<'a>,
        statement_config: &'a StatementConfig,
        execution_profile: Arc<ExecutionProfileInner>,
        run_request_once: impl Fn(Arc<Connection>, Consistency, &ExecutionProfileInner) -> QueryFut,
        request_span: &'a RequestSpan,
    ) -> Result<(RunRequestResult<NonErrorQueryResponse>, Coordinator), ExecutionError>
    where
        QueryFut: Future<Output = Result<NonErrorQueryResponse, RequestAttemptError>>,
    {
        let history_listener_and_id: Option<(&'a dyn HistoryListener, history::RequestId)> =
            statement_config
                .history_listener
                .as_ref()
                .map(|hl| (&**hl, hl.log_request_start()));

        let load_balancer = statement_config
            .load_balancing_policy
            .as_deref()
            .unwrap_or(execution_profile.load_balancing_policy.as_ref());

        let runner = async {
            let cluster_state = self.cluster.get_state();
            let request_plan =
                load_balancing::Plan::new(load_balancer, &statement_info, &cluster_state);

            // If a speculative execution policy is used to run request, request_plan has to be shared
            // between different async functions. This struct helps to wrap request_plan in mutex so it
            // can be shared safely.
            struct SharedPlan<'a, I>
            where
                I: Iterator<Item = (NodeRef<'a>, Shard)>,
            {
                iter: std::sync::Mutex<I>,
            }

            impl<'a, I> Iterator for &SharedPlan<'a, I>
            where
                I: Iterator<Item = (NodeRef<'a>, Shard)>,
            {
                type Item = (NodeRef<'a>, Shard);

                fn next(&mut self) -> Option<Self::Item> {
                    self.iter.lock().unwrap().next()
                }
            }

            let retry_policy = statement_config
                .retry_policy
                .as_deref()
                .unwrap_or(&*execution_profile.retry_policy);

            let speculative_policy = execution_profile.speculative_execution_policy.as_ref();

            match speculative_policy {
                Some(speculative) if statement_config.is_idempotent => {
                    let shared_request_plan = SharedPlan {
                        iter: std::sync::Mutex::new(request_plan),
                    };

                    let request_runner_generator = |is_speculative: bool| {
                        let history_data: Option<HistoryData> = history_listener_and_id
                            .as_ref()
                            .map(|(history_listener, request_id)| {
                                let speculative_id: Option<history::SpeculativeId> =
                                    if is_speculative {
                                        Some(
                                            history_listener.log_new_speculative_fiber(*request_id),
                                        )
                                    } else {
                                        None
                                    };
                                HistoryData {
                                    listener: *history_listener,
                                    request_id: *request_id,
                                    speculative_id,
                                }
                            });

                        if is_speculative {
                            request_span.inc_speculative_executions();
                        }

                        self.run_request_speculative_fiber(
                            &shared_request_plan,
                            &run_request_once,
                            &execution_profile,
                            ExecuteRequestContext {
                                is_idempotent: statement_config.is_idempotent,
                                consistency_set_on_statement: statement_config.consistency,
                                retry_session: retry_policy.new_session(),
                                history_data,
                                load_balancing_policy: load_balancer,
                                query_info: &statement_info,
                                request_span,
                            },
                        )
                    };

                    let context = speculative_execution::Context {
                        #[cfg(feature = "metrics")]
                        metrics: Arc::clone(&self.metrics),
                    };

                    speculative_execution::execute(
                        speculative.as_ref(),
                        &context,
                        request_runner_generator,
                    )
                    .await
                }
                _ => {
                    let history_data: Option<HistoryData> =
                        history_listener_and_id
                            .as_ref()
                            .map(|(history_listener, request_id)| HistoryData {
                                listener: *history_listener,
                                request_id: *request_id,
                                speculative_id: None,
                            });
                    self.run_request_speculative_fiber(
                        request_plan,
                        &run_request_once,
                        &execution_profile,
                        ExecuteRequestContext {
                            is_idempotent: statement_config.is_idempotent,
                            consistency_set_on_statement: statement_config.consistency,
                            retry_session: retry_policy.new_session(),
                            history_data,
                            load_balancing_policy: load_balancer,
                            query_info: &statement_info,
                            request_span,
                        },
                    )
                    .await
                    .unwrap_or(Err(RequestError::EmptyPlan))
                }
            }
        };

        let effective_timeout = statement_config
            .request_timeout
            .or(execution_profile.request_timeout);
        let result = match effective_timeout {
            Some(timeout) => tokio::time::timeout(timeout, runner).await.unwrap_or_else(
                |_: tokio::time::error::Elapsed| {
                    #[cfg(feature = "metrics")]
                    self.metrics.inc_request_timeouts();

                    let timeout_error = RequestError::RequestTimeout(timeout);
                    trace!(
                        parent: request_span.span(),
                        error = %timeout_error,
                        "Request timed out"
                    );
                    Err(timeout_error)
                },
            ),
            None => runner.await,
        };

        if let Some((history_listener, request_id)) = history_listener_and_id {
            match &result {
                Ok(_) => history_listener.log_request_success(request_id),
                Err(e) => history_listener.log_request_error(request_id, e),
            }
        }

        // Automatically handle meaningful responses.
        if let Ok((RunRequestResult::Completed(ref response), ref coordinator)) = result {
            self.handle_set_keyspace_response(response).await?;
            self.handle_auto_await_schema_agreement(response, coordinator.node().host_id)
                .await?;
        }

        result.map_err(RequestError::into_execution_error)
    }

    /// Executes the closure `run_request_once`, provided the load balancing plan and some information
    /// about the request, including retry session.
    /// If request fails, retry session is used to perform retries.
    ///
    /// Returns None, if provided plan is empty.
    async fn run_request_speculative_fiber<'a, QueryFut>(
        &'a self,
        request_plan: impl Iterator<Item = (NodeRef<'a>, Shard)>,
        run_request_once: impl Fn(Arc<Connection>, Consistency, &ExecutionProfileInner) -> QueryFut,
        execution_profile: &ExecutionProfileInner,
        mut context: ExecuteRequestContext<'a>,
    ) -> Option<Result<(RunRequestResult<NonErrorQueryResponse>, Coordinator), RequestError>>
    where
        QueryFut: Future<Output = Result<NonErrorQueryResponse, RequestAttemptError>>,
    {
        let mut last_error: Option<RequestError> = None;
        let mut current_consistency: Consistency = context
            .consistency_set_on_statement
            .unwrap_or(execution_profile.consistency);

        'nodes_in_plan: for (node, shard) in request_plan {
            let span = trace_span!("Executing request", node = %node.address, shard = %shard);
            'same_node_retries: loop {
                trace!(parent: &span, "Execution started");
                let connection = match node.connection_for_shard(shard).await {
                    Ok(connection) => connection,
                    Err(e) => {
                        trace!(
                            parent: &span,
                            error = %e,
                            "Choosing connection failed"
                        );
                        last_error = Some(e.into());
                        // Broken connection doesn't count as a failed request, don't log in metrics
                        continue 'nodes_in_plan;
                    }
                };
                context.request_span.record_shard_id(&connection);

                #[cfg(feature = "metrics")]
                self.metrics.inc_total_nonpaged_queries();
                let request_start = std::time::Instant::now();

                let connect_address = connection.get_connect_address();
                trace!(
                    parent: &span,
                    connection = %connect_address,
                    "Sending"
                );
                let coordinator =
                    Coordinator::new(node, node.sharder().is_some().then_some(shard), &connection);

                let attempt_id: Option<history::AttemptId> =
                    context.log_attempt_start(connect_address);
                let request_result: Result<NonErrorQueryResponse, RequestAttemptError> =
                    run_request_once(connection, current_consistency, execution_profile)
                        .instrument(span.clone())
                        .await;

                let elapsed = request_start.elapsed();
                let request_error: RequestAttemptError = match request_result {
                    Ok(response) => {
                        trace!(parent: &span, "Request succeeded");
                        #[cfg(feature = "metrics")]
                        let _ = self.metrics.log_query_latency(elapsed.as_millis() as u64);
                        context.log_attempt_success(&attempt_id);
                        context.load_balancing_policy.on_request_success(
                            context.query_info,
                            elapsed,
                            node,
                        );
                        return Some(Ok((RunRequestResult::Completed(response), coordinator)));
                    }
                    Err(e) => {
                        trace!(
                            parent: &span,
                            last_error = %e,
                            "Request failed"
                        );
                        #[cfg(feature = "metrics")]
                        self.metrics.inc_failed_nonpaged_queries();
                        context.load_balancing_policy.on_request_failure(
                            context.query_info,
                            elapsed,
                            node,
                            &e,
                        );
                        e
                    }
                };

                // Use retry policy to decide what to do next
                let request_info = RequestInfo {
                    error: &request_error,
                    is_idempotent: context.is_idempotent,
                    consistency: context
                        .consistency_set_on_statement
                        .unwrap_or(execution_profile.consistency),
                };

                let retry_decision = context.retry_session.decide_should_retry(request_info);
                trace!(
                    parent: &span,
                    retry_decision = ?retry_decision
                );

                context.log_attempt_error(&attempt_id, &request_error, &retry_decision);

                last_error = Some(request_error.into());

                match retry_decision {
                    RetryDecision::RetrySameTarget(new_cl) => {
                        #[cfg(feature = "metrics")]
                        self.metrics.inc_retries_num();
                        current_consistency = new_cl.unwrap_or(current_consistency);
                        continue 'same_node_retries;
                    }
                    RetryDecision::RetryNextTarget(new_cl) => {
                        #[cfg(feature = "metrics")]
                        self.metrics.inc_retries_num();
                        current_consistency = new_cl.unwrap_or(current_consistency);
                        continue 'nodes_in_plan;
                    }
                    RetryDecision::DontRetry => break 'nodes_in_plan,

                    RetryDecision::IgnoreWriteError => {
                        return Some(Ok((RunRequestResult::IgnoredWriteError, coordinator)));
                    }
                };
            }
        }

        last_error.map(Result::Err)
    }

    /// Awaits schema agreement among all reachable nodes.
    ///
    /// Issues an agreement check each `Session::schema_agreement_interval`.
    /// If agreement is not reached in `Session::schema_agreement_timeout`,
    /// `SchemaAgreementError::Timeout` is returned.
    pub async fn await_schema_agreement(&self) -> Result<Uuid, SchemaAgreementError> {
        self.await_schema_agreement_with_required_node(None).await
    }

    /// Decides if an error should result in await_schema_agreement stopping immediately,
    /// or if it's fine to try again (after schema agreement interval).
    /// The errors that should stop immediately are non-transient ones, for which
    /// there is little or no hope that a retry will succeed.
    fn classify_schema_check_error(error: &SchemaAgreementError) -> ControlFlow<()> {
        let classify_attempt_error = |request_attempt_error: &RequestAttemptError| {
            #[deny(clippy::wildcard_enum_match_arm)]
            match request_attempt_error {
                // It may be possible to recover from those errors.
                RequestAttemptError::UnableToAllocStreamId
                | RequestAttemptError::BrokenConnectionError(_)
                | RequestAttemptError::BodyExtensionsParseError(_)
                | RequestAttemptError::CqlResultParseError(_)
                | RequestAttemptError::CqlErrorParseError(_) => ControlFlow::Continue(()),

                // Those errors should not happen, but if they did, something is
                // really wrong. Let's return early.
                RequestAttemptError::SerializationError(_)
                | RequestAttemptError::CqlRequestSerialization(_)
                | RequestAttemptError::UnexpectedResponse(_)
                | RequestAttemptError::RepreparedIdChanged { .. }
                | RequestAttemptError::RepreparedIdMissingInBatch
                | RequestAttemptError::NonfinishedPagingState => ControlFlow::Break(()),

                #[deny(clippy::wildcard_enum_match_arm)]
                RequestAttemptError::DbError(db_error, _) => match db_error {
                    // Those errors should not happen, but if they did, something is
                    // really wrong. Let's return early.
                    DbError::SyntaxError
                    | DbError::Invalid
                    | DbError::AlreadyExists { .. }
                    | DbError::FunctionFailure { .. }
                    | DbError::AuthenticationError
                    | DbError::Unauthorized
                    | DbError::ConfigError
                    | DbError::TruncateError
                    | DbError::ProtocolError => ControlFlow::Break(()),

                    // Those errors likely won't go away on retry
                    DbError::Unavailable { .. }
                    | DbError::ReadFailure { .. }
                    | DbError::WriteFailure { .. }
                    | DbError::ServerError
                    | DbError::Other(_) => ControlFlow::Break(()),

                    DbError::Overloaded
                    | DbError::ReadTimeout { .. }
                    | DbError::WriteTimeout { .. }
                    | DbError::Unprepared { .. }
                    | DbError::RateLimitReached { .. }
                    | DbError::IsBootstrapping
                    | _ => ControlFlow::Continue(()),
                },
            }
        };
        #[deny(clippy::wildcard_enum_match_arm)]
        match error {
            // Unexpected format (type, row count, frame type etc) or deserialization error of response.
            // It should not happen, but if it did it indicates a serious issue.
            SchemaAgreementError::SingleRowError(_)
            | SchemaAgreementError::TracesEventsIntoRowsResultError(_) => ControlFlow::Break(()),

            // Should not be possible - we create this error only after returning here.
            // Let's not panic, but log a warning so that it gets noticed.
            SchemaAgreementError::Timeout(_) => {
                error!("Unexpected schema agreement error type: {}", error);
                ControlFlow::Break(())
            }

            // Definitely a transient error.
            SchemaAgreementError::ConnectionPoolError(_)
            | SchemaAgreementError::RequiredHostAbsent(_) => ControlFlow::Continue(()),

            SchemaAgreementError::RequestError(request_attempt_error) => {
                classify_attempt_error(request_attempt_error)
            }
            SchemaAgreementError::PrepareError(err) => match err {
                PrepareError::ConnectionPoolError(_) => ControlFlow::Continue(()),
                PrepareError::AllAttemptsFailed { first_attempt } => {
                    classify_attempt_error(first_attempt)
                }
                PrepareError::PreparedStatementIdsMismatch => ControlFlow::Break(()),
            },
        }
    }

    /// Awaits schema agreement among all reachable nodes.
    ///
    /// Issues an agreement check each `Session::schema_agreement_interval`.
    /// If agreement is not reached in `Session::schema_agreement_timeout`,
    /// `SchemaAgreementError::Timeout` is returned.
    ///
    /// If `required_node` is Some, only returns Ok if this node successfully
    /// returned its schema version during the agreement process.
    pub(crate) async fn await_schema_agreement_with_required_node(
        &self,
        required_node: Option<Uuid>,
    ) -> Result<Uuid, SchemaAgreementError> {
        // None: no finished attempt recorded
        // Some(Ok(())): Last attempt successful, without agreement
        // Some(Err(_)): Last attempt failed
        let mut last_agreement_failure: Option<Result<(), SchemaAgreementError>> = None;
        // The future passed to timeout returns either Ok(Uuid) if agreement was
        // reached, or Err(SchemaAgreementError) if there was an error that should
        // stop the waiting before timeout.
        let agreement_result = timeout(self.schema_agreement_timeout, async {
            loop {
                let result = self
                    .check_schema_agreement_with_required_node(required_node)
                    .await;
                debug!("Schema agreement check result: {:?}", result);
                match result {
                    Ok(Some(agreed_version)) => return Ok(agreed_version),
                    Ok(None) => last_agreement_failure = Some(Ok(())),
                    Err(err) => {
                        let decision = Self::classify_schema_check_error(&err);
                        match decision {
                            ControlFlow::Continue(_) => {
                                last_agreement_failure = Some(Err(err));
                            }
                            ControlFlow::Break(_) => return Err(err),
                        }
                    }
                }
                tokio::time::sleep(self.schema_agreement_interval).await;
            }
        })
        .await;
        match agreement_result {
            Err(_timeout) => {
                // Timeout occurred. Either all attempts returned possibly-transient errors,
                // or just did not reach agreement in time.
                let effective_error = match last_agreement_failure {
                    // There were no finished attempts - the only error we can return is Timeout.
                    None => SchemaAgreementError::Timeout(self.schema_agreement_timeout),
                    // If the last finished attempt resulted in an error, this error will be more informative than Timeout.
                    Some(Err(err)) => err,
                    // This is the canonical case for timeout - last attempt finished successfully, but without agreement.
                    Some(Ok(())) => SchemaAgreementError::Timeout(self.schema_agreement_timeout),
                };
                Err(effective_error)
            }
            // Agreement encountered a non-transient error, we must return it.
            Ok(Err(inner_error)) => Err(inner_error),
            // Agreement successful
            Ok(Ok(uuid)) => Ok(uuid),
        }
    }

    /// Checks if all reachable nodes have the same schema version.
    ///
    /// If so, returns that agreed upon version.
    pub async fn check_schema_agreement(&self) -> Result<Option<Uuid>, SchemaAgreementError> {
        self.check_schema_agreement_with_required_node(None).await
    }

    /// Checks if all reachable nodes have the same schema version.
    /// If so, returns that agreed upon version.
    ///
    /// If `required_node` is Some, only returns Ok if this node successfully
    /// returned its schema version.
    async fn check_schema_agreement_with_required_node(
        &self,
        required_node: Option<Uuid>,
    ) -> Result<Option<Uuid>, SchemaAgreementError> {
        let cluster_state = self.get_cluster_state();
        // The iterator is guaranteed to be nonempty.
        let per_node_connections = cluster_state.iter_working_connections_per_node()?;

        // Therefore, this iterator is guaranteed to be nonempty, too.
        let handles = per_node_connections.map(|(host_id, pool)| async move {
            (host_id, self.read_node_schema_version(pool).await)
        });
        // Hence, this is nonempty, too.
        let versions_results = join_all(handles).await;

        // Verify that required host is present, and returned success.
        if let Some(required_node) = required_node {
            match versions_results
                .iter()
                .find(|(host_id, _)| *host_id == required_node)
            {
                Some((_, Ok(SchemaNodeResult::Success(_version)))) => (),
                // For other connections we can ignore Broken error, but for required
                // host we need an actual schema version.
                Some((_, Ok(SchemaNodeResult::BrokenConnection(e)))) => {
                    return Err(SchemaAgreementError::RequestError(
                        RequestAttemptError::BrokenConnectionError(e.clone()),
                    ));
                }
                Some((_, Err(e))) => return Err(e.clone()),
                None => return Err(SchemaAgreementError::RequiredHostAbsent(required_node)),
            }
        }

        // Now we no longer need all the errors. We can return if there is
        // irrecoverable one, and collect the Ok values otherwise.
        // TODO(2.0): This expect can be avoided in next major release
        #[expect(clippy::result_large_err)]
        let versions_results: Vec<_> = versions_results
            .into_iter()
            .map(|(_, result)| result)
            .try_collect()?;

        // unwrap is safe because iterator is still not empty.
        let local_version = match versions_results
            .iter()
            .find_or_first(|r| matches!(r, SchemaNodeResult::Success(_)))
            .unwrap()
        {
            SchemaNodeResult::Success(v) => *v,
            SchemaNodeResult::BrokenConnection(err) => {
                // There are only broken connection errors. Nothing better to do
                // than to return an error.
                return Err(SchemaAgreementError::RequestError(
                    RequestAttemptError::BrokenConnectionError(err.clone()),
                ));
            }
        };

        let in_agreement = versions_results
            .into_iter()
            .filter_map(|v_r| match v_r {
                SchemaNodeResult::Success(v) => Some(v),
                SchemaNodeResult::BrokenConnection(_) => None,
            })
            .all(|v| v == local_version);
        Ok(in_agreement.then_some(local_version))
    }

    /// Iterate over connections to the node.
    /// If fetching succeeds on some connection, return first fetched schema version,
    /// as Ok(SchemaNodeResult::Success(...)).
    /// Otherwise it means there are only errors:
    /// - If, and only if, all connections returned ConnectionBrokenError, first such error will be returned,
    ///   as Ok(SchemaNodeResult::BrokenConnection(...)).
    /// - Otherwise there is some other type of error on some connection. First such error will be returned as Err(...).
    ///
    /// `connections_to_node` iterator must be non-empty!
    async fn read_node_schema_version(
        &self,
        connections_to_node: impl Iterator<Item = Arc<Connection>>,
    ) -> Result<SchemaNodeResult, SchemaAgreementError> {
        let mut first_broken_connection_err: Option<BrokenConnectionError> = None;
        let mut first_unignorable_err: Option<SchemaAgreementError> = None;
        for connection in connections_to_node {
            match self.fetch_connection_schema_version(&connection).await {
                Ok(schema_version) => return Ok(SchemaNodeResult::Success(schema_version)),
                Err(SchemaAgreementError::RequestError(
                    RequestAttemptError::BrokenConnectionError(conn_err),
                )) => {
                    if first_broken_connection_err.is_none() {
                        first_broken_connection_err = Some(conn_err);
                    }
                }
                Err(err) => {
                    if first_unignorable_err.is_none() {
                        first_unignorable_err = Some(err);
                    }
                }
            }
        }
        // The iterator was guaranteed to be nonempty, so there must have been at least one error.
        // It means at least one of `first_broken_connection_err` and `first_unrecoverable_err` is Some.
        if let Some(err) = first_unignorable_err {
            return Err(err);
        }

        Ok(SchemaNodeResult::BrokenConnection(
            first_broken_connection_err.unwrap(),
        ))
    }

    /// Fetches the schema version from a single connection.
    async fn fetch_connection_schema_version(
        &self,
        connection: &Connection,
    ) -> Result<Uuid, SchemaAgreementError> {
        let result = connection
            .query_unpaged(self.get_schema_version_statement())
            .await?;

        let (version_id,) = result
            .into_rows_result()
            .map_err(SchemaAgreementError::TracesEventsIntoRowsResultError)?
            .single_row::<(Uuid,)>()
            .map_err(SchemaAgreementError::SingleRowError)?;

        Ok(version_id)
    }

    /// Retrieves the handle to execution profile that is used by this session
    /// by default, i.e. when an executed statement does not define its own handle.
    pub fn get_default_execution_profile_handle(&self) -> &ExecutionProfileHandle {
        &self.default_execution_profile_handle
    }
}

struct ExecuteRequestContext<'a> {
    is_idempotent: bool,
    consistency_set_on_statement: Option<Consistency>,
    retry_session: Box<dyn RetrySession>,
    history_data: Option<HistoryData<'a>>,
    load_balancing_policy: &'a dyn load_balancing::LoadBalancingPolicy,
    query_info: &'a load_balancing::RoutingInfo<'a>,
    request_span: &'a RequestSpan,
}

struct HistoryData<'a> {
    listener: &'a dyn HistoryListener,
    request_id: history::RequestId,
    speculative_id: Option<history::SpeculativeId>,
}

impl ExecuteRequestContext<'_> {
    fn log_attempt_start(&self, node_addr: SocketAddr) -> Option<history::AttemptId> {
        self.history_data.as_ref().map(|hd| {
            hd.listener
                .log_attempt_start(hd.request_id, hd.speculative_id, node_addr)
        })
    }

    fn log_attempt_success(&self, attempt_id_opt: &Option<history::AttemptId>) {
        let attempt_id: &history::AttemptId = match attempt_id_opt {
            Some(id) => id,
            None => return,
        };

        let history_data: &HistoryData = match &self.history_data {
            Some(data) => data,
            None => return,
        };

        history_data.listener.log_attempt_success(*attempt_id);
    }

    fn log_attempt_error(
        &self,
        attempt_id_opt: &Option<history::AttemptId>,
        error: &RequestAttemptError,
        retry_decision: &RetryDecision,
    ) {
        let attempt_id: &history::AttemptId = match attempt_id_opt {
            Some(id) => id,
            None => return,
        };

        let history_data: &HistoryData = match &self.history_data {
            Some(data) => data,
            None => return,
        };

        history_data
            .listener
            .log_attempt_error(*attempt_id, error, retry_decision);
    }
}

#[derive(Debug)]
enum SchemaNodeResult {
    Success(Uuid),
    BrokenConnection(BrokenConnectionError),
}