Skip to content

Commit

Permalink
api: avoid codegen for syncing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mahmood Ali committed Jan 18, 2019
1 parent b1293a8 commit 5e18538
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 247 deletions.
14 changes: 14 additions & 0 deletions api/allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions api/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
92 changes: 0 additions & 92 deletions api/nomadstructs.go

This file was deleted.

3 changes: 0 additions & 3 deletions api/nomadstructs_codegen.go

This file was deleted.

5 changes: 0 additions & 5 deletions api/nomadstructs_sync.sh

This file was deleted.

60 changes: 40 additions & 20 deletions api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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),
}
}

Expand Down
25 changes: 13 additions & 12 deletions api/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/hashicorp/nomad/nomad/structs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -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),
},
},
{
Expand Down Expand Up @@ -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"),
Expand All @@ -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),
},
},
}
Expand Down
Loading

0 comments on commit 5e18538

Please sign in to comment.