-
Notifications
You must be signed in to change notification settings - Fork 11
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
import multiple tables at same time - 1 #2191
Open
makalaaneesh
wants to merge
13
commits into
main
Choose a base branch
from
aneesh/import-multiple-tables-at-same-time-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+571
−128
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fa29368
wip
makalaaneesh ec54ea6
base logic for producing next batch
makalaaneesh a4aabc8
store line from previous batch
makalaaneesh 440a5f3
test
makalaaneesh c14e69e
rewrite test
makalaaneesh a056385
minor fix
makalaaneesh c45b6a3
more tests
makalaaneesh a78387e
batch value verify
makalaaneesh 65c7645
assert less than
makalaaneesh a9a294c
resumable test
makalaaneesh 1f32f7a
import data change to use filebatchproducer
makalaaneesh cbc1ede
unit tag
makalaaneesh 943bd22
cleanup
makalaaneesh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
/* | ||
Copyright (c) YugabyteDB, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/yugabyte/yb-voyager/yb-voyager/src/datafile" | ||
"github.com/yugabyte/yb-voyager/yb-voyager/src/utils" | ||
) | ||
|
||
type FileBatchProducer struct { | ||
task *ImportFileTask | ||
state *ImportDataState | ||
|
||
pendingBatches []*Batch | ||
lastBatchNumber int64 | ||
lastOffset int64 | ||
fileFullySplit bool | ||
completed bool | ||
|
||
dataFile datafile.DataFile | ||
header string | ||
numLinesTaken int64 | ||
lineFromPreviousBatch string | ||
} | ||
|
||
func NewFileBatchProducer(task *ImportFileTask, state *ImportDataState) (*FileBatchProducer, error) { | ||
err := state.PrepareForFileImport(task.FilePath, task.TableNameTup) | ||
if err != nil { | ||
return nil, fmt.Errorf("preparing for file import: %s", err) | ||
} | ||
pendingBatches, lastBatchNumber, lastOffset, fileFullySplit, err := state.Recover(task.FilePath, task.TableNameTup) | ||
if err != nil { | ||
return nil, fmt.Errorf("recovering state for table: %q: %s", task.TableNameTup, err) | ||
} | ||
var completed bool | ||
if len(pendingBatches) == 0 && fileFullySplit { | ||
completed = true | ||
} | ||
|
||
return &FileBatchProducer{ | ||
task: task, | ||
state: state, | ||
pendingBatches: pendingBatches, | ||
lastBatchNumber: lastBatchNumber, | ||
lastOffset: lastOffset, | ||
fileFullySplit: fileFullySplit, | ||
completed: completed, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
numLinesTaken: lastOffset, | ||
}, nil | ||
} | ||
|
||
func (p *FileBatchProducer) Done() bool { | ||
return p.completed | ||
} | ||
|
||
func (p *FileBatchProducer) NextBatch() (*Batch, error) { | ||
if p.Done() { | ||
return nil, fmt.Errorf("already completed producing all batches") | ||
} | ||
if len(p.pendingBatches) > 0 { | ||
batch := p.pendingBatches[0] | ||
p.pendingBatches = p.pendingBatches[1:] | ||
// file is fully split and returning the last batch, so mark the producer as completed | ||
if len(p.pendingBatches) == 0 && p.fileFullySplit { | ||
p.completed = true | ||
} | ||
return batch, nil | ||
} | ||
|
||
return p.produceNextBatch() | ||
} | ||
|
||
func (p *FileBatchProducer) produceNextBatch() (*Batch, error) { | ||
if p.dataFile == nil { | ||
err := p.openDataFile() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
var readLineErr error | ||
var line string | ||
var currentBytesRead int64 | ||
batchNum := p.lastBatchNumber + 1 | ||
|
||
batchWriter, err := p.newBatchWriter() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if p.lineFromPreviousBatch != "" { | ||
err = batchWriter.WriteRecord(p.lineFromPreviousBatch) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add comment for explaining about this |
||
if err != nil { | ||
return nil, fmt.Errorf("Write to batch %d: %s", batchNum, err) | ||
} | ||
p.lineFromPreviousBatch = "" | ||
} | ||
|
||
for readLineErr == nil { | ||
|
||
line, currentBytesRead, readLineErr = p.dataFile.NextLine() | ||
if readLineErr == nil || (readLineErr == io.EOF && line != "") { | ||
// handling possible case: last dataline(i.e. EOF) but no newline char at the end | ||
p.numLinesTaken += 1 | ||
} | ||
log.Debugf("Batch %d: totalBytesRead %d, currentBytes %d \n", batchNum, p.dataFile.GetBytesRead(), currentBytesRead) | ||
if currentBytesRead > tdb.MaxBatchSizeInBytes() { | ||
//If a row is itself larger than MaxBatchSizeInBytes erroring out | ||
ybSpecificMsg := "" | ||
if tconf.TargetDBType == YUGABYTEDB { | ||
ybSpecificMsg = ", but should be strictly lower than the the rpc_max_message_size on YugabyteDB (default 267386880 bytes)" | ||
} | ||
return nil, fmt.Errorf("record of size %d larger than max batch size: record num=%d for table %q in file %s is larger than the max batch size %d bytes. Max Batch size can be changed using env var MAX_BATCH_SIZE_BYTES%s", currentBytesRead, p.numLinesTaken, p.task.TableNameTup.ForOutput(), p.task.FilePath, tdb.MaxBatchSizeInBytes(), ybSpecificMsg) | ||
} | ||
if line != "" { | ||
// can't use importBatchArgsProto.Columns as to use case insenstiive column names | ||
columnNames, _ := TableToColumnNames.Get(p.task.TableNameTup) | ||
line, err = valueConverter.ConvertRow(p.task.TableNameTup, columnNames, line) | ||
if err != nil { | ||
return nil, fmt.Errorf("transforming line number=%d for table: %q in file %s: %s", p.numLinesTaken, p.task.TableNameTup.ForOutput(), p.task.FilePath, err) | ||
} | ||
|
||
// Check if adding this record exceeds the max batch size | ||
if batchWriter.NumRecordsWritten == batchSizeInNumRows || | ||
p.dataFile.GetBytesRead() > tdb.MaxBatchSizeInBytes() { // GetBytesRead - returns the total bytes read until now including the currentBytesRead | ||
|
||
// Finalize the current batch without adding the record | ||
batch, err := p.finalizeBatch(batchWriter, false, p.numLinesTaken-1, p.dataFile.GetBytesRead()-currentBytesRead) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
//carry forward the bytes to next batch | ||
p.dataFile.ResetBytesRead(currentBytesRead) | ||
p.lineFromPreviousBatch = line | ||
|
||
// Start a new batch by calling the initBatchWriter function | ||
// initBatchWriter() | ||
return batch, nil | ||
} | ||
|
||
// Write the record to the new or current batch | ||
err = batchWriter.WriteRecord(line) | ||
if err != nil { | ||
return nil, fmt.Errorf("Write to batch %d: %s", batchNum, err) | ||
} | ||
} | ||
|
||
// Finalize the batch if it's the last line or the end of the file and reset the bytes read to 0 | ||
if readLineErr == io.EOF { | ||
batch, err := p.finalizeBatch(batchWriter, true, p.numLinesTaken, p.dataFile.GetBytesRead()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
p.completed = true | ||
p.dataFile.ResetBytesRead(0) | ||
return batch, nil | ||
} else if readLineErr != nil { | ||
return nil, fmt.Errorf("read line from data file: %q: %s", p.task.FilePath, readLineErr) | ||
} | ||
} | ||
// ideally should not reach here | ||
return nil, fmt.Errorf("could not produce next batch: err: %w", readLineErr) | ||
} | ||
|
||
func (p *FileBatchProducer) openDataFile() error { | ||
reader, err := dataStore.Open(p.task.FilePath) | ||
if err != nil { | ||
return fmt.Errorf("preparing reader for split generation on file: %q: %v", p.task.FilePath, err) | ||
} | ||
|
||
dataFile, err := datafile.NewDataFile(p.task.FilePath, reader, dataFileDescriptor) | ||
|
||
if err != nil { | ||
return fmt.Errorf("open datafile: %q: %v", p.task.FilePath, err) | ||
} | ||
p.dataFile = dataFile | ||
|
||
log.Infof("Skipping %d lines from %q", p.lastOffset, p.task.FilePath) | ||
err = dataFile.SkipLines(p.lastOffset) | ||
if err != nil { | ||
return fmt.Errorf("skipping line for offset=%d: %v", p.lastOffset, err) | ||
} | ||
if dataFileDescriptor.HasHeader { | ||
p.header = dataFile.GetHeader() | ||
} | ||
return nil | ||
} | ||
|
||
func (p *FileBatchProducer) newBatchWriter() (*BatchWriter, error) { | ||
batchNum := p.lastBatchNumber + 1 | ||
batchWriter := p.state.NewBatchWriter(p.task.FilePath, p.task.TableNameTup, batchNum) | ||
err := batchWriter.Init() | ||
if err != nil { | ||
return nil, fmt.Errorf("initializing batch writer for table: %q: %s", p.task.TableNameTup, err) | ||
} | ||
// Write the header if necessary | ||
if p.header != "" && dataFileDescriptor.FileFormat == datafile.CSV { | ||
err = batchWriter.WriteHeader(p.header) | ||
if err != nil { | ||
utils.ErrExit("writing header for table: %q: %s", p.task.TableNameTup, err) | ||
} | ||
} | ||
return batchWriter, nil | ||
} | ||
|
||
func (p *FileBatchProducer) finalizeBatch(batchWriter *BatchWriter, isLastBatch bool, offsetEnd int64, bytesInBatch int64) (*Batch, error) { | ||
batchNum := p.lastBatchNumber + 1 | ||
batch, err := batchWriter.Done(isLastBatch, offsetEnd, bytesInBatch) | ||
if err != nil { | ||
utils.ErrExit("finalizing batch %d: %s", batchNum, err) | ||
} | ||
batchWriter = nil | ||
p.lastBatchNumber = batchNum | ||
return batch, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to do this
PrepareForFileImport
here? we are already doing it inNewFileBatchProducer