Skip to content
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

@actions/cache: using concurrent download whenever server supported #1835

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions packages/cache/src/internal/cacheHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import * as utils from './cacheUtils'
import {CompressionMethod} from './constants'
import {
ArtifactCacheEntry,
InternalCacheOptions,
ArtifactCacheList,
CommitCacheRequest,
ReserveCacheRequest,
ReserveCacheResponse,
InternalCacheOptions,
ITypedResponseWithError,
ArtifactCacheList
ReserveCacheRequest,
ReserveCacheResponse
} from './contracts'
import {
downloadCacheHttpClient,
Expand All @@ -27,9 +27,9 @@ import {
} from './downloadUtils'
import {
DownloadOptions,
UploadOptions,
getDownloadOptions,
getUploadOptions
getUploadOptions,
UploadOptions
} from '../options'
import {
isSuccessStatusCode,
Expand Down Expand Up @@ -179,22 +179,45 @@ export async function downloadCache(
if (archiveUrl.hostname.endsWith('.blob.core.windows.net')) {
if (downloadOptions.useAzureSdk) {
// Use Azure storage SDK to download caches hosted on Azure to improve speed and reliability.
await downloadCacheStorageSDK(
return await downloadCacheStorageSDK(
archiveLocation,
archivePath,
downloadOptions
)
} else if (downloadOptions.concurrentBlobDownloads) {
// Use concurrent implementation with HttpClient to work around blob SDK issue
await downloadCacheHttpClientConcurrent(
archiveLocation,
archivePath,
downloadOptions
)
} else {
// Otherwise, download using the Actions http-client.
await downloadCacheHttpClient(archiveLocation, archivePath)
}
}

let acceptRange = false
let contentLength = -1
// Determine partial file downloads is supported by server
// via `Accept-Ranges: bytes` response header.
try {
const httpClient = new HttpClient('actions/cache', undefined, {
socketTimeout: downloadOptions.timeoutInMs,
keepAlive: true
})

const res = await retryHttpClientResponse(
'downloadCacheMetadata',
async () => await httpClient.request('HEAD', archiveLocation, null, {})
)

acceptRange = res.message.headers['Accept-Ranges'] === 'bytes'

const lengthHeader = res.message.headers['Content-Length']
contentLength = parseInt(lengthHeader)
} catch {
// ignore
}

if (acceptRange && contentLength > 0) {
// Use concurrent implementation with HttpClient to work around blob SDK issue
await downloadCacheHttpClientConcurrent(
archiveLocation,
archivePath,
contentLength,
downloadOptions
)
} else {
await downloadCacheHttpClient(archiveLocation, archivePath)
}
Expand Down
24 changes: 6 additions & 18 deletions packages/cache/src/internal/downloadUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,13 @@ export async function downloadCacheHttpClient(
*
* @param archiveLocation the URL for the cache
* @param archivePath the local path where the cache is saved
* @param contentLength
* @param options
*/
export async function downloadCacheHttpClientConcurrent(
archiveLocation: string,
archivePath: fs.PathLike,
contentLength: number,
options: DownloadOptions
): Promise<void> {
const archiveDescriptor = await fs.promises.open(archivePath, 'w')
Expand All @@ -220,29 +223,14 @@ export async function downloadCacheHttpClientConcurrent(
keepAlive: true
})
try {
const res = await retryHttpClientResponse(
'downloadCacheMetadata',
async () => await httpClient.request('HEAD', archiveLocation, null, {})
)

const lengthHeader = res.message.headers['content-length']
if (lengthHeader === undefined || lengthHeader === null) {
throw new Error('Content-Length not found on blob response')
}

const length = parseInt(lengthHeader)
if (Number.isNaN(length)) {
throw new Error(`Could not interpret Content-Length: ${length}`)
}

const downloads: {
offset: number
promiseGetter: () => Promise<DownloadSegment>
}[] = []
const blockSize = 4 * 1024 * 1024

for (let offset = 0; offset < length; offset += blockSize) {
const count = Math.min(blockSize, length - offset)
for (let offset = 0; offset < contentLength; offset += blockSize) {
const count = Math.min(blockSize, contentLength - offset)
downloads.push({
offset,
promiseGetter: async () => {
Expand All @@ -260,7 +248,7 @@ export async function downloadCacheHttpClientConcurrent(
downloads.reverse()
let actives = 0
let bytesDownloaded = 0
const progress = new DownloadProgress(length)
const progress = new DownloadProgress(contentLength)
progress.startDisplayTimer()
const progressFn = progress.onProgress()

Expand Down