Commit 5e185386 authored by Mahmood Ali's avatar Mahmood Ali
Browse files

api: avoid codegen for syncing

Given that the values will rarely change, specially considering that any
changes would be backward incompatible change.  As such, it's simpler to
keep syncing manually in the rare occasion and avoid the syncing code
overhead.
parent b1293a89
Showing with 165 additions and 247 deletions
+165 -247
......@@ -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
......
......@@ -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
......
......@@ -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
......
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
}
package api
//go:generate ./nomadstructs_sync.sh
#!/bin/sh
cat ../nomad/structs/apistructs.go | \
sed 's|^package structs|package api|g' \
> nomadstructs.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),
}
}
......
......@@ -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),
},
},
}
......
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
}
......@@ -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)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment