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 */ 019package org.apache.hadoop.hbase.master.normalizer; 020 021import java.util.Collections; 022import java.util.LinkedList; 023import java.util.List; 024import org.apache.commons.lang3.builder.EqualsBuilder; 025import org.apache.commons.lang3.builder.HashCodeBuilder; 026import org.apache.commons.lang3.builder.ToStringBuilder; 027import org.apache.commons.lang3.builder.ToStringStyle; 028import org.apache.hadoop.hbase.client.RegionInfo; 029import org.apache.hadoop.hbase.master.MasterServices; 030import org.apache.yetus.audience.InterfaceAudience; 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)} 036 * with {@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 038 * {code mergeRegions} 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) 065 .toString(); 066 } 067 068 @Override 069 public boolean equals(Object o) { 070 if (this == o) { 071 return true; 072 } 073 074 if (o == null || getClass() != o.getClass()) { 075 return false; 076 } 077 078 MergeNormalizationPlan that = (MergeNormalizationPlan) o; 079 080 return new EqualsBuilder() 081 .append(normalizationTargets, that.normalizationTargets) 082 .isEquals(); 083 } 084 085 @Override 086 public int hashCode() { 087 return new HashCodeBuilder(17, 37) 088 .append(normalizationTargets) 089 .toHashCode(); 090 } 091 092 /** 093 * A helper for constructing instances of {@link MergeNormalizationPlan}. 094 */ 095 static class Builder { 096 097 private final List<NormalizationTarget> normalizationTargets = new LinkedList<>(); 098 099 public Builder setTargets(final List<NormalizationTarget> targets) { 100 normalizationTargets.clear(); 101 normalizationTargets.addAll(targets); 102 return this; 103 } 104 105 public Builder addTarget(final RegionInfo regionInfo, final long regionSizeMb) { 106 normalizationTargets.add(new NormalizationTarget(regionInfo, regionSizeMb)); 107 return this; 108 } 109 110 public MergeNormalizationPlan build() { 111 return new MergeNormalizationPlan(normalizationTargets); 112 } 113 } 114}