/****************************************************************
 *         MapReduce Benchmarking Experiments for               *  
 * "The Performance of MapReduce: An Indepth Study" (VLDB 2010) *
 ================================================================
 *       NUS epiC group  http://www.comp.nus.edu.sg/~epic/      *
 *                        July 21, 2010                         *
 ****************************************************************/

The software in this package is provided "as is" without further support. However, we are still interested in hearing what and how you use them. If you have any questions or comments, please contact:
  JIANG Dawei (jiangdw@comp.nus.edu.sg)
  SHI Lei (shilei@comp.nus.edu.sg)

The provided package consists of two parts: 
  "hadoop-0.19.2-VLDB" folder contains the hadoop-core jar file and the source code of the modification based on hadoop-0.19.2;
  "MapReduceBenchmark" folder contains the necessary libraries and the source code for our MapReduce performance study.

This README file covers the following topics regarding the performance study on MapReduce:
1. Prerequisites
2. Quick start guide
3. Benchmarking query examples
4. How to compile source code

===============
1. Prerequisits
===============

We use a modified version of Hadoop based on hadoop-0.19.2. For simplicity user can use the jar file (in "hadoop-0.19.2-VLDB" folder) to launch Hadoop clusters. We also provide the source code for our modified Hadoop. Please refer to Section 4 "How to compile source code" for more details.

If you want to run HadoopDB queries, PostgreSQL is required to be installed. Please refer to http://hadoopdb.sourceforge.net/guide/ on the installation and configuration of PostgreSQL and HadoopDB.


====================
2. Quick start guide
====================

-----------------------
2.1 Required jar files:
-----------------------
In our MapReduce benchmarking experiment, we use the following jar files:

  MRBenchmark.jar  // including all the benchmarks we implemented	
  hadoopdb.jar     // including all the benchmarks used in A.Pavlo's comparison paper and HadoopDB paper. Download from http://sourceforge.net/projects/hadoopdb/ 
  je-4.0.9.jar	   // for BerkerleyDB parsing
  protobuf.jar	   // for Google protocol buffer

For convenience, we provide all the jar files in the folder "MapReduceBenchmark/lib". Copy the first two jar files to your working directory, and the other two files to $HADOOP_HOME/lib/

MRBenchmark can be compiled using the source code included in MapReduceBenchmarks package. Please refer to Section 4 "How to compile source code" for more details.

-------------------------
2.2 Hadoop Configuration:
-------------------------
We configured hadoop cluster using the following parameters:

  dfs.block.size = 536870912
  io.file.buffer.size = 131072
  io.sort.mb = 500
  io.sort.spill.percent = 0.9
  io.sort.record.percent = 0.18
  io.sort.factor = 100
  mapred.job.shuffle.input.buffer.percent = 0.8
  mapred.inmem.merge.threshold=0
  mapred.job.reduce.input.buffer.percent=1.0
  mapred.map.tasks.speculative.execution=false
  mapred.reduce.tasks.speculative.execution=false
  dfs.replication=1
  mapred.job.reuse.jvm.num.tasks=-1
  mapred.child.java.opts=-server -Djava.net.preferIPv4Stack=true -Xmx1024m

If there are multiple disks on your machine, you can configure Hadoop to use multiple disks by specifying a comma-separated list of directories in "dfs.data.dir" and "mapred.local.dir".

--------------------
2.3 Data Generation:
--------------------
Data generator can be downloaded from: http://graffiti.cs.brown.edu/svn/benchmarks/datagen/

The generation follows A. Pavlo's procedure in http://database.cs.brown.edu/projects/mapreduce-vs-dbms/

There are three generated datasets: Grep, Rankings, and UserVisits. Grep dataset is stored in HDFS, and two files "Rankings.dat" and "UserVisits.dat" are located in the local disk on each node.

-------------------
2.4 Data Loading:
-------------------
a) Loading Text-format data
   Since teragen generates data directly to HDFS, We only need to upload Rankings and UserVisits data from local disk on each node to HDFS. User should use Hadoop "put" or "copyFromLocal" to upload the data. 

