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

Add sleep to allow ES sufficient time for CCR #11172

Merged
merged 2 commits into from
Mar 13, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"net/http"
"os"
"testing"
"time"

"github.com/pkg/errors"

Expand Down Expand Up @@ -255,9 +256,48 @@ func createCCRStats(host string) error {
return errors.Wrap(err, "error creating CCR follower index")
}

// Give ES sufficient time to do the replication and produce stats
checkCCRStats := func() (bool, error) {
return checkCCRStatsExists(host)
}

exists, err := waitForSuccess(checkCCRStats, 200, 5)
if err != nil {
return errors.Wrap(err, "error checking if CCR stats exist")
}

if !exists {
return fmt.Errorf("expected to find CCR stats but not found")
}

return nil
}

func checkCCRStatsExists(host string) (bool, error) {
resp, err := http.Get("http://" + host + "/_ccr/stats")
if err != nil {
return false, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}

var data struct {
FollowStats struct {
Indices []map[string]interface{} `json:"indices"`
} `json:"follow_stats"`
}
err = json.Unmarshal(body, &data)
if err != nil {
return false, err
}

return len(data.FollowStats.Indices) > 0, nil
}

func setupCCRRemote(host string) error {
remoteSettings, err := ioutil.ReadFile("ccr/_meta/test/test_remote_settings.json")
if err != nil {
Expand Down Expand Up @@ -364,3 +404,23 @@ func httpPutJSON(host, path string, body []byte) ([]byte, *http.Response, error)

return body, resp, nil
}

type checkSuccessFunction func() (bool, error)

func waitForSuccess(f checkSuccessFunction, retryIntervalMs time.Duration, numAttempts int) (bool, error) {
for numAttempts > 0 {
success, err := f()
if err != nil {
return false, err
}

if success {
return success, nil
}

time.Sleep(retryIntervalMs * time.Millisecond)
numAttempts--
}

return false, nil
}