forked from omniti-labs/jlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jlog_sanity_check.pl
executable file
·286 lines (270 loc) · 8.53 KB
/
jlog_sanity_check.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use Fcntl qw/SEEK_CUR SEEK_SET O_RDONLY O_WRONLY/;
sub systell { sysseek($_[0], 0, SEEK_CUR) }
my $le = 1;
my $jlog = shift;
my $jlog_endian = shift;
if (defined $jlog_endian) {
if ($jlog_endian =~ /^(little|le)$/) {
$le = 1;
} elsif ($jlog_endian =~ /^(big|be)$/) {
$le = 0;
}
}
if (!defined $le or !defined $jlog) {
print "Usage: $0 <path to jlog> <jlog endianness (be or le)>\n";
exit 1;
}
sub unpack_32 {
if ($le) {
return unpack('V1', $_[0]);
} else {
return unpack('N1', $_[0]);
}
}
# we have to use 2 numbers to represent a 64-bit value so this works on 32-bit
sub unpack_64 {
if ($le) {
return unpack('V1', substr($_[0], 4, 4)), unpack('V1', substr($_[0], 0, 4));
} else {
return unpack('N1', substr($_[0], 0, 4)), unpack('N1', substr($_[0], 4, 4));
}
}
opendir(DIR, $jlog) or die "could not opendir $jlog: $!";
my $files = [ readdir(DIR) ];
closedir DIR;
my $metastore = grep /^metastore$/, @$files;
$files = [ grep !/^metastore$/, @$files ];
my $checkpoints = [ grep /^cp[.]/, @$files ];
$files = [ grep !/^cp[.]/, @$files ];
my $segments = [ sort { hex $a <=> hex $b } grep /^[0-9A-Fa-f]{8}$/, @$files ];
$files = [ grep !/^[0-9A-Fa-f]{8}$/, @$files ];
my $indexes = [ grep /^[0-9A-Fa-f]{8}.idx$/, @$files ];
$files = [ grep !/^[0-9A-Fa-f]{8}.idx$/, @$files ];
if (!$metastore) {
die "no metastore found\n";
}
$files = [ grep !/^[.]{1,2}$/, @$files ];
foreach (@$files) {
print "unexpected file found: $_ (skipping)\n";
}
undef $files;
if ((stat "$jlog/metastore")[7] != 16) {
die "metastore has invalid size\n";
}
my ($current_segment, $unit_limit, $safety, $hdr_magic);
sysopen(META, "$jlog/metastore", O_RDONLY)
or die "could not sysopen $jlog/metastore: $!";
my $data;
sysread(META, $data, 4) == 4 or die "metastore read error: $!";
$current_segment = unpack_32($data);
sysread(META, $data, 4) == 4 or die "metastore read error: $!";
$unit_limit = unpack_32($data);
sysread(META, $data, 4) == 4 or die "metastore read error: $!";
$safety = unpack_32($data);
sysread(META, $data, 4) == 4 or die "metastore read error: $!";
$hdr_magic = unpack_32($data);
close META;
my $oldest_cp_segment = 0xffffffff;
my $cpbyname = {};
foreach my $cp (@$checkpoints) {
my $cpname = $cp;
$cpname =~ s/^cp[.]//;
$cpname = pack('H*', $cpname);
if ((stat "$jlog/$cp")[7] != 8) {
print "checkpoint $cpname has invalid size\n";
next;
}
sysopen(CP, "$jlog/$cp", O_RDONLY) or die "could not sysopen $jlog/$cp: $!";
sysread(CP, $data, 4) == 4 or die "checkpoint $cpname: read error: $!";
my $segment = unpack_32($data);
sysread(CP, $data, 4) == 4 or die "checkpoint $cpname: read error: $!";
my $marker = unpack_32($data);
close CP;
if ($segment > $current_segment) {
print "checkpoint $cpname points to segment newer than current segment\n";
next;
}
$oldest_cp_segment = $segment if ($segment < $oldest_cp_segment);
$segment = sprintf "%08x", $segment;
$cpbyname->{$cpname} = {
segment => $segment,
marker => $marker,
};
}
if (!scalar keys %$cpbyname) {
warn "no valid checkpoints\n";
}
my $lastnum = $oldest_cp_segment;
foreach my $seg (@$segments) {
my $num = hex $seg;
if ($num < $oldest_cp_segment) {
print "segment $seg is older than any checkpoint\n";
}
if ($num > $current_segment) {
print "segment $seg is newer than the current segment\n";
}
if ($num > $lastnum + 1) {
if ($num > $lastnum + 2) {
printf "segments %08x though %08x missing\n", $lastnum + 1, $num - 1;
} else {
printf "segment %08x missing\n", $lastnum + 1;
}
}
if ($num > $lastnum) {
$lastnum = $num;
}
my ($idx) = grep /$seg[.]idx/i, @$indexes;
$indexes = [ grep !/$seg[.]idx/i, @$indexes ];
$seg = [ $seg, $idx ];
}
if ($current_segment > $lastnum + 1) {
if ($current_segment > $lastnum + 2) {
printf "segments %08x though %08x missing\n",
$lastnum + 1, $current_segment - 1;
} else {
printf "segment %08x missing\n", $lastnum + 1;
}
}
foreach my $idx (@$indexes) {
print "index $idx doesn't correspond to any segment (skipping)\n";
}
foreach my $segdata (@$segments) {
my ($seg, $idx) = @$segdata;
my $last_marker = 0;
my $last_tv_sec = 0;
my $last_tv_usec = 0;
my $warned_timewarp = 0;
my $warned_toobig = 0;
my $data_off = 0;
my $idx_off = 0;
sysopen(SEG, "$jlog/$seg", O_RDONLY) or die "could not sysopen $jlog/$seg: $!";
my $data_len = (stat SEG)[7];
my $idx_len;
if ($idx) {
sysopen(IDX, "$jlog/$idx", O_RDONLY) or die "could not sysopen $jlog/$idx: $!";
$idx_len = (stat IDX)[7];
}
my $nrec = 0;
my @fixers = ();
while ($data_off < $data_len) {
if (!$warned_toobig and ($data_off > $unit_limit)) {
print "segment $seg has message offset larger than unit limit\n";
$warned_toobig = 1;
}
if ($data_off + 16 > $data_len) {
print "segment $seg offset $data_off: not enough room for message header\n";
last;
}
my $offset = systell(*SEG);
sysread(SEG, $data, 16) == 16
or die "segment $seg offset $data_off: read error: $!";
my $reserved = unpack_32(substr $data, 0, 4);
my $tv_sec = unpack_32(substr $data, 4, 4);
my $tv_usec = unpack_32(substr $data, 8, 4);
my $mlen = unpack_32(substr $data, 12, 4);
if ($reserved != $hdr_magic) {
printf "segment $seg offset $data_off: reserved field (%08x != %08x)\n",
$reserved, $hdr_magic;
push @fixers, $offset if ($reserved == 0);
}
if (!$warned_timewarp) {
if ($tv_sec < $last_tv_sec or
($tv_sec == $last_tv_sec and $tv_usec < $last_tv_usec))
{
print "segment $seg offset $data_off: time goes backwards\n";
$warned_timewarp = 1;
} else {
$last_tv_sec = $tv_sec;
$last_tv_usec = $tv_usec;
}
}
if ($data_off + $mlen > $data_len) {
print "segment $seg offset $data_off: not enough room for message body\n";
last;
}
sysread(SEG, $data, $mlen) == $mlen
or die "segment $seg offset $data_off + 16: read error: $!";
$last_marker++;
$nrec++;
if ($idx) {
if ($idx_off == $idx_len) {
if ($current_segment > hex $seg) {
print "index $idx is incomplete (not an error)\n";
}
close IDX;
undef $idx;
} elsif ($idx_off + 8 > $idx_len) {
print "index $idx offset $idx_off: no room for next offset\n";
close IDX;
undef $idx;
} else {
sysread(IDX, $data, 8) == 8
or die "index $idx offset $idx_off: read error: $!";
my ($offh, $offl) = unpack_64($data);
if ($offh != 0 or $offl != $data_off) {
print "index $idx offset $idx_off: index points to wrong offset\n";
close IDX;
undef $idx;
} else {
$idx_off += 8;
}
}
}
$data_off += 16 + $mlen;
}
if ($data_off == $data_len) {
$segdata->[2] = $last_marker;
foreach my $offset (@fixers) {
printf "writing new hdr_magic at off: %d\n", $offset;
sysopen(FIX, "$jlog/$seg", O_WRONLY) or die "could not sysopen $jlog/$seg: $!";
sysseek(FIX, $offset, SEEK_SET);
my $hdr_blob = pack('V1', $hdr_magic);
die unless length($hdr_blob) == 4;
syswrite(FIX, pack('V1', $hdr_magic), 4);
close FIX;
}
} else {
close IDX;
undef $idx;
}
close SEG;
if ($idx) {
if ($idx_off == $idx_len) {
if (hex $seg < $current_segment) {
print "index $idx not current or closed\n";
}
} elsif ($idx_off + 8 > $idx_len) {
print "index $idx offset $idx_off: no room for closing index\n";
} elsif ($idx_off + 8 < $idx_len) {
print "index $idx offset $idx_off: index too long\n";
} else {
sysread(IDX, $data, 8) == 8
or die "index $idx offset $idx_off: read error: $!";
my ($offh, $offl) = unpack_64($data);
if ($offh != 0 or $offl != 0) {
print "index $idx offset $idx_off: closing offset not 0\n";
}
}
close IDX;
}
printf "segment $seg has %d records\n", $nrec;
}
foreach my $cp (keys %$cpbyname) {
if ($cpbyname->{$cp}{segment} ne '00000000' or
$cpbyname->{$cp}{marker} != 0)
{
my ($segdata) = grep { $_->[0] eq $cpbyname->{$cp}{segment} } @$segments;
if (!defined $segdata) {
if (hex $cpbyname->{$cp}{segment} != $current_segment) {
printf "checkpoint $cp points to nonexistent segment\n";
}
} elsif (!defined $segdata->[2]) {
print "checkpoint $cp points to a damaged segment\n";
} elsif ($cpbyname->{$cp}{marker} > $segdata->[2]) {
print "checkpoint $cp points past the end of segment\n";
}
}
}