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.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026import org.apache.commons.lang3.builder.ToStringBuilder;
027import org.apache.hadoop.hbase.util.GsonUtil;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.apache.yetus.audience.InterfaceStability;
030
031import org.apache.hbase.thirdparty.com.google.gson.Gson;
032import org.apache.hbase.thirdparty.com.google.gson.JsonSerializer;
033
034/**
035 * History of detail information that balancer movements was rejected
036 */
037@InterfaceAudience.Public
038@InterfaceStability.Evolving
039final public class BalancerRejection extends LogEntry {
040  //The reason why balancer was rejected
041  private final String reason;
042  private final List<String> costFuncInfoList;
043
044  // used to convert object to pretty printed format
045  // used by toJsonPrettyPrint()
046  private static final Gson GSON = GsonUtil.createGson()
047    .setPrettyPrinting()
048    .disableHtmlEscaping()
049    .registerTypeAdapter(BalancerRejection.class, (JsonSerializer<BalancerRejection>)
050      (balancerRejection, type, jsonSerializationContext) -> {
051        Gson gson = new Gson();
052        return gson.toJsonTree(balancerRejection);
053      }).create();
054
055  private BalancerRejection(String reason, List<String> costFuncInfoList) {
056    this.reason = reason;
057    if(costFuncInfoList == null){
058      this.costFuncInfoList = Collections.emptyList();
059    }
060    else {
061      this.costFuncInfoList = costFuncInfoList;
062    }
063  }
064
065  public String getReason() {
066    return reason;
067  }
068
069  public List<String> getCostFuncInfoList() {
070    return costFuncInfoList;
071  }
072
073  @Override
074  public String toString() {
075    return new ToStringBuilder(this)
076      .append("reason", reason)
077      .append("costFuncInfoList", costFuncInfoList.toString())
078      .toString();
079  }
080
081  @Override
082  public String toJsonPrettyPrint() {
083    return GSON.toJson(this);
084  }
085
086  public static class Builder {
087    private String reason;
088    private List<String> costFuncInfoList;
089
090    public Builder setReason(String reason) {
091      this.reason = reason;
092      return this;
093    }
094
095    public void addCostFuncInfo(String funcName, double cost, float multiplier){
096      if(costFuncInfoList == null){
097        costFuncInfoList = new ArrayList<>();
098      }
099      costFuncInfoList.add(
100        new StringBuilder()
101          .append(funcName)
102          .append(" cost:").append(cost)
103          .append(" multiplier:").append(multiplier)
104          .toString());
105    }
106
107    public Builder setCostFuncInfoList(List<String> costFuncInfoList){
108      this.costFuncInfoList = costFuncInfoList;
109      return this;
110    }
111
112    public BalancerRejection build() {
113      return new BalancerRejection(reason, costFuncInfoList);
114    }
115  }
116}