Skip to content

Commit

Permalink
RetryInterval should be a time.Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
chelseakomlo committed May 25, 2018
1 parent a4d07e8 commit 82f0a38
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 90 deletions.
10 changes: 9 additions & 1 deletion command/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ func (c *Command) readConfig() *Config {
}
}

// COMPAT: Remove in 0.10. Parse the RetryInterval
dur, err := time.ParseDuration(config.Server.RetryInterval)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing retry interval: %s", err))
return nil
}
config.Server.retryInterval = dur

// Check that the server is running in at least one mode.
if !(config.Server.Enabled || config.Client.Enabled) {
c.Ui.Error("Must specify either server, client or dev mode for the agent.")
Expand Down Expand Up @@ -558,7 +566,7 @@ func (c *Command) Run(args []string) int {
RetryJoin: config.Server.RetryJoin,
StartJoin: config.Server.StartJoin,
RetryMaxAttempts: config.Server.RetryMaxAttempts,
RetryInterval: config.Server.RetryInterval,
RetryInterval: config.Server.retryInterval,
}
go joiner.RetryJoin(serverJoinInfo)
}
Expand Down
5 changes: 2 additions & 3 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ type ServerJoin struct {
// RetryInterval specifies the amount of time to wait in between join
// attempts on agent start. The minimum allowed value is 1 second and
// the default is 30s.
RetryInterval string `mapstructure:"retry_interval"`
retryInterval time.Duration `mapstructure:"-"`
RetryInterval time.Duration `mapstructure:"retry_interval"`
}

