Skip to content

Commit

Permalink
use chunk filters to store dedup
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeauchamp committed Jul 24, 2023
1 parent 50f378e commit 725b262
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion @xen-orchestra/backups/RemoteAdapter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ export class RemoteAdapter {
async writeVhd(path, input, { checksum = true, validator = noop, writeBlockConcurrency, dedup = false } = {}) {
const handler = this._handler
if (this.useVhdDirectory()) {
const dataPath = `${dirname(path)}/data/${uuidv4()}${dedup ? '.dedup' : ''}.vhd`
const dataPath = `${dirname(path)}/data/${uuidv4()}.vhd`
const size = await createVhdDirectoryFromStream(handler, dataPath, input, {
concurrency: writeBlockConcurrency,
compression: this.#getCompressionType(),
Expand Down
6 changes: 3 additions & 3 deletions packages/vhd-lib/Vhd/VhdAbstract.integ.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('VhdAbstract', async () => {
it('renames and unlink a VhdDirectory', async () => {
const initalSize = 4
const vhdDirectory = `${tempDir}/randomfile.dir`
await createRandomVhdDirectory(vhdDirectory, initalSize)
await createRandomVhdDirectory(vhdDirectory, initalSize, { dedup: true })

await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' })
Expand All @@ -123,8 +123,8 @@ describe('VhdAbstract', async () => {

it('unlinks a deduplicated VhdDirectory', async () => {
const initalSize = 4
const vhdDirectory = `${tempDir}/random.dedup.vhd`
await createRandomVhdDirectory(vhdDirectory, initalSize)
const vhdDirectory = `${tempDir}/random.vhd`
await createRandomVhdDirectory(vhdDirectory, initalSize, { dedup: true })

await Disposable.use(async function* () {
const handler = yield getSyncedHandler({ url: 'file:///' })
Expand Down
10 changes: 8 additions & 2 deletions packages/vhd-lib/Vhd/VhdAbstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,14 @@ exports.VhdAbstract = class VhdAbstract {
await handler.unlink(resolved)
} catch (err) {
if (err.code === 'EISDIR') {
const dedup = resolved.endsWith('.dedup.vhd')
await handler.rmtree(resolved, { dedup })
// @todo : should we open it ?
const chunkFilters = await handler.readFile(path + '/chunk-filters.json').then(JSON.parse, error => {
if (error.code === 'ENOENT') {
return []
}
throw error
})
await handler.rmtree(resolved, { dedup: chunkFilters[1] === true })
} else {
throw err
}
Expand Down
1 change: 1 addition & 0 deletions packages/vhd-lib/Vhd/VhdDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const NULL_COMPRESSOR = {
}

const COMPRESSORS = {
none: NULL_COMPRESSOR,
gzip: {
compress: (
gzip => buffer =>
Expand Down
8 changes: 5 additions & 3 deletions packages/vhd-lib/tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ exports.recoverRawContent = async function recoverRawContent(vhdName, rawName, o
}

// @ todo how can I call vhd-cli copy from here
async function convertToVhdDirectory(rawFileName, vhdFileName, path) {
async function convertToVhdDirectory(rawFileName, vhdFileName, path, { dedup }) {
fs.mkdirp(path)

const srcVhd = await fs.open(vhdFileName, 'r')
Expand Down Expand Up @@ -95,15 +95,17 @@ async function convertToVhdDirectory(rawFileName, vhdFileName, path) {
await fs.read(srcRaw, blockData, 0, blockData.length, offset)
await fs.writeFile(path + '/blocks/0/' + i, Buffer.concat([bitmap, blockData]))
}

await fs.writeFile(path + '/chunk-filters.json', JSON.stringify(['none', dedup]))
await fs.close(srcRaw)
}
exports.convertToVhdDirectory = convertToVhdDirectory

exports.createRandomVhdDirectory = async function createRandomVhdDirectory(path, sizeMB) {
exports.createRandomVhdDirectory = async function createRandomVhdDirectory(path, sizeMB, { dedup = false } = {}) {
fs.mkdirp(path)
const rawFileName = `${path}/temp.raw`
await createRandomFile(rawFileName, sizeMB)
const vhdFileName = `${path}/temp.vhd`
await convertFromRawToVhd(rawFileName, vhdFileName)
await convertToVhdDirectory(rawFileName, vhdFileName, path)
await convertToVhdDirectory(rawFileName, vhdFileName, path, { dedup })
}

0 comments on commit 725b262

Please sign in to comment.