-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathavro_sync.go
More file actions
416 lines (366 loc) · 12.8 KB
/
Copy pathavro_sync.go
File metadata and controls
416 lines (366 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package connclickhouse
import (
"context"
"fmt"
"io"
"log/slog"
"runtime/debug"
"strings"
"sync/atomic"
"time"
"github.com/google/uuid"
"github.com/hamba/avro/v2/ocf"
"github.com/PeerDB-io/peerdb/flow/connectors/utils"
"github.com/PeerDB-io/peerdb/flow/generated/protos"
"github.com/PeerDB-io/peerdb/flow/internal"
"github.com/PeerDB-io/peerdb/flow/internal/clickhouse"
"github.com/PeerDB-io/peerdb/flow/model"
peerdb_clickhouse "github.com/PeerDB-io/peerdb/flow/pkg/clickhouse"
"github.com/PeerDB-io/peerdb/flow/shared"
"github.com/PeerDB-io/peerdb/flow/shared/exceptions"
"github.com/PeerDB-io/peerdb/flow/shared/types"
)
type ClickHouseAvroSyncMethod struct {
*ClickHouseConnector
config *protos.QRepConfig
}
func NewClickHouseAvroSyncMethod(
config *protos.QRepConfig,
connector *ClickHouseConnector,
) *ClickHouseAvroSyncMethod {
return &ClickHouseAvroSyncMethod{
ClickHouseConnector: connector,
config: config,
}
}
func (s *ClickHouseAvroSyncMethod) CopyStageToDestination(ctx context.Context, avroFile utils.AvroFile) error {
stagingTableFunction, err := s.staging.TableFunctionExpr(ctx, avroFile.FilePath, stagingFormat)
if err != nil {
s.logger.Error("failed to build staging table function",
slog.String("avroFilePath", avroFile.FilePath),
slog.Any("error", err))
return fmt.Errorf("failed to build staging table function: %w", err)
}
query := fmt.Sprintf("INSERT INTO %s SELECT * FROM %s",
peerdb_clickhouse.QuoteIdentifier(s.config.DestinationTableIdentifier), stagingTableFunction)
return s.exec(ctx, query)
}
func (s *ClickHouseAvroSyncMethod) SyncRecords(
ctx context.Context,
env map[string]string,
stream *model.QRecordStream,
flowJobName string,
syncBatchID int64,
) (int64, error) {
dstTableName := s.config.DestinationTableIdentifier
schema, err := stream.Schema()
if err != nil {
return 0, err
}
s.logger.Info("sync function called and schema acquired",
slog.String("dstTable", dstTableName))
avroSchema, err := s.getAvroSchema(ctx, env, dstTableName, schema, nil)
if err != nil {
return 0, err
}
batchIdentifierForFile := fmt.Sprintf("%s_%d", shared.RandomString(16), syncBatchID)
avroFile, err := s.writeToAvroFile(ctx, env, stream, nil, avroSchema, batchIdentifierForFile, flowJobName, nil, nil)
if err != nil {
return 0, err
}
s.logger.Info("[SyncRecords] written records to Avro file",
slog.String("dstTable", dstTableName),
slog.String("avroFile", avroFile.FilePath),
slog.Int64("numRecords", avroFile.NumRecords),
slog.Int64("syncBatchID", syncBatchID))
if err := SetAvroStage(ctx, flowJobName, syncBatchID, avroFile); err != nil {
return 0, fmt.Errorf("failed to set avro stage: %w", err)
}
return avroFile.NumRecords, nil
}
func (s *ClickHouseAvroSyncMethod) SyncQRepRecords(
ctx context.Context,
config *protos.QRepConfig,
partition *protos.QRepPartition,
stream *model.QRecordStream,
) (int64, shared.QRepWarnings, error) {
dstTableName := config.DestinationTableIdentifier
startTime := time.Now()
schema, err := stream.Schema()
if err != nil {
return 0, nil, err
}
destTypeConversions := findTypeConversions(schema, config.Columns)
if len(destTypeConversions) > 0 {
schema = applyTypeConversions(schema, destTypeConversions)
}
numericTruncator := model.NewSnapshotTableNumericTruncator(dstTableName, schema.Fields)
columnNameAvroFieldMap := model.ConstructColumnNameAvroFieldMap(schema.Fields)
avroFiles, totalRecords, err := s.pushDataToStagingForSnapshot(ctx, config, dstTableName, schema,
columnNameAvroFieldMap, partition, stream, destTypeConversions, numericTruncator)
if err != nil {
s.logger.Error("failed to push data to S3",
slog.String("dstTable", dstTableName),
slog.Any("error", err))
return 0, nil, err
}
if err := s.pushStagingDataToClickHouseForSnapshot(
ctx, avroFiles, schema, columnNameAvroFieldMap, config); err != nil {
s.logger.Error("failed to push data to ClickHouse",
slog.String("dstTable", dstTableName),
slog.Any("error", err))
return 0, nil, err
}
warnings := numericTruncator.Warnings()
if err := s.FinishQRepPartition(ctx, partition, config.FlowJobName, startTime); err != nil {
s.logger.Error("Failed to finish QRep partition", slog.Any("error", err))
return 0, nil, err
}
return totalRecords, warnings, nil
}
func (s *ClickHouseAvroSyncMethod) pushDataToStagingForSnapshot(
ctx context.Context,
config *protos.QRepConfig,
dstTableName string,
schema types.QRecordSchema,
columnNameAvroFieldMap map[string]string,
partition *protos.QRepPartition,
stream *model.QRecordStream,
destTypeConversions map[string]types.TypeConversion,
numericTruncator model.SnapshotTableNumericTruncator,
) ([]utils.AvroFile, int64, error) {
avroSchema, err := s.getAvroSchema(ctx, config.Env, dstTableName, schema, columnNameAvroFieldMap)
if err != nil {
return nil, 0, err
}
bytesPerAvroFile, err := internal.PeerDBS3BytesPerAvroFile(ctx, config.Env)
if err != nil {
return nil, 0, err
}
s.logger.Info("writing avro chunks to S3 start",
slog.String("partitionId", partition.PartitionId),
slog.Int64("bytesPerAvroFile", bytesPerAvroFile))
// helper function to create a substream for splitting the main stream into chunks
createChunkedSubstream := func(done *atomic.Bool) (*model.QRecordStream, *model.QRecordAvroChunkSizeTracker) {
substream := model.NewQRecordStream(0)
substream.SetSchema(schema)
substream.SetSchemaDebug(stream.SchemaDebug())
sizeTracker := model.QRecordAvroChunkSizeTracker{}
go func() {
recordsDone := true
for record := range stream.Records {
substream.Records <- record
if sizeTracker.Bytes.Load() >= bytesPerAvroFile {
recordsDone = false
break
}
}
if recordsDone {
done.Store(true)
}
substream.Close(stream.Err())
}()
return substream, &sizeTracker
}
var avroFiles []utils.AvroFile
var totalRecords int64
if bytesPerAvroFile != 0 {
chunkNum := 0
var done atomic.Bool
for !done.Load() {
if err := ctx.Err(); err != nil {
return nil, 0, err
}
substream, sizeTracker := createChunkedSubstream(&done)
subFile, err := s.writeToAvroFile(ctx, config.Env, substream, sizeTracker, avroSchema,
fmt.Sprintf("%s.%06d", partition.PartitionId, chunkNum),
config.FlowJobName, destTypeConversions, numericTruncator,
)
if err != nil {
return nil, 0, err
}
avroFiles = append(avroFiles, subFile)
chunkNum += 1
totalRecords += subFile.NumRecords
}
if err := ctx.Err(); err != nil {
return nil, 0, err
}
} else {
avroFile, err := s.writeToAvroFile(
ctx, config.Env, stream, nil, avroSchema, partition.PartitionId, config.FlowJobName,
destTypeConversions, numericTruncator,
)
if err != nil {
return nil, 0, err
}
avroFiles = append(avroFiles, avroFile)
totalRecords = avroFile.NumRecords
}
s.logger.Info("finished writing avro chunks to S3",
slog.String("partitionId", partition.PartitionId),
slog.Int("totalChunks", len(avroFiles)),
slog.Int64("totalRecords", totalRecords))
return avroFiles, totalRecords, nil
}
func (s *ClickHouseAvroSyncMethod) pushStagingDataToClickHouseForSnapshot(
ctx context.Context,
avroFiles []utils.AvroFile,
schema types.QRecordSchema,
columnNameAvroFieldMap map[string]string,
config *protos.QRepConfig,
) error {
insertConfig := &insertFromTableFunctionConfig{
destinationTable: config.DestinationTableIdentifier,
schema: schema,
columnNameMap: columnNameAvroFieldMap,
excludedColumns: config.Exclude,
config: config,
connector: s.ClickHouseConnector,
logger: s.logger,
}
numParts, err := internal.PeerDBClickHouseInitialLoadPartsPerPartition(ctx, s.config.Env)
if err != nil {
s.logger.Warn("failed to get chunking parts, proceeding without chunking", slog.Any("error", err))
numParts = 1
}
numParts = max(numParts, 1)
chSettings := clickhouse.NewCHSettings(s.chVersion)
chSettings.Add(clickhouse.SettingThrowOnMaxPartitionsPerInsertBlock, "0")
chSettings.Add(clickhouse.SettingTypeJsonSkipDuplicatedPaths, "1")
if config.Version >= shared.InternalVersion_JsonEscapeDotsInKeys {
chSettings.Add(clickhouse.SettingJsonTypeEscapeDotsInKeys, "1")
}
// Process each chunk file individually
for chunkIdx, avroFile := range avroFiles {
s.logger.Info("processing chunk",
slog.Int("chunkIdx", chunkIdx),
slog.Int("totalChunks", len(avroFiles)),
slog.String("avroFilePath", avroFile.FilePath))
for i := range numParts {
// Get fresh credentials for each part
stagingTableFunction, err := s.staging.TableFunctionExpr(ctx, avroFile.FilePath, stagingFormat)
if err != nil {
s.logger.Error("failed to build staging table function",
slog.String("avroFilePath", avroFile.FilePath),
slog.Any("error", err),
slog.Uint64("part", i),
slog.Uint64("numParts", numParts),
slog.Int("chunkIdx", chunkIdx),
)
return fmt.Errorf("failed to build staging table function: %w", err)
}
var query string
if numParts > 1 {
query, err = buildInsertFromTableFunctionQueryWithPartitioning(
ctx, insertConfig, stagingTableFunction, i, numParts, chSettings)
} else {
query, err = buildInsertFromTableFunctionQuery(ctx, insertConfig, stagingTableFunction, chSettings)
}
if err != nil {
s.logger.Error("failed to build insert query",
slog.String("avroFilePath", avroFile.FilePath),
slog.Any("error", err),
slog.Uint64("part", i),
slog.Uint64("numParts", numParts),
slog.Int("chunkIdx", chunkIdx),
)
return fmt.Errorf("failed to build insert query: %w", err)
}
s.logger.Info("inserting part",
slog.Uint64("part", i),
slog.Uint64("numParts", numParts),
slog.Int("chunkIdx", chunkIdx),
slog.Int("totalChunks", len(avroFiles)))
if err := s.exec(ctx, query); err != nil {
s.logger.Error("failed to insert part",
slog.Uint64("part", i),
slog.Uint64("numParts", numParts),
slog.Int("chunkIdx", chunkIdx),
slog.Any("error", err))
return exceptions.NewClickHouseQRepSyncError(err, config.DestinationTableIdentifier, s.ClickHouseConnector.Config.Database)
}
s.logger.Info("inserted part",
slog.Uint64("part", i),
slog.Uint64("numParts", numParts),
slog.Int("chunkIdx", chunkIdx),
slog.Int("totalChunks", len(avroFiles)))
}
s.logger.Info("processed chunk",
slog.Int("chunkIdx", chunkIdx),
slog.Int("totalChunks", len(avroFiles)),
slog.String("avroFilePath", avroFile.FilePath))
}
return nil
}
func (s *ClickHouseAvroSyncMethod) getAvroSchema(
ctx context.Context,
env map[string]string,
dstTableName string,
schema types.QRecordSchema,
avroNameMap map[string]string,
) (*model.QRecordAvroSchemaDefinition, error) {
avroSchema, err := model.GetAvroSchemaDefinition(ctx, env, dstTableName, schema, protos.DBType_CLICKHOUSE, avroNameMap)
if err != nil {
return nil, fmt.Errorf("failed to define Avro schema: %w", err)
}
return avroSchema, nil
}
func (s *ClickHouseAvroSyncMethod) writeToAvroFile(
ctx context.Context,
env map[string]string,
stream *model.QRecordStream,
sizeTracker *model.QRecordAvroChunkSizeTracker,
avroSchema *model.QRecordAvroSchemaDefinition,
identifierForFile string,
flowJobName string,
typeConversions map[string]types.TypeConversion,
numericTruncator model.SnapshotTableNumericTruncator,
) (utils.AvroFile, error) {
ocfWriter := utils.NewPeerDBOCFWriter(stream, avroSchema, ocf.ZStandard, protos.DBType_CLICKHOUSE, sizeTracker)
prefix := s.staging.KeyPrefix()
s3UuidPrefix, err := internal.PeerDBS3UuidPrefix(ctx, s.config.Env)
if err != nil {
return utils.AvroFile{}, err
}
var stagingAvroFileKey string
if s3UuidPrefix {
stagingAvroFileKey = fmt.Sprintf("%s/%s/%s/%s.avro", prefix, uuid.NewString(), flowJobName, identifierForFile)
} else {
stagingAvroFileKey = fmt.Sprintf("%s/%s/%s.avro", prefix, flowJobName, identifierForFile)
}
stagingAvroFileKey = strings.TrimLeft(stagingAvroFileKey, "/")
r, w := io.Pipe()
defer r.Close()
var writeOcfError error
var numRows int64
go func() {
defer func() {
if r := recover(); r != nil {
writeOcfError = fmt.Errorf("panic occurred during WriteOCF: %v\n%s", r, debug.Stack())
}
w.Close()
}()
numRows, writeOcfError = ocfWriter.WriteOCF(ctx, env, w, typeConversions, numericTruncator)
}()
if err := s.staging.Upload(ctx, env, stagingAvroFileKey, r); err != nil {
return utils.AvroFile{}, fmt.Errorf("failed to upload to staging: %w", err)
}
if writeOcfError != nil {
return utils.AvroFile{}, writeOcfError
}
return utils.AvroFile{
StorageLocation: utils.AvroS3Storage,
FilePath: stagingAvroFileKey,
NumRecords: numRows,
}, nil
}
func (s *ClickHouseAvroSyncMethod) SyncQRepObjects(
ctx context.Context,
config *protos.QRepConfig,
partition *protos.QRepPartition,
stream *model.QObjectStream,
) (int64, shared.QRepWarnings, error) {
// Delegate to the ClickHouse connector's implementation
return s.ClickHouseConnector.SyncQRepObjects(ctx, config, partition, stream)
}