/
calls.go
468 lines (424 loc) · 14.3 KB
/
calls.go
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
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,
},
}
}
func OpGrowVolume(v mesos.Resource, a mesos.Resource) mesos.Offer_Operation {
return mesos.Offer_Operation{
Type: mesos.Offer_Operation_GROW_VOLUME,
GrowVolume: &mesos.Offer_Operation_GrowVolume{
Volume: v,
Addition: a,
},
}
}
func OpShrinkVolume(v mesos.Resource, s mesos.Value_Scalar) mesos.Offer_Operation {
return mesos.Offer_Operation{
Type: mesos.Offer_Operation_SHRINK_VOLUME,
ShrinkVolume: &mesos.Offer_Operation_ShrinkVolume{
Volume: v,
Subtract: s,
},
}
}
func OpCreateDisk(src mesos.Resource, t mesos.Resource_DiskInfo_Source_Type) mesos.Offer_Operation {
return mesos.Offer_Operation{
Type: mesos.Offer_Operation_CREATE_DISK,
CreateDisk: &mesos.Offer_Operation_CreateDisk{
Source: src,
TargetType: t,
},
}
}
func OpDestroyDisk(src mesos.Resource) mesos.Offer_Operation {
return mesos.Offer_Operation{
Type: mesos.Offer_Operation_DESTROY_DISK,
DestroyDisk: &mesos.Offer_Operation_DestroyDisk{
Source: src,
},
}
}
// 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 optionalResourceProviderID(id string) *mesos.ResourceProviderID {
if id == "" {
return nil
}
return &mesos.ResourceProviderID{Value: id}
}
func errInvalidCall(reason string) error {
return errors.New("invalid call: " + reason)
}
// AcknowledgeOperationStatus acks the receipt of an operation status update. Schedulers are responsible for
// explicitly acknowledging the receipt of updates which have the 'OperationStatusUpdate.status().uuid()'
// field set. Such status updates are retried by the agent or resource provider until they are acknowledged by the scheduler.
// agentID and resourceProviderID are optional, the remaining fields are required.
func AcknowledgeOperationStatus(agentID, resourceProviderID string, uuid []byte, operationID string) *scheduler.Call {
return &scheduler.Call{
Type: scheduler.Call_ACKNOWLEDGE_OPERATION_STATUS,
AcknowledgeOperationStatus: &scheduler.Call_AcknowledgeOperationStatus{
AgentID: optionalAgentID(agentID),
ResourceProviderID: optionalResourceProviderID(resourceProviderID),
UUID: uuid,
OperationID: mesos.OperationID{Value: operationID},
},
}
}
// ReconcileOperationRequest is a convenience type for which each instance maps to an instance of
// scheduler.Call_ReconcileOfferOperations_Operation.
type ReconcileOperationRequest struct {
OperationID string // OperationID is required
AgentID string // AgentID is optional
ResourceProviderID string // ResourceProviderID is optional
}
// ReconcileOperations allows the scheduler to query the status of operations. This causes the master to send
// back the latest status for each operation in 'req', if possible. If 'req' is empty, then the master will send
// the latest status for each operation currently known.
func ReconcileOperations(req []ReconcileOperationRequest) *scheduler.Call {
var operations []scheduler.Call_ReconcileOperations_Operation
for i := range req {
operations = append(operations, scheduler.Call_ReconcileOperations_Operation{
OperationID: mesos.OperationID{Value: req[i].OperationID},
AgentID: optionalAgentID(req[i].AgentID),
ResourceProviderID: optionalResourceProviderID(req[i].ResourceProviderID),
})
}
return &scheduler.Call{
Type: scheduler.Call_RECONCILE_OPERATIONS,
ReconcileOperations: &scheduler.Call_ReconcileOperations{
Operations: operations,
},
}
}
// UpdateFramework updates the FrameworkInfo. All fields can be updated except for:
//
// * FrameworkInfo.checkpoint
// * FrameworkInfo.principal
// * FrameworkInfo.user
//
// The call returns after the update is either applied completely or
// not applied at all. No incomplete updates occur.
//
// The HTTP response codes specific to this call are:
//
// * 200 OK: update operation was successfully completed.
// * 400 Bad Request: the requested update is not valid.
// * 403 Forbidden: framework is not authorized to use some entities
// requested by the update (e.g. not authorized to use some of the
// supplied roles).
// * 409 Conflict: framework disappeared while this call was being processed
// (example: the framework was removed by a concurrent TEARDOWN call).
//
func UpdateFramework(fi mesos.FrameworkInfo, options ...func(*scheduler.Call_UpdateFramework)) *scheduler.Call {
c := &scheduler.Call{
Type: scheduler.Call_UPDATE_FRAMEWORK,
UpdateFramework: &scheduler.Call_UpdateFramework{
FrameworkInfo: fi,
},
}
for _, f := range options {
if f != nil {
f(c.UpdateFramework)
}
}
return c
}