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.chaos.actions;
019
020import java.io.IOException;
021import java.util.Collections;
022import java.util.HashSet;
023import java.util.Set;
024import java.util.stream.Collectors;
025import org.apache.hadoop.hbase.ClusterMetrics;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.net.Address;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * Action to dump the cluster status.
033 */
034public class DumpClusterStatusAction extends Action {
035  private static final Logger LOG = LoggerFactory.getLogger(DumpClusterStatusAction.class);
036
037  private Set<Address> initialRegionServers;
038
039  @Override
040  protected Logger getLogger() {
041    return LOG;
042  }
043
044  @Override
045  public void init(ActionContext context) throws IOException {
046    super.init(context);
047    initialRegionServers = collectKnownRegionServers(initialStatus);
048  }
049
050  @Override
051  public void perform() throws Exception {
052    getLogger().debug("Performing action: Dump cluster status");
053    final ClusterMetrics currentMetrics = cluster.getClusterMetrics();
054    getLogger().info("Cluster status\n{}", currentMetrics);
055    reportMissingRegionServers(currentMetrics);
056    reportNewRegionServers(currentMetrics);
057  }
058
059  /**
060   * Build a set of all the host:port pairs of region servers known to this cluster.
061   */
062  private static Set<Address> collectKnownRegionServers(final ClusterMetrics clusterMetrics) {
063    final Set<Address> regionServers = clusterMetrics.getLiveServerMetrics().keySet().stream()
064      .map(ServerName::getAddress).collect(Collectors.toSet());
065    clusterMetrics.getDeadServerNames().stream().map(ServerName::getAddress)
066      .forEach(regionServers::add);
067    return Collections.unmodifiableSet(regionServers);
068  }
069
070  private void reportMissingRegionServers(final ClusterMetrics clusterMetrics) {
071    final Set<Address> regionServers = collectKnownRegionServers(clusterMetrics);
072    final Set<Address> missingRegionServers = new HashSet<>(initialRegionServers);
073    missingRegionServers.removeAll(regionServers);
074    if (!missingRegionServers.isEmpty()) {
075      final StringBuilder stringBuilder =
076        new StringBuilder().append("region server(s) are missing from this cluster report");
077      missingRegionServers.stream().sorted()
078        .forEach(address -> stringBuilder.append("\n  ").append(address));
079      getLogger().warn(stringBuilder.toString());
080    }
081  }
082
083  private void reportNewRegionServers(final ClusterMetrics clusterMetrics) {
084    final Set<Address> regionServers = collectKnownRegionServers(clusterMetrics);
085    final Set<Address> newRegionServers = new HashSet<>(regionServers);
086    newRegionServers.removeAll(initialRegionServers);
087    if (!newRegionServers.isEmpty()) {
088      final StringBuilder stringBuilder =
089        new StringBuilder().append("region server(s) are new for this cluster report");
090      newRegionServers.stream().sorted()
091        .forEach(address -> stringBuilder.append("\n  ").append(address));
092      getLogger().warn(stringBuilder.toString());
093    }
094  }
095}