@@ -587,9 +587,6 @@ func (m *GroupManager) SplitKeyspaceGroupByID(
587587 splitSourceKg .SplitState = & endpoint.SplitState {
588588 SplitSource : splitSourceKg .ID ,
589589 }
590- if err = m .store .SaveKeyspaceGroup (txn , splitSourceKg ); err != nil {
591- return err
592- }
593590 splitTargetKg = & endpoint.KeyspaceGroup {
594591 ID : splitTargetID ,
595592 // Keep the same user kind and members as the old keyspace group.
@@ -600,8 +597,18 @@ func (m *GroupManager) SplitKeyspaceGroupByID(
600597 SplitSource : splitSourceKg .ID ,
601598 },
602599 }
600+ // Save the split target keyspace group first, then save the split source keyspace group.
601+ // The order matters: if we save splitSourceKg first (which removes some keyspaces from it),
602+ // there will be a brief moment where those keyspaces don't belong to any group, causing
603+ // them to fallback to group 0. By saving splitTargetKg first (which contains the split
604+ // keyspaces), we ensure the keyspaces always belong to a valid group during the transition.
605+
603606 // Create the new split keyspace group.
604- return m .store .SaveKeyspaceGroup (txn , splitTargetKg )
607+ if err = m .store .SaveKeyspaceGroup (txn , splitTargetKg ); err != nil {
608+ return err
609+ }
610+ // Update the source keyspace group.
611+ return m .store .SaveKeyspaceGroup (txn , splitSourceKg )
605612 }); err != nil {
606613 return err
607614 }
@@ -611,9 +618,9 @@ func (m *GroupManager) SplitKeyspaceGroupByID(
611618 return nil
612619}
613620
621+ // `old` is the original keyspace list which will be split out,
622+ // `new` is the keyspace list which will be split from the old keyspace list.
614623func buildSplitKeyspaces (
615- // `old` is the original keyspace list which will be split out,
616- // `new` is the keyspace list which will be split from the old keyspace list.
617624 old , new []uint32 ,
618625 startKeyspaceID , endKeyspaceID uint32 ,
619626) (oldSplit , newSplit []uint32 , err error ) {
0 commit comments