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