-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathauditStore.ts
More file actions
659 lines (643 loc) · 23.3 KB
/
Copy pathauditStore.ts
File metadata and controls
659 lines (643 loc) · 23.3 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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import { readKey, writeKey } from 'ordered-binary';
import { initSync, get as envGet } from '../utility/environment/environmentManager.js';
import { AUDIT_STORE_NAME } from '../utility/lmdb/terms.js';
import { CONFIG_PARAMS } from '../utility/hdbTerms.ts';
import { getWorkerIndex, getWorkerCount } from '../server/threads/manageThreads.js';
import { convertToMS } from '../utility/common_utils.js';
import { PREVIOUS_TIMESTAMP_PLACEHOLDER, LAST_TIMESTAMP_PLACEHOLDER } from './RecordEncoder.ts';
import * as harperLogger from '../utility/logging/harper_logger.js';
import { getRecordAtTime } from './crdt.ts';
import { decodeFromDatabase } from './blob.ts';
import { onStorageReclamation } from '../server/storageReclamation.ts';
import { RocksDatabase } from '@harperfast/rocksdb-js';
import { RocksTransactionLogStore } from './RocksTransactionLogStore.ts';
/**
* This module is responsible for the binary representation of audit records in an efficient form.
* This includes a custom key encoder that specifically encodes arrays with the first element (timestamp) as a
* 64-bit float, second (table id) as a 32-unsigned int, and third using standard ordered-binary encoding
*
* This also defines a binary representation for the audit records themselves which is:
* 1 or 2 bytes: action, describes the action of this record and any flags for which other parts are included
* tableId
* recordId
* origin version
* previous local version
* 1 or 2 bytes: position of end of the username section. 0 if there is no username
* 2 or 4 bytes: node-id
* 8 bytes (optional): last version timestamp (allows for backwards traversal through history of a record)
* username
* remaining bytes (optional, not included for deletes/invalidation): the record itself, using the same encoding as its primary store
*/
initSync();
export type AuditRecord = {
version?: number;
localTime?: number; // only to be used by LMDB (from the key)
type: string;
encodedRecord: Buffer;
extendedType: number;
residencyId: number;
previousResidencyId: number;
expiresAt: Date | null;
originatingOperation: string;
tableId: number;
recordId: number;
previousVersion: number;
user?: string;
nodeId?: number;
previousNodeId?: number;
previousAdditionalAuditRefs?: Array<{ version: number; nodeId: number }>;
endTxn?: boolean;
structureVersion?: number;
getBinaryRecordId?: any;
};
const ENTRY_HEADER = Buffer.alloc(2816); // this is sized to be large enough for the maximum key size (1976) plus large usernames. We may want to consider some limits on usernames to ensure this all fits
export const ENTRY_DATAVIEW = new DataView(ENTRY_HEADER.buffer, ENTRY_HEADER.byteOffset, 2816);
export const transactionKeyEncoder = {
writeKey(key, buffer, position) {
if (key === LAST_TIMESTAMP_PLACEHOLDER) {
buffer.set(LAST_TIMESTAMP_PLACEHOLDER, position);
return position + 8;
}
if (typeof key === 'number') {
const dataView =
buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength));
dataView.setFloat64(position, key);
return position + 8;
} else {
return writeKey(key, buffer, position);
}
},
readKey(buffer, start, end) {
if (buffer[start] === 66) {
const dataView =
buffer.dataView || (buffer.dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength));
// Without this bounds check, a truncated key buffer escapes as RangeError up
// through lmdb-js's iterator and lands as an uncaughtException on a later tick,
// stalling outgoing replication for the affected (peer, db) pair.
if (start + 8 > buffer.byteLength) {
harperLogger.warn('Audit key buffer too short for float64 read; returning NaN sentinel', {
start,
byteLength: buffer.byteLength,
});
return NaN;
}
return dataView.getFloat64(start);
} else {
return readKey(buffer, start, end);
}
},
};
export const AUDIT_STORE_OPTIONS = {
encoder: {
needsStableBuffer: true,
encode: (auditRecord: AuditRecord) =>
auditRecord && (auditRecord instanceof Uint8Array ? auditRecord : createAuditEntry(auditRecord)),
decode: (encoding: Buffer) => readAuditEntry(encoding),
},
keyEncoder: transactionKeyEncoder,
};
export let auditRetention = convertToMS(envGet(CONFIG_PARAMS.LOGGING_AUDITRETENTION)) || 86400 * 1000;
const MAX_DELETES_PER_CLEANUP = 1000;
const FLOAT_TARGET = new Float64Array(1);
const FLOAT_BUFFER = new Uint8Array(FLOAT_TARGET.buffer);
let DEFAULT_AUDIT_CLEANUP_DELAY = 10000; // default delay of 10 seconds
let timestampErrored = false;
export function openAuditStore(rootStore) {
let auditStore;
if (rootStore instanceof RocksDatabase) {
auditStore = new RocksTransactionLogStore(rootStore);
auditStore.env = {};
} else {
auditStore = rootStore.openDB(AUDIT_STORE_NAME, {
create: false,
...AUDIT_STORE_OPTIONS,
});
if (!auditStore) {
// this means we are creating a new audit store. Initialize with the last removed timestamp (we don't want to put this in legacy audit logs since we don't know if they have had deletions or not).
auditStore = rootStore.openDB(AUDIT_STORE_NAME, AUDIT_STORE_OPTIONS);
updateLastRemoved(auditStore, 1);
}
const superGetRange = auditStore.getRange.bind(auditStore);
auditStore.getRange = function (options) {
if (options.values === false) return superGetRange(options); // getKeys shouldn't be modified
return superGetRange(options).map(({ key, value }) => {
value.key = value.localTime = key;
return value;
});
};
}
rootStore.auditStore = auditStore;
auditStore.rootStore = rootStore;
auditStore.tableStores = [];
const deleteCallbacks = [];
auditStore.addDeleteRemovalCallback = function (tableId, table, callback) {
deleteCallbacks[tableId] = callback;
auditStore.tableStores[tableId] = table;
auditStore.deleteCallbacks = deleteCallbacks;
return {
remove() {
delete deleteCallbacks[tableId];
},
};
};
let pendingCleanup = null;
let lastCleanupResolution: Promise<void>;
let cleanupPriority = 0;
let auditCleanupDelay = DEFAULT_AUDIT_CLEANUP_DELAY;
onStorageReclamation(rootStore.path, (priority) => {
cleanupPriority = priority; // update the priority
if (priority) {
// and if we have a priority, schedule cleanup soon
return scheduleAuditCleanup(100);
}
});
function scheduleAuditCleanup(newCleanupDelay?: number): Promise<void> {
if (auditStore instanceof RocksTransactionLogStore) {
auditStore.rootStore.purgeLogs({
before: Date.now() - auditRetention / (1 + cleanupPriority * cleanupPriority),
});
return;
}
if (newCleanupDelay) auditCleanupDelay = newCleanupDelay;
clearTimeout(pendingCleanup);
const resolution = new Promise<void>((resolve) => {
pendingCleanup = setTimeout(async () => {
await lastCleanupResolution;
lastCleanupResolution = resolution;
// query for audit entries that are old
if (auditStore.rootStore.status === 'closed' || auditStore.rootStore.status === 'closing') return;
let deleted = 0;
let committed: Promise<void>;
let lastKey: any;
try {
for (const auditRecord of auditStore.getRange({
start: 1, // must not be zero or it will be interpreted as null and overlap with symbols in search
snapshot: false,
end: Date.now() - auditRetention / (1 + cleanupPriority * cleanupPriority), // remove up until the audit retention time, reducing audit retention time if cleanup is higher priority
})) {
try {
committed = removeAuditEntry(auditStore, auditRecord);
} catch (error) {
harperLogger.warn('Error removing audit entry', error);
}
lastKey = auditRecord.key;
await new Promise(setImmediate);
if (++deleted >= MAX_DELETES_PER_CLEANUP) {
// limit the amount we cleanup per event turn so we don't use too much memory/CPU
auditCleanupDelay = 10; // and keep trying very soon
break;
}
}
await committed;
} finally {
if (deleted === 0) {
// if we didn't delete anything, we can increase the delay (double until we get to one tenth of the retention time)
auditCleanupDelay = Math.min(auditCleanupDelay << 1, auditRetention / 10);
} else {
// if we did delete something, update our updates since timestamp
updateLastRemoved(auditStore, lastKey);
// and do updates faster
if (auditCleanupDelay > 100) auditCleanupDelay = auditCleanupDelay >> 1;
}
resolve(undefined);
scheduleAuditCleanup();
}
// we can run this pretty frequently since there is very little overhead to these queries
}, auditCleanupDelay).unref();
});
return resolution;
}
auditStore.scheduleAuditCleanup = scheduleAuditCleanup;
if (getWorkerIndex() === getWorkerCount() - 1) {
scheduleAuditCleanup();
}
if (getWorkerIndex() === 0 && !timestampErrored) {
// make sure the timestamp is valid
for (const time of auditStore.getKeys({ reverse: true, limit: 1 })) {
if (time > Date.now()) {
timestampErrored = true;
harperLogger.error(
'The current time is before the last recorded entry in the audit log. Time reversal can undermine the integrity of data tracking and certificate validation and the time must be corrected.'
);
}
}
}
return auditStore;
}
export function removeAuditEntry(auditStore: any, auditRecord: AuditRecord): Promise<void> {
if (auditRecord.type === 'delete') {
// if this is a delete, we remove the delete entry from the primary table
// at the same time so the audit table the primary table are in sync, assuming the entry matches this audit record version
const tableId = auditRecord.tableId;
const primaryStore = auditStore.tableStores[auditRecord.tableId];
if (primaryStore?.getEntry(auditRecord.recordId)?.version === auditRecord.version)
auditStore.deleteCallbacks?.[tableId]?.(auditRecord.recordId, auditRecord.version);
}
return auditStore.remove(auditRecord.key);
}
function updateLastRemoved(auditStore, lastKey) {
FLOAT_TARGET[0] = lastKey;
auditStore.put(Symbol.for('last-removed'), FLOAT_BUFFER);
}
export function getLastRemoved(auditStore) {
const lastRemoved = auditStore.get(Symbol.for('last-removed'));
if (lastRemoved) {
FLOAT_BUFFER.set(lastRemoved);
return FLOAT_TARGET[0];
}
}
export function setAuditRetention(retentionTime, defaultDelay = DEFAULT_AUDIT_CLEANUP_DELAY) {
auditRetention = retentionTime;
DEFAULT_AUDIT_CLEANUP_DELAY = defaultDelay;
}
/**
* One-shot purge of transaction-log files already older than the audit retention window,
* intended to run during startup/recovery before transaction-log replay. The steady-state
* cleanup loop (scheduleAuditCleanup) only starts once a worker reaches steady state, so a node
* that crash-loops during recovery never purges and its aged backlog only grows, enlarging the
* next replay/full-copy. Safe to run before replay: the native purge only deletes log files
* entirely before the last-flushed-to-RocksDB position, so unflushed entries that replay still
* needs are never removed. Returns the names of the purged files. See harper#1115.
*/
export function purgeAgedLogs(rootStore: RocksDatabase): string[] {
return rootStore.purgeLogs({ before: Date.now() - auditRetention });
}
const HAS_RECORD = 16;
const HAS_PARTIAL_RECORD = 32; // will be used for CRDTs
const PUT = 1;
const DELETE = 2;
const MESSAGE = 3;
const INVALIDATE = 4;
const PATCH = 5;
const RELOCATE = 6;
const STRUCTURES = 7;
export const ACTION_32_BIT = 14;
export const ACTION_64_BIT = 15;
/** Used to indicate we have received a remote local time update */
export const REMOTE_SEQUENCE_UPDATE = 11;
export const HAS_CURRENT_RESIDENCY_ID = 512;
export const HAS_PREVIOUS_RESIDENCY_ID = 1024;
export const HAS_ORIGINATING_OPERATION = 2048;
export const HAS_EXPIRATION_EXTENDED_TYPE = 0x1000;
export const HAS_BLOBS = 0x2000;
export const HAS_ADDITIONAL_AUDIT_REFS = 0x4000;
const EVENT_TYPES = {
put: PUT | HAS_RECORD,
[PUT]: 'put',
delete: DELETE,
[DELETE]: 'delete',
message: MESSAGE | HAS_RECORD,
[MESSAGE]: 'message',
invalidate: INVALIDATE | HAS_PARTIAL_RECORD,
[INVALIDATE]: 'invalidate',
patch: PATCH | HAS_PARTIAL_RECORD,
[PATCH]: 'patch',
relocate: RELOCATE,
[RELOCATE]: 'relocate',
structures: STRUCTURES,
[STRUCTURES]: 'structures',
remoteSequenceUpdate: REMOTE_SEQUENCE_UPDATE,
[REMOTE_SEQUENCE_UPDATE]: 'remoteSequenceUpdate',
};
const ORIGINATING_OPERATIONS = {
insert: 1,
update: 2,
upsert: 3,
1: 'insert',
2: 'update',
3: 'upsert',
};
/**
* Creates a binary audit entry
* @param txnTime
* @param tableId
* @param recordId
* @param previousVersion
* @param nodeId
* @param user
* @param type
* @param encodedRecord
* @param extendedType
* @param residencyId
* @param previousResidencyId
*/
export function createAuditEntry(auditRecord: AuditRecord, start = 0) {
const {
version,
tableId,
recordId,
previousVersion,
nodeId,
user,
type,
encodedRecord,
extendedType,
residencyId,
previousResidencyId,
expiresAt,
originatingOperation,
previousAdditionalAuditRefs,
} = auditRecord;
const action = EVENT_TYPES[type];
if (!action) {
throw new Error(`Invalid audit entry type ${type}`);
}
let position = start + 1;
if (previousVersion) {
if (previousVersion > 1) ENTRY_DATAVIEW.setFloat64(start, previousVersion);
else ENTRY_HEADER.set(PREVIOUS_TIMESTAMP_PLACEHOLDER, start);
position = start + 9;
}
if (extendedType) {
if (extendedType & 0xff) {
throw new Error('Illegal extended type');
}
position += 3;
}
writeInt(nodeId);
writeInt(tableId);
writeValue(recordId);
// TODO: Once we support multiple format versions, we can conditionally write the version (and the previousResidencyId)
// if (formatVersion === 1) {
ENTRY_DATAVIEW.setFloat64(position, version);
position += 8;
if (extendedType & HAS_CURRENT_RESIDENCY_ID) writeInt(residencyId);
if (extendedType & HAS_PREVIOUS_RESIDENCY_ID) writeInt(previousResidencyId);
if (extendedType & HAS_EXPIRATION_EXTENDED_TYPE) {
ENTRY_DATAVIEW.setFloat64(position, expiresAt);
position += 8;
}
if (extendedType & HAS_ORIGINATING_OPERATION) {
writeInt(ORIGINATING_OPERATIONS[originatingOperation]);
}
if (extendedType & HAS_ADDITIONAL_AUDIT_REFS) {
if (previousAdditionalAuditRefs && previousAdditionalAuditRefs.length > 0) {
ENTRY_HEADER[position++] = previousAdditionalAuditRefs.length;
for (const ref of previousAdditionalAuditRefs) {
ENTRY_DATAVIEW.setFloat64(position, ref.version);
position += 8;
writeInt(ref.nodeId);
}
} else {
ENTRY_HEADER[position++] = 0;
}
}
if (user) writeValue(user);
else ENTRY_HEADER[position++] = 0;
if (extendedType) ENTRY_DATAVIEW.setUint32(start + (previousVersion ? 8 : 0), action | extendedType | 0xc0000000);
else ENTRY_HEADER[start + (previousVersion ? 8 : 0)] = action;
const header = ENTRY_HEADER.subarray(0, position);
if (encodedRecord) {
return Buffer.concat([header, encodedRecord]);
} else return header;
function writeValue(value) {
const valueLengthPosition = position;
position += 1;
position = writeKey(value, ENTRY_HEADER, position);
const keyLength = position - valueLengthPosition - 1;
if (keyLength > 0x7f) {
if (keyLength > 0x3fff) {
harperLogger.error('Key or username was too large for audit entry', value);
position = valueLengthPosition + 1;
ENTRY_HEADER[valueLengthPosition] = 0;
} else {
// requires two byte length header, need to move the value/key to make room for it
ENTRY_HEADER.copyWithin(valueLengthPosition + 2, valueLengthPosition + 1, position);
// now write a two-byte length header
ENTRY_DATAVIEW.setUint16(valueLengthPosition, keyLength | 0x8000);
// must adjust the position by one since we moved everything one position
position++;
}
} else {
// one byte length header, as expected
ENTRY_HEADER[valueLengthPosition] = keyLength;
}
}
function writeInt(number) {
if (number < 128) {
ENTRY_HEADER[position++] = number;
} else if (number < 0x4000) {
ENTRY_DATAVIEW.setUint16(position, number | 0x8000);
position += 2;
} else if (number < 0x3f000000) {
ENTRY_DATAVIEW.setUint32(position, number | 0xc0000000);
position += 4;
} else {
ENTRY_HEADER[position] = 0xff;
ENTRY_DATAVIEW.setUint32(position + 1, number);
position += 5;
}
}
}
/**
* Reads a audit entry from binary data
* @param buffer
* @param start
* @param end
*/
export function readAuditEntry(buffer: Uint8Array, start = 0, end = undefined): AuditRecord {
try {
const decoder =
buffer.decoder || (buffer.decoder = new Decoder(buffer.buffer, buffer.byteOffset, buffer.byteLength));
decoder.position = start;
let previousVersion;
if (buffer[decoder.position] == 66) {
// 66 is the first byte in a date double.
previousVersion = decoder.readFloat64();
}
const action = decoder.readInt();
const nodeId = decoder.readInt();
const tableId = decoder.readInt();
let length = decoder.readInt();
// A corrupt length field (e.g., a 0xff-prefixed uint32) would otherwise push
// decoder.position hundreds of megabytes past the buffer; the next readFloat64
// then throws with the bogus position in the message. Failing fast here keeps
// the throw inside this try/catch so we surface a sentinel instead.
if (length < 0 || decoder.position + length > buffer.byteLength) {
throw new RangeError(
`Audit entry recordId length ${length} exceeds remaining buffer (position ${decoder.position}, byteLength ${buffer.byteLength})`
);
}
const recordIdStart = decoder.position;
const recordIdEnd = (decoder.position += length);
// TODO: Once we support multiple format versions, we can conditionally read the version (and the previousResidencyId)
const version = decoder.readFloat64();
let residencyId, previousResidencyId, expiresAt, originatingOperation, previousAdditionalAuditRefs;
if (action & HAS_CURRENT_RESIDENCY_ID) {
residencyId = decoder.readInt();
}
if (action & HAS_PREVIOUS_RESIDENCY_ID) {
previousResidencyId = decoder.readInt();
}
if (action & HAS_EXPIRATION_EXTENDED_TYPE) {
expiresAt = decoder.readFloat64();
}
if (action & HAS_ORIGINATING_OPERATION) {
const operationId = decoder.readInt();
originatingOperation = ORIGINATING_OPERATIONS[operationId];
}
if (action & HAS_ADDITIONAL_AUDIT_REFS) {
const count = buffer[decoder.position++];
if (count > 0) {
previousAdditionalAuditRefs = [];
for (let i = 0; i < count; i++) {
const refVersion = decoder.readFloat64();
const refNodeId = decoder.readInt();
previousAdditionalAuditRefs.push({ version: refVersion, nodeId: refNodeId });
}
}
}
length = decoder.readInt();
if (length < 0 || decoder.position + length > buffer.byteLength) {
throw new RangeError(
`Audit entry username length ${length} exceeds remaining buffer (position ${decoder.position}, byteLength ${buffer.byteLength})`
);
}
const usernameStart = decoder.position;
const usernameEnd = (decoder.position += length);
let value: any;
return {
type: EVENT_TYPES[action & 7],
tableId,
nodeId,
get recordId() {
// The recordId is decoded lazily and lives outside readAuditEntry's try/catch,
// so a corrupt recordId region would otherwise escape as an uncaught RangeError
// on property access. Catch and return undefined; callers already treat missing
// recordId as a skip-eligible entry.
try {
// use a subarray to protect against the underlying buffer being modified
return readKey(buffer.subarray(0, recordIdEnd), recordIdStart, recordIdEnd);
} catch (error) {
harperLogger.warn('Failed to decode audit recordId; treating as corrupt', error);
return undefined;
}
},
getBinaryRecordId() {
return buffer.subarray(recordIdStart, recordIdEnd);
},
version,
previousVersion,
get user() {
try {
return usernameEnd > usernameStart
? readKey(buffer.subarray(0, usernameEnd), usernameStart, usernameEnd)
: undefined;
} catch (error) {
harperLogger.warn('Failed to decode audit username; treating as corrupt', error);
return undefined;
}
},
get encoded() {
return start ? buffer.subarray(start, end) : buffer;
},
get size() {
return start !== undefined && end !== undefined ? end - start : buffer.byteLength;
},
getValue(store, fullRecord?, auditTime?) {
if (action & HAS_RECORD || (action & HAS_PARTIAL_RECORD && !fullRecord)) {
if (!value) {
value = decodeFromDatabase(
// the audit value has no on-disk timestamp/metadata prefix (the audit entry carries
// its own time), so skip the prefix heuristic — otherwise a classic record whose
// structure-id byte is 66 (0x42) is misread as a rocksdb timestamp. See RecordEncoder.decode.
() => store.decoder.decode(buffer.subarray(decoder.position, end), { noMetadata: true }),
store.rootStore
);
}
return value;
}
if (action & HAS_PARTIAL_RECORD && auditTime) {
const recordId = this.recordId;
return getRecordAtTime(store.getEntry(recordId), auditTime, store, tableId, recordId);
} // TODO: If we store a partial and full record, may need to read both sequentially
},
getBinaryValue() {
return buffer.subarray(decoder.position, end);
},
extendedType: action,
residencyId,
previousResidencyId,
expiresAt,
originatingOperation,
previousAdditionalAuditRefs,
};
} catch (error) {
harperLogger.error('Reading audit entry error', error, buffer);
return createCorruptAuditSentinel(buffer, start, end);
}
}
/**
* Build a structurally complete audit record for an entry that failed to decode. The fields
* mirror the happy-path shape so downstream consumers that access (e.g.) `getValue` or the
* `recordId` getter don't blow up with a `TypeError: not a function` / `undefined.is(...)`
* after the header decode already failed. Consumers identify these by the undefined
* `tableId`/`type` (the same signal lmdb has produced from this catch since before this
* change) and skip them — `classifyAuditEntryForReplay` calls them out as `corrupt-header`,
* and the dispatch loops in Table.ts / transactionBroadcast.ts filter via tableId guards.
*/
function createCorruptAuditSentinel(buffer: Uint8Array, start: number, end: number | undefined): AuditRecord {
return {
type: undefined,
tableId: undefined,
nodeId: undefined,
recordId: undefined,
version: undefined,
previousVersion: undefined,
user: undefined,
extendedType: undefined,
residencyId: undefined,
previousResidencyId: undefined,
expiresAt: undefined,
originatingOperation: undefined,
previousAdditionalAuditRefs: undefined,
get encoded() {
return start ? buffer.subarray(start, end) : buffer;
},
get size() {
return start !== undefined && end !== undefined ? end - start : buffer.byteLength;
},
getBinaryRecordId() {
return undefined;
},
getValue() {
return undefined;
},
getBinaryValue() {
return undefined;
},
} as any;
}
export class Decoder extends DataView<ArrayBufferLike> {
position = 0;
readInt() {
let number;
number = this.getUint8(this.position++);
if (number >= 0x80) {
if (number >= 0xc0) {
if (number === 0xff) {
number = this.getUint32(this.position);
this.position += 4;
return number;
}
number = this.getUint32(this.position - 1) & 0x3fffffff;
this.position += 3;
return number;
}
number = this.getUint16(this.position - 1) & 0x7fff;
this.position++;
return number;
}
return number;
}
readFloat64() {
try {
const value = this.getFloat64(this.position);
this.position += 8;
return value;
} catch (error) {
error.message = `Error reading float64: ${error.message} at position ${this.position}`;
throw error;
}
}
}