[Notes] [Git][BuildStream/buildstream][Qinusty/491] _context.py: Cache size is now restricted to available disk space



Title: GitLab

Josh pushed to branch Qinusty/491 at BuildStream / buildstream

Commits:

2 changed files:

Changes:

  • buildstream/_context.py
    ... ... @@ -197,30 +197,54 @@ 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
    +        if 'BST_TEST_SUITE' in os.environ:
    
    201
    +            headroom = 0
    
    202
    +        else:
    
    203
    +            headroom = 2e9
    
    204
    +
    
    205
    +        stat = os.statvfs(artifactdir_volume)
    
    206
    +        available_space = (stat.f_bsize * stat.f_bavail)
    
    207
    +        # Place a slight headroom (2e9 (2GB) on the cache_quota) into
    
    208
    +        # bst_available_space to try and avoid exceptions.
    
    203 209
             #
    
    204 210
             # Of course, we might still end up running out during a build
    
    205 211
             # if we end up writing more than 2G, but hey, this stuff is
    
    206 212
             # already really fuzzy.
    
    207 213
             #
    
    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
    
    214
    +        bst_available_space = available_space - headroom
    
    217 215
     
    
    218
    -        if 'BST_TEST_SUITE' in os.environ:
    
    219
    -            headroom = 0
    
    216
    +        # Again, the artifact directory may not yet have been created yet
    
    217
    +        #
    
    218
    +        if not os.path.exists(self.artifactdir):
    
    219
    +            cache_size = 0
    
    220 220
             else:
    
    221
    -            headroom = 2e9
    
    221
    +            cache_size = utils._get_dir_size(self.artifactdir)
    
    222 222
     
    
    223
    -        self.cache_quota = cache_quota - headroom
    
    223
    +        # Ensure system has enough storage for the cache_quota
    
    224
    +        #
    
    225
    +        # If cache_quota is none, set it to the maximum it could possibly be.
    
    226
    +        #
    
    227
    +        if cache_quota is None:  # Infinity, set to max system storage
    
    228
    +            cache_quota = cache_size + available_space
    
    229
    +        elif cache_quota < headroom:
    
    230
    +            raise LoadError(LoadErrorReason.INVALID_DATA,
    
    231
    +                            "Invalid cache quota ({}): ".format(utils._pretty_size(cache_quota)) +
    
    232
    +                            "BuildStream requires a minimum cache quota of 2GB.")
    
    233
    +        elif cache_size + cache_quota > bst_available_space:
    
    234
    +            raise LoadError(LoadErrorReason.INVALID_DATA,
    
    235
    +                            ("Your system does not have enough available " +
    
    236
    +                             "space to support the cache quota specified.\n" +
    
    237
    +                             "You currently have:\n" +
    
    238
    +                             "- {used} of cache in use at {local_cache_path}\n" +
    
    239
    +                             "- {available} of available system storage.\n" +
    
    240
    +                             "Note: BuildStream requires the cache quota be at least {headroom} " +
    
    241
    +                             "smaller than your available system storage").format(
    
    242
    +                                 used=utils._pretty_size(cache_size),
    
    243
    +                                 local_cache_path=self.artifactdir,
    
    244
    +                                 available=utils._pretty_size(available_space),
    
    245
    +                                 headroom=utils._pretty_size(headroom)))
    
    246
    +
    
    247
    +        self.cache_quota = cache_quota
    
    224 248
             self.cache_lower_threshold = self.cache_quota / 2
    
    225 249
     
    
    226 250
             # Load artifact share configuration
    

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



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