scylla/transport/session.rs
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
//! `Session` is the main object used in the driver.\
//! It manages all connections to the cluster and allows to perform queries.
use crate::batch::batch_values;
#[cfg(feature = "cloud")]
use crate::cloud::CloudConfig;
use crate::history;
use crate::history::HistoryListener;
use crate::utils::pretty::{CommaSeparatedDisplayer, CqlValueDisplayer};
use arc_swap::ArcSwapOption;
use async_trait::async_trait;
use bytes::Bytes;
use futures::future::join_all;
use futures::future::try_join_all;
use itertools::{Either, Itertools};
pub use scylla_cql::errors::TranslationError;
use scylla_cql::frame::response::result::{deser_cql_value, ColumnSpec, Rows};
use scylla_cql::frame::response::NonErrorResponse;
use scylla_cql::types::serialize::batch::BatchValues;
use scylla_cql::types::serialize::row::SerializeRow;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::fmt::Display;
use std::future::Future;
use std::net::SocketAddr;
use std::num::NonZeroU32;
use std::str::FromStr;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;
use tokio::time::timeout;
use tracing::{debug, trace, trace_span, Instrument};
use uuid::Uuid;
use super::connection::NonErrorQueryResponse;
use super::connection::QueryResponse;
#[cfg(feature = "ssl")]
use super::connection::SslConfig;
use super::errors::{NewSessionError, QueryError};
use super::execution_profile::{ExecutionProfile, ExecutionProfileHandle, ExecutionProfileInner};
#[cfg(feature = "cloud")]
use super::node::CloudEndpoint;
use super::node::KnownNode;
use super::partitioner::PartitionerName;
use super::topology::UntranslatedPeer;
use super::NodeRef;
use crate::cql_to_rust::FromRow;
use crate::frame::response::cql_to_rust::FromRowError;
use crate::frame::response::result;
use crate::prepared_statement::PreparedStatement;
use crate::query::Query;
use crate::routing::Token;
use crate::statement::Consistency;
use crate::tracing::{TracingEvent, TracingInfo};
use crate::transport::cluster::{Cluster, ClusterData, ClusterNeatDebug};
use crate::transport::connection::{Connection, ConnectionConfig, VerifiedKeyspaceName};
use crate::transport::connection_pool::PoolConfig;
use crate::transport::host_filter::HostFilter;
use crate::transport::iterator::{PreparedIteratorConfig, RowIterator};
use crate::transport::load_balancing::{self, RoutingInfo};
use crate::transport::metrics::Metrics;
use crate::transport::node::Node;
use crate::transport::query_result::QueryResult;
use crate::transport::retry_policy::{QueryInfo, RetryDecision, RetrySession};
use crate::transport::speculative_execution;
use crate::transport::Compression;
use crate::{
batch::{Batch, BatchStatement},
statement::StatementConfig,
};
pub use crate::transport::connection_pool::PoolSize;
use crate::authentication::AuthenticatorProvider;
#[cfg(feature = "ssl")]
use openssl::ssl::SslContext;
use scylla_cql::errors::BadQuery;
/// Translates IP addresses received from ScyllaDB nodes into locally reachable addresses.
///
/// The driver auto-detects new ScyllaDB nodes added to the cluster through server side pushed
/// notifications and through checking the system tables. For each node, the address the driver
/// receives corresponds to the address set as `rpc_address` in the node yaml file. In most
/// cases, this is the correct address to use by the driver and that is what is used by default.
/// However, sometimes the addresses received through this mechanism will either not be reachable
/// directly by the driver or should not be the preferred address to use to reach the node (for
/// instance, the `rpc_address` set on ScyllaDB nodes might be a private IP, but some clients
/// may have to use a public IP, or pass by a router, e.g. through NAT, to reach that node).
/// This interface allows to deal with such cases, by allowing to translate an address as sent
/// by a ScyllaDB node to another address to be used by the driver for connection.
///
/// Please note that the "known nodes" addresses provided while creating the [`Session`]
/// instance are not translated, only IP address retrieved from or sent by Cassandra nodes
/// to the driver are.
#[async_trait]
pub trait AddressTranslator: Send + Sync {
async fn translate_address(
&self,
untranslated_peer: &UntranslatedPeer,
) -> Result<SocketAddr, TranslationError>;
}
#[async_trait]
impl AddressTranslator for HashMap<SocketAddr, SocketAddr> {
async fn translate_address(
&self,
untranslated_peer: &UntranslatedPeer,
) -> Result<SocketAddr, TranslationError> {
match self.get(&untranslated_peer.untranslated_address) {
Some(&translated_addr) => Ok(translated_addr),
None => Err(TranslationError::NoRuleForAddress),
}
}
}
#[async_trait]
// Notice: this is inefficient, but what else can we do with such poor representation as str?
// After all, the cluster size is small enough to make this irrelevant.
impl AddressTranslator for HashMap<&'static str, &'static str> {
async fn translate_address(
&self,
untranslated_peer: &UntranslatedPeer,
) -> Result<SocketAddr, TranslationError> {
for (&rule_addr_str, &translated_addr_str) in self.iter() {
if let Ok(rule_addr) = SocketAddr::from_str(rule_addr_str) {
if rule_addr == untranslated_peer.untranslated_address {
return SocketAddr::from_str(translated_addr_str)
.map_err(|_| TranslationError::InvalidAddressInRule);
}
}
}
Err(TranslationError::NoRuleForAddress)
}
}
/// `Session` manages connections to the cluster and allows to perform queries
pub struct Session {
cluster: Cluster,
default_execution_profile_handle: ExecutionProfileHandle,
schema_agreement_interval: Duration,
metrics: Arc<Metrics>,
schema_agreement_timeout: Duration,
schema_agreement_automatic_waiting: bool,
refresh_metadata_on_auto_schema_agreement: bool,
keyspace_name: ArcSwapOption<String>,
tracing_info_fetch_attempts: NonZeroU32,
tracing_info_fetch_interval: Duration,
tracing_info_fetch_consistency: Consistency,
}
/// 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 {
f.debug_struct("Session")
.field("cluster", &ClusterNeatDebug(&self.cluster))
.field(
"default_execution_profile_handle",
&self.default_execution_profile_handle,
)
.field("schema_agreement_interval", &self.schema_agreement_interval)
.field("metrics", &self.metrics)
.field(
"auto_await_schema_agreement_timeout",
&self.schema_agreement_timeout,
)
.finish()
}
}
/// 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>,
/// 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>,
pub tcp_nodelay: bool,
pub tcp_keepalive_interval: Option<Duration>,
pub default_execution_profile_handle: ExecutionProfileHandle,
pub used_keyspace: Option<String>,
pub keyspace_case_sensitive: bool,
/// Provide our Session with TLS
#[cfg(feature = "ssl")]
pub ssl_context: Option<SslContext>,
pub authenticator: Option<Arc<dyn AuthenticatorProvider>>,
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 Scylla 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,
/// 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,
/// 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,
/// 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>>,
/// 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>>,
/// If the driver is to connect to ScyllaCloud, there is a config for it.
#[cfg(feature = "cloud")]
pub cloud_config: Option<Arc<CloudConfig>>,
/// If true, the driver will inject a small delay before flushing data
/// to the socket - by rescheduling the task that writes data to the socket.
/// This gives the task 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,
/// 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,
}
impl SessionConfig {
/// Creates a [`SessionConfig`] with default configuration
/// # Default configuration
/// * Compression: None
/// * Load balancing policy: Token-aware Round-robin
///
/// # Example
/// ```
/// # use scylla::SessionConfig;
/// let config = SessionConfig::new();
/// ```
pub fn new() -> Self {
SessionConfig {
known_nodes: Vec::new(),
compression: None,
tcp_nodelay: true,
tcp_keepalive_interval: 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,
#[cfg(feature = "ssl")]
ssl_context: None,
authenticator: None,
connect_timeout: Duration::from_secs(5),
connection_pool_size: Default::default(),
disallow_shard_aware_port: false,
keyspaces_to_fetch: Vec::new(),
fetch_schema_metadata: true,
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,
refresh_metadata_on_auto_schema_agreement: true,
#[cfg(feature = "cloud")]
cloud_config: None,
enable_write_coalescing: true,
tracing_info_fetch_attempts: NonZeroU32::new(5).unwrap(),
tracing_info_fetch_interval: Duration::from_millis(3),
tracing_info_fetch_consistency: Consistency::One,
cluster_metadata_refresh_interval: Duration::from_secs(60),
}
}
/// Adds a known database server with a hostname.
/// If the port is not explicitly specified, 9042 is used as default
/// # Example
/// ```
/// # use scylla::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::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::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::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()
}
}
/// Trait used to implement `Vec<result::Row>::into_typed<RowT>`
// This is the only way to add custom method to Vec
pub trait IntoTypedRows {
fn into_typed<RowT: FromRow>(self) -> TypedRowIter<RowT>;
}
// Adds method Vec<result::Row>::into_typed<RowT>(self)
// It transforms the Vec into iterator mapping to custom row type
impl IntoTypedRows for Vec<result::Row> {
fn into_typed<RowT: FromRow>(self) -> TypedRowIter<RowT> {
TypedRowIter {
row_iter: self.into_iter(),
phantom_data: Default::default(),
}
}
}
/// Iterator over rows parsed as the given type\
/// Returned by `rows.into_typed::<(...)>()`
pub struct TypedRowIter<RowT: FromRow> {
row_iter: std::vec::IntoIter<result::Row>,
phantom_data: std::marker::PhantomData<RowT>,
}
impl<RowT: FromRow> Iterator for TypedRowIter<RowT> {
type Item = Result<RowT, FromRowError>;
fn next(&mut self) -> Option<Self::Item> {
self.row_iter.next().map(RowT::from_row)
}
}
pub(crate) enum RunQueryResult<ResT> {
IgnoredWriteError,
Completed(ResT),
}
/// Represents a CQL session, which can be used to communicate
/// with the database
impl Session {
/// Establishes a CQL session with the database
///
/// Usually it's easier to use [SessionBuilder](crate::transport::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::{Session, SessionConfig};
/// use scylla::transport::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<Session, NewSessionError> {
let known_nodes = config.known_nodes;
#[cfg(feature = "cloud")]
let known_nodes = if let Some(cloud_servers) =
config.cloud_config.as_ref().map(|cloud_config| {
cloud_config
.get_datacenters()
.iter()
.map(|(dc_name, dc_data)| {
KnownNode::CloudEndpoint(CloudEndpoint {
hostname: dc_data.get_server().to_owned(),
datacenter: dc_name.clone(),
})
})
.collect()
}) {
cloud_servers
} else {
known_nodes
};
// Ensure there is at least one known node
if known_nodes.is_empty() {
return Err(NewSessionError::EmptyKnownNodesList);
}
let connection_config = ConnectionConfig {
compression: config.compression,
tcp_nodelay: config.tcp_nodelay,
tcp_keepalive_interval: config.tcp_keepalive_interval,
#[cfg(feature = "ssl")]
ssl_config: config.ssl_context.map(SslConfig::new_with_global_context),
authenticator: config.authenticator.clone(),
connect_timeout: config.connect_timeout,
event_sender: None,
default_consistency: Default::default(),
address_translator: config.address_translator,
#[cfg(feature = "cloud")]
cloud_config: config.cloud_config,
enable_write_coalescing: config.enable_write_coalescing,
keepalive_interval: config.keepalive_interval,
keepalive_timeout: config.keepalive_timeout,
};
let pool_config = PoolConfig {
connection_config,
pool_size: config.connection_pool_size,
can_use_shard_aware_port: !config.disallow_shard_aware_port,
keepalive_interval: config.keepalive_interval,
};
let cluster = Cluster::new(
known_nodes,
pool_config,
config.keyspaces_to_fetch,
config.fetch_schema_metadata,
config.host_filter,
config.cluster_metadata_refresh_interval,
)
.await?;
let default_execution_profile_handle = config.default_execution_profile_handle;
let session = Session {
cluster,
default_execution_profile_handle,
schema_agreement_interval: config.schema_agreement_interval,
metrics: Arc::new(Metrics::new()),
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: 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,
};
if let Some(keyspace_name) = config.used_keyspace {
session
.use_keyspace(keyspace_name, config.keyspace_case_sensitive)
.await?;
}
Ok(session)
}
/// Sends a query to the database and receives a response.\
/// Returns only a single page of results, to receive multiple pages use [query_iter](Session::query_iter)
///
/// This is the easiest way to make a query, but performance is worse than that of prepared queries.
///
/// It is discouraged to use this method with non-empty values argument (`is_empty()` method from `SerializeRow`
/// trait returns false). In such case, query first needs to be prepared (on a single connection), so
/// driver will perform 2 round trips instead of 1. Please use [`Session::execute()`] instead.
///
/// See [the book](https://rust-driver.docs.scylladb.com/stable/queries/simple.html) for more information
/// # Arguments
/// * `query` - query to perform, can be just a `&str` or the [Query] struct.
/// * `values` - values bound to the query, easiest way is to use a tuple of bound values
///
/// # Examples
/// ```rust
/// # use scylla::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(
/// "INSERT INTO ks.tab (a, b) VALUES(?, ?)",
/// (2_i32, "some text")
/// )
/// .await?;
/// # Ok(())
/// # }
/// ```
/// ```rust
/// # use scylla::Session;
/// # use std::error::Error;
/// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
/// use scylla::IntoTypedRows;
///
/// // Read rows containing an int and text
/// let rows_opt = session
/// .query("SELECT a, b FROM ks.tab", &[])
/// .await?
/// .rows;
///
/// if let Some(rows) = rows_opt {
/// for row in rows.into_typed::<(i32, String)>() {
/// // Parse row as int and text \
/// let (int_val, text_val): (i32, String) = row?;
/// }
/// }
/// # Ok(())
/// # }
/// ```
pub async fn query(
&self,
query: impl Into<Query>,
values: impl SerializeRow,
) -> Result<QueryResult, QueryError> {
self.query_paged(query, values, None).await
}
/// Queries the database with a custom paging state.
///
/// It is discouraged to use this method with non-empty values argument (`is_empty()` method from `SerializeRow`
/// trait returns false). In such case, query first needs to be prepared (on a single connection), so
/// driver will perform 2 round trips instead of 1. Please use [`Session::execute_paged()`] instead.
///
/// # Arguments
///
/// * `query` - query to be performed
/// * `values` - values bound to the query
/// * `paging_state` - previously received paging state or None
pub async fn query_paged(
&self,
query: impl Into<Query>,
values: impl SerializeRow,
paging_state: Option<Bytes>,
) -> Result<QueryResult, QueryError> {
let query: Query = query.into();
let execution_profile = query
.get_execution_profile_handle()
.unwrap_or_else(|| self.get_default_execution_profile_handle())
.access();
let statement_info = RoutingInfo {
consistency: query
.config
.consistency
.unwrap_or(execution_profile.consistency),
serial_consistency: query
.config
.serial_consistency
.unwrap_or(execution_profile.serial_consistency),
..Default::default()
};
let span = RequestSpan::new_query(&query.contents);
let span_ref = &span;
let run_query_result = self
.run_query(
statement_info,
&query.config,
execution_profile,
|node: Arc<Node>| async move { node.random_connection().await },
|connection: Arc<Connection>,
consistency: Consistency,
execution_profile: &ExecutionProfileInner| {
let serial_consistency = query
.config
.serial_consistency
.unwrap_or(execution_profile.serial_consistency);
// Needed to avoid moving query and values into async move block
let query_ref = &query;
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_with_consistency(
query_ref,
consistency,
serial_consistency,
paging_state_ref.clone(),
)
.await
.and_then(QueryResponse::into_non_error_query_response)
} else {
let prepared = connection.prepare(query_ref).await?;
let serialized = prepared.serialize_values(values_ref)?;
span_ref.record_request_size(serialized.buffer_size());
connection
.execute_with_consistency(
&prepared,
&serialized,
consistency,
serial_consistency,
paging_state_ref.clone(),
)
.await
.and_then(QueryResponse::into_non_error_query_response)
}
}
},
&span,
)
.instrument(span.span().clone())
.await?;
let response = match run_query_result {
RunQueryResult::IgnoredWriteError => NonErrorQueryResponse {
response: NonErrorResponse::Result(result::Result::Void),
tracing_id: None,
warnings: Vec::new(),
},
RunQueryResult::Completed(response) => response,
};
self.handle_set_keyspace_response(&response).await?;
self.handle_auto_await_schema_agreement(&response).await?;
let result = response.into_query_result()?;
span.record_result_fields(&result);
Ok(result)
}
async fn handle_set_keyspace_response(
&self,
response: &NonErrorQueryResponse,
) -> Result<(), QueryError> {
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(())
}
async fn handle_auto_await_schema_agreement(
&self,
response: &NonErrorQueryResponse,
) -> Result<(), QueryError> {
if self.schema_agreement_automatic_waiting {
if response.as_schema_change().is_some() {
self.await_schema_agreement().await?;
}
if self.refresh_metadata_on_auto_schema_agreement
&& response.as_schema_change().is_some()
{
self.refresh_metadata().await?;
}
}
Ok(())
}
/// Run a simple query 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 [Query] passed to the function
///
/// It is discouraged to use this method with non-empty values argument (`is_empty()` method from `SerializeRow`
/// trait returns false). In such case, query 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/queries/paged.html) for more information
///
/// # Arguments
/// * `query` - query to perform, can be just a `&str` or the [Query] struct.
/// * `values` - values bound to the query, easiest way is to use a tuple of bound values
///
/// # Example
///
/// ```rust
/// # use scylla::Session;
/// # use std::error::Error;
/// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
/// use scylla::IntoTypedRows;
/// use futures::stream::StreamExt;
///
/// let mut rows_stream = session
/// .query_iter("SELECT a, b FROM ks.t", &[])
/// .await?
/// .into_typed::<(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,
query: impl Into<Query>,
values: impl SerializeRow,
) -> Result<RowIterator, QueryError> {
let query: Query = query.into();
let execution_profile = query
.get_execution_profile_handle()
.unwrap_or_else(|| self.get_default_execution_profile_handle())
.access();
if values.is_empty() {
RowIterator::new_for_query(
query,
execution_profile,
self.cluster.get_data(),
self.metrics.clone(),
)
.await
} else {
// Making RowIterator::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(query).await?;
let values = prepared.serialize_values(&values)?;
RowIterator::new_for_prepared_statement(PreparedIteratorConfig {
prepared,
values,
execution_profile,
cluster_data: self.cluster.get_data(),
metrics: self.metrics.clone(),
})
.await
}
}
/// Prepares a statement on the server side and returns a prepared statement,
/// which can later be used to perform more efficient queries
///
/// Prepared queries are much faster than simple queries:
/// * Database doesn't need to parse the query
/// * 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/queries/prepared.html#performance))
///
/// See [the book](https://rust-driver.docs.scylladb.com/stable/queries/prepared.html) for more information
///
/// # Arguments
/// * `query` - query to prepare, can be just a `&str` or the [Query] struct.
///
/// # Example
/// ```rust
/// # use scylla::Session;
/// # use std::error::Error;
/// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
/// use scylla::prepared_statement::PreparedStatement;
///
/// // Prepare the query for later execution
/// let prepared: PreparedStatement = session
/// .prepare("INSERT INTO ks.tab (a) VALUES(?)")
/// .await?;
///
/// // Run the prepared query with some values, just like a simple query
/// let to_insert: i32 = 12345;
/// session.execute(&prepared, (to_insert,)).await?;
/// # Ok(())
/// # }
/// ```
pub async fn prepare(&self, query: impl Into<Query>) -> Result<PreparedStatement, QueryError> {
let query = query.into();
let query_ref = &query;
let cluster_data = self.get_cluster_data();
let connections_iter = cluster_data.iter_working_connections()?;
// Prepare statements on all connections concurrently
let handles = connections_iter.map(|c| async move { c.prepare(query_ref).await });
let mut results = join_all(handles).await.into_iter();
// If at least one prepare was successful, `prepare()` returns Ok.
// Find the first result that is Ok, or Err if all failed.
// Safety: there is at least one node in the cluster, and `Cluster::iter_working_connections()`
// returns either an error or an iterator with at least one connection, so there will be at least one result.
let first_ok: Result<PreparedStatement, QueryError> =
results.by_ref().find_or_first(Result::is_ok).unwrap();
let mut prepared: PreparedStatement = first_ok?;
// Validate prepared ids equality
for statement in results.flatten() {
if prepared.get_id() != statement.get_id() {
return Err(QueryError::ProtocolError(
"Prepared statement Ids differ, all should be equal",
));
}
// Collect all tracing ids from prepare() queries in the final result
prepared
.prepare_tracing_ids
.extend(statement.prepare_tracing_ids);
}
prepared.set_partitioner_name(
self.extract_partitioner_name(&prepared, &self.cluster.get_data())
.and_then(PartitionerName::from_str)
.unwrap_or_default(),
);
Ok(prepared)
}
fn extract_partitioner_name<'a>(
&self,
prepared: &PreparedStatement,
cluster_data: &'a ClusterData,
) -> Option<&'a str> {
cluster_data
.keyspaces
.get(prepared.get_keyspace_name()?)?
.tables
.get(prepared.get_table_name()?)?
.partitioner
.as_deref()
}
/// Execute a prepared query. Requires a [PreparedStatement]
/// generated using [`Session::prepare`](Session::prepare)\
/// Returns only a single page of results, to receive multiple pages use [execute_iter](Session::execute_iter)
///
/// Prepared queries are much faster than simple queries:
/// * Database doesn't need to parse the query
/// * 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/queries/prepared.html#performance))
///
/// See [the book](https://rust-driver.docs.scylladb.com/stable/queries/prepared.html) for more information
///
/// # Arguments
/// * `prepared` - the prepared statement to execute, generated using [`Session::prepare`](Session::prepare)
/// * `values` - values bound to the query, easiest way is to use a tuple of bound values
///
/// # Example
/// ```rust
/// # use scylla::Session;
/// # use std::error::Error;
/// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
/// use scylla::prepared_statement::PreparedStatement;
///
/// // Prepare the query for later execution
/// let prepared: PreparedStatement = session
/// .prepare("INSERT INTO ks.tab (a) VALUES(?)")
/// .await?;
///
/// // Run the prepared query with some values, just like a simple query
/// let to_insert: i32 = 12345;
/// session.execute(&prepared, (to_insert,)).await?;
/// # Ok(())
/// # }
/// ```
pub async fn execute(
&self,
prepared: &PreparedStatement,
values: impl SerializeRow,
) -> Result<QueryResult, QueryError> {
self.execute_paged(prepared, values, None).await
}
/// Executes a previously prepared statement with previously received paging state
/// # Arguments
///
/// * `prepared` - a statement prepared with [prepare](crate::transport::session::Session::prepare)
/// * `values` - values bound to the query
/// * `paging_state` - paging state from the previous query or None
pub async fn execute_paged(
&self,
prepared: &PreparedStatement,
values: impl SerializeRow,
paging_state: Option<Bytes>,
) -> Result<QueryResult, QueryError> {
let serialized_values = prepared.serialize_values(&values)?;
let values_ref = &serialized_values;
let paging_state_ref = &paging_state;
let (partition_key, token) = prepared
.extract_partition_key_and_calculate_token(prepared.get_partitioner_name(), values_ref)?
.unzip();
let execution_profile = prepared
.get_execution_profile_handle()
.unwrap_or_else(|| self.get_default_execution_profile_handle())
.access();
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,
keyspace: prepared.get_keyspace_name(),
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() {
if let (Some(keyspace), Some(token)) = (statement_info.keyspace.as_ref(), token) {
let cluster_data = self.get_cluster_data();
let replicas: smallvec::SmallVec<[_; 8]> = cluster_data
.get_token_endpoints_iter(keyspace, token)
.collect();
span.record_replicas(&replicas)
}
}
let run_query_result: RunQueryResult<NonErrorQueryResponse> = self
.run_query(
statement_info,
&prepared.config,
execution_profile,
|node: Arc<Node>| async move {
match token {
Some(token) => node.connection_for_token(token).await,
None => node.random_connection().await,
}
},
|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_with_consistency(
prepared,
values_ref,
consistency,
serial_consistency,
paging_state_ref.clone(),
)
.await
.and_then(QueryResponse::into_non_error_query_response)
}
},
&span,
)
.instrument(span.span().clone())
.await?;
let response = match run_query_result {
RunQueryResult::IgnoredWriteError => NonErrorQueryResponse {
response: NonErrorResponse::Result(result::Result::Void),
tracing_id: None,
warnings: Vec::new(),
},
RunQueryResult::Completed(response) => response,
};
self.handle_set_keyspace_response(&response).await?;
self.handle_auto_await_schema_agreement(&response).await?;
let result = response.into_query_result()?;
span.record_result_fields(&result);
Ok(result)
}
/// Run a prepared query 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/queries/paged.html) for more information
///
/// # Arguments
/// * `prepared` - the prepared statement to execute, generated using [`Session::prepare`](Session::prepare)
/// * `values` - values bound to the query, easiest way is to use a tuple of bound values
///
/// # Example
///
/// ```rust
/// # use scylla::Session;
/// # use std::error::Error;
/// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
/// use scylla::prepared_statement::PreparedStatement;
/// use scylla::IntoTypedRows;
/// use futures::stream::StreamExt;
///
/// // Prepare the query for later execution
/// let prepared: PreparedStatement = session
/// .prepare("SELECT a, b FROM ks.t")
/// .await?;
///
/// // Execute the query and receive all pages
/// let mut rows_stream = session
/// .execute_iter(prepared, &[])
/// .await?
/// .into_typed::<(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<RowIterator, QueryError> {
let prepared = prepared.into();
let serialized_values = prepared.serialize_values(&values)?;
let execution_profile = prepared
.get_execution_profile_handle()
.unwrap_or_else(|| self.get_default_execution_profile_handle())
.access();
RowIterator::new_for_prepared_statement(PreparedIteratorConfig {
prepared,
values: serialized_values,
execution_profile,
cluster_data: self.cluster.get_data(),
metrics: self.metrics.clone(),
})
.await
}
/// Perform a batch query\
/// Batch contains many `simple` or `prepared` queries which are executed at once\
/// Batch doesn't return any rows
///
/// Batch values must contain values for each of the queries
///
/// Avoid using non-empty values (`SerializeRow::is_empty()` return false) for simple queries
/// inside the batch. Such queries will first need to be prepared, so the driver will need to
/// send (numer_of_unprepared_queries_with_values + 1) requests instead of 1 request, severly
/// affecting performance.
///
/// See [the book](https://rust-driver.docs.scylladb.com/stable/queries/batch.html) for more information
///
/// # Arguments
/// * `batch` - [Batch] to be performed
/// * `values` - List of values for each query, it's the easiest to use a tuple of tuples
///
/// # Example
/// ```rust
/// # use scylla::Session;
/// # use std::error::Error;
/// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
/// use scylla::batch::Batch;
///
/// let mut batch: Batch = Default::default();
///
/// // A query with two bound values
/// batch.append_statement("INSERT INTO ks.tab(a, b) VALUES(?, ?)");
///
/// // A query with one bound value
/// batch.append_statement("INSERT INTO ks.tab(a, b) VALUES(3, ?)");
///
/// // A query 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 query
/// let batch_values = ((1_i32, 2_i32), // Tuple with two values for the first query
/// (4_i32,), // Tuple with one value for the second query
/// ()); // Empty tuple/unit for the third query
///
/// // Run the batch
/// session.batch(&batch, batch_values).await?;
/// # Ok(())
/// # }
/// ```
pub async fn batch(
&self,
batch: &Batch,
values: impl BatchValues,
) -> Result<QueryResult, QueryError> {
// 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(QueryError::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 keyspace_name = match batch.statements.first() {
Some(BatchStatement::PreparedStatement(ps)) => ps.get_keyspace_name(),
_ => None,
};
let (first_value_token, values) =
batch_values::peek_first_token(values, batch.statements.first())?;
let values_ref = &values;
let statement_info = RoutingInfo {
consistency,
serial_consistency,
token: first_value_token,
keyspace: keyspace_name,
is_confirmed_lwt: false,
};
let span = RequestSpan::new_batch();
let run_query_result = self
.run_query(
statement_info,
&batch.config,
execution_profile,
|node: Arc<Node>| async move {
match first_value_token {
Some(first_value_token) => {
node.connection_for_token(first_value_token).await
}
None => node.random_connection().await,
}
},
|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
}
},
&span,
)
.instrument(span.span().clone())
.await?;
let result = match run_query_result {
RunQueryResult::IgnoredWriteError => QueryResult::default(),
RunQueryResult::Completed(response) => response,
};
span.record_result_fields(&result);
Ok(result)
}
/// Prepares all statements within the batch and returns a new batch where every
/// statement is prepared.
/// /// # Example
/// ```rust
/// # extern crate scylla;
/// # use scylla::Session;
/// # use std::error::Error;
/// # async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
/// use scylla::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 query
/// 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, QueryError> {
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(query.clone()).await?;
*statement = BatchStatement::PreparedStatement(prepared);
}
Ok::<(), QueryError>(())
}),
)
.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/queries/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 query will put keyspace name in quotes
/// # Example
/// ```rust
/// # use scylla::{Session, SessionBuilder};
/// # use scylla::transport::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("INSERT INTO my_keyspace.tab (a) VALUES ('test1')", &[])
/// .await?;
///
/// session.use_keyspace("my_keyspace", false).await?;
///
/// // Now we can omit keyspace name in the query
/// session
/// .query("INSERT INTO tab (a) VALUES ('test2')", &[])
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn use_keyspace(
&self,
keyspace_name: impl Into<String>,
case_sensitive: bool,
) -> Result<(), QueryError> {
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?;
Ok(())
}
/// 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<(), QueryError> {
self.cluster.refresh_metadata().await
}
/// Access metrics collected by the driver\
/// Driver collects various metrics like number of queries or query latencies.
/// They can be read using this method
pub fn get_metrics(&self) -> Arc<Metrics> {
self.metrics.clone()
}
/// Access cluster data collected by the driver\
/// Driver collects various information about network topology or schema.
/// They can be read using this method
pub fn get_cluster_data(&self) -> Arc<ClusterData> {
self.cluster.get_data()
}
/// 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, QueryError> {
// 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, Some(self.tracing_info_fetch_consistency))
.await?;
match current_try {
Some(tracing_info) => return Ok(tracing_info),
None => tokio::time::sleep(self.tracing_info_fetch_interval).await,
};
}
Err(QueryError::ProtocolError(
"All tracing queries returned an empty result, \
maybe the trace information didn't propagate yet. \
Consider configuring Session with \
a longer fetch interval (tracing_info_fetch_interval)",
))
}
/// 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,
consistency: Option<Consistency>,
) -> Result<Option<TracingInfo>, QueryError> {
// Query system_traces.sessions for TracingInfo
let mut traces_session_query = Query::new(crate::tracing::TRACES_SESSION_QUERY_STR);
traces_session_query.config.consistency = consistency;
traces_session_query.set_page_size(1024);
// Query system_traces.events for TracingEvents
let mut traces_events_query = Query::new(crate::tracing::TRACES_EVENTS_QUERY_STR);
traces_events_query.config.consistency = consistency;
traces_events_query.set_page_size(1024);
let (traces_session_res, traces_events_res) = tokio::try_join!(
self.query(traces_session_query, (tracing_id,)),
self.query(traces_events_query, (tracing_id,))
)?;
// Get tracing info
let tracing_info_row_res: Option<Result<TracingInfo, _>> = traces_session_res
.rows
.ok_or(QueryError::ProtocolError(
"Response to system_traces.sessions query was not Rows",
))?
.into_typed::<TracingInfo>()
.next();
let mut tracing_info: TracingInfo = match tracing_info_row_res {
Some(tracing_info_row_res) => tracing_info_row_res.map_err(|_| {
QueryError::ProtocolError(
"Columns from system_traces.session have an unexpected type",
)
})?,
None => return Ok(None),
};
// Get tracing events
let tracing_event_rows = traces_events_res
.rows
.ok_or(QueryError::ProtocolError(
"Response to system_traces.events query was not Rows",
))?
.into_typed::<TracingEvent>();
for event in tracing_event_rows {
let tracing_event: TracingEvent = event.map_err(|_| {
QueryError::ProtocolError(
"Columns from system_traces.events have an unexpected type",
)
})?;
tracing_info.events.push(tracing_event);
}
if tracing_info.events.is_empty() {
return Ok(None);
}
Ok(Some(tracing_info))
}
// This method allows to easily run a query using load balancing, retry policy etc.
// Requires some information about the query and two closures
// First closure is used to choose a connection
// - query will use node.random_connection()
// - execute will use node.connection_for_token()
// The second closure is used to do the query itself on a connection
// - query will use connection.query()
// - execute will use connection.execute()
// If this query closure fails with some errors retry policy is used to perform retries
// On success this query'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_query<'a, ConnFut, QueryFut, ResT>(
&'a self,
statement_info: RoutingInfo<'a>,
statement_config: &'a StatementConfig,
execution_profile: Arc<ExecutionProfileInner>,
choose_connection: impl Fn(Arc<Node>) -> ConnFut,
do_query: impl Fn(Arc<Connection>, Consistency, &ExecutionProfileInner) -> QueryFut,
request_span: &'a RequestSpan,
) -> Result<RunQueryResult<ResT>, QueryError>
where
ConnFut: Future<Output = Result<Arc<Connection>, QueryError>>,
QueryFut: Future<Output = Result<ResT, QueryError>>,
ResT: AllowedRunQueryResTType,
{
let history_listener_and_id: Option<(&'a dyn HistoryListener, history::QueryId)> =
statement_config
.history_listener
.as_ref()
.map(|hl| (&**hl, hl.log_query_start()));
let load_balancer = &execution_profile.load_balancing_policy;
let runner = async {
let cluster_data = self.cluster.get_data();
let query_plan =
load_balancing::Plan::new(load_balancer.as_ref(), &statement_info, &cluster_data);
// If a speculative execution policy is used to run query, query_plan has to be shared
// between different async functions. This struct helps to wrap query_plan in mutex so it
// can be shared safely.
struct SharedPlan<'a, I>
where
I: Iterator<Item = NodeRef<'a>>,
{
iter: std::sync::Mutex<I>,
}
impl<'a, I> Iterator for &SharedPlan<'a, I>
where
I: Iterator<Item = NodeRef<'a>>,
{
type Item = NodeRef<'a>;
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_query_plan = SharedPlan {
iter: std::sync::Mutex::new(query_plan),
};
let execute_query_generator = |is_speculative: bool| {
let history_data: Option<HistoryData> = history_listener_and_id
.as_ref()
.map(|(history_listener, query_id)| {
let speculative_id: Option<history::SpeculativeId> =
if is_speculative {
Some(history_listener.log_new_speculative_fiber(*query_id))
} else {
None
};
HistoryData {
listener: *history_listener,
query_id: *query_id,
speculative_id,
}
});
if is_speculative {
request_span.inc_speculative_executions();
}
self.execute_query(
&shared_query_plan,
&choose_connection,
&do_query,
&execution_profile,
ExecuteQueryContext {
is_idempotent: statement_config.is_idempotent,
consistency_set_on_statement: statement_config.consistency,
retry_session: retry_policy.new_session(),
history_data,
query_info: &statement_info,
request_span,
},
)
};
let context = speculative_execution::Context {
metrics: self.metrics.clone(),
};
speculative_execution::execute(
speculative.as_ref(),
&context,
execute_query_generator,
)
.await
}
_ => {
let history_data: Option<HistoryData> =
history_listener_and_id
.as_ref()
.map(|(history_listener, query_id)| HistoryData {
listener: *history_listener,
query_id: *query_id,
speculative_id: None,
});
self.execute_query(
query_plan,
&choose_connection,
&do_query,
&execution_profile,
ExecuteQueryContext {
is_idempotent: statement_config.is_idempotent,
consistency_set_on_statement: statement_config.consistency,
retry_session: retry_policy.new_session(),
history_data,
query_info: &statement_info,
request_span,
},
)
.await
.unwrap_or(Err(QueryError::ProtocolError(
"Empty query plan - driver bug!",
)))
}
}
};
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(|e| {
Err(QueryError::RequestTimeout(format!(
"Request took longer than {}ms: {}",
timeout.as_millis(),
e
)))
}),
None => runner.await,
};
if let Some((history_listener, query_id)) = history_listener_and_id {
match &result {
Ok(_) => history_listener.log_query_success(query_id),
Err(e) => history_listener.log_query_error(query_id, e),
}
}
result
}
async fn execute_query<'a, ConnFut, QueryFut, ResT>(
&'a self,
query_plan: impl Iterator<Item = NodeRef<'a>>,
choose_connection: impl Fn(Arc<Node>) -> ConnFut,
do_query: impl Fn(Arc<Connection>, Consistency, &ExecutionProfileInner) -> QueryFut,
execution_profile: &ExecutionProfileInner,
mut context: ExecuteQueryContext<'a>,
) -> Option<Result<RunQueryResult<ResT>, QueryError>>
where
ConnFut: Future<Output = Result<Arc<Connection>, QueryError>>,
QueryFut: Future<Output = Result<ResT, QueryError>>,
ResT: AllowedRunQueryResTType,
{
let mut last_error: Option<QueryError> = None;
let mut current_consistency: Consistency = context
.consistency_set_on_statement
.unwrap_or(execution_profile.consistency);
'nodes_in_plan: for node in query_plan {
let span = trace_span!("Executing query", node = %node.address);
'same_node_retries: loop {
trace!(parent: &span, "Execution started");
let connection: Arc<Connection> = match choose_connection(node.clone())
.instrument(span.clone())
.await
{
Ok(connection) => connection,
Err(e) => {
trace!(
parent: &span,
error = %e,
"Choosing connection failed"
);
last_error = Some(e);
// Broken connection doesn't count as a failed query, don't log in metrics
continue 'nodes_in_plan;
}
};
context.request_span.record_shard_id(&connection);
self.metrics.inc_total_nonpaged_queries();
let query_start = std::time::Instant::now();
trace!(
parent: &span,
connection = %connection.get_connect_address(),
"Sending"
);
let attempt_id: Option<history::AttemptId> =
context.log_attempt_start(connection.get_connect_address());
let query_result: Result<ResT, QueryError> =
do_query(connection, current_consistency, execution_profile)
.instrument(span.clone())
.await;
let elapsed = query_start.elapsed();
last_error = match query_result {
Ok(response) => {
trace!(parent: &span, "Query succeeded");
let _ = self.metrics.log_query_latency(elapsed.as_millis() as u64);
context.log_attempt_success(&attempt_id);
execution_profile.load_balancing_policy.on_query_success(
context.query_info,
elapsed,
node,
);
return Some(Ok(RunQueryResult::Completed(response)));
}
Err(e) => {
trace!(
parent: &span,
last_error = %e,
"Query failed"
);
self.metrics.inc_failed_nonpaged_queries();
execution_profile.load_balancing_policy.on_query_failure(
context.query_info,
elapsed,
node,
&e,
);
Some(e)
}
};
let the_error: &QueryError = last_error.as_ref().unwrap();
// Use retry policy to decide what to do next
let query_info = QueryInfo {
error: the_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(query_info);
trace!(
parent: &span,
retry_decision = format!("{:?}", retry_decision).as_str()
);
context.log_attempt_error(&attempt_id, the_error, &retry_decision);
match retry_decision {
RetryDecision::RetrySameNode(new_cl) => {
self.metrics.inc_retries_num();
current_consistency = new_cl.unwrap_or(current_consistency);
continue 'same_node_retries;
}
RetryDecision::RetryNextNode(new_cl) => {
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(RunQueryResult::IgnoredWriteError))
}
};
}
}
last_error.map(Result::Err)
}
async fn await_schema_agreement_indefinitely(&self) -> Result<Uuid, QueryError> {
loop {
tokio::time::sleep(self.schema_agreement_interval).await;
if let Some(agreed_version) = self.check_schema_agreement().await? {
return Ok(agreed_version);
}
}
}
pub async fn await_schema_agreement(&self) -> Result<Uuid, QueryError> {
timeout(
self.schema_agreement_timeout,
self.await_schema_agreement_indefinitely(),
)
.await
.unwrap_or(Err(QueryError::RequestTimeout(
"schema agreement not reached in time".to_owned(),
)))
}
pub async fn check_schema_agreement(&self) -> Result<Option<Uuid>, QueryError> {
let cluster_data = self.get_cluster_data();
let connections_iter = cluster_data.iter_working_connections()?;
let handles = connections_iter.map(|c| async move { c.fetch_schema_version().await });
let versions = try_join_all(handles).await?;
let local_version: Uuid = versions[0];
let in_agreement = versions.into_iter().all(|v| v == local_version);
Ok(in_agreement.then_some(local_version))
}
/// 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
}
}
// run_query, execute_query, etc have a template type called ResT.
// There was a bug where ResT was set to QueryResponse, which could
// be an error response. This was not caught by retry policy which
// assumed all errors would come from analyzing Result<ResT, QueryError>.
// This trait is a guard to make sure that this mistake doesn't
// happen again.
// When using run_query make sure that the ResT type is NOT able
// to contain any errors.
// See https://github.com/scylladb/scylla-rust-driver/issues/501
pub(crate) trait AllowedRunQueryResTType {}
impl AllowedRunQueryResTType for Uuid {}
impl AllowedRunQueryResTType for QueryResult {}
impl AllowedRunQueryResTType for NonErrorQueryResponse {}
struct ExecuteQueryContext<'a> {
is_idempotent: bool,
consistency_set_on_statement: Option<Consistency>,
retry_session: Box<dyn RetrySession>,
history_data: Option<HistoryData<'a>>,
query_info: &'a load_balancing::RoutingInfo<'a>,
request_span: &'a RequestSpan,
}
struct HistoryData<'a> {
listener: &'a dyn HistoryListener,
query_id: history::QueryId,
speculative_id: Option<history::SpeculativeId>,
}
impl<'a> ExecuteQueryContext<'a> {
fn log_attempt_start(&self, node_addr: SocketAddr) -> Option<history::AttemptId> {
self.history_data.as_ref().map(|hd| {
hd.listener
.log_attempt_start(hd.query_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: &QueryError,
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);
}
}
pub(crate) struct RequestSpan {
span: tracing::Span,
speculative_executions: AtomicUsize,
}
impl RequestSpan {
pub(crate) fn new_query(contents: &str) -> Self {
use tracing::field::Empty;
let span = trace_span!(
"Request",
kind = "unprepared",
contents = contents,
//
request_size = Empty,
result_size = Empty,
result_rows = Empty,
replicas = Empty,
shard = Empty,
speculative_executions = Empty,
);
Self {
span,
speculative_executions: 0.into(),
}
}
pub(crate) fn new_prepared<'ps>(
partition_key: Option<impl Iterator<Item = (&'ps [u8], &'ps ColumnSpec)> + Clone>,
token: Option<Token>,
request_size: usize,
) -> Self {
use tracing::field::Empty;
let span = trace_span!(
"Request",
kind = "prepared",
partition_key = Empty,
token = Empty,
//
request_size = request_size,
result_size = Empty,
result_rows = Empty,
replicas = Empty,
shard = Empty,
speculative_executions = Empty,
);
if let Some(partition_key) = partition_key {
span.record(
"partition_key",
tracing::field::display(
format_args!("{}", partition_key_displayer(partition_key),),
),
);
}
if let Some(token) = token {
span.record("token", token.value);
}
Self {
span,
speculative_executions: 0.into(),
}
}
pub(crate) fn new_batch() -> Self {
use tracing::field::Empty;
let span = trace_span!(
"Request",
kind = "batch",
//
request_size = Empty,
result_size = Empty,
result_rows = Empty,
replicas = Empty,
shard = Empty,
speculative_executions = Empty,
);
Self {
span,
speculative_executions: 0.into(),
}
}
pub(crate) fn record_shard_id(&self, conn: &Connection) {
if let Some(info) = conn.get_shard_info() {
self.span.record("shard", info.shard);
}
}
pub(crate) fn record_result_fields(&self, result: &QueryResult) {
self.span.record("result_size", result.serialized_size);
if let Some(rows) = result.rows.as_ref() {
self.span.record("result_rows", rows.len());
}
}
pub(crate) fn record_rows_fields(&self, rows: &Rows) {
self.span.record("result_size", rows.serialized_size);
self.span.record("result_rows", rows.rows.len());
}
pub(crate) fn record_replicas<'a>(&'a self, replicas: &'a [impl Borrow<Arc<Node>>]) {
struct ReplicaIps<'a, N>(&'a [N]);
impl<'a, N> Display for ReplicaIps<'a, N>
where
N: Borrow<Arc<Node>>,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut nodes = self.0.iter();
if let Some(node) = nodes.next() {
write!(f, "{}", node.borrow().address.ip())?;
for node in nodes {
write!(f, ",{}", node.borrow().address.ip())?;
}
}
Ok(())
}
}
self.span
.record("replicas", tracing::field::display(&ReplicaIps(replicas)));
}
pub(crate) fn record_request_size(&self, size: usize) {
self.span.record("request_size", size);
}
pub(crate) fn inc_speculative_executions(&self) {
self.speculative_executions.fetch_add(1, Ordering::Relaxed);
}
pub(crate) fn span(&self) -> &tracing::Span {
&self.span
}
}
impl Drop for RequestSpan {
fn drop(&mut self) {
self.span.record(
"speculative_executions",
self.speculative_executions.load(Ordering::Relaxed),
);
}
}
fn partition_key_displayer<'ps, 'res>(
mut pk_values_iter: impl Iterator<Item = (&'ps [u8], &'ps ColumnSpec)> + 'res + Clone,
) -> impl Display + 'res {
CommaSeparatedDisplayer(
std::iter::from_fn(move || {
pk_values_iter
.next()
.map(|(mut cell, spec)| deser_cql_value(&spec.typ, &mut cell))
})
.map(|c| match c {
Ok(c) => Either::Left(CqlValueDisplayer(c)),
Err(_) => Either::Right("<decoding error>"),
}),
)
}