This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor: convert repo API to async/await #2674
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
'use strict' | ||
|
||
const CID = require('cids') | ||
const { cidToString } = require('../../../utils/cid') | ||
const log = require('debug')('ipfs:repo:gc') | ||
const { MFS_ROOT_KEY } = require('ipfs-mfs') | ||
const Repo = require('ipfs-repo') | ||
const { Errors } = require('interface-datastore') | ||
const ERR_NOT_FOUND = Errors.notFoundError().code | ||
const { parallelMerge, transform } = require('streaming-iterables') | ||
|
||
// Limit on the number of parallel block remove operations | ||
const BLOCK_RM_CONCURRENCY = 256 | ||
|
||
// Perform mark and sweep garbage collection | ||
module.exports = ({ gcLock, pin, pinManager, refs, repo }) => { | ||
return async function * gc () { | ||
const start = Date.now() | ||
log('Creating set of marked blocks') | ||
|
||
const release = await gcLock.writeLock() | ||
|
||
try { | ||
// Mark all blocks that are being used | ||
const markedSet = await createMarkedSet({ pin, pinManager, repo }) | ||
// Get all blocks keys from the blockstore | ||
const blockKeys = repo.blocks.query({ keysOnly: true }) | ||
|
||
// Delete blocks that are not being used | ||
yield * deleteUnmarkedBlocks({ repo, refs }, markedSet, blockKeys) | ||
|
||
log(`Complete (${Date.now() - start}ms)`) | ||
} finally { | ||
release() | ||
} | ||
} | ||
} | ||
|
||
// Get Set of CIDs of blocks to keep | ||
async function createMarkedSet ({ pin, pinManager, refs, repo }) { | ||
const pinsSource = async function * () { | ||
for await (const { hash } of pin.ls()) { | ||
yield hash | ||
} | ||
} | ||
|
||
const pinInternalsSource = async function * () { | ||
const cids = await pinManager.getInternalBlocks() | ||
yield * cids | ||
} | ||
|
||
const mfsSource = async function * () { | ||
const mh = await repo.root.get(MFS_ROOT_KEY) | ||
const rootCid = new CID(mh) | ||
yield rootCid | ||
try { | ||
for await (const { ref } of refs(rootCid, { recursive: true })) { | ||
yield new CID(ref) | ||
} | ||
} catch (err) { | ||
if (err.code === ERR_NOT_FOUND) { | ||
log('No blocks in MFS') | ||
return | ||
} | ||
throw err | ||
} | ||
} | ||
|
||
const output = new Set() | ||
for await (const cid of parallelMerge(pinsSource, pinInternalsSource, mfsSource)) { | ||
output.add(cidToString(cid, { base: 'base32' })) | ||
} | ||
return output | ||
} | ||
|
||
// Delete all blocks that are not marked as in use | ||
async function * deleteUnmarkedBlocks ({ repo, refs }, markedSet, blockKeys) { | ||
// Iterate through all blocks and find those that are not in the marked set | ||
// blockKeys yields { key: Key() } | ||
let blocksCount = 0 | ||
let removedBlocksCount = 0 | ||
|
||
const removeBlock = async ({ key: k }) => { | ||
blocksCount++ | ||
|
||
try { | ||
const cid = Repo.utils.blockstore.keyToCid(k) | ||
const b32 = cid.toV1().toString('base32') | ||
if (markedSet.has(b32)) return null | ||
const res = { cid } | ||
|
||
try { | ||
await repo.blocks.delete(cid) | ||
removedBlocksCount++ | ||
} catch (err) { | ||
res.err = new Error(`Could not delete block with CID ${cid}: ${err.message}`) | ||
} | ||
|
||
return res | ||
} catch (err) { | ||
const msg = `Could not convert block with key '${k}' to CID` | ||
log(msg, err) | ||
return { err: new Error(msg + `: ${err.message}`) } | ||
} | ||
} | ||
|
||
for await (const res of transform(BLOCK_RM_CONCURRENCY, removeBlock, blockKeys)) { | ||
// filter nulls (blocks that were retained) | ||
if (res) yield res | ||
} | ||
|
||
log(`Marked set has ${markedSet.size} unique blocks. Blockstore has ${blocksCount} blocks. ` + | ||
`Deleted ${removedBlocksCount} blocks.`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict' | ||
|
||
module.exports = ({ repo }) => { | ||
return async function stat () { | ||
const stats = await repo.stat() | ||
|
||
return { | ||
numObjects: stats.numObjects, | ||
repoSize: stats.repoSize, | ||
repoPath: stats.repoPath, | ||
version: stats.version.toString(), | ||
storageMax: stats.storageMax | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict' | ||
|
||
const { repoVersion } = require('ipfs-repo') | ||
|
||
module.exports = ({ repo }) => { | ||
/** | ||
* If the repo has been initialized, report the current version. | ||
* Otherwise report the version that would be initialized. | ||
* | ||
* @returns {number} | ||
*/ | ||
return async function version () { | ||
try { | ||
await repo._checkInitialized() | ||
} catch (err) { | ||
// TODO: (dryajov) This is really hacky, there must be a better way | ||
const match = [ | ||
/Key not found in database \[\/version\]/, | ||
/ENOENT/, | ||
/repo is not initialized yet/ | ||
].some((m) => { | ||
return m.test(err.message) | ||
}) | ||
if (match) { | ||
// this repo has not been initialized | ||
return repoVersion | ||
} | ||
throw err | ||
} | ||
|
||
return repo.version.get() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we use streaming-iterables
map
here?