1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.graph.partition; |
20 | |
|
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Collection; |
23 | |
import java.util.Collections; |
24 | |
import java.util.Comparator; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
import java.util.PriorityQueue; |
29 | |
|
30 | |
import org.apache.giraph.graph.WorkerInfo; |
31 | |
import org.apache.hadoop.conf.Configuration; |
32 | |
import org.apache.log4j.Logger; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 8 | public class PartitionBalancer { |
38 | |
|
39 | |
public static final String PARTITION_BALANCE_ALGORITHM = |
40 | |
"hash.partitionBalanceAlgorithm"; |
41 | |
|
42 | |
public static final String STATIC_BALANCE_ALGORITHM = |
43 | |
"static"; |
44 | |
|
45 | |
public static final String EGDE_BALANCE_ALGORITHM = |
46 | |
"edges"; |
47 | |
|
48 | |
public static final String VERTICES_BALANCE_ALGORITHM = |
49 | |
"vertices"; |
50 | |
|
51 | 1 | private static Logger LOG = Logger.getLogger(PartitionBalancer.class); |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 5 | private enum BalanceValue { |
57 | |
|
58 | 1 | UNSET, |
59 | |
|
60 | 1 | EDGES, |
61 | |
|
62 | 1 | VERTICES |
63 | |
} |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 0 | private PartitionBalancer() { } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private static long getBalanceValue(PartitionStats partitionStat, |
78 | |
BalanceValue balanceValue) { |
79 | 1 | switch (balanceValue) { |
80 | |
case EDGES: |
81 | 0 | return partitionStat.getEdgeCount(); |
82 | |
case VERTICES: |
83 | 8 | return partitionStat.getVertexCount(); |
84 | |
default: |
85 | 0 | throw new IllegalArgumentException( |
86 | |
"getBalanceValue: Illegal balance value " + balanceValue); |
87 | |
} |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | 0 | private static class PartitionOwnerComparator implements |
94 | |
Comparator<PartitionOwner> { |
95 | |
|
96 | |
private final Map<PartitionOwner, PartitionStats> ownerStatMap; |
97 | |
|
98 | |
private final BalanceValue balanceValue; |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public PartitionOwnerComparator( |
108 | |
Map<PartitionOwner, PartitionStats> ownerStatMap, |
109 | 8 | BalanceValue balanceValue) { |
110 | 8 | this.ownerStatMap = ownerStatMap; |
111 | 8 | this.balanceValue = balanceValue; |
112 | 8 | } |
113 | |
|
114 | |
@Override |
115 | |
public int compare(PartitionOwner owner1, PartitionOwner owner2) { |
116 | 0 | return (int) |
117 | |
(getBalanceValue(ownerStatMap.get(owner1), balanceValue) - |
118 | |
getBalanceValue(ownerStatMap.get(owner2), balanceValue)); |
119 | |
} |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | 0 | private static class WorkerInfoAssignments implements |
127 | |
Comparable<WorkerInfoAssignments> { |
128 | |
|
129 | |
private final WorkerInfo workerInfo; |
130 | |
|
131 | |
private final BalanceValue balanceValue; |
132 | |
|
133 | |
private final Map<PartitionOwner, PartitionStats> ownerStatsMap; |
134 | |
|
135 | 8 | private long value = 0; |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public WorkerInfoAssignments( |
145 | |
WorkerInfo workerInfo, |
146 | |
BalanceValue balanceValue, |
147 | 8 | Map<PartitionOwner, PartitionStats> ownerStatsMap) { |
148 | 8 | this.workerInfo = workerInfo; |
149 | 8 | this.balanceValue = balanceValue; |
150 | 8 | this.ownerStatsMap = ownerStatsMap; |
151 | 8 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public long getValue() { |
159 | 0 | return value; |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public void assignPartitionOwner( |
168 | |
PartitionOwner partitionOwner) { |
169 | 8 | value += getBalanceValue(ownerStatsMap.get(partitionOwner), |
170 | |
balanceValue); |
171 | 8 | if (!partitionOwner.getWorkerInfo().equals(workerInfo)) { |
172 | 0 | partitionOwner.setPreviousWorkerInfo( |
173 | |
partitionOwner.getWorkerInfo()); |
174 | 0 | partitionOwner.setWorkerInfo(workerInfo); |
175 | |
} else { |
176 | 8 | partitionOwner.setPreviousWorkerInfo(null); |
177 | |
} |
178 | 8 | } |
179 | |
|
180 | |
@Override |
181 | |
public int compareTo(WorkerInfoAssignments other) { |
182 | 0 | return (int) |
183 | |
(getValue() - ((WorkerInfoAssignments) other).getValue()); |
184 | |
} |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
public static Collection<PartitionOwner> balancePartitionsAcrossWorkers( |
197 | |
Configuration conf, |
198 | |
Collection<PartitionOwner> partitionOwners, |
199 | |
Collection<PartitionStats> allPartitionStats, |
200 | |
Collection<WorkerInfo> availableWorkerInfos) { |
201 | |
|
202 | 178 | String balanceAlgorithm = |
203 | |
conf.get(PARTITION_BALANCE_ALGORITHM, STATIC_BALANCE_ALGORITHM); |
204 | 178 | if (LOG.isInfoEnabled()) { |
205 | 178 | LOG.info("balancePartitionsAcrossWorkers: Using algorithm " + |
206 | |
balanceAlgorithm); |
207 | |
} |
208 | 178 | BalanceValue balanceValue = BalanceValue.UNSET; |
209 | 178 | if (balanceAlgorithm.equals(STATIC_BALANCE_ALGORITHM)) { |
210 | 170 | return partitionOwners; |
211 | 8 | } else if (balanceAlgorithm.equals(EGDE_BALANCE_ALGORITHM)) { |
212 | 0 | balanceValue = BalanceValue.EDGES; |
213 | 8 | } else if (balanceAlgorithm.equals(VERTICES_BALANCE_ALGORITHM)) { |
214 | 8 | balanceValue = BalanceValue.VERTICES; |
215 | |
} else { |
216 | 0 | throw new IllegalArgumentException( |
217 | |
"balancePartitionsAcrossWorkers: Illegal balance " + |
218 | |
"algorithm - " + balanceAlgorithm); |
219 | |
} |
220 | |
|
221 | |
|
222 | 8 | Map<Integer, PartitionStats> idStatMap = |
223 | |
new HashMap<Integer, PartitionStats>(); |
224 | 8 | for (PartitionStats partitionStats : allPartitionStats) { |
225 | 8 | if (idStatMap.put(partitionStats.getPartitionId(), partitionStats) != |
226 | |
null) { |
227 | 0 | throw new IllegalStateException( |
228 | |
"balancePartitionsAcrossWorkers: Duplicate partition id " + |
229 | |
"for " + partitionStats); |
230 | |
} |
231 | |
} |
232 | 8 | Map<PartitionOwner, PartitionStats> ownerStatsMap = |
233 | |
new HashMap<PartitionOwner, PartitionStats>(); |
234 | 8 | for (PartitionOwner partitionOwner : partitionOwners) { |
235 | 8 | PartitionStats partitionStats = |
236 | |
idStatMap.get(partitionOwner.getPartitionId()); |
237 | 8 | if (partitionStats == null) { |
238 | 0 | throw new IllegalStateException( |
239 | |
"balancePartitionsAcrossWorkers: Missing partition " + |
240 | |
"stats for " + partitionOwner); |
241 | |
} |
242 | 8 | if (ownerStatsMap.put(partitionOwner, partitionStats) != null) { |
243 | 0 | throw new IllegalStateException( |
244 | |
"balancePartitionsAcrossWorkers: Duplicate partition " + |
245 | |
"owner " + partitionOwner); |
246 | |
} |
247 | 8 | } |
248 | 8 | if (ownerStatsMap.size() != partitionOwners.size()) { |
249 | 0 | throw new IllegalStateException( |
250 | |
"balancePartitionsAcrossWorkers: ownerStats count = " + |
251 | |
ownerStatsMap.size() + ", partitionOwners count = " + |
252 | |
partitionOwners.size() + " and should match."); |
253 | |
} |
254 | |
|
255 | 8 | List<WorkerInfoAssignments> workerInfoAssignmentsList = |
256 | |
new ArrayList<WorkerInfoAssignments>(availableWorkerInfos.size()); |
257 | 8 | for (WorkerInfo workerInfo : availableWorkerInfos) { |
258 | 8 | workerInfoAssignmentsList.add( |
259 | |
new WorkerInfoAssignments( |
260 | |
workerInfo, balanceValue, ownerStatsMap)); |
261 | |
} |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | 8 | List<PartitionOwner> partitionOwnerList = |
273 | |
new ArrayList<PartitionOwner>(partitionOwners); |
274 | 8 | Collections.sort(partitionOwnerList, |
275 | |
Collections.reverseOrder( |
276 | |
new PartitionOwnerComparator(ownerStatsMap, balanceValue))); |
277 | 8 | PriorityQueue<WorkerInfoAssignments> minQueue = |
278 | |
new PriorityQueue<WorkerInfoAssignments>(workerInfoAssignmentsList); |
279 | 8 | for (PartitionOwner partitionOwner : partitionOwnerList) { |
280 | 8 | WorkerInfoAssignments chosenWorker = minQueue.remove(); |
281 | 8 | chosenWorker.assignPartitionOwner(partitionOwner); |
282 | 8 | minQueue.add(chosenWorker); |
283 | 8 | } |
284 | |
|
285 | 8 | return partitionOwnerList; |
286 | |
} |
287 | |
} |
288 | |
|