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            let mut format = FormatDescriptionEvent::default();
54            format.decode(frame.body)?;
55            format.checksum_algorithm =
56                match frame.raw_data.get(EventHeader::LEN + frame.body.len()) {
57                    Some(0) => ChecksumAlgorithm::None,
58                    Some(1) => ChecksumAlgorithm::Crc32,
59                    _ => return Err(invalid("missing FDE checksum algorithm")),
60                };
61            let (major, minor, _) = crate::version::parse_server_version(format.server_version)?;
62            if format
63                .server_version
64                .to_ascii_lowercase()
65                .contains("mariadb")
66                || !matches!((major, minor), (5, 7) | (8, 0..=4) | (9, 0 | 7))
67            {
68                return Err(Error::Unsupported(format!(
69                    "unregistered producer {}",
70                    format.server_version
71                )));
72            }
73            for (kind, length) in [
74                (code::QUERY_EVENT, 13),
75                (code::ROTATE_EVENT, 8),
76                (code::TABLE_MAP_EVENT, 8),
77                (code::WRITE_ROWS_EVENT, 10),
78                (code::UPDATE_ROWS_EVENT, 10),
79                (code::DELETE_ROWS_EVENT, 10),
80            ] {
81                if format.post_header_lengths.get(usize::from(kind) - 1) != Some(&length) {
82                    return Err(Error::Unsupported(format!(
83                        "unvalidated post-header length for event {kind}"
84                    )));
85                }
86            }
87            for kind in [
88                code::WRITE_ROWS_EVENT_V1,
89                code::UPDATE_ROWS_EVENT_V1,
90                code::DELETE_ROWS_EVENT_V1,
91            ] {
92                if !matches!(
93                    format.post_header_lengths.get(usize::from(kind) - 1),
94                    Some(0 | 8)
95                ) {
96                    return Err(Error::Unsupported(format!(
97                        "unvalidated ROWS v1 post-header {kind}"
98                    )));
99                }
100            }
101            let mut headers = [0; 256];
102            if format.post_header_lengths.len() > 255 {
103                return Err(invalid("too many FDE event types"));
104            }
105            headers[1..1 + format.post_header_lengths.len()]
106                .copy_from_slice(format.post_header_lengths);
107            self.headers = Some(headers);
108            EventData::FormatDescription(format)
109        } else {
110            if self.headers.is_none() && kind != code::ROTATE_EVENT {
111                return Err(invalid("event before FORMAT_DESCRIPTION"));
112            }
113            let post_header = self
114                .headers
115                .as_ref()
116                .map_or(8, |h| usize::from(h[usize::from(kind)]));
117            match kind {
118                code::QUERY_EVENT => EventData::Query(QueryEvent::with_post_header(post_header)),
119                code::ROTATE_EVENT => EventData::Rotate(RotateEvent::with_post_header(post_header)),
120                code::TABLE_MAP_EVENT => {
121                    EventData::TableMap(TableMapEvent::with_post_header(post_header))
122                }
123                code::WRITE_ROWS_EVENT_V1
124                | code::UPDATE_ROWS_EVENT_V1
125                | code::DELETE_ROWS_EVENT_V1
126                | code::WRITE_ROWS_EVENT
127                | code::UPDATE_ROWS_EVENT
128                | code::DELETE_ROWS_EVENT => {
129                    if post_header == 0 {
130                        return Err(Error::Unsupported(format!(
131                            "ROWS event {kind} absent from FDE"
132                        )));
133                    }
134                    let operation = match kind {
135                        code::WRITE_ROWS_EVENT_V1 | code::WRITE_ROWS_EVENT => RowsKind::Write,
136                        code::UPDATE_ROWS_EVENT_V1 | code::UPDATE_ROWS_EVENT => RowsKind::Update,
137                        _ => RowsKind::Delete,
138                    };
139                    let v2 = matches!(
140                        kind,
141                        code::WRITE_ROWS_EVENT | code::UPDATE_ROWS_EVENT | code::DELETE_ROWS_EVENT
142                    );
143                    EventData::Rows(RowsEvent::<M>::with_layout(operation, post_header, v2))
144                }
145                code::XID_EVENT => EventData::Xid(Default::default()),
146                code::GTID_EVENT if self.config.decode_gtids => EventData::Gtid(Default::default()),
147                code::ANONYMOUS_GTID_EVENT if self.config.decode_gtids => {
148                    EventData::AnonymousGtid(Default::default())
149                }
150                code::GTID_TAGGED_LOG_EVENT if self.config.decode_gtids => {
151                    EventData::TaggedGtid(Default::default())
152                }
153                code::PREVIOUS_GTIDS_EVENT if self.config.decode_previous_gtids => {
154                    EventData::PreviousGtids(Default::default())
155                }
156                code::HEARTBEAT_EVENT => EventData::Heartbeat(Default::default()),
157                _ => EventData::Raw(Default::default()),
158            }
159        };
160        if kind != code::FORMAT_DESCRIPTION_EVENT {
161            event.decode(frame.body)?;
162        }
163        match &mut event {
164            EventData::TableMap(table) => {
165                self.tables.remove(&table.table_id);
166                let decoder = Arc::new(RowsDecoder::<M>::new(table)?);
167                self.tables.insert(table.table_id, decoder);
168            }
169            EventData::Rows(rows) if rows.is_statement_end() => {
170                self.tables.clear();
171                event = EventData::RowsStatementEnd(RowsStatementEndEvent::new({
172                    let mut sentinel =
173                        RowsEvent::<Minimal>::with_layout(rows.kind, rows.post_header, rows.v2);
174                    sentinel.decode(frame.body)?;
175                    sentinel
176                }));
177            }
178            EventData::Rows(rows) => {
179                let decoder = self
180                    .tables
181                    .get(&rows.table_id)
182                    .ok_or_else(|| invalid(format!("missing TABLE_MAP for {}", rows.table_id)))?;
183                if rows.images()?.column_count != decoder.columns().len() {
184                    return Err(invalid("ROWS column count differs from TABLE_MAP"));
185                }
186                rows.decoder = Some(Arc::clone(decoder));
187                if rows.flags & 1 != 0 {
188                    self.tables.clear();
189                }
190            }
191            _ => {}
192        }
193        if let EventData::Gtid(gtid) = &event
194            && gtid.gno == 0
195        {
196            return Err(invalid("GTID GNO must be positive"));
197        }
198        Ok(BinlogEvent {
199            raw_data: frame.raw_data,
200            header: frame.header,
201            event,
202            body: frame.body,
203        })
204    }
205}