View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.chaos.actions;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Set;
27  import org.apache.hadoop.hbase.ClusterStatus;
28  import org.apache.hadoop.hbase.ServerName;
29  import org.apache.hadoop.hbase.net.Address;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * Action to dump the cluster status.
35   */
36  public class DumpClusterStatusAction extends Action {
37    private static final Logger LOG = LoggerFactory.getLogger(DumpClusterStatusAction.class);
38  
39    private Set<Address> initialRegionServers;
40  
41    @Override
42    protected Logger getLogger() {
43      return LOG;
44    }
45  
46    @Override
47    public void init(ActionContext context) throws IOException {
48      super.init(context);
49      initialRegionServers = collectKnownRegionServers(initialStatus);
50    }
51  
52    @Override
53    public void perform() throws Exception {
54      getLogger().debug("Performing action: Dump cluster status");
55      final ClusterStatus currentMetrics = cluster.getClusterStatus();
56      getLogger().info("Cluster status\n{}", currentMetrics);
57      reportMissingRegionServers(currentMetrics);
58      reportNewRegionServers(currentMetrics);
59    }
60  
61    /**
62     * Build a set of all the host:port pairs of region servers known to this cluster.
63     */
64    private static Set<Address> collectKnownRegionServers(final ClusterStatus clusterStatus) {
65      final Set<Address> regionServers = new HashSet<>();
66      final Set<ServerName> serverNames = new HashSet<>(clusterStatus.getLiveServersLoad().keySet());
67      serverNames.addAll(clusterStatus.getDeadServerNames());
68  
69      for (final ServerName serverName : serverNames) {
70        regionServers.add(serverName.getAddress());
71      }
72      return Collections.unmodifiableSet(regionServers);
73    }
74  
75    private void reportMissingRegionServers(final ClusterStatus clusterStatus) {
76      final Set<Address> regionServers = collectKnownRegionServers(clusterStatus);
77      final Set<Address> missingRegionServers = new HashSet<>(initialRegionServers);
78      missingRegionServers.removeAll(regionServers);
79      if (!missingRegionServers.isEmpty()) {
80        final StringBuilder stringBuilder = new StringBuilder()
81          .append("region server(s) are missing from this cluster report");
82        final List<Address> sortedAddresses = new ArrayList<>(missingRegionServers);
83        Collections.sort(sortedAddresses);
84        for (final Address address : sortedAddresses) {
85          stringBuilder.append("\n  ").append(address);
86        }
87        getLogger().warn(stringBuilder.toString());
88      }
89    }
90  
91    private void reportNewRegionServers(final ClusterStatus clusterStatus) {
92      final Set<Address> regionServers = collectKnownRegionServers(clusterStatus);
93      final Set<Address> newRegionServers = new HashSet<>(regionServers);
94      newRegionServers.removeAll(initialRegionServers);
95      if (!newRegionServers.isEmpty()) {
96        final StringBuilder stringBuilder = new StringBuilder()
97          .append("region server(s) are new for this cluster report");
98        final List<Address> sortedAddresses = new ArrayList<>(newRegionServers);
99        Collections.sort(sortedAddresses);
100       for (final Address address : sortedAddresses) {
101         stringBuilder.append("\n  ").append(address);
102       }
103       getLogger().warn(stringBuilder.toString());
104     }
105   }
106 }