task(#920): B3 blob-to-rows migration with a readback gate that can fail

Per key, once: count the blob's records, convert it to rows off the UI
isolate, insert them in one transaction, read the stored count back, and
only then mark the key as reading from rows.

**The blob is never deleted**, not even on a verified match. Rows become the
read and write path; the blob is left untouched. Reclaiming that space is a
separate owner-gated step, because the moment the delete runs is the moment
a mistake stops being recoverable, and a downgrade to an older build still
finds its history where it looks for it.

Every failure leaves the blob authoritative and says so loudly: an
undecodable blob, a converter that throws, a count mismatch, a failed
insert. None of them retries, because a count that did not match once will
not match on a loop.

The migrated set is its own marker key, not "this key has rows". Those are
different statements: a key can have rows and an incomplete migration at the
same time, and treating rows as proof of completeness is how half a history
would read as a whole one. An unreadable marker treats every key as
unmigrated, which is the safe direction because the blobs are all still
there.

The first version of the readback gate was a check that could not fail. It
compared the stored row count against the number of rows the converter
produced, so a converter that silently dropped records matched itself and
passed. Caught by a test built to drop a record. The blob's element count is
now measured by a separate decode that does nothing but count, so the
converter cannot report its own homework: `rows.length` must equal that
count, and the stored count must equal the distinct identities. Both can
fail independently, and together they close the loop from blob element to
stored row.

Duplicate identities in a source blob are deduped rather than refused. The
old merge should not have produced any, but the legacy slot-index adoption
path (#194) could have, and that must not read as data loss.

Tests: 15, nine of them failure paths. The lossy-converter case is the one
that caught the broken gate.

Citadel: meshcore-open-x9q
Refs #920, #917, #334

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
fix/754-discovery-per-radio
diffwireauto 1 month ago committed by Benjamin Wiechel
parent 4d4d44a61a
commit d20d567bba

@ -243,6 +243,179 @@ class BlobStore {
return rows.map((r) => r.read(key)!).toSet();
}
/// Holds the set of store keys whose blob has been moved into rows.
///
/// A separate marker rather than "this key has rows" because those are not
/// the same statement. A key can have rows and an incomplete migration at
/// the same time, and treating rows as proof of completeness is how a
/// half-migrated history would read as a whole one.
static const String migratedMarkerKey = '__msgrows_migrated';
Set<String>? _migratedKeys;
Future<Set<String>> _loadMigratedKeys() async {
if (_migratedKeys != null) return _migratedKeys!;
final raw = await read(migratedMarkerKey);
if (raw == null || raw.isEmpty) return _migratedKeys = <String>{};
try {
return _migratedKeys = (jsonDecode(raw) as List<dynamic>)
.cast<String>()
.toSet();
} catch (e) {
// Refuse to guess. An unreadable marker means every key is treated as
// unmigrated, which is the safe direction: the blobs are all still there.
appLogger.error(
'Could not decode the row-migration marker, treating every key as '
'unmigrated (the blobs are intact): $e',
tag: 'Storage',
);
return _migratedKeys = <String>{};
}
}
Future<void> _markMigrated(String storeKey) async {
await synchronized(migratedMarkerKey, () async {
final keys = await _loadMigratedKeys();
if (keys.contains(storeKey)) return;
keys.add(storeKey);
await write(migratedMarkerKey, jsonEncode(keys.toList()));
});
}
/// Whether [storeKey] reads from rows. False means its blob is still the
/// source of truth.
Future<bool> isOnMessageRows(String storeKey) async =>
(await _loadMigratedKeys()).contains(storeKey);
@visibleForTesting
void resetMigratedKeyCache() => _migratedKeys = null;
/// Moves [storeKey]'s blob into rows, once, and reports whether the key is
/// now on rows (#920).
///
/// Idempotent and safe to call on every save and load: a key already marked
/// migrated returns immediately from an in-memory set.
///
/// The gate, in order:
///
/// 1. No blob, or an empty one, means there is nothing to move. The key is
/// marked migrated and starts on rows.
/// 2. The blob's top-level element count is measured **independently of
/// [toRows]**, by a decode that does nothing but count. Asking the
/// converter how many records it saw would be a check that cannot fail;
/// the first version of this did exactly that and a deliberately lossy
/// converter sailed through it.
/// 3. [toRows] decodes the blob **off the UI isolate**, because it is the
/// same multi-megabyte decode #919 moved. If it throws, nothing is
/// written and the key stays on the blob. It must return one row per
/// source element; a shortfall against step 2 refuses the migration.
/// 4. The rows are inserted in one transaction, so a failure part-way leaves
/// no partial set behind.
/// 5. The stored row count is read back and compared to the number of
/// DISTINCT merge keys. Only an exact match marks the key migrated.
///
/// Steps 2 and 5 together close the loop from blob element to stored row,
/// and either can fail on its own.
///
/// **The blob is never deleted**, not even on a verified match. Rows become
/// the read and write path and the blob is left untouched. Reclaiming that
/// space is deliberately a separate, owner-gated step: the moment the delete
/// runs is the moment a mistake stops being recoverable.
///
/// A mismatch keeps the key on the blob path, which still works, and says so
/// loudly. It does not retry: a count that did not match once will not match
/// on a loop, and a retry would just hammer the same keys.
Future<bool> ensureMessageRowsMigrated(
String storeKey,
BlobToRowsCallback toRows,
) async {
if (await isOnMessageRows(storeKey)) return true;
return synchronized(storeKey, () async {
// Re-check inside the lock: a concurrent save may have migrated it while
// this call waited its turn.
if (await isOnMessageRows(storeKey)) return true;
final blob = await readWithPrefsFallback(storeKey);
if (blob == null || blob.isEmpty) {
await _markMigrated(storeKey);
return true;
}
final int sourceCount;
final List<MessageRowInput> rows;
try {
sourceCount = await compute(countJsonArrayElements, blob);
rows = await compute(toRows, blob);
} catch (e, stack) {
appLogger.error(
'Row migration of $storeKey FAILED while decoding its ${blob.length} '
'char blob: $e. Nothing was written and the key still reads from the '
'blob.\n$stack',
tag: 'Storage',
);
return false;
}
if (rows.length != sourceCount) {
appLogger.error(
'Row migration of $storeKey REFUSED: the blob holds $sourceCount '
'records but the converter produced ${rows.length} rows. Nothing '
'was written. The blob is kept and still the source of truth.',
tag: 'Storage',
);
return false;
}
final deduped = <String, MessageRowInput>{};
for (final r in rows) {
deduped[r.mergeKey] = r;
}
if (deduped.length != rows.length) {
appLogger.warn(
'Row migration of $storeKey: the blob held ${rows.length} records '
'but only ${deduped.length} distinct identities, so '
'${rows.length - deduped.length} were duplicates. Comparing against '
'the distinct count.',
tag: 'Storage',
);
}
try {
await upsertMessageRows(storeKey, deduped.values);
} catch (e, stack) {
appLogger.error(
'Row migration of $storeKey FAILED while inserting '
'${deduped.length} rows: $e. The blob is untouched and still the '
'source of truth for this key.\n$stack',
tag: 'Storage',
);
return false;
}
final stored = await messageRowCount(storeKey);
if (stored != deduped.length) {
appLogger.error(
'Row migration of $storeKey REFUSED: read back $stored rows but '
'expected ${deduped.length}. The blob is kept, nothing is deleted, '
'and this key keeps reading from the blob. Rows written so far are '
'left in place and are harmless because the key is not marked '
'migrated.',
tag: 'Storage',
);
return false;
}
await _markMigrated(storeKey);
appLogger.info(
'Row migration of $storeKey: ${deduped.length} messages now stored as '
'rows. The blob is retained untouched (#920).',
tag: 'Storage',
);
return true;
});
}
/// Read-modify-write for a bulk key with the decode, merge and re-encode run
/// **off the UI isolate** (#919).
///
@ -517,6 +690,23 @@ class BlobStore {
}
}
/// Counts the top-level elements of a JSON array, and nothing else.
///
/// Exists so [BlobStore.ensureMessageRowsMigrated] can learn how many records
/// a blob holds without asking the converter it is checking. Top-level so
/// `compute` can send it.
int countJsonArrayElements(String json) =>
(jsonDecode(json) as List<dynamic>).length;
/// Turns one history blob into per-message rows, run on another isolate.
///
/// Must be a top-level or static function so `compute` can send it. Takes the
/// raw blob JSON and returns one [MessageRowInput] per message. Throwing is the
/// correct response to an undecodable blob: [BlobStore.ensureMessageRowsMigrated]
/// turns that into a refused migration and leaves the blob as the source of
/// truth.
typedef BlobToRowsCallback = List<MessageRowInput> Function(String blobJson);
/// One message on its way into [MessageRecords] (#920).
///
/// A plain record rather than a drift companion so the stores can build it

@ -0,0 +1,245 @@
import 'dart:convert';
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/storage/drift/blob_store.dart';
import 'package:meshcore_open/storage/drift/offband_database.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// #920 B3: moving a history blob into rows. The gate matters more than the
/// move: every failure path here has to leave the blob as the source of truth,
/// because the blob is the only copy of someone's messages.
void main() {
late OffbandDatabase db;
late BlobStore blobs;
setUp(() async {
SharedPreferences.setMockInitialValues({});
PrefsManager.reset();
await PrefsManager.initialize();
db = OffbandDatabase(NativeDatabase.memory());
blobs = BlobStore(db);
BlobStore.overrideForTest(blobs);
});
tearDown(() async {
await db.close();
BlobStore.clearTestOverride();
});
String blobOf(List<int> ids) => jsonEncode([
for (final i in ids) {'messageId': 'id$i', 'timestamp': 1000 + i},
]);
test('moves a blob into rows and marks the key migrated', () async {
await blobs.write('k', blobOf([1, 2, 3]));
final ok = await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob);
expect(ok, isTrue);
expect(await blobs.messageRowCount('k'), 3);
expect(await blobs.isOnMessageRows('k'), isTrue);
});
test('NEVER deletes the blob, even on a verified match', () async {
final original = blobOf([1, 2]);
await blobs.write('k', original);
await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob);
expect(
await blobs.read('k'),
original,
reason: 'reclaiming the space is a separate owner-gated step (#920)',
);
});
test('rows carry the payload through unchanged', () async {
await blobs.write('k', blobOf([7]));
await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob);
expect(await blobs.messagePayloads('k'), [
'{"messageId":"id7","timestamp":1007}',
]);
});
test('a second call is a no-op and does not re-import', () async {
await blobs.write('k', blobOf([1, 2]));
await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob);
// Something arrives and is saved as a row after the migration.
await blobs.upsertMessageRows('k', const [
MessageRowInput(mergeKey: 'id:new', timestampMs: 9999, payload: '{}'),
]);
final ok = await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob);
expect(ok, isTrue);
expect(
await blobs.messageRowCount('k'),
3,
reason: 'a repeat migration must not touch the rows that exist',
);
});
test('an absent blob marks the key migrated with no rows', () async {
final ok = await blobs.ensureMessageRowsMigrated('fresh', rowsFromTestBlob);
expect(ok, isTrue);
expect(await blobs.isOnMessageRows('fresh'), isTrue);
expect(await blobs.messageRowCount('fresh'), 0);
});
test('an empty blob marks the key migrated with no rows', () async {
await blobs.write('k', '');
final ok = await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob);
expect(ok, isTrue);
expect(await blobs.isOnMessageRows('k'), isTrue);
});
group('failure paths all keep the blob authoritative', () {
test('an undecodable blob refuses the migration', () async {
const corrupt = '{not json at all';
await blobs.write('k', corrupt);
final ok = await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob);
expect(ok, isFalse);
expect(await blobs.isOnMessageRows('k'), isFalse);
expect(await blobs.read('k'), corrupt);
expect(await blobs.messageRowCount('k'), 0);
});
test('a decoder that throws refuses the migration', () async {
await blobs.write('k', blobOf([1]));
final ok = await blobs.ensureMessageRowsMigrated('k', _alwaysThrows);
expect(ok, isFalse);
expect(await blobs.isOnMessageRows('k'), isFalse);
expect(await blobs.read('k'), blobOf([1]));
});
test('a decoder that drops records refuses on the count gate', () async {
// The case the readback gate exists for: the decode "succeeded" but
// produced fewer messages than the blob held. Marking this migrated
// would silently shorten someone's history.
await blobs.write('k', blobOf([1, 2, 3, 4, 5]));
final ok = await blobs.ensureMessageRowsMigrated('k', _dropsOneRecord);
expect(
ok,
isFalse,
reason: 'fewer rows than records must never be accepted',
);
expect(await blobs.isOnMessageRows('k'), isFalse);
expect(
await blobs.read('k'),
blobOf([1, 2, 3, 4, 5]),
reason: 'the blob is the only copy of those messages',
);
});
test('a refused key can still be read from its blob afterwards', () async {
await blobs.write('k', blobOf([1, 2]));
await blobs.ensureMessageRowsMigrated('k', _alwaysThrows);
// The whole point of refusing rather than throwing: the app keeps
// working on the old path.
expect(await blobs.read('k'), blobOf([1, 2]));
expect(await blobs.isOnMessageRows('k'), isFalse);
});
});
test('duplicate identities in a blob are deduped, not refused', () async {
// A blob built by the old merge should not contain duplicates, but the
// legacy-key adoption path could have produced them. Comparing against
// the DISTINCT count means that does not read as data loss.
await blobs.write(
'k',
jsonEncode([
{'messageId': 'id1', 'timestamp': 1001},
{'messageId': 'id1', 'timestamp': 1001},
{'messageId': 'id2', 'timestamp': 1002},
]),
);
final ok = await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob);
expect(ok, isTrue);
expect(await blobs.messageRowCount('k'), 2);
});
test('the marker survives a reopen of the same database', () async {
await blobs.write('k', blobOf([1]));
await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob);
// Same database, fresh in-memory cache: the marker has to be on disk.
blobs.resetMigratedKeyCache();
expect(await blobs.isOnMessageRows('k'), isTrue);
});
test('an unreadable marker treats every key as unmigrated', () async {
await blobs.write('k', blobOf([1]));
await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob);
await blobs.write(BlobStore.migratedMarkerKey, 'not json');
blobs.resetMigratedKeyCache();
// Safe direction: the blobs are all still there, so re-reading them is
// correct, where trusting a corrupt marker would hide histories.
expect(await blobs.isOnMessageRows('k'), isFalse);
});
test('migrating one key does not mark or touch another', () async {
await blobs.write('a', blobOf([1, 2]));
await blobs.write('b', blobOf([3]));
await blobs.ensureMessageRowsMigrated('a', rowsFromTestBlob);
expect(await blobs.isOnMessageRows('b'), isFalse);
expect(await blobs.messageRowCount('b'), 0);
expect(await blobs.read('b'), blobOf([3]));
});
test('concurrent calls on one key import it exactly once', () async {
await blobs.write('k', blobOf([1, 2, 3]));
final results = await Future.wait([
blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob),
blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob),
blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob),
]);
expect(results, everyElement(isTrue));
expect(await blobs.messageRowCount('k'), 3);
});
}
/// Stands in for a store's real decoder. Top-level so `compute` can send it.
List<MessageRowInput> rowsFromTestBlob(String blobJson) {
return (jsonDecode(blobJson) as List<dynamic>)
.map((e) => e as Map<String, dynamic>)
.map(
(m) => MessageRowInput(
mergeKey: 'id:${m['messageId']}',
timestampMs: m['timestamp'] as int,
payload: jsonEncode(m),
),
)
.toList();
}
List<MessageRowInput> _alwaysThrows(String blobJson) =>
throw StateError('decoder blew up');
/// Silently loses the last record, which is exactly what the readback gate is
/// there to catch.
List<MessageRowInput> _dropsOneRecord(String blobJson) {
final all = rowsFromTestBlob(blobJson);
return all.sublist(0, all.length - 1);
}
Loading…
Cancel
Save

Powered by TurnKey Linux.