Skip to content

Commit c629bae

Browse files
committed
Handle errors and cancel the remaining tasks
Signed-off-by: Levko Kravets <levko.ne@gmail.com>
1 parent b65c005 commit c629bae

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

internal/rows/arrowbased/batchloader.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,11 @@ func (bi *cloudBatchIterator) Next() (SparkArrowBatch, error) {
131131
link.StartRowOffset,
132132
link.RowCount,
133133
)
134+
135+
cancelCtx, cancelFn := context.WithCancel(bi.ctx)
134136
task := &cloudFetchDownloadTask{
135-
ctx: bi.ctx,
137+
ctx: cancelCtx,
138+
cancel: cancelFn,
136139
useLz4Compression: bi.cfg.UseLz4Compression,
137140
link: link,
138141
resultChan: make(chan cloudFetchDownloadTaskResult),
@@ -147,17 +150,29 @@ func (bi *cloudBatchIterator) Next() (SparkArrowBatch, error) {
147150
return nil, io.EOF
148151
}
149152

150-
return task.GetResult()
153+
batch, err := task.GetResult()
154+
155+
// once we've got an errored out task - cancel the remaining ones
156+
if err != nil {
157+
bi.Close()
158+
return nil, err
159+
}
160+
161+
// explicitly call cancel function on successfully completed task to avoid context leak
162+
task.cancel()
163+
return batch, nil
151164
}
152165

153166
func (bi *cloudBatchIterator) HasNext() bool {
154167
return (bi.pendingLinks.Len() > 0) || (bi.downloadTasks.Len() > 0)
155168
}
156169

157170
func (bi *cloudBatchIterator) Close() {
158-
bi.pendingLinks.Clear() // Clear the list
159-
// TODO: Cancel all download tasks
160-
bi.downloadTasks.Clear() // Clear the list
171+
bi.pendingLinks.Clear()
172+
for bi.downloadTasks.Len() > 0 {
173+
task := bi.downloadTasks.Dequeue()
174+
task.cancel()
175+
}
161176
}
162177

163178
type cloudFetchDownloadTaskResult struct {
@@ -167,6 +182,7 @@ type cloudFetchDownloadTaskResult struct {
167182

168183
type cloudFetchDownloadTask struct {
169184
ctx context.Context
185+
cancel context.CancelFunc
170186
useLz4Compression bool
171187
minTimeToExpiry time.Duration
172188
link *cli_service.TSparkArrowResultLink
@@ -180,9 +196,10 @@ func (cft *cloudFetchDownloadTask) GetResult() (SparkArrowBatch, error) {
180196
if ok {
181197
if result.err != nil {
182198
logger.Debug().Msgf(
183-
"CloudFetch: failed to download link at offset %d row count %d",
199+
"CloudFetch: failed to download link at offset %d row count %d, reason: %s",
184200
link.StartRowOffset,
185201
link.RowCount,
202+
result.err.Error(),
186203
)
187204
return nil, result.err
188205
}

0 commit comments

Comments
 (0)