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

[CLD-7290] Add retry mechanism for Thanos queries to mitigate random network connectivity issues #31

Merged
merged 1 commit into from
Mar 14, 2024
Merged
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
33 changes: 24 additions & 9 deletions internal/elrond/elrond_soak.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,36 @@ func querySLOMetrics(ring *model.Ring, url string, queryTime time.Time, logger *
}

v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var results []pmodel.Vector

for _, installationGroup := range ring.InstallationGroups {
query := fmt.Sprintf("((slo:sli_error:ratio_rate5m{slo_service='%[1]s-ring-%[2]s'} > (14.4 * 0.005)) and ignoring(slo_window)(slo:sli_error:ratio_rate1h{slo_service='%[1]s-ring-%[2]s'} > (14.4 * 0.005))) or ignoring(slo_window)((slo:sli_error:ratio_rate30m{slo_service='%[1]s-ring-%[2]s'} > (6 * 0.005)) and ignoring(slo_window)(slo:sli_error:ratio_rate6h{slo_service='%[1]s-ring-%[2]s'} > (3.3 * 0.005))) or vector(0)", installationGroup.Name, installationGroup.ProvisionerGroupID)
logger.Infof("Running Thanos query %s", query)
result, warnings, err := v1api.Query(ctx, query, queryTime)
if err != nil {
return nil, errors.Wrap(err, "failed to query")
var lastErr error
// Retry mechanism for Thanos network connectivity issues.
for attempt := 0; attempt < 10; attempt++ {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
logger.Infof("Running Thanos query %s, attempt %d", query, attempt+1)
result, warnings, err := v1api.Query(ctx, query, queryTime)
cancel()

if err == nil {
if len(warnings) > 0 {
logger.Warnf("Encountered warnings obtaining metrics: %s", strings.Join(warnings, ", "))
}
results = append(results, result.(pmodel.Vector))
break
}

lastErr = err
logger.Warnf("Query failed: %v", err)
if attempt+1 < 10 {
time.Sleep(time.Second * time.Duration(2<<attempt)) // Exponential backoff
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

}
}

if len(warnings) > 0 {
return nil, errors.Errorf("encounted warnings obtaining metrics: %s", strings.Join(warnings, ", "))
if lastErr != nil {
return nil, errors.Wrap(lastErr, "failed to query after retries")
}
results = append(results, result.(pmodel.Vector))
}

return results, nil
Expand Down
Loading