You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tez.apache.org by ss...@apache.org on 2013/10/15 01:36:25 UTC

git commit: TEZ-558. Optionally send broadcast output in the DataMovementEvent if the size of the data is small. (sseth)

Updated Branches:
  refs/heads/master cbe0910e0 -> d154b49fc


TEZ-558. Optionally send broadcast output in the DataMovementEvent if
the size of the data is small. (sseth)


Project: http://git-wip-us.apache.org/repos/asf/incubator-tez/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tez/commit/d154b49f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tez/tree/d154b49f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tez/diff/d154b49f

Branch: refs/heads/master
Commit: d154b49fc38908378ba26c5d9d26a7b18995a59c
Parents: cbe0910
Author: Siddharth Seth <ss...@apache.org>
Authored: Mon Oct 14 16:36:08 2013 -0700
Committer: Siddharth Seth <ss...@apache.org>
Committed: Mon Oct 14 16:36:08 2013 -0700

----------------------------------------------------------------------
 .../org/apache/tez/common/TezJobConfig.java     |   6 +
 .../apache/tez/runtime/api/impl/InputSpec.java  |   2 +-
 .../apache/tez/runtime/api/impl/OutputSpec.java |   2 +-
 .../runtime/api/impl/TezHeartbeatRequest.java   |   2 +-
 .../BroadcastShuffleInputEventHandler.java      |  72 ++++++++--
 .../input/BroadcastShuffleManager.java          | 131 +++++++++++++------
 .../broadcast/output/FileBasedKVWriter.java     |  60 +++++++--
 .../library/input/ShuffledUnorderedKVInput.java |   3 +-
 .../library/output/OnFileUnorderedKVOutput.java |  46 ++++++-
 .../runtime/library/shuffle/common/Fetcher.java |  86 +-----------
 .../library/shuffle/common/ShuffleUtils.java    |  81 ++++++++++++
 .../src/main/proto/ShufflePayloads.proto        |   9 +-
 12 files changed, 353 insertions(+), 147 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/d154b49f/tez-api/src/main/java/org/apache/tez/common/TezJobConfig.java
----------------------------------------------------------------------
diff --git a/tez-api/src/main/java/org/apache/tez/common/TezJobConfig.java b/tez-api/src/main/java/org/apache/tez/common/TezJobConfig.java
index 07602ea..88ea16d 100644
--- a/tez-api/src/main/java/org/apache/tez/common/TezJobConfig.java
+++ b/tez-api/src/main/java/org/apache/tez/common/TezJobConfig.java
@@ -307,4 +307,10 @@ public class TezJobConfig {
    */
   public static final String DAG_CREDENTIALS_BINARY =  "tez.dag.credentials.binary";
   
+  public static final String TEZ_RUNTIME_BROADCAST_DATA_VIA_EVENTS_ENABLED = "tez.runtime.broadcast.data-via-events.enabled";
+  public static final boolean TEZ_RUNTIME_BROADCAST_DATA_VIA_EVENTS_ENABLED_DEFAULT = false;
+  
+  public static final String TEZ_RUNTIME_BROADCAST_DATA_VIA_EVENTS_MAX_SIZE = "tez.runtime.broadcast.data-via-events.max-size";
+  public static final int TEZ_RUNTIME_BROADCAST_DATA_VIA_EVENTS_MAX_SIZE_DEFAULT = 200 << 10;// 200KB
+  
 }

