Skip to content

Commit bf2c209

Browse files
committed
Add filename to corruption errors
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.
1 parent 0c40829 commit bf2c209

File tree

13 files changed

+45
-8
lines changed

13 files changed

+45
-8
lines changed

db/db_impl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ Status DBImpl::RecoverLogFile(uint64_t log_number, bool last_log,
428428
while (reader.ReadRecord(&record, &scratch) && status.ok()) {
429429
if (record.size() < 12) {
430430
reporter.Corruption(record.size(),
431-
Status::Corruption("log record too small"));
431+
Status::Corruption("log record too small", fname));
432432
continue;
433433
}
434434
WriteBatchInternal::SetContents(&batch, record);

db/db_test.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class SpecialEnv : public EnvWrapper {
158158
}
159159
return base_->Sync();
160160
}
161+
std::string GetName() const override { return ""; }
161162
};
162163
class ManifestFile : public WritableFile {
163164
private:
@@ -183,6 +184,7 @@ class SpecialEnv : public EnvWrapper {
183184
return base_->Sync();
184185
}
185186
}
187+
std::string GetName() const override { return ""; }
186188
};
187189

188190
if (non_writable_.load(std::memory_order_acquire)) {
@@ -216,6 +218,7 @@ class SpecialEnv : public EnvWrapper {
216218
counter_->Increment();
217219
return target_->Read(offset, n, result, scratch);
218220
}
221+
std::string GetName() const override { return ""; }
219222
};
220223

221224
Status s = target()->NewRandomAccessFile(f, r);

db/fault_injection_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class TestWritableFile : public WritableFile {
114114
Status Close() override;
115115
Status Flush() override;
116116
Status Sync() override;
117+
std::string GetName() const override { return ""; }
117118

118119
private:
119120
FileState state_;

db/leveldbutil.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class StdoutPrinter : public WritableFile {
2020
Status Close() override { return Status::OK(); }
2121
Status Flush() override { return Status::OK(); }
2222
Status Sync() override { return Status::OK(); }
23+
std::string GetName() const override { return "[stdout]"; }
2324
};
2425

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

db/log_reader.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ bool Reader::ReadRecord(Slice* record, std::string* scratch) {
176176
uint64_t Reader::LastRecordOffset() { return last_record_offset_; }
177177

178178
void Reader::ReportCorruption(uint64_t bytes, const char* reason) {
179-
ReportDrop(bytes, Status::Corruption(reason));
179+
ReportDrop(bytes, Status::Corruption(reason, file_->GetName()));
180180
}
181181

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

db/log_test.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ class LogTest {
168168
contents_.append(slice.data(), slice.size());
169169
return Status::OK();
170170
}
171+
std::string GetName() const override { return ""; }
171172

172173
std::string contents_;
173174
};
@@ -204,6 +205,7 @@ class LogTest {
204205

205206
return Status::OK();
206207
}
208+
std::string GetName() const { return ""; }
207209

208210
Slice contents_;
209211
bool force_error_;

db/repair.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class Repairer {
183183
while (reader.ReadRecord(&record, &scratch)) {
184184
if (record.size() < 12) {
185185
reporter.Corruption(record.size(),
186-
Status::Corruption("log record too small"));
186+
Status::Corruption("log record too small", logname));
187187
continue;
188188
}
189189
WriteBatchInternal::SetContents(&batch, record);

helpers/memenv/memenv.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class SequentialFileImpl : public SequentialFile {
178178
return Status::OK();
179179
}
180180

181+
virtual std::string GetName() const override { return "[memenv]"; }
181182
private:
182183
FileState* file_;
183184
uint64_t pos_;
@@ -194,6 +195,7 @@ class RandomAccessFileImpl : public RandomAccessFile {
194195
return file_->Read(offset, n, result, scratch);
195196
}
196197

198+
virtual std::string GetName() const override { return "[memenv]"; }
197199
private:
198200
FileState* file_;
199201
};
@@ -210,6 +212,7 @@ class WritableFileImpl : public WritableFile {
210212
Status Flush() override { return Status::OK(); }
211213
Status Sync() override { return Status::OK(); }
212214

215+
virtual std::string GetName() const override { return "[memenv]"; }
213216
private:
214217
FileState* file_;
215218
};

include/leveldb/env.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ class LEVELDB_EXPORT SequentialFile {
217217
//
218218
// REQUIRES: External synchronization
219219
virtual Status Skip(uint64_t n) = 0;
220+
221+
// Get a name for the file, only for error reporting
222+
virtual std::string GetName() const = 0;
220223
};
221224

222225
// A file abstraction for randomly reading the contents of a file.
@@ -240,6 +243,9 @@ class LEVELDB_EXPORT RandomAccessFile {
240243
// Safe for concurrent use by multiple threads.
241244
virtual Status Read(uint64_t offset, size_t n, Slice* result,
242245
char* scratch) const = 0;
246+
247+
// Get a name for the file, only for error reporting
248+
virtual std::string GetName() const = 0;
243249
};
244250

245251
// A file abstraction for sequential writing. The implementation
@@ -258,6 +264,9 @@ class LEVELDB_EXPORT WritableFile {
258264
virtual Status Close() = 0;
259265
virtual Status Flush() = 0;
260266
virtual Status Sync() = 0;
267+
268+
// Get a name for the file, only for error reporting
269+
virtual std::string GetName() const = 0;
261270
};
262271

263272
// An interface for writing log messages.

table/format.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Status ReadBlock(RandomAccessFile* file, const ReadOptions& options,
7979
}
8080
if (contents.size() != n + kBlockTrailerSize) {
8181
delete[] buf;
82-
return Status::Corruption("truncated block read");
82+
return Status::Corruption("truncated block read", file->GetName());
8383
}
8484

8585
// Check the crc of the type and the block contents
@@ -89,7 +89,7 @@ Status ReadBlock(RandomAccessFile* file, const ReadOptions& options,
8989
const uint32_t actual = crc32c::Value(data, n + 1);
9090
if (actual != crc) {
9191
delete[] buf;
92-
s = Status::Corruption("block checksum mismatch");
92+
s = Status::Corruption("block checksum mismatch", file->GetName());
9393
return s;
9494
}
9595
}
@@ -116,13 +116,13 @@ Status ReadBlock(RandomAccessFile* file, const ReadOptions& options,
116116
size_t ulength = 0;
117117
if (!port::Snappy_GetUncompressedLength(data, n, &ulength)) {
118118
delete[] buf;
119-
return Status::Corruption("corrupted compressed block contents");
119+
return Status::Corruption("corrupted compressed block contents", file->GetName());
120120
}
121121
char* ubuf = new char[ulength];
122122
if (!port::Snappy_Uncompress(data, n, ubuf)) {
123123
delete[] buf;
124124
delete[] ubuf;
125-
return Status::Corruption("corrupted compressed block contents");
125+
return Status::Corruption("corrupted compressed block contents", file->GetName());
126126
}
127127
delete[] buf;
128128
result->data = Slice(ubuf, ulength);
@@ -132,7 +132,7 @@ Status ReadBlock(RandomAccessFile* file, const ReadOptions& options,
132132
}
133133
default:
134134
delete[] buf;
135-
return Status::Corruption("bad block type");
135+
return Status::Corruption("bad block type", file->GetName());
136136
}
137137

138138
return Status::OK();

0 commit comments

Comments
 (0)