This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
/
BlockMasterLogic.java
293 lines (268 loc) · 10.5 KB
/
BlockMasterLogic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.giraph.block_app.framework.internal;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.apache.commons.lang3.time.DurationFormatUtils;
import org.apache.giraph.block_app.framework.BlockFactory;
import org.apache.giraph.block_app.framework.BlockUtils;
import org.apache.giraph.block_app.framework.api.BlockApiHandle;
import org.apache.giraph.block_app.framework.api.BlockMasterApi;
import org.apache.giraph.block_app.framework.api.BlockOutputHandleAccessor;
import org.apache.giraph.block_app.framework.block.Block;
import org.apache.giraph.block_app.framework.block.BlockWithApiHandle;
import org.apache.giraph.block_app.framework.piece.AbstractPiece;
import org.apache.giraph.conf.GiraphConfiguration;
import org.apache.giraph.function.Consumer;
import org.apache.giraph.writable.tuple.IntLongWritable;
import org.apache.log4j.Logger;
import com.google.common.base.Preconditions;
/**
* Block execution logic on master, iterating over Pieces of the
* application Block, executing master logic, and providing what needs to be
* executed on the workers.
*
* @param <S> Execution stage type
*/
@SuppressWarnings("rawtypes")
public class BlockMasterLogic<S> {
private static final Logger LOG = Logger.getLogger(BlockMasterLogic.class);
private Iterator<AbstractPiece> pieceIterator;
private PairedPieceAndStage<S> previousPiece;
private transient BlockMasterApi masterApi;
private long lastTimestamp = -1;
private BlockWorkerPieces previousWorkerPieces;
private boolean computationDone;
private BlockApiHandle blockApiHandle;
/** Tracks elapsed time on master for each distinct Piece */
private final TimeStatsPerEvent masterPerPieceTimeStats =
new TimeStatsPerEvent("master");
/** Tracks elapsed time on workers for each pair of recieve/send pieces. */
private final TimeStatsPerEvent workerPerPieceTimeStats =
new TimeStatsPerEvent("worker");
/**
* Initialize master logic to execute BlockFactory defined in
* the configuration.
*/
public void initialize(
GiraphConfiguration conf, final BlockMasterApi masterApi) {
BlockFactory<S> factory = BlockUtils.createBlockFactory(conf);
initialize(factory.createBlock(conf), factory.createExecutionStage(conf),
masterApi);
}
/**
* Initialize Master Logic to execute given block, starting
* with given executionStage.
*/
public void initialize(
Block executionBlock, S executionStage, final BlockMasterApi masterApi) {
this.masterApi = masterApi;
this.computationDone = false;
LOG.info("Executing application - " + executionBlock);
if (executionBlock instanceof BlockWithApiHandle) {
blockApiHandle =
((BlockWithApiHandle) executionBlock).getBlockApiHandle();
}
if (blockApiHandle == null) {
blockApiHandle = new BlockApiHandle();
}
blockApiHandle.setMasterApi(masterApi);
// We register all possible aggregators at the beginning
executionBlock.forAllPossiblePieces(new Consumer<AbstractPiece>() {
private final HashSet<AbstractPiece> registeredPieces = new HashSet<>();
@SuppressWarnings("deprecation")
@Override
public void apply(AbstractPiece piece) {
// no need to register the same piece twice.
if (registeredPieces.add(piece)) {
try {
piece.registerAggregators(masterApi);
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
});
pieceIterator = executionBlock.iterator();
// Invariant is that ReceiveWorkerPiece of previousPiece has already been
// executed and that previousPiece.nextExecutionStage() should be used for
// iterating. So passing piece as null, and initial state as current state,
// so that nothing get's executed in first half, and calculateNextState
// returns initial state.
previousPiece = new PairedPieceAndStage<>(null, executionStage);
}
/**
* Initialize object after deserializing it.
* BlockMasterApi is not serializable, so it is transient, and set via this
* method afterwards.
*/
public void initializeAfterRead(BlockMasterApi masterApi) {
this.masterApi = masterApi;
}
/**
* Executes operations on master (master compute and registering reducers),
* and calculates next pieces to be exectued on workers.
*
* @param superstep Current superstep
* @return Next BlockWorkerPieces to be executed on workers, or null
* if computation should be halted.
*/
public BlockWorkerPieces<S> computeNext(long superstep) {
long beforeMaster = System.currentTimeMillis();
if (lastTimestamp != -1) {
BlockCounters.setWorkerTimeCounter(
previousWorkerPieces, superstep - 1,
beforeMaster - lastTimestamp, masterApi, workerPerPieceTimeStats);
}
if (previousPiece == null) {
postApplication();
return null;
} else {
boolean logExecutionStatus =
BlockUtils.LOG_EXECUTION_STATUS.get(masterApi.getConf());
if (logExecutionStatus) {
LOG.info("Master executing " + previousPiece +
", in superstep " + superstep);
}
previousPiece.masterCompute(masterApi);
((BlockOutputHandleAccessor) masterApi).getBlockOutputHandle().
returnAllWriters();
long afterMaster = System.currentTimeMillis();
if (previousPiece.getPiece() != null) {
BlockCounters.setMasterTimeCounter(
previousPiece, superstep, afterMaster - beforeMaster, masterApi,
masterPerPieceTimeStats);
}
PairedPieceAndStage<S> nextPiece;
if (pieceIterator.hasNext()) {
nextPiece = new PairedPieceAndStage<S>(
pieceIterator.next(), previousPiece.nextExecutionStage());
nextPiece.registerReducers(masterApi);
} else {
nextPiece = null;
}
BlockCounters.setStageCounters(
"Master finished stage: ", previousPiece.getExecutionStage(),
masterApi);
if (logExecutionStatus) {
LOG.info(
"Master passing next " + nextPiece + ", in superstep " + superstep);
}
// if there is nothing more to compute, no need for additional superstep
// this can only happen if application uses no pieces.
BlockWorkerPieces<S> result;
if (previousPiece.getPiece() == null && nextPiece == null) {
postApplication();
result = null;
} else {
result = new BlockWorkerPieces<>(
previousPiece, nextPiece, blockApiHandle);
if (logExecutionStatus) {
LOG.info("Master in " + superstep + " superstep passing " +
result + " to be executed");
}
}
previousPiece = nextPiece;
lastTimestamp = afterMaster;
previousWorkerPieces = result;
return result;
}
}
/**
* Clean up any master state, after application has finished.
*/
private void postApplication() {
((BlockOutputHandleAccessor) masterApi).getBlockOutputHandle().
closeAllWriters();
Preconditions.checkState(!computationDone);
computationDone = true;
IntLongWritable masterTimes = masterPerPieceTimeStats.logTimeSums();
IntLongWritable workerTimes = workerPerPieceTimeStats.logTimeSums();
LOG.info("Time split:\n" +
TimeStatsPerEvent.header() +
TimeStatsPerEvent.line(
masterTimes.getLeft().get(),
100.0 * masterTimes.getRight().get() /
(masterTimes.getRight().get() + workerTimes.getRight().get()),
masterTimes.getRight().get(),
"master") +
TimeStatsPerEvent.line(
workerTimes.getLeft().get(),
100.0 * workerTimes.getRight().get() /
(masterTimes.getRight().get() + workerTimes.getRight().get()),
workerTimes.getRight().get(),
"worker"));
}
/**
* Class tracking invocation count and elapsed time for a set of events,
* each event being having a String name.
*/
public static class TimeStatsPerEvent {
private final String groupName;
private final Map<String, IntLongWritable> keyToCountAndTime =
new TreeMap<>();
public TimeStatsPerEvent(String groupName) {
this.groupName = groupName;
}
public void inc(String name, long millis) {
IntLongWritable val = keyToCountAndTime.get(name);
if (val == null) {
val = new IntLongWritable();
keyToCountAndTime.put(name, val);
}
val.getLeft().set(val.getLeft().get() + 1);
val.getRight().set(val.getRight().get() + millis);
}
public IntLongWritable logTimeSums() {
StringBuilder sb = new StringBuilder("Time sums " + groupName + ":\n");
sb.append(header());
long total = 0;
int count = 0;
for (Entry<String, IntLongWritable> entry :
keyToCountAndTime.entrySet()) {
total += entry.getValue().getRight().get();
count += entry.getValue().getLeft().get();
}
for (Entry<String, IntLongWritable> entry :
keyToCountAndTime.entrySet()) {
sb.append(line(
entry.getValue().getLeft().get(),
(100.0 * entry.getValue().getRight().get()) / total,
entry.getValue().getRight().get(),
entry.getKey()));
}
LOG.info(sb);
return new IntLongWritable(count, total);
}
public static String header() {
return String.format(
"%10s%10s%11s %s%n", "count", "time %", "time", "name");
}
public static String line(
int count, double percTime, long time, String name) {
return String.format("%10d%9.2f%%%11s %s%n",
count,
percTime,
DurationFormatUtils.formatDuration(time, "HH:mm:ss"),
name);
}
}
}