risingwave_stream/executor/backfill/snapshot_backfill/
state.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
// Copyright 2025 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::mem::replace;
use std::sync::Arc;

use anyhow::anyhow;
use futures::future::try_join_all;
use futures::TryFutureExt;
use risingwave_common::bitmap::Bitmap;
use risingwave_common::hash::{VirtualNode, VnodeBitmapExt};
use risingwave_common::must_match;
use risingwave_common::row::{OwnedRow, Row, RowExt};
use risingwave_common::types::{DataType, ScalarImpl};
use risingwave_common::util::row_serde::OrderedRowSerde;

#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) enum EpochBackfillProgress {
    Consuming { latest_pk: OwnedRow },
    Consumed,
}

#[derive(Debug, Eq, PartialEq)]
pub(super) struct VnodeBackfillProgress {
    pub(super) epoch: u64,
    pub(super) row_count: usize,
    pub(super) progress: EpochBackfillProgress,
}

/// `vnode`, `epoch`, `row_count`, `is_finished`
const EXTRA_COLUMN_TYPES: [DataType; 4] = [
    DataType::Int16,
    DataType::Int64,
    DataType::Int64,
    DataType::Boolean,
];

impl VnodeBackfillProgress {
    fn validate_progress_table_schema(
        progress_table_column_types: &[DataType],
        upstream_pk_column_types: &[DataType],
    ) -> StreamExecutorResult<()> {
        if progress_table_column_types.len()
            != EXTRA_COLUMN_TYPES.len() + upstream_pk_column_types.len()
        {
            return Err(anyhow!(
                "progress table columns len not matched with the len derived from upstream table pk. progress table: {:?}, pk: {:?}",
                progress_table_column_types,
                upstream_pk_column_types)
                .into()
            );
        }
        for (expected_type, progress_table_type) in EXTRA_COLUMN_TYPES
            .iter()
            .chain(upstream_pk_column_types.iter())
            .zip_eq_debug(progress_table_column_types.iter())
        {
            if expected_type != progress_table_type {
                return Err(anyhow!(
                    "progress table column not matched with upstream table schema: progress table: {:?}, pk: {:?}",
                    progress_table_column_types,
                    upstream_pk_column_types)
                    .into()
                );
            }
        }
        Ok(())
    }

    pub(super) fn from_row(row: &OwnedRow, pk_serde: &OrderedRowSerde) -> Self {
        assert_eq!(
            row.len(),
            pk_serde.get_data_types().len() + EXTRA_COLUMN_TYPES.len() - 1, /* Pk of the progress state table (i.e. vnode column) not included */
        );
        let epoch = must_match!(&row[0], Some(ScalarImpl::Int64(epoch)) => {
           *epoch as u64
        });
        let row_count = must_match!(&row[1], Some(ScalarImpl::Int64(row_count)) => {
           *row_count as usize
        });
        let is_finished = must_match!(&row[2], Some(ScalarImpl::Bool(is_finished)) => {
           *is_finished
        });
        Self {
            epoch,
            row_count,
            progress: if !is_finished {
                EpochBackfillProgress::Consuming {
                    latest_pk: row.slice(EXTRA_COLUMN_TYPES.len()..).to_owned_row(),
                }
            } else {
                row.slice(EXTRA_COLUMN_TYPES.len()..)
                    .iter()
                    .enumerate()
                    .for_each(|(i, datum)| {
                        if datum.is_some() {
                            if cfg!(debug_assertions) {
                                panic!("get non-empty pk row: {:?}", row);
                            } else {
                                warn!(
                                    i,
                                    row = ?row,
                                    "get non-empty pk row. will be ignore"
                                );
                            }
                        }
                    });
                EpochBackfillProgress::Consumed
            },
        }
    }

    fn build_row<'a>(
        &'a self,
        vnode: VirtualNode,
        consumed_pk_rows: &'a OwnedRow,
    ) -> impl Row + 'a {
        let (is_finished, pk) = match &self.progress {
            EpochBackfillProgress::Consuming { latest_pk } => {
                assert_eq!(latest_pk.len(), consumed_pk_rows.len());
                (false, latest_pk)
            }
            EpochBackfillProgress::Consumed => (true, consumed_pk_rows),
        };
        [
            Some(ScalarImpl::Int16(vnode.to_scalar())),
            Some(ScalarImpl::Int64(self.epoch as _)),
            Some(ScalarImpl::Int64(self.row_count as _)),
            Some(ScalarImpl::Bool(is_finished)),
        ]
        .chain(pk)
    }
}

#[derive(Debug, Eq, PartialEq)]
enum VnodeBackfillState {
    New(VnodeBackfillProgress),
    Update {
        latest: VnodeBackfillProgress,
        committed: VnodeBackfillProgress,
    },
    Committed(VnodeBackfillProgress),
}