func (s *ServerJoin) Merge(b *ServerJoin) {
Expand All @@ -391,7 +390,7 @@ func (s *ServerJoin) Merge(b *ServerJoin) {
if b.RetryMaxAttempts != 0 {
s.RetryMaxAttempts = b.RetryMaxAttempts
}
if b.RetryInterval != "" {
if b.RetryInterval != 0 {
s.RetryInterval = b.RetryInterval
}
}
Expand Down
4 changes: 2 additions & 2 deletions command/agent/config_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestConfig_Parse(t *testing.T) {
NodeClass: "linux-medium-64bit",
ServerJoin: &ServerJoin{
RetryJoin: []string{"1.1.1.1", "2.2.2.2"},
RetryInterval: "15s",
RetryInterval: time.Duration(15),
RetryMaxAttempts: 3,
},
Meta: map[string]string{
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestConfig_Parse(t *testing.T) {
ServerJoin: &ServerJoin{
RetryJoin: []string{"1.1.1.1", "2.2.2.2"},
StartJoin: []string{"1.1.1.1", "2.2.2.2"},
RetryInterval: "15s",
RetryInterval: time.Duration(15),
RetryMaxAttempts: 3,
},
},
Expand Down
12 changes: 6 additions & 6 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,13 +916,13 @@ func TestMergeServerJoin(t *testing.T) {
retryJoin := []string{"127.0.0.1", "127.0.0.2"}
startJoin := []string{"127.0.0.1", "127.0.0.2"}
retryMaxAttempts := 1
retryInterval := "1"
retryInterval := 0

a := &ServerJoin{
RetryJoin: retryJoin,
StartJoin: startJoin,
RetryMaxAttempts: retryMaxAttempts,
RetryInterval: retryInterval,
RetryInterval: time.Duration(retryInterval),
}
b := &ServerJoin{}

Expand All @@ -936,14 +936,14 @@ func TestMergeServerJoin(t *testing.T) {
retryJoin := []string{"127.0.0.1", "127.0.0.2"}
startJoin := []string{"127.0.0.1", "127.0.0.2"}
retryMaxAttempts := 1
retryInterval := "1"
retryInterval := 0

a := &ServerJoin{}
b := &ServerJoin{
RetryJoin: retryJoin,
StartJoin: startJoin,
RetryMaxAttempts: retryMaxAttempts,
RetryInterval: retryInterval,
RetryInterval: time.Duration(retryInterval),
}

a.Merge(b)
Expand All @@ -956,15 +956,15 @@ func TestMergeServerJoin(t *testing.T) {
retryJoin := []string{"127.0.0.1", "127.0.0.2"}
startJoin := []string{"127.0.0.1", "127.0.0.2"}
retryMaxAttempts := 1
retryInterval := "1"
retryInterval := 0

a := &ServerJoin{
RetryJoin: retryJoin,
StartJoin: startJoin,
}
b := &ServerJoin{
RetryMaxAttempts: retryMaxAttempts,
RetryInterval: retryInterval,
RetryInterval: time.Duration(retryInterval),
}

a.Merge(b)
Expand Down
29 changes: 1 addition & 28 deletions command/agent/retry_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,33 +82,6 @@ func (r *retryJoiner) Validate(config *Config) error {
}
}

if config.Server != nil {
dur, err := time.ParseDuration(config.Server.RetryInterval)
if err != nil {
return fmt.Errorf("Error parsing server retry interval: %s", err)
} else {
config.Server.retryInterval = dur
}

if config.Server.ServerJoin != nil {
dur, err := time.ParseDuration(config.Server.RetryInterval)
if err != nil {
return fmt.Errorf("Error parsing server retry interval: %s", err)
} else {
config.Server.ServerJoin.retryInterval = dur
}
}
}

if config.Client != nil && config.Client.ServerJoin != nil {
dur, err := time.ParseDuration(config.Client.ServerJoin.RetryInterval)
if err != nil {
return fmt.Errorf("Error parsing retry interval: %s", err)
} else {
config.Client.ServerJoin.retryInterval = dur
}
}

return nil
}

Expand Down Expand Up @@ -170,6 +143,6 @@ func (r *retryJoiner) RetryJoin(serverJoin *ServerJoin) {
r.logger.Printf("[WARN] agent: Join failed: %v, retrying in %v", err,
serverJoin.RetryInterval)
}
time.Sleep(serverJoin.retryInterval)
time.Sleep(serverJoin.RetryInterval)
}
}
62 changes: 12 additions & 50 deletions command/agent/retry_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"testing"
"time"

"github.com/hashicorp/nomad/testutil"
"github.com/hashicorp/nomad/version"
Expand Down Expand Up @@ -219,7 +220,7 @@ func TestRetryJoin_Validate(t *testing.T) {
ServerJoin: &ServerJoin{
RetryJoin: []string{"127.0.0.1"},
RetryMaxAttempts: 0,
RetryInterval: "0",
RetryInterval: 0,
StartJoin: []string{},
},
RetryJoin: []string{"127.0.0.1"},
Expand All @@ -237,7 +238,7 @@ func TestRetryJoin_Validate(t *testing.T) {
ServerJoin: &ServerJoin{
RetryJoin: []string{"127.0.0.1"},
RetryMaxAttempts: 0,
RetryInterval: "0",
RetryInterval: 0,
StartJoin: []string{},
},
StartJoin: []string{"127.0.0.1"},
Expand All @@ -255,7 +256,7 @@ func TestRetryJoin_Validate(t *testing.T) {
ServerJoin: &ServerJoin{
RetryJoin: []string{"127.0.0.1"},
RetryMaxAttempts: 0,
RetryInterval: "0",
RetryInterval: 0,
StartJoin: []string{},
},
StartJoin: []string{},
Expand All @@ -273,12 +274,13 @@ func TestRetryJoin_Validate(t *testing.T) {
ServerJoin: &ServerJoin{
RetryJoin: []string{"127.0.0.1"},
RetryMaxAttempts: 0,
RetryInterval: "0",
RetryInterval: time.Duration(1),
StartJoin: []string{},
},
StartJoin: []string{},
RetryMaxAttempts: 0,
RetryInterval: "1",
RetryInterval: "3s",
retryInterval: time.Duration(3),
RetryJoin: []string{},
},
},
Expand All @@ -291,7 +293,7 @@ func TestRetryJoin_Validate(t *testing.T) {
ServerJoin: &ServerJoin{
RetryJoin: []string{"127.0.0.1"},
RetryMaxAttempts: 0,
RetryInterval: "0",
RetryInterval: 0,
StartJoin: []string{"127.0.0.1"},
},
},
Expand All @@ -305,7 +307,7 @@ func TestRetryJoin_Validate(t *testing.T) {
ServerJoin: &ServerJoin{
RetryJoin: []string{},
RetryMaxAttempts: 0,
RetryInterval: "0",
RetryInterval: 0,
StartJoin: []string{"127.0.0.1"},
},
},
Expand All @@ -319,7 +321,7 @@ func TestRetryJoin_Validate(t *testing.T) {
ServerJoin: &ServerJoin{
RetryJoin: []string{"127.0.0.1"},
RetryMaxAttempts: 0,
RetryInterval: "0",
RetryInterval: 0,
},
},
},
Expand All @@ -332,7 +334,7 @@ func TestRetryJoin_Validate(t *testing.T) {
ServerJoin: &ServerJoin{
RetryJoin: []string{"127.0.0.1"},
RetryMaxAttempts: 0,
RetryInterval: "0",
RetryInterval: 0,
StartJoin: []string{},
},
StartJoin: []string{},
Expand All @@ -349,53 +351,13 @@ func TestRetryJoin_Validate(t *testing.T) {
Server: &ServerConfig{
StartJoin: []string{"127.0.0.1"},
RetryMaxAttempts: 1,
RetryInterval: "3s",
RetryInterval: "0",
RetryJoin: []string{},
},
},
isValid: true,
reason: "server deprecated retry_join configuration should be valid",
},
{
config: &Config{
Server: &ServerConfig{
StartJoin: []string{"127.0.0.1"},
RetryMaxAttempts: 1,
RetryInterval: "invalid!TimeInterval",
RetryJoin: []string{},
},
},
isValid: false,
reason: "invalid time interval",
},
{
config: &Config{
Server: &ServerConfig{
ServerJoin: &ServerJoin{
StartJoin: []string{"127.0.0.1"},
RetryMaxAttempts: 1,
RetryInterval: "invalid!TimeInterval",
RetryJoin: []string{},
},
},
},
isValid: false,
reason: "invalid time interval",
},
{
config: &Config{
Client: &ClientConfig{
ServerJoin: &ServerJoin{
StartJoin: []string{"127.0.0.1"},
RetryMaxAttempts: 1,
RetryInterval: "invalid!TimeInterval",
RetryJoin: []string{},
},
},
},
isValid: false,
reason: "invalid time interval",
},
}

joiner := retryJoiner{}
Expand Down

0 comments on commit 82f0a38

Please sign in to comment.