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

Stilgar: Add Clickhouse query level settings #1728

Merged
merged 1 commit into from
May 16, 2024
Merged
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
7 changes: 7 additions & 0 deletions flow/connectors/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ import (
"github.com/PeerDB-io/peer-flow/shared"
)

const (
PeerDBClickhouseQueryMaxMemoryUsage string = "64000000000"
PeerDBClickhouseMaxBlockSize string = "10240"
PeerDBClickhouseMaxInsertBlockSize string = "10240"
PeerDBClickhouseMaxInsertThreads string = "2"
)

type ClickhouseConnector struct {
*metadataStore.PostgresMetadata
database *sql.DB
Expand Down
19 changes: 17 additions & 2 deletions flow/connectors/clickhouse/qrep_avro_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"go.temporal.io/sdk/activity"

"github.com/ClickHouse/clickhouse-go/v2"
"github.com/PeerDB-io/peer-flow/connectors/utils"
avro "github.com/PeerDB-io/peer-flow/connectors/utils/avro"
"github.com/PeerDB-io/peer-flow/generated/protos"
Expand Down Expand Up @@ -52,12 +53,19 @@ func (s *ClickhouseAvroSyncMethod) CopyStageToDestination(ctx context.Context, a
if creds.AWS.SessionToken != "" {
sessionTokenPart = fmt.Sprintf(", '%s'", creds.AWS.SessionToken)
}

insertSelectQueryCtx := clickhouse.Context(ctx, clickhouse.WithSettings(clickhouse.Settings{
"max_memory_usage": PeerDBClickhouseQueryMaxMemoryUsage,
"max_block_size": PeerDBClickhouseMaxBlockSize,
"max_insert_block_size": PeerDBClickhouseMaxInsertBlockSize,
"max_insert_threads": PeerDBClickhouseMaxInsertThreads,
}))
//nolint:gosec
query := fmt.Sprintf("INSERT INTO `%s` SELECT * FROM s3('%s','%s','%s'%s, 'Avro')",
s.config.DestinationTableIdentifier, avroFileUrl,
creds.AWS.AccessKeyID, creds.AWS.SecretAccessKey, sessionTokenPart)

_, err = s.connector.database.ExecContext(ctx, query)
_, err = s.connector.database.ExecContext(insertSelectQueryCtx, query)

return err
}
Expand Down Expand Up @@ -147,12 +155,19 @@ func (s *ClickhouseAvroSyncMethod) SyncQRepRecords(
if creds.AWS.SessionToken != "" {
sessionTokenPart = fmt.Sprintf(", '%s'", creds.AWS.SessionToken)
}

insertSelectQueryCtx := clickhouse.Context(ctx, clickhouse.WithSettings(clickhouse.Settings{
"max_memory_usage": PeerDBClickhouseQueryMaxMemoryUsage,
"max_block_size": PeerDBClickhouseMaxBlockSize,
"max_insert_block_size": PeerDBClickhouseMaxInsertBlockSize,
"max_insert_threads": PeerDBClickhouseMaxInsertThreads,
}))
//nolint:gosec
query := fmt.Sprintf("INSERT INTO `%s`(%s) SELECT %s FROM s3('%s','%s','%s'%s, 'Avro')",
config.DestinationTableIdentifier, selectorStr, selectorStr, avroFileUrl,
creds.AWS.AccessKeyID, creds.AWS.SecretAccessKey, sessionTokenPart)

_, err = s.connector.database.ExecContext(ctx, query)
_, err = s.connector.database.ExecContext(insertSelectQueryCtx, query)
if err != nil {
s.connector.logger.Error("Failed to insert into select for Clickhouse: ", err)
return 0, err
Expand Down
Loading