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

lightning: add function getChunkCompressedSizeForParquet for solving parquet oom issue (#49021) #49328

Open
wants to merge 3 commits into
base: release-6.1
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions br/pkg/lightning/restore/table_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pingcap/tidb/br/pkg/lightning/mydump"
verify "github.com/pingcap/tidb/br/pkg/lightning/verification"
"github.com/pingcap/tidb/br/pkg/lightning/worker"
"github.com/pingcap/tidb/br/pkg/storage"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/parser/model"
Expand Down Expand Up @@ -526,6 +527,13 @@ func (tr *TableRestore) restoreEngine(
break
}

if chunk.FileMeta.Type == mydump.SourceTypeParquet {
// TODO: use the compressed size of the chunk to conduct memory control
if _, err = getChunkCompressedSizeForParquet(ctx, chunk, rc.store); err != nil {
return nil, errors.Trace(err)
}
}

restoreWorker := rc.regionWorkers.Apply()
wg.Add(1)
go func(w *worker.Worker, cr *chunkRestore) {
Expand Down Expand Up @@ -884,6 +892,40 @@ func (tr *TableRestore) postProcess(
return true, nil
}

func getChunkCompressedSizeForParquet(
ctx context.Context,
chunk *checkpoints.ChunkCheckpoint,
store storage.ExternalStorage,
) (int64, error) {
reader, err := mydump.OpenParquetReader(ctx, store, chunk.FileMeta.Path, chunk.FileMeta.FileSize)
if err != nil {
return 0, errors.Trace(err)
}
parser, err := mydump.NewParquetParser(ctx, store, reader, chunk.FileMeta.Path)
if err != nil {
_ = reader.Close()
return 0, errors.Trace(err)
}
//nolint: errcheck
defer parser.Close()
err = parser.Reader.ReadFooter()
if err != nil {
return 0, errors.Trace(err)
}
rowGroups := parser.Reader.Footer.GetRowGroups()
var maxRowGroupSize int64
for _, rowGroup := range rowGroups {
var rowGroupSize int64
columnChunks := rowGroup.GetColumns()
for _, columnChunk := range columnChunks {
columnChunkSize := columnChunk.MetaData.GetTotalCompressedSize()
rowGroupSize += columnChunkSize
}
maxRowGroupSize = mathutil.Max(maxRowGroupSize, rowGroupSize)
}
return maxRowGroupSize, nil
}

func parseColumnPermutations(tableInfo *model.TableInfo, columns []string, ignoreColumns map[string]struct{}) ([]int, error) {
colPerm := make([]int, 0, len(tableInfo.Columns)+1)

Expand Down
36 changes: 36 additions & 0 deletions br/pkg/lightning/restore/table_restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1851,3 +1851,39 @@ func (s *tableRestoreSuite) TestGBKEncodedSchemaIsValid() {
require.NoError(s.T(), err)
require.Len(s.T(), msgs, 0)
}

func TestGetChunkCompressedSizeForParquet(t *testing.T) {
dir := "./testdata/"
fileName := "000000_0.parquet"
store, err := storage.NewLocalStorage(dir)
require.NoError(t, err)

dataFiles := make([]mydump.FileInfo, 0)
dataFiles = append(dataFiles, mydump.FileInfo{
TableName: filter.Table{Schema: "db", Name: "table"},
FileMeta: mydump.SourceFileMeta{
Path: fileName,
Type: mydump.SourceTypeParquet,
Compression: mydump.CompressionNone,
SortKey: "99",
FileSize: 192,
},
})

chunk := checkpoints.ChunkCheckpoint{
Key: checkpoints.ChunkCheckpointKey{Path: dataFiles[0].FileMeta.Path, Offset: 0},
FileMeta: dataFiles[0].FileMeta,
Chunk: mydump.Chunk{
Offset: 0,
EndOffset: 192,
PrevRowIDMax: 0,
RowIDMax: 100,
},
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

compressedSize, err := getChunkCompressedSizeForParquet(ctx, &chunk, store)
require.NoError(t, err)
require.Equal(t, compressedSize, int64(192))
}
Binary file added br/pkg/lightning/restore/testdata/000000_0.parquet
Binary file not shown.