Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DNM] sql: pre-split on TRUNCATE #63041

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions pkg/sql/drop_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
Expand Down Expand Up @@ -388,7 +389,11 @@ func (p *planner) dropTableImpl(
}

// unsplitRangesForTable unsplit any manually split ranges within the table span.
func (p *planner) unsplitRangesForTable(ctx context.Context, tableDesc *tabledesc.Mutable) error {
func (p *planner) unsplitRangesForTable(
ctx context.Context,
tableDesc *tabledesc.Mutable,
indexIDMapping map[descpb.IndexID]descpb.IndexID,
) error {
// Gate this on being the system tenant because secondary tenants aren't
// allowed to scan the meta ranges directly.
if p.ExecCfg().Codec.ForSystemTenant() {
Expand All @@ -397,6 +402,7 @@ func (p *planner) unsplitRangesForTable(ctx context.Context, tableDesc *tabledes
if err != nil {
return err
}
var mappedSplitPoints []roachpb.Key
for _, r := range ranges {
var desc roachpb.RangeDescriptor
if err := r.ValueProto(&desc); err != nil {
Expand All @@ -411,6 +417,34 @@ func (p *planner) unsplitRangesForTable(ctx context.Context, tableDesc *tabledes
return err
}
}

rest, tableID, indexID, err := p.ExecCfg().Codec.DecodeIndexPrefix(desc.StartKey.AsRawKey())
if err != nil {
continue
}
if tableID != uint32(tableDesc.GetID()) {
return errors.Errorf("expected table %d, got table ID %d", tableDesc, tableID)
}
mappedID, ok := indexIDMapping[descpb.IndexID(indexID)]
if !ok {
continue
}
indexPrefix := p.ExecCfg().Codec.IndexPrefix(tableID, uint32(mappedID))
mappedSplitPoints = append(mappedSplitPoints, append(indexPrefix, rest...))
}
// Downsample, remap to new table, split.
// TODO(nvanbenschoten): base this on num nodes or something.
newRanges := 64
jordanlewis marked this conversation as resolved.
Show resolved Hide resolved
if newRanges > len(mappedSplitPoints) {
newRanges = len(mappedSplitPoints)
}
every := float64(len(mappedSplitPoints)) / float64(newRanges)
expTime := p.ExecCfg().Clock.Now().Add(time.Hour.Nanoseconds(), 0)
for i := 0; i < newRanges; i++ {
splitPoint := mappedSplitPoints[int(float64(i)*every)]
if err := p.ExecCfg().DB.SplitAndScatter(ctx, splitPoint, expTime); err != nil {
return err
}
}
}
return nil
Expand Down Expand Up @@ -441,7 +475,7 @@ func (p *planner) initiateDropTable(

// Unsplit all manually split ranges in the table so they can be
// automatically merged by the merge queue.
if err := p.unsplitRangesForTable(ctx, tableDesc); err != nil {
if err := p.unsplitRangesForTable(ctx, tableDesc, nil); err != nil {
return err
}

Expand Down
14 changes: 13 additions & 1 deletion pkg/sql/truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
Expand Down Expand Up @@ -169,6 +170,12 @@ func (t *truncateNode) Next(runParams) (bool, error) { return false, nil }
func (t *truncateNode) Values() tree.Datums { return tree.Datums{} }
func (t *truncateNode) Close(context.Context) {}

var replaceSplits = settings.RegisterBoolSetting(
"sql.truncate.replace_splits",
"whether a truncate operation should replace existing split points with new ones",
true,
)

// truncateTable truncates the data of a table in a single transaction. It does
// so by dropping all existing indexes on the table and creating new ones without
// backfilling any data into the new indexes. The old indexes are cleaned up
Expand Down Expand Up @@ -259,9 +266,14 @@ func (p *planner) truncateTable(
return err
}

var replaceSplitsMapping map[descpb.IndexID]descpb.IndexID
if replaceSplits.Get(&p.ExecCfg().Settings.SV) {
replaceSplitsMapping = indexIDMapping
}

// Unsplit all manually split ranges in the table so they can be
// automatically merged by the merge queue.
if err := p.unsplitRangesForTable(ctx, tableDesc); err != nil {
if err := p.unsplitRangesForTable(ctx, tableDesc, replaceSplitsMapping); err != nil {
return err
}

Expand Down