From 470858f6f69223b4fc65e4712365b0fbb2f722e3 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Tue, 23 Jul 2024 22:20:10 +0900 Subject: [PATCH] cache: add support for `zstd --adapt` Signed-off-by: Sora Morimoto --- packages/cache/__tests__/tar.test.ts | 4 ++-- packages/cache/src/internal/cacheUtils.ts | 8 ++++++-- packages/cache/src/internal/constants.ts | 5 ++++- packages/cache/src/internal/tar.ts | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/cache/__tests__/tar.test.ts b/packages/cache/__tests__/tar.test.ts index 4145d9a946..30a7d8dfca 100644 --- a/packages/cache/__tests__/tar.test.ts +++ b/packages/cache/__tests__/tar.test.ts @@ -233,7 +233,7 @@ test('zstd create tar', async () => { .concat(IS_MAC ? ['--delay-directory-restore'] : []) .concat([ '--use-compress-program', - IS_WINDOWS ? '"zstd -T0 --long=30"' : 'zstdmt --long=30' + IS_WINDOWS ? '"zstd -T0 --adapt --long=30"' : 'zstdmt --adapt --long=30' ]) .join(' '), undefined, // args @@ -292,7 +292,7 @@ test('zstd create tar with windows BSDtar', async () => { expect(execMock).toHaveBeenNthCalledWith( 2, [ - 'zstd -T0 --long=30 --force -o', + 'zstd -T0 --adapt --long=30 --force -o', CacheFilename.Zstd.replace(/\\/g, '/'), TarFilename.replace(/\\/g, '/') ].join(' '), diff --git a/packages/cache/src/internal/cacheUtils.ts b/packages/cache/src/internal/cacheUtils.ts index 91bae9a8db..69874e61fa 100644 --- a/packages/cache/src/internal/cacheUtils.ts +++ b/packages/cache/src/internal/cacheUtils.ts @@ -102,10 +102,14 @@ export async function getCompressionMethod(): Promise { const version = semver.clean(versionOutput) core.debug(`zstd version: ${version}`) - if (versionOutput === '') { + if (version === null) { return CompressionMethod.Gzip - } else { + } else if (semver.lt(version, '1.3.2')) { return CompressionMethod.ZstdWithoutLong + } else if (semver.lt(version, '1.3.6')) { + return CompressionMethod.ZstdWithoutAdapt + } else { + return CompressionMethod.Zstd } } diff --git a/packages/cache/src/internal/constants.ts b/packages/cache/src/internal/constants.ts index 4dbff574a0..c4f8cd36c7 100644 --- a/packages/cache/src/internal/constants.ts +++ b/packages/cache/src/internal/constants.ts @@ -6,8 +6,11 @@ export enum CacheFilename { export enum CompressionMethod { Gzip = 'gzip', // Long range mode was added to zstd in v1.3.2. - // This enum is for earlier version of zstd that does not have --long support + // https://github.com/facebook/zstd/releases/tag/v1.3.2 ZstdWithoutLong = 'zstd-without-long', + // Adapt mode was added to zstd in v1.3.6. + // https://github.com/facebook/zstd/releases/tag/v1.3.6 + ZstdWithoutAdapt = 'zstd-without-adapt', Zstd = 'zstd' } diff --git a/packages/cache/src/internal/tar.ts b/packages/cache/src/internal/tar.ts index adf610694f..c91a304534 100644 --- a/packages/cache/src/internal/tar.ts +++ b/packages/cache/src/internal/tar.ts @@ -207,6 +207,7 @@ async function getDecompressionProgram( // Used for creating the archive // -T#: Compress using # working thread. If # is 0, attempt to detect and use the number of physical CPU cores. // zstdmt is equivalent to 'zstd -T0' +// --adapt: Dynamically adapt compression level to I/O conditions. // --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit. // Using 30 here because we also support 32-bit self-hosted runners. // Long range mode is added to zstd in v1.3.2 release, so we will not use --long in older version of zstd. @@ -221,6 +222,19 @@ async function getCompressionProgram( IS_WINDOWS switch (compressionMethod) { case CompressionMethod.Zstd: + return BSD_TAR_ZSTD + ? [ + 'zstd -T0 --adapt --long=30 --force -o', + cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), + TarFilename + ] + : [ + '--use-compress-program', + IS_WINDOWS + ? '"zstd -T0 --adapt --long=30"' + : 'zstdmt --adapt --long=30' + ] + case CompressionMethod.ZstdWithoutAdapt: return BSD_TAR_ZSTD ? [ 'zstd -T0 --long=30 --force -o',