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
feat: clickhouse optimize S3 #4825
Open
makeavish
wants to merge
9
commits into
develop
Choose a base branch
from
feat/optimize-s3-archive
base: develop
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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.
+149
−1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
19d4c34
feat: clickhouseOptimizeS3
makeavish 8bc1864
Merge branch 'develop' into feat/optimize-s3-archive
prashant-shahi 982f453
Merge branch 'develop' into feat/optimize-s3-archive
prashant-shahi ce7fdac
Merge branch 'develop' into feat/optimize-s3-archive
makeavish cf8cc5d
Merge branch 'develop' into feat/optimize-s3-archive
makeavish 394b30b
chore: address review comments
makeavish b3665ab
Merge branch 'develop' into feat/optimize-s3-archive
prashant-shahi dab4ccc
Merge branch 'develop' into feat/optimize-s3-archive
prashant-shahi 5627172
Merge branch 'develop' into feat/optimize-s3-archive
prashant-shahi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,142 @@ | ||
package clickhouseOptimizeS3 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/ClickHouse/clickhouse-go/v2" | ||
"github.com/ClickHouse/clickhouse-go/v2/lib/driver" | ||
"github.com/go-co-op/gocron" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// General | ||
const ( | ||
CH_OPTIMIZE_INTERVAL_IN_HOURS = 24 | ||
CH_TIMEOUT_WAIT_IN_MINUTES = 30 | ||
S3_DISK_TYPE = "s3" | ||
) | ||
|
||
// Error message templates | ||
const ( | ||
ERROR_RUNNING_CRON_JOB = "error running ch optimize cron job" | ||
ERROR_SCHEDULING_CRON_JOB = "error scheduling cron job for %s" | ||
) | ||
|
||
func InitChOptimizer(cluster string) error { | ||
|
||
chConn, err := initClickhouse() | ||
if err != nil { | ||
zap.L().Error("failed to initialize ClickHouse", zap.Error(err)) | ||
return err | ||
} | ||
|
||
if s3Enabled, err := checkS3Enabled(chConn); err != nil { | ||
zap.L().Error("failed to check if S3 is enabled", zap.Error(err)) | ||
return err | ||
} else if !s3Enabled { | ||
zap.L().Info("S3 is not enabled, skipping clickhouse s3 optimization") | ||
return nil | ||
} | ||
|
||
err = runCronJobAsync(chConn, cluster) | ||
if err != nil { | ||
zap.L().Error(ERROR_RUNNING_CRON_JOB, zap.Error(err)) | ||
return err | ||
} | ||
|
||
zap.L().Info("clickhouseOptimizeS3 cron job started successfully") | ||
|
||
return nil | ||
} | ||
|
||
func initClickhouse() (driver.Conn, error) { | ||
datasource := os.Getenv("ClickHouseUrl") | ||
ctx := context.Background() | ||
options, err := clickhouse.ParseDSN(datasource) | ||
if err != nil { | ||
zap.L().Error("failed to parse DSN", zap.Error(err)) | ||
return nil, err | ||
} | ||
|
||
zap.L().Info("Connecting to Clickhouse", zap.String("at", options.Addr[0])) | ||
db, err := clickhouse.Open(options) | ||
if err != nil { | ||
zap.L().Error("failed to initialize ClickHouse", zap.Error(err)) | ||
return nil, err | ||
} | ||
|
||
if err := db.Ping(ctx); err != nil { | ||
zap.L().Error("failed to ping ClickHouse", zap.Error(err)) | ||
return nil, err | ||
} | ||
|
||
return db, nil | ||
} | ||
|
||
func runCronJobAsync(chConn driver.Conn, cluster string) error { | ||
var s *gocron.Scheduler | ||
var err error | ||
|
||
s = gocron.NewScheduler(time.UTC) | ||
_, err = s.Every(CH_OPTIMIZE_INTERVAL_IN_HOURS).Hour().Do(optimizeTables, chConn, cluster) | ||
if err != nil { | ||
return fmt.Errorf(ERROR_SCHEDULING_CRON_JOB, err) | ||
} | ||
|
||
s.StartAsync() | ||
|
||
return nil | ||
} | ||
|
||
func optimizeTables(conn driver.Conn, cluster string) { | ||
|
||
// Array of db_name.table_name | ||
tables := []string{ | ||
"signoz_logs.logs", | ||
"signoz_metrics.samples_v2", | ||
"signoz_metrics.time_series_v4", | ||
"signoz_metrics.time_series_v3", | ||
"signoz_metrics.time_series_v2", | ||
"signoz_traces.usage_explorer", | ||
"signoz_traces.span_attributes", | ||
"signoz_traces.dependency_graph_minutes", | ||
"signoz_traces.dependency_graph_minutes_v2", | ||
"signoz_traces.signoz_error_index_v2", | ||
"signoz_traces.signoz_index_v2", | ||
"signoz_traces.signoz_spans", | ||
"signoz_traces.durationSort", | ||
} | ||
for _, table := range tables { | ||
// run OPTIMIZE TABLE db_name.table_name ON CLUSTER cluster FINAL SETTINGS optimize_skip_merged_partitions=1; | ||
err := conn.Exec(context.Background(), "OPTIMIZE TABLE "+table+" ON CLUSTER "+cluster+" FINAL SETTINGS optimize_skip_merged_partitions=1;") | ||
|
||
if err != nil { | ||
if exception, ok := err.(*clickhouse.Exception); ok { | ||
if exception.Code == 159 { | ||
// sleep for CH_TIMEOUT_WAIT_IN_MINUTES if there's TIMEOUT_EXCEEDED - 159 error | ||
time.Sleep(CH_TIMEOUT_WAIT_IN_MINUTES * time.Minute) | ||
} else { | ||
log.Println("Error while optimizing table: ", table, err) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func checkS3Enabled(conn clickhouse.Conn) (bool, error) { | ||
var s3DiskCount uint64 | ||
ctx := context.Background() | ||
query := fmt.Sprintf("SELECT count() FROM system.disks where type='%v'", S3_DISK_TYPE) | ||
row := conn.QueryRow(ctx, query) | ||
if err := row.Scan(&s3DiskCount); err != nil { | ||
return false, err | ||
} | ||
if s3DiskCount > 0 { | ||
return true, nil | ||
} | ||
return false, nil | ||
} |
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
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.
this is not respected in case of slowdown