View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import java.lang.reflect.Type;
23  import java.util.List;
24  
25  import org.apache.commons.lang.builder.ToStringBuilder;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.util.GsonUtil;
29  
30  import org.apache.hbase.thirdparty.com.google.gson.Gson;
31  import org.apache.hbase.thirdparty.com.google.gson.JsonElement;
32  import org.apache.hbase.thirdparty.com.google.gson.JsonSerializationContext;
33  import org.apache.hbase.thirdparty.com.google.gson.JsonSerializer;
34  
35  /**
36   * History of balancer decisions taken for region movements.
37   */
38  @InterfaceAudience.Public
39  @InterfaceStability.Evolving
40  final public class BalancerDecision extends LogEntry {
41  
42    private final String initialFunctionCosts;
43    private final String finalFunctionCosts;
44    private final double initTotalCost;
45    private final double computedTotalCost;
46    private final long computedSteps;
47    private final List<String> regionPlans;
48  
49    // used to convert object to pretty printed format
50    // used by toJsonPrettyPrint()
51    private static final Gson GSON = GsonUtil.createGson()
52      .setPrettyPrinting()
53      .registerTypeAdapter(BalancerDecision.class, new JsonSerializer<BalancerDecision>() {
54        @Override
55        public JsonElement serialize(BalancerDecision balancerDecision, Type type,
56          JsonSerializationContext jsonSerializationContext) {
57          Gson gson = new Gson();
58          return gson.toJsonTree(balancerDecision);
59        }
60      }).create();
61  
62    private BalancerDecision(String initialFunctionCosts, String finalFunctionCosts,
63      double initTotalCost, double computedTotalCost, List<String> regionPlans,
64      long computedSteps) {
65      this.initialFunctionCosts = initialFunctionCosts;
66      this.finalFunctionCosts = finalFunctionCosts;
67      this.initTotalCost = initTotalCost;
68      this.computedTotalCost = computedTotalCost;
69      this.regionPlans = regionPlans;
70      this.computedSteps = computedSteps;
71    }
72  
73    public String getInitialFunctionCosts() {
74      return initialFunctionCosts;
75    }
76  
77    public String getFinalFunctionCosts() {
78      return finalFunctionCosts;
79    }
80  
81    public double getInitTotalCost() {
82      return initTotalCost;
83    }
84  
85    public double getComputedTotalCost() {
86      return computedTotalCost;
87    }
88  
89    public List<String> getRegionPlans() {
90      return regionPlans;
91    }
92  
93    public long getComputedSteps() {
94      return computedSteps;
95    }
96  
97    @Override
98    public String toString() {
99      return new ToStringBuilder(this)
100       .append("initialFunctionCosts", initialFunctionCosts)
101       .append("finalFunctionCosts", finalFunctionCosts)
102       .append("initTotalCost", initTotalCost)
103       .append("computedTotalCost", computedTotalCost)
104       .append("computedSteps", computedSteps)
105       .append("regionPlans", regionPlans)
106       .toString();
107   }
108 
109   @Override
110   public String toJsonPrettyPrint() {
111     return GSON.toJson(this);
112   }
113 
114   @InterfaceAudience.Public
115   @InterfaceStability.Evolving
116   public static class Builder {
117     private String initialFunctionCosts;
118     private String finalFunctionCosts;
119     private double initTotalCost;
120     private double computedTotalCost;
121     private long computedSteps;
122     private List<String> regionPlans;
123 
124     public Builder setInitialFunctionCosts(String initialFunctionCosts) {
125       this.initialFunctionCosts = initialFunctionCosts;
126       return this;
127     }
128 
129     public Builder setFinalFunctionCosts(String finalFunctionCosts) {
130       this.finalFunctionCosts = finalFunctionCosts;
131       return this;
132     }
133 
134     public Builder setInitTotalCost(double initTotalCost) {
135       this.initTotalCost = initTotalCost;
136       return this;
137     }
138 
139     public Builder setComputedTotalCost(double computedTotalCost) {
140       this.computedTotalCost = computedTotalCost;
141       return this;
142     }
143 
144     public Builder setRegionPlans(List<String> regionPlans) {
145       this.regionPlans = regionPlans;
146       return this;
147     }
148 
149     public Builder setComputedSteps(long computedSteps) {
150       this.computedSteps = computedSteps;
151       return this;
152     }
153 
154     public BalancerDecision build() {
155       return new BalancerDecision(initialFunctionCosts, finalFunctionCosts,
156         initTotalCost, computedTotalCost, regionPlans, computedSteps);
157     }
158   }
159 
160 }