diff --git a/api/allocations.go b/api/allocations.go index 6f5f90220d1..d622f86b0a5 100644 --- a/api/allocations.go +++ b/api/allocations.go @@ -12,6 +12,20 @@ var ( NodeDownErr = fmt.Errorf("node down") ) +const ( + AllocDesiredStatusRun = "run" // Allocation should run + AllocDesiredStatusStop = "stop" // Allocation should stop + AllocDesiredStatusEvict = "evict" // Allocation should stop, and was evicted +) + +const ( + AllocClientStatusPending = "pending" + AllocClientStatusRunning = "running" + AllocClientStatusComplete = "complete" + AllocClientStatusFailed = "failed" + AllocClientStatusLost = "lost" +) + // Allocations is used to query the alloc-related endpoints. type Allocations struct { client *Client diff --git a/api/jobs.go b/api/jobs.go index 008705fea08..827ef124e75 100644 --- a/api/jobs.go +++ b/api/jobs.go @@ -576,13 +576,27 @@ func (p *PeriodicConfig) Canonicalize() { func (p *PeriodicConfig) Next(fromTime time.Time) (time.Time, error) { if *p.SpecType == PeriodicSpecCron { if e, err := cronexpr.Parse(*p.Spec); err == nil { - return CronParseNext(e, fromTime, *p.Spec) + return cronParseNext(e, fromTime, *p.Spec) } } return time.Time{}, nil } +// cronParseNext is a helper that parses the next time for the given expression +// but captures any panic that may occur in the underlying library. +// --- THIS FUNCTION IS REPLICATED IN nomad/structs/structs.go +// and should be kept in sync. +func cronParseNext(e *cronexpr.Expression, fromTime time.Time, spec string) (t time.Time, err error) { + defer func() { + if recover() != nil { + t = time.Time{} + err = fmt.Errorf("failed parsing cron expression: %q", spec) + } + }() + + return e.Next(fromTime), nil +} func (p *PeriodicConfig) GetLocation() (*time.Location, error) { if p.TimeZone == nil || *p.TimeZone == "" { return time.UTC, nil diff --git a/api/nodes.go b/api/nodes.go index b43a0636844..ed768760e68 100644 --- a/api/nodes.go +++ b/api/nodes.go @@ -8,6 +8,18 @@ import ( "time" ) +const ( + NodeStatusInit = "initializing" + NodeStatusReady = "ready" + NodeStatusDown = "down" + + // NodeSchedulingEligible and Ineligible marks the node as eligible or not, + // respectively, for receiving allocations. This is orthoginal to the node + // status being ready. + NodeSchedulingEligible = "eligible" + NodeSchedulingIneligible = "ineligible" +) + // Nodes is used to query node-related API endpoints type Nodes struct { client *Client diff --git a/api/nomadstructs.go b/api/nomadstructs.go deleted file mode 100644 index 93fcb7c8208..00000000000 --- a/api/nomadstructs.go +++ /dev/null @@ -1,92 +0,0 @@ -package api - -import ( - "fmt" - "time" - - "github.com/gorhill/cronexpr" -) - -// This file contains objects that are shared between /nomad/structs and /api package. -// The packages need to be independent: -// -// * /api package is public facing and should be minimal -// .* /nomad/structs is an internal package and may reference some libraries (e.g. raft, codec libraries) -// - -var ( - defaultServiceJobRestartPolicyDelay = 15 * time.Second - defaultServiceJobRestartPolicyAttempts = 2 - defaultServiceJobRestartPolicyInterval = 30 * time.Minute - defaultServiceJobRestartPolicyMode = RestartPolicyModeFail - - defaultBatchJobRestartPolicyDelay = 15 * time.Second - defaultBatchJobRestartPolicyAttempts = 3 - defaultBatchJobRestartPolicyInterval = 24 * time.Hour - defaultBatchJobRestartPolicyMode = RestartPolicyModeFail -) - -const ( - NodeStatusInit = "initializing" - NodeStatusReady = "ready" - NodeStatusDown = "down" - - // NodeSchedulingEligible and Ineligible marks the node as eligible or not, - // respectively, for receiving allocations. This is orthoginal to the node - // status being ready. - NodeSchedulingEligible = "eligible" - NodeSchedulingIneligible = "ineligible" -) - -const ( - AllocDesiredStatusRun = "run" // Allocation should run - AllocDesiredStatusStop = "stop" // Allocation should stop - AllocDesiredStatusEvict = "evict" // Allocation should stop, and was evicted -) - -const ( - AllocClientStatusPending = "pending" - AllocClientStatusRunning = "running" - AllocClientStatusComplete = "complete" - AllocClientStatusFailed = "failed" - AllocClientStatusLost = "lost" -) - -var ( - defaultServiceJobReschedulePolicyAttempts = 0 - defaultServiceJobReschedulePolicyInterval = time.Duration(0) - defaultServiceJobReschedulePolicyDelay = 30 * time.Second - defaultServiceJobReschedulePolicyDelayFunction = "exponential" - defaultServiceJobReschedulePolicyMaxDelay = 1 * time.Hour - defaultServiceJobReschedulePolicyUnlimited = true - - defaultBatchJobReschedulePolicyAttempts = 1 - defaultBatchJobReschedulePolicyInterval = 24 * time.Hour - defaultBatchJobReschedulePolicyDelay = 5 * time.Second - defaultBatchJobReschedulePolicyDelayFunction = "constant" - defaultBatchJobReschedulePolicyMaxDelay = time.Duration(0) - defaultBatchJobReschedulePolicyUnlimited = false -) - -const ( - // RestartPolicyModeDelay causes an artificial delay till the next interval is - // reached when the specified attempts have been reached in the interval. - RestartPolicyModeDelay = "delay" - - // RestartPolicyModeFail causes a job to fail if the specified number of - // attempts are reached within an interval. - RestartPolicyModeFail = "fail" -) - -// CronParseNext is a helper that parses the next time for the given expression -// but captures any panic that may occur in the underlying library. -func CronParseNext(e *cronexpr.Expression, fromTime time.Time, spec string) (t time.Time, err error) { - defer func() { - if recover() != nil { - t = time.Time{} - err = fmt.Errorf("failed parsing cron expression: %q", spec) - } - }() - - return e.Next(fromTime), nil -} diff --git a/api/nomadstructs_codegen.go b/api/nomadstructs_codegen.go deleted file mode 100644 index 2b2f60c80f8..00000000000 --- a/api/nomadstructs_codegen.go +++ /dev/null @@ -1,3 +0,0 @@ -package api - -//go:generate ./nomadstructs_sync.sh diff --git a/api/nomadstructs_sync.sh b/api/nomadstructs_sync.sh deleted file mode 100755 index f83134a6733..00000000000 --- a/api/nomadstructs_sync.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -cat ../nomad/structs/apistructs.go | \ - sed 's|^package structs|package api|g' \ - > nomadstructs.go diff --git a/api/tasks.go b/api/tasks.go index 839660be0ae..26b6c8d6623 100644 --- a/api/tasks.go +++ b/api/tasks.go @@ -8,6 +8,16 @@ import ( "time" ) +const ( + // RestartPolicyModeDelay causes an artificial delay till the next interval is + // reached when the specified attempts have been reached in the interval. + RestartPolicyModeDelay = "delay" + + // RestartPolicyModeFail causes a job to fail if the specified number of + // attempts are reached within an interval. + RestartPolicyModeFail = "fail" +) + // MemoryStats holds memory usage related stats type MemoryStats struct { RSS uint64 @@ -167,22 +177,28 @@ func NewDefaultReschedulePolicy(jobType string) *ReschedulePolicy { var dp *ReschedulePolicy switch jobType { case "service": + // This needs to be in sync with DefaultServiceJobReschedulePolicy + // in nomad/structs/structs.go dp = &ReschedulePolicy{ - Attempts: intToPtr(defaultServiceJobReschedulePolicyAttempts), - Interval: timeToPtr(defaultServiceJobReschedulePolicyInterval), - Delay: timeToPtr(defaultServiceJobReschedulePolicyDelay), - DelayFunction: stringToPtr(defaultServiceJobReschedulePolicyDelayFunction), - MaxDelay: timeToPtr(defaultServiceJobReschedulePolicyMaxDelay), - Unlimited: boolToPtr(defaultServiceJobReschedulePolicyUnlimited), + Delay: timeToPtr(30 * time.Second), + DelayFunction: stringToPtr("exponential"), + MaxDelay: timeToPtr(1 * time.Hour), + Unlimited: boolToPtr(true), + + Attempts: intToPtr(0), + Interval: timeToPtr(0), } case "batch": + // This needs to be in sync with DefaultBatchJobReschedulePolicy + // in nomad/structs/structs.go dp = &ReschedulePolicy{ - Attempts: intToPtr(defaultBatchJobReschedulePolicyAttempts), - Interval: timeToPtr(defaultBatchJobReschedulePolicyInterval), - Delay: timeToPtr(defaultBatchJobReschedulePolicyDelay), - DelayFunction: stringToPtr(defaultBatchJobReschedulePolicyDelayFunction), - MaxDelay: timeToPtr(defaultBatchJobReschedulePolicyMaxDelay), - Unlimited: boolToPtr(defaultBatchJobReschedulePolicyUnlimited), + Attempts: intToPtr(1), + Interval: timeToPtr(24 * time.Hour), + Delay: timeToPtr(5 * time.Second), + DelayFunction: stringToPtr("constant"), + + MaxDelay: timeToPtr(0), + Unlimited: boolToPtr(false), } case "system": @@ -552,18 +568,22 @@ func (g *TaskGroup) Canonicalize(job *Job) { var defaultRestartPolicy *RestartPolicy switch *job.Type { case "service", "system": + // These needs to be in sync with DefaultServiceJobRestartPolicy in + // in nomad/structs/structs.go defaultRestartPolicy = &RestartPolicy{ - Delay: timeToPtr(defaultServiceJobRestartPolicyDelay), - Attempts: intToPtr(defaultServiceJobRestartPolicyAttempts), - Interval: timeToPtr(defaultServiceJobRestartPolicyInterval), - Mode: stringToPtr(defaultServiceJobRestartPolicyMode), + Delay: timeToPtr(15 * time.Second), + Attempts: intToPtr(2), + Interval: timeToPtr(30 * time.Minute), + Mode: stringToPtr(RestartPolicyModeFail), } default: + // These needs to be in sync with DefaultBatchJobRestartPolicy in + // in nomad/structs/structs.go defaultRestartPolicy = &RestartPolicy{ - Delay: timeToPtr(defaultBatchJobRestartPolicyDelay), - Attempts: intToPtr(defaultBatchJobRestartPolicyAttempts), - Interval: timeToPtr(defaultBatchJobRestartPolicyInterval), - Mode: stringToPtr(defaultBatchJobRestartPolicyMode), + Delay: timeToPtr(15 * time.Second), + Attempts: intToPtr(3), + Interval: timeToPtr(24 * time.Hour), + Mode: stringToPtr(RestartPolicyModeFail), } } diff --git a/api/tasks_test.go b/api/tasks_test.go index aea293cddb8..d54718f4aed 100644 --- a/api/tasks_test.go +++ b/api/tasks_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/hashicorp/nomad/nomad/structs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -406,12 +407,12 @@ func TestTaskGroup_Canonicalize_ReschedulePolicy(t *testing.T) { jobReschedulePolicy: nil, taskReschedulePolicy: nil, expected: &ReschedulePolicy{ - Attempts: intToPtr(defaultBatchJobReschedulePolicyAttempts), - Interval: timeToPtr(defaultBatchJobReschedulePolicyInterval), - Delay: timeToPtr(defaultBatchJobReschedulePolicyDelay), - DelayFunction: stringToPtr(defaultBatchJobReschedulePolicyDelayFunction), - MaxDelay: timeToPtr(defaultBatchJobReschedulePolicyMaxDelay), - Unlimited: boolToPtr(defaultBatchJobReschedulePolicyUnlimited), + Attempts: intToPtr(structs.DefaultBatchJobReschedulePolicy.Attempts), + Interval: timeToPtr(structs.DefaultBatchJobReschedulePolicy.Interval), + Delay: timeToPtr(structs.DefaultBatchJobReschedulePolicy.Delay), + DelayFunction: stringToPtr(structs.DefaultBatchJobReschedulePolicy.DelayFunction), + MaxDelay: timeToPtr(structs.DefaultBatchJobReschedulePolicy.MaxDelay), + Unlimited: boolToPtr(structs.DefaultBatchJobReschedulePolicy.Unlimited), }, }, { @@ -510,7 +511,7 @@ func TestTaskGroup_Canonicalize_ReschedulePolicy(t *testing.T) { }, expected: &ReschedulePolicy{ Attempts: intToPtr(5), - Interval: timeToPtr(defaultBatchJobReschedulePolicyInterval), + Interval: timeToPtr(structs.DefaultBatchJobReschedulePolicy.Interval), Delay: timeToPtr(20 * time.Second), MaxDelay: timeToPtr(20 * time.Minute), DelayFunction: stringToPtr("constant"), @@ -525,11 +526,11 @@ func TestTaskGroup_Canonicalize_ReschedulePolicy(t *testing.T) { taskReschedulePolicy: nil, expected: &ReschedulePolicy{ Attempts: intToPtr(1), - Interval: timeToPtr(defaultBatchJobReschedulePolicyInterval), - Delay: timeToPtr(defaultBatchJobReschedulePolicyDelay), - DelayFunction: stringToPtr(defaultBatchJobReschedulePolicyDelayFunction), - MaxDelay: timeToPtr(defaultBatchJobReschedulePolicyMaxDelay), - Unlimited: boolToPtr(defaultBatchJobReschedulePolicyUnlimited), + Interval: timeToPtr(structs.DefaultBatchJobReschedulePolicy.Interval), + Delay: timeToPtr(structs.DefaultBatchJobReschedulePolicy.Delay), + DelayFunction: stringToPtr(structs.DefaultBatchJobReschedulePolicy.DelayFunction), + MaxDelay: timeToPtr(structs.DefaultBatchJobReschedulePolicy.MaxDelay), + Unlimited: boolToPtr(structs.DefaultBatchJobReschedulePolicy.Unlimited), }, }, } diff --git a/nomad/structs/apistructs.go b/nomad/structs/apistructs.go deleted file mode 100644 index 9907f40694b..00000000000 --- a/nomad/structs/apistructs.go +++ /dev/null @@ -1,92 +0,0 @@ -package structs - -import ( - "fmt" - "time" - - "github.com/gorhill/cronexpr" -) - -// This file contains objects that are shared between /nomad/structs and /api package. -// The packages need to be independent: -// -// * /api package is public facing and should be minimal -// .* /nomad/structs is an internal package and may reference some libraries (e.g. raft, codec libraries) -// - -var ( - defaultServiceJobRestartPolicyDelay = 15 * time.Second - defaultServiceJobRestartPolicyAttempts = 2 - defaultServiceJobRestartPolicyInterval = 30 * time.Minute - defaultServiceJobRestartPolicyMode = RestartPolicyModeFail - - defaultBatchJobRestartPolicyDelay = 15 * time.Second - defaultBatchJobRestartPolicyAttempts = 3 - defaultBatchJobRestartPolicyInterval = 24 * time.Hour - defaultBatchJobRestartPolicyMode = RestartPolicyModeFail -) - -const ( - NodeStatusInit = "initializing" - NodeStatusReady = "ready" - NodeStatusDown = "down" - - // NodeSchedulingEligible and Ineligible marks the node as eligible or not, - // respectively, for receiving allocations. This is orthoginal to the node - // status being ready. - NodeSchedulingEligible = "eligible" - NodeSchedulingIneligible = "ineligible" -) - -const ( - AllocDesiredStatusRun = "run" // Allocation should run - AllocDesiredStatusStop = "stop" // Allocation should stop - AllocDesiredStatusEvict = "evict" // Allocation should stop, and was evicted -) - -const ( - AllocClientStatusPending = "pending" - AllocClientStatusRunning = "running" - AllocClientStatusComplete = "complete" - AllocClientStatusFailed = "failed" - AllocClientStatusLost = "lost" -) - -var ( - defaultServiceJobReschedulePolicyAttempts = 0 - defaultServiceJobReschedulePolicyInterval = time.Duration(0) - defaultServiceJobReschedulePolicyDelay = 30 * time.Second - defaultServiceJobReschedulePolicyDelayFunction = "exponential" - defaultServiceJobReschedulePolicyMaxDelay = 1 * time.Hour - defaultServiceJobReschedulePolicyUnlimited = true - - defaultBatchJobReschedulePolicyAttempts = 1 - defaultBatchJobReschedulePolicyInterval = 24 * time.Hour - defaultBatchJobReschedulePolicyDelay = 5 * time.Second - defaultBatchJobReschedulePolicyDelayFunction = "constant" - defaultBatchJobReschedulePolicyMaxDelay = time.Duration(0) - defaultBatchJobReschedulePolicyUnlimited = false -) - -const ( - // RestartPolicyModeDelay causes an artificial delay till the next interval is - // reached when the specified attempts have been reached in the interval. - RestartPolicyModeDelay = "delay" - - // RestartPolicyModeFail causes a job to fail if the specified number of - // attempts are reached within an interval. - RestartPolicyModeFail = "fail" -) - -// CronParseNext is a helper that parses the next time for the given expression -// but captures any panic that may occur in the underlying library. -func CronParseNext(e *cronexpr.Expression, fromTime time.Time, spec string) (t time.Time, err error) { - defer func() { - if recover() != nil { - t = time.Time{} - err = fmt.Errorf("failed parsing cron expression: %q", spec) - } - }() - - return e.Next(fromTime), nil -} diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 0697b892cad..b735d6e37e1 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -1298,6 +1298,12 @@ func (ne *NodeEvent) AddDetail(k, v string) *NodeEvent { return ne } +const ( + NodeStatusInit = "initializing" + NodeStatusReady = "ready" + NodeStatusDown = "down" +) + // ShouldDrainNode checks if a given node status should trigger an // evaluation. Some states don't require any further action. func ShouldDrainNode(status string) bool { @@ -1321,6 +1327,14 @@ func ValidNodeStatus(status string) bool { } } +const ( + // NodeSchedulingEligible and Ineligible marks the node as eligible or not, + // respectively, for receiving allocations. This is orthoginal to the node + // status being ready. + NodeSchedulingEligible = "eligible" + NodeSchedulingIneligible = "ineligible" +) + // DrainSpec describes a Node's desired drain behavior. type DrainSpec struct { // Deadline is the duration after StartTime when the remaining @@ -3833,6 +3847,19 @@ func (p *PeriodicConfig) Canonicalize() { p.location = l } +// CronParseNext is a helper that parses the next time for the given expression +// but captures any panic that may occur in the underlying library. +func CronParseNext(e *cronexpr.Expression, fromTime time.Time, spec string) (t time.Time, err error) { + defer func() { + if recover() != nil { + t = time.Time{} + err = fmt.Errorf("failed parsing cron expression: %q", spec) + } + }() + + return e.Next(fromTime), nil +} + // Next returns the closest time instant matching the spec that is after the // passed time. If no matching instance exists, the zero value of time.Time is // returned. The `time.Location` of the returned value matches that of the @@ -3990,42 +4017,50 @@ func (d *DispatchPayloadConfig) Validate() error { } var ( + // These default restart policies needs to be in sync with + // Canonicalize in api/tasks.go + DefaultServiceJobRestartPolicy = RestartPolicy{ - Delay: defaultServiceJobRestartPolicyDelay, - Attempts: defaultServiceJobRestartPolicyAttempts, - Interval: defaultServiceJobRestartPolicyInterval, - Mode: defaultServiceJobRestartPolicyMode, + Delay: 15 * time.Second, + Attempts: 2, + Interval: 30 * time.Minute, + Mode: RestartPolicyModeFail, } - DefaultBatchJobRestartPolicy = RestartPolicy{ - Delay: defaultBatchJobRestartPolicyDelay, - Attempts: defaultBatchJobRestartPolicyAttempts, - Interval: defaultBatchJobRestartPolicyInterval, - Mode: defaultBatchJobRestartPolicyMode, + Delay: 15 * time.Second, + Attempts: 3, + Interval: 24 * time.Hour, + Mode: RestartPolicyModeFail, } ) var ( + // These default reschedule policies needs to be in sync with + // NewDefaultReschedulePolicy in api/tasks.go + DefaultServiceJobReschedulePolicy = ReschedulePolicy{ - Attempts: defaultServiceJobReschedulePolicyAttempts, - Interval: defaultServiceJobReschedulePolicyInterval, - Delay: defaultServiceJobReschedulePolicyDelay, - DelayFunction: defaultServiceJobReschedulePolicyDelayFunction, - MaxDelay: defaultServiceJobReschedulePolicyMaxDelay, - Unlimited: defaultServiceJobReschedulePolicyUnlimited, + Delay: 30 * time.Second, + DelayFunction: "exponential", + MaxDelay: 1 * time.Hour, + Unlimited: true, } - DefaultBatchJobReschedulePolicy = ReschedulePolicy{ - Attempts: defaultBatchJobReschedulePolicyAttempts, - Interval: defaultBatchJobReschedulePolicyInterval, - Delay: defaultBatchJobReschedulePolicyDelay, - DelayFunction: defaultBatchJobReschedulePolicyDelayFunction, - MaxDelay: defaultBatchJobReschedulePolicyMaxDelay, - Unlimited: defaultBatchJobReschedulePolicyUnlimited, + Attempts: 1, + Interval: 24 * time.Hour, + Delay: 5 * time.Second, + DelayFunction: "constant", } ) const ( + // RestartPolicyModeDelay causes an artificial delay till the next interval is + // reached when the specified attempts have been reached in the interval. + RestartPolicyModeDelay = "delay" + + // RestartPolicyModeFail causes a job to fail if the specified number of + // attempts are reached within an interval. + RestartPolicyModeFail = "fail" + // RestartPolicyMinInterval is the minimum interval that is accepted for a // restart policy. RestartPolicyMinInterval = 5 * time.Second @@ -7059,6 +7094,20 @@ func (d *DesiredTransition) ShouldForceReschedule() bool { return d.ForceReschedule != nil && *d.ForceReschedule } +const ( + AllocDesiredStatusRun = "run" // Allocation should run + AllocDesiredStatusStop = "stop" // Allocation should stop + AllocDesiredStatusEvict = "evict" // Allocation should stop, and was evicted +) + +const ( + AllocClientStatusPending = "pending" + AllocClientStatusRunning = "running" + AllocClientStatusComplete = "complete" + AllocClientStatusFailed = "failed" + AllocClientStatusLost = "lost" +) + // Allocation is used to allocate the placement of a task group to a node. type Allocation struct { // ID of the allocation (UUID)