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.master.normalizer; 019 020import java.util.Collections; 021import java.util.LinkedList; 022import java.util.List; 023import org.apache.commons.lang3.builder.EqualsBuilder; 024import org.apache.commons.lang3.builder.HashCodeBuilder; 025import org.apache.commons.lang3.builder.ToStringBuilder; 026import org.apache.commons.lang3.builder.ToStringStyle; 027import org.apache.hadoop.hbase.client.RegionInfo; 028import org.apache.hadoop.hbase.master.MasterServices; 029import org.apache.yetus.audience.InterfaceAudience; 030 031import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; 032 033/** 034 * Normalization plan to merge adjacent regions. As with any call to 035 * {@link MasterServices#mergeRegions(RegionInfo[], boolean, long, long)} with 036 * {@code forcible=false}, Region order and adjacency are important. It's the caller's 037 * responsibility to ensure the provided parameters are ordered according to the {code mergeRegions} 038 * method requirements. 039 */ 040@InterfaceAudience.Private 041final class MergeNormalizationPlan implements NormalizationPlan { 042 043 private final List<NormalizationTarget> normalizationTargets; 044 045 private MergeNormalizationPlan(List<NormalizationTarget> normalizationTargets) { 046 Preconditions.checkNotNull(normalizationTargets); 047 Preconditions.checkState(normalizationTargets.size() >= 2, 048 "normalizationTargets.size() must be >= 2 but was %s", normalizationTargets.size()); 049 this.normalizationTargets = Collections.unmodifiableList(normalizationTargets); 050 } 051 052 @Override 053 public PlanType getType() { 054 return PlanType.MERGE; 055 } 056 057 public List<NormalizationTarget> getNormalizationTargets() { 058 return normalizationTargets; 059 } 060 061 @Override 062 public String toString() { 063 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 064 .append("normalizationTargets", normalizationTargets).toString(); 065 } 066 067 @Override 068 public boolean equals(Object o) { 069 if (this == o) { 070 return true; 071 } 072 073 if (o == null || getClass() != o.getClass()) { 074 return false; 075 } 076 077 MergeNormalizationPlan that = (MergeNormalizationPlan) o; 078 079 return new EqualsBuilder().append(normalizationTargets, that.normalizationTargets).isEquals(); 080 } 081 082 @Override 083 public int hashCode() { 084 return new HashCodeBuilder(17, 37).append(normalizationTargets).toHashCode(); 085 } 086 087 /** 088 * A helper for constructing instances of {@link MergeNormalizationPlan}. 089 */ 090 static class Builder { 091 092 private final List<NormalizationTarget> normalizationTargets = new LinkedList<>(); 093 094 public Builder setTargets(final List<NormalizationTarget> targets) { 095 normalizationTargets.clear(); 096 normalizationTargets.addAll(targets); 097 return this; 098 } 099 100 public Builder addTarget(final RegionInfo regionInfo, final long regionSizeMb) { 101 normalizationTargets.add(new NormalizationTarget(regionInfo, regionSizeMb)); 102 return this; 103 } 104 105 public MergeNormalizationPlan build() { 106 return new MergeNormalizationPlan(normalizationTargets); 107 } 108 } 109}