2015-06-01 15:49:10 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-06-03 10:26:50 +00:00
|
|
|
"net"
|
2015-06-03 10:58:00 +00:00
|
|
|
"os"
|
2015-08-23 20:59:26 +00:00
|
|
|
"runtime"
|
2015-06-05 21:54:45 +00:00
|
|
|
"time"
|
2015-06-01 15:49:10 +00:00
|
|
|
|
2015-06-03 10:58:00 +00:00
|
|
|
"github.com/hashicorp/memberlist"
|
2015-08-07 00:04:35 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2016-06-14 05:58:39 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs/config"
|
2015-07-28 23:19:52 +00:00
|
|
|
"github.com/hashicorp/nomad/scheduler"
|
2015-06-01 15:49:10 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2015-06-03 10:58:00 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-09-14 01:18:40 +00:00
|
|
|
DefaultRegion = "global"
|
2015-06-03 10:58:00 +00:00
|
|
|
DefaultDC = "dc1"
|
2015-08-16 22:10:11 +00:00
|
|
|
DefaultSerfPort = 4648
|
2015-06-01 15:49:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// These are the protocol versions that Nomad can understand
|
|
|
|
const (
|
|
|
|
ProtocolVersionMin uint8 = 1
|
|
|
|
ProtocolVersionMax = 1
|
|
|
|
)
|
|
|
|
|
2015-06-03 10:58:00 +00:00
|
|
|
// ProtocolVersionMap is the mapping of Nomad protocol versions
|
|
|
|
// to Serf protocol versions. We mask the Serf protocols using
|
|
|
|
// our own protocol version.
|
|
|
|
var protocolVersionMap map[uint8]uint8
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
protocolVersionMap = map[uint8]uint8{
|
2015-09-11 18:22:36 +00:00
|
|
|
1: 4,
|
2015-06-03 10:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 10:26:50 +00:00
|
|
|
var (
|
2015-08-24 00:39:49 +00:00
|
|
|
DefaultRPCAddr = &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4647}
|
2015-06-03 10:26:50 +00:00
|
|
|
)
|
|
|
|
|
2015-06-01 15:49:10 +00:00
|
|
|
// Config is used to parameterize the server
|
|
|
|
type Config struct {
|
2016-05-22 01:06:35 +00:00
|
|
|
// Bootstrap mode is used to bring up the first Nomad server. It is
|
|
|
|
// required so that it can elect a leader without any other nodes
|
|
|
|
// being present
|
2015-06-01 15:49:10 +00:00
|
|
|
Bootstrap bool
|
|
|
|
|
2016-05-22 01:06:35 +00:00
|
|
|
// BootstrapExpect mode is used to automatically bring up a
|
|
|
|
// collection of Nomad servers. This can be used to automatically
|
2016-06-16 19:00:15 +00:00
|
|
|
// bring up a collection of nodes. All operations on BootstrapExpect
|
|
|
|
// must be handled via `atomic.*Int32()` calls.
|
|
|
|
BootstrapExpect int32
|
2015-06-03 10:58:00 +00:00
|
|
|
|
2015-06-01 15:49:10 +00:00
|
|
|
// DataDir is the directory to store our state in
|
|
|
|
DataDir string
|
|
|
|
|
2015-06-01 19:11:40 +00:00
|
|
|
// DevMode is used for development purposes only and limits the
|
|
|
|
// use of persistence or state.
|
|
|
|
DevMode bool
|
|
|
|
|
2015-06-05 21:54:45 +00:00
|
|
|
// DevDisableBootstrap is used to disable bootstrap mode while
|
|
|
|
// in DevMode. This is largely used for testing.
|
|
|
|
DevDisableBootstrap bool
|
|
|
|
|
2015-06-01 15:49:10 +00:00
|
|
|
// LogOutput is the location to write logs to. If this is not set,
|
|
|
|
// logs will go to stderr.
|
|
|
|
LogOutput io.Writer
|
|
|
|
|
|
|
|
// ProtocolVersion is the protocol version to speak. This must be between
|
|
|
|
// ProtocolVersionMin and ProtocolVersionMax.
|
|
|
|
ProtocolVersion uint8
|
|
|
|
|
2015-06-03 10:26:50 +00:00
|
|
|
// RPCAddr is the RPC address used by Nomad. This should be reachable
|
|
|
|
// by the other servers and clients
|
|
|
|
RPCAddr *net.TCPAddr
|
|
|
|
|
|
|
|
// RPCAdvertise is the address that is advertised to other nodes for
|
|
|
|
// the RPC endpoint. This can differ from the RPC address, if for example
|
|
|
|
// the RPCAddr is unspecified "0.0.0.0:4646", but this address must be
|
|
|
|
// reachable
|
|
|
|
RPCAdvertise *net.TCPAddr
|
|
|
|
|
2015-06-01 15:49:10 +00:00
|
|
|
// RaftConfig is the configuration used for Raft in the local DC
|
|
|
|
RaftConfig *raft.Config
|
2015-06-03 10:26:50 +00:00
|
|
|
|
2015-08-26 00:36:52 +00:00
|
|
|
// RaftTimeout is applied to any network traffic for raft. Defaults to 10s.
|
|
|
|
RaftTimeout time.Duration
|
|
|
|
|
2015-06-03 10:26:50 +00:00
|
|
|
// RequireTLS ensures that all RPC traffic is protected with TLS
|
|
|
|
RequireTLS bool
|
2015-06-03 10:58:00 +00:00
|
|
|
|
|
|
|
// SerfConfig is the configuration for the serf cluster
|
|
|
|
SerfConfig *serf.Config
|
|
|
|
|
|
|
|
// Node name is the name we use to advertise. Defaults to hostname.
|
|
|
|
NodeName string
|
|
|
|
|
|
|
|
// Region is the region this Nomad server belongs to.
|
|
|
|
Region string
|
|
|
|
|
|
|
|
// Datacenter is the datacenter this Nomad server belongs to.
|
|
|
|
Datacenter string
|
|
|
|
|
|
|
|
// Build is a string that is gossiped around, and can be used to help
|
|
|
|
// operators track which versions are actively deployed
|
|
|
|
Build string
|
2015-06-05 21:54:45 +00:00
|
|
|
|
2015-07-28 22:12:08 +00:00
|
|
|
// NumSchedulers is the number of scheduler thread that are run.
|
|
|
|
// This can be as many as one per core, or zero to disable this server
|
|
|
|
// from doing any scheduling work.
|
|
|
|
NumSchedulers int
|
|
|
|
|
|
|
|
// EnabledSchedulers controls the set of sub-schedulers that are
|
|
|
|
// enabled for this server to handle. This will restrict the evaluations
|
|
|
|
// that the workers dequeue for processing.
|
|
|
|
EnabledSchedulers []string
|
|
|
|
|
2015-06-05 21:54:45 +00:00
|
|
|
// ReconcileInterval controls how often we reconcile the strongly
|
|
|
|
// consistent store with the Serf info. This is used to handle nodes
|
|
|
|
// that are force removed, as well as intermittent unavailability during
|
|
|
|
// leader election.
|
|
|
|
ReconcileInterval time.Duration
|
2015-07-24 04:44:17 +00:00
|
|
|
|
2015-08-15 22:15:00 +00:00
|
|
|
// EvalGCInterval is how often we dispatch a job to GC evaluations
|
|
|
|
EvalGCInterval time.Duration
|
|
|
|
|
|
|
|
// EvalGCThreshold is how "old" an evaluation must be to be eligible
|
|
|
|
// for GC. This gives users some time to debug a failed evaluation.
|
|
|
|
EvalGCThreshold time.Duration
|
|
|
|
|
2015-12-15 03:20:57 +00:00
|
|
|
// JobGCInterval is how often we dispatch a job to GC jobs that are
|
|
|
|
// available for garbage collection.
|
|
|
|
JobGCInterval time.Duration
|
|
|
|
|
2015-12-15 21:07:25 +00:00
|
|
|
// JobGCThreshold is how old a job must be before it eligible for GC. This gives
|
|
|
|
// the user time to inspect the job.
|
|
|
|
JobGCThreshold time.Duration
|
|
|
|
|
2015-09-07 18:01:29 +00:00
|
|
|
// NodeGCInterval is how often we dispatch a job to GC failed nodes.
|
|
|
|
NodeGCInterval time.Duration
|
|
|
|
|
|
|
|
// NodeGCThreshold is how "old" a nodemust be to be eligible
|
|
|
|
// for GC. This gives users some time to view and debug a failed nodes.
|
|
|
|
NodeGCThreshold time.Duration
|
|
|
|
|
2015-07-24 04:44:17 +00:00
|
|
|
// EvalNackTimeout controls how long we allow a sub-scheduler to
|
|
|
|
// work on an evaluation before we consider it failed and Nack it.
|
|
|
|
// This allows that evaluation to be handed to another sub-scheduler
|
|
|
|
// to work on. Defaults to 60 seconds. This should be long enough that
|
|
|
|
// no evaluation hits it unless the sub-scheduler has failed.
|
|
|
|
EvalNackTimeout time.Duration
|
2015-08-16 17:55:55 +00:00
|
|
|
|
|
|
|
// EvalDeliveryLimit is the limit of attempts we make to deliver and
|
|
|
|
// process an evaluation. This is used so that an eval that will never
|
|
|
|
// complete eventually fails out of the system.
|
|
|
|
EvalDeliveryLimit int
|
2015-08-29 21:14:19 +00:00
|
|
|
|
|
|
|
// MinHeartbeatTTL is the minimum time between heartbeats.
|
|
|
|
// This is used as a floor to prevent excessive updates.
|
|
|
|
MinHeartbeatTTL time.Duration
|
|
|
|
|
|
|
|
// MaxHeartbeatsPerSecond is the maximum target rate of heartbeats
|
|
|
|
// being processed per second. This allows the TTL to be increased
|
|
|
|
// to meet the target rate.
|
|
|
|
MaxHeartbeatsPerSecond float64
|
|
|
|
|
|
|
|
// HeartbeatGrace is the additional time given as a grace period
|
|
|
|
// beyond the TTL to account for network and processing delays
|
|
|
|
// as well as clock skew.
|
|
|
|
HeartbeatGrace time.Duration
|
|
|
|
|
|
|
|
// FailoverHeartbeatTTL is the TTL applied to heartbeats after
|
|
|
|
// a new leader is elected, since we no longer know the status
|
|
|
|
// of all the heartbeats.
|
|
|
|
FailoverHeartbeatTTL time.Duration
|
2016-06-14 05:58:39 +00:00
|
|
|
|
|
|
|
// ConsulConfig is this Agent's Consul configuration
|
|
|
|
ConsulConfig *config.ConsulConfig
|
2016-07-10 17:35:48 +00:00
|
|
|
|
|
|
|
// RPCHoldTimeout is how long an RPC can be "held" before it is errored.
|
|
|
|
// This is used to paper over a loss of leadership by instead holding RPCs,
|
|
|
|
// so that the caller experiences a slow response rather than an error.
|
|
|
|
// This period is meant to be long enough for a leader election to take
|
|
|
|
// place, and a small jitter is applied to avoid a thundering herd.
|
|
|
|
RPCHoldTimeout time.Duration
|
2015-06-01 15:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckVersion is used to check if the ProtocolVersion is valid
|
|
|
|
func (c *Config) CheckVersion() error {
|
|
|
|
if c.ProtocolVersion < ProtocolVersionMin {
|
|
|
|
return fmt.Errorf("Protocol version '%d' too low. Must be in range: [%d, %d]",
|
|
|
|
c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax)
|
|
|
|
} else if c.ProtocolVersion > ProtocolVersionMax {
|
|
|
|
return fmt.Errorf("Protocol version '%d' too high. Must be in range: [%d, %d]",
|
|
|
|
c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultConfig returns the default configuration
|
|
|
|
func DefaultConfig() *Config {
|
2015-06-03 10:58:00 +00:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2015-06-01 15:49:10 +00:00
|
|
|
c := &Config{
|
2015-08-29 21:14:19 +00:00
|
|
|
Region: DefaultRegion,
|
|
|
|
Datacenter: DefaultDC,
|
|
|
|
NodeName: hostname,
|
|
|
|
ProtocolVersion: ProtocolVersionMax,
|
|
|
|
RaftConfig: raft.DefaultConfig(),
|
|
|
|
RaftTimeout: 10 * time.Second,
|
2016-06-17 13:44:10 +00:00
|
|
|
LogOutput: os.Stderr,
|
2015-08-29 21:14:19 +00:00
|
|
|
RPCAddr: DefaultRPCAddr,
|
|
|
|
SerfConfig: serf.DefaultConfig(),
|
|
|
|
NumSchedulers: 1,
|
|
|
|
ReconcileInterval: 60 * time.Second,
|
2015-09-07 18:01:29 +00:00
|
|
|
EvalGCInterval: 5 * time.Minute,
|
2015-08-29 21:14:19 +00:00
|
|
|
EvalGCThreshold: 1 * time.Hour,
|
2015-12-15 03:20:57 +00:00
|
|
|
JobGCInterval: 5 * time.Minute,
|
2015-12-15 21:07:25 +00:00
|
|
|
JobGCThreshold: 4 * time.Hour,
|
2015-09-07 18:01:29 +00:00
|
|
|
NodeGCInterval: 5 * time.Minute,
|
|
|
|
NodeGCThreshold: 24 * time.Hour,
|
2015-08-29 21:14:19 +00:00
|
|
|
EvalNackTimeout: 60 * time.Second,
|
|
|
|
EvalDeliveryLimit: 3,
|
|
|
|
MinHeartbeatTTL: 10 * time.Second,
|
|
|
|
MaxHeartbeatsPerSecond: 50.0,
|
|
|
|
HeartbeatGrace: 10 * time.Second,
|
|
|
|
FailoverHeartbeatTTL: 300 * time.Second,
|
2016-06-17 03:41:05 +00:00
|
|
|
ConsulConfig: config.DefaultConsulConfig(),
|
2016-07-10 17:35:48 +00:00
|
|
|
RPCHoldTimeout: 5 * time.Second,
|
2015-06-01 15:49:10 +00:00
|
|
|
}
|
2015-06-03 10:58:00 +00:00
|
|
|
|
2015-07-28 23:19:52 +00:00
|
|
|
// Enable all known schedulers by default
|
|
|
|
c.EnabledSchedulers = make([]string, 0, len(scheduler.BuiltinSchedulers))
|
|
|
|
for name := range scheduler.BuiltinSchedulers {
|
|
|
|
c.EnabledSchedulers = append(c.EnabledSchedulers, name)
|
2015-07-28 22:12:08 +00:00
|
|
|
}
|
2015-08-15 19:38:58 +00:00
|
|
|
c.EnabledSchedulers = append(c.EnabledSchedulers, structs.JobTypeCore)
|
2015-07-28 22:12:08 +00:00
|
|
|
|
2015-08-23 20:59:26 +00:00
|
|
|
// Default the number of schedulers to match the coores
|
|
|
|
c.NumSchedulers = runtime.NumCPU()
|
|
|
|
|
2015-06-05 22:32:28 +00:00
|
|
|
// Increase our reap interval to 3 days instead of 24h.
|
|
|
|
c.SerfConfig.ReconnectTimeout = 3 * 24 * time.Hour
|
|
|
|
|
2015-06-03 10:58:00 +00:00
|
|
|
// Serf should use the WAN timing, since we are using it
|
|
|
|
// to communicate between DC's
|
|
|
|
c.SerfConfig.MemberlistConfig = memberlist.DefaultWANConfig()
|
|
|
|
c.SerfConfig.MemberlistConfig.BindPort = DefaultSerfPort
|
|
|
|
|
|
|
|
// Disable shutdown on removal
|
|
|
|
c.RaftConfig.ShutdownOnRemove = false
|
2015-06-01 15:49:10 +00:00
|
|
|
return c
|
|
|
|
}
|