From a735d9bcd466cb2cb76a37653c7b978250736272 Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Mon, 14 Nov 2022 05:24:09 +0000 Subject: [PATCH 01/10] Add logs for cache version on miss --- packages/cache/src/internal/cacheHttpClient.ts | 18 +++++++++++++++++- packages/cache/src/internal/contracts.d.ts | 6 ++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index c66d1a73..922c2750 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -17,7 +17,8 @@ import { CommitCacheRequest, ReserveCacheRequest, ReserveCacheResponse, - ITypedResponseWithError + ITypedResponseWithError, + ArtifactCacheList } from './contracts' import {downloadCacheHttpClient, downloadCacheStorageSDK} from './downloadUtils' import { @@ -113,6 +114,21 @@ export async function getCacheEntry( const cacheResult = response.result const cacheDownloadUrl = cacheResult?.archiveLocation if (!cacheDownloadUrl) { + // List cache for primary key only if cache miss occurs + const resource = `caches?key=${encodeURIComponent(keys[0])}` + const response = await httpClient.getJson(getCacheApiUrl(resource)) + if(response.statusCode === 204) { + const cacheListResult = response.result + const totalCount = cacheListResult?.totalCount + if(totalCount && totalCount > 0) { + core.info(`Cache miss occurred on the cache key '${keys[0]}' and version '${version} but there is ${totalCount} existing version of the cache for this key. More info on versioning can be found here: https://github.com/actions/cache#cache-version`) + core.debug(`Other versions are as follows:`) + cacheListResult?.artifactCaches?.forEach(cacheEntry => { + core.debug(`Cache Key: ${cacheEntry?.cacheKey}, Cache Version: ${cacheEntry?.cacheVersion}, Cache Scope: ${cacheEntry?.scope}, Cache Created: ${cacheEntry?.creationTime}`) + }) + } + } + throw new Error('Cache not found.') } core.setSecret(cacheDownloadUrl) diff --git a/packages/cache/src/internal/contracts.d.ts b/packages/cache/src/internal/contracts.d.ts index 1b2a13a1..b5f53bdc 100644 --- a/packages/cache/src/internal/contracts.d.ts +++ b/packages/cache/src/internal/contracts.d.ts @@ -9,10 +9,16 @@ export interface ITypedResponseWithError extends TypedResponse { export interface ArtifactCacheEntry { cacheKey?: string scope?: string + cacheVersion?: string creationTime?: string archiveLocation?: string } +export interface ArtifactCacheList { + totalCount: number + artifactCaches?: ArtifactCacheEntry[] +} + export interface CommitCacheRequest { size: number } From aaac0e6c9895ec46d5d87b004b063b325681a35b Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Tue, 15 Nov 2022 10:32:24 +0000 Subject: [PATCH 02/10] Address review comments --- .../cache/src/internal/cacheHttpClient.ts | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index 922c2750..6d2cccde 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -115,20 +115,7 @@ export async function getCacheEntry( const cacheDownloadUrl = cacheResult?.archiveLocation if (!cacheDownloadUrl) { // List cache for primary key only if cache miss occurs - const resource = `caches?key=${encodeURIComponent(keys[0])}` - const response = await httpClient.getJson(getCacheApiUrl(resource)) - if(response.statusCode === 204) { - const cacheListResult = response.result - const totalCount = cacheListResult?.totalCount - if(totalCount && totalCount > 0) { - core.info(`Cache miss occurred on the cache key '${keys[0]}' and version '${version} but there is ${totalCount} existing version of the cache for this key. More info on versioning can be found here: https://github.com/actions/cache#cache-version`) - core.debug(`Other versions are as follows:`) - cacheListResult?.artifactCaches?.forEach(cacheEntry => { - core.debug(`Cache Key: ${cacheEntry?.cacheKey}, Cache Version: ${cacheEntry?.cacheVersion}, Cache Scope: ${cacheEntry?.scope}, Cache Created: ${cacheEntry?.creationTime}`) - }) - } - } - + await listCache(keys[0], httpClient, version) throw new Error('Cache not found.') } core.setSecret(cacheDownloadUrl) @@ -138,6 +125,32 @@ export async function getCacheEntry( return cacheResult } +async function listCache( + key: string, + httpClient: HttpClient, + version: string +): Promise { + const resource = `caches?key=${encodeURIComponent(key)}` + const response = await retryTypedResponse('listCache', async () => + httpClient.getJson(getCacheApiUrl(resource)) + ) + if (response.statusCode === 204) { + const cacheListResult = response.result + const totalCount = cacheListResult?.totalCount + if (totalCount && totalCount > 0) { + core.info( + `Cache miss occurred on the cache key '${key}' and version '${version} but there is ${totalCount} existing version of the cache for this key. More info on versioning can be found here: https://github.com/actions/cache#cache-version` + ) + core.debug(`Other versions are as follows:`) + cacheListResult?.artifactCaches?.forEach(cacheEntry => { + core.debug( + `Cache Key: ${cacheEntry?.cacheKey}, Cache Version: ${cacheEntry?.cacheVersion}, Cache Scope: ${cacheEntry?.scope}, Cache Created: ${cacheEntry?.creationTime}` + ) + }) + } + } +} + export async function downloadCache( archiveLocation: string, archivePath: string, From b9d1dd898e5a340d18ca45b95c5150213c5de7b6 Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Tue, 15 Nov 2022 11:04:24 +0000 Subject: [PATCH 03/10] Address review comments --- packages/cache/src/internal/cacheHttpClient.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index 6d2cccde..b4b50f8b 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -115,7 +115,7 @@ export async function getCacheEntry( const cacheDownloadUrl = cacheResult?.archiveLocation if (!cacheDownloadUrl) { // List cache for primary key only if cache miss occurs - await listCache(keys[0], httpClient, version) + await listCache(keys[0], httpClient, version, cacheResult?.scope) throw new Error('Cache not found.') } core.setSecret(cacheDownloadUrl) @@ -128,7 +128,8 @@ export async function getCacheEntry( async function listCache( key: string, httpClient: HttpClient, - version: string + version: string, + scope?: string ): Promise { const resource = `caches?key=${encodeURIComponent(key)}` const response = await retryTypedResponse('listCache', async () => @@ -139,7 +140,7 @@ async function listCache( const totalCount = cacheListResult?.totalCount if (totalCount && totalCount > 0) { core.info( - `Cache miss occurred on the cache key '${key}' and version '${version} but there is ${totalCount} existing version of the cache for this key. More info on versioning can be found here: https://github.com/actions/cache#cache-version` + `No matching cache found for cache key '${key}', version '${version} and scope ${scope} but there is ${totalCount} existing version of the cache for this key. More info on versioning can be found here: https://github.com/actions/cache#cache-version` ) core.debug(`Other versions are as follows:`) cacheListResult?.artifactCaches?.forEach(cacheEntry => { From 816c1b37609e775ac7d11622568feb04065ed034 Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Tue, 13 Dec 2022 11:25:40 +0000 Subject: [PATCH 04/10] Fix tests --- .../cache/src/internal/cacheHttpClient.ts | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index b4b50f8b..98e9d8d2 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -105,6 +105,10 @@ export async function getCacheEntry( httpClient.getJson(getCacheApiUrl(resource)) ) if (response.statusCode === 204) { + // List cache for primary key only if cache miss occurs + if (core.isDebug()) { + await listCache(keys[0], httpClient, version, response) + } return null } if (!isSuccessStatusCode(response.statusCode)) { @@ -114,8 +118,6 @@ export async function getCacheEntry( const cacheResult = response.result const cacheDownloadUrl = cacheResult?.archiveLocation if (!cacheDownloadUrl) { - // List cache for primary key only if cache miss occurs - await listCache(keys[0], httpClient, version, cacheResult?.scope) throw new Error('Cache not found.') } core.setSecret(cacheDownloadUrl) @@ -129,25 +131,25 @@ async function listCache( key: string, httpClient: HttpClient, version: string, - scope?: string + getCacheEntryResponse: any ): Promise { + const scope = getCacheEntryResponse.result?.scope const resource = `caches?key=${encodeURIComponent(key)}` const response = await retryTypedResponse('listCache', async () => httpClient.getJson(getCacheApiUrl(resource)) ) - if (response.statusCode === 204) { + if (response.statusCode === 200) { const cacheListResult = response.result const totalCount = cacheListResult?.totalCount if (totalCount && totalCount > 0) { - core.info( - `No matching cache found for cache key '${key}', version '${version} and scope ${scope} but there is ${totalCount} existing version of the cache for this key. More info on versioning can be found here: https://github.com/actions/cache#cache-version` + core.debug( + `No matching cache found for cache key '${key}', version '${version} and scope ${scope} but there are ${totalCount} existing version of the cache for this key. More info on versioning can be found here: https://github.com/actions/cache#cache-version \nOther versions are as follows:` ) - core.debug(`Other versions are as follows:`) - cacheListResult?.artifactCaches?.forEach(cacheEntry => { + for (const cacheEntry of cacheListResult.artifactCaches || []) { core.debug( `Cache Key: ${cacheEntry?.cacheKey}, Cache Version: ${cacheEntry?.cacheVersion}, Cache Scope: ${cacheEntry?.scope}, Cache Created: ${cacheEntry?.creationTime}` ) - }) + } } } } From e559a15ca64cdea38a2afda1a343b25d2770274a Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Tue, 13 Dec 2022 11:36:10 +0000 Subject: [PATCH 05/10] Fix test --- packages/cache/src/internal/cacheHttpClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index 98e9d8d2..d6ea5e4a 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -145,7 +145,7 @@ async function listCache( core.debug( `No matching cache found for cache key '${key}', version '${version} and scope ${scope} but there are ${totalCount} existing version of the cache for this key. More info on versioning can be found here: https://github.com/actions/cache#cache-version \nOther versions are as follows:` ) - for (const cacheEntry of cacheListResult.artifactCaches || []) { + for (const cacheEntry of cacheListResult?.artifactCaches || []) { core.debug( `Cache Key: ${cacheEntry?.cacheKey}, Cache Version: ${cacheEntry?.cacheVersion}, Cache Scope: ${cacheEntry?.scope}, Cache Created: ${cacheEntry?.creationTime}` ) From 24685611e29a439826d4525ae90c7d110ee62cbd Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Tue, 13 Dec 2022 11:48:30 +0000 Subject: [PATCH 06/10] Add current scope from github ref --- packages/cache/src/internal/cacheHttpClient.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index d6ea5e4a..532a09d6 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -107,7 +107,7 @@ export async function getCacheEntry( if (response.statusCode === 204) { // List cache for primary key only if cache miss occurs if (core.isDebug()) { - await listCache(keys[0], httpClient, version, response) + await listCache(keys[0], httpClient, version) } return null } @@ -130,20 +130,19 @@ export async function getCacheEntry( async function listCache( key: string, httpClient: HttpClient, - version: string, - getCacheEntryResponse: any + version: string ): Promise { - const scope = getCacheEntryResponse.result?.scope const resource = `caches?key=${encodeURIComponent(key)}` const response = await retryTypedResponse('listCache', async () => httpClient.getJson(getCacheApiUrl(resource)) ) - if (response.statusCode === 200) { + core.debug(`List Cache Response Status Code: ${response.statusCode}`) + if (response.statusCode !== 200) { const cacheListResult = response.result const totalCount = cacheListResult?.totalCount if (totalCount && totalCount > 0) { core.debug( - `No matching cache found for cache key '${key}', version '${version} and scope ${scope} but there are ${totalCount} existing version of the cache for this key. More info on versioning can be found here: https://github.com/actions/cache#cache-version \nOther versions are as follows:` + `No matching cache found for cache key '${key}', version '${version} and scope ${process.env['GITHUB_REF']} but there are ${totalCount} existing version of the cache for this key. More info on versioning can be found here: https://github.com/actions/cache#cache-version \nOther versions are as follows:` ) for (const cacheEntry of cacheListResult?.artifactCaches || []) { core.debug( From b8c50aa82d788116e1daf54f636f12d1c5f142af Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Fri, 16 Dec 2022 05:55:53 +0000 Subject: [PATCH 07/10] Fix response code --- packages/cache/src/internal/cacheHttpClient.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index 532a09d6..14a25803 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -136,8 +136,7 @@ async function listCache( const response = await retryTypedResponse('listCache', async () => httpClient.getJson(getCacheApiUrl(resource)) ) - core.debug(`List Cache Response Status Code: ${response.statusCode}`) - if (response.statusCode !== 200) { + if (response.statusCode === 200) { const cacheListResult = response.result const totalCount = cacheListResult?.totalCount if (totalCount && totalCount > 0) { From e96dc8a69a919789e27edf4d06e37a1293907908 Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Fri, 16 Dec 2022 18:17:37 +0530 Subject: [PATCH 08/10] Update packages/cache/src/internal/cacheHttpClient.ts Co-authored-by: Bishal Prasad --- packages/cache/src/internal/cacheHttpClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index 14a25803..0f28af3f 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -141,7 +141,7 @@ async function listCache( const totalCount = cacheListResult?.totalCount if (totalCount && totalCount > 0) { core.debug( - `No matching cache found for cache key '${key}', version '${version} and scope ${process.env['GITHUB_REF']} but there are ${totalCount} existing version of the cache for this key. More info on versioning can be found here: https://github.com/actions/cache#cache-version \nOther versions are as follows:` + `No matching cache found for cache key '${key}', version '${version} and scope ${process.env['GITHUB_REF']}. There exist one or more cache(s) with similar key but they have different version or scope. See more info on cache matching here: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key \nOther caches with similar key:` ) for (const cacheEntry of cacheListResult?.artifactCaches || []) { core.debug( From ccfa36f304056fb24583eecf03e14cdbe334bec3 Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Wed, 21 Dec 2022 10:24:52 +0000 Subject: [PATCH 09/10] Address review comments --- packages/cache/src/internal/cacheHttpClient.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index 14a25803..8f42c7db 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -107,7 +107,7 @@ export async function getCacheEntry( if (response.statusCode === 204) { // List cache for primary key only if cache miss occurs if (core.isDebug()) { - await listCache(keys[0], httpClient, version) + await printCachesListForDiagnostics(keys[0], httpClient, version) } return null } @@ -127,7 +127,7 @@ export async function getCacheEntry( return cacheResult } -async function listCache( +async function printCachesListForDiagnostics( key: string, httpClient: HttpClient, version: string @@ -143,7 +143,7 @@ async function listCache( core.debug( `No matching cache found for cache key '${key}', version '${version} and scope ${process.env['GITHUB_REF']} but there are ${totalCount} existing version of the cache for this key. More info on versioning can be found here: https://github.com/actions/cache#cache-version \nOther versions are as follows:` ) - for (const cacheEntry of cacheListResult?.artifactCaches || []) { + for (const cacheEntry of cacheListResult.artifactCaches || []) { core.debug( `Cache Key: ${cacheEntry?.cacheKey}, Cache Version: ${cacheEntry?.cacheVersion}, Cache Scope: ${cacheEntry?.scope}, Cache Created: ${cacheEntry?.creationTime}` ) From c23fe4b81fb21b14e9f64bb1b10fbe6f0baaffb0 Mon Sep 17 00:00:00 2001 From: Sampark Sharma Date: Wed, 21 Dec 2022 10:30:22 +0000 Subject: [PATCH 10/10] FIx test --- packages/cache/src/internal/cacheHttpClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index f0244c43..d5ecd9a8 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -143,7 +143,7 @@ async function printCachesListForDiagnostics( core.debug( `No matching cache found for cache key '${key}', version '${version} and scope ${process.env['GITHUB_REF']}. There exist one or more cache(s) with similar key but they have different version or scope. See more info on cache matching here: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key \nOther caches with similar key:` ) - for (const cacheEntry of cacheListResult.artifactCaches || []) { + for (const cacheEntry of cacheListResult?.artifactCaches || []) { core.debug( `Cache Key: ${cacheEntry?.cacheKey}, Cache Version: ${cacheEntry?.cacheVersion}, Cache Scope: ${cacheEntry?.scope}, Cache Created: ${cacheEntry?.creationTime}` )