Apache Mesos
type_utils.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __MESOS_TYPE_UTILS_H__
18 #define __MESOS_TYPE_UTILS_H__
19 
20 #include <iosfwd>
21 #include <string>
22 #include <vector>
23 
24 #include <boost/functional/hash.hpp>
25 
26 #include <google/protobuf/repeated_field.h>
27 
28 #include <mesos/mesos.hpp>
29 
30 #include <stout/stringify.hpp>
31 #include <stout/strings.hpp>
32 #include <stout/uuid.hpp>
33 
34 // This file includes definitions for operators on public protobuf
35 // classes (defined in mesos.proto, module.proto, etc.) that don't
36 // have these operators generated by the protobuf compiler. The
37 // corresponding definitions are in src/common/type_utils.cpp.
38 //
39 // Mesos modules need some of the protobuf classes defined in
40 // mesos.proto, module.proto, etc., and require some of these
41 // operators declared in type_utils.hpp. Exposing type_utils.hpp
42 // allows us to build modules without having a dependency on mesos
43 // source tree (src/*).
44 
45 namespace mesos {
46 
47 bool operator==(const CheckStatusInfo& left, const CheckStatusInfo& right);
48 bool operator==(const CommandInfo& left, const CommandInfo& right);
49 bool operator==(const CommandInfo::URI& left, const CommandInfo::URI& right);
50 bool operator==(const ContainerID& left, const ContainerID& right);
51 bool operator==(const ContainerInfo& left, const ContainerInfo& right);
52 bool operator==(const Credential& left, const Credential& right);
53 bool operator==(const CSIPluginInfo& left, const CSIPluginInfo& right);
54 
55 bool operator==(
56  const CSIPluginContainerInfo& left,
57  const CSIPluginContainerInfo& right);
58 
59 bool operator==(const DiscoveryInfo& left, const DiscoveryInfo& right);
60 bool operator==(const Environment& left, const Environment& right);
61 bool operator==(const ExecutorInfo& left, const ExecutorInfo& right);
62 bool operator==(const Label& left, const Label& right);
63 bool operator==(const Labels& left, const Labels& right);
64 bool operator==(const MasterInfo& left, const MasterInfo& right);
65 bool operator==(const OperationStatus& left, const OperationStatus& right);
66 
67 bool operator==(
68  const ResourceProviderInfo& left,
69  const ResourceProviderInfo& right);
70 
71 bool operator==(
72  const ResourceStatistics& left,
73  const ResourceStatistics& right);
74 
75 bool operator==(const SlaveInfo& left, const SlaveInfo& right);
76 bool operator==(const Task& left, const Task& right);
77 bool operator==(const TaskGroupInfo& left, const TaskGroupInfo& right);
78 bool operator==(const TaskInfo& left, const TaskInfo& right);
79 bool operator==(const TaskStatus& left, const TaskStatus& right);
80 bool operator==(const URL& left, const URL& right);
81 bool operator==(const UUID& left, const UUID& right);
82 bool operator==(const Volume& left, const Volume& right);
83 
84 bool operator!=(const CheckStatusInfo& left, const CheckStatusInfo& right);
85 bool operator!=(const ExecutorInfo& left, const ExecutorInfo& right);
86 bool operator!=(const Labels& left, const Labels& right);
87 bool operator!=(const OperationStatus& left, const OperationStatus& right);
88 
89 bool operator!=(const TaskStatus& left, const TaskStatus& right);
90 
91 inline bool operator==(const ExecutorID& left, const ExecutorID& right)
92 {
93  return left.value() == right.value();
94 }
95 
96 
97 inline bool operator==(const FrameworkID& left, const FrameworkID& right)
98 {
99  return left.value() == right.value();
100 }
101 
102 
103 inline bool operator==(const FrameworkInfo& left, const FrameworkInfo& right)
104 {
105  return (left.name() == right.name()) && (left.user() == right.user());
106 }
107 
108 
109 inline bool operator==(const OfferID& left, const OfferID& right)
110 {
111  return left.value() == right.value();
112 }
113 
114 
115 inline bool operator==(const OperationID& left, const OperationID& right)
116 {
117  return left.value() == right.value();
118 }
119 
120 
121 inline bool operator==(
122  const ResourceProviderID& left,
123  const ResourceProviderID& right)
124 {
125  return left.value() == right.value();
126 }
127 
128 
129 inline bool operator==(const SlaveID& left, const SlaveID& right)
130 {
131  return left.value() == right.value();
132 }
133 
134 
135 inline bool operator==(const TaskID& left, const TaskID& right)
136 {
137  return left.value() == right.value();
138 }
139 
140 
141 inline bool operator==(const TimeInfo& left, const TimeInfo& right)
142 {
143  return left.nanoseconds() == right.nanoseconds();
144 }
145 
146 
147 inline bool operator==(const DurationInfo& left, const DurationInfo& right)
148 {
149  return left.nanoseconds() == right.nanoseconds();
150 }
151 
152 
153 inline bool operator==(const ContainerID& left, const std::string& right)
154 {
155  return left.value() == right;
156 }
157 
158 
159 inline bool operator==(const ExecutorID& left, const std::string& right)
160 {
161  return left.value() == right;
162 }
163 
164 
165 inline bool operator==(const FrameworkID& left, const std::string& right)
166 {
167  return left.value() == right;
168 }
169 
170 
171 inline bool operator==(const OfferID& left, const std::string& right)
172 {
173  return left.value() == right;
174 }
175 
176 
177 inline bool operator==(const SlaveID& left, const std::string& right)
178 {
179  return left.value() == right;
180 }
181 
182 
183 inline bool operator==(const TaskID& left, const std::string& right)
184 {
185  return left.value() == right;
186 }
187 
188 
189 inline bool operator==(
190  const DomainInfo::FaultDomain::RegionInfo& left,
191  const DomainInfo::FaultDomain::RegionInfo& right)
192 {
193  return left.name() == right.name();
194 }
195 
196 
197 inline bool operator==(
198  const DomainInfo::FaultDomain::ZoneInfo& left,
199  const DomainInfo::FaultDomain::ZoneInfo& right)
200 {
201  return left.name() == right.name();
202 }
203 
204 
205 inline bool operator==(
206  const DomainInfo::FaultDomain& left,
207  const DomainInfo::FaultDomain& right)
208 {
209  return left.region() == right.region() && left.zone() == right.zone();
210 }
211 
212 
213 inline bool operator==(const DomainInfo& left, const DomainInfo& right)
214 {
215  return left.fault_domain() == right.fault_domain();
216 }
217 
218 
223 inline bool operator==(const MachineID& left, const MachineID& right)
224 {
225  // NOTE: Both fields default to the empty string if they are not specified,
226  // so the string comparisons are safe.
227  return left.has_hostname() == right.has_hostname() &&
228  strings::lower(left.hostname()) == strings::lower(right.hostname()) &&
229  left.has_ip() == right.has_ip() &&
230  left.ip() == right.ip();
231 }
232 
233 
234 inline bool operator!=(const ContainerID& left, const ContainerID& right)
235 {
236  return !(left == right);
237 }
238 
239 
240 inline bool operator!=(
241  const CSIPluginContainerInfo& left,
242  const CSIPluginContainerInfo& right)
243 {
244  return !(left == right);
245 }
246 
247 
248 inline bool operator!=(const ExecutorID& left, const ExecutorID& right)
249 {
250  return left.value() != right.value();
251 }
252 
253 
254 inline bool operator!=(const FrameworkID& left, const FrameworkID& right)
255 {
256  return left.value() != right.value();
257 }
258 
259 
260 inline bool operator!=(const OperationID& left, const OperationID& right)
261 {
262  return left.value() != right.value();
263 }
264 
265 
266 inline bool operator!=(
267  const ResourceProviderID& left,
268  const ResourceProviderID& right)
269 {
270  return left.value() != right.value();
271 }
272 
273 
274 inline bool operator!=(const SlaveID& left, const SlaveID& right)
275 {
276  return left.value() != right.value();
277 }
278 
279 
280 inline bool operator!=(
281  const ResourceProviderInfo& left,
282  const ResourceProviderInfo& right)
283 {
284  return !(left == right);
285 }
286 
287 
288 inline bool operator!=(const TimeInfo& left, const TimeInfo& right)
289 {
290  return !(left == right);
291 }
292 
293 
294 inline bool operator!=(const UUID& left, const UUID& right)
295 {
296  return !(left == right);
297 }
298 
299 
300 inline bool operator!=(const DurationInfo& left, const DurationInfo& right)
301 {
302  return !(left == right);
303 }
304 
305 
306 inline bool operator!=(
307  const DomainInfo::FaultDomain::RegionInfo& left,
308  const DomainInfo::FaultDomain::RegionInfo& right)
309 {
310  return left.name() != right.name();
311 }
312 
313 
314 inline bool operator<(const ContainerID& left, const ContainerID& right)
315 {
316  return left.value() < right.value();
317 }
318 
319 
320 inline bool operator<(const ExecutorID& left, const ExecutorID& right)
321 {
322  return left.value() < right.value();
323 }
324 
325 
326 inline bool operator<(const FrameworkID& left, const FrameworkID& right)
327 {
328  return left.value() < right.value();
329 }
330 
331 
332 inline bool operator<(const OfferID& left, const OfferID& right)
333 {
334  return left.value() < right.value();
335 }
336 
337 
338 inline bool operator<(const SlaveID& left, const SlaveID& right)
339 {
340  return left.value() < right.value();
341 }
342 
343 
344 inline bool operator<(const TaskID& left, const TaskID& right)
345 {
346  return left.value() < right.value();
347 }
348 
349 
350 std::ostream& operator<<(
351  std::ostream& stream,
352  const CapabilityInfo& capabilityInfo);
353 
354 
355 std::ostream& operator<<(
356  std::ostream& stream,
357  const DeviceWhitelist& deviceWhitelist);
358 
359 
360 std::ostream& operator<<(
361  std::ostream& stream,
362  const CheckStatusInfo& checkStatusInfo);
363 
364 
365 std::ostream& operator<<(std::ostream& stream, const CommandInfo& commandInfo);
366 
367 
368 std::ostream& operator<<(std::ostream& stream, const ContainerID& containerId);
369 
370 
371 std::ostream& operator<<(
372  std::ostream& stream,
373  const ContainerInfo& containerInfo);
374 
375 
376 std::ostream& operator<<(std::ostream& stream, const DomainInfo& domainInfo);
377 
378 
379 std::ostream& operator<<(std::ostream& stream, const Environment& environment);
380 
381 
382 std::ostream& operator<<(std::ostream& stream, const ExecutorID& executorId);
383 
384 
385 std::ostream& operator<<(std::ostream& stream, const ExecutorInfo& executor);
386 
387 
388 std::ostream& operator<<(std::ostream& stream, const FrameworkID& frameworkId);
389 
390 
391 std::ostream& operator<<(std::ostream& stream, const MasterInfo& master);
392 
393 
394 std::ostream& operator<<(std::ostream& stream, const OfferID& offerId);
395 
396 
397 std::ostream& operator<<(std::ostream& stream, const OperationID& operationId);
398 
399 
400 std::ostream& operator<<(std::ostream& stream, const OperationState& state);
401 
402 
403 std::ostream& operator<<(std::ostream& stream, const RateLimits& limits);
404 
405 
406 std::ostream& operator<<(
407  std::ostream& stream,
408  const ResourceProviderID& resourceProviderId);
409 
410 
411 std::ostream& operator<<(
412  std::ostream& stream,
413  const ResourceProviderInfo& resourceProviderInfo);
414 
415 
416 std::ostream& operator<<(std::ostream& stream, const RLimitInfo& rlimitInfo);
417 
418 
419 std::ostream& operator<<(std::ostream& stream, const SlaveID& slaveId);
420 
421 
422 std::ostream& operator<<(std::ostream& stream, const SlaveInfo& slave);
423 
424 
425 std::ostream& operator<<(std::ostream& stream, const TaskID& taskId);
426 
427 
428 std::ostream& operator<<(std::ostream& stream, const MachineID& machineId);
429 
430 
431 std::ostream& operator<<(std::ostream& stream, const TaskInfo& task);
432 
433 
434 std::ostream& operator<<(std::ostream& stream, const TaskState& state);
435 
436 
437 std::ostream& operator<<(
438  std::ostream& stream,
439  const UUID& uuid);
440 
441 
442 std::ostream& operator<<(std::ostream& stream, const CheckInfo::Type& type);
443 
444 
445 std::ostream& operator<<(
446  std::ostream& stream,
447  const CSIPluginContainerInfo::Service& service);
448 
449 
450 std::ostream& operator<<(
451  std::ostream& stream,
452  const FrameworkInfo::Capability& capability);
453 
454 
455 std::ostream& operator<<(std::ostream& stream, const Image::Type& imageType);
456 
457 
458 std::ostream& operator<<(std::ostream& stream, const Secret::Type& secretType);
459 
460 
461 std::ostream& operator<<(
462  std::ostream& stream,
463  const Offer::Operation::Type& operationType);
464 
465 
466 std::ostream& operator<<(
467  std::ostream& stream,
468  const Resource::DiskInfo::Source::Type& sourceType);
469 
470 
471 template <typename T>
472 inline std::ostream& operator<<(
473  std::ostream& stream,
474  const google::protobuf::RepeatedPtrField<T>& messages)
475 {
476  stream << "[ ";
477  for (auto it = messages.begin(); it != messages.end(); ++it) {
478  if (it != messages.begin()) {
479  stream << ", ";
480  }
481  stream << *it;
482  }
483  stream << " ]";
484  return stream;
485 }
486 
487 
488 template <typename T>
489 inline std::ostream& operator<<(
490  std::ostream& stream,
491  const std::vector<T>& messages)
492 {
493  stream << "[ ";
494  for (auto it = messages.begin(); it != messages.end(); ++it) {
495  if (it != messages.begin()) {
496  stream << ", ";
497  }
498  stream << *it;
499  }
500  stream << " ]";
501  return stream;
502 }
503 
504 } // namespace mesos {
505 
506 namespace std {
507 
508 template <>
509 struct hash<mesos::CommandInfo_URI>
510 {
511  typedef size_t result_type;
512 
513  typedef mesos::CommandInfo_URI argument_type;
514 
516  {
517  size_t seed = 0;
518 
519  if (uri.extract()) {
520  seed += 11;
521  }
522 
523  if (uri.executable()) {
524  seed += 2003;
525  }
526 
527  boost::hash_combine(seed, uri.value());
528  return seed;
529  }
530 };
531 
532 
533 template <>
534 struct hash<mesos::ContainerID>
535 {
536  typedef size_t result_type;
537 
538  typedef mesos::ContainerID argument_type;
539 
540  result_type operator()(const argument_type& containerId) const
541  {
542  size_t seed = 0;
543  boost::hash_combine(seed, containerId.value());
544 
545  if (containerId.has_parent()) {
546  boost::hash_combine(
547  seed,
548  std::hash<mesos::ContainerID>()(containerId.parent()));
549  }
550 
551  return seed;
552  }
553 };
554 
555 
556 template <>
557 struct hash<mesos::ExecutorID>
558 {
559  typedef size_t result_type;
560 
561  typedef mesos::ExecutorID argument_type;
562 
563  result_type operator()(const argument_type& executorId) const
564  {
565  size_t seed = 0;
566  boost::hash_combine(seed, executorId.value());
567  return seed;
568  }
569 };
570 
571 
572 template <>
573 struct hash<mesos::FrameworkID>
574 {
575  typedef size_t result_type;
576 
577  typedef mesos::FrameworkID argument_type;
578 
579  result_type operator()(const argument_type& frameworkId) const
580  {
581  size_t seed = 0;
582  boost::hash_combine(seed, frameworkId.value());
583  return seed;
584  }
585 };
586 
587 
588 template <>
589 struct hash<mesos::OfferID>
590 {
591  typedef size_t result_type;
592 
593  typedef mesos::OfferID argument_type;
594 
595  result_type operator()(const argument_type& offerId) const
596  {
597  size_t seed = 0;
598  boost::hash_combine(seed, offerId.value());
599  return seed;
600  }
601 };
602 
603 
604 template <>
605 struct hash<mesos::SlaveID>
606 {
607  typedef size_t result_type;
608 
609  typedef mesos::SlaveID argument_type;
610 
611  result_type operator()(const argument_type& slaveId) const
612  {
613  size_t seed = 0;
614  boost::hash_combine(seed, slaveId.value());
615  return seed;
616  }
617 };
618 
619 
620 template <>
621 struct hash<mesos::TaskID>
622 {
623  typedef size_t result_type;
624 
625  typedef mesos::TaskID argument_type;
626 
627  result_type operator()(const argument_type& taskId) const
628  {
629  size_t seed = 0;
630  boost::hash_combine(seed, taskId.value());
631  return seed;
632  }
633 };
634 
635 
636 template <>
637 struct hash<mesos::TaskState>
638 {
639  typedef size_t result_type;
640 
641  typedef mesos::TaskState argument_type;
642 
643  result_type operator()(const argument_type& taskState) const
644  {
645  // Use the underlying type of the enum as hash value.
646  return static_cast<size_t>(taskState);
647  }
648 };
649 
650 
651 template <>
652 struct hash<mesos::TaskStatus_Source>
653 {
654  typedef size_t result_type;
655 
656  typedef mesos::TaskStatus_Source argument_type;
657 
658  result_type operator()(const argument_type& source) const
659  {
660  // Use the underlying type of the enum as hash value.
661  return static_cast<size_t>(source);
662  }
663 };
664 
665 
666 template <>
667 struct hash<mesos::TaskStatus_Reason>
668 {
669  typedef size_t result_type;
670 
671  typedef mesos::TaskStatus_Reason argument_type;
672 
673  result_type operator()(const argument_type& reason) const
674  {
675  // Use the underlying type of the enum as hash value.
676  return static_cast<size_t>(reason);
677  }
678 };
679 
680 
681 template <>
682 struct hash<mesos::Image::Type>
683 {
684  typedef size_t result_type;
685 
687 
688  result_type operator()(const argument_type& imageType) const
689  {
690  // Use the underlying type of the enum as hash value.
691  return static_cast<size_t>(imageType);
692  }
693 };
694 
695 
696 template <>
697 struct hash<std::pair<mesos::FrameworkID, mesos::ExecutorID>>
698 {
699  typedef size_t result_type;
700 
701  typedef std::pair<
702  mesos::FrameworkID, mesos::ExecutorID> argument_type;
703 
705  {
706  size_t seed = 0;
707  boost::hash_combine(seed, std::hash<mesos::FrameworkID>()(pair.first));
708  boost::hash_combine(seed, std::hash<mesos::ExecutorID>()(pair.second));
709  return seed;
710  }
711 };
712 
713 
714 template <>
715 struct hash<mesos::MachineID>
716 {
717  typedef size_t result_type;
718 
719  typedef mesos::MachineID argument_type;
720 
721  result_type operator()(const argument_type& machineId) const
722  {
723  size_t seed = 0;
724  boost::hash_combine(seed, strings::lower(machineId.hostname()));
725  boost::hash_combine(seed, machineId.ip());
726  return seed;
727  }
728 };
729 
730 
731 template <>
732 struct hash<mesos::OperationID>
733 {
734  typedef size_t result_type;
735 
736  typedef mesos::OperationID argument_type;
737 
738  result_type operator()(const argument_type& operationId) const
739  {
740  size_t seed = 0;
741  boost::hash_combine(seed, operationId.value());
742  return seed;
743  }
744 };
745 
746 
747 template <>
748 struct hash<mesos::ResourceProviderID>
749 {
750  typedef size_t result_type;
751 
752  typedef mesos::ResourceProviderID argument_type;
753 
754  result_type operator()(const argument_type& resourceProviderId) const
755  {
756  size_t seed = 0;
757  boost::hash_combine(seed, resourceProviderId.value());
758  return seed;
759  }
760 };
761 
762 } // namespace std {
763 
764 #endif // __MESOS_TYPE_UTILS_H__
std::ostream & operator<<(std::ostream &stream, const Attribute &attribute)
Definition: type_utils.hpp:573
mesos::MachineID argument_type
Definition: type_utils.hpp:719
result_type operator()(const argument_type &executorId) const
Definition: type_utils.hpp:563
size_t result_type
Definition: type_utils.hpp:559
mesos::CommandInfo_URI argument_type
Definition: type_utils.hpp:513
result_type operator()(const argument_type &containerId) const
Definition: type_utils.hpp:540
mesos::Image::Type argument_type
Definition: type_utils.hpp:686
result_type operator()(const argument_type &slaveId) const
Definition: type_utils.hpp:611
size_t result_type
Definition: type_utils.hpp:623
result_type operator()(const argument_type &resourceProviderId) const
Definition: type_utils.hpp:754
mesos::TaskStatus_Source argument_type
Definition: type_utils.hpp:656
bool operator==(const Resource::ReservationInfo &left, const Resource::ReservationInfo &right)
result_type operator()(const argument_type &offerId) const
Definition: type_utils.hpp:595
mesos::TaskState argument_type
Definition: type_utils.hpp:641
mesos::ContainerID argument_type
Definition: type_utils.hpp:538
Capability
Definition: capabilities.hpp:35
result_type operator()(const argument_type &reason) const
Definition: type_utils.hpp:673
result_type operator()(const argument_type &machineId) const
Definition: type_utils.hpp:721
result_type operator()(const argument_type &uri) const
Definition: type_utils.hpp:515
size_t result_type
Definition: type_utils.hpp:717
bool operator<(const ContainerID &left, const ContainerID &right)
Definition: type_utils.hpp:314
mesos::TaskID argument_type
Definition: type_utils.hpp:625
size_t result_type
Definition: type_utils.hpp:669
size_t result_type
Definition: type_utils.hpp:654
mesos::ResourceProviderID argument_type
Definition: type_utils.hpp:752
mesos::FrameworkID argument_type
Definition: type_utils.hpp:577
size_t result_type
Definition: type_utils.hpp:684
result_type operator()(const argument_type &source) const
Definition: type_utils.hpp:658
bool operator!=(const Resource::ReservationInfo &left, const Resource::ReservationInfo &right)
Environment * environment
size_t result_type
Definition: type_utils.hpp:591
size_t result_type
Definition: type_utils.hpp:639
size_t result_type
Definition: type_utils.hpp:734
result_type operator()(const argument_type &taskId) const
Definition: type_utils.hpp:627
size_t result_type
Definition: type_utils.hpp:511
std::pair< mesos::FrameworkID, mesos::ExecutorID > argument_type
Definition: type_utils.hpp:702
result_type operator()(const argument_type &frameworkId) const
Definition: type_utils.hpp:579
result_type operator()(const argument_type &pair) const
Definition: type_utils.hpp:704
Type
Definition: capabilities.hpp:79
Try< uint32_t > type(const std::string &path)
Definition: type_utils.hpp:534
size_t result_type
Definition: type_utils.hpp:536
mesos::ExecutorID argument_type
Definition: type_utils.hpp:561
result_type operator()(const argument_type &imageType) const
Definition: type_utils.hpp:688
size_t result_type
Definition: type_utils.hpp:607
mesos::SlaveID argument_type
Definition: type_utils.hpp:609
mesos::TaskStatus_Reason argument_type
Definition: type_utils.hpp:671
std::string lower(const std::string &s)
Definition: strings.hpp:412
Option< Error > checkStatusInfo(const CheckStatusInfo &checkStatusInfo)
mesos::OperationID argument_type
Definition: type_utils.hpp:736
mesos::OfferID argument_type
Definition: type_utils.hpp:593
size_t result_type
Definition: type_utils.hpp:750
result_type operator()(const argument_type &operationId) const
Definition: type_utils.hpp:738
Definition: type_utils.hpp:557
size_t result_type
Definition: type_utils.hpp:575
result_type operator()(const argument_type &taskState) const
Definition: type_utils.hpp:643