001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.client;
021
022import java.util.List;
023
024import org.apache.commons.lang3.builder.ToStringBuilder;
025import org.apache.hadoop.hbase.util.GsonUtil;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.yetus.audience.InterfaceStability;
028
029import org.apache.hbase.thirdparty.com.google.gson.Gson;
030import org.apache.hbase.thirdparty.com.google.gson.JsonSerializer;
031
032/**
033 * History of balancer decisions taken for region movements.
034 */
035@InterfaceAudience.Public
036@InterfaceStability.Evolving
037final public class BalancerDecision extends LogEntry {
038
039  private final String initialFunctionCosts;
040  private final String finalFunctionCosts;
041  private final double initTotalCost;
042  private final double computedTotalCost;
043  private final long computedSteps;
044  private final List<String> regionPlans;
045
046  // used to convert object to pretty printed format
047  // used by toJsonPrettyPrint()
048  private static final Gson GSON = GsonUtil.createGson()
049    .setPrettyPrinting()
050    .registerTypeAdapter(BalancerDecision.class, (JsonSerializer<BalancerDecision>)
051      (balancerDecision, type, jsonSerializationContext) -> {
052        Gson gson = new Gson();
053        return gson.toJsonTree(balancerDecision);
054      }).create();
055
056  private BalancerDecision(String initialFunctionCosts, String finalFunctionCosts,
057      double initTotalCost, double computedTotalCost, List<String> regionPlans,
058      long computedSteps) {
059    this.initialFunctionCosts = initialFunctionCosts;
060    this.finalFunctionCosts = finalFunctionCosts;
061    this.initTotalCost = initTotalCost;
062    this.computedTotalCost = computedTotalCost;
063    this.regionPlans = regionPlans;
064    this.computedSteps = computedSteps;
065  }
066
067  public String getInitialFunctionCosts() {
068    return initialFunctionCosts;
069  }
070
071  public String getFinalFunctionCosts() {
072    return finalFunctionCosts;
073  }
074
075  public double getInitTotalCost() {
076    return initTotalCost;
077  }
078
079  public double getComputedTotalCost() {
080    return computedTotalCost;
081  }
082
083  public List<String> getRegionPlans() {
084    return regionPlans;
085  }
086
087  public long getComputedSteps() {
088    return computedSteps;
089  }
090
091  @Override
092  public String toString() {
093    return new ToStringBuilder(this)
094      .append("initialFunctionCosts", initialFunctionCosts)
095      .append("finalFunctionCosts", finalFunctionCosts)
096      .append("initTotalCost", initTotalCost)
097      .append("computedTotalCost", computedTotalCost)
098      .append("computedSteps", computedSteps)
099      .append("regionPlans", regionPlans)
100      .toString();
101  }
102
103  @Override
104  public String toJsonPrettyPrint() {
105    return GSON.toJson(this);
106  }
107
108  public static class Builder {
109    private String initialFunctionCosts;
110    private String finalFunctionCosts;
111    private double initTotalCost;
112    private double computedTotalCost;
113    private long computedSteps;
114    private List<String> regionPlans;
115
116    public Builder setInitialFunctionCosts(String initialFunctionCosts) {
117      this.initialFunctionCosts = initialFunctionCosts;
118      return this;
119    }
120
121    public Builder setFinalFunctionCosts(String finalFunctionCosts) {
122      this.finalFunctionCosts = finalFunctionCosts;
123      return this;
124    }
125
126    public Builder setInitTotalCost(double initTotalCost) {
127      this.initTotalCost = initTotalCost;
128      return this;
129    }
130
131    public Builder setComputedTotalCost(double computedTotalCost) {
132      this.computedTotalCost = computedTotalCost;
133      return this;
134    }
135
136    public Builder setRegionPlans(List<String> regionPlans) {
137      this.regionPlans = regionPlans;
138      return this;
139    }
140
141    public Builder setComputedSteps(long computedSteps) {
142      this.computedSteps = computedSteps;
143      return this;
144    }
145
146    public BalancerDecision build() {
147      return new BalancerDecision(initialFunctionCosts, finalFunctionCosts,
148        initTotalCost, computedTotalCost, regionPlans, computedSteps);
149    }
150  }
151
152}