impl VnodeBackfillState {
    fn update_inner(&mut self, latest_progress: VnodeBackfillProgress) {
        let temp_place_holder = Self::temp_placeholder();
        let prev_state = replace(self, temp_place_holder);
        *self = match prev_state {
            VnodeBackfillState::New(_) => VnodeBackfillState::New(latest_progress),
            VnodeBackfillState::Update { committed, .. } => VnodeBackfillState::Update {
                latest: latest_progress,
                committed,
            },
            VnodeBackfillState::Committed(committed) => VnodeBackfillState::Update {
                latest: latest_progress,
                committed,
            },
        };
    }

    fn mark_committed(&mut self) {
        *self = VnodeBackfillState::Committed(match replace(self, Self::temp_placeholder()) {
            VnodeBackfillState::New(progress) => progress,
            VnodeBackfillState::Update { latest, .. } => latest,
            VnodeBackfillState::Committed(progress) => progress,
        });
    }

    fn latest_progress(&self) -> &VnodeBackfillProgress {
        match self {
            VnodeBackfillState::New(progress) => progress,
            VnodeBackfillState::Update { latest, .. } => latest,
            VnodeBackfillState::Committed(progress) => progress,
        }
    }

    fn temp_placeholder() -> Self {
        Self::New(VnodeBackfillProgress {
            epoch: 0,
            row_count: 0,
            progress: EpochBackfillProgress::Consumed,
        })
    }
}

use risingwave_common::util::epoch::EpochPair;
use risingwave_common::util::iter_util::ZipEqDebug;
use risingwave_storage::StateStore;

use crate::common::table::state_table::StateTablePostCommit;
use crate::executor::prelude::StateTable;
use crate::executor::StreamExecutorResult;

pub(super) struct BackfillState<S: StateStore> {
    vnode_state: HashMap<VirtualNode, VnodeBackfillState>,
    pk_serde: OrderedRowSerde,
    consumed_pk_rows: OwnedRow,
    state_table: StateTable<S>,
}

impl<S: StateStore> BackfillState<S> {
    pub(super) async fn new(
        mut state_table: StateTable<S>,
        init_epoch: EpochPair,
        pk_serde: OrderedRowSerde,
    ) -> StreamExecutorResult<Self> {
        VnodeBackfillProgress::validate_progress_table_schema(
            state_table.get_data_types(),
            pk_serde.get_data_types(),
        )?;
        state_table.init_epoch(init_epoch).await?;
        let mut vnode_state = HashMap::new();
        let committed_progress_row = Self::load_vnode_progress_row(&state_table).await?;
        for (vnode, progress_row) in committed_progress_row {
            let Some(progress_row) = progress_row else {
                continue;
            };
            let progress = VnodeBackfillProgress::from_row(&progress_row, &pk_serde);
            assert!(vnode_state
                .insert(vnode, VnodeBackfillState::Committed(progress))
                .is_none());
        }
        let consumed_pk_rows = OwnedRow::new(vec![None; pk_serde.get_data_types().len()]);
        Ok(Self {
            vnode_state,
            pk_serde,
            consumed_pk_rows,
            state_table,
        })
    }

    async fn load_vnode_progress_row(
        state_table: &StateTable<S>,
    ) -> StreamExecutorResult<Vec<(VirtualNode, Option<OwnedRow>)>> {
        let rows = try_join_all(state_table.vnodes().iter_vnodes().map(|vnode| {
            state_table
                .get_row([vnode.to_datum()])
                .map_ok(move |progress_row| (vnode, progress_row))
        }))
        .await?;
        Ok(rows)
    }

    fn update_progress(&mut self, vnode: VirtualNode, progress: VnodeBackfillProgress) {
        match self.vnode_state.entry(vnode) {
            Entry::Occupied(entry) => {
                let state = entry.into_mut();
                let prev_progress = state.latest_progress();
                if prev_progress == &progress {
                    // ignore if no update
                    return;
                }
                // sanity check
                {
                    assert!(
                        prev_progress.epoch <= progress.epoch,
                        "progress epoch regress from {} to {}",
                        prev_progress.epoch,
                        progress.epoch
                    );
                    match &prev_progress.progress {
                        EpochBackfillProgress::Consuming { latest_pk: prev_pk } => {
                            if prev_progress.epoch == progress.epoch
                                && let EpochBackfillProgress::Consuming { latest_pk: pk } =
                                    &progress.progress
                            {
                                assert_eq!(pk.len(), self.pk_serde.get_data_types().len());
                                assert!(prev_progress.row_count <= progress.row_count);
                                if cfg!(debug_assertions) {
                                    let mut prev_buf = vec![];
                                    self.pk_serde.serialize(prev_pk, &mut prev_buf);
                                    let mut buf = vec![];
                                    self.pk_serde.serialize(pk, &mut buf);
                                    assert!(
                                        buf > prev_buf,
                                        "new pk progress: {:?} not exceed prev pk progress: {:?}",
                                        pk,
                                        prev_pk
                                    );
                                }
                            }
                        }
                        EpochBackfillProgress::Consumed => {
                            assert!(
                                prev_progress.epoch < progress.epoch,
                                "{:?} {:?}",
                                prev_progress,
                                progress
                            );
                        }
                    }
                }
                state.update_inner(progress);
            }
            Entry::Vacant(entry) => {
                entry.insert(VnodeBackfillState::New(progress));
            }
        }
    }

