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.util.ArrayList;
21 import java.util.List;
22 
23 import org.apache.hadoop.classification.InterfaceAudience;
24 import org.apache.hadoop.classification.InterfaceStability;
25 
26 import org.apache.hadoop.mapreduce.TaskAttemptID;
27 
28 @InterfaceAudience.LimitedPrivate({"MapReduce"})
29 @InterfaceStability.Unstable
30 public class MapHost {
31 
32   public static enum State {
33     IDLE,               // No map outputs available
34     BUSY,               // Map outputs are being fetched
35     PENDING,            // Known map outputs which need to be fetched
36     PENALIZED           // Host penalized due to shuffle failures
37   }
38 
39   private State state = State.IDLE;
40   private final String hostName;
41   private final String baseUrl;
42   private List<TaskAttemptID> maps = new ArrayList<TaskAttemptID>();
43 
MapHost(String hostName, String baseUrl)44   public MapHost(String hostName, String baseUrl) {
45     this.hostName = hostName;
46     this.baseUrl = baseUrl;
47   }
48 
getState()49   public State getState() {
50     return state;
51   }
52 
getHostName()53   public String getHostName() {
54     return hostName;
55   }
56 
getBaseUrl()57   public String getBaseUrl() {
58     return baseUrl;
59   }
60 
addKnownMap(TaskAttemptID mapId)61   public synchronized void addKnownMap(TaskAttemptID mapId) {
62     maps.add(mapId);
63     if (state == State.IDLE) {
64       state = State.PENDING;
65     }
66   }
67 
getAndClearKnownMaps()68   public synchronized List<TaskAttemptID> getAndClearKnownMaps() {
69     List<TaskAttemptID> currentKnownMaps = maps;
70     maps = new ArrayList<TaskAttemptID>();
71     return currentKnownMaps;
72   }
73 
markBusy()74   public synchronized void markBusy() {
75     state = State.BUSY;
76   }
77 
markPenalized()78   public synchronized void markPenalized() {
79     state = State.PENALIZED;
80   }
81 
getNumKnownMapOutputs()82   public synchronized int getNumKnownMapOutputs() {
83     return maps.size();
84   }
85 
86   /**
87    * Called when the node is done with its penalty or done copying.
88    * @return the host's new state
89    */
markAvailable()90   public synchronized State markAvailable() {
91     if (maps.isEmpty()) {
92       state = State.IDLE;
93     } else {
94       state = State.PENDING;
95     }
96     return state;
97   }
98 
99   @Override
toString()100   public String toString() {
101     return hostName;
102   }
103 
104   /**
105    * Mark the host as penalized
106    */
penalize()107   public synchronized void penalize() {
108     state = State.PENALIZED;
109   }
110 }
111