This repository has been archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
history.go
408 lines (342 loc) · 10.5 KB
/
history.go
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"path"
"strconv"
"strings"
"time"
"github.com/colinmarc/hdfs"
)
var jhistHeader = []byte("Avro-Json")
type histEvent struct {
Type string `json:"type"`
Event json.RawMessage `json:"event"`
}
type jobSubmittedEvent struct {
Ev struct {
ID string `json:"jobid"`
Name string `json:"jobName"`
User string `json:"userName"`
} `json:"org.apache.hadoop.mapreduce.jobhistory.JobSubmitted"`
}
type jobInitedEvent struct {
Ev struct {
ID string `json:"jobid"`
LaunchTime int64 `json:"launchTime"`
TotalMaps int `json:"totalMaps"`
TotalReduces int `json:"totalReduces"`
} `json:"org.apache.hadoop.mapreduce.jobhistory.JobInited"`
}
type jobFinishedEvent struct {
Ev struct {
ID string `json:"jobid"`
FinishTime int64 `json:"finishTime"`
} `json:"org.apache.hadoop.mapreduce.jobhistory.JobFinished"`
}
type jobFailedEvent struct {
Ev struct {
ID string `json:"jobid"`
FinishTime int64 `json:"finishTime"`
Status string `json:"jobStatus"`
} `json:"org.apache.hadoop.mapreduce.jobhistory.JobUnsuccessfulCompletion"`
}
type attemptStartedEvent struct {
Ev attemptEvent `json:"org.apache.hadoop.mapreduce.jobhistory.TaskAttemptStarted"`
}
type mapFinishedEvent struct {
Ev attemptEvent `json:"org.apache.hadoop.mapreduce.jobhistory.MapAttemptFinished"`
}
type reduceFinishedEvent struct {
Ev attemptEvent `json:"org.apache.hadoop.mapreduce.jobhistory.ReduceAttemptFinished"`
}
type taskFailedEvent struct {
Ev attemptEvent `json:"org.apache.hadoop.mapreduce.jobhistory.TaskAttemptUnsuccessfulCompletion"`
}
type attemptEvent struct {
ID string `json:"attemptId"`
Type string `json:"taskType"`
StartTime int64 `json:"startTime"`
FinishTime int64 `json:"finishTime"`
Error string `json:"error"`
Hostname string `json:"hostname"`
Status string `json:"status"`
Counters struct {
Groups []struct {
Name string `json:"name"`
Counts []struct {
Name string `json:"name"`
Value int `json:"value"`
}
} `json:"groups"`
} `json:"counters"`
}
// HdfsJobHistoryClient fetches job history from HDFS
type HdfsJobHistoryClient interface {
updateFromHistoryFile(jt *jobTracker, job *job, full bool) error
}
type hdfsJobHistoryClient struct{}
type jhistParser struct {
job *job
full bool
scanner *bufio.Scanner
attempts map[string]attemptEvent
}
// loadHistFile streams through the jhist file represented by r, and updates
// the given job's details.
func loadHistFile(r io.Reader, job *job, full bool) error {
scanner := bufio.NewScanner(r)
scanner.Scan()
if scanner.Err() != nil {
return scanner.Err()
}
if !bytes.Equal(scanner.Bytes(), jhistHeader) {
return errors.New("invalid Avro-Json header")
}
parser := &jhistParser{
job: job,
full: full,
scanner: scanner,
attempts: make(map[string]attemptEvent),
}
return parser.parse()
}
func (jp *jhistParser) parse() error {
// Reset these so we can count the events.
jp.job.Details.MapsCompleted = 0
jp.job.Details.MapsFailed = 0
jp.job.Details.MapsKilled = 0
jp.job.Details.ReducesCompleted = 0
jp.job.Details.ReducesFailed = 0
jp.job.Details.ReducesKilled = 0
lineNumber := 1
for jp.scanner.Scan() {
lineNumber++
line := bytes.TrimSpace(jp.scanner.Bytes())
if len(line) == 0 {
continue
}
wrapper := histEvent{}
err := json.Unmarshal(line, &wrapper)
if err != nil {
return fmt.Errorf("line %d: %s", lineNumber, err)
}
switch wrapper.Type {
case "JOB_SUBMITTED":
jp.parseJobSubmitted(wrapper.Event)
case "JOB_INITED":
jp.parseJobInited(wrapper.Event)
case "JOB_FINISHED":
jp.parseJobFinished(wrapper.Event)
case "JOB_FAILED":
jp.parseJobFailed(wrapper.Event)
case "MAP_ATTEMPT_STARTED":
jp.parseTaskStarted(wrapper.Event)
case "MAP_ATTEMPT_FINISHED":
jp.job.Details.MapsCompleted++
jp.parseMapFinished(wrapper.Event)
case "MAP_ATTEMPT_FAILED":
jp.job.Details.MapsFailed++
jp.parseTaskFailed(wrapper.Event)
case "MAP_ATTEMPT_KILLED":
jp.job.Details.MapsKilled++
jp.parseTaskFailed(wrapper.Event)
case "REDUCE_ATTEMPT_STARTED":
jp.parseTaskStarted(wrapper.Event)
case "REDUCE_ATTEMPT_FINISHED":
jp.job.Details.ReducesCompleted++
jp.parseReduceFinished(wrapper.Event)
case "REDUCE_ATTEMPT_FAILED":
jp.job.Details.ReducesFailed++
jp.parseTaskFailed(wrapper.Event)
case "REDUCE_ATTEMPT_KILLED":
jp.job.Details.ReducesKilled++
jp.parseTaskFailed(wrapper.Event)
}
lineNumber++
}
if jp.scanner.Err() != nil {
return jp.scanner.Err()
}
if !jp.full {
return nil
}
// Consolidate tasks and counters. We kinda lazily combine the attempts into
// tasks here when trimTasks runs over them. We can't just look at the task
// events, because the historyserver does the same misdirection - it sets
// startTime for the task to the startTime of the first attempt, for example.
tasks := tasks{
Map: make([][]int64, 0),
Reduce: make([][]int64, 0),
Errors: make(map[string][]taskAttempt),
}
counters := make(map[string]counter)
for _, attempt := range jp.attempts {
// Save the task times.
if attempt.Type == "MAP" {
tasks.Map = append(tasks.Map, []int64{attempt.StartTime, attempt.FinishTime})
} else if attempt.Type == "REDUCE" {
tasks.Reduce = append(tasks.Reduce, []int64{attempt.StartTime, attempt.FinishTime})
}
if attempt.Status == "FAILED" && attempt.Error != "" {
if _, exists := tasks.Errors[attempt.Error]; !exists {
tasks.Errors[attempt.Error] = make([]taskAttempt, 0)
}
tasks.Errors[attempt.Error] = append(tasks.Errors[attempt.Error], taskAttempt{
ID: attempt.ID,
Hostname: attempt.Hostname,
Type: attempt.Type,
})
}
// Update any counters from the attempt.
for _, group := range attempt.Counters.Groups {
groupName := group.Name[strings.LastIndex(group.Name, ".")+1:]
for _, count := range group.Counts {
counterName := fmt.Sprintf("%s.%s", groupName, count.Name)
counter := counters[counterName]
counter.Name = counterName
counter.Total += count.Value
if attempt.Type == "MAP" {
counter.Map += count.Value
} else if attempt.Type == "REDUCE" {
counter.Reduce += count.Value
}
counters[counterName] = counter
}
}
}
jp.job.Details.MapsTotalTime = sumTimes(tasks.Map)
jp.job.Details.ReducesTotalTime = sumTimes(tasks.Reduce)
jp.job.Tasks.Map = trimTasks(tasks.Map)
jp.job.Tasks.Reduce = trimTasks(tasks.Reduce)
jp.job.Tasks.Errors = tasks.Errors
for _, counter := range counters {
jp.job.Counters = append(jp.job.Counters, counter)
}
return nil
}
func (jp *jhistParser) parseJobSubmitted(b []byte) {
ev := jobSubmittedEvent{}
json.Unmarshal(b, &ev)
jp.job.Details.ID = ev.Ev.ID
jp.job.Details.Name = ev.Ev.Name
jp.job.Details.User = ev.Ev.User
}
func (jp *jhistParser) parseJobInited(b []byte) {
ev := jobInitedEvent{}
json.Unmarshal(b, &ev)
jp.job.Details.ID = ev.Ev.ID
jp.job.Details.StartTime = ev.Ev.LaunchTime
jp.job.Details.MapsTotal = ev.Ev.TotalMaps
jp.job.Details.ReducesTotal = ev.Ev.TotalReduces
}
func (jp *jhistParser) parseJobFinished(b []byte) {
ev := jobFinishedEvent{}
json.Unmarshal(b, &ev)
jp.job.Details.ID = ev.Ev.ID
jp.job.Details.FinishTime = ev.Ev.FinishTime
jp.job.Details.State = "SUCCEEDED"
}
func (jp *jhistParser) parseJobFailed(b []byte) {
ev := jobFailedEvent{}
json.Unmarshal(b, &ev)
jp.job.Details.ID = ev.Ev.ID
jp.job.Details.FinishTime = ev.Ev.FinishTime
jp.job.Details.State = ev.Ev.Status
}
func (jp *jhistParser) parseTaskStarted(b []byte) {
ev := attemptStartedEvent{}
json.Unmarshal(b, &ev)
jp.attempts[ev.Ev.ID] = ev.Ev
}
func (jp *jhistParser) parseMapFinished(b []byte) {
ev := mapFinishedEvent{}
json.Unmarshal(b, &ev)
startTime := jp.attempts[ev.Ev.ID].StartTime
ev.Ev.StartTime = startTime
jp.attempts[ev.Ev.ID] = ev.Ev
}
func (jp *jhistParser) parseReduceFinished(b []byte) {
ev := reduceFinishedEvent{}
json.Unmarshal(b, &ev)
startTime := jp.attempts[ev.Ev.ID].StartTime
ev.Ev.StartTime = startTime
jp.attempts[ev.Ev.ID] = ev.Ev
}
func (jp *jhistParser) parseTaskFailed(b []byte) {
ev := taskFailedEvent{}
json.Unmarshal(b, &ev)
startTime := jp.attempts[ev.Ev.ID].StartTime
ev.Ev.StartTime = startTime
jp.attempts[ev.Ev.ID] = ev.Ev
}
// findHistoryAndConfFiles locates and returns the .jhist and _conf.xml files for the given
// job.
func findHistoryAndConfFiles(client *hdfs.Client, jobID jobID, finishTime int64) (string, string, error) {
parts := strings.Split(string(jobID), "_")
sort, _ := strconv.ParseInt(parts[len(parts)-1], 10, 0)
t := time.Unix(finishTime/1000, 0)
histPath := fmt.Sprintf("%s/%04d/%02d/%02d/%06d",
*yarnHistoryDir, t.Year(), t.Month(), t.Day(), sort/1000)
infos, err := client.ReadDir(histPath)
if err != nil {
return "", "", err
}
var confFile, histFile string
for _, info := range infos {
if strings.HasPrefix(info.Name(), string(jobID)) {
p := path.Join(histPath, info.Name())
if strings.HasSuffix(p, "conf.xml") {
confFile = p
} else if strings.HasSuffix(p, ".jhist") {
histFile = p
}
if confFile != "" && histFile != "" {
return confFile, histFile, nil
}
}
}
return "", "", fmt.Errorf("no matching files found at %s", histPath)
}
// updateFromHistoryFile updates a job's details by loading its saved 'jhist'
// file stored in hdfs, along with the stored jobconf xml file.
func (jc *hdfsJobHistoryClient) updateFromHistoryFile(jt *jobTracker, job *job, full bool) error {
now := time.Now()
client, err := hdfs.New(jt.jobClient.getNamenodeAddress())
if err != nil {
return err
}
defer client.Close()
_, jobID := hadoopIDs(job.Details.ID)
confFile, histFile, err := findHistoryAndConfFiles(client, jobID, job.Details.FinishTime)
if err != nil {
return fmt.Errorf("couldn't find history file for %s in cluster %s: %s", jobID, jt.clusterName, err)
}
histFileReader, err := client.Open(histFile)
if err != nil {
return fmt.Errorf("couldn't open history file at %s: %s", histFile, err)
}
err = loadHistFile(histFileReader, job, full)
if err != nil {
return fmt.Errorf("couldn't read history file at %s: %s", histFile, err)
}
confFileReader, err := client.Open(confFile)
if err != nil {
return fmt.Errorf("couldn't open jobconf at %s: %s", confFile, err)
}
conf, err := loadConf(confFileReader)
if err != nil {
return fmt.Errorf("couldn't read jobconf at %s: %s", confFile, err)
}
log.Println("Read jobConf and history file for", jobID, "in", time.Now().Sub(now))
job.conf.update(conf)
if full {
job.partial = false
}
return nil
}