http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/d154b49f/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/InputSpec.java
----------------------------------------------------------------------
diff --git a/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/InputSpec.java b/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/InputSpec.java
index 78ed886..2610b66 100644
--- a/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/InputSpec.java
+++ b/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/InputSpec.java
@@ -80,7 +80,7 @@ public class InputSpec implements Writable {
 
   public String toString() {
     return "{ sourceVertexName=" + sourceVertexName
-        + ", physicalEdgeCount" + physicalEdgeCount
+        + ", physicalEdgeCount=" + physicalEdgeCount
         + ", inputClassName=" + inputDescriptor.getClassName()
         + " }";
   }

http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/d154b49f/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/OutputSpec.java
----------------------------------------------------------------------
diff --git a/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/OutputSpec.java b/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/OutputSpec.java
index 4034cdd..96a407f 100644
--- a/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/OutputSpec.java
+++ b/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/OutputSpec.java
@@ -80,7 +80,7 @@ public class OutputSpec implements Writable {
 
   public String toString() {
     return "{ destinationVertexName=" + destinationVertexName
-        + ", physicalEdgeCount" + physicalEdgeCount
+        + ", physicalEdgeCount=" + physicalEdgeCount
         + ", outputClassName=" + outputDescriptor.getClassName()
         + " }";
   }

http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/d154b49f/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/TezHeartbeatRequest.java
----------------------------------------------------------------------
diff --git a/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/TezHeartbeatRequest.java b/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/TezHeartbeatRequest.java
index af7cebb..4bbf5ca 100644
--- a/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/TezHeartbeatRequest.java
+++ b/tez-runtime-internals/src/main/java/org/apache/tez/runtime/api/impl/TezHeartbeatRequest.java
@@ -130,7 +130,7 @@ public class TezHeartbeatRequest implements Writable {
         + ", requestId=" + requestId
         + ", startIndex=" + startIndex
         + ", maxEventsToGet=" + maxEvents
-        + ", taskAttemptId" + currentTaskAttemptID
+        + ", taskAttemptId=" + currentTaskAttemptID
         + ", eventCount=" + (events != null ? events.size() : 0)
         + " }";
   }

http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/d154b49f/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/input/BroadcastShuffleInputEventHandler.java
----------------------------------------------------------------------
diff --git a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/input/BroadcastShuffleInputEventHandler.java b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/input/BroadcastShuffleInputEventHandler.java
index 5011140..1c2092a 100644
--- a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/input/BroadcastShuffleInputEventHandler.java
+++ b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/input/BroadcastShuffleInputEventHandler.java
@@ -19,39 +19,55 @@
 
 package org.apache.tez.runtime.library.broadcast.input;
 
+import java.io.IOException;
 import java.util.List;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.io.compress.CompressionCodec;
 import org.apache.tez.dag.api.TezUncheckedException;
 import org.apache.tez.runtime.api.Event;
 import org.apache.tez.runtime.api.TezInputContext;
 import org.apache.tez.runtime.api.events.DataMovementEvent;
 import org.apache.tez.runtime.api.events.InputFailedEvent;
 import org.apache.tez.runtime.library.common.InputAttemptIdentifier;
+import org.apache.tez.runtime.library.shuffle.common.DiskFetchedInput;
+import org.apache.tez.runtime.library.shuffle.common.FetchedInput;
+import org.apache.tez.runtime.library.shuffle.common.FetchedInputAllocator;
+import org.apache.tez.runtime.library.shuffle.common.MemoryFetchedInput;
+import org.apache.tez.runtime.library.shuffle.common.ShuffleUtils;
 import org.apache.tez.runtime.library.shuffle.impl.ShuffleUserPayloads.DataMovementEventPayloadProto;
+import org.apache.tez.runtime.library.shuffle.impl.ShuffleUserPayloads.DataProto;
 
 import com.google.common.base.Preconditions;
 import com.google.protobuf.InvalidProtocolBufferException;
-import com.google.protobuf.TextFormat;
 
 public class BroadcastShuffleInputEventHandler {
 
   private static final Log LOG = LogFactory.getLog(BroadcastShuffleInputEventHandler.class);
   
+  private final Configuration conf;
   private final BroadcastShuffleManager shuffleManager;
+  private final FetchedInputAllocator inputAllocator;
+  private final CompressionCodec codec;
   
-  public BroadcastShuffleInputEventHandler(TezInputContext inputContext, BroadcastShuffleManager shuffleManager) {
+  public BroadcastShuffleInputEventHandler(TezInputContext inputContext, Configuration conf,
+      BroadcastShuffleManager shuffleManager,
+      FetchedInputAllocator inputAllocator, CompressionCodec codec) {
+    this.conf = conf;
     this.shuffleManager = shuffleManager;
+    this.inputAllocator = inputAllocator;
+    this.codec = codec;
   }
 
-  public void handleEvents(List<Event> events) {
+  public void handleEvents(List<Event> events) throws IOException {
     for (Event event : events) {
       handleEvent(event);
     }
   }
   
-  private void handleEvent(Event event) {
+  private void handleEvent(Event event) throws IOException {
     if (event instanceof DataMovementEvent) {
       processDataMovementEvent((DataMovementEvent)event);
     } else if (event instanceof InputFailedEvent) {
@@ -62,7 +78,7 @@ public class BroadcastShuffleInputEventHandler {
   }
   
   
-  private void processDataMovementEvent(DataMovementEvent dme) {
+  private void processDataMovementEvent(DataMovementEvent dme) throws IOException {
     Preconditions.checkArgument(dme.getSourceIndex() == 0,
         "Unexpected srcIndex: " + dme.getSourceIndex()
             + " on DataMovementEvent. Can only be 0");
@@ -75,18 +91,58 @@ public class BroadcastShuffleInputEventHandler {
     LOG.info("Processing DataMovementEvent with srcIndex: "
         + dme.getSourceIndex() + ", targetIndex: " + dme.getTargetIndex()
         + ", attemptNum: " + dme.getVersion() + ", payload: "
-        + TextFormat.shortDebugString(shufflePayload));
+        + stringify(shufflePayload));
     if (shufflePayload.getOutputGenerated()) {
-      InputAttemptIdentifier srcAttemptIdentifier = new InputAttemptIdentifier(dme.getTargetIndex(), dme.getVersion(), shufflePayload.getPathComponent());
-      shuffleManager.addKnownInput(shufflePayload.getHost(), shufflePayload.getPort(), srcAttemptIdentifier, 0);
+      InputAttemptIdentifier srcAttemptIdentifier = new InputAttemptIdentifier(
+          dme.getTargetIndex(), dme.getVersion(),
+          shufflePayload.getPathComponent());
+      if (shufflePayload.hasData()) {
+        DataProto dataProto = shufflePayload.getData();
+        FetchedInput fetchedInput = inputAllocator.allocate(dataProto.getRawLength(), srcAttemptIdentifier);
+        moveDataToFetchedInput(dataProto, fetchedInput);
+        shuffleManager.addCompletedInputWithData(srcAttemptIdentifier, fetchedInput);
+      } else {
+        shuffleManager.addKnownInput(shufflePayload.getHost(), shufflePayload.getPort(), srcAttemptIdentifier, 0);
+      }
     } else {
       shuffleManager.addCompletedInputWithNoData(new InputAttemptIdentifier(dme.getTargetIndex(), dme.getVersion()));
     }
   }
   
+  private void moveDataToFetchedInput(DataProto dataProto,
+      FetchedInput fetchedInput) throws IOException {
+    switch (fetchedInput.getType()) {
+    case DISK:
+      ShuffleUtils.shuffleToDisk((DiskFetchedInput) fetchedInput, dataProto
+          .getData().newInput(), dataProto.getCompressedLength(), LOG);
+      break;
+    case MEMORY:
+      ShuffleUtils.shuffleToMemory(conf, (MemoryFetchedInput) fetchedInput,
+          dataProto.getData().newInput(), dataProto.getRawLength(),
+          dataProto.getCompressedLength(), codec, LOG);
+      break;
+    case WAIT:
+    default:
+      throw new TezUncheckedException("Unexpected type: "
+          + fetchedInput.getType());
+    }
+  }
+  
   private void processInputFailedEvent(InputFailedEvent ife) {
     InputAttemptIdentifier srcAttemptIdentifier = new InputAttemptIdentifier(ife.getTargetIndex(), ife.getVersion());
     shuffleManager.obsoleteKnownInput(srcAttemptIdentifier);
   }
+  
+  private String stringify(DataMovementEventPayloadProto dmProto) {
+    StringBuilder sb = new StringBuilder();
+    sb.append("[");
+    sb.append("outputGenerated: " + dmProto.getOutputGenerated()).append(", ");
+    sb.append("host: " + dmProto.getHost()).append(", ");
+    sb.append("port: " + dmProto.getPort()).append(", ");
+    sb.append("pathComponent: " + dmProto.getPathComponent()).append(", ");
+    sb.append("runDuration: " + dmProto.getRunDuration()).append(", ");
+    sb.append("hasData: " + dmProto.hasData());
+    return sb.toString();
+  }
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/d154b49f/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/input/BroadcastShuffleManager.java
----------------------------------------------------------------------
diff --git a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/input/BroadcastShuffleManager.java b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/input/BroadcastShuffleManager.java
index 16e2fa6..5f77cb1 100644
--- a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/input/BroadcastShuffleManager.java
+++ b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/input/BroadcastShuffleManager.java
@@ -42,9 +42,7 @@ import javax.crypto.SecretKey;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.io.compress.CodecPool;
 import org.apache.hadoop.io.compress.CompressionCodec;
-import org.apache.hadoop.io.compress.Decompressor;
 import org.apache.hadoop.io.compress.DefaultCodec;
 import org.apache.hadoop.util.ReflectionUtils;
 import org.apache.tez.common.TezJobConfig;
@@ -114,7 +112,6 @@ public class BroadcastShuffleManager implements FetcherCallback {
   private final int connectionTimeout;
   private final int readTimeout;
   private final CompressionCodec codec;
-  private final Decompressor decompressor;
   
   private final FetchFutureCallback fetchFutureCallback = new FetchFutureCallback();
   
@@ -126,9 +123,17 @@ public class BroadcastShuffleManager implements FetcherCallback {
     this.inputContext = inputContext;
     this.conf = conf;
     this.numInputs = numInputs;
-    
-    this.inputEventHandler = new BroadcastShuffleInputEventHandler(inputContext, this);
+
+    if (ConfigUtils.isIntermediateInputCompressed(conf)) {
+      Class<? extends CompressionCodec> codecClass = ConfigUtils
+          .getIntermediateInputCompressorClass(conf, DefaultCodec.class);
+      codec = ReflectionUtils.newInstance(codecClass, conf);
+    } else {
+      codec = null;
+    }
+
     this.inputManager = new BroadcastInputManager(inputContext.getUniqueIdentifier(), conf);
+    this.inputEventHandler = new BroadcastShuffleInputEventHandler(inputContext, this.conf, this, this.inputManager, codec);
 
     completedInputSet = Collections.newSetFromMap(new ConcurrentHashMap<InputIdentifier, Boolean>(numInputs));
     completedInputs = new LinkedBlockingQueue<FetchedInput>(numInputs);
@@ -175,17 +180,9 @@ public class BroadcastShuffleManager implements FetcherCallback {
         TezJobConfig.TEZ_RUNTIME_SHUFFLE_READ_TIMEOUT,
         TezJobConfig.DEFAULT_TEZ_RUNTIME_SHUFFLE_READ_TIMEOUT);
     
-    if (ConfigUtils.isIntermediateInputCompressed(conf)) {
-      Class<? extends CompressionCodec> codecClass = ConfigUtils
-          .getIntermediateInputCompressorClass(conf, DefaultCodec.class);
-      codec = ReflectionUtils.newInstance(codecClass, conf);
-      decompressor = CodecPool.getDecompressor(codec);
-    } else {
-      codec = null;
-      decompressor = null;
-    }
+    
     LOG.info("BroadcastShuffleManager -> numInputs: " + numInputs
-        + " compressionCodec: " + (codec == null ? null : codec.getClass()
+        + " compressionCodec: " + (codec == null ? "NoCompressionCodec" : codec.getClass()
         .getName()) + ", numFetchers: " + numFetchers);
   }
   
@@ -204,16 +201,19 @@ public class BroadcastShuffleManager implements FetcherCallback {
     public Void call() throws Exception {
       while (numCompletedInputs.get() < numInputs) {
         lock.lock();
-        if (numRunningFetchers.get() >= numFetchers || pendingHosts.size() == 0) {
-          try {
-            wakeLoop.await();
-          } finally {
-            lock.unlock();
+        try {
+          if (numRunningFetchers.get() >= numFetchers || pendingHosts.size() == 0) {
+            if (numCompletedInputs.get() < numInputs) {
+              wakeLoop.await();
+            }
           }
+        } finally {
+          lock.unlock();
         }
+
         if (shuffleError != null) {
-          // InputContext has already been informed of a fatal error.
-          // Initiate shutdown.
+          // InputContext has already been informed of a fatal error. Relying on
+          // tez to kill the task.
           break;
         }
 
@@ -266,7 +266,9 @@ public class BroadcastShuffleManager implements FetcherCallback {
         BroadcastShuffleManager.this, inputManager,
         inputContext.getApplicationId(), shuffleSecret, conf);
     fetcherBuilder.setConnectionParameters(connectionTimeout, readTimeout);
-    fetcherBuilder.setCompressionParameters(codec, decompressor);
+    if (codec != null) {
+      fetcherBuilder.setCompressionParameters(codec);
+    }
 
     // Remove obsolete inputs from the list being given to the fetcher. Also
     // remove from the obsolete list.
@@ -332,9 +334,7 @@ public class BroadcastShuffleManager implements FetcherCallback {
     if (!completedInputSet.contains(inputIdentifier)) {
       synchronized (completedInputSet) {
         if (!completedInputSet.contains(inputIdentifier)) {
-          completedInputSet.add(inputIdentifier);
-          completedInputs.add(new NullFetchedInput(srcAttemptIdentifier));
-          numCompletedInputs.incrementAndGet();
+          registerCompletedInput(new NullFetchedInput(srcAttemptIdentifier));
         }
       }
     }
@@ -348,13 +348,52 @@ public class BroadcastShuffleManager implements FetcherCallback {
     }
   }
 
+  public void addCompletedInputWithData(
+      InputAttemptIdentifier srcAttemptIdentifier, FetchedInput fetchedInput)
+      throws IOException {
+    InputIdentifier inputIdentifier = srcAttemptIdentifier.getInputIdentifier();
+
+    LOG.info("Received Data via Event: " + srcAttemptIdentifier + " to "
+        + fetchedInput.getType());
+    // Count irrespective of whether this is a copy of an already fetched input
+    lock.lock();
+    try {
+      lastProgressTime = System.currentTimeMillis();
+    } finally {
+      lock.unlock();
+    }
+
+    boolean committed = false;
+    if (!completedInputSet.contains(inputIdentifier)) {
+      synchronized (completedInputSet) {
+        if (!completedInputSet.contains(inputIdentifier)) {
+          fetchedInput.commit();
+          committed = true;
+          registerCompletedInput(fetchedInput);
+        }
+      }
+    }
+    if (!committed) {
+      fetchedInput.abort(); // If this fails, the fetcher may attempt another
+                            // abort.
+    } else {
+      lock.lock();
+      try {
+        // Signal the wakeLoop to check for termination.
+        wakeLoop.signal();
+      } finally {
+        lock.unlock();
+      }
+    }
+  }
+
   public synchronized void obsoleteKnownInput(InputAttemptIdentifier srcAttemptIdentifier) {
     obsoletedInputs.add(srcAttemptIdentifier);
     // TODO NEWTEZ Maybe inform the fetcher about this. For now, this is used during the initial fetch list construction.
   }
   
   
-  public void handleEvents(List<Event> events) {
+  public void handleEvents(List<Event> events) throws IOException {
     inputEventHandler.handleEvents(events);
   }
 
@@ -383,9 +422,7 @@ public class BroadcastShuffleManager implements FetcherCallback {
         if (!completedInputSet.contains(inputIdentifier)) {
           fetchedInput.commit();
           committed = true;
-          completedInputSet.add(inputIdentifier);
-          completedInputs.add(fetchedInput);
-          numCompletedInputs.incrementAndGet();
+          registerCompletedInput(fetchedInput);
         }
       }
     }
@@ -434,6 +471,17 @@ public class BroadcastShuffleManager implements FetcherCallback {
     }
   }
   
+  private void registerCompletedInput(FetchedInput fetchedInput) {
+    lock.lock();
+    try {
+      completedInputSet.add(fetchedInput.getInputAttemptIdentifier().getInputIdentifier());
+      completedInputs.add(fetchedInput);
+      numCompletedInputs.incrementAndGet();
+    } finally {
+      lock.unlock();
+    }
+  }
+  
   /////////////////// Methods for walking the available inputs
   
   /**
@@ -452,7 +500,12 @@ public class BroadcastShuffleManager implements FetcherCallback {
    * @return true if all of the required inputs have been fetched.
    */
   public boolean allInputsFetched() {
-    return numCompletedInputs.get() == numInputs;
+    lock.lock();
+    try {
+      return numCompletedInputs.get() == numInputs;
+    } finally {
+      lock.unlock();
+    }
   }
 
   /**
@@ -463,20 +516,20 @@ public class BroadcastShuffleManager implements FetcherCallback {
   public FetchedInput getNextInput() throws InterruptedException {
     FetchedInput input = null;
     do {
-      input = completedInputs.peek();
-      if (input == null) {
-        if (allInputsFetched()) {
+      // Check for no additional inputs
+      lock.lock();
+      try {
+        input = completedInputs.peek();
+        if (input == null && allInputsFetched()) {
           break;
-        } else {
-          input = completedInputs.take(); // block
         }
-      } else {
-        input = completedInputs.poll();
+      } finally {
+        lock.unlock();
       }
+      input = completedInputs.take(); // block
     } while (input instanceof NullFetchedInput);
     return input;
   }
-
   /////////////////// End of methods for walking the available inputs
   
   

http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/d154b49f/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/output/FileBasedKVWriter.java
----------------------------------------------------------------------
diff --git a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/output/FileBasedKVWriter.java b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/output/FileBasedKVWriter.java
index e6dc581..3bfa3f3 100644
--- a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/output/FileBasedKVWriter.java
+++ b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/broadcast/output/FileBasedKVWriter.java
@@ -23,14 +23,14 @@ import java.io.IOException;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FSDataInputStream;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.LocalFileSystem;
 import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.io.IOUtils;
 import org.apache.hadoop.io.compress.CompressionCodec;
 import org.apache.hadoop.io.compress.DefaultCodec;
 import org.apache.hadoop.util.ReflectionUtils;
-import org.apache.tez.common.TezJobConfig;
-import org.apache.tez.common.TezUtils;
 import org.apache.tez.runtime.api.TezOutputContext;
 import org.apache.tez.runtime.library.api.KeyValueWriter;
 import org.apache.tez.runtime.library.common.ConfigUtils;
@@ -40,6 +40,8 @@ import org.apache.tez.runtime.library.common.sort.impl.TezIndexRecord;
 import org.apache.tez.runtime.library.common.sort.impl.TezSpillRecord;
 import org.apache.tez.runtime.library.common.task.local.output.TezTaskOutput;
 
+import com.google.common.base.Preconditions;
+
 public class FileBasedKVWriter implements KeyValueWriter {
 
   private static final Log LOG = LogFactory.getLog(FileBasedKVWriter.class);
@@ -57,18 +59,19 @@ public class FileBasedKVWriter implements KeyValueWriter {
   private FileSystem rfs;
   private IFile.Writer writer;
 
+  private Path outputPath;
+  private Path indexPath;
+  
   private TezTaskOutput ouputFileManager;
+  private boolean closed = false;
 
   // TODO NEWTEZ Define Counters
   // Number of records
   // Time waiting for a write to complete, if that's possible.
   // Size of key-value pairs written.
 
-  public FileBasedKVWriter(TezOutputContext outputContext) throws IOException {
-    this.conf = TezUtils.createConfFromUserPayload(outputContext
-        .getUserPayload());
-    this.conf.setStrings(TezJobConfig.LOCAL_DIRS,
-        outputContext.getWorkDirs());
+  public FileBasedKVWriter(TezOutputContext outputContext, Configuration conf) throws IOException {
+    this.conf = conf;
 
     this.rfs = ((LocalFileSystem) FileSystem.getLocal(this.conf)).getRaw();
 
@@ -87,6 +90,8 @@ public class FileBasedKVWriter implements KeyValueWriter {
 
     this.ouputFileManager = TezRuntimeUtils.instantiateTaskOutputManager(conf,
         outputContext);
+    LOG.info("Craeted KVWriter -> " + "compressionCodec: " + (codec == null ? "NoCompressionCodec"
+        : codec.getClass().getName()));
 
     initWriter();
   }
@@ -96,16 +101,17 @@ public class FileBasedKVWriter implements KeyValueWriter {
    * @throws IOException
    */
   public boolean close() throws IOException {
+    this.closed = true;
     this.writer.close();
     TezIndexRecord rec = new TezIndexRecord(0, writer.getRawLength(),
         writer.getCompressedLength());
     TezSpillRecord sr = new TezSpillRecord(1);
     sr.putIndex(rec, 0);
 
-    Path indexFile = ouputFileManager
+    this.indexPath = ouputFileManager
         .getOutputIndexFileForWrite(INDEX_RECORD_LENGTH);
-    LOG.info("Writing index file: " + indexFile);
-    sr.writeToFile(indexFile, conf);
+    LOG.info("Writing index file: " + indexPath);
+    sr.writeToFile(indexPath, conf);
     return numRecords > 0;
   }
 
@@ -116,16 +122,42 @@ public class FileBasedKVWriter implements KeyValueWriter {
   }
 
   public void initWriter() throws IOException {
-    Path outputFile = ouputFileManager.getOutputFileForWrite();
-    LOG.info("Writing data file: " + outputFile);
+    this.outputPath = ouputFileManager.getOutputFileForWrite();
+    LOG.info("Writing data file: " + outputPath);
 
     // TODO NEWTEZ Consider making the buffer size configurable. Also consider
     // setting up an in-memory buffer which is occasionally flushed to disk so
     // that the output does not block.
 
     // TODO NEWTEZ maybe use appropriate counter
-    this.writer = new IFile.Writer(conf, rfs, outputFile, keyClass, valClass,
+    this.writer = new IFile.Writer(conf, rfs, outputPath, keyClass, valClass,
         codec, null);
   }
+  
+  public long getRawLength() {
+    Preconditions.checkState(closed, "Only available after the Writer has been closed");
+    return this.writer.getRawLength();
+  }
+  
+  public long getCompressedLength() {
+    Preconditions.checkState(closed, "Only available after the Writer has been closed");
+    return this.writer.getCompressedLength();
+  }
 
-}
+  public byte[] getData() throws IOException {
+    Preconditions.checkState(closed,
+        "Only available after the Writer has been closed");
+    FSDataInputStream inStream = null;
+    byte[] buf = null;
+    try {
+      inStream = rfs.open(outputPath);
+      buf = new byte[(int) getCompressedLength()];
+      IOUtils.readFully(inStream, buf, 0, (int) getCompressedLength());
+    } finally {
+      if (inStream != null) {
+        inStream.close();
+      }
+    }
+    return buf;
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/d154b49f/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/input/ShuffledUnorderedKVInput.java
----------------------------------------------------------------------
diff --git a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/input/ShuffledUnorderedKVInput.java b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/input/ShuffledUnorderedKVInput.java
index b6e17b6..4f94210 100644
--- a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/input/ShuffledUnorderedKVInput.java
+++ b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/input/ShuffledUnorderedKVInput.java
@@ -18,6 +18,7 @@
 
 package org.apache.tez.runtime.library.input;
 
+import java.io.IOException;
 import java.util.List;
 
 import org.apache.commons.logging.Log;
@@ -68,7 +69,7 @@ public class ShuffledUnorderedKVInput implements LogicalInput {
   }
 
   @Override
-  public void handleEvents(List<Event> inputEvents) {
+  public void handleEvents(List<Event> inputEvents) throws IOException {
     shuffleManager.handleEvents(inputEvents);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/d154b49f/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/output/OnFileUnorderedKVOutput.java
----------------------------------------------------------------------
diff --git a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/output/OnFileUnorderedKVOutput.java b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/output/OnFileUnorderedKVOutput.java
index db4085c..658e993 100644
--- a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/output/OnFileUnorderedKVOutput.java
+++ b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/output/OnFileUnorderedKVOutput.java
@@ -22,8 +22,13 @@ import java.nio.ByteBuffer;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.classification.InterfaceAudience.Private;
+import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.yarn.api.ApplicationConstants;
+import org.apache.tez.common.TezJobConfig;
+import org.apache.tez.common.TezUtils;
 import org.apache.tez.dag.api.TezUncheckedException;
 import org.apache.tez.runtime.api.Event;
 import org.apache.tez.runtime.api.LogicalOutput;
@@ -33,15 +38,24 @@ import org.apache.tez.runtime.library.api.KeyValueWriter;
 import org.apache.tez.runtime.library.broadcast.output.FileBasedKVWriter;
 import org.apache.tez.runtime.library.shuffle.common.ShuffleUtils;
 import org.apache.tez.runtime.library.shuffle.impl.ShuffleUserPayloads.DataMovementEventPayloadProto;
+import org.apache.tez.runtime.library.shuffle.impl.ShuffleUserPayloads.DataProto;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
+import com.google.protobuf.ByteString;
 
 public class OnFileUnorderedKVOutput implements LogicalOutput {
 
+  private static final Log LOG = LogFactory.getLog(OnFileUnorderedKVOutput.class);
+
   private TezOutputContext outputContext;
   private FileBasedKVWriter kvWriter;
+  
+  private Configuration conf;
+  
+  private boolean dataViaEventsEnabled;
+  private int dataViaEventsMaxSize;
 
   public OnFileUnorderedKVOutput() {
   }
@@ -50,7 +64,23 @@ public class OnFileUnorderedKVOutput implements LogicalOutput {
   public List<Event> initialize(TezOutputContext outputContext)
       throws Exception {
     this.outputContext = outputContext;
-    this.kvWriter = new FileBasedKVWriter(outputContext);
+    this.conf = TezUtils.createConfFromUserPayload(outputContext
+        .getUserPayload());
+    this.conf.setStrings(TezJobConfig.LOCAL_DIRS,
+        outputContext.getWorkDirs());
+    
+    this.dataViaEventsEnabled = conf.getBoolean(
+        TezJobConfig.TEZ_RUNTIME_BROADCAST_DATA_VIA_EVENTS_ENABLED,
+        TezJobConfig.TEZ_RUNTIME_BROADCAST_DATA_VIA_EVENTS_ENABLED_DEFAULT);
+    this.dataViaEventsMaxSize = conf.getInt(
+        TezJobConfig.TEZ_RUNTIME_BROADCAST_DATA_VIA_EVENTS_MAX_SIZE,
+        TezJobConfig.TEZ_RUNTIME_BROADCAST_DATA_VIA_EVENTS_MAX_SIZE_DEFAULT);
+    
+    LOG.info(this.getClass().getSimpleName() + " running with params -> "
+        + "dataViaEventsEnabled: " + dataViaEventsEnabled
+        + ", dataViaEventsMaxSize: " + dataViaEventsMaxSize);
+    
+    this.kvWriter = new FileBasedKVWriter(outputContext, conf);
     return Collections.emptyList();
   }
 
@@ -67,8 +97,22 @@ public class OnFileUnorderedKVOutput implements LogicalOutput {
   @Override
   public List<Event> close() throws Exception {
     boolean outputGenerated = this.kvWriter.close();
+    
     DataMovementEventPayloadProto.Builder payloadBuilder = DataMovementEventPayloadProto
         .newBuilder();
+    
+    LOG.info("Closing KVOutput: RawLegnth: " + this.kvWriter.getRawLength()
+        + ", CompressedLength: " + this.kvWriter.getCompressedLength());
+
+    if (dataViaEventsEnabled && outputGenerated && this.kvWriter.getCompressedLength() <= dataViaEventsMaxSize) {
+      LOG.info("Serialzing actual data into DataMovementEvent, dataSize: " + this.kvWriter.getCompressedLength());
+      byte[] data = this.kvWriter.getData();
+      DataProto.Builder dataProtoBuilder = DataProto.newBuilder();
+      dataProtoBuilder.setData(ByteString.copyFrom(data));
+      dataProtoBuilder.setRawLength((int)this.kvWriter.getRawLength());
+      dataProtoBuilder.setCompressedLength((int)this.kvWriter.getCompressedLength());
+      payloadBuilder.setData(dataProtoBuilder.build());
+    }
 
     String host = getHost();
     ByteBuffer shuffleMetadata = outputContext

http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/d154b49f/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/shuffle/common/Fetcher.java
----------------------------------------------------------------------
diff --git a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/shuffle/common/Fetcher.java b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/shuffle/common/Fetcher.java
index 1b5a8b2..0f41cda 100644
--- a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/shuffle/common/Fetcher.java
+++ b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/shuffle/common/Fetcher.java
@@ -20,8 +20,6 @@ package org.apache.tez.runtime.library.shuffle.common;
 
 import java.io.DataInputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -45,14 +43,12 @@ import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.io.IOUtils;
 import org.apache.hadoop.io.compress.CompressionCodec;
-import org.apache.hadoop.io.compress.Decompressor;
 import org.apache.hadoop.security.ssl.SSLFactory;
 import org.apache.hadoop.yarn.api.records.ApplicationId;
 import org.apache.tez.common.TezJobConfig;
 import org.apache.tez.runtime.library.common.InputAttemptIdentifier;
 import org.apache.tez.runtime.library.common.security.SecureShuffleUtils;
 import org.apache.tez.runtime.library.common.shuffle.impl.ShuffleHeader;
-import org.apache.tez.runtime.library.common.sort.impl.IFileInputStream;
 import org.apache.tez.runtime.library.shuffle.common.FetchedInput.Type;
 
 import com.google.common.base.Preconditions;
@@ -70,7 +66,6 @@ public class Fetcher implements Callable<FetchResult> {
 
   // Configurable fields.
   private CompressionCodec codec;
-  private Decompressor decompressor;
   private int connectionTimeout;
   private int readTimeout;
 
@@ -264,10 +259,12 @@ public class Fetcher implements Callable<FetchResult> {
           + fetchedInput.getType());
 
       if (fetchedInput.getType() == Type.MEMORY) {
-        shuffleToMemory((MemoryFetchedInput) fetchedInput, input,
-            (int) decompressedLength, (int) compressedLength);
+        ShuffleUtils.shuffleToMemory(conf, (MemoryFetchedInput) fetchedInput,
+            input, (int) decompressedLength, (int) compressedLength, codec,
+            LOG);
       } else {
-        shuffleToDisk((DiskFetchedInput) fetchedInput, input, compressedLength);
+        ShuffleUtils.shuffleToDisk((DiskFetchedInput) fetchedInput, input,
+            compressedLength, LOG);
       }
 
       // Inform the shuffle scheduler
@@ -305,75 +302,6 @@ public class Fetcher implements Callable<FetchResult> {
     }
   }
 
-  @SuppressWarnings("resource")
-  private void shuffleToMemory(MemoryFetchedInput fetchedInput,
-      InputStream input, int decompressedLength, int compressedLength)
-      throws IOException {
-    IFileInputStream checksumIn = new IFileInputStream(input, compressedLength,
-        conf);
-
-    input = checksumIn;
-
-    // Are map-outputs compressed?
-    if (codec != null) {
-      decompressor.reset();
-      input = codec.createInputStream(input, decompressor);
-    }
-    // Copy map-output into an in-memory buffer
-    byte[] shuffleData = fetchedInput.getBytes();
-
-    try {
-      IOUtils.readFully(input, shuffleData, 0, shuffleData.length);
-      // metrics.inputBytes(shuffleData.length);
-      LOG.info("Read " + shuffleData.length + " bytes from input for "
-          + fetchedInput.getInputAttemptIdentifier());
-    } catch (IOException ioe) {
-      // Close the streams
-      IOUtils.cleanup(LOG, input);
-      // Re-throw
-      throw ioe;
-    }
-  }
-
-  private void shuffleToDisk(DiskFetchedInput fetchedInput, InputStream input,
-      long compressedLength) throws IOException {
-    // Copy data to local-disk
-    OutputStream output = fetchedInput.getOutputStream();
-    long bytesLeft = compressedLength;
-    try {
-      final int BYTES_TO_READ = 64 * 1024;
-      byte[] buf = new byte[BYTES_TO_READ];
-      while (bytesLeft > 0) {
-        int n = input.read(buf, 0, (int) Math.min(bytesLeft, BYTES_TO_READ));
-        if (n < 0) {
-          throw new IOException("read past end of stream reading "
-              + fetchedInput.getInputAttemptIdentifier());
-        }
-        output.write(buf, 0, n);
-        bytesLeft -= n;
-        // metrics.inputBytes(n);
-      }
-
-      LOG.info("Read " + (compressedLength - bytesLeft)
-          + " bytes from input for " + fetchedInput.getInputAttemptIdentifier());
-
-      output.close();
-    } catch (IOException ioe) {
-      // Close the streams
-      IOUtils.cleanup(LOG, input, output);
-
-      // Re-throw
-      throw ioe;
-    }
-
-    // Sanity check
-    if (bytesLeft != 0) {
-      throw new IOException("Incomplete input received for "
-          + fetchedInput.getInputAttemptIdentifier() + " from " + host + " ("
-          + bytesLeft + " bytes missing of " + compressedLength + ")");
-    }
-  }
-
   /**
    * Do some basic verification on the input received -- Being defensive
    * 
@@ -558,10 +486,8 @@ public class Fetcher implements Callable<FetchResult> {
           shuffleSecret, conf);
     }
 
-    public FetcherBuilder setCompressionParameters(CompressionCodec codec,
-        Decompressor decompressor) {
+    public FetcherBuilder setCompressionParameters(CompressionCodec codec) {
       fetcher.codec = codec;
-      fetcher.decompressor = decompressor;
       return this;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/d154b49f/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/shuffle/common/ShuffleUtils.java
----------------------------------------------------------------------
diff --git a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/shuffle/common/ShuffleUtils.java b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/shuffle/common/ShuffleUtils.java
index 5bbd6fb..25718e1 100644
--- a/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/shuffle/common/ShuffleUtils.java
+++ b/tez-runtime-library/src/main/java/org/apache/tez/runtime/library/shuffle/common/ShuffleUtils.java
@@ -19,16 +19,25 @@
 package org.apache.tez.runtime.library.shuffle.common;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.nio.ByteBuffer;
 
 import javax.crypto.SecretKey;
 
+import org.apache.commons.logging.Log;
+import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.io.DataInputByteBuffer;
 import org.apache.hadoop.io.DataOutputBuffer;
+import org.apache.hadoop.io.IOUtils;
+import org.apache.hadoop.io.compress.CodecPool;
+import org.apache.hadoop.io.compress.CompressionCodec;
+import org.apache.hadoop.io.compress.Decompressor;
 import org.apache.hadoop.security.token.Token;
 import org.apache.hadoop.yarn.api.records.ApplicationId;
 import org.apache.tez.runtime.library.common.security.JobTokenIdentifier;
 import org.apache.tez.runtime.library.common.security.JobTokenSecretManager;
+import org.apache.tez.runtime.library.common.sort.impl.IFileInputStream;
 
 public class ShuffleUtils {
 
@@ -64,6 +73,78 @@ public class ShuffleUtils {
     }
   }
   
+  @SuppressWarnings("resource")
+  public static void shuffleToMemory(Configuration conf,
+      MemoryFetchedInput fetchedInput, InputStream input,
+      int decompressedLength, int compressedLength,
+      CompressionCodec codec, Log LOG) throws IOException {
+    IFileInputStream checksumIn = new IFileInputStream(input, compressedLength,
+        conf);
+
+    input = checksumIn;
+
+    // Are map-outputs compressed?
+    if (codec != null) {
+      Decompressor decompressor = CodecPool.getDecompressor(codec);
+      decompressor.reset();
+      input = codec.createInputStream(input, decompressor);
+    }
+    // Copy map-output into an in-memory buffer
+    byte[] shuffleData = fetchedInput.getBytes();
+
+    try {
+      IOUtils.readFully(input, shuffleData, 0, shuffleData.length);
+      // metrics.inputBytes(shuffleData.length);
+      LOG.info("Read " + shuffleData.length + " bytes from input for "
+          + fetchedInput.getInputAttemptIdentifier());
+    } catch (IOException ioe) {
+      // Close the streams
+      IOUtils.cleanup(LOG, input);
+      // Re-throw
+      throw ioe;
+    }
+  }
+  
+  public static void shuffleToDisk(DiskFetchedInput fetchedInput,
+      InputStream input, long compressedLength, Log LOG)
+      throws IOException {
+    // Copy data to local-disk
+    OutputStream output = fetchedInput.getOutputStream();
+    long bytesLeft = compressedLength;
+    try {
+      final int BYTES_TO_READ = 64 * 1024;
+      byte[] buf = new byte[BYTES_TO_READ];
+      while (bytesLeft > 0) {
+        int n = input.read(buf, 0, (int) Math.min(bytesLeft, BYTES_TO_READ));
+        if (n < 0) {
+          throw new IOException("read past end of stream reading "
+              + fetchedInput.getInputAttemptIdentifier());
+        }
+        output.write(buf, 0, n);
+        bytesLeft -= n;
+        // metrics.inputBytes(n);
+      }
+
+      LOG.info("Read " + (compressedLength - bytesLeft)
+          + " bytes from input for " + fetchedInput.getInputAttemptIdentifier());
+
+      output.close();
+    } catch (IOException ioe) {
+      // Close the streams
+      IOUtils.cleanup(LOG, input, output);
+
+      // Re-throw
+      throw ioe;
+    }
+
+    // Sanity check
+    if (bytesLeft != 0) {
+      throw new IOException("Incomplete input received for "
+          + fetchedInput.getInputAttemptIdentifier() + " ("
+          + bytesLeft + " bytes missing of " + compressedLength + ")");
+    }
+  }
+  
   // TODO NEWTEZ handle ssl shuffle
   public static StringBuilder constructBaseURIForShuffleHandler(String host, int port, int partition, ApplicationId appId) {
     StringBuilder sb = new StringBuilder("http://");

http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/d154b49f/tez-runtime-library/src/main/proto/ShufflePayloads.proto
----------------------------------------------------------------------
diff --git a/tez-runtime-library/src/main/proto/ShufflePayloads.proto b/tez-runtime-library/src/main/proto/ShufflePayloads.proto
index 0396eec..fb0d82a 100644
--- a/tez-runtime-library/src/main/proto/ShufflePayloads.proto
+++ b/tez-runtime-library/src/main/proto/ShufflePayloads.proto
@@ -26,12 +26,19 @@ message DataMovementEventPayloadProto {
   optional int32 port = 3;
   optional string path_component = 4;
   optional int32 run_duration = 5;
+  optional DataProto data = 6;
 } 
 
+message DataProto {
+  optional int32 raw_length = 1;
+  optional int32 compressed_length = 2;
+  optional bytes data = 3;
+}
+
 message InputInformationEventPayloadProto {
   optional int32 partition_range = 1;
 }
 
 message VertexManagerEventPayloadProto {
   optional int64 output_size = 1;
-}
\ No newline at end of file
+}