github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/nomad/config.go (about)

     1  package nomad
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net"
     7  	"os"
     8  	"runtime"
     9  	"time"
    10  
    11  	"github.com/hashicorp/memberlist"
    12  	"github.com/hashicorp/nomad/helper/uuid"
    13  	"github.com/hashicorp/nomad/nomad/structs"
    14  	"github.com/hashicorp/nomad/nomad/structs/config"
    15  	"github.com/hashicorp/nomad/scheduler"
    16  	"github.com/hashicorp/raft"
    17  	"github.com/hashicorp/serf/serf"
    18  )
    19  
    20  const (
    21  	DefaultRegion   = "global"
    22  	DefaultDC       = "dc1"
    23  	DefaultSerfPort = 4648
    24  )
    25  
    26  // These are the protocol versions that Nomad can understand
    27  const (
    28  	ProtocolVersionMin uint8 = 1
    29  	ProtocolVersionMax       = 1
    30  )
    31  
    32  // ProtocolVersionMap is the mapping of Nomad protocol versions
    33  // to Serf protocol versions. We mask the Serf protocols using
    34  // our own protocol version.
    35  var protocolVersionMap map[uint8]uint8
    36  
    37  func init() {
    38  	protocolVersionMap = map[uint8]uint8{
    39  		1: 4,
    40  	}
    41  }
    42  
    43  var (
    44  	DefaultRPCAddr = &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4647}
    45  )
    46  
    47  // Config is used to parameterize the server
    48  type Config struct {
    49  	// Bootstrap mode is used to bring up the first Nomad server.  It is
    50  	// required so that it can elect a leader without any other nodes
    51  	// being present
    52  	Bootstrap bool
    53  
    54  	// BootstrapExpect mode is used to automatically bring up a
    55  	// collection of Nomad servers. This can be used to automatically
    56  	// bring up a collection of nodes.  All operations on BootstrapExpect
    57  	// must be handled via `atomic.*Int32()` calls.
    58  	BootstrapExpect int32
    59  
    60  	// DataDir is the directory to store our state in
    61  	DataDir string
    62  
    63  	// DevMode is used for development purposes only and limits the
    64  	// use of persistence or state.
    65  	DevMode bool
    66  
    67  	// DevDisableBootstrap is used to disable bootstrap mode while
    68  	// in DevMode. This is largely used for testing.
    69  	DevDisableBootstrap bool
    70  
    71  	// LogOutput is the location to write logs to. If this is not set,
    72  	// logs will go to stderr.
    73  	LogOutput io.Writer
    74  
    75  	// ProtocolVersion is the protocol version to speak. This must be between
    76  	// ProtocolVersionMin and ProtocolVersionMax.
    77  	ProtocolVersion uint8
    78  
    79  	// RPCAddr is the RPC address used by Nomad. This should be reachable
    80  	// by the other servers and clients
    81  	RPCAddr *net.TCPAddr
    82  
    83  	// ClientRPCAdvertise is the address that is advertised to client nodes for
    84  	// the RPC endpoint. This can differ from the RPC address, if for example
    85  	// the RPCAddr is unspecified "0.0.0.0:4646", but this address must be
    86  	// reachable
    87  	ClientRPCAdvertise *net.TCPAddr
    88  
    89  	// ServerRPCAdvertise is the address that is advertised to other servers for
    90  	// the RPC endpoint. This can differ from the RPC address, if for example
    91  	// the RPCAddr is unspecified "0.0.0.0:4646", but this address must be
    92  	// reachable
    93  	ServerRPCAdvertise *net.TCPAddr
    94  
    95  	// RaftConfig is the configuration used for Raft in the local DC
    96  	RaftConfig *raft.Config
    97  
    98  	// RaftTimeout is applied to any network traffic for raft. Defaults to 10s.
    99  	RaftTimeout time.Duration
   100  
   101  	// (Enterprise-only) NonVoter is used to prevent this server from being added
   102  	// as a voting member of the Raft cluster.
   103  	NonVoter bool
   104  
   105  	// (Enterprise-only) RedundancyZone is the redundancy zone to use for this server.
   106  	RedundancyZone string
   107  
   108  	// (Enterprise-only) UpgradeVersion is the custom upgrade version to use when
   109  	// performing upgrade migrations.
   110  	UpgradeVersion string
   111  
   112  	// SerfConfig is the configuration for the serf cluster
   113  	SerfConfig *serf.Config
   114  
   115  	// Node name is the name we use to advertise. Defaults to hostname.
   116  	NodeName string
   117  
   118  	// NodeID is the uuid of this server.
   119  	NodeID string
   120  
   121  	// Region is the region this Nomad server belongs to.
   122  	Region string
   123  
   124  	// AuthoritativeRegion is the region which is treated as the authoritative source
   125  	// for ACLs and Policies. This provides a single source of truth to resolve conflicts.
   126  	AuthoritativeRegion string
   127  
   128  	// Datacenter is the datacenter this Nomad server belongs to.
   129  	Datacenter string
   130  
   131  	// Build is a string that is gossiped around, and can be used to help
   132  	// operators track which versions are actively deployed
   133  	Build string
   134  
   135  	// NumSchedulers is the number of scheduler thread that are run.
   136  	// This can be as many as one per core, or zero to disable this server
   137  	// from doing any scheduling work.
   138  	NumSchedulers int
   139  
   140  	// EnabledSchedulers controls the set of sub-schedulers that are
   141  	// enabled for this server to handle. This will restrict the evaluations
   142  	// that the workers dequeue for processing.
   143  	EnabledSchedulers []string
   144  
   145  	// ReconcileInterval controls how often we reconcile the strongly
   146  	// consistent store with the Serf info. This is used to handle nodes
   147  	// that are force removed, as well as intermittent unavailability during
   148  	// leader election.
   149  	ReconcileInterval time.Duration
   150  
   151  	// EvalGCInterval is how often we dispatch a job to GC evaluations
   152  	EvalGCInterval time.Duration
   153  
   154  	// EvalGCThreshold is how "old" an evaluation must be to be eligible
   155  	// for GC. This gives users some time to debug a failed evaluation.
   156  	EvalGCThreshold time.Duration
   157  
   158  	// JobGCInterval is how often we dispatch a job to GC jobs that are
   159  	// available for garbage collection.
   160  	JobGCInterval time.Duration
   161  
   162  	// JobGCThreshold is how old a job must be before it eligible for GC. This gives
   163  	// the user time to inspect the job.
   164  	JobGCThreshold time.Duration
   165  
   166  	// NodeGCInterval is how often we dispatch a job to GC failed nodes.
   167  	NodeGCInterval time.Duration
   168  
   169  	// NodeGCThreshold is how "old" a node must be to be eligible
   170  	// for GC. This gives users some time to view and debug a failed nodes.
   171  	NodeGCThreshold time.Duration
   172  
   173  	// DeploymentGCInterval is how often we dispatch a job to GC terminal
   174  	// deployments.
   175  	DeploymentGCInterval time.Duration
   176  
   177  	// DeploymentGCThreshold is how "old" a deployment must be to be eligible
   178  	// for GC. This gives users some time to view terminal deployments.
   179  	DeploymentGCThreshold time.Duration
   180  
   181  	// EvalNackTimeout controls how long we allow a sub-scheduler to
   182  	// work on an evaluation before we consider it failed and Nack it.
   183  	// This allows that evaluation to be handed to another sub-scheduler
   184  	// to work on. Defaults to 60 seconds. This should be long enough that
   185  	// no evaluation hits it unless the sub-scheduler has failed.
   186  	EvalNackTimeout time.Duration
   187  
   188  	// EvalDeliveryLimit is the limit of attempts we make to deliver and
   189  	// process an evaluation. This is used so that an eval that will never
   190  	// complete eventually fails out of the system.
   191  	EvalDeliveryLimit int
   192  
   193  	// EvalNackInitialReenqueueDelay is the delay applied before reenqueuing a
   194  	// Nacked evaluation for the first time. This value should be small as the
   195  	// initial Nack can be due to a down machine and the eval should be retried
   196  	// quickly for liveliness.
   197  	EvalNackInitialReenqueueDelay time.Duration
   198  
   199  	// EvalNackSubsequentReenqueueDelay is the delay applied before reenqueuing
   200  	// an evaluation that has been Nacked more than once. This delay is
   201  	// compounding after the first Nack. This value should be significantly
   202  	// longer than the initial delay as the purpose it severs is to apply
   203  	// back-pressure as evaluations are being Nacked either due to scheduler
   204  	// failures or because they are hitting their Nack timeout, both of which
   205  	// are signs of high server resource usage.
   206  	EvalNackSubsequentReenqueueDelay time.Duration
   207  
   208  	// EvalFailedFollowupBaselineDelay is the minimum time waited before
   209  	// retrying a failed evaluation.
   210  	EvalFailedFollowupBaselineDelay time.Duration
   211  
   212  	// EvalFailedFollowupDelayRange defines the range of additional time from
   213  	// the baseline in which to wait before retrying a failed evaluation. The
   214  	// additional delay is selected from this range randomly.
   215  	EvalFailedFollowupDelayRange time.Duration
   216  
   217  	// MinHeartbeatTTL is the minimum time between heartbeats.
   218  	// This is used as a floor to prevent excessive updates.
   219  	MinHeartbeatTTL time.Duration
   220  
   221  	// MaxHeartbeatsPerSecond is the maximum target rate of heartbeats
   222  	// being processed per second. This allows the TTL to be increased
   223  	// to meet the target rate.
   224  	MaxHeartbeatsPerSecond float64
   225  
   226  	// HeartbeatGrace is the additional time given as a grace period
   227  	// beyond the TTL to account for network and processing delays
   228  	// as well as clock skew.
   229  	HeartbeatGrace time.Duration
   230  
   231  	// FailoverHeartbeatTTL is the TTL applied to heartbeats after
   232  	// a new leader is elected, since we no longer know the status
   233  	// of all the heartbeats.
   234  	FailoverHeartbeatTTL time.Duration
   235  
   236  	// ConsulConfig is this Agent's Consul configuration
   237  	ConsulConfig *config.ConsulConfig
   238  
   239  	// VaultConfig is this Agent's Vault configuration
   240  	VaultConfig *config.VaultConfig
   241  
   242  	// RPCHoldTimeout is how long an RPC can be "held" before it is errored.
   243  	// This is used to paper over a loss of leadership by instead holding RPCs,
   244  	// so that the caller experiences a slow response rather than an error.
   245  	// This period is meant to be long enough for a leader election to take
   246  	// place, and a small jitter is applied to avoid a thundering herd.
   247  	RPCHoldTimeout time.Duration
   248  
   249  	// TLSConfig holds various TLS related configurations
   250  	TLSConfig *config.TLSConfig
   251  
   252  	// ACLEnabled controls if ACL enforcement and management is enabled.
   253  	ACLEnabled bool
   254  
   255  	// ReplicationBackoff is how much we backoff when replication errors.
   256  	// This is a tunable knob for testing primarily.
   257  	ReplicationBackoff time.Duration
   258  
   259  	// ReplicationToken is the ACL Token Secret ID used to fetch from
   260  	// the Authoritative Region.
   261  	ReplicationToken string
   262  
   263  	// SentinelGCInterval is the interval that we GC unused policies.
   264  	SentinelGCInterval time.Duration
   265  
   266  	// SentinelConfig is this Agent's Sentinel configuration
   267  	SentinelConfig *config.SentinelConfig
   268  
   269  	// StatsCollectionInterval is the interval at which the Nomad server
   270  	// publishes metrics which are periodic in nature like updating gauges
   271  	StatsCollectionInterval time.Duration
   272  
   273  	// DisableTaggedMetrics determines whether metrics will be displayed via a
   274  	// key/value/tag format, or simply a key/value format
   275  	DisableTaggedMetrics bool
   276  
   277  	// BackwardsCompatibleMetrics determines whether to show methods of
   278  	// displaying metrics for older versions, or to only show the new format
   279  	BackwardsCompatibleMetrics bool
   280  
   281  	// AutopilotConfig is used to apply the initial autopilot config when
   282  	// bootstrapping.
   283  	AutopilotConfig *structs.AutopilotConfig
   284  
   285  	// ServerHealthInterval is the frequency with which the health of the
   286  	// servers in the cluster will be updated.
   287  	ServerHealthInterval time.Duration
   288  
   289  	// AutopilotInterval is the frequency with which the leader will perform
   290  	// autopilot tasks, such as promoting eligible non-voters and removing
   291  	// dead servers.
   292  	AutopilotInterval time.Duration
   293  }
   294  
   295  // CheckVersion is used to check if the ProtocolVersion is valid
   296  func (c *Config) CheckVersion() error {
   297  	if c.ProtocolVersion < ProtocolVersionMin {
   298  		return fmt.Errorf("Protocol version '%d' too low. Must be in range: [%d, %d]",
   299  			c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax)
   300  	} else if c.ProtocolVersion > ProtocolVersionMax {
   301  		return fmt.Errorf("Protocol version '%d' too high. Must be in range: [%d, %d]",
   302  			c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax)
   303  	}
   304  	return nil
   305  }
   306  
   307  // DefaultConfig returns the default configuration
   308  func DefaultConfig() *Config {
   309  	hostname, err := os.Hostname()
   310  	if err != nil {
   311  		panic(err)
   312  	}
   313  
   314  	c := &Config{
   315  		Region:                           DefaultRegion,
   316  		AuthoritativeRegion:              DefaultRegion,
   317  		Datacenter:                       DefaultDC,
   318  		NodeName:                         hostname,
   319  		NodeID:                           uuid.Generate(),
   320  		ProtocolVersion:                  ProtocolVersionMax,
   321  		RaftConfig:                       raft.DefaultConfig(),
   322  		RaftTimeout:                      10 * time.Second,
   323  		LogOutput:                        os.Stderr,
   324  		RPCAddr:                          DefaultRPCAddr,
   325  		SerfConfig:                       serf.DefaultConfig(),
   326  		NumSchedulers:                    1,
   327  		ReconcileInterval:                60 * time.Second,
   328  		EvalGCInterval:                   5 * time.Minute,
   329  		EvalGCThreshold:                  1 * time.Hour,
   330  		JobGCInterval:                    5 * time.Minute,
   331  		JobGCThreshold:                   4 * time.Hour,
   332  		NodeGCInterval:                   5 * time.Minute,
   333  		NodeGCThreshold:                  24 * time.Hour,
   334  		DeploymentGCInterval:             5 * time.Minute,
   335  		DeploymentGCThreshold:            1 * time.Hour,
   336  		EvalNackTimeout:                  60 * time.Second,
   337  		EvalDeliveryLimit:                3,
   338  		EvalNackInitialReenqueueDelay:    1 * time.Second,
   339  		EvalNackSubsequentReenqueueDelay: 20 * time.Second,
   340  		EvalFailedFollowupBaselineDelay:  1 * time.Minute,
   341  		EvalFailedFollowupDelayRange:     5 * time.Minute,
   342  		MinHeartbeatTTL:                  10 * time.Second,
   343  		MaxHeartbeatsPerSecond:           50.0,
   344  		HeartbeatGrace:                   10 * time.Second,
   345  		FailoverHeartbeatTTL:             300 * time.Second,
   346  		ConsulConfig:                     config.DefaultConsulConfig(),
   347  		VaultConfig:                      config.DefaultVaultConfig(),
   348  		RPCHoldTimeout:                   5 * time.Second,
   349  		StatsCollectionInterval:          1 * time.Minute,
   350  		TLSConfig:                        &config.TLSConfig{},
   351  		ReplicationBackoff:               30 * time.Second,
   352  		SentinelGCInterval:               30 * time.Second,
   353  		AutopilotConfig: &structs.AutopilotConfig{
   354  			CleanupDeadServers:      true,
   355  			LastContactThreshold:    200 * time.Millisecond,
   356  			MaxTrailingLogs:         250,
   357  			ServerStabilizationTime: 10 * time.Second,
   358  		},
   359  		ServerHealthInterval: 2 * time.Second,
   360  		AutopilotInterval:    10 * time.Second,
   361  	}
   362  
   363  	// Enable all known schedulers by default
   364  	c.EnabledSchedulers = make([]string, 0, len(scheduler.BuiltinSchedulers))
   365  	for name := range scheduler.BuiltinSchedulers {
   366  		c.EnabledSchedulers = append(c.EnabledSchedulers, name)
   367  	}
   368  	c.EnabledSchedulers = append(c.EnabledSchedulers, structs.JobTypeCore)
   369  
   370  	// Default the number of schedulers to match the cores
   371  	c.NumSchedulers = runtime.NumCPU()
   372  
   373  	// Increase our reap interval to 3 days instead of 24h.
   374  	c.SerfConfig.ReconnectTimeout = 3 * 24 * time.Hour
   375  
   376  	// Serf should use the WAN timing, since we are using it
   377  	// to communicate between DC's
   378  	c.SerfConfig.MemberlistConfig = memberlist.DefaultWANConfig()
   379  	c.SerfConfig.MemberlistConfig.BindPort = DefaultSerfPort
   380  
   381  	// Disable shutdown on removal
   382  	c.RaftConfig.ShutdownOnRemove = false
   383  
   384  	// Enable interoperability with new raft APIs, requires all servers
   385  	// to be on raft v1 or higher.
   386  	c.RaftConfig.ProtocolVersion = 2
   387  
   388  	return c
   389  }