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.hadoop.mapreduce.task.reduce;
19 
20 import java.io.InputStream;
21 import java.io.IOException;
22 
23 import java.util.Comparator;
24 import java.util.concurrent.atomic.AtomicInteger;
25 
26 import org.apache.hadoop.classification.InterfaceAudience;
27 import org.apache.hadoop.classification.InterfaceStability;
28 
29 import org.apache.hadoop.mapred.Reporter;
30 
31 import org.apache.hadoop.mapreduce.TaskAttemptID;
32 
33 @InterfaceAudience.LimitedPrivate({"MapReduce"})
34 @InterfaceStability.Unstable
35 public abstract class MapOutput<K, V> {
36   private static AtomicInteger ID = new AtomicInteger(0);
37 
38   private final int id;
39   private final TaskAttemptID mapId;
40   private final long size;
41   private final boolean primaryMapOutput;
42 
MapOutput(TaskAttemptID mapId, long size, boolean primaryMapOutput)43   public MapOutput(TaskAttemptID mapId, long size, boolean primaryMapOutput) {
44     this.id = ID.incrementAndGet();
45     this.mapId = mapId;
46     this.size = size;
47     this.primaryMapOutput = primaryMapOutput;
48   }
49 
isPrimaryMapOutput()50   public boolean isPrimaryMapOutput() {
51     return primaryMapOutput;
52   }
53 
54   @Override
equals(Object obj)55   public boolean equals(Object obj) {
56     if (obj instanceof MapOutput) {
57       return id == ((MapOutput)obj).id;
58     }
59     return false;
60   }
61 
62   @Override
hashCode()63   public int hashCode() {
64     return id;
65   }
66 
getMapId()67   public TaskAttemptID getMapId() {
68     return mapId;
69   }
70 
getSize()71   public long getSize() {
72     return size;
73   }
74 
shuffle(MapHost host, InputStream input, long compressedLength, long decompressedLength, ShuffleClientMetrics metrics, Reporter reporter)75   public abstract void shuffle(MapHost host, InputStream input,
76                                long compressedLength,
77                                long decompressedLength,
78                                ShuffleClientMetrics metrics,
79                                Reporter reporter) throws IOException;
80 
commit()81   public abstract void commit() throws IOException;
82 
abort()83   public abstract void abort();
84 
getDescription()85   public abstract String getDescription();
86 
toString()87   public String toString() {
88     return "MapOutput(" + mapId + ", " + getDescription() + ")";
89   }
90 
91   public static class MapOutputComparator<K, V>
92   implements Comparator<MapOutput<K, V>> {
compare(MapOutput<K, V> o1, MapOutput<K, V> o2)93     public int compare(MapOutput<K, V> o1, MapOutput<K, V> o2) {
94       if (o1.id == o2.id) {
95         return 0;
96       }
97 
98       if (o1.size < o2.size) {
99         return -1;
100       } else if (o1.size > o2.size) {
101         return 1;
102       }
103 
104       if (o1.id < o2.id) {
105         return -1;
106       } else {
107         return 1;
108 
109       }
110     }
111   }
112 
113 }
114