Skip to content

Commit 336dfad

Browse files
committed
TEZ-4038: Add a /prof profiler endpoint like HiveServer2 has
1 parent 798ddda commit 336dfad

6 files changed

Lines changed: 504 additions & 3 deletions

File tree

tez-common/src/main/java/org/apache/tez/common/TezUtilsInternal.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.FileInputStream;
2222
import java.io.FileNotFoundException;
2323
import java.io.IOException;
24+
import java.lang.management.ManagementFactory;
2425
import java.lang.reflect.InvocationTargetException;
2526
import java.lang.reflect.Method;
2627
import java.nio.charset.Charset;
@@ -322,6 +323,25 @@ public static <T extends Enum<T>> Set<T> getEnums(Configuration conf, String con
322323
return enums;
323324
}
324325

326+
public static Integer getPid() {
327+
String pidStr = null;
328+
String name = ManagementFactory.getRuntimeMXBean().getName();
329+
if (name != null) {
330+
int idx = name.indexOf("@");
331+
if (idx != -1) {
332+
pidStr = name.substring(0, name.indexOf("@"));
333+
}
334+
}
335+
try {
336+
if (pidStr != null) {
337+
return Integer.valueOf(pidStr);
338+
}
339+
} catch (NumberFormatException nfe) {
340+
LOG.info("Couldn't parse \"{}\" into integer pid", pidStr);
341+
}
342+
return null;
343+
}
344+
325345
@Private
326346
public static void setHadoopCallerContext(HadoopShim hadoopShim, TezTaskAttemptID attemptID) {
327347
hadoopShim.setHadoopCallerContext("tez_ta:" + attemptID.toString());
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
package org.apache.tez.common.web;
19+
20+
import org.apache.hadoop.yarn.webapp.MimeType;
21+
import org.eclipse.jetty.servlet.DefaultServlet;
22+
23+
import javax.servlet.ServletException;
24+
import javax.servlet.http.HttpServletRequest;
25+
import javax.servlet.http.HttpServletResponse;
26+
import java.io.File;
27+
import java.io.IOException;
28+
import java.io.PrintWriter;
29+
import java.nio.file.Files;
30+
import java.nio.file.Paths;
31+
32+
/**
33+
* Servlet to serve files generated by {@link ProfileServlet}.
34+
*/
35+
public class ProfileOutputServlet extends DefaultServlet {
36+
public static final String FILE_QUERY_PARAM = "file";
37+
38+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
39+
String queriedFile = request.getParameter(FILE_QUERY_PARAM);
40+
if (queriedFile == null) {
41+
writeMessage(response, "Run the profiler to be able to receive its output");
42+
return;
43+
}
44+
File outputFile = new File(ProfileServlet.OUTPUT_DIR, queriedFile);
45+
if (!outputFile.exists()) {
46+
writeMessage(response, "Requested file does not exist: " + queriedFile);
47+
return;
48+
}
49+
if (outputFile.length() < 100) {
50+
response.setIntHeader("Refresh", 2);
51+
writeMessage(response, "This page auto-refreshes every 2 seconds until output file is ready...");
52+
return;
53+
}
54+
response.setContentType(MimeType.HTML);
55+
response.getOutputStream().write(Files.readAllBytes(Paths.get(outputFile.getPath())));
56+
response.getOutputStream().flush();
57+
response.getOutputStream().close();
58+
}
59+
60+
private void writeMessage(HttpServletResponse response, String message) throws IOException {
61+
response.setContentType(MimeType.TEXT);
62+
PrintWriter out = response.getWriter();
63+
out.println(message);
64+
out.close();
65+
}
66+
}

0 commit comments

Comments
 (0)