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.webapp;
019
020import java.util.Collections;
021import java.util.List;
022import java.util.stream.Collectors;
023import java.util.stream.StreamSupport;
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.HRegionLocation;
029import org.apache.hadoop.hbase.MetaTableAccessor;
030import org.apache.hadoop.hbase.RegionLocations;
031import org.apache.hadoop.hbase.ServerName;
032import org.apache.hadoop.hbase.client.RegionInfo;
033import org.apache.hadoop.hbase.client.Result;
034import org.apache.hadoop.hbase.master.RegionState;
035import org.apache.hadoop.hbase.master.assignment.RegionStateStore;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.apache.yetus.audience.InterfaceAudience;
038
039/**
040 * A POJO that consolidates the information about a single region replica that's stored in meta.
041 */
042@InterfaceAudience.Private
043public final class RegionReplicaInfo {
044  private final byte[] row;
045  private final RegionInfo regionInfo;
046  private final RegionState.State regionState;
047  private final ServerName serverName;
048
049  private RegionReplicaInfo(final Result result, final HRegionLocation location) {
050    this.row = result != null ? result.getRow() : null;
051    this.regionInfo = location != null ? location.getRegion() : null;
052    this.regionState = (result != null && regionInfo != null)
053      ? RegionStateStore.getRegionState(result, regionInfo)
054      : null;
055    this.serverName = location != null ? location.getServerName() : null;
056  }
057
058  public static List<RegionReplicaInfo> from(final Result result) {
059    if (result == null) {
060      return Collections.singletonList(null);
061    }
062
063    final RegionLocations locations = MetaTableAccessor.getRegionLocations(result);
064    if (locations == null) {
065      return Collections.singletonList(null);
066    }
067
068    return StreamSupport.stream(locations.spliterator(), false)
069      .map(location -> new RegionReplicaInfo(result, location))
070      .collect(Collectors.toList());
071  }
072
073  public byte[] getRow() {
074    return row;
075  }
076
077  public RegionInfo getRegionInfo() {
078    return regionInfo;
079  }
080
081  public byte[] getRegionName() {
082    return regionInfo != null ? regionInfo.getRegionName() : null;
083  }
084
085  public byte[] getStartKey() {
086    return regionInfo != null ? regionInfo.getStartKey() : null;
087  }
088
089  public byte[] getEndKey() {
090    return regionInfo != null ? regionInfo.getEndKey() : null;
091  }
092
093  public Integer getReplicaId() {
094    return regionInfo != null ? regionInfo.getReplicaId() : null;
095  }
096
097  public RegionState.State getRegionState() {
098    return regionState;
099  }
100
101  public ServerName getServerName() {
102    return serverName;
103  }
104
105  @Override
106  public boolean equals(Object other) {
107    if (this == other) {
108      return true;
109    }
110
111    if (other == null || getClass() != other.getClass()) {
112      return false;
113    }
114
115    RegionReplicaInfo that = (RegionReplicaInfo) other;
116
117    return new EqualsBuilder()
118      .append(row, that.row)
119      .append(regionInfo, that.regionInfo)
120      .append(regionState, that.regionState)
121      .append(serverName, that.serverName)
122      .isEquals();
123  }
124
125  @Override
126  public int hashCode() {
127    return new HashCodeBuilder(17, 37)
128      .append(row)
129      .append(regionInfo)
130      .append(regionState)
131      .append(serverName)
132      .toHashCode();
133  }
134
135  @Override public String toString() {
136    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
137      .append("row", Bytes.toStringBinary(row))
138      .append("regionInfo", regionInfo)
139      .append("regionState", regionState)
140      .append("serverName", serverName)
141      .toString();
142  }
143}