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 long getPlanSizeMb() { 063 long total = 0; 064 for (NormalizationTarget target : normalizationTargets) { 065 total += target.getRegionSizeMb(); 066 } 067 return total; 068 } 069 070 @Override 071 public String toString() { 072 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 073 .append("normalizationTargets", normalizationTargets).toString(); 074 } 075 076 @Override 077 public boolean equals(Object o) { 078 if (this == o) { 079 return true; 080 } 081 082 if (o == null || getClass() != o.getClass()) { 083 return false; 084 } 085 086 MergeNormalizationPlan that = (MergeNormalizationPlan) o; 087 088 return new EqualsBuilder().append(normalizationTargets, that.normalizationTargets).isEquals(); 089 } 090 091 @Override 092 public int hashCode() { 093 return new HashCodeBuilder(17, 37).append(normalizationTargets).toHashCode(); 094 } 095 096 /** 097 * A helper for constructing instances of {@link MergeNormalizationPlan}. 098 */ 099 static class Builder { 100 101 private final List<NormalizationTarget> normalizationTargets = new LinkedList<>(); 102 103 public Builder setTargets(final List<NormalizationTarget> targets) { 104 normalizationTargets.clear(); 105 normalizationTargets.addAll(targets); 106 return this; 107 } 108 109 public Builder addTarget(final RegionInfo regionInfo, final long regionSizeMb) { 110 normalizationTargets.add(new NormalizationTarget(regionInfo, regionSizeMb)); 111 return this; 112 } 113 114 public MergeNormalizationPlan build() { 115 return new MergeNormalizationPlan(normalizationTargets); 116 } 117 } 118}