[Notes] [Git][BuildStream/buildstream][bst-1.2] 2 commits: _context.py: Cache size is now restricted to available disk space



Title: GitLab

Tristan Van Berkom pushed to branch bst-1.2 at BuildStream / buildstream

Commits:

2 changed files:

Changes:

  • buildstream/_context.py
    ... ... @@ -197,29 +197,55 @@ class Context():
    197 197
                                 "\nValid values are, for example: 800M 10G 1T 50%\n"
    
    198 198
                                 .format(str(e))) from e
    
    199 199
     
    
    200
    -        # If we are asked not to set a quota, we set it to the maximum
    
    201
    -        # disk space available minus a headroom of 2GB, such that we
    
    202
    -        # at least try to avoid raising Exceptions.
    
    200
    +        # Headroom intended to give BuildStream a bit of leeway.
    
    201
    +        # This acts as the minimum size of cache_quota and also
    
    202
    +        # is taken from the user requested cache_quota.
    
    203 203
             #
    
    204
    -        # Of course, we might still end up running out during a build
    
    205
    -        # if we end up writing more than 2G, but hey, this stuff is
    
    206
    -        # already really fuzzy.
    
    207
    -        #
    
    208
    -        if cache_quota is None:
    
    209
    -            stat = os.statvfs(artifactdir_volume)
    
    210
    -            # Again, the artifact directory may not yet have been
    
    211
    -            # created
    
    212
    -            if not os.path.exists(self.artifactdir):
    
    213
    -                cache_size = 0
    
    214
    -            else:
    
    215
    -                cache_size = utils._get_dir_size(self.artifactdir)
    
    216
    -            cache_quota = cache_size + stat.f_bsize * stat.f_bavail
    
    217
    -
    
    218 204
             if 'BST_TEST_SUITE' in os.environ:
    
    219 205
                 headroom = 0
    
    220 206
             else:
    
    221 207
                 headroom = 2e9
    
    222 208
     
    
    209
    +        stat = os.statvfs(artifactdir_volume)
    
    210
    +        available_space = (stat.f_bsize * stat.f_bavail)
    
    211
    +
    
    212
    +        # Again, the artifact directory may not yet have been created yet
    
    213
    +        #
    
    214
    +        if not os.path.exists(self.artifactdir):
    
    215
    +            cache_size = 0
    
    216
    +        else:
    
    217
    +            cache_size = utils._get_dir_size(self.artifactdir)
    
    218
    +
    
    219
    +        # Ensure system has enough storage for the cache_quota
    
    220
    +        #
    
    221
    +        # If cache_quota is none, set it to the maximum it could possibly be.
    
    222
    +        #
    
    223
    +        # Also check that cache_quota is atleast as large as our headroom.
    
    224
    +        #
    
    225
    +        if cache_quota is None:  # Infinity, set to max system storage
    
    226
    +            cache_quota = cache_size + available_space
    
    227
    +        if cache_quota < headroom:  # Check minimum
    
    228
    +            raise LoadError(LoadErrorReason.INVALID_DATA,
    
    229
    +                            "Invalid cache quota ({}): ".format(utils._pretty_size(cache_quota)) +
    
    230
    +                            "BuildStream requires a minimum cache quota of 2G.")
    
    231
    +        elif cache_quota > cache_size + available_space:  # Check maximum
    
    232
    +            raise LoadError(LoadErrorReason.INVALID_DATA,
    
    233
    +                            ("Your system does not have enough available " +
    
    234
    +                             "space to support the cache quota specified.\n" +
    
    235
    +                             "You currently have:\n" +
    
    236
    +                             "- {used} of cache in use at {local_cache_path}\n" +
    
    237
    +                             "- {available} of available system storage").format(
    
    238
    +                                 used=utils._pretty_size(cache_size),
    
    239
    +                                 local_cache_path=self.artifactdir,
    
    240
    +                                 available=utils._pretty_size(available_space)))
    
    241
    +
    
    242
    +        # Place a slight headroom (2e9 (2GB) on the cache_quota) into
    
    243
    +        # cache_quota to try and avoid exceptions.
    
    244
    +        #
    
    245
    +        # Of course, we might still end up running out during a build
    
    246
    +        # if we end up writing more than 2G, but hey, this stuff is
    
    247
    +        # already really fuzzy.
    
    248
    +        #
    
    223 249
             self.cache_quota = cache_quota - headroom
    
    224 250
             self.cache_lower_threshold = self.cache_quota / 2
    
    225 251
     
    

  • buildstream/utils.py
    ... ... @@ -612,6 +612,27 @@ def _parse_size(size, volume):
    612 612
         return int(num) * 1024**units.index(unit)
    
    613 613
     
    
    614 614
     
    
    615
    +# _pretty_size()
    
    616
    +#
    
    617
    +# Converts a number of bytes into a string representation in KB, MB, GB, TB
    
    618
    +# represented as K, M, G, T etc.
    
    619
    +#
    
    620
    +# Args:
    
    621
    +#   size (int): The size to convert in bytes.
    
    622
    +#   dec_places (int): The number of decimal places to output to.
    
    623
    +#
    
    624
    +# Returns:
    
    625
    +#   (str): The string representation of the number of bytes in the largest
    
    626
    +def _pretty_size(size, dec_places=0):
    
    627
    +    psize = size
    
    628
    +    unit = 'B'
    
    629
    +    for unit in ('B', 'K', 'M', 'G', 'T'):
    
    630
    +        if psize < 1024:
    
    631
    +            break
    
    632
    +        else:
    
    633
    +            psize /= 1024
    
    634
    +    return "{size:g}{unit}".format(size=round(psize, dec_places), unit=unit)
    
    635
    +
    
    615 636
     # A sentinel to be used as a default argument for functions that need
    
    616 637
     # to distinguish between a kwarg set to None and an unset kwarg.
    
    617 638
     _sentinel = object()
    



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