From 03e3c74d1cbe826fd7b94f1956644b550f0a0e9e Mon Sep 17 00:00:00 2001 From: Abel Hu Date: Wed, 26 Feb 2025 00:54:46 +0000 Subject: [PATCH 1/3] chore: replace endpoint in Mooncake --- vhdbuilder/packer/test/linux-vhd-content-test.sh | 4 ++-- vhdbuilder/packer/test/windows-files-check.ps1 | 11 ++++++++--- vhdbuilder/packer/test/windows-vhd-content-test.ps1 | 10 ++++++++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/vhdbuilder/packer/test/linux-vhd-content-test.sh b/vhdbuilder/packer/test/linux-vhd-content-test.sh index 7593df2d85e..48aaf8c64a0 100644 --- a/vhdbuilder/packer/test/linux-vhd-content-test.sh +++ b/vhdbuilder/packer/test/linux-vhd-content-test.sh @@ -216,7 +216,7 @@ testPackagesInstalled() { echo $test "[INFO] File ${downloadedPackage} exists and has the correct size ${fileSizeDownloaded} bytes" # Validate whether package exists in Azure China cloud if [[ $downloadURL == https://acs-mirror.azureedge.net/* ]]; then - mcURL="${downloadURL/https:\/\/acs-mirror.azureedge.net/https:\/\/kubernetesartifacts.blob.core.chinacloudapi.cn}" + mcURL="${downloadURL/https:\/\/acs-mirror.azureedge.net/https:\/\/mirror.azk8s.cn}" echo "Validating: $mcURL" isExist=$(curl -sLI "$mcURL" | grep -i "404 The specified blob does not exist." | awk '{print $2}') if [[ "$isExist" == "404" ]]; then @@ -224,7 +224,7 @@ testPackagesInstalled() { continue fi - fileSizeInMC=$(curl -sLI $mcURL | grep -i Content-Length | tail -n1 | awk '{print $2}' | tr -d '\r') + fileSizeInMC=$(wget -S $mcURL --start-pos=500G 2>&1 | grep -i Content-Length | tail -n1 | awk '{print $2}' | tr -d '\r') if [[ "$fileSizeInMC" != "$fileSizeDownloaded" ]]; then err "$mcURL is valid but the file size is different. Expected file size: ${fileSizeDownloaded} - downloaded file size: ${fileSizeInMC}" continue diff --git a/vhdbuilder/packer/test/windows-files-check.ps1 b/vhdbuilder/packer/test/windows-files-check.ps1 index 25dd097d16f..1ee8db658a5 100644 --- a/vhdbuilder/packer/test/windows-files-check.ps1 +++ b/vhdbuilder/packer/test/windows-files-check.ps1 @@ -197,9 +197,14 @@ function Test-CompareSingleDir { } if ($URL.StartsWith("https://acs-mirror.azureedge.net/")) { - $mcURL = $URL.replace("https://acs-mirror.azureedge.net/", "https://kubernetesartifacts.blob.core.chinacloudapi.cn/") - - $mooncakeFileSize = (Invoke-WebRequest $mcURL -UseBasicParsing -Method Head).Headers.'Content-Length' + $mcURL = $URL.replace("https://acs-mirror.azureedge.net/", "https://mirror.azk8s.cn/") + + $webRequest = [System.Net.HttpWebRequest]::Create($mcURL) + # Set the 'Range' header using the AddRange method + $webRequest.AddRange(0, 1023) + # Get the response + $response = $webRequest.GetResponse() + $mooncakeFileSize = $response.Headers["Content-Range"] -split "/" | Select-Object -Last 1 if ($globalFileSize -ne $mooncakeFileSize) { $MisMatchFiles[$URL]=$mcURL diff --git a/vhdbuilder/packer/test/windows-vhd-content-test.ps1 b/vhdbuilder/packer/test/windows-vhd-content-test.ps1 index 83f68858a8d..c826b440040 100644 --- a/vhdbuilder/packer/test/windows-vhd-content-test.ps1 +++ b/vhdbuilder/packer/test/windows-vhd-content-test.ps1 @@ -167,12 +167,18 @@ function Test-FilesToCacheOnVHD if ( $URL.StartsWith("https://acs-mirror.azureedge.net/")) { - $mcURL = $URL.replace("https://acs-mirror.azureedge.net/", "https://kubernetesartifacts.blob.core.chinacloudapi.cn/") + $mcURL = $URL.replace("https://acs-mirror.azureedge.net/", "https://mirror.azk8s.cn/") try { # It's too slow to download the file from the China Cloud. So we only compare the file size. $localFileSize = (Get-Item $dest).length - $remoteFileSize = (Invoke-WebRequest $mcURL -UseBasicParsing -Method Head).Headers.'Content-Length' + + $webRequest = [System.Net.HttpWebRequest]::Create($mcURL) + # Set the 'Range' header using the AddRange method + $webRequest.AddRange(0, 1023) + # Get the response + $response = $webRequest.GetResponse() + $remoteFileSize = $response.Headers["Content-Range"] -split "/" | Select-Object -Last 1 if ($localFileSize -ne $remoteFileSize) { $isIgnore = $False From 35fb605291101fef378e935c26c020354a7b2939 Mon Sep 17 00:00:00 2001 From: Abel Hu Date: Wed, 26 Feb 2025 01:59:41 +0000 Subject: [PATCH 2/3] chore: add logs --- vhdbuilder/packer/test/windows-files-check.ps1 | 1 + vhdbuilder/packer/test/windows-vhd-content-test.ps1 | 1 + 2 files changed, 2 insertions(+) diff --git a/vhdbuilder/packer/test/windows-files-check.ps1 b/vhdbuilder/packer/test/windows-files-check.ps1 index 1ee8db658a5..47468c1cb0e 100644 --- a/vhdbuilder/packer/test/windows-files-check.ps1 +++ b/vhdbuilder/packer/test/windows-files-check.ps1 @@ -199,6 +199,7 @@ function Test-CompareSingleDir { if ($URL.StartsWith("https://acs-mirror.azureedge.net/")) { $mcURL = $URL.replace("https://acs-mirror.azureedge.net/", "https://mirror.azk8s.cn/") + Write-Output "Getting the size of the file: $mcURL" $webRequest = [System.Net.HttpWebRequest]::Create($mcURL) # Set the 'Range' header using the AddRange method $webRequest.AddRange(0, 1023) diff --git a/vhdbuilder/packer/test/windows-vhd-content-test.ps1 b/vhdbuilder/packer/test/windows-vhd-content-test.ps1 index c826b440040..9d6865938b6 100644 --- a/vhdbuilder/packer/test/windows-vhd-content-test.ps1 +++ b/vhdbuilder/packer/test/windows-vhd-content-test.ps1 @@ -173,6 +173,7 @@ function Test-FilesToCacheOnVHD # It's too slow to download the file from the China Cloud. So we only compare the file size. $localFileSize = (Get-Item $dest).length + Write-Output "Getting the size of the file: $mcURL" $webRequest = [System.Net.HttpWebRequest]::Create($mcURL) # Set the 'Range' header using the AddRange method $webRequest.AddRange(0, 1023) From 79985667b9c7036eebae56351ad20fd26787af3e Mon Sep 17 00:00:00 2001 From: Abel Hu Date: Wed, 26 Feb 2025 02:33:23 +0000 Subject: [PATCH 3/3] add retry --- .../packer/test/windows-files-check.ps1 | 8 +-- .../packer/test/windows-vhd-content-test.ps1 | 51 ++++++++++++++++++- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/vhdbuilder/packer/test/windows-files-check.ps1 b/vhdbuilder/packer/test/windows-files-check.ps1 index 47468c1cb0e..46a7c7464ee 100644 --- a/vhdbuilder/packer/test/windows-files-check.ps1 +++ b/vhdbuilder/packer/test/windows-files-check.ps1 @@ -203,9 +203,11 @@ function Test-CompareSingleDir { $webRequest = [System.Net.HttpWebRequest]::Create($mcURL) # Set the 'Range' header using the AddRange method $webRequest.AddRange(0, 1023) - # Get the response - $response = $webRequest.GetResponse() - $mooncakeFileSize = $response.Headers["Content-Range"] -split "/" | Select-Object -Last 1 + Retry-Command -ScriptBlock { + # Get the response + $global:response = $webRequest.GetResponse() + } -ErrorMessage "Failed to get the size ofthe file: $mcURL" + $mooncakeFileSize = $global:response.Headers["Content-Range"] -split "/" | Select-Object -Last 1 if ($globalFileSize -ne $mooncakeFileSize) { $MisMatchFiles[$URL]=$mcURL diff --git a/vhdbuilder/packer/test/windows-vhd-content-test.ps1 b/vhdbuilder/packer/test/windows-vhd-content-test.ps1 index 9d6865938b6..b99a6de9dac 100644 --- a/vhdbuilder/packer/test/windows-vhd-content-test.ps1 +++ b/vhdbuilder/packer/test/windows-vhd-content-test.ps1 @@ -107,6 +107,50 @@ function DownloadFileWithRetry } } +function Retry-Command { + [CmdletBinding()] + Param( + [Parameter(Position=0, Mandatory=$true)] + [scriptblock]$ScriptBlock, + + [Parameter(Position=1, Mandatory=$true)] + [string]$ErrorMessage, + + [Parameter(Position=2, Mandatory=$false)] + [int]$Maximum = 5, + + [Parameter(Position=3, Mandatory=$false)] + [int]$Delay = 10 + ) + + Begin { + $cnt = 0 + } + + Process { + do { + $cnt++ + try { + $ScriptBlock.Invoke() + if ($LASTEXITCODE) { + throw "Retry $cnt : $ErrorMessage" + } + return + } catch { + Write-ErrorWithTimestamp $_.Exception.InnerException.Message -ErrorAction Continue + if ($_.Exception.InnerException.Message.Contains("There is not enough space on the disk. (0x70)")) { + throw "Exit retry since there is not enough space on the disk" + } + Start-Sleep $Delay + } + } while ($cnt -lt $Maximum) + + # Throw an error after $Maximum unsuccessful invocations. Doesn't need + # a condition, since the function returns upon successful invocation. + throw 'All retries failed. $ErrorMessage' + } +} + function Test-FilesToCacheOnVHD { $invalidFiles = @() @@ -177,8 +221,11 @@ function Test-FilesToCacheOnVHD $webRequest = [System.Net.HttpWebRequest]::Create($mcURL) # Set the 'Range' header using the AddRange method $webRequest.AddRange(0, 1023) - # Get the response - $response = $webRequest.GetResponse() + Retry-Command -ScriptBlock { + # Get the response + $global:response = $webRequest.GetResponse() + } -ErrorMessage "Failed to get the size ofthe file: $mcURL" + $mooncakeFileSize = $global:response.Headers["Content-Range"] -split "/" | Select-Object -Last 1 $remoteFileSize = $response.Headers["Content-Range"] -split "/" | Select-Object -Last 1 if ($localFileSize -ne $remoteFileSize) {