github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/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/tlsutil"
    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  	// RPCAdvertise is the address that is advertised to other 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  	RPCAdvertise *net.TCPAddr
    88  
    89  	// RaftConfig is the configuration used for Raft in the local DC
    90  	RaftConfig *raft.Config
    91  
    92  	// RaftTimeout is applied to any network traffic for raft. Defaults to 10s.
    93  	RaftTimeout time.Duration
    94  
    95  	// RequireTLS ensures that all RPC traffic is protected with TLS
    96  	RequireTLS bool
    97  
    98  	// SerfConfig is the configuration for the serf cluster
    99  	SerfConfig *serf.Config
   100  
   101  	// Node name is the name we use to advertise. Defaults to hostname.
   102  	NodeName string
   103  
   104  	// Region is the region this Nomad server belongs to.
   105  	Region string
   106  
   107  	// Datacenter is the datacenter this Nomad server belongs to.
   108  	Datacenter string
   109  
   110  	// Build is a string that is gossiped around, and can be used to help
   111  	// operators track which versions are actively deployed
   112  	Build string
   113  
   114  	// NumSchedulers is the number of scheduler thread that are run.
   115  	// This can be as many as one per core, or zero to disable this server
   116  	// from doing any scheduling work.
   117  	NumSchedulers int
   118  
   119  	// EnabledSchedulers controls the set of sub-schedulers that are
   120  	// enabled for this server to handle. This will restrict the evaluations
   121  	// that the workers dequeue for processing.
   122  	EnabledSchedulers []string
   123  
   124  	// ReconcileInterval controls how often we reconcile the strongly
   125  	// consistent store with the Serf info. This is used to handle nodes
   126  	// that are force removed, as well as intermittent unavailability during
   127  	// leader election.
   128  	ReconcileInterval time.Duration
   129  
   130  	// EvalGCInterval is how often we dispatch a job to GC evaluations
   131  	EvalGCInterval time.Duration
   132  
   133  	// EvalGCThreshold is how "old" an evaluation must be to be eligible
   134  	// for GC. This gives users some time to debug a failed evaluation.
   135  	EvalGCThreshold time.Duration
   136  
   137  	// JobGCInterval is how often we dispatch a job to GC jobs that are
   138  	// available for garbage collection.
   139  	JobGCInterval time.Duration
   140  
   141  	// JobGCThreshold is how old a job must be before it eligible for GC. This gives
   142  	// the user time to inspect the job.
   143  	JobGCThreshold time.Duration
   144  
   145  	// NodeGCInterval is how often we dispatch a job to GC failed nodes.
   146  	NodeGCInterval time.Duration
   147  
   148  	// NodeGCThreshold is how "old" a nodemust be to be eligible
   149  	// for GC. This gives users some time to view and debug a failed nodes.
   150  	NodeGCThreshold time.Duration
   151  
   152  	// EvalNackTimeout controls how long we allow a sub-scheduler to
   153  	// work on an evaluation before we consider it failed and Nack it.
   154  	// This allows that evaluation to be handed to another sub-scheduler
   155  	// to work on. Defaults to 60 seconds. This should be long enough that
   156  	// no evaluation hits it unless the sub-scheduler has failed.
   157  	EvalNackTimeout time.Duration
   158  
   159  	// EvalDeliveryLimit is the limit of attempts we make to deliver and
   160  	// process an evaluation. This is used so that an eval that will never
   161  	// complete eventually fails out of the system.
   162  	EvalDeliveryLimit int
   163  
   164  	// MinHeartbeatTTL is the minimum time between heartbeats.
   165  	// This is used as a floor to prevent excessive updates.
   166  	MinHeartbeatTTL time.Duration
   167  
   168  	// MaxHeartbeatsPerSecond is the maximum target rate of heartbeats
   169  	// being processed per second. This allows the TTL to be increased
   170  	// to meet the target rate.
   171  	MaxHeartbeatsPerSecond float64
   172  
   173  	// HeartbeatGrace is the additional time given as a grace period
   174  	// beyond the TTL to account for network and processing delays
   175  	// as well as clock skew.
   176  	HeartbeatGrace time.Duration
   177  
   178  	// FailoverHeartbeatTTL is the TTL applied to heartbeats after
   179  	// a new leader is elected, since we no longer know the status
   180  	// of all the heartbeats.
   181  	FailoverHeartbeatTTL time.Duration
   182  
   183  	// ConsulConfig is this Agent's Consul configuration
   184  	ConsulConfig *config.ConsulConfig
   185  
   186  	// VaultConfig is this Agent's Vault configuration
   187  	VaultConfig *config.VaultConfig
   188  
   189  	// RPCHoldTimeout is how long an RPC can be "held" before it is errored.
   190  	// This is used to paper over a loss of leadership by instead holding RPCs,
   191  	// so that the caller experiences a slow response rather than an error.
   192  	// This period is meant to be long enough for a leader election to take
   193  	// place, and a small jitter is applied to avoid a thundering herd.
   194  	RPCHoldTimeout time.Duration
   195  
   196  	// TLSConfig holds various TLS related configurations
   197  	TLSConfig *config.TLSConfig
   198  }
   199  
   200  // CheckVersion is used to check if the ProtocolVersion is valid
   201  func (c *Config) CheckVersion() error {
   202  	if c.ProtocolVersion < ProtocolVersionMin {
   203  		return fmt.Errorf("Protocol version '%d' too low. Must be in range: [%d, %d]",
   204  			c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax)
   205  	} else if c.ProtocolVersion > ProtocolVersionMax {
   206  		return fmt.Errorf("Protocol version '%d' too high. Must be in range: [%d, %d]",
   207  			c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax)
   208  	}
   209  	return nil
   210  }
   211  
   212  // DefaultConfig returns the default configuration
   213  func DefaultConfig() *Config {
   214  	hostname, err := os.Hostname()
   215  	if err != nil {
   216  		panic(err)
   217  	}
   218  
   219  	c := &Config{
   220  		Region:                 DefaultRegion,
   221  		Datacenter:             DefaultDC,
   222  		NodeName:               hostname,
   223  		ProtocolVersion:        ProtocolVersionMax,
   224  		RaftConfig:             raft.DefaultConfig(),
   225  		RaftTimeout:            10 * time.Second,
   226  		LogOutput:              os.Stderr,
   227  		RPCAddr:                DefaultRPCAddr,
   228  		SerfConfig:             serf.DefaultConfig(),
   229  		NumSchedulers:          1,
   230  		ReconcileInterval:      60 * time.Second,
   231  		EvalGCInterval:         5 * time.Minute,
   232  		EvalGCThreshold:        1 * time.Hour,
   233  		JobGCInterval:          5 * time.Minute,
   234  		JobGCThreshold:         4 * time.Hour,
   235  		NodeGCInterval:         5 * time.Minute,
   236  		NodeGCThreshold:        24 * time.Hour,
   237  		EvalNackTimeout:        60 * time.Second,
   238  		EvalDeliveryLimit:      3,
   239  		MinHeartbeatTTL:        10 * time.Second,
   240  		MaxHeartbeatsPerSecond: 50.0,
   241  		HeartbeatGrace:         10 * time.Second,
   242  		FailoverHeartbeatTTL:   300 * time.Second,
   243  		ConsulConfig:           config.DefaultConsulConfig(),
   244  		VaultConfig:            config.DefaultVaultConfig(),
   245  		RPCHoldTimeout:         5 * time.Second,
   246  		TLSConfig:              &config.TLSConfig{},
   247  	}
   248  
   249  	// Enable all known schedulers by default
   250  	c.EnabledSchedulers = make([]string, 0, len(scheduler.BuiltinSchedulers))
   251  	for name := range scheduler.BuiltinSchedulers {
   252  		c.EnabledSchedulers = append(c.EnabledSchedulers, name)
   253  	}
   254  	c.EnabledSchedulers = append(c.EnabledSchedulers, structs.JobTypeCore)
   255  
   256  	// Default the number of schedulers to match the coores
   257  	c.NumSchedulers = runtime.NumCPU()
   258  
   259  	// Increase our reap interval to 3 days instead of 24h.
   260  	c.SerfConfig.ReconnectTimeout = 3 * 24 * time.Hour
   261  
   262  	// Serf should use the WAN timing, since we are using it
   263  	// to communicate between DC's
   264  	c.SerfConfig.MemberlistConfig = memberlist.DefaultWANConfig()
   265  	c.SerfConfig.MemberlistConfig.BindPort = DefaultSerfPort
   266  
   267  	// Disable shutdown on removal
   268  	c.RaftConfig.ShutdownOnRemove = false
   269  	return c
   270  }
   271  
   272  // tlsConfig returns a TLSUtil Config based on the server configuration
   273  func (c *Config) tlsConfig() *tlsutil.Config {
   274  	tlsConf := &tlsutil.Config{
   275  		VerifyIncoming:       true,
   276  		VerifyOutgoing:       true,
   277  		VerifyServerHostname: c.TLSConfig.VerifyServerHostname,
   278  		CAFile:               c.TLSConfig.CAFile,
   279  		CertFile:             c.TLSConfig.CertFile,
   280  		KeyFile:              c.TLSConfig.KeyFile,
   281  	}
   282  	return tlsConf
   283  }