Skip to content

Commit 8b942ff

Browse files
committed
refactor(clickhouse): inline staging upload instead of WriteRecordsToStaging
Remove the WriteRecordsToStaging wrapper from avro_writer.go and call StagingStore.Upload directly in the ClickHouse connector. This avoids adding a new method to avro_writer.go and keeps the upload logic colocated with the ClickHouse-specific staging setup.
1 parent bbcc449 commit 8b942ff

2 files changed

Lines changed: 21 additions & 51 deletions

File tree

flow/connectors/clickhouse/avro_sync.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package connclickhouse
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"log/slog"
78
"strings"
89
"sync/atomic"
@@ -378,14 +379,28 @@ func (s *ClickHouseAvroSyncMethod) writeToAvroFile(
378379
}
379380
s3AvroFileKey = strings.TrimLeft(s3AvroFileKey, "/")
380381

381-
avroFile, err := ocfWriter.WriteRecordsToStaging(
382-
ctx, env, s.staging, s3AvroFileKey, typeConversions, numericTruncator,
383-
)
384-
if err != nil {
385-
return utils.AvroFile{}, fmt.Errorf("failed to write records to S3: %w", err)
382+
r, w := io.Pipe()
383+
defer r.Close()
384+
385+
var writeOcfError error
386+
var numRows int64
387+
go func() {
388+
defer w.Close()
389+
numRows, writeOcfError = ocfWriter.WriteOCF(ctx, env, w, typeConversions, numericTruncator)
390+
}()
391+
392+
if err := s.staging.Upload(ctx, env, s3AvroFileKey, r); err != nil {
393+
return utils.AvroFile{}, fmt.Errorf("failed to upload to staging: %w", err)
394+
}
395+
if writeOcfError != nil {
396+
return utils.AvroFile{}, writeOcfError
386397
}
387398

388-
return avroFile, nil
399+
return utils.AvroFile{
400+
StorageLocation: utils.AvroS3Storage,
401+
FilePath: s3AvroFileKey,
402+
NumRecords: numRows,
403+
}, nil
389404
}
390405

391406
func (s *ClickHouseAvroSyncMethod) SyncQRepObjects(

flow/connectors/utils/avro_writer.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -166,51 +166,6 @@ func (p *peerDBOCFWriter) WriteRecordsToS3(
166166
}, nil
167167
}
168168

169-
// WriteRecordsToStaging serializes records as Avro OCF and uploads via the given StagingStore.
170-
func (p *peerDBOCFWriter) WriteRecordsToStaging(
171-
ctx context.Context,
172-
env map[string]string,
173-
store StagingStore,
174-
key string,
175-
typeConversions map[string]types.TypeConversion,
176-
numericTruncator model.SnapshotTableNumericTruncator,
177-
) (AvroFile, error) {
178-
logger := internal.LoggerFromCtx(ctx)
179-
180-
r, w := io.Pipe()
181-
defer r.Close()
182-
183-
var writeOcfError error
184-
var numRows int64
185-
186-
go func() {
187-
defer func() {
188-
if r := recover(); r != nil {
189-
writeOcfError = fmt.Errorf("panic occurred during WriteOCF: %v", r)
190-
stack := string(debug.Stack())
191-
logger.Error("panic during WriteOCF", slog.Any("error", writeOcfError), slog.String("stack", stack))
192-
}
193-
w.Close()
194-
}()
195-
numRows, writeOcfError = p.WriteOCF(ctx, env, w, typeConversions, numericTruncator)
196-
}()
197-
198-
if err := store.Upload(ctx, env, key, r); err != nil {
199-
return AvroFile{}, fmt.Errorf("failed to upload to staging: %w", err)
200-
}
201-
202-
if writeOcfError != nil {
203-
logger.Error("failed to write records to OCF", slog.Any("error", writeOcfError))
204-
return AvroFile{}, writeOcfError
205-
}
206-
207-
return AvroFile{
208-
StorageLocation: AvroS3Storage,
209-
FilePath: key,
210-
NumRecords: numRows,
211-
}, nil
212-
}
213-
214169
func (p *peerDBOCFWriter) WriteRecordsToAvroFile(ctx context.Context, env map[string]string, filePath string) (AvroFile, error) {
215170
file, err := os.Create(filePath)
216171
if err != nil {

0 commit comments

Comments
 (0)