risingwave_meta/stream/
test_scale.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
// Copyright 2024 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.

#[cfg(test)]
mod tests {
    use std::collections::{BTreeSet, HashMap};

    use itertools::Itertools;
    use maplit::btreeset;
    use risingwave_common::bitmap::Bitmap;
    use risingwave_common::hash::{ActorMapping, VirtualNode};

    use crate::model::ActorId;
    use crate::stream::scale::rebalance_actor_vnode;
    use crate::stream::CustomActorInfo;

    fn simulated_parallelism(min: Option<usize>, max: Option<usize>) -> Vec<usize> {
        let mut raw = vec![1, 3, 12, 42, VirtualNode::COUNT_FOR_TEST];
        if let Some(min) = min {
            raw.retain(|n| *n > min);
            raw.push(min);
        }
        if let Some(max) = max {
            raw.retain(|n| *n < max);
            raw.push(max);
        }
        raw
    }

    fn build_fake_actors(actor_ids: Vec<ActorId>) -> Vec<CustomActorInfo> {
        let actor_bitmaps =
            ActorMapping::new_uniform(actor_ids.clone().into_iter(), VirtualNode::COUNT_FOR_TEST)
                .to_bitmaps();
        actor_ids
            .iter()
            .map(|actor_id| CustomActorInfo {
                actor_id: *actor_id,
                vnode_bitmap: actor_bitmaps.get(actor_id).cloned(),
                ..Default::default()
            })
            .collect()
    }

    fn check_affinity_for_scale_in(bitmap: &Bitmap, actor: &CustomActorInfo) {
        let prev_bitmap = actor.vnode_bitmap.as_ref().unwrap();

        for idx in 0..VirtualNode::COUNT_FOR_TEST {
            if prev_bitmap.is_set(idx) {
                assert!(bitmap.is_set(idx));
            }
        }
    }

    fn check_bitmaps<T>(bitmaps: &HashMap<T, Bitmap>) {
        let mut target = (0..VirtualNode::COUNT_FOR_TEST)
            .map(|_| false)
            .collect_vec();

        for bitmap in bitmaps.values() {
            for (idx, pos) in target.iter_mut().enumerate() {
                if bitmap.is_set(idx) {
                    // *pos should be false
                    assert!(!*pos);
                    *pos = true;
                }
            }
        }

        for (idx, b) in target.iter().enumerate() {
            assert!(*b, "vnode {} should be set", idx);
        }

        let vnodes = bitmaps.values().map(|bitmap| bitmap.count_ones());
        let (min, max) = vnodes.minmax().into_option().unwrap();

        assert!((max - min) <= 1, "min {} max {}", min, max);
    }

    #[test]
    fn test_build_actor_mapping() {
        for parallelism in simulated_parallelism(None, None) {
            let actor_ids = (0..parallelism as ActorId).collect_vec();
            let actor_mapping =
                ActorMapping::new_uniform(actor_ids.into_iter(), VirtualNode::COUNT_FOR_TEST);

            assert_eq!(actor_mapping.len(), VirtualNode::COUNT_FOR_TEST);

            let mut check: HashMap<u32, Vec<_>> = HashMap::new();
            for (vnode, actor_id) in actor_mapping.iter_with_vnode() {
                check.entry(actor_id).or_default().push(vnode);
            }

            assert_eq!(check.len(), parallelism);

            let (min, max) = check
                .values()
                .map(|indexes| indexes.len())
                .minmax()
                .into_option()
                .unwrap();

            assert!(max - min <= 1);
        }
    }

    fn generate_actor_mapping(parallelism: usize) -> (ActorMapping, HashMap<ActorId, Bitmap>) {
        let actor_ids = (0..parallelism).map(|i| i as ActorId).collect_vec();
        let actors = build_fake_actors(actor_ids);

        let bitmaps: HashMap<_, _> = actors
            .into_iter()
            .map(|actor| {
                (
                    actor.actor_id as ActorId,
                    actor.vnode_bitmap.unwrap().clone(),
                )
            })
            .collect();

        (ActorMapping::from_bitmaps(&bitmaps), bitmaps)
    }

    #[test]
    fn test_actor_mapping_from_bitmaps() {
        for parallelism in simulated_parallelism(None, None) {
            let (actor_mapping, bitmaps) = generate_actor_mapping(parallelism);
            check_bitmaps(&bitmaps);

            for (actor_id, bitmap) in &bitmaps {
                for (vnode, value) in actor_mapping.iter_with_vnode() {
                    if bitmap.is_set(vnode.to_index()) {
                        assert_eq!(value, *actor_id);
                    }
                }
            }
        }
    }

    #[test]
    fn test_rebalance_empty() {
        let actors = build_fake_actors((0..3 as ActorId).collect_vec());

        // empty input
        let result = rebalance_actor_vnode(&actors, &BTreeSet::new(), &BTreeSet::new());
        assert_eq!(result.len(), actors.len());
    }

