Skip to content

Commit 755b92d

Browse files
author
Bikas Saha
committed
Merge r1495538 from branch-2 to branch-2.1-beta for YARN-827. Need to make Resource arithmetic methods accessible (Jian He via bikas)
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2.1-beta@1495540 13f79535-47bb-0310-9956-ffa450edef68
1 parent 47e76f0 commit 755b92d

File tree

57 files changed

+764
-75
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+764
-75
lines changed

hadoop-yarn-project/CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ Release 2.1.0-beta - UNRELEASED
393393
YARN-846. Move pb Impl classes from yarn-api to yarn-common. (Jian He via
394394
vinodkv)
395395

396+
YARN-827. Need to make Resource arithmetic methods accessible (Jian He via
397+
bikas)
398+
396399
OPTIMIZATIONS
397400

398401
YARN-512. Log aggregation root directory check is more expensive than it
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.yarn.util.resource;
19+
20+
import org.apache.hadoop.classification.InterfaceAudience.Private;
21+
import org.apache.hadoop.classification.InterfaceStability.Unstable;
22+
import org.apache.hadoop.yarn.api.records.Resource;
23+
24+
@Private
25+
@Unstable
26+
public class DefaultResourceCalculator extends ResourceCalculator {
27+
28+
@Override
29+
public int compare(Resource unused, Resource lhs, Resource rhs) {
30+
// Only consider memory
31+
return lhs.getMemory() - rhs.getMemory();
32+
}
33+
34+
@Override
35+
public int computeAvailableContainers(Resource available, Resource required) {
36+
// Only consider memory
37+
return available.getMemory() / required.getMemory();
38+
}
39+
40+
@Override
41+
public float divide(Resource unused,
42+
Resource numerator, Resource denominator) {
43+
return ratio(numerator, denominator);
44+
}
45+
46+
@Override
47+
public float ratio(Resource a, Resource b) {
48+
return (float)a.getMemory() / b.getMemory();
49+
}
50+
51+
@Override
52+
public Resource divideAndCeil(Resource numerator, int denominator) {
53+
return Resources.createResource(
54+
divideAndCeil(numerator.getMemory(), denominator));
55+
}
56+
57+
@Override
58+
public Resource normalize(Resource r, Resource minimumResource,
59+
Resource maximumResource, Resource stepFactor) {
60+
int normalizedMemory = Math.min(
61+
roundUp(
62+
Math.max(r.getMemory(), minimumResource.getMemory()),
63+
stepFactor.getMemory()),
64+
maximumResource.getMemory());
65+
return Resources.createResource(normalizedMemory);
66+
}
67+
68+
@Override
69+
public Resource normalize(Resource r, Resource minimumResource,
70+
Resource maximumResource) {
71+
return normalize(r, minimumResource, maximumResource, minimumResource);
72+
}
73+
74+
@Override
75+
public Resource roundUp(Resource r, Resource stepFactor) {
76+
return Resources.createResource(
77+
roundUp(r.getMemory(), stepFactor.getMemory())
78+
);
79+
}
80+
81+
@Override
82+
public Resource roundDown(Resource r, Resource stepFactor) {
83+
return Resources.createResource(
84+
roundDown(r.getMemory(), stepFactor.getMemory()));
85+
}
86+
87+
@Override
88+
public Resource multiplyAndNormalizeUp(Resource r, double by,
89+
Resource stepFactor) {
90+
return Resources.createResource(
91+
roundUp((int)(r.getMemory() * by + 0.5), stepFactor.getMemory())
92+
);
93+
}
94+
95+
@Override
96+
public Resource multiplyAndNormalizeDown(Resource r, double by,
97+
Resource stepFactor) {
98+
return Resources.createResource(
99+
roundDown(
100+
(int)(r.getMemory() * by),
101+
stepFactor.getMemory()
102+
)
103+
);
104+
}
105+
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.yarn.util.resource;
19+
20+
import org.apache.hadoop.classification.InterfaceAudience.Private;
21+
import org.apache.hadoop.classification.InterfaceStability.Unstable;
22+
import org.apache.hadoop.yarn.api.records.Resource;
23+
24+
/**
25+
* A {@link ResourceCalculator} which uses the concept of
26+
* <em>dominant resource</em> to compare multi-dimensional resources.
27+
*
28+
* Essentially the idea is that the in a multi-resource environment,
29+
* the resource allocation should be determined by the dominant share
30+
* of an entity (user or queue), which is the maximum share that the
31+
* entity has been allocated of any resource.
32+
*
33+
* In a nutshell, it seeks to maximize the minimum dominant share across
34+
* all entities.
35+
*
36+
* For example, if user A runs CPU-heavy tasks and user B runs
37+
* memory-heavy tasks, it attempts to equalize CPU share of user A
38+
* with Memory-share of user B.
39+
*
40+
* In the single resource case, it reduces to max-min fairness for that resource.
41+
*
42+
* See the Dominant Resource Fairness paper for more details:
43+
* www.cs.berkeley.edu/~matei/papers/2011/nsdi_drf.pdf
44+
*/
45+
@Private
46+
@Unstable
47+
public class DominantResourceCalculator extends ResourceCalculator {
48+
49+
@Override
50+
public int compare(Resource clusterResource, Resource lhs, Resource rhs) {
51+
52+
if (lhs.equals(rhs)) {
53+
return 0;
54+
}
55+
56+
float l = getResourceAsValue(clusterResource, lhs, true);
57+
float r = getResourceAsValue(clusterResource, rhs, true);
58+
59+
if (l < r) {
60+
return -1;
61+
} else if (l > r) {
62+
return 1;
63+
} else {
64+
l = getResourceAsValue(clusterResource, lhs, false);
65+
r = getResourceAsValue(clusterResource, rhs, false);
66+
if (l < r) {
67+
return -1;
68+
} else if (l > r) {
69+
return 1;
70+
}
71+
}
72+
73+
return 0;
74+
}
75+
76+
/**
77+
* Use 'dominant' for now since we only have 2 resources - gives us a slight
78+
* performance boost.
79+
*
80+
* Once we add more resources, we'll need a more complicated (and slightly
81+
* less performant algorithm).
82+
*/
83+
protected float getResourceAsValue(
84+
Resource clusterResource, Resource resource, boolean dominant) {
85+
// Just use 'dominant' resource
86+
return (dominant) ?
87+
Math.max(
88+
(float)resource.getMemory() / clusterResource.getMemory(),
89+
(float)resource.getVirtualCores() / clusterResource.getVirtualCores()
90+
)
91+
:
92+
Math.min(
93+
(float)resource.getMemory() / clusterResource.getMemory(),
94+
(float)resource.getVirtualCores() / clusterResource.getVirtualCores()
95+
);
96+
}
97+
98+
@Override
99+
public int computeAvailableContainers(Resource available, Resource required) {
100+
return Math.min(
101+
available.getMemory() / required.getMemory(),
102+
available.getVirtualCores() / required.getVirtualCores());
103+
}
104+
105+
@Override
106+
public float divide(Resource clusterResource,
107+
Resource numerator, Resource denominator) {
108+
return
109+
getResourceAsValue(clusterResource, numerator, true) /
110+
getResourceAsValue(clusterResource, denominator, true);
111+
}
112+
113+
@Override
114+
public float ratio(Resource a, Resource b) {
115+
return Math.max(
116+
(float)a.getMemory()/b.getMemory(),
117+
(float)a.getVirtualCores()/b.getVirtualCores()
118+
);
119+
}
120+
121+
@Override
122+
public Resource divideAndCeil(Resource numerator, int denominator) {
123+
return Resources.createResource(
124+
divideAndCeil(numerator.getMemory(), denominator),
125+
divideAndCeil(numerator.getVirtualCores(), denominator)
126+
);
127+
}
128+
129+
@Override
130+
public Resource normalize(Resource r, Resource minimumResource,
131+
Resource maximumResource, Resource stepFactor) {
132+
int normalizedMemory = Math.min(
133+
roundUp(
134+
Math.max(r.getMemory(), minimumResource.getMemory()),
135+
stepFactor.getMemory()),
136+
maximumResource.getMemory());
137+
int normalizedCores = Math.min(
138+
roundUp(
139+
Math.max(r.getVirtualCores(), minimumResource.getVirtualCores()),
140+
stepFactor.getVirtualCores()),
141+
maximumResource.getVirtualCores());
142+
return Resources.createResource(normalizedMemory,
143+
normalizedCores);
144+
}
145+
146+
@Override
147+
public Resource roundUp(Resource r, Resource stepFactor) {
148+
return Resources.createResource(
149+
roundUp(r.getMemory(), stepFactor.getMemory()),
150+
roundUp(r.getVirtualCores(), stepFactor.getVirtualCores())
151+
);
152+
}
153+
154+
@Override
155+
public Resource roundDown(Resource r, Resource stepFactor) {
156+
return Resources.createResource(
157+
roundDown(r.getMemory(), stepFactor.getMemory()),
158+
roundDown(r.getVirtualCores(), stepFactor.getVirtualCores())
159+
);
160+
}
161+
162+
@Override
163+
public Resource multiplyAndNormalizeUp(Resource r, double by,
164+
Resource stepFactor) {
165+
return Resources.createResource(
166+
roundUp(
167+
(int)Math.ceil(r.getMemory() * by), stepFactor.getMemory()),
168+
roundUp(
169+
(int)Math.ceil(r.getVirtualCores() * by),
170+
stepFactor.getVirtualCores())
171+
);
172+
}
173+
174+
@Override
175+
public Resource multiplyAndNormalizeDown(Resource r, double by,
176+
Resource stepFactor) {
177+
return Resources.createResource(
178+
roundDown(
179+
(int)(r.getMemory() * by),
180+
stepFactor.getMemory()
181+
),
182+
roundDown(
183+
(int)(r.getVirtualCores() * by),
184+
stepFactor.getVirtualCores()
185+
)
186+
);
187+
}
188+
189+
}

0 commit comments

Comments
 (0)