Apache Mesos
sorter.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 __MASTER_ALLOCATOR_SORTER_SORTER_HPP__
18 #define __MASTER_ALLOCATOR_SORTER_SORTER_HPP__
19 
20 #include <functional>
21 #include <string>
22 #include <vector>
23 
24 #include <mesos/resources.hpp>
25 #include <mesos/type_utils.hpp>
26 
27 #include <process/pid.hpp>
28 
29 namespace mesos {
30 namespace internal {
31 namespace master {
32 namespace allocator {
33 
34 // Sorters implement the logic for determining the
35 // order in which users or frameworks should receive
36 // resource allocations.
37 //
38 // TODO(bmahler): Templatize this on Client, so that callers can
39 // don't need to do string conversion, e.g. FrameworkID, string role,
40 // etc.
41 class Sorter
42 {
43 public:
44  Sorter() = default;
45 
46  // Provides the allocator's execution context (via a UPID)
47  // and a name prefix in order to support metrics within the
48  // sorter implementation.
49  explicit Sorter(
50  const process::UPID& allocator,
51  const std::string& metricsPrefix) {}
52 
53  virtual ~Sorter() = default;
54 
55  // Initialize the sorter.
56  virtual void initialize(
57  const Option<std::set<std::string>>& fairnessExcludeResourceNames) = 0;
58 
59  // Adds a client to allocate resources to.
60  // A client may be a user or a framework.
61  // This function will not activate the client.
62  virtual void add(const std::string& client) = 0;
63 
64  // Removes a client.
65  virtual void remove(const std::string& client) = 0;
66 
67  // Readds a client to the sort after deactivate.
68  // It is a no-op if the client is already in the sort.
69  virtual void activate(const std::string& client) = 0;
70 
71  // Removes a client from the sort, so it won't get allocated to.
72  // It is a no-op if the client is already not in the sort.
73  virtual void deactivate(const std::string& client) = 0;
74 
75  // Updates the weight of a client path. This changes the sorter's
76  // behavior for all clients in the subtree identified by this path
77  // (both clients currently in the sorter and any clients that may be
78  // added later). If a client's weight is not explicitly set, the
79  // default weight of 1.0 is used. This interface does not support
80  // unsetting previously set weights; instead, the weight should be
81  // reset to the default value.
82  virtual void updateWeight(const std::string& path, double weight) = 0;
83 
84  // Specify that resources have been allocated to the given client.
85  virtual void allocated(
86  const std::string& client,
87  const SlaveID& slaveId,
88  const Resources& resources) = 0;
89 
90  // Updates a portion of the allocation for the client, in order to
91  // augment the resources with additional metadata (e.g., volumes).
92  // This means that the new allocation must not affect the static
93  // roles, or the overall quantities of resources!
94  virtual void update(
95  const std::string& client,
96  const SlaveID& slaveId,
97  const Resources& oldAllocation,
98  const Resources& newAllocation) = 0;
99 
100  // Specify that resources have been unallocated from the given client.
101  virtual void unallocated(
102  const std::string& client,
103  const SlaveID& slaveId,
104  const Resources& resources) = 0;
105 
106  // Returns the resources that have been allocated to this client.
108  const std::string& client) const = 0;
109 
110  // Returns the total scalar resource quantities that are allocated to
111  // this client. This omits metadata about dynamic reservations and
112  // persistent volumes; see `Resources::createStrippedScalarQuantity`.
113  virtual const Resources& allocationScalarQuantities(
114  const std::string& client) const = 0;
115 
116  // Returns the clients that have allocations on this slave.
118  const SlaveID& slaveId) const = 0;
119 
120  // Returns the given slave's resources that have been allocated to
121  // this client.
122  virtual Resources allocation(
123  const std::string& client,
124  const SlaveID& slaveId) const = 0;
125 
126  // Returns the total scalar resource quantities in this sorter. This
127  // omits metadata about dynamic reservations and persistent volumes; see
128  // `Resources::createStrippedScalarQuantity`.
129  virtual const Resources& totalScalarQuantities() const = 0;
130 
131  // Add resources to the total pool of resources this
132  // Sorter should consider.
133  virtual void add(const SlaveID& slaveId, const Resources& resources) = 0;
134 
135  // Remove resources from the total pool.
136  virtual void remove(const SlaveID& slaveId, const Resources& resources) = 0;
137 
138  // Returns all of the clients in the order that they should
139  // be allocated to, according to this Sorter's policy.
140  virtual std::vector<std::string> sort() = 0;
141 
142  // Returns true if this Sorter contains the specified client,
143  // which may be active or inactive.
144  virtual bool contains(const std::string& client) const = 0;
145 
146  // Returns the number of clients this Sorter contains,
147  // either active or inactive.
148  virtual size_t count() const = 0;
149 };
150 
151 } // namespace allocator {
152 } // namespace master {
153 } // namespace internal {
154 } // namespace mesos {
155 
156 #endif // __MASTER_ALLOCATOR_SORTER_SORTER_HPP__
Definition: path.hpp:26
virtual void unallocated(const std::string &client, const SlaveID &slaveId, const Resources &resources)=0
Definition: option.hpp:28
Sorter(const process::UPID &allocator, const std::string &metricsPrefix)
Definition: sorter.hpp:49
Definition: master.hpp:27
Definition: resources.hpp:79
virtual void initialize(const Option< std::set< std::string >> &fairnessExcludeResourceNames)=0
virtual const hashmap< SlaveID, Resources > & allocation(const std::string &client) const =0
virtual void update(const std::string &client, const SlaveID &slaveId, const Resources &oldAllocation, const Resources &newAllocation)=0
virtual void updateWeight(const std::string &path, double weight)=0
Definition: hashmap.hpp:38
virtual const Resources & totalScalarQuantities() const =0
virtual const Resources & allocationScalarQuantities(const std::string &client) const =0
An "untyped" PID, used to encapsulate the process ID for lower-layer abstractions (eg...
Definition: pid.hpp:39
Definition: spec.hpp:30
virtual bool contains(const std::string &client) const =0
Definition: attributes.hpp:24
virtual void add(const std::string &client)=0
virtual std::vector< std::string > sort()=0
virtual void activate(const std::string &client)=0
virtual void deactivate(const std::string &client)=0
virtual void allocated(const std::string &client, const SlaveID &slaveId, const Resources &resources)=0