-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBRepairer.ts
335 lines (335 loc) · 12.4 KB
/
DBRepairer.ts
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
/**
* Copyright (c) 2018-present, heineiuo.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Options, DatabaseOptions, defaultOptions, ReadOptions, } from "./Options.ts";
import { parseFilename, getLogFilename, getTableFilename, getTempFilename, getManifestFilename, setCurrentFile, } from "./Filename.ts";
import { FileType, InternalKeyComparator, SequenceNumber, parseInternalKey, ParsedInternalKey, } from "./Format.ts";
import { Env, FileHandle } from "./Env.ts";
import LogReader from "./LogReader.ts";
import MemTable from "./MemTable.ts";
import { WriteBatch, WriteBatchInternal } from "./WriteBatch.ts";
import { FileMetaData } from "./VersionFormat.ts";
import { buildTable } from "./Builder.ts";
import { TableCache } from "./SSTableCache.ts";
import SSTableBuilder from "./SSTableBuilder.ts";
import VersionEdit from "./VersionEdit.ts";
import LogWriter from "./LogWriter.ts";
import VersionEditRecord from "./VersionEditRecord.ts";
import { path, assert } from "./DBHelper.ts";
type TableInfo = {
meta: FileMetaData;
maxSequence: SequenceNumber;
};
export class InternalDBRepairer {
constructor(dbpath: string, originalOptions: DatabaseOptions) {
if (!originalOptions.env)
throw new Error("env required");
const env = originalOptions.env;
this._dbpath = dbpath;
this._options = { ...defaultOptions, ...originalOptions, env };
this._manifests = [];
this._logs = [];
this._icmp = new InternalKeyComparator(this._options.comparator);
this._tables = [];
this._env = this._options.env;
this._nextFileNumber = 1;
this._tableCache = new TableCache(dbpath, this._options, 10);
this._tableInfos = [];
this._edit = new VersionEdit();
}
private _env: Env;
private _icmp: InternalKeyComparator;
private _manifests: string[];
private _logs: number[];
private _tables: number[];
private _tableInfos: TableInfo[];
private _dbpath: string;
private _options: Options;
private _nextFileNumber: number;
private _tableCache: TableCache;
private _edit: VersionEdit;
async run(): Promise<void | Error> {
let error: void | Error;
error = await this.findFiles();
if (!error) {
await this.convertLogFilesToTables();
await this.extractMetaData();
error = await this.writeDescriptor();
}
if (!error) {
let bytes = 0;
for (const tableInfo of this._tableInfos) {
bytes += tableInfo.meta.fileSize;
}
await this._options.log(`**** Repaired leveldb ${this._dbpath};
recovered ${this._tableInfos.length} files; ${bytes} bytes.
Some data may have been lost.
****`);
}
if (this._options.debug && error) {
console.log(error);
}
return error;
}
async findFiles(): Promise<void | Error> {
const filenames = (await this._options.env.readdir(this._dbpath)).reduce((filenames: string[], dirent) => {
if (dirent.isFile()) {
filenames.push(dirent.name);
}
return filenames;
}, []);
if (filenames.length === 0) {
throw new Error("DBRepairer found no files");
}
for (const filename of filenames) {
const parsedFile = parseFilename(filename);
if (parsedFile.type === FileType.kDescriptorFile) {
this._manifests.push(filename);
}
else {
if (parsedFile.number + 1 > this._nextFileNumber) {
this._nextFileNumber = parsedFile.number + 1;
}
if (parsedFile.type === FileType.kLogFile) {
this._logs.push(parsedFile.number);
}
else if (parsedFile.type === FileType.kTableFile) {
this._tables.push(parsedFile.number);
}
else {
// Ignore other files
}
}
}
}
async convertLogFilesToTables(): Promise<void> {
for (const logFileNumber of this._logs) {
const logFilename = getLogFilename(this._dbpath, logFileNumber);
try {
await this.convertLogToTable(logFileNumber);
}
catch (e) {
this._options.log(`Log #${logFileNumber}: Ignoring conversion error: ${e.message}`);
}
finally {
this.archiveFile(logFilename);
}
}
}
async convertLogToTable(logNumber: number): Promise<void> {
const logFilename = getLogFilename(this._dbpath, logNumber);
const reader = new LogReader(await this._options.env.open(logFilename, "r"));
const mem = new MemTable(this._icmp);
mem.ref();
for await (const record of reader.iterator()) {
if (record.size < 12) {
this._options.log(` ${logFilename} log record too small: dropping ${record.size} bytes`);
continue;
}
try {
const batch = new WriteBatch();
WriteBatchInternal.setContents(batch, record.buffer);
WriteBatchInternal.insert(batch, mem);
}
catch (e) {
this._options.log(`Log #${logNumber}: Ignoring ${e.message}`);
}
}
await reader.close();
const meta = new FileMetaData();
meta.number = this._nextFileNumber++;
const status = await buildTable(this._dbpath, this._env, this._options, mem.iterator(), meta);
mem.unref();
if (await status.ok()) {
if (meta.fileSize > 0) {
this._tables.push(meta.number);
}
}
this._options.log(`Log #${logNumber}: ops saved to Table #${meta.number} ${status.message}`);
}
async archiveFile(filename: string): Promise<void> {
// Move into another directory. E.g., for
// dir/foo
// rename to
// dir/lost/foo
const slashIndex = filename.lastIndexOf("/");
const lostDir = filename.substr(0, slashIndex) + "/lost";
try {
await this._env.mkdir(lostDir);
}
catch (e) { }
const newFilename = lostDir + filename.substr(slashIndex);
await this._env.rename(filename, newFilename);
this._options.log(`Archiving ${filename}: ${newFilename}`);
}
async extractMetaData(): Promise<void> {
for (const tableNumber of this._tables) {
await this.scanTable(tableNumber);
}
}
async scanTable(tableNumber: number): Promise<void> {
const tableInfo: TableInfo = {
meta: new FileMetaData(),
maxSequence: 0n,
};
tableInfo.meta.number = tableNumber;
const tableFilename = getTableFilename(this._dbpath, tableNumber);
try {
tableInfo.meta.fileSize = await this._env.getFileSize(tableFilename);
}
catch (e) {
try {
await this.archiveFile(tableFilename);
}
catch (e) { }
}
// Extract metadata by scanning through table.
let counter = 0;
let empty = true;
const options: ReadOptions = {};
const parsed = new ParsedInternalKey();
let error: Error | void;
try {
for await (const entry of this._tableCache.entryIterator(options, tableInfo.meta.number, tableInfo.meta.fileSize)) {
if (!parseInternalKey(entry.key, parsed)) {
this._options.log(`Table #${tableInfo.meta.number}: unparseable key ${entry.key.toString()}`);
continue;
}
counter++;
if (empty) {
empty = false;
tableInfo.meta.smallest.decodeFrom(entry.key);
}
tableInfo.meta.largest.decodeFrom(entry.key);
if (parsed.sn > tableInfo.maxSequence) {
tableInfo.maxSequence = parsed.sn;
}
}
assert(tableInfo.meta.smallest.length > 0);
}
catch (e) {
error = e;
}
this._options.log(`Table #${tableInfo.meta.number}: ${counter} entries ${error ? error.message : "success"}`);
if (!error) {
this._tableInfos.push(tableInfo);
}
else {
await this.repairTable(tableFilename, tableInfo); // RepairTable archives input file.
}
}
async repairTable(srcTableFilename: string, tableInfo: TableInfo): Promise<void | Error> {
// We will copy src contents to a new table and then rename the
// new table over the source.
const destTableFilename = getTableFilename(this._dbpath, this._nextFileNumber++);
let error: Error | void;
try {
let counter = 0;
const fd = await this._env.open(destTableFilename, "a+");
const tableBuilder = new SSTableBuilder(this._options, fd);
const options: ReadOptions = {};
for await (const entry of this._tableCache.entryIterator(options, tableInfo.meta.number, tableInfo.meta.fileSize)) {
await tableBuilder.add(entry.key, entry.value);
counter++;
}
try {
await this.archiveFile(srcTableFilename);
if (counter === 0) {
await tableBuilder.abandon();
}
else {
await tableBuilder.finish();
tableInfo.meta.fileSize = tableBuilder.fileSize;
}
}
catch (e) {
error = e;
}
finally {
try {
if (counter > 0 && !error) {
const orig = getTableFilename(this._dbpath, tableInfo.meta.number);
await this._env.rename(destTableFilename, orig);
this._options.log(`Table #${tableInfo.meta.number}: ${counter} entries repaired`);
}
}
catch (e) {
error = e;
}
}
}
catch (e) {
error = e;
}
finally {
if (error) {
try {
await this._env.unlink(destTableFilename);
}
catch (e) { }
}
}
}
async writeDescriptor(): Promise<void | Error> {
const tempFilename = getTempFilename(this._dbpath, 1);
let error: void | Error;
let file: FileHandle | void;
try {
file = await this._env.open(tempFilename, "a+");
}
catch (e) {
error = e;
}
if (error || !file)
return;
let maxSequence = 0n;
for (const tableInfo of this._tableInfos) {
if (maxSequence < tableInfo.maxSequence) {
maxSequence = tableInfo.maxSequence;
}
}
this._edit.comparator = this._icmp.userComparator.getName();
this._edit.logNumber = 0;
this._edit.nextFileNumber = this._nextFileNumber;
this._edit.lastSequence = maxSequence;
for (const tableInfo of this._tableInfos) {
this._edit.addFile(0, tableInfo.meta.number, tableInfo.meta.fileSize, tableInfo.meta.smallest, tableInfo.meta.largest);
}
let writer: void | LogWriter;
try {
writer = new LogWriter(file);
await writer.addRecord(VersionEditRecord.add(this._edit));
}
catch (e) {
error = e;
}
if (!error && writer) {
await writer.close();
}
if (error) {
await this._env.unlink(tempFilename);
}
else {
try {
for (const manifest of this._manifests) {
await this.archiveFile(path.resolve(this._dbpath, manifest));
}
// Install new manifest
this._env.rename(tempFilename, getManifestFilename(this._dbpath, 1));
}
catch (e) {
error = e;
}
if (error) {
this._env.unlink(tempFilename);
}
else {
await setCurrentFile(this._env, this._dbpath, 1);
}
}
return error;
}
}