OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ResourceGrantPolicy.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cmath>
18 #include <limits>
19 #include <sstream>
20 
21 #include "Logger/Logger.h"
22 #include "ResourceGrantPolicy.h"
23 
24 namespace ExecutorResourceMgr_Namespace {
25 
27  const ResourceSubtype resource_subtype,
28  const ResourceGrantPolicySizeType policy_size_type,
29  const ResourceGrantSizeInfo size_info)
30  : resource_subtype(resource_subtype)
31  , policy_size_type(policy_size_type)
32  , size_info(size_info) {}
33 
35  const size_t total_resource_quantity) const {
36  switch (policy_size_type) {
38  return std::numeric_limits<size_t>::max();
40  return size_info.constant;
42  return static_cast<size_t>(
43  ceil(size_info.ratio_to_total * total_resource_quantity));
44  default:
45  UNREACHABLE();
46  }
47  UNREACHABLE();
48  return std::numeric_limits<size_t>::max();
49 }
50 // Optionally caps quantity at total available, for case of DISALLOW_REQUESTS
51 // for ConcurrentResourceGrantPolicy oversubscription sub-policy
52 size_t ResourceGrantPolicy::get_grant_quantity(const size_t total_resource_quantity,
53  const bool cap_at_total_available) const {
54  const size_t max_per_query_grant_without_cap =
55  get_grant_quantity(total_resource_quantity);
56  if (cap_at_total_available) {
57  return std::min(max_per_query_grant_without_cap, total_resource_quantity);
58  }
59  return max_per_query_grant_without_cap;
60 }
61 
62 std::string ResourceGrantPolicy::to_string() const {
63  std::ostringstream oss;
64  oss << "RESOURCE TYPE: "
65  << ResourceSubtypeStrings[static_cast<size_t>(resource_subtype)] << " ";
66  switch (policy_size_type) {
68  oss << "SIZE TYPE: Unlimited";
69  break;
71  oss << "SIZE TYPE: Constant (" << size_info.constant << ")";
72  break;
74  oss << "SIZE TYPE: Ratio-to-total (" << size_info.ratio_to_total << ")";
75  break;
76  default:
77  UNREACHABLE();
78  }
79  return oss.str();
80 }
81 
83  const ResourceSubtype resource_subtype) {
84  // Make a dummy size_info union
85  const ResourceGrantSizeInfo size_info;
86  const ResourceGrantPolicy resource_grant_policy(
87  resource_subtype, ResourceGrantPolicySizeType::UNLIMITED, size_info);
88  return resource_grant_policy;
89 }
90 
92  const ResourceSubtype resource_subtype,
93  const size_t constant_grant) {
94  const ResourceGrantSizeInfo size_info(constant_grant);
95  const ResourceGrantPolicy resource_grant_policy(
96  resource_subtype, ResourceGrantPolicySizeType::CONSTANT, size_info);
97  return resource_grant_policy;
98 }
99 
101  const ResourceSubtype resource_subtype,
102  const double ratio_grant) {
103  const ResourceGrantSizeInfo size_info(ratio_grant);
104  const ResourceGrantPolicy resource_grant_policy(
105  resource_subtype, ResourceGrantPolicySizeType::RATIO_TO_TOTAL, size_info);
106  return resource_grant_policy;
107 }
108 
110  const ResourceConcurrencyPolicy& resource_concurrency_policy) {
111  std::ostringstream oss;
112  switch (resource_concurrency_policy) {
114  oss << "Allow concurrent requests";
115  break;
117  oss << "Allow single request for resource type";
118  break;
120  oss << "Allow single request globally";
121  break;
122  case DISALLOW_REQUESTS:
123  oss << "Disallow requests";
124  break;
125  default:
126  UNREACHABLE();
127  }
128  return oss.str();
129 }
130 
132  const ConcurrentResourceGrantPolicy& rhs) const {
133  return resource_type == rhs.resource_type &&
136 }
137 
139  std::ostringstream oss;
140  oss << "Resource Type: " << resource_type_to_string(resource_type)
141  << "Undersubscribed Policy: "
143  << " Oversubscribed Policy: "
145  return oss.str();
146 }
147 
148 } // namespace ExecutorResourceMgr_Namespace
bool operator==(const ConcurrentResourceGrantPolicy &rhs) const
ResourceConcurrencyPolicy
Specifies whether grants for a specified resource can be made concurrently (ALLOW_CONCURRENT_REQEUSTS...
size_t get_grant_quantity(const size_t total_resource_quantity) const
Used to calculate the maximum quantity that can be granted of the resource.
ResourceType resource_type
The type of a resource this concurrent resource grant policy pertains to.
ResourceGrantPolicySizeType
The sizing type for a ResourceGrantPolicy of a given resource type, specifying whether resource grant...
#define UNREACHABLE()
Definition: Logger.h:338
Specifies the policies for resource grants in the presence of other requests, both under situations o...
ResourceGrantPolicy gen_unlimited_resource_grant_policy(const ResourceSubtype resource_subtype)
Generates a ResourceGrantPolicy with ResourceGrantPolicySizeType::UNLIMITED
ResourceSubtype
Stores the resource sub-type for a ExecutorResourcePool request.
ResourceConcurrencyPolicy concurrency_policy
The grant policy in effect when there are concurrent requests for the resource specified by resource_...
ResourceGrantPolicy gen_ratio_resource_grant_policy(const ResourceSubtype resource_subtype, const double ratio_grant)
Generates a ResourceGrantPolicy with ResourceGrantPolicySizeType::RATIO_TO_TOTAL
std::string get_resource_concurrency_policy_string(const ResourceConcurrencyPolicy &resource_concurrency_policy)
ResourceConcurrencyPolicy oversubscription_concurrency_policy
The grant policy in effect when there are concurrent requests for the resource specified by resource_...
Specifies the policy for granting a resource of a specific ResourceSubtype. Note that this policy onl...
ResourceGrantPolicy gen_constant_resource_grant_policy(const ResourceSubtype resource_subtype, const size_t constant_grant)
Generates a ResourceGrantPolicy with ResourceGrantPolicySizeType::CONSTANT
std::string resource_type_to_string(const ResourceType resource_type)
static const char * ResourceSubtypeStrings[]