1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
package calls
import (
"errors"
"math/rand"
"time"
"github.com/mesos/mesos-go/api/v1/lib"
"github.com/mesos/mesos-go/api/v1/lib/scheduler"
)
// Filters sets a scheduler.Call's internal Filters, required for Accept and Decline calls.
func Filters(fo ...mesos.FilterOpt) scheduler.CallOpt {
return func(c *scheduler.Call) {
switch c.Type {
case scheduler.Call_ACCEPT:
c.Accept.Filters = mesos.OptionalFilters(fo...)
case scheduler.Call_ACCEPT_INVERSE_OFFERS:
c.AcceptInverseOffers.Filters = mesos.OptionalFilters(fo...)
case scheduler.Call_DECLINE:
c.Decline.Filters = mesos.OptionalFilters(fo...)
case scheduler.Call_DECLINE_INVERSE_OFFERS:
c.DeclineInverseOffers.Filters = mesos.OptionalFilters(fo...)
default:
panic("filters not supported for type " + c.Type.String())
}
}
}
// RefuseSecondsWithJitter returns a calls.Filters option that sets RefuseSeconds to a random number
// of seconds between 0 and the given duration.
func RefuseSecondsWithJitter(r *rand.Rand, d time.Duration) scheduler.CallOpt {
return Filters(func(f *mesos.Filters) {
s := time.Duration(r.Int63n(int64(d))).Seconds()
f.RefuseSeconds = &s
})
}
// RefuseSeconds returns a calls.Filters option that sets RefuseSeconds to the given duration
func RefuseSeconds(d time.Duration) scheduler.CallOpt {
asFloat := d.Seconds()
return Filters(func(f *mesos.Filters) {
f.RefuseSeconds = &asFloat
})
}
// Framework sets a scheduler.Call's FrameworkID
func Framework(id string) scheduler.CallOpt {
return func(c *scheduler.Call) {
c.FrameworkID = &mesos.FrameworkID{Value: id}
}
}
// Subscribe returns a subscribe call with the given parameters.
// The call's FrameworkID is automatically filled in from the info specification.
func Subscribe(info *mesos.FrameworkInfo) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_SUBSCRIBE,
FrameworkID: info.GetID(),
Subscribe: &scheduler.Call_Subscribe{FrameworkInfo: info},
}
}
// SubscribeTo returns an option that configures a SUBSCRIBE call w/ a framework ID.
// If frameworkID is "" then the SUBSCRIBE call is cleared of all framework ID references.
// Panics if the call does not contain a non-nil Subscribe reference.
func SubscribeTo(frameworkID string) scheduler.CallOpt {
return func(call *scheduler.Call) {
if call.Subscribe == nil {
panic("illegal call option: Call.Subscribe was unexpectedly nil")
}
var frameworkProto *mesos.FrameworkID
if frameworkID != "" {
frameworkProto = &mesos.FrameworkID{Value: frameworkID}
}
call.Subscribe.FrameworkInfo.ID = frameworkProto
call.FrameworkID = frameworkProto
}
}
type acceptBuilder struct {
offerIDs map[mesos.OfferID]struct{}
operations []mesos.Offer_Operation
}
type AcceptOpt func(*acceptBuilder)
type OfferOperations []mesos.Offer_Operation
// WithOffers allows a client to pair some set of OfferOperations with multiple resource offers.
// Example: calls.Accept(calls.OfferOperations{calls.OpLaunch(tasks...)}.WithOffers(offers...))
func (ob OfferOperations) WithOffers(ids ...mesos.OfferID) AcceptOpt {
return func(ab *acceptBuilder) {
for i := range ids {
ab.offerIDs[ids[i]] = struct{}{}
}
ab.operations = append(ab.operations, ob...)
}
}
// Accept returns an accept call with the given parameters.
// Callers are expected to fill in the FrameworkID and Filters.
func Accept(ops ...AcceptOpt) *scheduler.Call {
ab := &acceptBuilder{
offerIDs: make(map[mesos.OfferID]struct{}, len(ops)),
}
for _, op := range ops {
op(ab)
}
offerIDs := make([]mesos.OfferID, 0, len(ab.offerIDs))
for id := range ab.offerIDs {
offerIDs = append(offerIDs, id)
}
return &scheduler.Call{
Type: scheduler.Call_ACCEPT,
Accept: &scheduler.Call_Accept{
OfferIDs: offerIDs,
Operations: ab.operations,
},
}
}
// AcceptInverseOffers returns an accept-inverse-offers call for the given offer IDs.
// Callers are expected to fill in the FrameworkID and Filters.
func AcceptInverseOffers(offerIDs ...mesos.OfferID) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_ACCEPT_INVERSE_OFFERS,
AcceptInverseOffers: &scheduler.Call_AcceptInverseOffers{
InverseOfferIDs: offerIDs,
},
}
}
// DeclineInverseOffers returns a decline-inverse-offers call for the given offer IDs.
// Callers are expected to fill in the FrameworkID and Filters.
func DeclineInverseOffers(offerIDs ...mesos.OfferID) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_DECLINE_INVERSE_OFFERS,
DeclineInverseOffers: &scheduler.Call_DeclineInverseOffers{
InverseOfferIDs: offerIDs,
},
}
}
// OpLaunch returns a launch operation builder for the given tasks
func OpLaunch(ti ...mesos.TaskInfo) mesos.Offer_Operation {
return mesos.Offer_Operation{
Type: mesos.Offer_Operation_LAUNCH,
Launch: &mesos.Offer_Operation_Launch{
TaskInfos: ti,
},
}
}
func OpLaunchGroup(ei mesos.ExecutorInfo, ti ...mesos.TaskInfo) mesos.Offer_Operation {
return mesos.Offer_Operation{
Type: mesos.Offer_Operation_LAUNCH_GROUP,
LaunchGroup: &mesos.Offer_Operation_LaunchGroup{
Executor: ei,
TaskGroup: mesos.TaskGroupInfo{
Tasks: ti,
},
},
}
}
func OpReserve(rs ...mesos.Resource) mesos.Offer_Operation {
return mesos.Offer_Operation{
Type: mesos.Offer_Operation_RESERVE,
Reserve: &mesos.Offer_Operation_Reserve{
Resources: rs,
},
}
}
func OpUnreserve(rs ...mesos.Resource) mesos.Offer_Operation {
return mesos.Offer_Operation{
Type: mesos.Offer_Operation_UNRESERVE,
Unreserve: &mesos.Offer_Operation_Unreserve{
Resources: rs,
},
}
}
func OpCreate(rs ...mesos.Resource) mesos.Offer_Operation {
return mesos.Offer_Operation{
Type: mesos.Offer_Operation_CREATE,
Create: &mesos.Offer_Operation_Create{
Volumes: rs,
},
}
}
func OpDestroy(rs ...mesos.Resource) mesos.Offer_Operation {
return mesos.Offer_Operation{
Type: mesos.Offer_Operation_DESTROY,
Destroy: &mesos.Offer_Operation_Destroy{
Volumes: rs,
},
}
}
// Revive returns a revive call.
// Callers are expected to fill in the FrameworkID.
func Revive() *scheduler.Call {
return &scheduler.Call{Type: scheduler.Call_REVIVE}
}
// Revive returns a revive call with the given filters.
// Callers are expected to fill in the FrameworkID.
func ReviveWith(roles []string) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_REVIVE,
Revive: &scheduler.Call_Revive{Roles: roles},
}
}
// Suppress returns a suppress call.
// Callers are expected to fill in the FrameworkID.
func Suppress() *scheduler.Call {
return &scheduler.Call{Type: scheduler.Call_SUPPRESS}
}
// Suppress returns a suppress call with the given filters.
// Callers are expected to fill in the FrameworkID.
func SuppressWith(roles []string) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_SUPPRESS,
Suppress: &scheduler.Call_Suppress{Roles: roles},
}
}
// Decline returns a decline call with the given parameters.
// Callers are expected to fill in the FrameworkID and Filters.
func Decline(offerIDs ...mesos.OfferID) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_DECLINE,
Decline: &scheduler.Call_Decline{
OfferIDs: offerIDs,
},
}
}
// Kill returns a kill call with the given parameters.
// Callers are expected to fill in the FrameworkID.
func Kill(taskID, agentID string) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_KILL,
Kill: &scheduler.Call_Kill{
TaskID: mesos.TaskID{Value: taskID},
AgentID: optionalAgentID(agentID),
},
}
}
// Shutdown returns a shutdown call with the given parameters.
// Callers are expected to fill in the FrameworkID.
func Shutdown(executorID, agentID string) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_SHUTDOWN,
Shutdown: &scheduler.Call_Shutdown{
ExecutorID: mesos.ExecutorID{Value: executorID},
AgentID: mesos.AgentID{Value: agentID},
},
}
}
// Acknowledge returns an acknowledge call with the given parameters.
// Callers are expected to fill in the FrameworkID.
func Acknowledge(agentID, taskID string, uuid []byte) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_ACKNOWLEDGE,
Acknowledge: &scheduler.Call_Acknowledge{
AgentID: mesos.AgentID{Value: agentID},
TaskID: mesos.TaskID{Value: taskID},
UUID: uuid,
},
}
}
// ReconcileTasks constructs a []Call_Reconcile_Task from the given mappings:
// map[string]string{taskID:agentID}
// Map keys (taskID's) are required to be non-empty, but values (agentID's) *may* be empty.
func ReconcileTasks(tasks map[string]string) scheduler.ReconcileOpt {
return func(cr *scheduler.Call_Reconcile) {
if len(tasks) == 0 {
cr.Tasks = nil
return
}
result := make([]scheduler.Call_Reconcile_Task, len(tasks))
i := 0
for k, v := range tasks {
result[i].TaskID = mesos.TaskID{Value: k}
result[i].AgentID = optionalAgentID(v)
i++
}
cr.Tasks = result
}
}
// Reconcile returns a reconcile call with the given parameters.
// See ReconcileTask.
// Callers are expected to fill in the FrameworkID.
func Reconcile(opts ...scheduler.ReconcileOpt) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_RECONCILE,
Reconcile: (&scheduler.Call_Reconcile{}).With(opts...),
}
}
// Message returns a message call with the given parameters.
// Callers are expected to fill in the FrameworkID.
func Message(agentID, executorID string, data []byte) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_MESSAGE,
Message: &scheduler.Call_Message{
AgentID: mesos.AgentID{Value: agentID},
ExecutorID: mesos.ExecutorID{Value: executorID},
Data: data,
},
}
}
// Request returns a resource request call with the given parameters.
// Callers are expected to fill in the FrameworkID.
func Request(requests ...mesos.Request) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_REQUEST,
Request: &scheduler.Call_Request{
Requests: requests,
},
}
}
func optionalAgentID(agentID string) *mesos.AgentID {
if agentID == "" {
return nil
}
return &mesos.AgentID{Value: agentID}
}
func errInvalidCall(reason string) error {
return errors.New("invalid call: " + reason)
}
|