github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/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/nomad/structs" 13 "github.com/hashicorp/nomad/scheduler" 14 "github.com/hashicorp/raft" 15 "github.com/hashicorp/serf/serf" 16 ) 17 18 const ( 19 DefaultRegion = "global" 20 DefaultDC = "dc1" 21 DefaultSerfPort = 4648 22 ) 23 24 // These are the protocol versions that Nomad can understand 25 const ( 26 ProtocolVersionMin uint8 = 1 27 ProtocolVersionMax = 1 28 ) 29 30 // ProtocolVersionMap is the mapping of Nomad protocol versions 31 // to Serf protocol versions. We mask the Serf protocols using 32 // our own protocol version. 33 var protocolVersionMap map[uint8]uint8 34 35 func init() { 36 protocolVersionMap = map[uint8]uint8{ 37 1: 4, 38 } 39 } 40 41 var ( 42 DefaultRPCAddr = &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4647} 43 ) 44 45 // Config is used to parameterize the server 46 type Config struct { 47 // Bootstrap mode is used to bring up the first Consul server. 48 // It is required so that it can elect a leader without any 49 // other nodes being present 50 Bootstrap bool 51 52 // BootstrapExpect mode is used to automatically bring up a collection of 53 // Consul servers. This can be used to automatically bring up a collection 54 // of nodes. 55 BootstrapExpect int 56 57 // DataDir is the directory to store our state in 58 DataDir string 59 60 // DevMode is used for development purposes only and limits the 61 // use of persistence or state. 62 DevMode bool 63 64 // DevDisableBootstrap is used to disable bootstrap mode while 65 // in DevMode. This is largely used for testing. 66 DevDisableBootstrap bool 67 68 // LogOutput is the location to write logs to. If this is not set, 69 // logs will go to stderr. 70 LogOutput io.Writer 71 72 // ProtocolVersion is the protocol version to speak. This must be between 73 // ProtocolVersionMin and ProtocolVersionMax. 74 ProtocolVersion uint8 75 76 // RPCAddr is the RPC address used by Nomad. This should be reachable 77 // by the other servers and clients 78 RPCAddr *net.TCPAddr 79 80 // RPCAdvertise is the address that is advertised to other nodes for 81 // the RPC endpoint. This can differ from the RPC address, if for example 82 // the RPCAddr is unspecified "0.0.0.0:4646", but this address must be 83 // reachable 84 RPCAdvertise *net.TCPAddr 85 86 // RaftConfig is the configuration used for Raft in the local DC 87 RaftConfig *raft.Config 88 89 // RaftTimeout is applied to any network traffic for raft. Defaults to 10s. 90 RaftTimeout time.Duration 91 92 // RequireTLS ensures that all RPC traffic is protected with TLS 93 RequireTLS bool 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 181 // CheckVersion is used to check if the ProtocolVersion is valid 182 func (c *Config) CheckVersion() error { 183 if c.ProtocolVersion < ProtocolVersionMin { 184 return fmt.Errorf("Protocol version '%d' too low. Must be in range: [%d, %d]", 185 c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax) 186 } else if c.ProtocolVersion > ProtocolVersionMax { 187 return fmt.Errorf("Protocol version '%d' too high. Must be in range: [%d, %d]", 188 c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax) 189 } 190 return nil 191 } 192 193 // DefaultConfig returns the default configuration 194 func DefaultConfig() *Config { 195 hostname, err := os.Hostname() 196 if err != nil { 197 panic(err) 198 } 199 200 c := &Config{ 201 Region: DefaultRegion, 202 Datacenter: DefaultDC, 203 NodeName: hostname, 204 ProtocolVersion: ProtocolVersionMax, 205 RaftConfig: raft.DefaultConfig(), 206 RaftTimeout: 10 * time.Second, 207 RPCAddr: DefaultRPCAddr, 208 SerfConfig: serf.DefaultConfig(), 209 NumSchedulers: 1, 210 ReconcileInterval: 60 * time.Second, 211 EvalGCInterval: 5 * time.Minute, 212 EvalGCThreshold: 1 * time.Hour, 213 JobGCInterval: 5 * time.Minute, 214 JobGCThreshold: 4 * time.Hour, 215 NodeGCInterval: 5 * time.Minute, 216 NodeGCThreshold: 24 * time.Hour, 217 EvalNackTimeout: 60 * time.Second, 218 EvalDeliveryLimit: 3, 219 MinHeartbeatTTL: 10 * time.Second, 220 MaxHeartbeatsPerSecond: 50.0, 221 HeartbeatGrace: 10 * time.Second, 222 FailoverHeartbeatTTL: 300 * time.Second, 223 } 224 225 // Enable all known schedulers by default 226 c.EnabledSchedulers = make([]string, 0, len(scheduler.BuiltinSchedulers)) 227 for name := range scheduler.BuiltinSchedulers { 228 c.EnabledSchedulers = append(c.EnabledSchedulers, name) 229 } 230 c.EnabledSchedulers = append(c.EnabledSchedulers, structs.JobTypeCore) 231 232 // Default the number of schedulers to match the coores 233 c.NumSchedulers = runtime.NumCPU() 234 235 // Increase our reap interval to 3 days instead of 24h. 236 c.SerfConfig.ReconnectTimeout = 3 * 24 * time.Hour 237 238 // Serf should use the WAN timing, since we are using it 239 // to communicate between DC's 240 c.SerfConfig.MemberlistConfig = memberlist.DefaultWANConfig() 241 c.SerfConfig.MemberlistConfig.BindPort = DefaultSerfPort 242 243 // Disable shutdown on removal 244 c.RaftConfig.ShutdownOnRemove = false 245 return c 246 }