forked from etsy/Sahale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlowTracker.scala
374 lines (310 loc) · 15.3 KB
/
FlowTracker.scala
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package com.etsy.sahale
import org.apache.commons.httpclient.{HttpClient, MultiThreadedHttpConnectionManager}
import org.apache.commons.httpclient.cookie.CookiePolicy
import org.apache.commons.httpclient.methods.{StringRequestEntity, PostMethod}
import org.apache.http.client.params.ClientPNames
import org.apache.log4j.Logger
import cascading.flow.Flow
import java.io.IOException
import java.util.concurrent.atomic.AtomicBoolean
import java.util.Properties
import scala.util.Try
import scala.collection.mutable
import scala.collection.JavaConversions._
import spray.json._
import DefaultJsonProtocol._
object FlowTracker {
type AggFunc = () => Any
val PROPSFILE = "flow-tracker.properties"
val NOT_LAUNCHED = "NOT_LAUNCHED"
val NOT_YARN_JOB = "false"
val UNKNOWN = "UNKNOWN"
val UPDATE_FLOW = "flow/update"
val UPDATE_STEPS = "steps/update"
val INSERT_AGG = "agg/update"
val CREATE_EDGES= "edges/update"
val REFRESH_INTERVAL_MS = 8 * 1000
val CheckIsCascadingFlowId = """([A-Fa-f0-9]+)""".r
val HTTP_CONNECTION_TIMEOUT = 5 * 60 * 1000;
val HTTP_SOCKET_TIMEOUT = 5 * 60 * 1000;
val LOG: Logger = Logger.getLogger(classOf[FlowTracker])
val props = getTrackerProperties
private var client: HttpClient = getHttpClient()
// master map of steps' hadoop counters to be aggregated and added to the flow data
private val StepCounterAggregators = Map[String, (String, String)](
"flow_hdfs_bytes_read" -> ("org.apache.hadoop.mapreduce.FileSystemCounter", "HDFS_BYTES_READ"),
"flow_file_bytes_read" -> ("org.apache.hadoop.mapreduce.FileSystemCounter", "FILE_BYTES_READ"),
"flow_gs_bytes_read" -> ("org.apache.hadoop.mapreduce.FileSystemCounter", "GS_BYTES_READ"),
"flow_hdfs_bytes_written" -> ("org.apache.hadoop.mapreduce.FileSystemCounter", "HDFS_BYTES_WRITTEN"),
"flow_file_bytes_written" -> ("org.apache.hadoop.mapreduce.FileSystemCounter", "FILE_BYTES_WRITTEN"),
"flow_gs_bytes_written" -> ("org.apache.hadoop.mapreduce.FileSystemCounter", "GS_BYTES_WRITTEN"),
"flow_total_map_tasks" -> ("org.apache.hadoop.mapreduce.JobCounter", "TOTAL_LAUNCHED_MAPS"),
"flow_total_reduce_tasks" -> ("org.apache.hadoop.mapreduce.JobCounter", "TOTAL_LAUNCHED_REDUCES"),
"flow_map_vcore_millis" -> ("org.apache.hadoop.mapreduce.JobCounter", "VCORES_MILLIS_MAPS"),
"flow_reduce_vcore_millis" -> ("org.apache.hadoop.mapreduce.JobCounter", "VCORES_MILLIS_REDUCES"),
"flow_shuffled_maps" -> ("org.apache.hadoop.mapreduce.TaskCounter", "SHUFFLED_MAPS"),
"flow_map_records_out" -> ("org.apache.hadoop.mapreduce.TaskCounter", "MAP_OUTPUT_RECORDS"),
"flow_reduce_records_in" -> ("org.apache.hadoop.mapreduce.TaskCounter", "REDUCE_INPUT_RECORDS"),
"flow_reduce_records_out" -> ("org.apache.hadoop.mapreduce.TaskCounter", "REDUCE_OUTPUT_RECORDS"),
"flow_map_bytes_out" -> ("org.apache.hadoop.mapreduce.TaskCounter", "MAP_OUTPUT_BYTES"),
"flow_merged_map_outputs" -> ("org.apache.hadoop.mapreduce.TaskCounter", "MERGED_MAP_OUTPUTS"),
"flow_reduce_shuffle_bytes" -> ("org.apache.hadoop.mapreduce.TaskCounter", "REDUCE_SHUFFLE_BYTES"),
"flow_cpu_millis" -> ("org.apache.hadoop.mapreduce.TaskCounter", "CPU_MILLISECONDS"),
"flow_gc_millis" -> ("org.apache.hadoop.mapreduce.TaskCounter", "GC_TIME_MILLIS"),
"flow_spilled_records" -> ("org.apache.hadoop.mapreduce.TaskCounter", "SPILLED_RECORDS"),
"flow_heap_committed_bytes" -> ("org.apache.hadoop.mapreduce.TaskCounter", "COMMITTED_HEAP_BYTES"),
"flow_virtual_mem_bytes" -> ("org.apache.hadoop.mapreduce.TaskCounter", "VIRTUAL_MEMORY_BYTES"),
"flow_physical_mem_bytes" -> ("org.apache.hadoop.mapreduce.TaskCounter", "PHYSICAL_MEMORY_BYTES"),
"flow_tuples_read" -> ("cascading.flow.StepCounters", "Tuples_Read"),
"flow_tuples_written" -> ("cascading.flow.StepCounters", "Tuples_Written")
// counter metrics set here will be aggregated and published in flow status updates
)
def getDefaultHostPort = props("host").trim + ":" + props("port").trim
private def getTrackerProperties: Properties = {
val props = new Properties();
props.load(Thread.currentThread.getContextClassLoader.getResourceAsStream(PROPSFILE))
props
}
def getHttpClient(
httpConnectionTimeout: Int = HTTP_CONNECTION_TIMEOUT,
httpSocketTimeout: Int = HTTP_SOCKET_TIMEOUT) = client match {
case hc: HttpClient if (null != hc) => hc
case _ =>
client = new HttpClient(new MultiThreadedHttpConnectionManager)
client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
client.getHttpConnectionManager().getParams().setConnectionTimeout(httpConnectionTimeout);
client.getHttpConnectionManager().getParams().setSoTimeout(httpSocketTimeout);
client
}
}
/**
* Poll running Cascading Flow for metrics and push to Sahale server configued in
* <code>src/main/resources/flow-tracker.properties</code>
*
* @author Eli Reisman
*/
class FlowTracker(val flow: Flow[_],
val runCompleted: AtomicBoolean,
val hostPort: String,
val disableProgressBar: Boolean,
val httpConnectionTimeout: Int = FlowTracker.HTTP_CONNECTION_TIMEOUT,
val httpSocketTimeout: Int = FlowTracker.HTTP_SOCKET_TIMEOUT) extends java.lang.Runnable {
import com.etsy.sahale.FlowTracker._
// mutable because we have to build this mapping as we go after run() is called
val stepStatusMap = mutable.Map[String, StepStatus]()
val edgeMap = mutable.Map[String, Set[Int]]()
// manages global job state for this run
val flowStatus = new FlowStatus(flow, FlowTracker.props)
// so that we can compose a chain of multiple strategies, end users might
// already have FlowStepStrategy implementations they need to apply later
flow.setFlowStepStrategy(
FlowStepStrategies.plus(flow.getFlowStepStrategy, new FlowTrackerStepStrategy(stepStatusMap))
)
// Store the server's host:port so that subclasses have access to it
val serverHostPort = if(hostPort.trim.isEmpty) getDefaultHostPort else hostPort.trim
// Constructors below are for easy Java interop with FlowTracker
def this(flow: Flow[_], runCompleted: AtomicBoolean, hostPort: String, httpConnectionTimeout: Int, httpSocketTimeout: Int) =
this(flow, runCompleted, hostPort, false, httpConnectionTimeout, httpSocketTimeout)
def this(flow: Flow[_], runCompleted: AtomicBoolean, hostPort: String, disableProgressBar: Boolean) =
this(flow, runCompleted, hostPort, disableProgressBar, FlowTracker.HTTP_CONNECTION_TIMEOUT, FlowTracker.HTTP_SOCKET_TIMEOUT)
def this(flow: Flow[_], runCompleted: AtomicBoolean, hostPort: String) =
this(flow, runCompleted, hostPort, false, FlowTracker.HTTP_CONNECTION_TIMEOUT, FlowTracker.HTTP_SOCKET_TIMEOUT)
def this(flow: Flow[_], runCompleted: AtomicBoolean) =
this(flow, runCompleted, "", false, FlowTracker.HTTP_CONNECTION_TIMEOUT, FlowTracker.HTTP_SOCKET_TIMEOUT)
override def run(): Unit = {
var isTrackable: Boolean = true
Try {
initializeTrackedJobState
}.recover {
case t: Throwable =>
LOG.warn("""
Failed to initialize FlowTracker. This is NOT a fatal error.
The job will run as normal, but it will not be tracked by Sahale.
""".stripMargin.trim, t)
runCompleted.set(true)
isTrackable = false
}
val maxFailures = 10
var numFailures = 0
while(!runCompleted.get && isTrackable) {
Try {
updateSteps
updateFlow
updateAggregates
if (!disableProgressBar) {
logFlowStatus
}
}.recover {
case t: Throwable if 1 + numFailures < maxFailures=>
LOG.warn(s"""
FlowTracker has thrown an exception because an update iteration
failed. This is NOT a fatal error. We will skip this update
iteration. ${maxFailures - numFailures} attempts remain.
""".stripMargin.trim, t)
numFailures += 1
case t: Throwable =>
LOG.warn("""
FlowTracker for this run has thrown an exception. This is NOT a
fatal error. The run will complete as normal, but the remainder
will not be tracked by Sahale.
""".stripMargin.trim, t)
runCompleted.set(true)
}
sleep(REFRESH_INTERVAL_MS)
}
// Push the final updates, but only if we have not hit our failure limit
if(numFailures < maxFailures && isTrackable) {
Try {
updateSteps
updateFlow
updateAggregates
}
}
}
def updateFlow: Unit = { flowStatus.update ; pushFlowReport }
// only update & push step data that's changed since last push
def updateSteps: Unit = pushStepReport(
stepStatusMap.foldLeft(Map.empty[String, StepStatus]) { (next, entry) =>
val old = entry._2.get[String]("step_status")
val current = entry._2.getStatus
(old, current) match {
case (o, c) if (c == "RUNNING" || o != c) => entry._2.update ; next + entry
case _ => next
}
}
)
def updateAggregates: Unit = {
import spray.json._
import DefaultJsonProtocol._
val agg = Map[String, Any](
"flow_id" -> flow.getID,
"epoch_ms" -> System.currentTimeMillis,
"agg_json" -> flowStatus.get[Map[String, Any]]("aggregated")
)
val json = com.etsy.sahale.JsonUtil.toJsonMap(agg).toJson.compactPrint
pushReport(sahaleUrl(INSERT_AGG), json)
}
/**
* Init our flow, edge, and stage maps since we can't until run() is called.
*/
def initializeTrackedJobState: Unit = {
new FlowGraphBuilder(flow, stepStatusMap, edgeMap)
flowStatus.registerAggregators(
// add the progress aggregator func
Map[String, AggFunc]("flow_progress" -> calculateFlowProgress) ++
// add all counter-based aggregator functions
StepCounterAggregators.map { case(k,v) => agg(k, v) }
.foldLeft(Map.empty[String, AggFunc]) { (acc, next) => acc ++ next }
)
pushEdgeReport // only sent once
pushStepReport(stepStatusMap.toMap) // first time send all, after just changeset
pushFlowReport // sent every update
updateAggregates // sent every update
println(Console.REVERSED + "Follow your running job's progress from your browser: " + sahaleUrl(s"flowgraph/${flow.getID}") + Console.RESET)
}
def sleep(millis: Long): Unit = try { Thread.sleep(millis) } catch { case _: Exception => }
/////////////////// Utility functions for aggregating Step data for Flow updates /////////////////
def sumMapProgress: (Double, Int) = stepStatusMap.values.foldLeft( (0.0, 0) ) {
(acc: (Double, Int), step: StepStatus) => (acc._1 + step.get[Double]("map_progress"), acc._2 + 1)
}
def sumReduceProgress: (Double, Int) = stepStatusMap.values.foldLeft( (0.0, 0) ) {
(acc: (Double, Int), step: StepStatus) => step.get[Boolean]("has_reduce_stage") match {
case true => (acc._1 + step.get[Double]("reduce_progress"), acc._2 + 1)
case _ => (acc._1, acc._2)
}
}
def calculateFlowProgress(): Any = {
val mapProg = sumMapProgress
val reduceProg = sumReduceProgress
val subtotal = ((mapProg._1 + reduceProg._1) / (mapProg._2 + reduceProg._2))
("%3.2f" format subtotal).toDouble
}
def agg(name: String, groupAndKey: (String, String)): Map[String, AggFunc] = Map[String, AggFunc](
name -> (() => stepStatusMap.values.foldLeft(0L) {
(sum: Long, step: StepStatus) => step.aggrFunc(groupAndKey._1, groupAndKey._2) + sum
})
)
/////////////////// Utiilty functions for pushing data to Sahale server /////////////////
def pushFlowReport: Int = pushReport(sahaleUrl(UPDATE_FLOW), flowStatus.send.compactPrint)
def pushEdgeReport: Int = pushReport(sahaleUrl(CREATE_EDGES), edgeMap.toMap.toJson.compactPrint)
def pushStepReport(steps: Map[String, StepStatus]): Int = steps.keys.size match {
case none: Int if (none == 0) =>
//LOG.info("No new FlowStep updates to push to Sahale")
none
case count =>
//LOG.info(s"Pushing $count FlowStep updates to Sahale")
pushReport(sahaleUrl(UPDATE_STEPS), steps.map { case(k, v) => v.send }.toJson.compactPrint)
}
def setAdditionalHeaders: Map[String, String] = {
// To be overridden by child classes, e.g. to set authentication headers
Map.empty
}
def pushReport(uri: String, json: String): Int = {
flow.getID match {
case CheckIsCascadingFlowId(id) => {
val url = uri + "/" + id + "?method=POST"
// LOG.info("REQUEST URL TO API:" + url)
val request = new PostMethod(url)
val entity = new StringRequestEntity(json, "application/json", "UTF-8")
try {
request.setRequestEntity(entity)
setAdditionalHeaders.foreach { case (headerName, headerValue) =>
request.addRequestHeader(headerName, headerValue)
}
val code = getHttpClient(httpConnectionTimeout, httpSocketTimeout).executeMethod(request)
code
} catch {
case e: IOException =>
logRequestResponse(url, request, json)
throw e
} finally {
if (null != request) {
try {
val asStream = request.getResponseBodyAsStream
if (asStream != null)
asStream.close()
} catch {
case e: IOException => LOG.warn("Could not close response stream for [" + url + "]", e)
}
request.releaseConnection
}
}
}
case _ => {
LOG.warn(s"Bad FlowID: ${flow.getID}")
-1 // error
}
}
}
def logRequestResponse(url: String, response: PostMethod, json: String): Unit = {
LOG.info(s"Sent JSON to $url:\n${json}")
if (response.getStatusLine != null) {
LOG.info(s"Response status code: ${response.getStatusCode}")
LOG.info(s"Response status line: ${response.getStatusLine}")
LOG.info(s"Response status text: ${response.getStatusText}")
} else {
LOG.info("No response status to debug")
}
LOG.info(s"Response x-error-detail: ${response.getResponseHeader("x-error-detail")}")
}
def sahaleUrl(suffix: String = ""): String = s"${serverHostPort}/${suffix}"
/////////////////// Utility functions for console progress bar /////////////////
def logFlowStatus: Unit = {
val flowProgress = flowStatus.getAggregate[Double]("flow_progress", 0.0)
val flowDuration = flowStatus.get[Long]("flow_duration")
val arrows = (20 * (flowProgress / 100.0)).toInt
val progressBar = Console.WHITE + "[" + Console.YELLOW + (">" * arrows) + Console.WHITE + (" " * (20 - arrows)) + "]"
val progressIndicator = "Job Status: " + getColoredFlowStatus + "\tProgress: " + ("%3.2f" format flowProgress) + "% "
print("\u000D" + (" " * 60));
print("\u000D" + Console.WHITE + progressIndicator + progressBar +
"\t Running " + (flowDuration / 1000L) + "secs" + Console.RESET + (" " * 10));
}
def getColoredFlowStatus: String = {
val statusColor = flow.getFlowStats.getStatus.toString match {
case "SUCCESSFUL" | "RUNNING" => Console.GREEN
case "FAILED" => Console.RED
case _ => Console.WHITE
}
statusColor + flow.getFlowStats.getStatus.toString + Console.WHITE
}
}