Skip to content

Review handling of status code in HTTP requests #1549

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

Merged
merged 11 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 19 additions & 15 deletions internal/benchrunner/runners/rally/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ func (c *collector) stop() {
}

func (c *collector) collectMetricsBeforeRallyRun() {
_, err := c.esAPI.Indices.Refresh(c.esAPI.Indices.Refresh.WithIndex(c.datastream))
resp, err := c.esAPI.Indices.Refresh(c.esAPI.Indices.Refresh.WithIndex(c.datastream))
if err != nil {
logger.Errorf("unable to refresh data stream at the beginning of rally run")
logger.Errorf("unable to refresh data stream at the beginning of rally run: %s", err)
return
}
defer resp.Body.Close()
if resp.IsError() {
logger.Errorf("unable to refresh data stream at the beginning of rally run: %s", resp)
return
}

Expand Down Expand Up @@ -157,19 +162,13 @@ func (c *collector) publish(events [][]byte) {
logger.Errorf("error indexing event in metricstore: %w", err)
return
}

if resp.Body == nil {
logger.Errorf("empty index response body from metricstore: %w", err)
return
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
logger.Errorf("failed to read index response body from metricstore: %w", err)
}

resp.Body.Close()

if resp.StatusCode != 201 {
logger.Errorf("error indexing event in metricstore (%d): %s: %v", resp.StatusCode, resp.Status(), elasticsearch.NewError(body))
}
Expand All @@ -187,18 +186,18 @@ func (c *collector) createMetricsIndex() {

logger.Debugf("creating %s index in metricstore...", c.indexName())

createRes, err := c.metricsAPI.Indices.Create(
resp, err := c.metricsAPI.Indices.Create(
c.indexName(),
c.metricsAPI.Indices.Create.WithBody(reader),
)
if err != nil {
logger.Errorf("could not create index: %w", err)
return
}
createRes.Body.Close()
defer resp.Body.Close()

if createRes.IsError() {
logger.Errorf("got a response error while creating index")
if resp.IsError() {
logger.Errorf("got a response error while creating index: %s", resp)
}
}

Expand Down Expand Up @@ -287,9 +286,14 @@ func (c *collector) collectDiskUsage() map[string]ingest.DiskUsage {
}

func (c *collector) collectMetricsAfterRallyRun() {
_, err := c.esAPI.Indices.Refresh(c.esAPI.Indices.Refresh.WithIndex(c.datastream))
resp, err := c.esAPI.Indices.Refresh(c.esAPI.Indices.Refresh.WithIndex(c.datastream))
if err != nil {
logger.Errorf("unable to refresh data stream at the end of rally run")
logger.Errorf("unable to refresh data stream at the end of rally run: %s", err)
return
}
defer resp.Body.Close()
if resp.IsError() {
logger.Errorf("unable to refresh data stream at the end of rally run: %s", resp)
return
}

Expand Down
111 changes: 72 additions & 39 deletions internal/benchrunner/runners/rally/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -336,10 +337,17 @@ func (r *runner) collectAndSummarizeMetrics() (*metricsSummary, error) {

func (r *runner) deleteDataStreamDocs(dataStream string) error {
body := strings.NewReader(`{ "query": { "match_all": {} } }`)
_, err := r.options.ESAPI.DeleteByQuery([]string{dataStream}, body)
resp, err := r.options.ESAPI.DeleteByQuery([]string{dataStream}, body)
if err != nil {
return err
return fmt.Errorf("failed to delete data stream docs for data stream %s: %w", dataStream, err)
}
defer resp.Body.Close()

// Not found error is fine here, this means that data was already not there.
if resp.IsError() && resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("failed to delete data stream docs for data stream %s: %s", dataStream, resp.String())
}

return nil
}

Expand Down Expand Up @@ -665,6 +673,9 @@ func (r *runner) reindexData() error {
return fmt.Errorf("error getting mapping: %w", err)
}
defer mappingRes.Body.Close()
if mappingRes.IsError() {
return fmt.Errorf("error getting mapping: %s", mappingRes)
}

body, err := io.ReadAll(mappingRes.Body)
if err != nil {
Expand Down Expand Up @@ -709,7 +720,7 @@ func (r *runner) reindexData() error {
defer createRes.Body.Close()

if createRes.IsError() {
return errors.New("got a response error while creating index")
return fmt.Errorf("got a response error while creating index: %s", createRes)
}

bodyReader := strings.NewReader(`{"query":{"match_all":{}}}`)
Expand All @@ -725,21 +736,13 @@ func (r *runner) reindexData() error {
return fmt.Errorf("error executing search: %w", err)
}
defer res.Body.Close()

type searchRes struct {
Error *struct {
Reason string `json:"reson"`
} `json:"error"`
ScrollID string `json:"_scroll_id"`
Hits []struct {
ID string `json:"_id"`
Source map[string]interface{} `json:"_source"`
} `json:"hits"`
if res.IsError() {
return fmt.Errorf("error executing search: %s", res)
}

// Iterate through the search results using the Scroll API
for {
var sr searchRes
var sr searchResponse
if err := json.NewDecoder(res.Body).Decode(&sr); err != nil {
return fmt.Errorf("error decoding search response: %w", err)
}
Expand All @@ -752,40 +755,66 @@ func (r *runner) reindexData() error {
break
}

var bulkBodyBuilder strings.Builder
for _, hit := range sr.Hits {
bulkBodyBuilder.WriteString(fmt.Sprintf("{\"index\":{\"_index\":\"%s\",\"_id\":\"%s\"}}\n", indexName, hit.ID))
enriched := r.enrichEventWithBenchmarkMetadata(hit.Source)
src, err := json.Marshal(enriched)
if err != nil {
return fmt.Errorf("error decoding _source: %w", err)
}
bulkBodyBuilder.WriteString(fmt.Sprintf("%s\n", string(src)))
err := r.bulkMetrics(indexName, sr)
if err != nil {
return err
}
}

logger.Debugf("bulk request of %d events...", len(sr.Hits))
logger.Debug("reindexing operation finished")
return nil
}

type searchResponse struct {
Error *struct {
Reason string `json:"reson"`
} `json:"error"`
ScrollID string `json:"_scroll_id"`
Hits []struct {
ID string `json:"_id"`
Source map[string]interface{} `json:"_source"`
} `json:"hits"`
}

bulkRes, err := r.options.ESMetricsAPI.Bulk(strings.NewReader(bulkBodyBuilder.String()))
func (r *runner) bulkMetrics(indexName string, sr searchResponse) error {
var bulkBodyBuilder strings.Builder
for _, hit := range sr.Hits {
bulkBodyBuilder.WriteString(fmt.Sprintf("{\"index\":{\"_index\":\"%s\",\"_id\":\"%s\"}}\n", indexName, hit.ID))
enriched := r.enrichEventWithBenchmarkMetadata(hit.Source)
src, err := json.Marshal(enriched)
if err != nil {
return fmt.Errorf("error performing the bulk index request: %w", err)
return fmt.Errorf("error decoding _source: %w", err)
}
bulkRes.Body.Close()
bulkBodyBuilder.WriteString(fmt.Sprintf("%s\n", string(src)))
}

if sr.ScrollID == "" {
return errors.New("error getting scroll ID")
}
logger.Debugf("bulk request of %d events...", len(sr.Hits))

res, err = r.options.ESAPI.Scroll(
r.options.ESAPI.Scroll.WithScrollID(sr.ScrollID),
r.options.ESAPI.Scroll.WithScroll(time.Minute),
)
if err != nil {
return fmt.Errorf("error executing scroll: %s", err)
}
res.Body.Close()
resp, err := r.options.ESMetricsAPI.Bulk(strings.NewReader(bulkBodyBuilder.String()))
if err != nil {
return fmt.Errorf("error performing the bulk index request: %w", err)
}
defer resp.Body.Close()
if resp.IsError() {
return fmt.Errorf("error performing the bulk index request: %s", resp)
}

logger.Debug("reindexing operation finished")
if sr.ScrollID == "" {
return errors.New("error getting scroll ID")
}

resp, err = r.options.ESAPI.Scroll(
r.options.ESAPI.Scroll.WithScrollID(sr.ScrollID),
r.options.ESAPI.Scroll.WithScroll(time.Minute),
)
if err != nil {
return fmt.Errorf("error executing scroll: %s", err)
}
if resp.IsError() {
return fmt.Errorf("error executing scroll: %s", resp)
}
resp.Body.Close()

return nil
}

Expand Down Expand Up @@ -815,6 +844,10 @@ func getTotalHits(esapi *elasticsearch.API, dataStream string) (int, error) {
}
defer resp.Body.Close()

if resp.IsError() {
return 0, fmt.Errorf("failed to get hits count: %s", resp.String())
}

var results struct {
Count int
Error *struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/benchrunner/runners/system/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (c *collector) createMetricsIndex() {
createRes.Body.Close()

if createRes.IsError() {
logger.Debug("got a response error while creating index")
logger.Debug("got a response error while creating index: %s", createRes)
}
}

Expand Down
Loading