| 1 |
package mesos |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"strconv" |
| 7 |
|
| 8 |
"github.com/gogo/protobuf/proto" |
| 9 |
) |
| 10 |
|
| 11 |
type ( |
| 12 |
Resources []Resource |
| 13 |
resourceErrorType int |
| 14 |
|
| 15 |
resourceError struct { |
| 16 |
errorType resourceErrorType |
| 17 |
reason string |
| 18 |
spec Resource |
| 19 |
} |
| 20 |
) |
| 21 |
|
| 22 |
const ( |
| 23 |
resourceErrorTypeIllegalName resourceErrorType = iota |
| 24 |
resourceErrorTypeIllegalType |
| 25 |
resourceErrorTypeUnsupportedType |
| 26 |
resourceErrorTypeIllegalScalar |
| 27 |
resourceErrorTypeIllegalRanges |
| 28 |
resourceErrorTypeIllegalSet |
| 29 |
resourceErrorTypeIllegalDisk |
| 30 |
resourceErrorTypeIllegalReservation |
| 31 |
|
| 32 |
noReason = "" |
| 33 |
) |
| 34 |
|
| 35 |
var ( |
| 36 |
resourceErrorMessages = map[resourceErrorType]string{ |
| 37 |
resourceErrorTypeIllegalName: "missing or illegal resource name", |
| 38 |
resourceErrorTypeIllegalType: "missing or illegal resource type", |
| 39 |
resourceErrorTypeUnsupportedType: "unsupported resource type", |
| 40 |
resourceErrorTypeIllegalScalar: "illegal scalar resource", |
| 41 |
resourceErrorTypeIllegalRanges: "illegal ranges resource", |
| 42 |
resourceErrorTypeIllegalSet: "illegal set resource", |
| 43 |
resourceErrorTypeIllegalDisk: "illegal disk resource", |
| 44 |
resourceErrorTypeIllegalReservation: "illegal resource reservation", |
| 45 |
} |
| 46 |
) |
| 47 |
|
| 48 |
func (t resourceErrorType) Generate(reason string) error { |
| 49 |
msg := resourceErrorMessages[t] |
| 50 |
if reason != noReason { |
| 51 |
if msg != "" { |
| 52 |
msg += ": " + reason |
| 53 |
} else { |
| 54 |
msg = reason |
| 55 |
} |
| 56 |
} |
| 57 |
return &resourceError{errorType: t, reason: msg} |
| 58 |
} |
| 59 |
|
| 60 |
func (err *resourceError) Reason() string { return err.reason } |
| 61 |
func (err *resourceError) Resource() Resource { return err.spec } |
| 62 |
func (err *resourceError) WithResource(r Resource) { err.spec = r } |
| 63 |
|
| 64 |
func (err *resourceError) Error() string { |
| 65 |
|
| 66 |
if err.reason != "" { |
| 67 |
return "resource error: " + err.reason |
| 68 |
} |
| 69 |
return "resource error" |
| 70 |
} |
| 71 |
|
| 72 |
func IsResourceError(err error) (ok bool) { |
| 73 |
_, ok = err.(*resourceError) |
| 74 |
return |
| 75 |
} |
| 76 |
|
| 77 |
func (r *Resource_ReservationInfo) Assign() func(interface{}) { |
| 78 |
return func(v interface{}) { |
| 79 |
type reserver interface { |
| 80 |
WithReservation(*Resource_ReservationInfo) |
| 81 |
} |
| 82 |
if ri, ok := v.(reserver); ok { |
| 83 |
ri.WithReservation(r) |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func (resources Resources) Clone() Resources { |
| 89 |
if resources == nil { |
| 90 |
return nil |
| 91 |
} |
| 92 |
clone := make(Resources, 0, len(resources)) |
| 93 |
for i := range resources { |
| 94 |
rr := proto.Clone(&resources[i]).(*Resource) |
| 95 |
clone = append(clone, *rr) |
| 96 |
} |
| 97 |
return clone |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
func (resources Resources) Minus(that ...Resource) Resources { |
| 103 |
x := resources.Clone() |
| 104 |
return x.Subtract(that...) |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
func (resources *Resources) Subtract(that ...Resource) (rs Resources) { |
| 110 |
if resources != nil { |
| 111 |
if len(that) > 0 { |
| 112 |
x := make(Resources, len(that)) |
| 113 |
copy(x, that) |
| 114 |
that = x |
| 115 |
|
| 116 |
for i := range that { |
| 117 |
resources.Subtract1(that[i]) |
| 118 |
} |
| 119 |
} |
| 120 |
rs = *resources |
| 121 |
} |
| 122 |
return |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
func (resources Resources) Plus(that ...Resource) Resources { |
| 128 |
x := resources.Clone() |
| 129 |
return x.Add(that...) |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
func (resources *Resources) Add(that ...Resource) (rs Resources) { |
| 135 |
if resources != nil { |
| 136 |
rs = *resources |
| 137 |
} |
| 138 |
for i := range that { |
| 139 |
rs = rs._add(that[i]) |
| 140 |
} |
| 141 |
if resources != nil { |
| 142 |
*resources = rs |
| 143 |
} |
| 144 |
return |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
func (resources *Resources) Add1(that Resource) (rs Resources) { |
| 150 |
if resources != nil { |
| 151 |
rs = *resources |
| 152 |
} |
| 153 |
rs = rs._add(that) |
| 154 |
if resources != nil { |
| 155 |
*resources = rs |
| 156 |
} |
| 157 |
return |
| 158 |
} |
| 159 |
|
| 160 |
func (resources Resources) _add(that Resource) Resources { |
| 161 |
if that.Validate() != nil || that.IsEmpty() { |
| 162 |
return resources |
| 163 |
} |
| 164 |
for i := range resources { |
| 165 |
r := &resources[i] |
| 166 |
if r.Addable(that) { |
| 167 |
r.Add(that) |
| 168 |
return resources |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
r := proto.Clone(&that).(*Resource) |
| 173 |
return append(resources, *r) |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
func (resources *Resources) Minus1(that Resource) Resources { |
| 179 |
x := resources.Clone() |
| 180 |
return x.Subtract1(that) |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
func (resources *Resources) Subtract1(that Resource) Resources { |
| 186 |
if resources == nil { |
| 187 |
return nil |
| 188 |
} |
| 189 |
if that.Validate() == nil && !that.IsEmpty() { |
| 190 |
for i := range *resources { |
| 191 |
r := &(*resources)[i] |
| 192 |
if r.Subtractable(that) { |
| 193 |
r.Subtract(that) |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
if r.Validate() != nil || r.IsEmpty() { |
| 198 |
|
| 199 |
|
| 200 |
(*resources), (*resources)[len((*resources))-1] = append((*resources)[:i], (*resources)[i+1:]...), Resource{} |
| 201 |
} |
| 202 |
break |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
return *resources |
| 207 |
} |
| 208 |
|
| 209 |
func (resources Resources) String() string { |
| 210 |
if len(resources) == 0 { |
| 211 |
return "" |
| 212 |
} |
| 213 |
buf := bytes.Buffer{} |
| 214 |
for i := range resources { |
| 215 |
if i > 0 { |
| 216 |
buf.WriteString(";") |
| 217 |
} |
| 218 |
r := &resources[i] |
| 219 |
buf.WriteString(r.Name) |
| 220 |
buf.WriteString("(") |
| 221 |
buf.WriteString(r.GetRole()) |
| 222 |
if ri := r.GetReservation(); ri != nil { |
| 223 |
buf.WriteString(", ") |
| 224 |
buf.WriteString(ri.GetPrincipal()) |
| 225 |
} |
| 226 |
buf.WriteString(")") |
| 227 |
if d := r.GetDisk(); d != nil { |
| 228 |
buf.WriteString("[") |
| 229 |
if s := d.GetSource(); s != nil { |
| 230 |
switch s.GetType() { |
| 231 |
case PATH: |
| 232 |
buf.WriteString("PATH:") |
| 233 |
if p := s.GetPath(); p != nil { |
| 234 |
buf.WriteString(p.GetRoot()) |
| 235 |
} |
| 236 |
case MOUNT: |
| 237 |
buf.WriteString("MOUNT:") |
| 238 |
if m := s.GetMount(); m != nil { |
| 239 |
buf.WriteString(m.GetRoot()) |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
if p := d.GetPersistence(); p != nil { |
| 244 |
if d.GetSource() != nil { |
| 245 |
buf.WriteString(",") |
| 246 |
} |
| 247 |
buf.WriteString(p.GetID()) |
| 248 |
} |
| 249 |
if v := d.GetVolume(); v != nil { |
| 250 |
buf.WriteString(":") |
| 251 |
vconfig := v.GetContainerPath() |
| 252 |
if h := v.GetHostPath(); h != "" { |
| 253 |
vconfig = h + ":" + vconfig |
| 254 |
} |
| 255 |
if m := v.Mode; m != nil { |
| 256 |
switch *m { |
| 257 |
case RO: |
| 258 |
vconfig += ":ro" |
| 259 |
case RW: |
| 260 |
vconfig += ":rw" |
| 261 |
default: |
| 262 |
panic("unrecognized volume mode: " + m.String()) |
| 263 |
} |
| 264 |
} |
| 265 |
buf.WriteString(vconfig) |
| 266 |
} |
| 267 |
buf.WriteString("]") |
| 268 |
} |
| 269 |
buf.WriteString(":") |
| 270 |
switch r.GetType() { |
| 271 |
case SCALAR: |
| 272 |
buf.WriteString(strconv.FormatFloat(r.GetScalar().GetValue(), 'f', -1, 64)) |
| 273 |
case RANGES: |
| 274 |
buf.WriteString("[") |
| 275 |
ranges := Ranges(r.GetRanges().GetRange()) |
| 276 |
for j := range ranges { |
| 277 |
if j > 0 { |
| 278 |
buf.WriteString(",") |
| 279 |
} |
| 280 |
buf.WriteString(strconv.FormatUint(ranges[j].Begin, 10)) |
| 281 |
buf.WriteString("-") |
| 282 |
buf.WriteString(strconv.FormatUint(ranges[j].End, 10)) |
| 283 |
} |
| 284 |
buf.WriteString("]") |
| 285 |
case SET: |
| 286 |
buf.WriteString("{") |
| 287 |
items := r.GetSet().GetItem() |
| 288 |
for j := range items { |
| 289 |
if j > 0 { |
| 290 |
buf.WriteString(",") |
| 291 |
} |
| 292 |
buf.WriteString(items[j]) |
| 293 |
} |
| 294 |
buf.WriteString("}") |
| 295 |
} |
| 296 |
} |
| 297 |
return buf.String() |
| 298 |
} |
| 299 |
|
| 300 |
func (left *Resource) Validate() error { |
| 301 |
if left.GetName() == "" { |
| 302 |
return resourceErrorTypeIllegalName.Generate(noReason) |
| 303 |
} |
| 304 |
if _, ok := Value_Type_name[int32(left.GetType())]; !ok { |
| 305 |
return resourceErrorTypeIllegalType.Generate(noReason) |
| 306 |
} |
| 307 |
switch left.GetType() { |
| 308 |
case SCALAR: |
| 309 |
if s := left.GetScalar(); s == nil || left.GetRanges() != nil || left.GetSet() != nil { |
| 310 |
return resourceErrorTypeIllegalScalar.Generate(noReason) |
| 311 |
} else if s.GetValue() < 0 { |
| 312 |
return resourceErrorTypeIllegalScalar.Generate("value < 0") |
| 313 |
} |
| 314 |
case RANGES: |
| 315 |
r := left.GetRanges() |
| 316 |
if left.GetScalar() != nil || r == nil || left.GetSet() != nil { |
| 317 |
return resourceErrorTypeIllegalRanges.Generate(noReason) |
| 318 |
} |
| 319 |
for i, rr := range r.GetRange() { |
| 320 |
|
| 321 |
if rr.Begin > rr.End { |
| 322 |
return resourceErrorTypeIllegalRanges.Generate("begin > end") |
| 323 |
} |
| 324 |
|
| 325 |
for j := i + 1; j < len(r.GetRange()); j++ { |
| 326 |
r2 := r.GetRange()[j] |
| 327 |
if rr.Begin <= r2.Begin && r2.Begin <= rr.End { |
| 328 |
return resourceErrorTypeIllegalRanges.Generate("overlapping ranges") |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
case SET: |
| 333 |
s := left.GetSet() |
| 334 |
if left.GetScalar() != nil || left.GetRanges() != nil || s == nil { |
| 335 |
return resourceErrorTypeIllegalSet.Generate(noReason) |
| 336 |
} |
| 337 |
unique := make(map[string]struct{}, len(s.GetItem())) |
| 338 |
for _, x := range s.GetItem() { |
| 339 |
if _, found := unique[x]; found { |
| 340 |
return resourceErrorTypeIllegalSet.Generate("duplicated elements") |
| 341 |
} |
| 342 |
unique[x] = struct{}{} |
| 343 |
} |
| 344 |
default: |
| 345 |
return resourceErrorTypeUnsupportedType.Generate(noReason) |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
if left.GetDisk() != nil && left.GetName() != "disk" { |
| 350 |
return resourceErrorTypeIllegalDisk.Generate("DiskInfo should not be set for \"" + left.GetName() + "\" resource") |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
if left.GetRole() == "*" && left.GetReservation() != nil { |
| 355 |
return resourceErrorTypeIllegalReservation.Generate("default role cannot be dynamically assigned") |
| 356 |
} |
| 357 |
|
| 358 |
return nil |
| 359 |
} |
| 360 |
|
| 361 |
func (r *Resource_ReservationInfo) Equivalent(right *Resource_ReservationInfo) bool { |
| 362 |
if (r == nil) != (right == nil) { |
| 363 |
return false |
| 364 |
} |
| 365 |
return r.GetPrincipal() == right.GetPrincipal() |
| 366 |
} |
| 367 |
|
| 368 |
func (left *Resource_DiskInfo) Equivalent(right *Resource_DiskInfo) bool { |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
if (left == nil) != (right == nil) { |
| 376 |
return false |
| 377 |
} |
| 378 |
|
| 379 |
if a, b := left.GetSource(), right.GetSource(); (a == nil) != (b == nil) { |
| 380 |
return false |
| 381 |
} else if a != nil { |
| 382 |
if a.GetType() != b.GetType() { |
| 383 |
return false |
| 384 |
} |
| 385 |
if aa, bb := a.GetMount(), b.GetMount(); (aa == nil) != (bb == nil) { |
| 386 |
return false |
| 387 |
} else if aa.GetRoot() != bb.GetRoot() { |
| 388 |
return false |
| 389 |
} |
| 390 |
if aa, bb := a.GetPath(), b.GetPath(); (aa == nil) != (bb == nil) { |
| 391 |
return false |
| 392 |
} else if aa.GetRoot() != bb.GetRoot() { |
| 393 |
return false |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
if a, b := left.GetPersistence(), right.GetPersistence(); (a == nil) != (b == nil) { |
| 398 |
return false |
| 399 |
} else if a != nil { |
| 400 |
return a.GetID() == b.GetID() |
| 401 |
} |
| 402 |
|
| 403 |
return true |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
func (left *Resource) Equivalent(right Resource) bool { |
| 409 |
if left.GetName() != right.GetName() || |
| 410 |
left.GetType() != right.GetType() || |
| 411 |
left.GetRole() != right.GetRole() { |
| 412 |
return false |
| 413 |
} |
| 414 |
if !left.GetReservation().Equivalent(right.GetReservation()) { |
| 415 |
return false |
| 416 |
} |
| 417 |
if !left.GetDisk().Equivalent(right.GetDisk()) { |
| 418 |
return false |
| 419 |
} |
| 420 |
if (left.Revocable == nil) != (right.Revocable == nil) { |
| 421 |
return false |
| 422 |
} |
| 423 |
|
| 424 |
switch left.GetType() { |
| 425 |
case SCALAR: |
| 426 |
return left.GetScalar().Compare(right.GetScalar()) == 0 |
| 427 |
case RANGES: |
| 428 |
return Ranges(left.GetRanges().GetRange()).Equivalent(right.GetRanges().GetRange()) |
| 429 |
case SET: |
| 430 |
return left.GetSet().Compare(right.GetSet()) == 0 |
| 431 |
default: |
| 432 |
return false |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
func (left *Resource) Addable(right Resource) bool { |
| 440 |
if left.GetName() != right.GetName() || |
| 441 |
left.GetType() != right.GetType() || |
| 442 |
left.GetRole() != right.GetRole() { |
| 443 |
return false |
| 444 |
} |
| 445 |
if !left.GetReservation().Equivalent(right.GetReservation()) { |
| 446 |
return false |
| 447 |
} |
| 448 |
if !left.GetDisk().Equivalent(right.GetDisk()) { |
| 449 |
return false |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
if left.GetDisk().GetPersistence() != nil { |
| 458 |
return false |
| 459 |
} |
| 460 |
if (left.Revocable == nil) != (right.Revocable == nil) { |
| 461 |
return false |
| 462 |
} |
| 463 |
return true |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
func (left *Resource) Subtractable(right Resource) bool { |
| 475 |
if left.GetName() != right.GetName() || |
| 476 |
left.GetType() != right.GetType() || |
| 477 |
left.GetRole() != right.GetRole() { |
| 478 |
return false |
| 479 |
} |
| 480 |
if !left.GetReservation().Equivalent(right.GetReservation()) { |
| 481 |
return false |
| 482 |
} |
| 483 |
if !left.GetDisk().Equivalent(right.GetDisk()) { |
| 484 |
return false |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
if left.GetDisk().GetPersistence() != nil && !left.Equivalent(right) { |
| 490 |
return false |
| 491 |
} |
| 492 |
if (left.Revocable == nil) != (right.Revocable == nil) { |
| 493 |
return false |
| 494 |
} |
| 495 |
return true |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
func (left Resource) Contains(right Resource) bool { |
| 500 |
if !left.Subtractable(right) { |
| 501 |
return false |
| 502 |
} |
| 503 |
switch left.GetType() { |
| 504 |
case SCALAR: |
| 505 |
return right.GetScalar().Compare(left.GetScalar()) <= 0 |
| 506 |
case RANGES: |
| 507 |
return right.GetRanges().Compare(left.GetRanges()) <= 0 |
| 508 |
case SET: |
| 509 |
return right.GetSet().Compare(left.GetSet()) <= 0 |
| 510 |
default: |
| 511 |
return false |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
func (left *Resource) Subtract(right Resource) { |
| 518 |
switch right.checkType(left.GetType()) { |
| 519 |
case SCALAR: |
| 520 |
left.Scalar = left.GetScalar().Subtract(right.GetScalar()) |
| 521 |
case RANGES: |
| 522 |
left.Ranges = left.GetRanges().Subtract(right.GetRanges()) |
| 523 |
case SET: |
| 524 |
left.Set = left.GetSet().Subtract(right.GetSet()) |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
func (left *Resource) Add(right Resource) { |
| 531 |
switch right.checkType(left.GetType()) { |
| 532 |
case SCALAR: |
| 533 |
left.Scalar = left.GetScalar().Add(right.GetScalar()) |
| 534 |
case RANGES: |
| 535 |
left.Ranges = left.GetRanges().Add(right.GetRanges()) |
| 536 |
case SET: |
| 537 |
left.Set = left.GetSet().Add(right.GetSet()) |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
func (left *Resource) checkType(t Value_Type) Value_Type { |
| 543 |
if left != nil && left.GetType() != t { |
| 544 |
panic(fmt.Sprintf("expected type %v instead of %v", t, left.GetType())) |
| 545 |
} |
| 546 |
return t |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
func (left *Resource) IsEmpty() bool { |
| 552 |
if left == nil { |
| 553 |
return true |
| 554 |
} |
| 555 |
switch left.GetType() { |
| 556 |
case SCALAR: |
| 557 |
return left.GetScalar().GetValue() == 0 |
| 558 |
case RANGES: |
| 559 |
return len(left.GetRanges().GetRange()) == 0 |
| 560 |
case SET: |
| 561 |
return len(left.GetSet().GetItem()) == 0 |
| 562 |
} |
| 563 |
return false |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
func (left *Resource) IsUnreserved() bool { |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
return left.GetRole() == "*" && left.GetReservation() == nil |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
func (left *Resource) IsReserved(role string) bool { |
| 579 |
if role != "" { |
| 580 |
return !left.IsUnreserved() && role == left.GetRole() |
| 581 |
} |
| 582 |
return !left.IsUnreserved() |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
func (left *Resource) IsDynamicallyReserved() bool { |
| 587 |
return left.GetReservation() != nil |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
func (left *Resource) IsRevocable() bool { |
| 592 |
return left.GetRevocable() != nil |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
func (left *Resource) IsPersistentVolume() bool { |
| 597 |
return left.GetDisk().GetPersistence() != nil |
| 598 |
} |