2015-06-01 15:49:10 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2015-06-04 10:33:12 +00:00
|
|
|
"fmt"
|
2016-02-17 19:47:02 +00:00
|
|
|
"math"
|
|
|
|
"math/big"
|
2015-06-05 22:14:08 +00:00
|
|
|
"math/rand"
|
2015-06-03 11:35:48 +00:00
|
|
|
"net"
|
2015-06-01 15:49:10 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-06-03 10:26:50 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2015-06-05 22:14:08 +00:00
|
|
|
"time"
|
2015-06-03 11:35:48 +00:00
|
|
|
|
2016-02-17 19:47:02 +00:00
|
|
|
crand "crypto/rand"
|
|
|
|
|
2015-06-03 11:35:48 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2015-06-01 15:49:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ensurePath is used to make sure a path exists
|
|
|
|
func ensurePath(path string, dir bool) error {
|
|
|
|
if !dir {
|
|
|
|
path = filepath.Dir(path)
|
|
|
|
}
|
|
|
|
return os.MkdirAll(path, 0755)
|
|
|
|
}
|
2015-06-03 10:26:50 +00:00
|
|
|
|
2015-08-20 22:29:30 +00:00
|
|
|
// RuntimeStats is used to return various runtime information
|
|
|
|
func RuntimeStats() map[string]string {
|
2015-06-03 10:26:50 +00:00
|
|
|
return map[string]string{
|
2015-08-28 08:30:47 +00:00
|
|
|
"kernel.name": runtime.GOOS,
|
|
|
|
"arch": runtime.GOARCH,
|
|
|
|
"version": runtime.Version(),
|
|
|
|
"max_procs": strconv.FormatInt(int64(runtime.GOMAXPROCS(0)), 10),
|
|
|
|
"goroutines": strconv.FormatInt(int64(runtime.NumGoroutine()), 10),
|
|
|
|
"cpu_count": strconv.FormatInt(int64(runtime.NumCPU()), 10),
|
2015-06-03 10:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-03 11:35:48 +00:00
|
|
|
|
|
|
|
// serverParts is used to return the parts of a server role
|
|
|
|
type serverParts struct {
|
|
|
|
Name string
|
|
|
|
Region string
|
|
|
|
Datacenter string
|
|
|
|
Port int
|
|
|
|
Bootstrap bool
|
|
|
|
Expect int
|
|
|
|
Version int
|
|
|
|
Addr net.Addr
|
|
|
|
}
|
|
|
|
|
2015-06-04 10:33:12 +00:00
|
|
|
func (s *serverParts) String() string {
|
|
|
|
return fmt.Sprintf("%s (Addr: %s) (DC: %s)",
|
|
|
|
s.Name, s.Addr, s.Datacenter)
|
|
|
|
}
|
|
|
|
|
2015-06-03 11:35:48 +00:00
|
|
|
// Returns if a member is a Nomad server. Returns a boolean,
|
|
|
|
// and a struct with the various important components
|
|
|
|
func isNomadServer(m serf.Member) (bool, *serverParts) {
|
|
|
|
if m.Tags["role"] != "nomad" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
region := m.Tags["region"]
|
|
|
|
datacenter := m.Tags["dc"]
|
|
|
|
_, bootstrap := m.Tags["bootstrap"]
|
|
|
|
|
|
|
|
expect := 0
|
|
|
|
expect_str, ok := m.Tags["expect"]
|
|
|
|
var err error
|
|
|
|
if ok {
|
|
|
|
expect, err = strconv.Atoi(expect_str)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
port_str := m.Tags["port"]
|
|
|
|
port, err := strconv.Atoi(port_str)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
vsn_str := m.Tags["vsn"]
|
|
|
|
vsn, err := strconv.Atoi(vsn_str)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := &net.TCPAddr{IP: m.Addr, Port: port}
|
|
|
|
parts := &serverParts{
|
|
|
|
Name: m.Name,
|
|
|
|
Region: region,
|
|
|
|
Datacenter: datacenter,
|
|
|
|
Port: port,
|
|
|
|
Bootstrap: bootstrap,
|
|
|
|
Expect: expect,
|
|
|
|
Addr: addr,
|
|
|
|
Version: vsn,
|
|
|
|
}
|
|
|
|
return true, parts
|
|
|
|
}
|
2015-06-05 22:14:08 +00:00
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|
2015-07-24 00:30:07 +00:00
|
|
|
|
|
|
|
// shuffleStrings randomly shuffles the list of strings
|
|
|
|
func shuffleStrings(list []string) {
|
|
|
|
for i := range list {
|
|
|
|
j := rand.Intn(i + 1)
|
|
|
|
list[i], list[j] = list[j], list[i]
|
|
|
|
}
|
|
|
|
}
|
2015-08-05 00:13:40 +00:00
|
|
|
|
|
|
|
// maxUint64 returns the maximum value
|
|
|
|
func maxUint64(a, b uint64) uint64 {
|
|
|
|
if a >= b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
2015-08-23 00:12:24 +00:00
|
|
|
|
|
|
|
// rateScaledInterval is used to choose an interval to perform an action in order
|
|
|
|
// to target an aggregate number of actions per second across the whole cluster.
|
|
|
|
func rateScaledInterval(rate float64, min time.Duration, n int) time.Duration {
|
|
|
|
interval := time.Duration(float64(time.Second) * float64(n) / rate)
|
|
|
|
if interval < min {
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
return interval
|
|
|
|
}
|
2016-02-17 19:47:02 +00:00
|
|
|
|
|
|
|
// seedRandom seeds the global random variable using a cryptographically random
|
|
|
|
// seed. It returns an error if determing the random seed fails.
|
|
|
|
func seedRandom() error {
|
|
|
|
n, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rand.Seed(n.Int64())
|
|
|
|
return nil
|
|
|
|
}
|