Skip to content

Commit

Permalink
sql: presplit temp hash sharded index
Browse files Browse the repository at this point in the history
fixes #76686
we presplit hash sharded index before backflling it. but with the new
mvcc index backfiller, we also create a temp index to take care of write
traffic when the origin index is being backfilled. We need to presplit
the temp index as well.

Release note: None
Release justification: this needed by the mvcc index backfiller
  • Loading branch information
chengxiong-ruan committed Mar 8, 2022
1 parent d384f95 commit 6dca2c9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,4 @@ t_presplit 116 t_presplit_idx_member_id /Table/116/2/"seattle"/3 /Tabl
t_presplit 116 t_presplit_idx_member_id /Table/116/2/"seattle"/4 /Table/116/2/"seattle"/5
t_presplit 116 t_presplit_idx_member_id /Table/116/2/"seattle"/5 /Table/116/2/"seattle"/6
t_presplit 116 t_presplit_idx_member_id /Table/116/2/"seattle"/6 /Table/116/2/"seattle"/7
t_presplit 116 t_presplit_idx_member_id /Table/116/2/"seattle"/7 /Max
t_presplit 116 t_presplit_idx_member_id /Table/116/2/"seattle"/7 /Table/116/3/"new york"/0
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/hash_sharded_index
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ t_hash_pre_split 139 t_hash_pre_split_idx_b /Table/139/2/3 /Table/139/2
t_hash_pre_split 139 t_hash_pre_split_idx_b /Table/139/2/4 /Table/139/2/5
t_hash_pre_split 139 t_hash_pre_split_idx_b /Table/139/2/5 /Table/139/2/6
t_hash_pre_split 139 t_hash_pre_split_idx_b /Table/139/2/6 /Table/139/2/7
t_hash_pre_split 139 t_hash_pre_split_idx_b /Table/139/2/7 /Max
t_hash_pre_split 139 t_hash_pre_split_idx_b /Table/139/2/7 /Table/139/3/0

subtest test_default_bucket_count

Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/schema_changer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2985,9 +2985,9 @@ func (sc *SchemaChanger) shouldSplitAndScatter(
return false
}

if m.Adding() && idx.IsSharded() && !idx.IsTemporaryIndexForBackfill() {
if m.Adding() && idx.IsSharded() {
if sc.mvccCompliantAddIndex {
return m.Backfilling()
return m.Backfilling() || (idx.IsTemporaryIndexForBackfill() && m.DeleteOnly())
}
return m.DeleteOnly()
}
Expand Down
34 changes: 20 additions & 14 deletions pkg/sql/schema_changer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7484,8 +7484,8 @@ func TestHashShardedIndexRangePreSplit(t *testing.T) {
defer log.Scope(t).Close(t)
ctx := context.Background()

getShardedIndexRanges := func(tableDesc *tabledesc.Mutable, kvDB *kv.DB, codec keys.SQLCodec) ([]kv.KeyValue, error) {
indexSpan := tableDesc.IndexSpan(codec, descpb.IndexID(2))
getShardedIndexRanges := func(tableDesc *tabledesc.Mutable, kvDB *kv.DB, codec keys.SQLCodec, indexID descpb.IndexID) ([]kv.KeyValue, error) {
indexSpan := tableDesc.IndexSpan(codec, indexID)
ranges, err := kvDB.Scan(
ctx,
keys.RangeMetaKey(keys.MustAddr(indexSpan.Key)),
Expand Down Expand Up @@ -7524,23 +7524,29 @@ CREATE TABLE t.test_split(a INT PRIMARY KEY, b INT NOT NULL);
)

runBeforePreSplitting = func(tableDesc *tabledesc.Mutable, kvDB *kv.DB, codec keys.SQLCodec) error {
ranges, err := getShardedIndexRanges(tableDesc, kvDB, codec)
if err != nil {
return err
}
if len(ranges) != 0 {
return errors.Newf("expected 0 ranges but found %d", len(ranges))
// 2 is the id for the new index
// 3 is the id for temp index for backfilling
for id := range []int{2, 3} {
ranges, err := getShardedIndexRanges(tableDesc, kvDB, codec, descpb.IndexID(id))
if err != nil {
return err
}
if len(ranges) != 0 {
return errors.Newf("expected 0 ranges but found %d", len(ranges))
}
}
return nil
}

runAfterPreSplitting = func(tableDesc *tabledesc.Mutable, kvDB *kv.DB, codec keys.SQLCodec) error {
ranges, err := getShardedIndexRanges(tableDesc, kvDB, codec)
if err != nil {
return err
}
if len(ranges) != 8 {
return errors.Newf("expected 8 ranges but found %d", len(ranges))
for _, id := range []int{2, 3} {
ranges, err := getShardedIndexRanges(tableDesc, kvDB, codec, descpb.IndexID(id))
if err != nil {
return err
}
if len(ranges) != 8 {
return errors.Newf("expected 8 ranges but found %d", len(ranges))
}
}
return nil
}
Expand Down

0 comments on commit 6dca2c9

Please sign in to comment.