1 | #!/usr/bin/perl
|
---|
2 |
|
---|
3 | # Pragma
|
---|
4 | use strict;
|
---|
5 | use warnings;
|
---|
6 |
|
---|
7 | # Configuration
|
---|
8 | my $debug = 0;
|
---|
9 |
|
---|
10 | # Requires setup.bash to have been sourced
|
---|
11 | BEGIN
|
---|
12 | {
|
---|
13 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
14 | die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
|
---|
15 | die "HADOOP_PREFIX not set (set in <gsdl>/ext/parallel_processing/setup.bash)\n" unless defined $ENV{'HADOOP_PREFIX'};
|
---|
16 | die "HDFS HOST not set (set in <gsdl>/ext/parallel_processing/setup.bash)\n" unless defined $ENV{'HDFSHOST'};
|
---|
17 | die "HDFS PORT not set (set in <gsdl>/ext/parallel_processing/setup.bash)\n" unless defined $ENV{'HDFSPORT'};
|
---|
18 | # Ensure Greenstone Perl locations are in INC
|
---|
19 | unshift (@INC, $ENV{'GSDLHOME'} . '/perllib');
|
---|
20 | unshift (@INC, $ENV{'GSDLHOME'} . '/perllib/cpan');
|
---|
21 | # we'll need the perl version number
|
---|
22 | my ($version_number) = `perl-version.pl`;
|
---|
23 | if (defined $ENV{'GSDLEXTS'})
|
---|
24 | {
|
---|
25 | my @extensions = split(/:/,$ENV{'GSDLEXTS'});
|
---|
26 | foreach my $e (@extensions)
|
---|
27 | {
|
---|
28 | my $ext_prefix = $ENV{'GSDLHOME'} . '/ext/' . $e;
|
---|
29 | unshift (@INC, $ext_prefix . '/perllib');
|
---|
30 | unshift (@INC, $ext_prefix . '/perllib/cpan');
|
---|
31 | unshift (@INC, $ext_prefix . '/' . $ENV{'GSDLOS'} . '/lib/perl/' . $version_number);
|
---|
32 | }
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | # Libraries (depends on unshift above
|
---|
37 | use Sort::Key::Natural qw(natsort);
|
---|
38 |
|
---|
39 | # Begin
|
---|
40 | print "===== Parse Hadoop Log =====\n";
|
---|
41 |
|
---|
42 | # 0. Init
|
---|
43 | if (!defined $ARGV[0])
|
---|
44 | {
|
---|
45 | die("usage: parse_task_info_from_hadoop_log.pl <results dir>\n");
|
---|
46 | }
|
---|
47 | my $results_dir = $ARGV[0];
|
---|
48 | if (!-d $results_dir)
|
---|
49 | {
|
---|
50 | die("Error! Can't find results directory: " . $results_dir . "\n");
|
---|
51 | }
|
---|
52 | print " Results directory: " . $results_dir . "\n";
|
---|
53 |
|
---|
54 | my $hadoop_log_path = &fileCat($results_dir, 'hadoop.log');
|
---|
55 | if (!-e $hadoop_log_path)
|
---|
56 | {
|
---|
57 | die("Error! Hadoop log file cannot be found: " . $hadoop_log_path . "\n");
|
---|
58 | }
|
---|
59 | print " Hadoop log path: " . $hadoop_log_path . "\n";
|
---|
60 |
|
---|
61 | my $username = `whoami`;
|
---|
62 | chomp($username);
|
---|
63 | print " Username: " . $username . "\n";
|
---|
64 | my $hostname = `hostname`;
|
---|
65 | chomp($hostname);
|
---|
66 | print " Hostname: " . $hostname . "\n";
|
---|
67 | my $jobtracker_log_path = &fileCat($ENV{'HADOOP_PREFIX'}, 'logs', 'hadoop-' . $username . '-jobtracker-' . $hostname . '.log');
|
---|
68 | if (!-e $jobtracker_log_path)
|
---|
69 | {
|
---|
70 | die("Error! Hadoop JobTracker log file cannot be found: " . $jobtracker_log_path . "\n");
|
---|
71 | }
|
---|
72 | print " Jobtracker log path: " . $jobtracker_log_path . "\n";
|
---|
73 | my $data_locality_report_path = &fileCat($results_dir, 'data_locality.csv');
|
---|
74 | print " Report path: " . $data_locality_report_path . "\n";
|
---|
75 |
|
---|
76 | # 1. Determine job ID
|
---|
77 | print " * Determine JobID: ";
|
---|
78 | my $job_id;
|
---|
79 | my $result = `grep "Running job:" "$hadoop_log_path"`;
|
---|
80 | if ($result =~ /Running job: job_(\d+_\d+)/)
|
---|
81 | {
|
---|
82 | $job_id = $1;
|
---|
83 | }
|
---|
84 | else
|
---|
85 | {
|
---|
86 | die("Error! Failed to locate JobID\n");
|
---|
87 | }
|
---|
88 | print $job_id . "\n";
|
---|
89 |
|
---|
90 | # 2. Parse log
|
---|
91 | print " * Parse JobTracker Log... ";
|
---|
92 | my $tid_2_splits = {};
|
---|
93 | my $tid_2_node = {};
|
---|
94 | my $aid_2_node = {};
|
---|
95 | if (open(JTLIN, '<', $jobtracker_log_path))
|
---|
96 | {
|
---|
97 | my $line = '';
|
---|
98 | while ($line = <JTLIN>)
|
---|
99 | {
|
---|
100 | # Tips provide a match between task and file splits
|
---|
101 | if ($line =~ /tip:task_${job_id}(_m_\d+) has split on node:\/default-rack\/([^\.\r\n]+)/)
|
---|
102 | {
|
---|
103 | my $task_id = $job_id . $1;
|
---|
104 | my $compute_node = $2;
|
---|
105 | &debugPrint('found tip: ' . $task_id . ' => ' . $compute_node);
|
---|
106 | if (!defined $tid_2_splits->{$task_id})
|
---|
107 | {
|
---|
108 | $tid_2_splits->{$task_id} = [$compute_node];
|
---|
109 | }
|
---|
110 | else
|
---|
111 | {
|
---|
112 | push(@{$tid_2_splits->{$task_id}}, $compute_node);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | # JobTracker (MAP) entries give us a mapping between task, attempt, and
|
---|
116 | # compute node
|
---|
117 | if ($line =~ /Adding task \(MAP\) 'attempt_${job_id}(_m_\d+)(_\d+)'.*tracker_([^\.]+).local/)
|
---|
118 | {
|
---|
119 | my $task_id = $job_id . $1;
|
---|
120 | my $attempt_id = $job_id . $1 . $2;
|
---|
121 | my $compute_node = $3;
|
---|
122 | &debugPrint('found MAP: ' . $attempt_id . ' => ' . $compute_node);
|
---|
123 | $aid_2_node->{$attempt_id} = {'compute_node' => $compute_node,
|
---|
124 | 'succeeded' => 0
|
---|
125 | };
|
---|
126 | }
|
---|
127 | # Watch for attempt successes (so we can weed out failures)
|
---|
128 | if ($line =~ /Task 'attempt_${job_id}(_m_\d+_\d+)' has completed .* successfully/)
|
---|
129 | {
|
---|
130 | my $attempt_id = $job_id . $1;
|
---|
131 | &debugPrint('successful attempt: ' . $attempt_id);
|
---|
132 | if (defined $aid_2_node->{$attempt_id})
|
---|
133 | {
|
---|
134 | $aid_2_node->{$attempt_id}->{'succeeded'} = 1;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 | close(JTLIN);
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | die("Error! Failed to open JobTracker log for reading: " . $jobtracker_log_path . "\n");
|
---|
143 | }
|
---|
144 | print "Done\n";
|
---|
145 |
|
---|
146 |
|
---|
147 | # 3. Write CSV of information
|
---|
148 | print " * Writing Job Information... ";
|
---|
149 | &debugPrint("\nAttemptID\tComputeNode\tSucceeded");
|
---|
150 | foreach my $attempt_id (keys %{$aid_2_node})
|
---|
151 | {
|
---|
152 | &debugPrint($attempt_id . "\t" . $aid_2_node->{$attempt_id}->{'compute_node'} . "\t" . $aid_2_node->{$attempt_id}->{'succeeded'});
|
---|
153 | }
|
---|
154 | &debugPrint("TaskID\tComputeNodeSplits");
|
---|
155 | foreach my $task_id (keys %{$tid_2_splits})
|
---|
156 | {
|
---|
157 | &debugPrint($task_id . "\t" . join(',', natsort(@{$tid_2_splits->{$task_id}})));
|
---|
158 | }
|
---|
159 |
|
---|
160 | # - open the CSV file and write out the combined information from above
|
---|
161 | if (open(CSVOUT, '>:utf8', $data_locality_report_path))
|
---|
162 | {
|
---|
163 | print CSVOUT "TaskNo,AttemptNo,Data Local,Compute Node,Splits\n";
|
---|
164 | foreach my $attempt_id (natsort(keys %{$aid_2_node}))
|
---|
165 | {
|
---|
166 | my ($job_id, $task_number, $attempt_number) = $attempt_id =~ /^(\d+_\d+)_m_(\d+)_(\d+)/;
|
---|
167 | my $task_id = $job_id . '_m_' . $task_number;
|
---|
168 | my $compute_node = $aid_2_node->{$attempt_id}->{'compute_node'};
|
---|
169 | my @splits = @{$tid_2_splits->{$task_id}};
|
---|
170 | my $data_local = 0;
|
---|
171 | if (grep($_ eq $compute_node, @splits))
|
---|
172 | {
|
---|
173 | $data_local = 1;
|
---|
174 | }
|
---|
175 | print CSVOUT $task_number . "," . $attempt_number . "," . $data_local . ",\"" . $compute_node . "\",\"" . join(',', natsort(@splits)) . "\"\n";
|
---|
176 | }
|
---|
177 | close(CSVOUT);
|
---|
178 | }
|
---|
179 | else
|
---|
180 | {
|
---|
181 | die("Error! Failed to open file for writing: " . $data_locality_report_path);
|
---|
182 | }
|
---|
183 | print "Done\n";
|
---|
184 |
|
---|
185 | # 4. Done
|
---|
186 | print "===== Complete! =====\n\n";
|
---|
187 | exit;
|
---|
188 |
|
---|
189 | # Subs
|
---|
190 |
|
---|
191 | sub debugPrint
|
---|
192 | {
|
---|
193 | my ($msg) = @_;
|
---|
194 | if ($debug)
|
---|
195 | {
|
---|
196 | print '[debug] ' . $msg . "\n";
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | sub fileCat
|
---|
201 | {
|
---|
202 | my $path = join('/', @_);
|
---|
203 | $path =~ s/\/\/+/\//g;
|
---|
204 | return $path;
|
---|
205 | }
|
---|