@@ -45,14 +45,8 @@ export function initializeVatState(kvStore, streamStore, vatID) {
45
45
kvStore . set ( `${ vatID } .d.nextID` , `${ FIRST_DEVICE_ID } ` ) ;
46
46
kvStore . set ( `${ vatID } .nextDeliveryNum` , `0` ) ;
47
47
kvStore . set ( `${ vatID } .incarnationNumber` , `1` ) ;
48
- kvStore . set (
49
- `${ vatID } .t.startPosition` ,
50
- `${ JSON . stringify ( streamStore . STREAM_START ) } ` ,
51
- ) ;
52
- kvStore . set (
53
- `${ vatID } .t.endPosition` ,
54
- `${ JSON . stringify ( streamStore . STREAM_START ) } ` ,
55
- ) ;
48
+ kvStore . set ( `${ vatID } .t.startPosition` , `${ streamStore . STREAM_START } ` ) ;
49
+ kvStore . set ( `${ vatID } .t.endPosition` , `${ streamStore . STREAM_START } ` ) ;
56
50
}
57
51
58
52
/**
@@ -486,9 +480,9 @@ export function makeVatKeeper(
486
480
*/
487
481
function * getTranscript ( startPos ) {
488
482
if ( startPos === undefined ) {
489
- startPos = JSON . parse ( getRequired ( `${ vatID } .t.startPosition` ) ) ;
483
+ startPos = Number ( getRequired ( `${ vatID } .t.startPosition` ) ) ;
490
484
}
491
- const endPos = JSON . parse ( getRequired ( `${ vatID } .t.endPosition` ) ) ;
485
+ const endPos = Number ( getRequired ( `${ vatID } .t.endPosition` ) ) ;
492
486
for ( const entry of streamStore . readStream (
493
487
transcriptStream ,
494
488
/** @type { StreamPosition } */ ( startPos ) ,
@@ -504,88 +498,38 @@ export function makeVatKeeper(
504
498
* @param {object } entry The transcript entry to append.
505
499
*/
506
500
function addToTranscript ( entry ) {
507
- const oldPos = JSON . parse ( getRequired ( `${ vatID } .t.endPosition` ) ) ;
501
+ const oldPos = Number ( getRequired ( `${ vatID } .t.endPosition` ) ) ;
508
502
const newPos = streamStore . writeStreamItem (
509
503
transcriptStream ,
510
504
JSON . stringify ( entry ) ,
511
505
oldPos ,
512
506
) ;
513
- kvStore . set ( `${ vatID } .t.endPosition` , `${ JSON . stringify ( newPos ) } ` ) ;
507
+ kvStore . set ( `${ vatID } .t.endPosition` , `${ newPos } ` ) ;
514
508
}
515
509
516
510
/** @returns {StreamPosition } */
517
511
function getTranscriptEndPosition ( ) {
518
- return JSON . parse (
512
+ const endPosition =
519
513
kvStore . get ( `${ vatID } .t.endPosition` ) ||
520
- assert . fail ( 'missing endPosition' ) ,
521
- ) ;
514
+ assert . fail ( 'missing endPosition' ) ;
515
+ return Number ( endPosition ) ;
522
516
}
523
517
524
- /**
525
- * @returns {{ snapshotID: string, startPos: StreamPosition } | undefined }
526
- */
527
- function getLastSnapshot ( ) {
528
- const notation = kvStore . get ( `local.${ vatID } .lastSnapshot` ) ;
529
- if ( ! notation ) {
518
+ function getSnapshotInfo ( ) {
519
+ if ( snapStore ) {
520
+ return snapStore . getSnapshotInfo ( vatID ) ;
521
+ } else {
530
522
return undefined ;
531
523
}
532
- const { snapshotID, startPos } = JSON . parse ( notation ) ;
533
- assert . typeof ( snapshotID , 'string' ) ;
534
- assert ( startPos ) ;
535
- return { snapshotID, startPos } ;
536
524
}
537
525
538
526
function transcriptSnapshotStats ( ) {
539
527
const totalEntries = getTranscriptEndPosition ( ) ;
540
- const lastSnapshot = getLastSnapshot ( ) ;
541
- const snapshottedEntries = lastSnapshot ? lastSnapshot . startPos : 0 ;
528
+ const snapshotInfo = getSnapshotInfo ( ) ;
529
+ const snapshottedEntries = snapshotInfo ? snapshotInfo . endPos : 0 ;
542
530
return { totalEntries, snapshottedEntries } ;
543
531
}
544
532
545
- /**
546
- * Add vatID to consumers of a snapshot.
547
- *
548
- * @param {string } snapshotID
549
- */
550
- function addToSnapshot ( snapshotID ) {
551
- const key = `local.snapshot.${ snapshotID } ` ;
552
- const consumers = JSON . parse ( kvStore . get ( key ) || '[]' ) ;
553
- assert ( Array . isArray ( consumers ) ) ;
554
-
555
- // We can't completely rule out the possibility that
556
- // a vat will use the same snapshot twice in a row.
557
- //
558
- // PERFORMANCE NOTE: we assume consumer lists are short;
559
- // usually length 1. So O(n) search here is better
560
- // than keeping the list sorted.
561
- if ( ! consumers . includes ( vatID ) ) {
562
- consumers . push ( vatID ) ;
563
- kvStore . set ( key , JSON . stringify ( consumers ) ) ;
564
- // console.log('addToSnapshot result:', { vatID, snapshotID, consumers });
565
- }
566
- }
567
-
568
- /**
569
- * Remove vatID from consumers of a snapshot.
570
- *
571
- * @param {string } snapshotID
572
- */
573
- function removeFromSnapshot ( snapshotID ) {
574
- const key = `local.snapshot.${ snapshotID } ` ;
575
- const consumersJSON = kvStore . get ( key ) ;
576
- if ( ! consumersJSON ) {
577
- throw Fail `cannot remove ${ vatID } : ${ key } key not defined` ;
578
- }
579
- const consumers = JSON . parse ( consumersJSON ) ;
580
- assert ( Array . isArray ( consumers ) ) ;
581
- const ix = consumers . indexOf ( vatID ) ;
582
- assert ( ix >= 0 ) ;
583
- consumers . splice ( ix , 1 ) ;
584
- // console.log('removeFromSnapshot done:', { vatID, snapshotID, consumers });
585
- kvStore . set ( key , JSON . stringify ( consumers ) ) ;
586
- return consumers . length ;
587
- }
588
-
589
533
/**
590
534
* Store a snapshot, if given a snapStore.
591
535
*
@@ -597,26 +541,15 @@ export function makeVatKeeper(
597
541
return false ;
598
542
}
599
543
600
- const info = await manager . makeSnapshot ( snapStore ) ;
544
+ const endPosition = getTranscriptEndPosition ( ) ;
545
+ const info = await manager . makeSnapshot ( endPosition , snapStore ) ;
601
546
const {
602
547
hash : snapshotID ,
603
548
rawByteCount,
604
549
rawSaveSeconds,
605
550
compressedByteCount,
606
551
compressSeconds,
607
552
} = info ;
608
- const old = getLastSnapshot ( ) ;
609
- if ( old && old . snapshotID !== snapshotID ) {
610
- if ( removeFromSnapshot ( old . snapshotID ) === 0 ) {
611
- snapStore . deleteSnapshot ( old . snapshotID ) ;
612
- }
613
- }
614
- const endPosition = getTranscriptEndPosition ( ) ;
615
- kvStore . set (
616
- `local.${ vatID } .lastSnapshot` ,
617
- JSON . stringify ( { snapshotID, startPos : endPosition } ) ,
618
- ) ;
619
- addToSnapshot ( snapshotID ) ;
620
553
kernelSlog . write ( {
621
554
type : 'heap-snapshot-save' ,
622
555
vatID,
@@ -630,17 +563,15 @@ export function makeVatKeeper(
630
563
return true ;
631
564
}
632
565
566
+ function deleteSnapshots ( ) {
567
+ if ( snapStore ) {
568
+ snapStore . deleteSnapshots ( vatID ) ;
569
+ }
570
+ }
571
+
633
572
function removeSnapshotAndTranscript ( ) {
634
- const skey = `local.${ vatID } .lastSnapshot` ;
635
573
if ( snapStore ) {
636
- const notation = kvStore . get ( skey ) ;
637
- if ( notation ) {
638
- const { snapshotID } = JSON . parse ( notation ) ;
639
- if ( removeFromSnapshot ( snapshotID ) === 0 ) {
640
- snapStore . deleteSnapshot ( snapshotID ) ;
641
- }
642
- kvStore . delete ( skey ) ;
643
- }
574
+ snapStore . deleteSnapshots ( vatID ) ;
644
575
}
645
576
646
577
const endPos = getRequired ( `${ vatID } .t.endPosition` ) ;
@@ -719,8 +650,8 @@ export function makeVatKeeper(
719
650
vatStats,
720
651
dumpState,
721
652
saveSnapshot,
722
- getLastSnapshot ,
723
- removeFromSnapshot ,
653
+ deleteSnapshots ,
654
+ getSnapshotInfo ,
724
655
removeSnapshotAndTranscript,
725
656
} ) ;
726
657
}
0 commit comments