@@ -0,0 +1,2915 @@
+/* (c) 2014 LinkedIn Corp. All rights reserved.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22); you may not use%0A * this file except in compliance with the License. You may obtain a copy of the%0A * License at  http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software distributed%0A * under the License is distributed on an %22AS IS%22 BASIS, WITHOUT WARRANTIES OR%0A * CONDITIONS OF ANY KIND, either express or implied.%0A */%0A%0Apackage gobblin.util;%0A%0Aimport java.io.BufferedWriter;%0Aimport java.io.IOException;%0Aimport java.io.OutputStreamWriter;%0A%0Aimport org.apache.hadoop.fs.FileSystem;%0Aimport org.apache.hadoop.fs.Path;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport com.google.common.io.Closer;%0A%0A%0A/**%0A * A utility class for generating script to move the heap dump .prof files to HDFS for hadoop tasks, when Java heap out of memory error is thrown.%0A *%0A */%0Apublic class HeapDumpForTaskUtils %7B%0A%0A  private static final Logger LOG = LoggerFactory.getLogger(HeapDumpForTaskUtils.class);%0A  private static final String DUMP_FOLDER = %22/dumps/%22;%0A%0A  /**%0A   * Generate the dumpScript, which is used when OOM error is thrown during task execution.%0A   * The current content dumpScript puts the .prof files to the DUMP_FOLDER within the same directory of the dumpScript.%0A   *%0A   * User needs to add the following options to the task java.opts:%0A   *%0A   * -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./heapFileName.hprof -XX:OnOutOfMemoryError=./dumpScriptFileName%0A   *%0A   * @param dumpScript The path to the dumpScript, which needs to be added to the Distributed cache.%0A   * To use it, simply put the path of dumpScript to the gobblin config: job.hdfs.files.%0A   * @param fs File system%0A   * @param heapFileName the name of the .prof file.%0A   * @throws IOException%0A   */%0A  public static void generateDumpScript(Path dumpScript, FileSystem fs, String heapFileName) throws IOException %7B%0A    BufferedWriter scriptWriter = null;%0A    Closer closer = Closer.create();%0A    try %7B%0A      if (fs.exists(dumpScript)) %7B%0A        LOG.info(%22Heap dump script already exists: %22 + dumpScript);%0A      %7D else %7B%0A        Path dumpDir = new Path(dumpScript.getParent() + DUMP_FOLDER);%0A        if (fs.exists(new Path(dumpScript.getParent() + DUMP_FOLDER))) %7B%0A          fs.create(dumpDir);%0A        %7D%0A        scriptWriter = closer.register(new BufferedWriter(new OutputStreamWriter(fs.create(dumpScript))));%0A        scriptWriter.write(%22#!/bin/sh%5Cn%22 + %22hadoop dfs -put %22 + heapFileName + %22 %22 + dumpDir + %22$%7BPWD//%5C%5C//_%7D.hprof%22);%0A        scriptWriter.flush();%0A        Runtime.getRuntime().exec(%22hadoop fs -chmod 755 %22 + dumpScript);%0A      %7D%0A    %7D catch (IOException e) %7B%0A      LOG.error(%22Heap dump script is not generated successfully.%22);%0A      if (fs.exists(dumpScript)) %7B%0A        fs.delete(dumpScript, true);%0A      %7D%0A    %7D finally %7B%0A      closer.close();%0A    %7D%0A  %7D%0A%7D%0A

Add HeapDumpForTaskUtils