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  // the lock here is for fencing SCP and TRSP, so not all operations need to hold this lock
039  private final ReadWriteLock lock = new ReentrantReadWriteLock();
040  private volatile ServerState state = ServerState.ONLINE;
041
042  public ServerStateNode(ServerName serverName) {
043    this.serverName = serverName;
044    this.regions = ConcurrentHashMap.newKeySet();
045  }
046
047  public ServerName getServerName() {
048    return serverName;
049  }
050
051  public ServerState getState() {
052    return state;
053  }
054
055  public boolean isInState(final ServerState... expected) {
056    boolean expectedState = false;
057    if (expected != null) {
058      for (int i = 0; i < expected.length; ++i) {
059        expectedState |= (state == expected[i]);
060      }
061    }
062    return expectedState;
063  }
064
065  void setState(final ServerState state) {
066    this.state = state;
067  }
068
069  public int getRegionCount() {
070    return regions.size();
071  }
072
073  public List<RegionInfo> getRegionInfoList() {
074    return regions.stream().map(RegionStateNode::getRegionInfo).collect(Collectors.toList());
075  }
076
077  public List<RegionInfo> getSystemRegionInfoList() {
078    return regions.stream().filter(RegionStateNode::isSystemTable)
079      .map(RegionStateNode::getRegionInfo).collect(Collectors.toList());
080  }
081
082  public void addRegion(final RegionStateNode regionNode) {
083    this.regions.add(regionNode);
084  }
085
086  public void removeRegion(final RegionStateNode regionNode) {
087    this.regions.remove(regionNode);
088  }
089
090  public Lock readLock() {
091    return lock.readLock();
092  }
093
094  public Lock writeLock() {
095    return lock.writeLock();
096  }
097
098  @Override
099  public int compareTo(final ServerStateNode other) {
100    return getServerName().compareTo(other.getServerName());
101  }
102
103  @Override
104  public int hashCode() {
105    return getServerName().hashCode();
106  }
107
108  @Override
109  public boolean equals(final Object other) {
110    if (this == other) {
111      return true;
112    }
113    if (!(other instanceof ServerStateNode)) {
114      return false;
115    }
116    return compareTo((ServerStateNode) other) == 0;
117  }
118
119  @Override
120  public String toString() {
121    return getServerName() + "/" + getState() + "/regionCount=" + this.regions.size() + "/lock="
122      + this.lock;
123  }
124}