Skip to main content

meta_srv/procedure/repartition/
repartition_start.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::any::Any;
16
17use common_meta::key::table_route::PhysicalTableRouteValue;
18use common_procedure::{Context as ProcedureContext, Status};
19use common_telemetry::debug;
20use partition::collider::Collider;
21use partition::expr::PartitionExpr;
22use partition::subtask::{self, RepartitionSubtask};
23use serde::{Deserialize, Deserializer, Serialize};
24use snafu::{OptionExt, ResultExt, ensure};
25use tokio::time::Instant;
26use uuid::Uuid;
27
28use crate::error::{self, Result};
29use crate::procedure::repartition::allocate_region::AllocateRegion;
30use crate::procedure::repartition::plan::{AllocationPlanEntry, SourceRegionDescriptor};
31use crate::procedure::repartition::repartition_end::RepartitionEnd;
32use crate::procedure::repartition::update_partition_metadata::{
33    PartitionMetadataUpdate, UpdatePartitionMetadata,
34};
35use crate::procedure::repartition::{Context, State};
36
37#[derive(Debug, Clone, Serialize)]
38pub enum RepartitionFrom {
39    Partitioned {
40        exprs: Vec<PartitionExpr>,
41        /// Full target partition columns to overwrite table metadata.
42        ///
43        /// `None` means the repartition keeps using the current table
44        /// partition columns, so the procedure won't update
45        /// `partition_key_indices`.
46        target_partition_columns: Option<Vec<String>>,
47    },
48    Unpartitioned {
49        partition_columns: Vec<String>,
50    },
51}
52
53impl<'de> Deserialize<'de> for RepartitionFrom {
54    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
55    where
56        D: Deserializer<'de>,
57    {
58        #[derive(Deserialize)]
59        enum CurrentRepartitionFrom {
60            Partitioned {
61                exprs: Vec<PartitionExpr>,
62                #[serde(default)]
63                target_partition_columns: Option<Vec<String>>,
64            },
65            Unpartitioned {
66                partition_columns: Vec<String>,
67            },
68        }
69
70        #[derive(Deserialize)]
71        #[serde(untagged)]
72        enum RepartitionFromRepr {
73            Current(CurrentRepartitionFrom),
74            Legacy(Vec<PartitionExpr>),
75        }
76
77        match RepartitionFromRepr::deserialize(deserializer)? {
78            RepartitionFromRepr::Current(CurrentRepartitionFrom::Partitioned {
79                exprs,
80                target_partition_columns,
81            }) => Ok(Self::Partitioned {
82                exprs,
83                target_partition_columns,
84            }),
85            RepartitionFromRepr::Current(CurrentRepartitionFrom::Unpartitioned {
86                partition_columns,
87            }) => Ok(Self::Unpartitioned { partition_columns }),
88            RepartitionFromRepr::Legacy(exprs) => Ok(Self::Partitioned {
89                exprs,
90                target_partition_columns: None,
91            }),
92        }
93    }
94}
95
96#[derive(Debug, Clone, Serialize, Deserialize)]
97pub struct RepartitionStart {
98    #[serde(alias = "from_exprs")]
99    from: RepartitionFrom,
100    to_exprs: Vec<PartitionExpr>,
101}
102
103impl RepartitionStart {
104    pub fn new(from: RepartitionFrom, to_exprs: Vec<PartitionExpr>) -> Self {
105        Self { from, to_exprs }
106    }
107
108    pub(crate) fn submitted_intent(&self) -> RepartitionSubmittedIntent<'_> {
109        RepartitionSubmittedIntent {
110            from: &self.from,
111            target_partition_exprs: &self.to_exprs,
112        }
113    }
114}
115
116pub(crate) struct RepartitionSubmittedIntent<'a> {
117    from: &'a RepartitionFrom,
118    target_partition_exprs: &'a [PartitionExpr],
119}
120
121impl RepartitionSubmittedIntent<'_> {
122    pub(crate) fn source_type(&self) -> &'static str {
123        match self.from {
124            RepartitionFrom::Partitioned { .. } => "partitioned",
125            RepartitionFrom::Unpartitioned { .. } => "unpartitioned",
126        }
127    }
128
129    pub(crate) fn source_partition_exprs(&self) -> &[PartitionExpr] {
130        match self.from {
131            RepartitionFrom::Partitioned { exprs, .. } => exprs,
132            RepartitionFrom::Unpartitioned { .. } => &[],
133        }
134    }
135
136    pub(crate) fn target_partition_exprs(&self) -> &[PartitionExpr] {
137        self.target_partition_exprs
138    }
139
140    pub(crate) fn target_partition_columns(&self) -> Option<&[String]> {
141        match self.from {
142            RepartitionFrom::Partitioned {
143                target_partition_columns,
144                ..
145            } => target_partition_columns.as_deref(),
146            RepartitionFrom::Unpartitioned { partition_columns } => Some(partition_columns),
147        }
148    }
149}
150
151#[async_trait::async_trait]
152#[typetag::serde]
153impl State for RepartitionStart {
154    async fn next(
155        &mut self,
156        ctx: &mut Context,
157        _: &ProcedureContext,
158    ) -> Result<(Box<dyn State>, Status)> {
159        ensure!(
160            !self.to_exprs.is_empty(),
161            error::InvalidArgumentsSnafu {
162                err_msg: "Repartition expects non-empty target partition expressions".to_string(),
163            }
164        );
165
166        let timer = Instant::now();
167        let (physical_table_id, table_route) = ctx
168            .table_metadata_manager
169            .table_route_manager()
170            .get_physical_table_route(ctx.persistent_ctx.table_id)
171            .await
172            .context(error::TableMetadataManagerSnafu)?;
173        let table_id = ctx.persistent_ctx.table_id;
174        ensure!(
175            physical_table_id == table_id,
176            error::UnexpectedSnafu {
177                violated: format!(
178                    "Repartition only works on the physical table, but got logical table: {}, physical table id: {}",
179                    table_id, physical_table_id
180                ),
181            }
182        );
183
184        let from_exprs = self.prepare_from(ctx).await?;
185        let plans = Self::build_plan(&table_route, from_exprs, &self.to_exprs)?;
186        let plan_count = plans.len();
187        let total_source_regions: usize = plans.iter().map(|p| p.source_regions.len()).sum();
188        let total_target_regions: usize =
189            plans.iter().map(|p| p.target_partition_exprs.len()).sum();
190        common_telemetry::info!(
191            "Repartition start, table_id: {}, plans: {}, total_source_regions: {}, total_target_regions: {}",
192            table_id,
193            plan_count,
194            total_source_regions,
195            total_target_regions
196        );
197
198        ctx.update_build_plan_elapsed(timer.elapsed());
199
200        if plans.is_empty() {
201            return Ok((Box::new(RepartitionEnd), Status::done()));
202        }
203        Self::ensure_no_pending_tails(ctx, &plans)?;
204
205        if ctx.persistent_ctx.partition_metadata_update.is_some() {
206            Ok((
207                Box::new(UpdatePartitionMetadata::new(plans)),
208                Status::executing(true),
209            ))
210        } else {
211            Ok((
212                Box::new(AllocateRegion::new(plans)),
213                Status::executing(false),
214            ))
215        }
216    }
217
218    fn as_any(&self) -> &dyn Any {
219        self
220    }
221}
222
223impl RepartitionStart {
224    /// Fails with a retryable error before any region is allocated or rerouted if a source
225    /// region of `plans` still had pending WAL tails when it last reported as leader, since
226    /// entering staging would then fail after the route change. Targets are the sources or new
227    /// regions, so only sources are checked. A source without a leader report to this metasrv
228    /// passes, and entering staging still refuses its pending tails. Registry entries are not
229    /// expired, so after a datanode restart the check uses the flag of the last report until
230    /// the reopened region reports as leader again.
231    fn ensure_no_pending_tails(ctx: &Context, plans: &[AllocationPlanEntry]) -> Result<()> {
232        let region_ids = plans
233            .iter()
234            .flat_map(|plan| plan.source_regions.iter().map(|source| source.region_id()));
235        let mut pending = ctx
236            .leader_region_registry
237            .batch_get(region_ids)
238            .into_iter()
239            .filter(|(_, leader)| leader.has_pending_tails)
240            .map(|(region_id, _)| region_id)
241            .collect::<Vec<_>>();
242        pending.sort_unstable();
243        ensure!(
244            pending.is_empty(),
245            error::RepartitionPendingTailsSnafu {
246                region_ids: pending
247            }
248        );
249        Ok(())
250    }
251
252    async fn prepare_from<'a>(&'a self, ctx: &mut Context) -> Result<&'a [PartitionExpr]> {
253        match &self.from {
254            RepartitionFrom::Partitioned {
255                exprs,
256                target_partition_columns,
257            } => {
258                Self::prepare_partitioned(ctx, target_partition_columns.as_deref()).await?;
259                Ok(exprs)
260            }
261            RepartitionFrom::Unpartitioned { partition_columns } => {
262                Self::prepare_unpartitioned(ctx, partition_columns).await?;
263                Ok(&[])
264            }
265        }
266    }
267
268    async fn prepare_unpartitioned(ctx: &mut Context, partition_columns: &[String]) -> Result<()> {
269        if ctx.persistent_ctx.partition_metadata_update.is_some() {
270            return Ok(());
271        }
272
273        ensure!(
274            !partition_columns.is_empty(),
275            error::InvalidArgumentsSnafu {
276                err_msg: "Unpartitioned repartition expects non-empty partition columns"
277                    .to_string(),
278            }
279        );
280
281        let table_info_value = ctx.get_table_info_value().await?;
282        ensure!(
283            table_info_value
284                .table_info
285                .meta
286                .partition_key_indices
287                .is_empty(),
288            error::InvalidArgumentsSnafu {
289                err_msg: format!(
290                    "Unpartitioned repartition expects an unpartitioned table, but table {} has partition key indices: {:?}",
291                    ctx.persistent_ctx.table_id,
292                    table_info_value.table_info.meta.partition_key_indices
293                ),
294            }
295        );
296
297        let schema = &table_info_value.table_info.meta.schema;
298        let partition_key_indices = partition_columns
299            .iter()
300            .map(|column_name| {
301                schema.column_index_by_name(column_name).with_context(|| {
302                    error::InvalidArgumentsSnafu {
303                        err_msg: format!(
304                            "Partition column {} not found in table {}",
305                            column_name, ctx.persistent_ctx.table_id
306                        ),
307                    }
308                })
309            })
310            .collect::<Result<Vec<_>>>()?;
311        ctx.persistent_ctx.partition_metadata_update = Some(
312            PartitionMetadataUpdate::from_unpartitioned(partition_key_indices),
313        );
314
315        Ok(())
316    }
317
318    async fn prepare_partitioned(
319        ctx: &mut Context,
320        target_partition_columns: Option<&[String]>,
321    ) -> Result<()> {
322        let Some(target_partition_columns) = target_partition_columns else {
323            return Ok(());
324        };
325        if ctx.persistent_ctx.partition_metadata_update.is_some() {
326            return Ok(());
327        }
328
329        ensure!(
330            !target_partition_columns.is_empty(),
331            error::InvalidArgumentsSnafu {
332                err_msg: "Partitioned source expects non-empty target partition columns"
333                    .to_string(),
334            }
335        );
336
337        let table_info_value = ctx.get_table_info_value().await?;
338        let schema = &table_info_value.table_info.meta.schema;
339        let target_partition_key_indices = target_partition_columns
340            .iter()
341            .map(|column_name| {
342                schema.column_index_by_name(column_name).with_context(|| {
343                    error::InvalidArgumentsSnafu {
344                        err_msg: format!(
345                            "Target partition column {} not found in table {}",
346                            column_name, ctx.persistent_ctx.table_id
347                        ),
348                    }
349                })
350            })
351            .collect::<Result<Vec<_>>>()?;
352        ctx.persistent_ctx.partition_metadata_update =
353            Some(PartitionMetadataUpdate::from_partitioned(
354                table_info_value.table_info.meta.partition_key_indices,
355                target_partition_key_indices,
356            ));
357
358        Ok(())
359    }
360
361    pub(crate) fn build_plan(
362        physical_route: &PhysicalTableRouteValue,
363        from_exprs: &[PartitionExpr],
364        to_exprs: &[PartitionExpr],
365    ) -> Result<Vec<AllocationPlanEntry>> {
366        let subtasks = if from_exprs.is_empty() {
367            Self::default_source_subtasks(to_exprs)?
368        } else {
369            subtask::create_subtasks(from_exprs, to_exprs)
370                .context(error::RepartitionCreateSubtasksSnafu)?
371        };
372        if subtasks.is_empty() {
373            return Ok(vec![]);
374        }
375
376        let src_descriptors = Self::source_region_descriptors(from_exprs, physical_route)?;
377        Ok(Self::build_plan_entries(
378            subtasks,
379            &src_descriptors,
380            to_exprs,
381        ))
382    }
383
384    fn build_plan_entries(
385        subtasks: Vec<RepartitionSubtask>,
386        source_index: &[SourceRegionDescriptor],
387        target_exprs: &[PartitionExpr],
388    ) -> Vec<AllocationPlanEntry> {
389        subtasks
390            .into_iter()
391            .map(|subtask| {
392                let group_id = Uuid::new_v4();
393                let source_regions = subtask
394                    .from_expr_indices
395                    .iter()
396                    .map(|&idx| source_index[idx].clone())
397                    .collect::<Vec<_>>();
398
399                let target_partition_exprs = subtask
400                    .to_expr_indices
401                    .iter()
402                    .map(|&idx| target_exprs[idx].clone())
403                    .collect::<Vec<_>>();
404                AllocationPlanEntry {
405                    group_id,
406                    source_regions,
407                    target_partition_exprs,
408                    transition_map: subtask.transition_map,
409                }
410            })
411            .collect::<Vec<_>>()
412    }
413
414    fn default_source_subtasks(to_exprs: &[PartitionExpr]) -> Result<Vec<RepartitionSubtask>> {
415        ensure!(
416            !to_exprs.is_empty(),
417            error::UnexpectedSnafu {
418                violated: "Default source repartition expects non-empty target partition exprs",
419            }
420        );
421
422        Collider::new(to_exprs).context(error::RepartitionCreateSubtasksSnafu)?;
423
424        let to_expr_indices = (0..to_exprs.len()).collect::<Vec<_>>();
425        Ok(vec![RepartitionSubtask {
426            from_expr_indices: vec![0],
427            to_expr_indices: to_expr_indices.clone(),
428            transition_map: vec![to_expr_indices],
429        }])
430    }
431
432    fn source_region_descriptors(
433        from_exprs: &[PartitionExpr],
434        physical_route: &PhysicalTableRouteValue,
435    ) -> Result<Vec<SourceRegionDescriptor>> {
436        if from_exprs.is_empty() {
437            return Self::default_source_region_descriptors(physical_route);
438        }
439
440        let existing_regions = physical_route
441            .region_routes
442            .iter()
443            .map(|route| (route.region.id, route.region.partition_expr()))
444            .collect::<Vec<_>>();
445
446        let descriptors = from_exprs
447            .iter()
448            .map(|expr| {
449                let expr_json = expr
450                    .as_json_str()
451                    .context(error::SerializePartitionExprSnafu)?;
452
453                let matched_region_id = existing_regions
454                    .iter()
455                    .find_map(|(region_id, existing_expr)| {
456                        (existing_expr == &expr_json).then_some(*region_id)
457                    })
458                    .with_context(|| error::RepartitionSourceExprMismatchSnafu { expr: &expr_json })
459                    .inspect_err(|_| {
460                        debug!("Failed to find matching region for partition expression: {}, existing regions: {:?}", expr_json, existing_regions);
461                    })?;
462
463                Ok(SourceRegionDescriptor::partitioned(
464                    matched_region_id,
465                    expr.clone(),
466                ))
467            })
468            .collect::<Result<Vec<_>>>()?;
469
470        Ok(descriptors)
471    }
472
473    fn default_source_region_descriptors(
474        physical_route: &PhysicalTableRouteValue,
475    ) -> Result<Vec<SourceRegionDescriptor>> {
476        ensure!(
477            physical_route.region_routes.len() == 1,
478            error::UnexpectedSnafu {
479                violated: format!(
480                    "Default source repartition expects exactly one source region, but got {}",
481                    physical_route.region_routes.len()
482                ),
483            }
484        );
485        let source_region = &physical_route.region_routes[0].region;
486        ensure!(
487            source_region.partition_expr().is_empty(),
488            error::UnexpectedSnafu {
489                violated: format!(
490                    "Default source repartition expects an empty partition expr, but got {}",
491                    source_region.partition_expr()
492                ),
493            }
494        );
495
496        Ok(vec![SourceRegionDescriptor::Default {
497            region_id: source_region.id,
498        }])
499    }
500}
501
502#[cfg(test)]
503mod tests {
504    use std::sync::Arc;
505
506    use common_error::ext::ErrorExt;
507    use common_error::status_code::StatusCode;
508    use common_meta::ddl::test_util::datanode_handler::NaiveDatanodeHandler;
509    use common_meta::key::table_route::PhysicalTableRouteValue;
510    use common_meta::peer::Peer;
511    use common_meta::rpc::router::{Region, RegionRoute};
512    use common_meta::test_util::MockDatanodeManager;
513    use datatypes::prelude::Value;
514    use partition::expr::{Operand, RestrictedOp};
515    use store_api::storage::RegionId;
516
517    use super::*;
518    use crate::procedure::repartition::test_util::{
519        TestingEnv, new_parent_context, range_expr, report_leader_regions, test_region_route,
520        test_region_wal_options,
521    };
522
523    fn physical_route(region_routes: Vec<RegionRoute>) -> PhysicalTableRouteValue {
524        PhysicalTableRouteValue::new(region_routes)
525    }
526
527    async fn new_test_context(env: &TestingEnv, table_id: u32) -> Context {
528        env.create_physical_table_metadata_for_repartition(
529            table_id,
530            vec![test_region_route(RegionId::new(table_id, 1), "")],
531            test_region_wal_options(&[1]),
532        )
533        .await;
534        let node_manager = Arc::new(MockDatanodeManager::new(NaiveDatanodeHandler));
535        new_parent_context(env, node_manager, table_id)
536    }
537
538    #[test]
539    fn test_build_plan_with_default_source_region() {
540        let table_id = 1024;
541        let physical_route =
542            physical_route(vec![test_region_route(RegionId::new(table_id, 1), "")]);
543        let to_exprs = vec![range_expr("x", 0, 50), range_expr("x", 50, 100)];
544
545        let plans = RepartitionStart::build_plan(&physical_route, &[], &to_exprs).unwrap();
546
547        assert_eq!(plans.len(), 1);
548        let plan = &plans[0];
549        assert_eq!(
550            plan.source_regions,
551            vec![SourceRegionDescriptor::Default {
552                region_id: RegionId::new(table_id, 1)
553            }]
554        );
555        assert_eq!(plan.target_partition_exprs, to_exprs);
556        assert_eq!(plan.transition_map, vec![vec![0, 1]]);
557    }
558
559    #[test]
560    fn test_build_plan_with_default_source_rejects_non_empty_partition_expr() {
561        let table_id = 1024;
562        let physical_route = physical_route(vec![test_region_route(
563            RegionId::new(table_id, 1),
564            &range_expr("x", 0, 100).as_json_str().unwrap(),
565        )]);
566        let to_exprs = vec![range_expr("x", 0, 50), range_expr("x", 50, 100)];
567
568        let err = RepartitionStart::build_plan(&physical_route, &[], &to_exprs).unwrap_err();
569
570        assert!(err.to_string().contains("empty partition expr"));
571    }
572
573    #[test]
574    fn test_build_plan_with_default_source_rejects_multiple_regions() {
575        let table_id = 1024;
576        let physical_route = physical_route(vec![
577            test_region_route(RegionId::new(table_id, 1), ""),
578            test_region_route(RegionId::new(table_id, 2), ""),
579        ]);
580        let to_exprs = vec![range_expr("x", 0, 50), range_expr("x", 50, 100)];
581
582        let err = RepartitionStart::build_plan(&physical_route, &[], &to_exprs).unwrap_err();
583
584        assert!(err.to_string().contains("exactly one source region"));
585    }
586
587    #[test]
588    fn test_build_plan_with_default_source_rejects_empty_targets() {
589        let table_id = 1024;
590        let physical_route =
591            physical_route(vec![test_region_route(RegionId::new(table_id, 1), "")]);
592
593        let err = RepartitionStart::build_plan(&physical_route, &[], &[]).unwrap_err();
594
595        assert!(err.to_string().contains("non-empty target partition exprs"));
596    }
597
598    #[test]
599    fn test_build_plan_with_default_source_rejects_invalid_targets() {
600        let table_id = 1024;
601        let physical_route =
602            physical_route(vec![test_region_route(RegionId::new(table_id, 1), "")]);
603        let invalid_to_expr = PartitionExpr::new(
604            Operand::Value(Value::Int64(1)),
605            RestrictedOp::Eq,
606            Operand::Value(Value::Int64(2)),
607        );
608
609        let err =
610            RepartitionStart::build_plan(&physical_route, &[], &[invalid_to_expr]).unwrap_err();
611
612        assert!(
613            err.to_string()
614                .contains("Failed to create repartition subtasks")
615        );
616    }
617
618    #[test]
619    fn test_build_plan_keeps_partitioned_source_matching() {
620        let table_id = 1024;
621        let from_exprs = vec![range_expr("x", 0, 100)];
622        let to_exprs = vec![range_expr("x", 0, 50), range_expr("x", 50, 100)];
623        let physical_route = physical_route(vec![RegionRoute {
624            region: Region {
625                id: RegionId::new(table_id, 1),
626                partition_expr: from_exprs[0].as_json_str().unwrap(),
627                ..Default::default()
628            },
629            leader_peer: Some(Peer::empty(1)),
630            ..Default::default()
631        }]);
632
633        let plans = RepartitionStart::build_plan(&physical_route, &from_exprs, &to_exprs).unwrap();
634
635        assert_eq!(plans.len(), 1);
636        assert_eq!(
637            plans[0].source_regions,
638            vec![SourceRegionDescriptor::partitioned(
639                RegionId::new(table_id, 1),
640                from_exprs[0].clone()
641            )]
642        );
643    }
644
645    #[test]
646    fn test_repartition_start_deserializes_legacy_from_exprs() {
647        let from_exprs = vec![range_expr("x", 0, 100)];
648        let to_exprs = vec![range_expr("x", 0, 50), range_expr("x", 50, 100)];
649        let json = serde_json::json!({
650            "from_exprs": from_exprs,
651            "to_exprs": to_exprs,
652        })
653        .to_string();
654
655        let state: RepartitionStart = serde_json::from_str(&json).unwrap();
656
657        let RepartitionFrom::Partitioned {
658            exprs,
659            target_partition_columns,
660        } = state.from
661        else {
662            panic!("expected partition source");
663        };
664        assert_eq!(exprs, vec![range_expr("x", 0, 100)]);
665        assert!(target_partition_columns.is_none());
666    }
667
668    #[test]
669    fn test_repartition_start_deserializes_current_from() {
670        let state = RepartitionStart::new(
671            RepartitionFrom::Unpartitioned {
672                partition_columns: vec!["col1".to_string()],
673            },
674            vec![range_expr("col1", 0, 50)],
675        );
676        let json = serde_json::to_string(&state).unwrap();
677
678        let state: RepartitionStart = serde_json::from_str(&json).unwrap();
679
680        let RepartitionFrom::Unpartitioned { partition_columns } = state.from else {
681            panic!("expected unpartitioned source");
682        };
683        assert_eq!(partition_columns, vec!["col1"]);
684    }
685
686    #[tokio::test]
687    async fn test_partitioned_source_does_not_initialize_partition_metadata_update() {
688        let env = TestingEnv::new();
689        let table_id = 1024;
690        env.create_physical_table_metadata_for_repartition(
691            table_id,
692            vec![test_region_route(
693                RegionId::new(table_id, 1),
694                &range_expr("x", 0, 100).as_json_str().unwrap(),
695            )],
696            test_region_wal_options(&[1]),
697        )
698        .await;
699        let node_manager = Arc::new(MockDatanodeManager::new(NaiveDatanodeHandler));
700        let mut ctx = new_parent_context(&env, node_manager, table_id);
701        let mut state = RepartitionStart::new(
702            RepartitionFrom::Partitioned {
703                exprs: vec![range_expr("x", 0, 100)],
704                target_partition_columns: None,
705            },
706            vec![range_expr("x", 0, 50), range_expr("x", 50, 100)],
707        );
708
709        let (next, status) = state
710            .next(&mut ctx, &TestingEnv::procedure_context())
711            .await
712            .unwrap();
713
714        assert!(!status.need_persist());
715        assert!(next.as_any().is::<AllocateRegion>());
716        assert!(ctx.persistent_ctx.partition_metadata_update.is_none());
717    }
718
719    #[tokio::test]
720    async fn test_partitioned_source_initializes_target_partition_metadata_update() {
721        let env = TestingEnv::new();
722        let table_id = 1024;
723        env.create_physical_table_metadata_for_repartition(
724            table_id,
725            vec![test_region_route(
726                RegionId::new(table_id, 1),
727                &range_expr("x", 0, 100).as_json_str().unwrap(),
728            )],
729            test_region_wal_options(&[1]),
730        )
731        .await;
732        let node_manager = Arc::new(MockDatanodeManager::new(NaiveDatanodeHandler));
733        let mut ctx = new_parent_context(&env, node_manager, table_id);
734        let current = ctx.get_raw_table_info_value().await.unwrap();
735        let mut table_info = current.table_info.clone();
736        table_info.meta.partition_key_indices = vec![0];
737        ctx.update_table_info(&current, current.update(table_info))
738            .await
739            .unwrap();
740        let mut state = RepartitionStart::new(
741            RepartitionFrom::Partitioned {
742                exprs: vec![range_expr("x", 0, 100)],
743                target_partition_columns: Some(vec!["col2".to_string(), "col1".to_string()]),
744            },
745            vec![range_expr("x", 0, 50), range_expr("x", 50, 100)],
746        );
747
748        let (next, status) = state
749            .next(&mut ctx, &TestingEnv::procedure_context())
750            .await
751            .unwrap();
752
753        assert!(status.need_persist());
754        assert!(next.as_any().is::<UpdatePartitionMetadata>());
755        let update = ctx
756            .persistent_ctx
757            .partition_metadata_update
758            .as_ref()
759            .unwrap();
760        assert_eq!(update.original_partition_key_indices, vec![0]);
761        assert_eq!(update.target_partition_key_indices, vec![2, 0]);
762        assert!(!update.expect_empty_partition_key_indices);
763    }
764
765    #[tokio::test]
766    async fn test_unpartitioned_source_initializes_partition_metadata_update() {
767        let env = TestingEnv::new();
768        let table_id = 1024;
769        let mut ctx = new_test_context(&env, table_id).await;
770        let mut state = RepartitionStart::new(
771            RepartitionFrom::Unpartitioned {
772                partition_columns: vec!["col2".to_string(), "col1".to_string()],
773            },
774            vec![range_expr("col2", 0, 50), range_expr("col2", 50, 100)],
775        );
776
777        let (next, status) = state
778            .next(&mut ctx, &TestingEnv::procedure_context())
779            .await
780            .unwrap();
781
782        assert!(status.need_persist());
783        assert!(next.as_any().is::<UpdatePartitionMetadata>());
784        assert_eq!(
785            ctx.persistent_ctx
786                .partition_metadata_update
787                .as_ref()
788                .unwrap()
789                .target_partition_key_indices,
790            vec![2, 0]
791        );
792    }
793
794    #[tokio::test]
795    async fn test_unpartitioned_source_rejects_existing_partition_metadata() {
796        let env = TestingEnv::new();
797        let table_id = 1024;
798        let mut ctx = new_test_context(&env, table_id).await;
799        let current = ctx.get_raw_table_info_value().await.unwrap();
800        let mut table_info = current.table_info.clone();
801        table_info.meta.partition_key_indices = vec![0];
802        ctx.update_table_info(&current, current.update(table_info))
803            .await
804            .unwrap();
805        let mut state = RepartitionStart::new(
806            RepartitionFrom::Unpartitioned {
807                partition_columns: vec!["col1".to_string()],
808            },
809            vec![range_expr("col1", 0, 50)],
810        );
811
812        let err = state
813            .next(&mut ctx, &TestingEnv::procedure_context())
814            .await
815            .unwrap_err();
816
817        assert!(err.to_string().contains("expects an unpartitioned table"));
818        assert!(ctx.persistent_ctx.partition_metadata_update.is_none());
819    }
820
821    #[tokio::test]
822    async fn test_repartition_start_rejects_empty_target_partition_exprs() {
823        let env = TestingEnv::new();
824        let table_id = 1024;
825        let mut ctx = new_test_context(&env, table_id).await;
826        let mut state = RepartitionStart::new(
827            RepartitionFrom::Partitioned {
828                exprs: vec![],
829                target_partition_columns: None,
830            },
831            vec![],
832        );
833
834        let err = state
835            .next(&mut ctx, &TestingEnv::procedure_context())
836            .await
837            .unwrap_err();
838
839        assert!(
840            err.to_string()
841                .contains("non-empty target partition expressions")
842        );
843    }
844
845    #[tokio::test]
846    async fn test_unpartitioned_source_rejects_empty_target_partition_exprs() {
847        let env = TestingEnv::new();
848        let table_id = 1024;
849        let mut ctx = new_test_context(&env, table_id).await;
850        let mut state = RepartitionStart::new(
851            RepartitionFrom::Unpartitioned {
852                partition_columns: vec!["col1".to_string()],
853            },
854            vec![],
855        );
856
857        let err = state
858            .next(&mut ctx, &TestingEnv::procedure_context())
859            .await
860            .unwrap_err();
861
862        assert!(
863            err.to_string()
864                .contains("non-empty target partition expressions")
865        );
866        assert!(ctx.persistent_ctx.partition_metadata_update.is_none());
867    }
868
869    #[tokio::test]
870    async fn test_unpartitioned_source_rejects_empty_partition_columns() {
871        let env = TestingEnv::new();
872        let table_id = 1024;
873        let mut ctx = new_test_context(&env, table_id).await;
874        let mut state = RepartitionStart::new(
875            RepartitionFrom::Unpartitioned {
876                partition_columns: vec![],
877            },
878            vec![range_expr("col1", 0, 50)],
879        );
880
881        let err = state
882            .next(&mut ctx, &TestingEnv::procedure_context())
883            .await
884            .unwrap_err();
885
886        assert!(err.to_string().contains("non-empty partition columns"));
887        assert!(ctx.persistent_ctx.partition_metadata_update.is_none());
888    }
889
890    #[tokio::test]
891    async fn test_unpartitioned_source_rejects_missing_partition_column() {
892        let env = TestingEnv::new();
893        let table_id = 1024;
894        let mut ctx = new_test_context(&env, table_id).await;
895        let mut state = RepartitionStart::new(
896            RepartitionFrom::Unpartitioned {
897                partition_columns: vec!["missing_col".to_string()],
898            },
899            vec![range_expr("col1", 0, 50)],
900        );
901
902        let err = state
903            .next(&mut ctx, &TestingEnv::procedure_context())
904            .await
905            .unwrap_err();
906
907        assert!(
908            err.to_string()
909                .contains("Partition column missing_col not found")
910        );
911        assert!(ctx.persistent_ctx.partition_metadata_update.is_none());
912    }
913
914    /// Creates a table of regions 1 and 2 and a repartition that merges them.
915    async fn new_merge(env: &TestingEnv, table_id: u32) -> (Context, RepartitionStart) {
916        env.create_physical_table_metadata_for_repartition(
917            table_id,
918            vec![
919                test_region_route(
920                    RegionId::new(table_id, 1),
921                    &range_expr("x", 0, 50).as_json_str().unwrap(),
922                ),
923                test_region_route(
924                    RegionId::new(table_id, 2),
925                    &range_expr("x", 50, 100).as_json_str().unwrap(),
926                ),
927            ],
928            test_region_wal_options(&[1, 2]),
929        )
930        .await;
931        let node_manager = Arc::new(MockDatanodeManager::new(NaiveDatanodeHandler));
932        let ctx = new_parent_context(env, node_manager, table_id);
933        let state = RepartitionStart::new(
934            RepartitionFrom::Partitioned {
935                exprs: vec![range_expr("x", 0, 50), range_expr("x", 50, 100)],
936                target_partition_columns: None,
937            },
938            vec![range_expr("x", 0, 100)],
939        );
940        (ctx, state)
941    }
942
943    #[tokio::test]
944    async fn test_repartition_start_retries_source_with_pending_tails() {
945        let env = TestingEnv::new();
946        let table_id = 1024;
947        let (mut ctx, mut state) = new_merge(&env, table_id).await;
948        report_leader_regions(&ctx, &[RegionId::new(table_id, 1)], false);
949        report_leader_regions(&ctx, &[RegionId::new(table_id, 2)], true);
950
951        let err = state
952            .next(&mut ctx, &TestingEnv::procedure_context())
953            .await
954            .unwrap_err();
955
956        assert!(err.is_retryable());
957        assert_eq!(StatusCode::RegionNotReady, err.status_code());
958        assert!(matches!(
959            &err,
960            error::Error::RepartitionPendingTails { region_ids, .. }
961                if region_ids == &[RegionId::new(table_id, 2)]
962        ));
963        report_leader_regions(&ctx, &[RegionId::new(table_id, 2)], false);
964        let (next, _) = state
965            .next(&mut ctx, &TestingEnv::procedure_context())
966            .await
967            .unwrap();
968        assert!(next.as_any().is::<AllocateRegion>());
969    }
970
971    #[tokio::test]
972    async fn test_repartition_start_passes_unreported_source() {
973        let env = TestingEnv::new();
974        let table_id = 1024;
975        let (mut ctx, mut state) = new_merge(&env, table_id).await;
976        report_leader_regions(&ctx, &[RegionId::new(table_id, 1)], false);
977
978        let (next, _) = state
979            .next(&mut ctx, &TestingEnv::procedure_context())
980            .await
981            .unwrap();
982        assert!(next.as_any().is::<AllocateRegion>());
983    }
984}