Skip to content

Commit 64052c7

Browse files
committed
Merge bitcoin#15: Add filename to corruption errors
135ed0f Add filename to corruption errors (Wladimir J. van der Laan) Pull request description: When a corruption happens, report the filename of the file where corruptions happens. This will aid in diagnosing database corruption issues. Adds a GetName() to all the environment file classes to make it possible to report a filename downstream. Tree-SHA512: 4b76803977ec6c2a4e113f2741619c586f5adbb2739a172e3dae3c21b79edf5a689d243850c56903e14c39b01224f8656310c2a39b3fb89ea107d7243b4794e1
2 parents c521b3a + 135ed0f commit 64052c7

File tree

9 files changed

+32
-8
lines changed

9 files changed

+32
-8
lines changed

db/db_impl.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ Status DBImpl::RecoverLogFile(uint64_t log_number, bool last_log,
414414
status.ok()) {
415415
if (record.size() < 12) {
416416
reporter.Corruption(
417-
record.size(), Status::Corruption("log record too small"));
417+
record.size(), Status::Corruption("log record too small", fname));
418418
continue;
419419
}
420420
WriteBatchInternal::SetContents(&batch, record);

db/leveldbutil.cc

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class StdoutPrinter : public WritableFile {
1919
virtual Status Close() { return Status::OK(); }
2020
virtual Status Flush() { return Status::OK(); }
2121
virtual Status Sync() { return Status::OK(); }
22+
virtual std::string GetName() const { return "[stdout]"; }
2223
};
2324

