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.monitoring;
019
020import java.io.IOException;
021import java.io.PrintWriter;
022import java.util.Map;
023import javax.servlet.http.HttpServlet;
024import javax.servlet.http.HttpServletRequest;
025import org.apache.hadoop.conf.ConfigRedactor;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.executor.ExecutorService;
028import org.apache.hadoop.hbase.executor.ExecutorService.ExecutorStatus;
029import org.apache.hadoop.hbase.util.VersionInfo;
030import org.apache.yetus.audience.InterfaceAudience;
031
032@InterfaceAudience.Private
033public abstract class StateDumpServlet extends HttpServlet {
034  static final long DEFAULT_TAIL_KB = 100;
035  private static final long serialVersionUID = 1L;
036  protected static final String REDACTED = "<redacted>";
037  protected static final String REDACTED_TEXT = "******";
038
039  protected void dumpVersionInfo(PrintWriter out) {
040    VersionInfo.writeTo(out);
041
042    out.println("Hadoop " + org.apache.hadoop.util.VersionInfo.getVersion());
043    out.println("Source code repository " + org.apache.hadoop.util.VersionInfo.getUrl()
044      + " revision=" + org.apache.hadoop.util.VersionInfo.getRevision());
045    out.println("Compiled by " + org.apache.hadoop.util.VersionInfo.getUser() + " on "
046      + org.apache.hadoop.util.VersionInfo.getDate());
047  }
048
049  protected boolean isShowQueueDump(Configuration conf) {
050    return conf.getBoolean("hbase.regionserver.servlet.show.queuedump", true);
051  }
052
053  protected long getTailKbParam(HttpServletRequest request) {
054    String param = request.getParameter("tailkb");
055    if (param == null) {
056      return DEFAULT_TAIL_KB;
057    }
058    return Long.parseLong(param);
059  }
060
061  protected void dumpExecutors(ExecutorService service, PrintWriter out) throws IOException {
062    if (service == null) {
063      out.println("ExecutorService is not initialized");
064      return;
065    }
066
067    Map<String, ExecutorStatus> statuses = service.getAllExecutorStatuses();
068    for (ExecutorStatus status : statuses.values()) {
069      status.dumpTo(out, "  ");
070    }
071  }
072
073  protected Configuration getRedactedConfiguration(Configuration conf) {
074    // YARN-11308 introduced a new method signature to the overloaded Configuration.writeXml()
075    // method. Within this new method, the ConfigRedactor is used on the Configuration object if
076    // that object is not null. This allows the XML output to have sensitive content redacted
077    // automatically. However, this new method is only available in Hadoop 3.4 and later, so we are
078    // performing the redaction here manually in order to ensure backward compatibility.
079    ConfigRedactor redactor = new ConfigRedactor(conf);
080    String redactResult;
081    for (Map.Entry<String, String> entry : conf) {
082      redactResult = redactor.redact(entry.getKey(), entry.getValue());
083      if (REDACTED.equals(redactResult)) {
084        conf.set(entry.getKey(), REDACTED_TEXT);
085      }
086    }
087    return conf;
088  }
089}