[Notes] [Git][BuildStream/buildstream][valentindavid/extract-expiry] Remove artifact extracts when artifact expires in cache



Title: GitLab

Valentin David pushed to branch valentindavid/extract-expiry at BuildStream / buildstream

Commits:

2 changed files:

Changes:

  • buildstream/_artifactcache/cascache.py
    ... ... @@ -524,6 +524,28 @@ class CASCache(ArtifactCache):
    524 524
         #
    
    525 525
         def remove(self, ref, *, defer_prune=False):
    
    526 526
     
    
    527
    +        # Remove extract if not used by other ref
    
    528
    +        tree = self.resolve_ref(ref)
    
    529
    +        ref_name, ref_hash = os.path.split(ref)
    
    530
    +        extract = os.path.join(self.extractdir, ref_name, tree.hash)
    
    531
    +        if os.path.exists(extract):
    
    532
    +            remove_extract = True
    
    533
    +
    
    534
    +            for other_hash in os.listdir(self._refpath(ref_name)):
    
    535
    +                if other_hash == ref_hash:
    
    536
    +                    continue
    
    537
    +
    
    538
    +                other_tree = self.resolve_ref(ref)
    
    539
    +                if other_tree.hash == tree.hash:
    
    540
    +                    # If another ref for this element uses the same tree,
    
    541
    +                    # then we keep the extract
    
    542
    +                    remove_extract = False
    
    543
    +                    break
    
    544
    +
    
    545
    +            if remove_extract:
    
    546
    +                utils._force_rmtree(extract)
    
    547
    +
    
    548
    +        # Remove cache ref
    
    527 549
             refpath = self._refpath(ref)
    
    528 550
             if not os.path.exists(refpath):
    
    529 551
                 raise ArtifactError("Could not find artifact for ref '{}'".format(ref))
    

  • tests/artifactcache/expiry.py
    ... ... @@ -268,3 +268,38 @@ def test_invalid_cache_quota(cli, datafiles, tmpdir, quota, success):
    268 268
             res.assert_success()
    
    269 269
         else:
    
    270 270
             res.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.INVALID_DATA)
    
    271
    +
    
    272
    +
    
    273
    +@pytest.mark.datafiles(DATA_DIR)
    
    274
    +def test_extract_expiry(cli, datafiles, tmpdir):
    
    275
    +    project = os.path.join(datafiles.dirname, datafiles.basename)
    
    276
    +    element_path = 'elements'
    
    277
    +
    
    278
    +    cli.configure({
    
    279
    +        'cache': {
    
    280
    +            'quota': 10000000,
    
    281
    +        }
    
    282
    +    })
    
    283
    +
    
    284
    +    create_element_size('target.bst', project, element_path, [], 6000000)
    
    285
    +    res = cli.run(project=project, args=['build', 'target.bst'])
    
    286
    +    res.assert_success()
    
    287
    +    assert cli.get_element_state(project, 'target.bst') == 'cached'
    
    288
    +
    
    289
    +    # Force creating extract
    
    290
    +    res = cli.run(project=project, args=['checkout', 'target.bst', os.path.join(str(tmpdir), 'checkout')])
    
    291
    +    res.assert_success()
    
    292
    +
    
    293
    +    extractdir = os.path.join(project, 'cache', 'artifacts', 'extract', 'test', 'target')
    
    294
    +    extracts = os.listdir(extractdir)
    
    295
    +    assert(len(extracts) == 1)
    
    296
    +    extract = os.path.join(extractdir, extracts[0])
    
    297
    +
    
    298
    +    # Remove target.bst from artifact cache
    
    299
    +    create_element_size('target2.bst', project, element_path, [], 6000000)
    
    300
    +    res = cli.run(project=project, args=['build', 'target2.bst'])
    
    301
    +    res.assert_success()
    
    302
    +    assert cli.get_element_state(project, 'target.bst') != 'cached'
    
    303
    +
    
    304
    +    # Now the extract should be removed.
    
    305
    +    assert not os.path.exists(extract)



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]