gorealis-v2/jobupdate.go

152 lines
4.2 KiB
Go
Raw Normal View History

2016-08-02 11:42:00 -07:00
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2016-08-02 11:42:00 -07:00
package realis
import (
"github.com/paypal/gorealis/gen-go/apache/aurora"
)
2016-08-02 11:42:00 -07:00
2016-08-24 11:59:01 -07:00
// Structure to collect all information required to create job update
type JobUpdate struct {
Task *Task
request *aurora.JobUpdateRequest
2016-08-02 11:42:00 -07:00
}
// Create a default JobUpdate object.
func NewDefaultJobUpdate(task *Task) *JobUpdate {
2016-08-02 11:42:00 -07:00
2018-11-08 18:07:38 -08:00
req := aurora.JobUpdateRequest{}
req.TaskConfig = task.task
req.Settings = NewUpdateSettings()
2016-08-02 11:42:00 -07:00
2016-08-24 11:59:01 -07:00
// Rebuild resource map from TaskConfig
for _, ptr := range task.task.Resources {
if ptr.NumCpus != nil {
task.resources["cpu"].NumCpus = ptr.NumCpus
2016-08-24 11:59:01 -07:00
continue // Guard against Union violations that Go won't enforce
}
if ptr.RamMb != nil {
task.resources["ram"].RamMb = ptr.RamMb
2016-08-24 11:59:01 -07:00
continue
}
if ptr.DiskMb != nil {
task.resources["disk"].DiskMb = ptr.DiskMb
2016-08-24 11:59:01 -07:00
continue
}
}
2016-08-02 11:42:00 -07:00
// Mirrors defaults set by Pystachio
req.Settings.UpdateOnlyTheseInstances = []*aurora.Range{}
2016-08-02 11:42:00 -07:00
req.Settings.UpdateGroupSize = 1
req.Settings.WaitForBatchCompletion = false
req.Settings.MinWaitInInstanceRunningMs = 45000
2016-08-02 11:42:00 -07:00
req.Settings.MaxPerInstanceFailures = 0
req.Settings.MaxFailedInstances = 0
req.Settings.RollbackOnFailure = true
//TODO(rdelvalle): Deep copy task struct to avoid unexpected behavior
return &JobUpdate{Task: task, request: &req}
2016-08-02 11:42:00 -07:00
}
func NewUpdateJob(config *aurora.TaskConfig, settings *aurora.JobUpdateSettings) *JobUpdate {
2016-11-14 23:16:36 -08:00
req := aurora.NewJobUpdateRequest()
req.TaskConfig = config
req.Settings = settings
task := NewTask()
task.task = config
2016-11-14 23:16:36 -08:00
// Rebuild resource map from TaskConfig
for _, ptr := range config.Resources {
2016-11-14 23:16:36 -08:00
if ptr.NumCpus != nil {
task.resources["cpu"].NumCpus = ptr.NumCpus
2016-11-14 23:16:36 -08:00
continue // Guard against Union violations that Go won't enforce
}
if ptr.RamMb != nil {
task.resources["ram"].RamMb = ptr.RamMb
2016-11-14 23:16:36 -08:00
continue
}
if ptr.DiskMb != nil {
task.resources["disk"].DiskMb = ptr.DiskMb
2016-11-14 23:16:36 -08:00
continue
}
}
//TODO(rdelvalle): Deep copy job struct to avoid unexpected behavior
return &JobUpdate{Task: task, request: req}
2016-11-14 23:16:36 -08:00
}
// Set instance count the job will have after the update.
func (u *JobUpdate) InstanceCount(inst int32) *JobUpdate {
u.request.InstanceCount = inst
2016-08-02 11:42:00 -07:00
return u
}
// Max number of instances being updated at any given moment.
func (u *JobUpdate) BatchSize(size int32) *JobUpdate {
u.request.Settings.UpdateGroupSize = size
2016-08-02 11:42:00 -07:00
return u
}
// Minimum number of seconds a shard must remain in RUNNING state before considered a success.
func (u *JobUpdate) WatchTime(ms int32) *JobUpdate {
u.request.Settings.MinWaitInInstanceRunningMs = ms
2016-08-02 11:42:00 -07:00
return u
}
// Wait for all instances in a group to be done before moving on.
func (u *JobUpdate) WaitForBatchCompletion(batchWait bool) *JobUpdate {
u.request.Settings.WaitForBatchCompletion = batchWait
2016-08-02 11:42:00 -07:00
return u
}
// Max number of instance failures to tolerate before marking instance as FAILED.
func (u *JobUpdate) MaxPerInstanceFailures(inst int32) *JobUpdate {
u.request.Settings.MaxPerInstanceFailures = inst
2016-08-02 11:42:00 -07:00
return u
}
// Max number of FAILED instances to tolerate before terminating the update.
func (u *JobUpdate) MaxFailedInstances(inst int32) *JobUpdate {
u.request.Settings.MaxFailedInstances = inst
2016-08-02 11:42:00 -07:00
return u
}
// When False, prevents auto rollback of a failed update.
func (u *JobUpdate) RollbackOnFail(rollback bool) *JobUpdate {
u.request.Settings.RollbackOnFailure = rollback
2016-08-02 11:42:00 -07:00
return u
}
func NewUpdateSettings() *aurora.JobUpdateSettings {
2018-11-08 18:07:38 -08:00
us := aurora.JobUpdateSettings{}
// Mirrors defaults set by Pystachio
us.UpdateOnlyTheseInstances = []*aurora.Range{}
us.UpdateGroupSize = 1
us.WaitForBatchCompletion = false
us.MinWaitInInstanceRunningMs = 45000
us.MaxPerInstanceFailures = 0
us.MaxFailedInstances = 0
us.RollbackOnFailure = true
2018-11-08 18:07:38 -08:00
return &us
}