Skip to content

Commit 53c3ae1

Browse files
authored
YARN-11610. [Federation] Add WeightedHomePolicyManager. (#6256) Contributed by Shilun Fan.
Reviewed-by: Inigo Goiri <[email protected]> Signed-off-by: Shilun Fan <[email protected]>
1 parent 0c10bab commit 53c3ae1

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to you under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* 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, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
18+
package org.apache.hadoop.yarn.server.federation.policies.manager;
19+
20+
import org.apache.hadoop.classification.VisibleForTesting;
21+
import org.apache.hadoop.yarn.server.federation.policies.amrmproxy.HomeAMRMProxyPolicy;
22+
import org.apache.hadoop.yarn.server.federation.policies.dao.WeightedPolicyInfo;
23+
import org.apache.hadoop.yarn.server.federation.policies.exceptions.FederationPolicyInitializationException;
24+
import org.apache.hadoop.yarn.server.federation.policies.router.WeightedRandomRouterPolicy;
25+
import org.apache.hadoop.yarn.server.federation.store.records.SubClusterPolicyConfiguration;
26+
27+
import java.nio.ByteBuffer;
28+
29+
/**
30+
* Policy that allows operator to configure "weights" for routing. This picks a
31+
* {@link WeightedRandomRouterPolicy} for the router and a {@link
32+
* HomeAMRMProxyPolicy} for the amrmproxy as they are designed to
33+
* work together.
34+
*/
35+
public class WeightedHomePolicyManager extends AbstractPolicyManager {
36+
37+
private WeightedPolicyInfo weightedPolicyInfo;
38+
39+
public WeightedHomePolicyManager() {
40+
// this structurally hard-codes two compatible policies for Router and
41+
// AMRMProxy.
42+
routerFederationPolicy = WeightedRandomRouterPolicy.class;
43+
amrmProxyFederationPolicy = HomeAMRMProxyPolicy.class;
44+
weightedPolicyInfo = new WeightedPolicyInfo();
45+
}
46+
47+
@Override
48+
public SubClusterPolicyConfiguration serializeConf()
49+
throws FederationPolicyInitializationException {
50+
ByteBuffer buf = weightedPolicyInfo.toByteBuffer();
51+
return SubClusterPolicyConfiguration
52+
.newInstance(getQueue(), this.getClass().getCanonicalName(), buf);
53+
}
54+
55+
@VisibleForTesting
56+
public WeightedPolicyInfo getWeightedPolicyInfo() {
57+
return weightedPolicyInfo;
58+
}
59+
60+
@VisibleForTesting
61+
public void setWeightedPolicyInfo(
62+
WeightedPolicyInfo weightedPolicyInfo) {
63+
this.weightedPolicyInfo = weightedPolicyInfo;
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to you under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* 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, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
package org.apache.hadoop.yarn.server.federation.policies.manager;
18+
19+
import org.apache.hadoop.yarn.server.federation.policies.amrmproxy.HomeAMRMProxyPolicy;
20+
import org.apache.hadoop.yarn.server.federation.policies.dao.WeightedPolicyInfo;
21+
import org.apache.hadoop.yarn.server.federation.policies.router.WeightedRandomRouterPolicy;
22+
import org.apache.hadoop.yarn.server.federation.store.records.SubClusterId;
23+
import org.apache.hadoop.yarn.server.federation.store.records.SubClusterIdInfo;
24+
import org.junit.Assert;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
31+
public class TestWeightedHomePolicyManager extends BasePolicyManagerTest {
32+
private WeightedPolicyInfo policyInfo;
33+
34+
@Before
35+
public void setup() {
36+
// configure a policy
37+
WeightedHomePolicyManager whpm = new WeightedHomePolicyManager();
38+
whpm.setQueue("queue1");
39+
40+
SubClusterId sc1 = SubClusterId.newInstance("sc1");
41+
policyInfo = new WeightedPolicyInfo();
42+
Map<SubClusterIdInfo, Float> routerWeights = new HashMap<>();
43+
routerWeights.put(new SubClusterIdInfo(sc1), 0.2f);
44+
policyInfo.setRouterPolicyWeights(routerWeights);
45+
46+
whpm.setWeightedPolicyInfo(policyInfo);
47+
this.wfp = whpm;
48+
49+
//set expected params that the base test class will use for tests
50+
expectedPolicyManager = WeightedHomePolicyManager.class;
51+
expectedAMRMProxyPolicy = HomeAMRMProxyPolicy.class;
52+
expectedRouterPolicy = WeightedRandomRouterPolicy.class;
53+
}
54+
55+
@Test
56+
public void testPolicyInfoSetCorrectly() throws Exception {
57+
serializeAndDeserializePolicyManager(wfp, expectedPolicyManager,
58+
expectedAMRMProxyPolicy, expectedRouterPolicy);
59+
// check the policyInfo propagates through ser/der correctly
60+
Assert.assertEquals(((WeightedHomePolicyManager) wfp)
61+
.getWeightedPolicyInfo(), policyInfo);
62+
}
63+
}

0 commit comments

Comments
 (0)