From 42e82abc418ce105335254d9833579a08391f547 Mon Sep 17 00:00:00 2001 From: ChristopherHX Date: Sat, 30 Mar 2024 18:23:29 +0100 Subject: [PATCH 1/2] Adjust Cache Restore Order Previous behavior didn't match GitHub's current behavior --- src/Runner.Server/Controllers/CacheController.cs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/Runner.Server/Controllers/CacheController.cs b/src/Runner.Server/Controllers/CacheController.cs index e791b2e8dc5..9de75377de3 100644 --- a/src/Runner.Server/Controllers/CacheController.cs +++ b/src/Runner.Server/Controllers/CacheController.cs @@ -68,21 +68,12 @@ public async Task GetCacheEntry( string owner, string repo, [From var repository = User.FindFirst("repository")?.Value ?? "Unknown/Unknown"; foreach(var cref in reference != defaultRef ? new [] { reference, defaultRef } : new [] { reference }) { foreach (var item in a) { - var record = (from rec in _context.Caches where rec.Repo.ToLower() == repository.ToLower() && rec.Ref == cref && rec.Key.ToLower() == item.ToLower() && (rec.Version == null || rec.Version == "" || rec.Version == version) orderby rec.LastUpdated descending select rec).FirstOrDefault(); + var record = (from rec in _context.Caches where rec.Repo.ToLower() == repository.ToLower() && rec.Ref == cref && rec.Key.ToLower() == item.ToLower() && (rec.Version == null || rec.Version == "" || rec.Version == version) orderby rec.LastUpdated descending select rec).FirstOrDefault() + ?? (from rec in _context.Caches where rec.Repo.ToLower() == repository.ToLower() && rec.Ref == cref && rec.Key.ToLower().StartsWith(item.ToLower()) && (rec.Version == null || rec.Version == "" || rec.Version == version) orderby rec.LastUpdated descending select rec).FirstOrDefault(); if(record != null) { - return await Ok(new ArtifactCacheEntry{ cacheKey = item, scope = cref, creationTime = record.LastUpdated.ToLongDateString(), archiveLocation = new Uri(new Uri(ServerUrl), $"_apis/artifactcache/get/{record.Id}").ToString() }); + return await Ok(new ArtifactCacheEntry{ cacheKey = record.Key, scope = cref, creationTime = record.LastUpdated.ToLongDateString(), archiveLocation = new Uri(new Uri(ServerUrl), $"_apis/artifactcache/get/{record.Id}").ToString() }); } } - CacheRecord partialMatch = null; - foreach (var item in a) { - var record = (from rec in _context.Caches where rec.Repo.ToLower() == repository.ToLower() && rec.Ref == cref && rec.Key.ToLower().StartsWith(item.ToLower()) && (rec.Version == null || rec.Version == "" || rec.Version == version) orderby rec.LastUpdated descending select rec).FirstOrDefault(); - if(record != null && (partialMatch == null || record.LastUpdated > partialMatch.LastUpdated)) { - partialMatch = record; - } - } - if(partialMatch != null) { - return await Ok(new ArtifactCacheEntry{ cacheKey = partialMatch.Key, scope = cref, creationTime = partialMatch.LastUpdated.ToLongDateString(), archiveLocation = new Uri(new Uri(ServerUrl), $"_apis/artifactcache/get/{partialMatch.Id}").ToString() }); - } } return NoContent(); } From 010e90dd032c18595dd3f6907fc68ee58a78fd66 Mon Sep 17 00:00:00 2001 From: ChristopherHX Date: Sat, 30 Mar 2024 18:41:39 +0100 Subject: [PATCH 2/2] cache regression tests --- .github/workflows/nuget.yml | 2 ++ .../.github/workflows/a.yml | 24 +++++++++++++++++ .../.github/workflows/b.yml | 26 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 testworkflows/cache-save-restore-order-tests/.github/workflows/a.yml create mode 100644 testworkflows/cache-save-restore-order-tests/.github/workflows/b.yml diff --git a/.github/workflows/nuget.yml b/.github/workflows/nuget.yml index a3f6de8f8f8..17cecb3be0d 100644 --- a/.github/workflows/nuget.yml +++ b/.github/workflows/nuget.yml @@ -159,6 +159,8 @@ jobs: gharun -C testworkflows/reusable-workflows-secrets-inherit-with-required-secrets -s TEST=topsecret -s OPT=testsec gharun -C testworkflows/inherit_vars --var ACTIONS_STEP_DEBUG=true gharun -C testworkflows/actions_artifacts_v4 -s ACTIONS_STEP_DEBUG=true -s ACTIONS_RUNNER_DEBUG=true --runner-version v2.311.0 + gharun -C testworkflows/actions_artifacts_v4 -s ACTIONS_STEP_DEBUG=true -s ACTIONS_RUNNER_DEBUG=true + gharun -C testworkflows/cache-save-restore-order-tests -s ACTIONS_STEP_DEBUG=true -s ACTIONS_RUNNER_DEBUG=true gharun --event azpipelines -C testworkflows/azpipelines/cross-repo-checkout -W testworkflows/azpipelines/cross-repo-checkout/pipeline.yml --local-repository az/containermatrix@main=testworkflows/azpipelines/containermatrix gharun --event azpipelines -C testworkflows/azpipelines/typedtemplates -W testworkflows/azpipelines/typedtemplates/pipeline.yml gharun --event azpipelines -C testworkflows/azpipelines/untypedtemplates -W testworkflows/azpipelines/untypedtemplates/pipeline.yml diff --git a/testworkflows/cache-save-restore-order-tests/.github/workflows/a.yml b/testworkflows/cache-save-restore-order-tests/.github/workflows/a.yml new file mode 100644 index 00000000000..9990f5da777 --- /dev/null +++ b/testworkflows/cache-save-restore-order-tests/.github/workflows/a.yml @@ -0,0 +1,24 @@ +on: + push: +jobs: + testa: + runs-on: self-hosted + steps: + - uses: actions/checkout@v4 + + - uses: actions/cache/save@v4 + with: + path: . + key: ${{ runner.os }}-${{ github.run_id }}-3 + - uses: actions/cache/save@v4 + with: + path: . + key: ${{ runner.os }}-${{ github.run_id }}-3-1 + - uses: actions/cache/restore@v4 + id: cache + with: + path: . + key: ${{ runner.os }}-${{ github.run_id }}-3 + - name: Assert that we got a cache hit + run: exit 1 + if: steps.cache.outputs.cache-hit != 'true' \ No newline at end of file diff --git a/testworkflows/cache-save-restore-order-tests/.github/workflows/b.yml b/testworkflows/cache-save-restore-order-tests/.github/workflows/b.yml new file mode 100644 index 00000000000..d3f7e1f1ca7 --- /dev/null +++ b/testworkflows/cache-save-restore-order-tests/.github/workflows/b.yml @@ -0,0 +1,26 @@ +on: + push: +jobs: + testa: + runs-on: self-hosted + steps: + - uses: actions/checkout@v4 + + - uses: actions/cache/save@v4 + with: + path: . + key: ${{ runner.os }}-${{ github.run_id }}-3 + - uses: actions/cache/save@v4 + with: + path: . + key: ${{ runner.os }}-${{ github.run_id }}-3-1 + - uses: actions/cache/restore@v4 + id: cache + with: + path: . + key: ----------------------------------------------------------- + restore-keys: + ${{ runner.os }}-${{ github.run_id }}-3 + - name: Assert that we got a cache hit + run: exit 1 + if: steps.cache.outputs.cache-matched-key != format('{0}-{1}-3', runner.os, github.run_id)