b) Loading BerkerlyDB binary-format data
   BerkerleyDB parser requires binary-format data. The following commands are executed on EACH NODE according to the original dataset located on the LOCAL DISK (specified in $PATH_TO_TableName_on_Local_Disk), and the results are located in HDFS (specified in $FILE_PATH_IN_HDFS). Note that on each node, the $FILE_PATH_IN_HDFS in the following commands are different (We use "part-xxxxx" to distinguish the file generated from different nodes).
   
   Rankings:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar KVLoader $PATH_TO_Rankings_on_Local_Disk $FILE_PATH_IN_HDFS 1
   
   UserVisits:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar KVLoader $PATH_TO_UserVisits_on_Local_Disk $FILE_PATH_IN_HDFS 2
   
   Note: In the above two commands, the last parameter is used to specified the table type. We use "1" representing Rankings table, and "2" representing UserVisits table.

c) DBMS data loading
   We also use HadoopDB to process Selection task. We followed the guide provided by HadoopDB (http://hadoopdb.sourceforge.net/guide/) to load data to PostgreSQL. 

--------------------------------------------------------
2.5 Creating indexes for Rankings and UserVisits datasets:
--------------------------------------------------------
Both text-format and binary indexes are created according to the TEXT-FORMAT Rankings and UserVisits data. In the following commands, $INPUT_PATH should be respectively specified as the path to the two datasets (text-format) located in HDFS.

a) Text-format indexes:
   Rankings:	
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar Loader -Ddfs.index.block.size=134217728 -p 1,string -s 0,int -i $INPUT_PATH -o $OUTPUT_PATH -r $NUM_OF_REDUCE_TASKS
   
   UserVisits:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar Loader -Ddfs.index.block.size=134217728 -p 1,string -s 2,date -i $INPUT_PATH -o $OUTPUT_PATH -r $NUM_OF_REDUCE_TASKS

b) BerkerlyDB Binary-format indexes:
   Rankings:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar Loader -Dmapred.loader.text=false -Dmapred.loader.kvwriter.dataset=1 -Ddfs.index.block.size=134217728 -p 1,string -s 0,int -i $INPUT_PATH -o $OUTPUT_PATH -r $NUM_OF_REDUCE_TASKS
   
   UserVisits:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar Loader -Dmapred.loader.text=false -Dmapred.loader.kvwriter.dataset=2 -Ddfs.index.block.size=134217728 -p 1,string -s 2,date -i $INPUT_PATH -o $OUTPUT_PATH -r $NUM_OF_REDUCE_TASKS


==============================
3. Benchmarking query examples
==============================

This section includes all the scripts that we run for the benchmarking experiments.

a) Scheduling Micro-Benchmark and Comparison of Different I/O modes
   We use TestDFSIO (located in hadoop-{$HADOOP_VERSION}-test.jar) to generate the dataset. For Direct I/O mode, we use our modified hadoop-0.19.3-dev-core.jar to launch the cluster; while for Stream I/O mode, we use original hadoop-0.19.2-core.jar. The following command is used to run this benchmark:
     
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar RawIO -Dio.file.buffer.size=131072 -i $INPUT_PATH -o $OUTPUT_PATH

b) Record Parsing Micro-Benchmark
   In this benchmark we excerpt approximately 512MB raw data from Rankings and UserVisits datasets.
     
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar ParseBenchmark $TABLE_NAME $TABLE_TYPE
   
   In this command, $TABLE_NAME is the 512MB raw data on local disk. $TABLE_TYPE is 1 if it is Rankings table, and 2 if it is UserVisits.

c) Grep Task
   HadoopMT:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar Grep -Dsearch.grep.pattern=XYZ -Dio.tuple.widths=10,90 -i $INPUT_PATH -o $OUTPUT_PATH

d) Selection Task
   HadoopMT:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar Selection -Dpage.rank=$PAGE_RANK -i $INPUT_PATH -o $OUTPUT_PATH

   HadoopMTi:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar IndexSelection -Dpage.rank=$PAGE_RANK -i $INPUT_PATH -o $OUTPUT_PATH
   
   HadoopBDB:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar KVSelection -Dpage.rank=$PAGE_RANK -i $INPUT_PATH -o $OUTPUT_PATH

   HadoopBDBi:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar KVIndexSelection -Dpage.rank=$PAGE_RANK -i $INPUT_PATH -o $OUTPUT_PATH

   Note: by default $PAGE_RANK=10

e) Aggregation Task
   We use Large Aggregation as examples, small aggragation tasks have same configuration parameters.
   HadoopMT:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar LargeAggregation -Dio.sort.key.usefp=$SORT_KEY_USEFP -i $INPUT_PATH -o $OUTPUT_PATH

   HadoopBDB:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar KVAggLarge -Dio.sort.key.usefp=$SORT_KEY_USEFP -i $INPUT_PATH -o $OUTPUT_PATH

   Note: Fingerprinting based grouping is enabled when $SORT_KEY_USEFP=true.

