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.assignment;
019
020import java.util.List;
021import java.util.Set;
022import java.util.concurrent.ConcurrentHashMap;
023import java.util.concurrent.locks.Lock;
024import java.util.concurrent.locks.ReadWriteLock;
025import java.util.concurrent.locks.ReentrantReadWriteLock;
026import java.util.stream.Collectors;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.yetus.audience.InterfaceAudience;
030
031/**
032 * State of Server; list of hosted regions, etc.
033 */
034@InterfaceAudience.Private
035public class ServerStateNode implements Comparable<ServerStateNode> {
036  private final Set<RegionStateNode> regions;
037  private final ServerName serverName;
038  private final ReadWriteLock lock = new ReentrantReadWriteLock();
039  private volatile ServerState state = ServerState.ONLINE;
040
041  public ServerStateNode(ServerName serverName) {
042    this.serverName = serverName;
043    this.regions = ConcurrentHashMap.newKeySet();
044  }
045
046  public ServerName getServerName() {
047    return serverName;
048  }
049
050  public ServerState getState() {
051    return state;
052  }
053
054  public boolean isInState(final ServerState... expected) {
055    boolean expectedState = false;
056    if (expected != null) {
057      for (int i = 0; i < expected.length; ++i) {
058        expectedState |= (state == expected[i]);
059      }
060    }
061    return expectedState;
062  }
063
064  void setState(final ServerState state) {
065    this.state = state;
066  }
067
068  public int getRegionCount() {
069    return regions.size();
070  }
071
072  public List<RegionInfo> getRegionInfoList() {
073    return regions.stream().map(RegionStateNode::getRegionInfo).collect(Collectors.toList());
074  }
075
076  public List<RegionInfo> getSystemRegionInfoList() {
077    return regions.stream().filter(RegionStateNode::isSystemTable)
078      .map(RegionStateNode::getRegionInfo).collect(Collectors.toList());
079  }
080
081  public void addRegion(final RegionStateNode regionNode) {
082    this.regions.add(regionNode);
083  }
084
085  public void removeRegion(final RegionStateNode regionNode) {
086    this.regions.remove(regionNode);
087  }
088
089  public Lock readLock() {
090    return lock.readLock();
091  }
092
093  public Lock writeLock() {
094    return lock.writeLock();
095  }
096
097  @Override
098  public int compareTo(final ServerStateNode other) {
099    return getServerName().compareTo(other.getServerName());
100  }
101
102  @Override
103  public int hashCode() {
104    return getServerName().hashCode();
105  }
106
107  @Override
108  public boolean equals(final Object other) {
109    if (this == other) {
110      return true;
111    }
112    if (!(other instanceof ServerStateNode)) {
113      return false;
114    }
115    return compareTo((ServerStateNode) other) == 0;
116  }
117
118  @Override
119  public String toString() {
120    return getServerName() + "/" + getState() + "/regionCount=" + this.regions.size() +
121        "/lock=" + this.lock;
122  }
123}