You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pig.apache.org by ch...@apache.org on 2013/04/30 19:16:46 UTC

svn commit: r1477728 - in /pig/trunk: ./ src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/ src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/plans/ src/org/apache/pig/impl/io/

Author: cheolsoo
Date: Tue Apr 30 17:16:46 2013
New Revision: 1477728

URL: http://svn.apache.org/r1477728
Log:
PIG-3169: Remove intermediate data after a job finishes (mwagner via cheolsoo)

Added:
    pig/trunk/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/plans/MRIntermediateDataVisitor.java
Modified:
    pig/trunk/CHANGES.txt
    pig/trunk/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/MapReduceLauncher.java
    pig/trunk/src/org/apache/pig/impl/io/FileLocalizer.java

Modified: pig/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/pig/trunk/CHANGES.txt?rev=1477728&r1=1477727&r2=1477728&view=diff
==============================================================================
--- pig/trunk/CHANGES.txt (original)
+++ pig/trunk/CHANGES.txt Tue Apr 30 17:16:46 2013
@@ -28,6 +28,8 @@ PIG-3174:  Remove rpm and deb artifacts 
 
 IMPROVEMENTS
 
+PIG-3169: Remove intermediate data after a job finishes (mwagner via cheolsoo)
+
 PIG-3173: Partition filter push down does not happen when partition keys condition include a AND and OR construct (rohini)
 
 PIG-2786: enhance Pig launcher script wrt. HBase/HCat integration (ndimiduk via daijy)

Modified: pig/trunk/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/MapReduceLauncher.java
URL: http://svn.apache.org/viewvc/pig/trunk/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/MapReduceLauncher.java?rev=1477728&r1=1477727&r2=1477728&view=diff
==============================================================================
--- pig/trunk/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/MapReduceLauncher.java (original)
+++ pig/trunk/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/MapReduceLauncher.java Tue Apr 30 17:16:46 2013
@@ -41,14 +41,15 @@ import org.apache.hadoop.mapred.jobcontr
 import org.apache.pig.ExecType;
 import org.apache.pig.PigConfiguration;
 import org.apache.pig.PigException;
-import org.apache.pig.PigWarning;
 import org.apache.pig.PigRunner.ReturnCode;
+import org.apache.pig.PigWarning;
 import org.apache.pig.backend.executionengine.ExecException;
 import org.apache.pig.backend.hadoop.datastorage.ConfigurationUtil;
 import org.apache.pig.backend.hadoop.executionengine.HExecutionEngine;
 import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MRCompiler.LastInputStreamingOptimizer;
 import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.plans.DotMRPrinter;
 import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.plans.EndOfAllInputSetter;
+import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.plans.MRIntermediateDataVisitor;
 import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.plans.MROperPlan;
 import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.plans.MRPrinter;
 import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.plans.POPackageAnnotator;
@@ -57,11 +58,12 @@ import org.apache.pig.backend.hadoop.exe
 import org.apache.pig.backend.hadoop.executionengine.physicalLayer.relationalOperators.POStore;
 import org.apache.pig.backend.hadoop.executionengine.shims.HadoopShims;
 import org.apache.pig.impl.PigContext;
+import org.apache.pig.impl.io.FileLocalizer;
 import org.apache.pig.impl.io.FileSpec;
 import org.apache.pig.impl.plan.CompilationMessageCollector;
+import org.apache.pig.impl.plan.CompilationMessageCollector.MessageType;
 import org.apache.pig.impl.plan.PlanException;
 import org.apache.pig.impl.plan.VisitorException;
-import org.apache.pig.impl.plan.CompilationMessageCollector.MessageType;
 import org.apache.pig.impl.util.ConfigurationValidator;
 import org.apache.pig.impl.util.LogUtils;
 import org.apache.pig.impl.util.UDFContext;
@@ -156,6 +158,11 @@ public class MapReduceLauncher extends L
         // start collecting statistics
         PigStatsUtil.startCollection(pc, jobClient, jcc, mrp); 
         
