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