Repository navigation
Prune dangling alias #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Prune dangling alias #175
Changes from all commits
8ed4c6f
27b1f96
0430463
53447c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| const test = require('brittle') | ||
| const crypto = require('hypercore-crypto') | ||
|
|
||
| const { create } = require('./helpers') | ||
|
|
||
| test('alias - a dangling alias fails the open instead of resolving to nothing', async function (t) { | ||
| const store = await create(t) | ||
|
|
||
| const fine = store.get({ name: 'fine' }) | ||
| await fine.append('ok') | ||
| await fine.close() | ||
|
|
||
| // an alias row whose core is gone, as left behind by storages that deleted | ||
| // cores without their alias | ||
| const dangling = crypto.discoveryKey(crypto.keyPair().publicKey) | ||
| const getAlias = store.storage.getAlias.bind(store.storage) | ||
| store.storage.getAlias = (alias) => (alias.name === 'bee' ? dangling : getAlias(alias)) | ||
|
Comment on lines
+13
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather |
||
|
|
||
| const bee = store.get({ name: 'bee' }) | ||
| const err = await bee.ready().then( | ||
| () => null, | ||
| (err) => err | ||
| ) | ||
| t.ok(err, 'the open fails') | ||
| t.is(err.code, 'STORAGE_EMPTY') | ||
| t.ok(/bee/.test(err.message), 'names the alias: ' + err.message) | ||
| t.is( | ||
| await store.storage.getAlias({ name: 'bee', namespace: store.ns }), | ||
| dangling, | ||
| 'the alias is left alone' | ||
| ) | ||
|
|
||
| const again = store.get({ name: 'fine' }) | ||
| await again.ready() | ||
| t.is(again.length, 1, 'other names still open') | ||
| await again.close() | ||
|
|
||
| await store.close() | ||
| }) | ||
|
|
||
| // a dangling row as left behind by storages that deleted cores without their alias | ||
| function injectDanglingAlias(store, name) { | ||
| const discoveryKey = crypto.discoveryKey(crypto.keyPair().publicKey) | ||
| const createAliasStream = store.storage.createAliasStream.bind(store.storage) | ||
| store.storage.createAliasStream = async function* (namespace) { | ||
| yield* createAliasStream(namespace) | ||
| yield { alias: { name, namespace: store.ns }, discoveryKey } | ||
| } | ||
| return discoveryKey | ||
| } | ||
|
Comment on lines
+42
to
+50
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should add an actual dangling alias instead of monkey patching to pretend there is one.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Helpers should be at the bottom of the file if not nested in the test case. If it doesn't get used above and it's only used below, maybe it should be nested in the test case? Otherwise the bottom of the file is preferable. |
||
|
|
||
| test('alias - audit reports every alias and flags the dangling ones', async function (t) { | ||
| const store = await create(t) | ||
|
|
||
| const fine = store.get({ name: 'fine' }) | ||
| await fine.append('ok') | ||
| await fine.close() | ||
|
|
||
| injectDanglingAlias(store, 'ghost') | ||
| const deleted = [] | ||
| store.storage.deleteAlias = (alias) => { | ||
| deleted.push(alias.name) | ||
| return Promise.resolve(true) | ||
| } | ||
|
Comment on lines
+61
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't, think this needs monkey patching. It can list the aliases afterwards and verify they where deleted instead. |
||
|
|
||
| const seen = [] | ||
| for await (const entry of store.audit({ dryRun: true })) { | ||
| if (entry.alias) seen.push([entry.alias.name, entry.dangling, entry.pruned]) | ||
| } | ||
| t.alike(seen, [ | ||
| ['fine', false, false], | ||
| ['ghost', true, false] | ||
| ]) | ||
| t.alike(deleted, [], 'a dry run prunes nothing') | ||
|
|
||
| const pruned = [] | ||
| for await (const entry of store.audit()) { | ||
| if (entry.alias) pruned.push([entry.alias.name, entry.dangling, entry.pruned]) | ||
| } | ||
| t.alike(pruned, [ | ||
| ['fine', false, false], | ||
| ['ghost', true, true] | ||
| ]) | ||
| t.alike(deleted, ['ghost'], 'the dangling alias is pruned') | ||
|
|
||
| await store.close() | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleting the dangling alias in audit is great! Also love the
dryRunusage.Should the yielded value be inconsistent though? I think it could be refactored to be a composite of different types of audits via a
typefield and maybe anauditso it matches theauditfrom the first type above, and then returnalias,danglingandprunedtogether as theaudit. This could then allow the type of audits to expand over time, but allow discriminating based on type.Aka the return type would be something like:
aka a tagged union type.
Currently code that assumes
auditexisting will fail.