2425
bool HandleDumpCommand(Env* env, char** files, int num) {

db/log_reader.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ uint64_t Reader::LastRecordOffset() {
186186
}
187187

188188
void Reader::ReportCorruption(uint64_t bytes, const char* reason) {
189-
ReportDrop(bytes, Status::Corruption(reason));
189+
ReportDrop(bytes, Status::Corruption(reason, file_->GetName()));
190190
}
191191

192192
void Reader::ReportDrop(uint64_t bytes, const Status& reason) {

db/repair.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class Repairer {
203203
while (reader.ReadRecord(&record, &scratch)) {
204204
if (record.size() < 12) {
205205
reporter.Corruption(
206-
record.size(), Status::Corruption("log record too small"));
206+
record.size(), Status::Corruption("log record too small", logname));
207207
continue;
208208
}
209209
WriteBatchInternal::SetContents(&batch, record);

helpers/memenv/memenv.cc

+3
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ class SequentialFileImpl : public SequentialFile {
176176
return Status::OK();
177177
}
178178

179+
virtual std::string GetName() const { return "[memenv]"; }
179180
private:
180181
FileState* file_;
181182
uint64_t pos_;
@@ -196,6 +197,7 @@ class RandomAccessFileImpl : public RandomAccessFile {
196197
return file_->Read(offset, n, result, scratch);
197198
}
198199

200+
virtual std::string GetName() const { return "[memenv]"; }
199201
private:
200202
FileState* file_;
201203
};
@@ -218,6 +220,7 @@ class WritableFileImpl : public WritableFile {
218220
virtual Status Flush() { return Status::OK(); }
219221
virtual Status Sync() { return Status::OK(); }
220222

223+
virtual std::string GetName() const { return "[memenv]"; }
221224
private:
222225
FileState* file_;
223226
};

include/leveldb/env.h

+9
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ class SequentialFile {
191191
// REQUIRES: External synchronization
192192
virtual Status Skip(uint64_t n) = 0;
193193

194+
// Get a name for the file, only for error reporting
195+
virtual std::string GetName() const = 0;
196+
194197
private:
195198
// No copying allowed
196199
SequentialFile(const SequentialFile&);
@@ -215,6 +218,9 @@ class RandomAccessFile {
215218
virtual Status Read(uint64_t offset, size_t n, Slice* result,
216219
char* scratch) const = 0;
217220

221+
// Get a name for the file, only for error reporting
222+
virtual std::string GetName() const = 0;
223+
218224
private:
219225
// No copying allowed
220226
RandomAccessFile(const RandomAccessFile&);
@@ -234,6 +240,9 @@ class WritableFile {
234240
virtual Status Flush() = 0;
235241
virtual Status Sync() = 0;
236242

243+
// Get a name for the file, only for error reporting
244+
virtual std::string GetName() const = 0;
245+
237246
private:
238247
// No copying allowed
239248
WritableFile(const WritableFile&);

table/format.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Status ReadBlock(RandomAccessFile* file,
8282
}
8383
if (contents.size() != n + kBlockTrailerSize) {
8484
delete[] buf;
85-
return Status::Corruption("truncated block read");
85+
return Status::Corruption("truncated block read", file->GetName());
8686
}
8787

8888
// Check the crc of the type and the block contents
@@ -92,7 +92,7 @@ Status ReadBlock(RandomAccessFile* file,
9292
const uint32_t actual = crc32c::Value(data, n + 1);
9393
if (actual != crc) {
9494
delete[] buf;
95-
s = Status::Corruption("block checksum mismatch");
95+
s = Status::Corruption("block checksum mismatch", file->GetName());
9696
return s;
9797
}
9898
}
@@ -119,13 +119,13 @@ Status ReadBlock(RandomAccessFile* file,
119119
size_t ulength = 0;
120120
if (!port::Snappy_GetUncompressedLength(data, n, &ulength)) {
121121
delete[] buf;
122-
return Status::Corruption("corrupted compressed block contents");
122+
return Status::Corruption("corrupted compressed block contents", file->GetName());
123123
}
124124
char* ubuf = new char[ulength];
125125
if (!port::Snappy_Uncompress(data, n, ubuf)) {
126126
delete[] buf;
127127
delete[] ubuf;
128-
return Status::Corruption("corrupted compressed block contents");
128+
return Status::Corruption("corrupted compressed block contents", file->GetName());
129129
}
130130
delete[] buf;
131131
result->data = Slice(ubuf, ulength);
@@ -135,7 +135,7 @@ Status ReadBlock(RandomAccessFile* file,
135135
}
136136
default:
137137
delete[] buf;
138-
return Status::Corruption("bad block type");
138+
return Status::Corruption("bad block type", file->GetName());
139139
}
140140

141141
return Status::OK();

util/env_posix.cc

+8
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ class PosixSequentialFile: public SequentialFile {
121121
}
122122
return Status::OK();
123123
}
124+
125+
virtual std::string GetName() const { return filename_; }
124126
};
125127

126128
// pread() based random-access
@@ -172,6 +174,8 @@ class PosixRandomAccessFile: public RandomAccessFile {
172174
}
173175
return s;
174176
}
177+
178+
virtual std::string GetName() const { return filename_; }
175179
};
176180

177181
// mmap() based random-access
@@ -206,6 +210,8 @@ class PosixMmapReadableFile: public RandomAccessFile {
206210
}
207211
return s;
208212
}
213+
214+
virtual std::string GetName() const { return filename_; }
209215
};
210216

211217
class PosixWritableFile : public WritableFile {
@@ -287,6 +293,8 @@ class PosixWritableFile : public WritableFile {
287293
}
288294
return s;
289295
}
296+
297+
virtual std::string GetName() const { return filename_; }
290298
};
291299

292300
static int LockOrUnlock(int fd, bool lock) {

util/env_win.cc

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class Win32SequentialFile : public SequentialFile
7878
virtual Status Read(size_t n, Slice* result, char* scratch);
7979
virtual Status Skip(uint64_t n);
8080
BOOL isEnable();
81+
virtual std::string GetName() const { return _filename; }
8182
private:
8283
BOOL _Init();
8384
void _CleanUp();
@@ -94,6 +95,7 @@ class Win32RandomAccessFile : public RandomAccessFile
9495
virtual ~Win32RandomAccessFile();
9596
virtual Status Read(uint64_t offset, size_t n, Slice* result,char* scratch) const;
9697
BOOL isEnable();
98+
virtual std::string GetName() const { return _filename; }
9799
private:
98100
BOOL _Init(LPCWSTR path);
99101
void _CleanUp();
@@ -114,6 +116,7 @@ class Win32WritableFile : public WritableFile
114116
virtual Status Flush();
115117
virtual Status Sync();
116118
BOOL isEnable();
119+
virtual std::string GetName() const { return filename_; }
117120
private:
118121
std::string filename_;
119122
::HANDLE _hFile;

0 commit comments

Comments
 (0)