Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TEZ-4038: Add a /prof profiler endpoint like HiveServer2 has #212

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -322,6 +323,25 @@ public static <T extends Enum<T>> Set<T> getEnums(Configuration conf, String con
return enums;
}

public static Integer getPid() {
String pidStr = null;
String name = ManagementFactory.getRuntimeMXBean().getName();
if (name != null) {
int idx = name.indexOf("@");
if (idx != -1) {
pidStr = name.substring(0, name.indexOf("@"));
}
}
try {
if (pidStr != null) {
return Integer.valueOf(pidStr);
}
} catch (NumberFormatException nfe) {
LOG.info("Couldn't parse \"{}\" into integer pid", pidStr);
}
return null;
}

@Private
public static void setHadoopCallerContext(HadoopShim hadoopShim, TezTaskAttemptID attemptID) {
hadoopShim.setHadoopCallerContext("tez_ta:" + attemptID.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tez.common.web;

import org.apache.hadoop.yarn.webapp.MimeType;
import org.eclipse.jetty.servlet.DefaultServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* Servlet to serve files generated by {@link ProfileServlet}.
*/
public class ProfileOutputServlet extends DefaultServlet {
public static final String FILE_QUERY_PARAM = "file";

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String queriedFile = request.getParameter(FILE_QUERY_PARAM);
if (queriedFile == null) {
writeMessage(response, "Run the profiler to be able to receive its output");
return;
}
File outputFile = new File(ProfileServlet.OUTPUT_DIR, queriedFile);
if (!outputFile.exists()) {
writeMessage(response, "Requested file does not exist: " + queriedFile);
return;
}
if (outputFile.length() < 100) {
response.setIntHeader("Refresh", 2);
writeMessage(response, "This page auto-refreshes every 2 seconds until output file is ready...");
return;
}
response.setContentType(MimeType.HTML);
response.getOutputStream().write(Files.readAllBytes(Paths.get(outputFile.getPath())));
response.getOutputStream().flush();
response.getOutputStream().close();
}

private void writeMessage(HttpServletResponse response, String message) throws IOException {
response.setContentType(MimeType.TEXT);
PrintWriter out = response.getWriter();
out.println(message);
out.close();
}
}
Loading