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