f) Aggregation Task with a date range
   Hadoop:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar HadoopAggregation -Duv.date.from=$START_DATE -Duv.date.to=$END_DATE -i $INPUT_PATH -o $OUTPUT_PATH

   HadoopMT:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar MutableAggregation -Dio.sort.key.usefp=$SORT_KEY_USEFP -Duv.date.from=$START_DATE -Duv.date.to=$END_DATE -i $INPUT_PATH -o $OUTPUT_PATH

   HadoopMTi:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar IndexAggregation -Dio.sort.key.usefp=$SORT_KEY_USEFP -Duv.date.from=$START_DATE -Duv.date.to=$END_DATE -i $INPUT_PATH -o $OUTPUT_PATH
   
   Note: the default date format is "YYYY-MM-DD".

g) Join Task
   Hadoop Mutable:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar MutableJoin -Duv.date.from=2000-01-15 -Duv.date.to=2000-01-22 -Djoin.rankings=$Rankings_PREFIX -Djoin.uservisits=$UserVisits_PREFIX -Dio.sort.key.usefp=$SORT_KEY_USEFP -i $PATH_TO_Rankings,$PATH_TO_UserVisits -r $NUM_OF_REDUCE_TASKS -o $OUTPUT_PATH

     Note: $Rankings_PREFIX and $UserVisits_PREFIX are used to distinguish different tables. For example, if Rankings data are located in "/benchmarks/data/Rankings/rankings[0-99].dat" in HDFS, then $Rankings_PREFIX=Rankings. $UserVisits_PREFIX has the similar definition.

   Simple Index:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar SimpleIndexJoin -Duv.date.from=2000-01-15 -Duv.date.to=2000-01-22 -Dio.sort.key.usefp=$SORT_KEY_USEFP -rankingsInput $PATH_to_Rankings -uvInput $PATH_TO_UserVisits_INDEX -r $NUM_OF_REDUCE_TASKS -o $OUTPUT_PATH

   HadoopMTi:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar IndexJoin -Dmapred.hashjoin.probing.table=$PATH_TO_UserVisits_INDEX -Duv.date.from=2000-01-15 -Duv.date.to=2000-01-22 -Dmapred.reduce.tasks=1 -Dio.sort.key.usefp=$SORT_KEY_USEFP -i $PATH_TO_Rankings_INDEX -o $OUTPUT_PATH

   HadoopBDBi:
     $HADOOP_HOME/bin/hadoop jar MRBenchmark.jar KVIndexJoin -Dmapred.hashjoin.probing.table=$PATH_TO_UserVisits_INDEX -Duv.date.from=2000-01-15 -Duv.date.to=2000-01-22 -Dmapred.reduce.tasks=1 -Dio.sort.key.usefp=$SORT_KEY_USEFP -i $PATH_TO_Rankings_INDEX -o $OUTPUT_PATH

=============================
4. How to compile source code
=============================
a) Compiling Hadoop:
   The source code is located in "hadoop-0.19.2-VLDB/src".
   Replace the corresponding files in original hadoop-0.19.2 (if the original file exists), and use "ant" command to generate "hadoop-0.19.3-dev-core.jar" files.

b) Compiling MapReduceBenchmark:
   We use Eclipse IDE to implement. The following compiling procedure is performed based on Eclipse:
   1) Import the project of MapReduceBenchmark
   2) Add the following jar files to Build Path:
      i. jar files in "lib" folder
      ii. $HADOOP_HOME/hadoop-0.19.3-dev-core.jar
      iii. jar files in "$HADOOP_HOME/lib"
   3) Export to a jar file. Note that in "JAR Manifest Specification" page, we select "sg.edu.nus.benchmark.AllBenchmarkDriver" as the main class.


=============
Known issues:
=============
The parser we use to decode the tuples from Rankings and UserVisits data requires a a vertical bar ("|") at the end of each record. However, the original data generator will generate data without a vertical bar at the end. As a result, if user uses the original generator, after generation he/she should manually add a vertical bar at the end of each line. Alternatively, user can modify Line 36 in TableGenerator.py:
  file.write( clrStr + "\n" )
However, if the generated data need to be loaded to PostgreSQL, the last vertical bar should be eliminated.