|
| 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.server.globalpolicygenerator.webapp; |
| 19 | + |
| 20 | +import com.google.inject.Inject; |
| 21 | +import org.apache.hadoop.yarn.server.federation.policies.dao.WeightedPolicyInfo; |
| 22 | +import org.apache.hadoop.yarn.server.federation.policies.exceptions.FederationPolicyInitializationException; |
| 23 | +import org.apache.hadoop.yarn.server.federation.store.records.SubClusterIdInfo; |
| 24 | +import org.apache.hadoop.yarn.server.federation.store.records.SubClusterPolicyConfiguration; |
| 25 | +import org.apache.hadoop.yarn.server.federation.utils.FederationStateStoreFacade; |
| 26 | +import org.apache.hadoop.yarn.server.globalpolicygenerator.GlobalPolicyGenerator; |
| 27 | +import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet; |
| 28 | +import org.apache.hadoop.yarn.webapp.view.HtmlBlock; |
| 29 | + |
| 30 | +import java.nio.ByteBuffer; |
| 31 | +import java.util.Collection; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +/** |
| 35 | + * Overview block for the GPG Policies Web UI. |
| 36 | + */ |
| 37 | +public class GPGPoliciesBlock extends HtmlBlock { |
| 38 | + |
| 39 | + private final GlobalPolicyGenerator gpg; |
| 40 | + |
| 41 | + private final FederationStateStoreFacade facade; |
| 42 | + |
| 43 | + @Inject |
| 44 | + GPGPoliciesBlock(GlobalPolicyGenerator gpg, ViewContext ctx) { |
| 45 | + super(ctx); |
| 46 | + this.gpg = gpg; |
| 47 | + this.facade = FederationStateStoreFacade.getInstance(gpg.getConfig()); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void render(Block html) { |
| 52 | + try { |
| 53 | + Collection<SubClusterPolicyConfiguration> policies = |
| 54 | + facade.getPoliciesConfigurations().values(); |
| 55 | + initYarnFederationPolicies(policies, html); |
| 56 | + } catch (Exception e) { |
| 57 | + LOG.error("Get GPGPolicies Error.", e); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private void initYarnFederationPolicies(Collection<SubClusterPolicyConfiguration> policies, |
| 62 | + Block html) throws FederationPolicyInitializationException { |
| 63 | + |
| 64 | + Hamlet.TBODY<Hamlet.TABLE<Hamlet>> tbody = html.table("#policies"). |
| 65 | + thead(). |
| 66 | + tr(). |
| 67 | + th(".queue", "Queue Name"). |
| 68 | + th(".policyType", "Policy Type"). |
| 69 | + th(".routerPolicyWeights", "Router PolicyWeights"). |
| 70 | + th(".amrmPolicyWeights", "Router AMRMPolicyWeights"). |
| 71 | + th(".headroomAlpha", "Router Headroom Alpha"). |
| 72 | + __().__(). |
| 73 | + tbody(); |
| 74 | + |
| 75 | + if (policies != null) { |
| 76 | + for (SubClusterPolicyConfiguration policy : policies) { |
| 77 | + Hamlet.TR<Hamlet.TBODY<Hamlet.TABLE<Hamlet>>> row = tbody.tr().td(policy.getQueue()); |
| 78 | + // Policy Type |
| 79 | + String type = policy.getType(); |
| 80 | + row = row.td(type); |
| 81 | + |
| 82 | + // WeightedPolicyInfo |
| 83 | + ByteBuffer params = policy.getParams(); |
| 84 | + WeightedPolicyInfo weightedPolicyInfo = WeightedPolicyInfo.fromByteBuffer(params); |
| 85 | + row = row.td(policyWeight2String(weightedPolicyInfo.getRouterPolicyWeights())); |
| 86 | + row = row.td(policyWeight2String(weightedPolicyInfo.getAMRMPolicyWeights())); |
| 87 | + row.td(String.valueOf(weightedPolicyInfo.getHeadroomAlpha())).__(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + tbody.__().__(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * We will convert the PolicyWeight to string format. |
| 96 | + * |
| 97 | + * @param weights PolicyWeight. |
| 98 | + * @return string format PolicyWeight. example: SC-1:0.91, SC-2:0.09 |
| 99 | + */ |
| 100 | + private String policyWeight2String(Map<SubClusterIdInfo, Float> weights) { |
| 101 | + StringBuilder sb = new StringBuilder(); |
| 102 | + for (Map.Entry<SubClusterIdInfo, Float> entry : weights.entrySet()) { |
| 103 | + sb.append(entry.getKey().toId()).append(": ").append(entry.getValue()).append(", "); |
| 104 | + } |
| 105 | + if (sb.length() > 2) { |
| 106 | + sb.setLength(sb.length() - 2); |
| 107 | + } |
| 108 | + return sb.toString(); |
| 109 | + } |
| 110 | +} |
0 commit comments