    pub(super) fn update_epoch_progress(
        &mut self,
        vnode: VirtualNode,
        epoch: u64,
        row_count: usize,
        pk: OwnedRow,
    ) {
        self.update_progress(
            vnode,
            VnodeBackfillProgress {
                epoch,
                row_count,
                progress: EpochBackfillProgress::Consuming { latest_pk: pk },
            },
        )
    }

    pub(super) fn finish_epoch(&mut self, vnode: VirtualNode, epoch: u64, row_count: usize) {
        self.update_progress(
            vnode,
            VnodeBackfillProgress {
                epoch,
                row_count,
                progress: EpochBackfillProgress::Consumed,
            },
        );
    }

    pub(super) fn latest_progress(
        &self,
    ) -> impl Iterator<Item = (VirtualNode, Option<&VnodeBackfillProgress>)> {
        self.state_table.vnodes().iter_vnodes().map(|vnode| {
            (
                vnode,
                self.vnode_state
                    .get(&vnode)
                    .map(VnodeBackfillState::latest_progress),
            )
        })
    }

    pub(super) async fn commit(
        &mut self,
        barrier_epoch: EpochPair,
    ) -> StreamExecutorResult<BackfillStatePostCommit<'_, S>> {
        for (vnode, state) in &self.vnode_state {
            match state {
                VnodeBackfillState::New(progress) => {
                    self.state_table
                        .insert(progress.build_row(*vnode, &self.consumed_pk_rows));
                }
                VnodeBackfillState::Update { latest, committed } => {
                    self.state_table.update(
                        committed.build_row(*vnode, &self.consumed_pk_rows),
                        latest.build_row(*vnode, &self.consumed_pk_rows),
                    );
                }
                VnodeBackfillState::Committed(_) => {}
            }
        }
        let post_commit = self.state_table.commit(barrier_epoch).await?;
        self.vnode_state
            .values_mut()
            .for_each(VnodeBackfillState::mark_committed);
        Ok(BackfillStatePostCommit {
            inner: post_commit,
            vnode_state: &mut self.vnode_state,
            pk_serde: &self.pk_serde,
        })
    }
}

#[must_use]
pub(super) struct BackfillStatePostCommit<'a, S: StateStore> {
    inner: StateTablePostCommit<'a, S>,
    vnode_state: &'a mut HashMap<VirtualNode, VnodeBackfillState>,
    pk_serde: &'a OrderedRowSerde,
}

impl<S: StateStore> BackfillStatePostCommit<'_, S> {
    pub(super) async fn post_yield_barrier(
        self,
        new_vnode_bitmap: Option<Arc<Bitmap>>,
    ) -> StreamExecutorResult<Option<Arc<Bitmap>>> {
        let new_vnode_bitmap = if let Some(((new_vnode_bitmap, prev_vnode_bitmap, state), _)) =
            self.inner.post_yield_barrier(new_vnode_bitmap).await?
        {
            Self::update_vnode_bitmap(&*state, self.vnode_state, self.pk_serde, prev_vnode_bitmap)
                .await?;
            Some(new_vnode_bitmap)
        } else {
            None
        };
        Ok(new_vnode_bitmap)
    }

    async fn update_vnode_bitmap(
        state_table: &StateTable<S>,
        vnode_state: &mut HashMap<VirtualNode, VnodeBackfillState>,
        pk_serde: &OrderedRowSerde,
        prev_vnode_bitmap: Arc<Bitmap>,
    ) -> StreamExecutorResult<()> {
        let committed_progress_rows = BackfillState::load_vnode_progress_row(state_table).await?;
        let mut new_state = HashMap::new();
        for (vnode, progress_row) in committed_progress_rows {
            if let Some(progress_row) = progress_row {
                let progress = VnodeBackfillProgress::from_row(&progress_row, pk_serde);
                assert!(new_state
                    .insert(vnode, VnodeBackfillState::Committed(progress))
                    .is_none());
            }

            if prev_vnode_bitmap.is_set(vnode.to_index()) {
                // if the vnode exist previously, the new state should be the same as the previous one
                assert_eq!(vnode_state.get(&vnode), new_state.get(&vnode));
            }
        }
        *vnode_state = new_state;
        Ok(())
    }
}