-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcopyDb.ts
More file actions
488 lines (449 loc) · 16.6 KB
/
Copy pathcopyDb.ts
File metadata and controls
488 lines (449 loc) · 16.6 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import { getDatabases, getDefaultCompression, resetDatabases } from '../resources/databases.ts';
import { open } from 'lmdb';
import { join } from 'path';
import { move, remove } from 'fs-extra';
import { existsSync, mkdirSync } from 'node:fs';
import { get } from '../utility/environment/environmentManager.js';
import OpenEnvironmentObject from '../utility/lmdb/OpenEnvironmentObject.js';
import { OpenDBIObject } from '../utility/lmdb/OpenDBIObject.js';
import { INTERNAL_DBIS_NAME, AUDIT_STORE_NAME } from '../utility/lmdb/terms.js';
import { CONFIG_PARAMS, DATABASES_DIR_NAME } from '../utility/hdbTerms.ts';
import { AUDIT_STORE_OPTIONS } from '../resources/auditStore.ts';
import { describeSchema } from '../dataLayer/schemaDescribe.js';
import { updateConfigValue } from '../config/configUtils.js';
import * as hdbLogger from '../utility/logging/harper_logger.js';
import { RocksDatabase, type RocksDatabaseOptions } from '@harperfast/rocksdb-js';
import { RocksIndexStore } from '../resources/RocksIndexStore.ts';
export async function compactOnStart() {
hdbLogger.notify('Running compact on start');
console.log('Running compact on start');
// Create compact copy and backup
const rootPath = get(CONFIG_PARAMS.ROOTPATH);
const compactedDb = new Map();
const databases = getDatabases();
updateConfigValue(CONFIG_PARAMS.STORAGE_COMPACTONSTART, false); // don't run this again, and update it before starting so that it fails we don't just keep retrying over and over
try {
for (const databaseName in databases) {
if (databaseName === 'system') continue;
if (databaseName.endsWith('-copy')) continue; // don't copy the copy
let dbPath;
for (const tableName in databases[databaseName]) {
dbPath = databases[databaseName][tableName].primaryStore.path;
break;
}
if (!dbPath) {
console.log("Couldn't find any tables in database", databaseName);
continue;
}
const backupDest = join(rootPath, 'backup', databaseName + '.mdb');
const copyDest = join(rootPath, DATABASES_DIR_NAME, databaseName + '-copy.mdb');
let recordCount = 0;
try {
recordCount = await getTotalDBRecordCount(databaseName);
console.log('Database', databaseName, 'before compact has a total record count of', recordCount);
} catch (error) {
hdbLogger.error('Error getting record count for database', databaseName, error);
console.error('Error getting record count for database', databaseName, error);
}
compactedDb.set(databaseName, {
dbPath,
copyDest,
backupDest,
recordCount,
});
await copyDb(databaseName, copyDest);
console.log('Backing up', databaseName, 'to', backupDest);
try {
await move(dbPath, backupDest, { overwrite: true });
} catch (error) {
console.log('Error moving database', dbPath, 'to', backupDest, error);
}
// Move compacted DB to back to original DB path
console.log('Moving copy compacted', databaseName, 'to', dbPath);
await move(copyDest, dbPath, { overwrite: true });
await remove(join(rootPath, DATABASES_DIR_NAME, `${databaseName}-copy.mdb-lock`));
}
try {
resetDatabases();
} catch (err) {
hdbLogger.error('Error resetting databases after backup', err);
console.error('Error resetting databases after backup', err);
}
try {
resetDatabases();
} catch (err) {
hdbLogger.error('Error resetting databases after backup', err);
console.error('Error resetting databases after backup', err);
process.exit(0); // just let the process restart
}
} catch (err) {
hdbLogger.error('Error compacting database, rolling back operation', err);
console.error('Error compacting database, rolling back operation', err);
updateConfigValue(CONFIG_PARAMS.STORAGE_COMPACTONSTART, false);
for (const [_db, { dbPath, backupDest }] of compactedDb) {
console.error('Moving backup database', backupDest, 'back to', dbPath);
try {
await move(backupDest, dbPath, { overwrite: true });
} catch (err) {
console.error(err);
}
}
resetDatabases();
throw err;
}
// Clean up backups
for (const [db, { backupDest, recordCount }] of compactedDb) {
const compactRecordCount = await getTotalDBRecordCount(db);
console.log('Database', db, 'after compact has a total record count of', compactRecordCount);
if (recordCount !== compactRecordCount) {
const errMsg = `There is a discrepancy between pre and post compact record count for database ${db}.\nTotal record count before compaction: ${recordCount}, total after: ${compactRecordCount}.\nDatabase backup has not been removed and can be found here: ${backupDest}`;
hdbLogger.warn(errMsg);
console.warn(errMsg);
}
if (get(CONFIG_PARAMS.STORAGE_COMPACTONSTARTKEEPBACKUP) === true) continue;
console.log('Removing backup', backupDest);
await remove(backupDest);
}
}
async function getTotalDBRecordCount(database: string) {
const dbDescribe = await describeSchema({ database });
let total = 0;
for (const table in dbDescribe) {
total += dbDescribe[table].record_count;
}
return total;
}
// we replace the write functions with a noop during this process, just in case they get called
function noop() {
// if there are any attempts to write to the db, ignore them
}
export async function copyDb(sourceDatabase: string, targetDatabasePath: string) {
console.log(`Copying database ${sourceDatabase} to ${targetDatabasePath}`);
const sourceDb = getDatabases()[sourceDatabase];
if (!sourceDb) throw new Error(`Source database not found: ${sourceDatabase}`);
let rootStore;
for (const tableName in sourceDb) {
const table = sourceDb[tableName];
// ensure that writes aren't occurring
table.primaryStore.put = noop;
table.primaryStore.remove = noop;
for (const attributeName in table.indices) {
const index = table.indices[attributeName];
index.put = noop;
index.remove = noop;
}
if (table.auditStore) {
table.auditStore.put = noop;
table.auditStore.remove = noop;
}
rootStore = table.primaryStore.rootStore;
}
if (!rootStore) throw new Error(`Source database does not have any tables: ${sourceDatabase}`);
// this contains the list of all the dbis
const sourceDbisDb = rootStore.dbisDb;
const sourceAuditStore = rootStore.auditStore;
const targetEnv = open(new OpenEnvironmentObject(targetDatabasePath));
const targetDbisDb = targetEnv.openDB({ name: INTERNAL_DBIS_NAME });
let written;
let outstandingWrites = 0;
// we use a single transaction to get a snapshot, also we can't use snapshot: false on dupsort dbs
const transaction = sourceDbisDb.useReadTransaction();
try {
for (const { key, value: attribute } of sourceDbisDb.getRange({ transaction })) {
const isPrimary = attribute.isPrimaryKey;
let existingCompression, newCompression;
if (isPrimary) {
existingCompression = attribute.compression;
newCompression = getDefaultCompression();
if (newCompression) attribute.compression = newCompression;
else delete attribute.compression;
if (existingCompression?.dictionary?.toString() === newCompression?.dictionary?.toString()) {
// no need to change the compression, it's the same, so we can, and should, skip decompressing and recompressing
existingCompression = null;
newCompression = null;
}
}
targetDbisDb.put(key, attribute);
if (!(isPrimary || attribute.indexed)) continue;
const dbiInit = new OpenDBIObject(!isPrimary, isPrimary);
// we want to directly copy bytes so we don't have the overhead of
// encoding and decoding
dbiInit.encoding = 'binary';
dbiInit.compression = existingCompression;
//dbiInit.keyEncoding = 'binary';
const sourceDbi = rootStore.openDB(key, dbiInit);
sourceDbi.decoder = null;
sourceDbi.decoderCopies = false;
sourceDbi.encoding = 'binary';
dbiInit.compression = newCompression;
const targetDbi = targetEnv.openDB(key, dbiInit);
targetDbi.encoder = null;
console.log('copying', key, 'from', sourceDatabase, 'to', targetDatabasePath);
await copyDbi(sourceDbi, targetDbi, isPrimary, transaction);
}
if (sourceAuditStore) {
const targetAuditStore = rootStore.openDB(AUDIT_STORE_NAME, AUDIT_STORE_OPTIONS);
console.log('copying audit log for', sourceDatabase, 'to', targetDatabasePath);
copyDbi(sourceAuditStore, targetAuditStore, false, transaction);
}
async function copyDbi(sourceDbi, targetDbi, isPrimary, transaction) {
let recordsCopied = 0;
let bytesCopied = 0;
let skippedRecord = 0;
let retries = 10000000;
let start = null;
while (retries-- > 0) {
try {
for (const key of sourceDbi.getKeys({ start, transaction })) {
try {
start = key;
const { value, version } = sourceDbi.getEntry(key, { transaction });
// deleted entries should be 13 bytes long (8 for timestamp, 4 bytes for flags, 1 byte of the encoding of null)
if (value?.length < 14 && isPrimary) {
skippedRecord++;
continue;
}
written = targetDbi.put(key, value, isPrimary ? version : undefined);
recordsCopied++;
if (transaction.openTimer) transaction.openTimer = 0; // reset the timer, don't want it to time out
bytesCopied += (key?.length || 10) + value.length;
if (outstandingWrites++ > 5000) {
await written;
console.log(
'copied',
recordsCopied,
'entries, skipped',
skippedRecord,
'delete records,',
bytesCopied,
'bytes'
);
outstandingWrites = 0;
}
} catch (error) {
console.error(
'Error copying record',
typeof key === 'symbol' ? 'symbol' : key,
'from',
sourceDatabase,
'to',
targetDatabasePath,
error
);
}
}
console.log(
'finish copying, copied',
recordsCopied,
'entries, skipped',
skippedRecord,
'delete records,',
bytesCopied,
'bytes'
);
return;
} catch {
// try to resume with a bigger key
if (typeof start === 'string') {
if (start === 'z') {
return console.error('Reached end of dbi', start, 'for', sourceDatabase, 'to', targetDatabasePath);
}
start = start.slice(0, -2) + 'z';
} else if (typeof start === 'number') start++;
else return console.error('Unknown key type', start, 'for', sourceDatabase, 'to', targetDatabasePath);
}
}
}
await written;
console.log('copied database ' + sourceDatabase + ' to ' + targetDatabasePath);
} finally {
transaction.done();
targetEnv.close();
}
}
function openRocksDb(path: string, options: RocksDatabaseOptions & { dupSort?: boolean } = {}) {
options.disableWAL ??= false;
if (!existsSync(path)) {
mkdirSync(path, { recursive: true });
}
let db;
if (options.dupSort) {
db = RocksDatabase.open(new RocksIndexStore(path, options));
} else {
db = RocksDatabase.open(path, options);
db.encoder.name = options.name;
}
return db;
}
export async function migrateOnStart() {
hdbLogger.notify('Running migrate on start (LMDB to RocksDB)');
console.log('Running migrate on start (LMDB to RocksDB)');
const rootPath = get(CONFIG_PARAMS.ROOTPATH);
const databases = getDatabases();
updateConfigValue(CONFIG_PARAMS.STORAGE_MIGRATEONSTART, false);
try {
for (const databaseName in databases) {
if (databaseName === 'system') continue;
if (databaseName.endsWith('-copy')) continue;
let rootStore;
for (const tableName in databases[databaseName]) {
const table = databases[databaseName][tableName];
table.primaryStore.put = noop;
table.primaryStore.remove = noop;
for (const attributeName in table.indices) {
const index = table.indices[attributeName];
index.put = noop;
index.remove = noop;
}
if (table.auditStore) {
table.auditStore.put = noop;
table.auditStore.remove = noop;
}
rootStore = table.primaryStore.rootStore;
}
if (!rootStore) {
console.log("Couldn't find any tables in database", databaseName);
continue;
}
if (rootStore instanceof RocksDatabase) {
console.log('Database', databaseName, 'is already RocksDB, skipping');
continue;
}
const targetPath = join(rootPath, DATABASES_DIR_NAME, databaseName);
const lmdbPath = rootStore.path;
const backupDest = join(rootPath, 'backup', databaseName + '.mdb');
console.log('Migrating', databaseName, 'from LMDB to RocksDB at', targetPath);
await copyDbToRocks(rootStore, databaseName, targetPath);
// Back up the original LMDB file
console.log('Backing up LMDB', databaseName, 'to', backupDest);
try {
await move(lmdbPath, backupDest, { overwrite: true });
} catch (error) {
console.log('Error moving database', lmdbPath, 'to', backupDest, error);
}
// Remove the lock file
try {
await remove(lmdbPath + '-lock');
} catch {
// lock file may not exist
}
}
try {
resetDatabases();
} catch (err) {
hdbLogger.error('Error resetting databases after migration', err);
console.error('Error resetting databases after migration', err);
}
} catch (err) {
hdbLogger.error('Error migrating database', err);
console.error('Error migrating database', err);
throw err;
}
}
async function copyDbToRocks(sourceRootStore, sourceDatabase: string, targetPath: string) {
console.log(`Migrating database ${sourceDatabase} to RocksDB at ${targetPath}`);
const sourceDbisDb = sourceRootStore.dbisDb;
const targetRootStore = openRocksDb(targetPath, { disableWAL: false });
const targetDbisDb = openRocksDb(targetPath, {
disableWAL: false,
name: INTERNAL_DBIS_NAME,
});
let written;
let outstandingWrites = 0;
const transaction = sourceDbisDb.useReadTransaction();
try {
for (const { key, value: attribute } of sourceDbisDb.getRange({ transaction })) {
const isPrimary = attribute.isPrimaryKey;
targetDbisDb.put(key, attribute);
if (!(isPrimary || attribute.indexed)) continue;
// Open source LMDB dbi with default encoding so values are decoded
const dbiInit = new OpenDBIObject(!isPrimary, isPrimary);
const sourceDbi = sourceRootStore.openDB(key, dbiInit);
let targetDbi;
if (!isPrimary) {
targetDbi = openRocksDb(targetPath, { dupSort: true, name: key });
} else {
targetDbi = openRocksDb(targetPath, { name: key });
}
console.log('migrating', key, 'from', sourceDatabase, 'to RocksDB');
await copyDbiToRocks(sourceDbi, targetDbi, isPrimary, transaction);
}
// Note: audit store is not migrated because LMDB and RocksDB use fundamentally different
// audit store formats (LMDB uses a custom binary encoding in a regular DB, RocksDB uses TransactionLog).
// A new audit store will be created automatically when the RocksDB database is opened.
await written;
console.log('migrated database ' + sourceDatabase + ' to RocksDB');
} finally {
transaction.done();
targetRootStore.close();
}
async function copyDbiToRocks(sourceDbi, targetDbi, isPrimary, transaction) {
let recordsCopied = 0;
let skippedRecord = 0;
let retries = 1000000;
let start = null;
while (retries-- > 0) {
try {
if (isPrimary) {
for (const { key, value, version } of sourceDbi.getRange({ start, transaction, versions: true })) {
try {
start = key;
if (value == null) {
skippedRecord++;
continue;
}
written = targetDbi.put(key, value, version);
recordsCopied++;
if (transaction.openTimer) transaction.openTimer = 0;
if (outstandingWrites++ > 5000) {
await written;
console.log('migrated', recordsCopied, 'entries, skipped', skippedRecord, 'delete records');
outstandingWrites = 0;
}
} catch (error) {
console.error(
'Error migrating record',
typeof key === 'symbol' ? 'symbol' : key,
'from',
sourceDatabase,
error
);
}
}
} else {
for (const { key, value } of sourceDbi.getRange({ start, transaction })) {
try {
start = key;
written = targetDbi.put(key, value);
recordsCopied++;
if (transaction.openTimer) transaction.openTimer = 0;
if (outstandingWrites++ > 5000) {
await written;
console.log('migrated', recordsCopied, 'index entries');
outstandingWrites = 0;
}
} catch (error) {
console.error(
'Error migrating index record',
typeof key === 'symbol' ? 'symbol' : key,
'from',
sourceDatabase,
error
);
}
}
}
console.log('finish migrating, copied', recordsCopied, 'entries, skipped', skippedRecord, 'delete records');
return;
} catch {
if (typeof start === 'string') {
if (start === 'z') {
return console.error('Reached end of dbi', start, 'for', sourceDatabase);
}
start = start.slice(0, -2) + 'z';
} else if (typeof start === 'number') start++;
else return console.error('Unknown key type', start, 'for', sourceDatabase);
}
}
}
}