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

[v15] Extend hostid test backoff #49216

Merged
merged 1 commit into from
Nov 20, 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
13 changes: 12 additions & 1 deletion lib/utils/hostid/hostid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"

"github.com/gravitational/teleport/api/utils/retryutils"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/teleport/lib/utils/hostid"
)
Expand All @@ -53,7 +55,16 @@ func TestReadOrCreate(t *testing.T) {
i := i
wg.Go(func() error {
<-barrier
id, err := hostid.ReadOrCreateFile(dir)
id, err := hostid.ReadOrCreateFile(
dir,
hostid.WithBackoff(retryutils.RetryV2Config{
First: 50 * time.Millisecond,
Driver: retryutils.NewExponentialDriver(100 * time.Millisecond),
Max: 15 * time.Second,
Jitter: retryutils.NewFullJitter(),
}),
hostid.WithIterationLimit(10),
)
ids[i] = id
return err
})
Expand Down
51 changes: 40 additions & 11 deletions lib/utils/hostid/hostid_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,52 @@ func WriteFile(dataDir string, id string) error {
return nil
}

type options struct {
retryConfig retryutils.RetryV2Config
iterationLimit int
}

// WithBackoff overrides the default backoff configuration of
// [ReadOrCreateFile].
func WithBackoff(cfg retryutils.RetryV2Config) func(*options) {
return func(o *options) {
o.retryConfig = cfg
}
}

// WithIterationLimit overrides the default number of time
// [ReadOrCreateFile] will attempt to produce a hostid.
func WithIterationLimit(limit int) func(*options) {
return func(o *options) {
o.iterationLimit = limit
}
}

// ReadOrCreateFile looks for a hostid file in the data dir. If present,
// returns the UUID from it, otherwise generates one
func ReadOrCreateFile(dataDir string) (string, error) {
// returns the UUID from it, otherwise generates one.
func ReadOrCreateFile(dataDir string, opts ...func(*options)) (string, error) {
o := options{
retryConfig: retryutils.RetryV2Config{
First: 100 * time.Millisecond,
Driver: retryutils.NewLinearDriver(100 * time.Millisecond),
Max: time.Second,
Jitter: retryutils.NewFullJitter(),
},
iterationLimit: 3,
}

for _, opt := range opts {
opt(&o)
}

hostUUIDFileLock := GetPath(dataDir) + ".lock"
const iterationLimit = 3

backoff, err := retryutils.NewRetryV2(retryutils.RetryV2Config{
First: 100 * time.Millisecond,
Driver: retryutils.NewLinearDriver(100 * time.Millisecond),
Max: time.Second,
Jitter: retryutils.NewFullJitter(),
})

backoff, err := retryutils.NewRetryV2(o.retryConfig)
if err != nil {
return "", trace.Wrap(err)
}

for i := 0; i < iterationLimit; i++ {
for i := 0; i < o.iterationLimit; i++ {
if read, err := ReadFile(dataDir); err == nil {
return read, nil
} else if !trace.IsNotFound(err) {
Expand Down
Loading