    #[test]
    fn test_rebalance_scale_in() {
        for parallelism in simulated_parallelism(Some(3), None) {
            let actors = build_fake_actors((0..parallelism as ActorId).collect_vec());

            // remove 1
            let actors_to_remove = btreeset! {0};
            let result = rebalance_actor_vnode(&actors, &actors_to_remove, &BTreeSet::new());
            assert_eq!(result.len(), actors.len() - actors_to_remove.len());
            check_bitmaps(&result);
            check_affinity_for_scale_in(result.get(&(1 as ActorId)).unwrap(), &actors[1]);

            // remove n-1
            let actors_to_remove = (1..parallelism as ActorId).collect();
            let result = rebalance_actor_vnode(&actors, &actors_to_remove, &BTreeSet::new());
            assert_eq!(result.len(), 1);
            check_bitmaps(&result);

            let (_, bitmap) = result.iter().exactly_one().unwrap();
            assert!(bitmap.all());
        }
    }

    #[test]
    fn test_rebalance_scale_out() {
        for parallelism in simulated_parallelism(Some(3), Some(VirtualNode::COUNT_FOR_TEST - 1)) {
            let actors = build_fake_actors((0..parallelism as ActorId).collect_vec());

            // add 1
            let actors_to_add = btreeset! {parallelism as ActorId};
            let result = rebalance_actor_vnode(&actors, &BTreeSet::new(), &actors_to_add);
            assert_eq!(result.len(), actors.len() + actors_to_add.len());
            check_bitmaps(&result);

            let actors = build_fake_actors((0..parallelism as ActorId).collect_vec());

            // add to VirtualNode::COUNT_FOR_TEST
            let actors_to_add =
                (parallelism as ActorId..VirtualNode::COUNT_FOR_TEST as ActorId).collect();
            let result = rebalance_actor_vnode(&actors, &BTreeSet::new(), &actors_to_add);
            assert_eq!(result.len(), actors.len() + actors_to_add.len());
            check_bitmaps(&result);
        }
    }

    #[test]
    fn test_rebalance_migration() {
        for parallelism in simulated_parallelism(Some(3), None) {
            let actors = build_fake_actors((0..parallelism as ActorId).collect_vec());

            for idx in 0..parallelism {
                let actors_to_remove = btreeset! {idx as ActorId};
                let actors_to_add = btreeset! {parallelism as ActorId};
                let result = rebalance_actor_vnode(&actors, &actors_to_remove, &actors_to_add);

                assert_eq!(
                    result.len(),
                    actors.len() - actors_to_remove.len() + actors_to_add.len()
                );

                check_bitmaps(&result);

                for actor in &actors {
                    if actor.actor_id == idx as ActorId {
                        continue;
                    }

                    let target_bitmap = result.get(&actor.actor_id).unwrap();
                    let prev_bitmap = actor.vnode_bitmap.as_ref().unwrap();
                    assert!(prev_bitmap.eq(target_bitmap));
                }
            }
            let actors = build_fake_actors((0..parallelism as ActorId).collect_vec());

            for migration_count in 1..parallelism {
                let actors_to_remove = (0..migration_count as ActorId).collect();
                let actors_to_add =
                    (parallelism as ActorId..(parallelism + migration_count) as ActorId).collect();
                let result = rebalance_actor_vnode(&actors, &actors_to_remove, &actors_to_add);

                assert_eq!(
                    result.len(),
                    actors.len() - actors_to_remove.len() + actors_to_add.len()
                );

                check_bitmaps(&result);
            }
        }
    }

    #[test]
    fn test_rebalance_scale() {
        for parallelism in simulated_parallelism(Some(3), None) {
            let actor_ids = (0..parallelism as ActorId).collect_vec();
            let actors = build_fake_actors(actor_ids);

            let parallelism = parallelism as ActorId;
            let actors_to_remove = btreeset! {0};
            let actors_to_add = btreeset! {parallelism, parallelism+1};
            let result = rebalance_actor_vnode(&actors, &actors_to_remove, &actors_to_add);

            assert_eq!(
                result.len(),
                actors.len() - actors_to_remove.len() + actors_to_add.len()
            );
            check_bitmaps(&result);

            let actors_to_remove = btreeset! {0, 1};
            let actors_to_add = btreeset! {parallelism};
            let result = rebalance_actor_vnode(&actors, &actors_to_remove, &actors_to_add);

            assert_eq!(
                result.len(),
                actors.len() - actors_to_remove.len() + actors_to_add.len()
            );
            check_bitmaps(&result);

            check_affinity_for_scale_in(result.get(&(2 as ActorId)).unwrap(), &actors[2]);
        }
    }

    #[test]
    fn test_rebalance_scale_real() {
        let actor_ids = (0..(VirtualNode::COUNT_FOR_TEST - 1) as ActorId).collect_vec();
        let actors = build_fake_actors(actor_ids);
        let actors_to_remove = btreeset! {0, 1};
        let actors_to_add = btreeset! {255};
        let result = rebalance_actor_vnode(&actors, &actors_to_remove, &actors_to_add);

        check_bitmaps(&result);
    }
}