Skip to main content

mysql_replication/
parser.rs

1use crate::{BinlogFrame, Error, Result, error::invalid, event_type as code, events::*};
2use std::{collections::BTreeMap, sync::Arc};
3
4#[derive(Debug, Clone, Copy)]
5pub struct ParserConfig {
6    pub decode_gtids: bool,
7    pub decode_previous_gtids: bool,
8}
9impl Default for ParserConfig {
10    fn default() -> Self {
11        Self {
12            decode_gtids: true,
13            decode_previous_gtids: true,
14        }
15    }
16}
17/// FDE selects a file's layouts once; row loops never inspect server versions.
18pub struct BinlogParser<M: BinlogMode> {
19    mode: std::marker::PhantomData<M>,
20    config: ParserConfig,
21    headers: Option<[u8; 256]>,
22    tables: BTreeMap<u64, Arc<RowsDecoder<M>>>,
23}
24impl<M: BinlogMode> Default for BinlogParser<M> {
25    fn default() -> Self {
26        Self::new(ParserConfig::default())
27    }
28}
29impl<M: BinlogMode> BinlogParser<M> {
30    pub fn new(config: ParserConfig) -> Self {
31        Self {
32            config,
33            headers: None,
34            tables: BTreeMap::new(),
35            mode: std::marker::PhantomData,
36        }
37    }
38    pub fn parse<'a>(&mut self, frame: BinlogFrame<'a>) -> Result<BinlogEvent<'a, M>> {
39        let kind = frame.header.event_type;
40        if kind == code::PARTIAL_UPDATE_ROWS_EVENT {
41            return Err(Error::Unsupported("partial JSON row images".into()));
42        }
43        if matches!(
44            kind,
45            code::FORMAT_DESCRIPTION_EVENT
46                | code::ROTATE_EVENT
47                | code::QUERY_EVENT
48                | code::XID_EVENT
49        ) {
50            self.tables.clear();
51        }
52        let mut event = if kind == code::FORMAT_DESCRIPTION_EVENT {
53            self.headers = None;
54            let mut format = FormatDescriptionEvent::default();
55            format.decode(frame.body)?;
56            format.checksum_algorithm =
57                match frame.raw_data.get(EventHeader::LEN + frame.body.len()) {
58                    Some(0) => ChecksumAlgorithm::None,
59                    Some(1) => ChecksumAlgorithm::Crc32,
60                    _ => return Err(invalid("missing FDE checksum algorithm")),
61                };
62            let (major, minor, _) = crate::version::parse_server_version(format.server_version)?;
63            if format
64                .server_version
65                .to_ascii_lowercase()
66                .contains("mariadb")
67                || !matches!((major, minor), (5, 7) | (8, 0..=4) | (9, 0 | 7) | (26, 10))
68            {
69                return Err(Error::Unsupported(format!(
70                    "unregistered producer {}",
71                    format.server_version
72                )));
73            }
74            for (kind, length) in [
75                (code::QUERY_EVENT, 13),
76                (code::ROTATE_EVENT, 8),
77                (code::TABLE_MAP_EVENT, 8),
78                (code::WRITE_ROWS_EVENT, 10),
79                (code::UPDATE_ROWS_EVENT, 10),
80                (code::DELETE_ROWS_EVENT, 10),
81            ] {
82                if format.post_header_lengths.get(usize::from(kind) - 1) != Some(&length) {
83                    return Err(Error::Unsupported(format!(
84                        "unvalidated post-header length for event {kind}"
85                    )));
86                }
87            }
88            for kind in [
89                code::WRITE_ROWS_EVENT_V1,
90                code::UPDATE_ROWS_EVENT_V1,
91                code::DELETE_ROWS_EVENT_V1,
92            ] {
93                if !matches!(
94                    format.post_header_lengths.get(usize::from(kind) - 1),
95                    Some(0 | 8)
96                ) {
97                    return Err(Error::Unsupported(format!(
98                        "unvalidated ROWS v1 post-header {kind}"
99                    )));
100                }
101            }
102            let mut headers = [0; 256];
103            if format.post_header_lengths.len() > 255 {
104                return Err(invalid("too many FDE event types"));
105            }
106            headers[1..1 + format.post_header_lengths.len()]
107                .copy_from_slice(format.post_header_lengths);
108            self.headers = Some(headers);
109            EventData::FormatDescription(format)
110        } else {
111            if self.headers.is_none() && kind != code::ROTATE_EVENT {
112                return Err(invalid("event before FORMAT_DESCRIPTION"));
113            }
114            let post_header = self
115                .headers
116                .as_ref()
117                .map_or(8, |h| usize::from(h[usize::from(kind)]));
118            match kind {
119                code::QUERY_EVENT => EventData::Query(QueryEvent::with_post_header(post_header)),
120                code::ROTATE_EVENT => EventData::Rotate(RotateEvent::with_post_header(post_header)),
121                code::TABLE_MAP_EVENT => {
122                    EventData::TableMap(TableMapEvent::with_post_header(post_header))
123                }
124                code::WRITE_ROWS_EVENT_V1
125                | code::UPDATE_ROWS_EVENT_V1
126                | code::DELETE_ROWS_EVENT_V1
127                | code::WRITE_ROWS_EVENT
128                | code::UPDATE_ROWS_EVENT
129                | code::DELETE_ROWS_EVENT => {
130                    if post_header == 0 {
131                        return Err(Error::Unsupported(format!(
132                            "ROWS event {kind} absent from FDE"
133                        )));
134                    }
135                    let operation = match kind {
136                        code::WRITE_ROWS_EVENT_V1 | code::WRITE_ROWS_EVENT => RowsKind::Write,
137                        code::UPDATE_ROWS_EVENT_V1 | code::UPDATE_ROWS_EVENT => RowsKind::Update,
138                        _ => RowsKind::Delete,
139                    };
140                    let v2 = matches!(
141                        kind,
142                        code::WRITE_ROWS_EVENT | code::UPDATE_ROWS_EVENT | code::DELETE_ROWS_EVENT
143                    );
144                    EventData::Rows(RowsEvent::<M>::with_layout(operation, post_header, v2))
145                }
146                code::XID_EVENT => EventData::Xid(Default::default()),
147                code::GTID_EVENT if self.config.decode_gtids => EventData::Gtid(Default::default()),
148                code::ANONYMOUS_GTID_EVENT if self.config.decode_gtids => {
149                    EventData::AnonymousGtid(Default::default())
150                }
151                code::GTID_TAGGED_LOG_EVENT if self.config.decode_gtids => {
152                    EventData::TaggedGtid(Default::default())
153                }
154                code::PREVIOUS_GTIDS_EVENT if self.config.decode_previous_gtids => {
155                    if frame.body.get(7) == Some(&1) {
156                        EventData::TaggedPreviousGtids(Default::default())
157                    } else {
158                        EventData::PreviousGtids(Default::default())
159                    }
160                }
161                code::HEARTBEAT_EVENT => EventData::Heartbeat(Default::default()),
162                _ => EventData::Raw(Default::default()),
163            }
164        };
165        if kind != code::FORMAT_DESCRIPTION_EVENT {
166            event.decode(frame.body)?;
167        }
168        match &mut event {
169            EventData::TableMap(table) => {
170                self.tables.remove(&table.table_id);
171                let decoder = Arc::new(RowsDecoder::<M>::new(table)?);
172                self.tables.insert(table.table_id, decoder);
173            }
174            EventData::Rows(rows)
175                if rows.is_statement_end() && !self.tables.contains_key(&rows.table_id) =>
176            {
177                self.tables.clear();
178                event = EventData::RowsStatementEnd(RowsStatementEndEvent::new({
179                    let mut sentinel =
180                        RowsEvent::<Minimal>::with_layout(rows.kind, rows.post_header, rows.v2);
181                    sentinel.decode(frame.body)?;
182                    sentinel
183                }));
184            }
185            EventData::Rows(rows) => {
186                let decoder = self
187                    .tables
188                    .get(&rows.table_id)
189                    .ok_or_else(|| invalid(format!("missing TABLE_MAP for {}", rows.table_id)))?;
190                if rows.images()?.column_count != decoder.columns().len() {
191                    return Err(invalid("ROWS column count differs from TABLE_MAP"));
192                }
193                rows.decoder = Some(Arc::clone(decoder));
194                if rows.flags & 1 != 0 {
195                    self.tables.clear();
196                }
197            }
198            _ => {}
199        }
200        if let EventData::Gtid(gtid) = &event
201            && gtid.gno == 0
202        {
203            return Err(invalid("GTID GNO must be positive"));
204        }
205        Ok(BinlogEvent {
206            raw_data: frame.raw_data,
207            header: frame.header,
208            event,
209            body: frame.body,
210        })
211    }
212}