+        // Find all the intermediate data stores. The plan will be destroyed during compile/execution
+        // so this needs to be done before.
+        MRIntermediateDataVisitor intermediateVisitor = new MRIntermediateDataVisitor(mrp);
+        intermediateVisitor.visit();
+        
         List<Job> failedJobs = new LinkedList<Job>();
         List<NativeMapReduceOper> failedNativeMR = new LinkedList<NativeMapReduceOper>();
         List<Job> completeFailedJobsInThisRun = new LinkedList<Job>();
@@ -393,7 +400,12 @@ public class MapReduceLauncher extends L
         if(failedNativeMR.size() > 0){
             failed = true;
         }
-        
+
+        // Clean up all the intermediate data
+        for (String path : intermediateVisitor.getIntermediate()) {
+            FileLocalizer.delete(path, pc);
+        }
+
         // Look to see if any jobs failed.  If so, we need to report that.
         if (failedJobs != null && failedJobs.size() > 0) {
             

Added: pig/trunk/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/plans/MRIntermediateDataVisitor.java
URL: http://svn.apache.org/viewvc/pig/trunk/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/plans/MRIntermediateDataVisitor.java?rev=1477728&view=auto
==============================================================================
--- pig/trunk/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/plans/MRIntermediateDataVisitor.java (added)
+++ pig/trunk/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/plans/MRIntermediateDataVisitor.java Tue Apr 30 17:16:46 2013
@@ -0,0 +1,82 @@
+/*
+ * 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.pig.backend.hadoop.executionengine.mapReduceLayer.plans;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceOper;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.PhysicalOperator;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.plans.PhyPlanVisitor;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.plans.PhysicalPlan;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.relationalOperators.POStore;
+import org.apache.pig.impl.plan.DepthFirstWalker;
+import org.apache.pig.impl.plan.VisitorException;
+
+/**
+ * An {@link MROpPlanVisitor} that gathers the paths for all
+ * intermediate data from a {@link MROperPlan}
+ */
+public class MRIntermediateDataVisitor extends MROpPlanVisitor {
+    private final Set<String> intermediate;
+
+    public MRIntermediateDataVisitor(MROperPlan plan) {
+        super(plan, new DepthFirstWalker<MapReduceOper, MROperPlan>(plan));
+        this.intermediate = new HashSet<String>();
+    }
+
+    /**
+     * Get all paths for intermediate data. visit() must be called before this.
+     * 
+     * @return All intermediate data ElementDescriptors
+     */
+    public Set<String> getIntermediate() {
+        return intermediate;
+    }
+
+    class StoreFinder extends PhyPlanVisitor {
+
+        protected StoreFinder(PhysicalPlan plan) {
+            super(plan, new DepthFirstWalker<PhysicalOperator, PhysicalPlan>(
+                    plan));
+        }
+
+        @Override
+        public void visitStore(POStore store) {
+            if (store.isTmpStore()) {
+                intermediate.add(store.getSFile().getFileName());
+            }
+        }
+    }
+
+    @Override
+    public void visitMROp(MapReduceOper mr) throws VisitorException {
+        if (mr.mapPlan != null && mr.mapPlan.size() > 0) {
+            StoreFinder finder = new StoreFinder(mr.mapPlan);
+            finder.visit();
+        }
+        if (mr.combinePlan != null && mr.combinePlan.size() > 0) {
+            StoreFinder finder = new StoreFinder(mr.combinePlan);
+            finder.visit();
+        }
+        if (mr.reducePlan != null && mr.reducePlan.size() > 0) {
+            StoreFinder finder = new StoreFinder(mr.reducePlan);
+            finder.visit();
+        }
+    }
+}

Modified: pig/trunk/src/org/apache/pig/impl/io/FileLocalizer.java
URL: http://svn.apache.org/viewvc/pig/trunk/src/org/apache/pig/impl/io/FileLocalizer.java?rev=1477728&r1=1477727&r2=1477728&view=diff
==============================================================================
--- pig/trunk/src/org/apache/pig/impl/io/FileLocalizer.java (original)
+++ pig/trunk/src/org/apache/pig/impl/io/FileLocalizer.java Tue Apr 30 17:16:46 2013
@@ -28,8 +28,6 @@ import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.net.URI;
 import java.util.ArrayList;
-import java.util.Deque;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -410,33 +408,19 @@ public class FileLocalizer {
     
     static public boolean delete(String fileSpec, PigContext pigContext) throws IOException{
         fileSpec = checkDefaultPrefix(pigContext.getExecType(), fileSpec);
+        ElementDescriptor elem = null;
         if (!fileSpec.startsWith(LOCAL_PREFIX)) {
-            ElementDescriptor elem = pigContext.getDfs().asElement(fileSpec);
-            elem.delete();
-            return true;
-        }
-        else {
-            ElementDescriptor elem = pigContext.getLfs().asElement(fileSpec);
-            elem.delete();
-            return true;
+            elem = pigContext.getDfs().asElement(fileSpec);
+        } else {
+            elem = pigContext.getLfs().asElement(fileSpec);
         }
+        elem.delete();
+        return true;
     }
 
     static Random      r           = new Random();
 
     /**
-     * Thread local toDelete Stack to hold descriptors to be deleted upon calling
-     * deleteTempFiles. Use the toDelete() method to access this stack.
-     */
-    private static ThreadLocal<Deque<ElementDescriptor>> toDelete =
-        new ThreadLocal<Deque<ElementDescriptor>>() {
-
-        protected Deque<ElementDescriptor> initialValue() {
-            return new LinkedList<ElementDescriptor>();
-        }
-    };
-
-    /**
      * Thread local relativeRoot ContainerDescriptor. Do not access this object
      * directly, since it's lazy initialized in the relativeRoot(PigContext)
      * method, which should be used instead.
@@ -446,14 +430,6 @@ public class FileLocalizer {
     };
 
     /**
-     * Convenience accessor method to the toDelete Stack bound to this thread.
-     * @return A Stack of ElementDescriptors that should be deleted.
-     */
-    private static Deque<ElementDescriptor> toDelete() {
-        return toDelete.get();
-    }
-
-    /**
      * This method is only used by test code to reset state.
      * @param initialized
      */
@@ -478,42 +454,20 @@ public class FileLocalizer {
         if (relativeRoot.get() == null) {
             String tdir= pigContext.getProperties().getProperty("pig.temp.dir", "/tmp");
             relativeRoot.set(pigContext.getDfs().asContainer(tdir + "/temp" + r.nextInt()));
-            toDelete().push(relativeRoot.get());
         }
 
         return relativeRoot.get();
     }
 
     public static void deleteTempFiles() {
-        while (!toDelete().isEmpty()) {
+        if (relativeRoot.get() != null) {
             try {
-                ElementDescriptor elem = toDelete().pop();
-                elem.delete();
-            } 
-            catch (IOException e) {
+                relativeRoot.get().delete();
+            } catch (IOException e) {
                 log.error(e);
             }
+            setInitialized(false);
         }
-        setInitialized(false);
-    }
-
-    /**
-     * @deprecated Use {@link #getTemporaryPath(PigContext)} instead
-     */
-    @Deprecated
-    public static synchronized ElementDescriptor 
-        getTemporaryPath(ElementDescriptor relative, 
-                         PigContext pigContext) throws IOException {
-        if (relative == null) {
-            relative = relativeRoot(pigContext);
-        }
-        if (!relativeRoot(pigContext).exists()) {
-            relativeRoot(pigContext).create();
-        }
-        ElementDescriptor elem= 
-            pigContext.getDfs().asElement(relative.toString(), "tmp" + r.nextInt());
-        toDelete().push(elem);
-        return elem;
     }
 
     public static Path getTemporaryPath(PigContext pigContext) throws IOException {
@@ -528,7 +482,6 @@ public class FileLocalizer {
       }
       ElementDescriptor elem=
           pigContext.getDfs().asElement(relative.toString(), "tmp" + r.nextInt() + suffix);
-      toDelete().push(elem);
       return ((HPath)elem).getPath();
   }
 
@@ -544,8 +497,8 @@ public class FileLocalizer {
             throw new FileNotFoundException(filename);
         }
             
-        ElementDescriptor distribElem = 
-            getTemporaryPath(null, pigContext);
+        ElementDescriptor distribElem = pigContext.getDfs().asElement(
+                getTemporaryPath(pigContext).toString());
     
         int suffixStart = filename.lastIndexOf('.');
         if (suffixStart != -1) {