mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
Use consul/lib's RandomStagger
Removes four redundant copies of the method in the process.
This commit is contained in:
@@ -13,8 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/armon/go-metrics"
|
||||
"github.com/mitchellh/hashstructure"
|
||||
|
||||
"github.com/hashicorp/consul/lib"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/hashicorp/nomad/client/allocdir"
|
||||
"github.com/hashicorp/nomad/client/config"
|
||||
@@ -24,6 +23,7 @@ import (
|
||||
"github.com/hashicorp/nomad/client/stats"
|
||||
"github.com/hashicorp/nomad/nomad"
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
"github.com/mitchellh/hashstructure"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -785,7 +785,7 @@ func (c *Client) retryIntv(base time.Duration) time.Duration {
|
||||
if c.config.DevMode {
|
||||
return devModeRetryIntv
|
||||
}
|
||||
return base + randomStagger(base)
|
||||
return base + lib.RandomStagger(base)
|
||||
}
|
||||
|
||||
// registerAndHeartbeat is a long lived goroutine used to register the client
|
||||
@@ -804,7 +804,7 @@ func (c *Client) registerAndHeartbeat() {
|
||||
if c.config.DevMode {
|
||||
heartbeat = time.After(0)
|
||||
} else {
|
||||
heartbeat = time.After(randomStagger(initialHeartbeatStagger))
|
||||
heartbeat = time.After(lib.RandomStagger(initialHeartbeatStagger))
|
||||
}
|
||||
|
||||
for {
|
||||
|
||||
@@ -2,10 +2,10 @@ package consul
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/consul/lib"
|
||||
cstructs "github.com/hashicorp/nomad/client/driver/structs"
|
||||
)
|
||||
|
||||
@@ -60,7 +60,7 @@ func (r *CheckRunner) Stop() {
|
||||
// run is invoked by a goroutine to run until Stop() is called
|
||||
func (r *CheckRunner) run() {
|
||||
// Get the randomized initial pause time
|
||||
initialPauseTime := randomStagger(r.check.Interval())
|
||||
initialPauseTime := lib.RandomStagger(r.check.Interval())
|
||||
r.logger.Printf("[DEBUG] agent: pausing %v before first invocation of %s", initialPauseTime, r.check.ID())
|
||||
next := time.NewTimer(initialPauseTime)
|
||||
for {
|
||||
@@ -82,8 +82,3 @@ type Check interface {
|
||||
Interval() time.Duration
|
||||
Timeout() time.Duration
|
||||
}
|
||||
|
||||
// Returns a random stagger interval between 0 and the duration
|
||||
func randomStagger(intv time.Duration) time.Duration {
|
||||
return time.Duration(uint64(rand.Int63()) % uint64(intv))
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
)
|
||||
@@ -69,11 +68,6 @@ func diffAllocs(existing []*structs.Allocation, allocs *allocUpdates) *diffResul
|
||||
return result
|
||||
}
|
||||
|
||||
// Returns a random stagger interval between 0 and the duration
|
||||
func randomStagger(intv time.Duration) time.Duration {
|
||||
return time.Duration(uint64(rand.Int63()) % uint64(intv))
|
||||
}
|
||||
|
||||
// shuffleStrings randomly shuffles the list of strings
|
||||
func shuffleStrings(list []string) {
|
||||
for i := range list {
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/nomad/nomad/mock"
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
@@ -56,17 +55,6 @@ func TestDiffAllocs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomStagger(t *testing.T) {
|
||||
t.Parallel()
|
||||
intv := time.Minute
|
||||
for i := 0; i < 10; i++ {
|
||||
stagger := randomStagger(intv)
|
||||
if stagger < 0 || stagger >= intv {
|
||||
t.Fatalf("Bad: %v", stagger)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestShuffleStrings(t *testing.T) {
|
||||
t.Parallel()
|
||||
// Generate input
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/armon/go-metrics"
|
||||
"github.com/hashicorp/consul/lib"
|
||||
"github.com/hashicorp/go-checkpoint"
|
||||
"github.com/hashicorp/go-syslog"
|
||||
"github.com/hashicorp/logutils"
|
||||
@@ -334,7 +335,7 @@ func (c *Command) setupAgent(config *Config, logOutput io.Writer) error {
|
||||
|
||||
// Do an immediate check within the next 30 seconds
|
||||
go func() {
|
||||
time.Sleep(randomStagger(30 * time.Second))
|
||||
time.Sleep(lib.RandomStagger(30 * time.Second))
|
||||
c.checkpointResults(checkpoint.Check(updateParams))
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -2,16 +2,9 @@ package agent
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Returns a random stagger interval between 0 and the duration
|
||||
func randomStagger(intv time.Duration) time.Duration {
|
||||
return time.Duration(uint64(rand.Int63()) % uint64(intv))
|
||||
}
|
||||
|
||||
// IpOfDevice returns a routable ip addr of a device
|
||||
func ipOfDevice(name string) (net.IP, error) {
|
||||
intf, err := net.InterfaceByName(name)
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRandomStagger(t *testing.T) {
|
||||
intv := time.Minute
|
||||
for i := 0; i < 10; i++ {
|
||||
stagger := randomStagger(intv)
|
||||
if stagger < 0 || stagger >= intv {
|
||||
t.Fatalf("Bad: %v", stagger)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ func (s *Server) resetHeartbeatTimer(id string) (time.Duration, error) {
|
||||
// Compute the target TTL value
|
||||
n := len(s.heartbeatTimers)
|
||||
ttl := lib.RateScaledInterval(s.config.MaxHeartbeatsPerSecond, s.config.MinHeartbeatTTL, n)
|
||||
ttl += randomStagger(ttl)
|
||||
ttl += lib.RandomStagger(ttl)
|
||||
|
||||
// Reset the TTL
|
||||
s.resetHeartbeatTimerLocked(id, ttl+s.config.HeartbeatGrace)
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/armon/go-metrics"
|
||||
"github.com/hashicorp/consul/lib"
|
||||
"github.com/hashicorp/net-rpc-msgpackrpc"
|
||||
"github.com/hashicorp/nomad/nomad/state"
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
@@ -308,7 +309,7 @@ func (s *Server) blockingRPC(opts *blockingOptions) error {
|
||||
}
|
||||
|
||||
// Apply a small amount of jitter to the request
|
||||
opts.queryOpts.MaxQueryTime += randomStagger(opts.queryOpts.MaxQueryTime / jitterFraction)
|
||||
opts.queryOpts.MaxQueryTime += lib.RandomStagger(opts.queryOpts.MaxQueryTime / jitterFraction)
|
||||
|
||||
// Setup a query timeout
|
||||
timeout = time.NewTimer(opts.queryOpts.MaxQueryTime)
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
crand "crypto/rand"
|
||||
|
||||
@@ -101,11 +100,6 @@ func isNomadServer(m serf.Member) (bool, *serverParts) {
|
||||
return true, parts
|
||||
}
|
||||
|
||||
// Returns a random stagger interval between 0 and the duration
|
||||
func randomStagger(intv time.Duration) time.Duration {
|
||||
return time.Duration(uint64(rand.Int63()) % uint64(intv))
|
||||
}
|
||||
|
||||
// shuffleStrings randomly shuffles the list of strings
|
||||
func shuffleStrings(list []string) {
|
||||
for i := range list {
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
"github.com/hashicorp/serf/serf"
|
||||
@@ -57,16 +56,6 @@ func TestIsNomadServer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomStagger(t *testing.T) {
|
||||
intv := time.Minute
|
||||
for i := 0; i < 10; i++ {
|
||||
stagger := randomStagger(intv)
|
||||
if stagger < 0 || stagger >= intv {
|
||||
t.Fatalf("Bad: %v", stagger)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestShuffleStrings(t *testing.T) {
|
||||
// Generate input
|
||||
inp := make([]string, 10)
|
||||
|
||||
Reference in New Issue
Block a user