1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
18 _logger = logging.getLogger('arvados.keep')
20 cacheblock_suffix = ".keepcacheblock"
22 class DiskCacheSlot(object):
23 __slots__ = ("locator", "ready", "content", "cachedir", "filehandle", "linger")
25 def __init__(self, locator, cachedir):
26 self.locator = locator
27 self.ready = threading.Event()
29 self.cachedir = cachedir
30 self.filehandle = None
35 # 'content' can None, an empty byte string, or a nonempty mmap
36 # region. If it is an mmap region, we want to advise the
37 # kernel we're going to use it. This nudges the kernel to
38 # re-read most or all of the block if necessary (instead of
39 # just a few pages at a time), reducing the number of page
40 # faults and improving performance by 4x compared to not
43 self.content.madvise(mmap.MADV_WILLNEED)
55 # Can't mmap a 0 length file
60 if self.content is not None:
61 # Has been set already
65 blockdir = os.path.join(self.cachedir, self.locator[0:3])
66 os.makedirs(blockdir, mode=0o700, exist_ok=True)
68 final = os.path.join(blockdir, self.locator) + cacheblock_suffix
70 self.filehandle = tempfile.NamedTemporaryFile(dir=blockdir, delete=False, prefix="tmp", suffix=cacheblock_suffix)
71 tmpfile = self.filehandle.name
72 os.chmod(tmpfile, stat.S_IRUSR | stat.S_IWUSR)
74 # aquire a shared lock, this tells other processes that
75 # we're using this block and to please not delete it.
76 fcntl.flock(self.filehandle, fcntl.LOCK_SH)
78 self.filehandle.write(value)
79 self.filehandle.flush()
80 os.rename(tmpfile, final)
83 self.content = mmap.mmap(self.filehandle.fileno(), 0, access=mmap.ACCESS_READ)
84 # only set the event when mmap is successful
88 if tmpfile is not None:
89 # If the tempfile hasn't been renamed on disk yet, try to delete it.
96 if self.content is None:
97 if self.linger is not None:
98 # If it is still lingering (object is still accessible
99 # through the weak reference) it is still taking up
101 content = self.linger()
102 if content is not None:
106 return len(self.content)
112 # The mmap region might be in use when we decided to evict
113 # it. This can happen if the cache is too small.
115 # If we call close() now, it'll throw an error if
116 # something tries to access it.
118 # However, we don't need to explicitly call mmap.close()
120 # I confirmed in mmapmodule.c that that both close
121 # and deallocate do the same thing:
123 # a) close the file descriptor
124 # b) unmap the memory range
126 # So we can forget it in the cache and delete the file on
127 # disk, and it will tear it down after any other
128 # lingering Python references to the mapped memory are
131 blockdir = os.path.join(self.cachedir, self.locator[0:3])
132 final = os.path.join(blockdir, self.locator) + cacheblock_suffix
134 fcntl.flock(self.filehandle, fcntl.LOCK_UN)
136 # try to get an exclusive lock, this ensures other
137 # processes are not using the block. It is
138 # nonblocking and will throw an exception if we
139 # can't get it, which is fine because that means
140 # we just won't try to delete it.
142 # I should note here, the file locking is not
143 # strictly necessary, we could just remove it and
144 # the kernel would ensure that the underlying
145 # inode remains available as long as other
146 # processes still have the file open. However, if
147 # you have multiple processes sharing the cache
148 # and deleting each other's files, you'll end up
149 # with a bunch of ghost files that don't show up
150 # in the file system but are still taking up
151 # space, which isn't particularly user friendly.
152 # The locking strategy ensures that cache blocks
153 # in use remain visible.
155 fcntl.flock(self.filehandle, fcntl.LOCK_EX | fcntl.LOCK_NB)
162 self.filehandle = None
166 def get_from_disk(locator, cachedir):
167 blockdir = os.path.join(cachedir, locator[0:3])
168 final = os.path.join(blockdir, locator) + cacheblock_suffix
171 filehandle = open(final, "rb")
173 # aquire a shared lock, this tells other processes that
174 # we're using this block and to please not delete it.
175 fcntl.flock(filehandle, fcntl.LOCK_SH)
177 content = mmap.mmap(filehandle.fileno(), 0, access=mmap.ACCESS_READ)
178 dc = DiskCacheSlot(locator, cachedir)
179 dc.filehandle = filehandle
183 except FileNotFoundError:
185 except Exception as e:
186 traceback.print_exc()
191 def cache_usage(cachedir):
193 for root, dirs, files in os.walk(cachedir):
195 if not name.endswith(cacheblock_suffix):
198 blockpath = os.path.join(root, name)
199 res = os.stat(blockpath)
205 def init_cache(cachedir, maxslots):
207 # First check the disk cache works at all by creating a 1 byte cache entry
209 checkexists = DiskCacheSlot.get_from_disk('0cc175b9c0f1b6a831c399e269772661', cachedir)
210 ds = DiskCacheSlot('0cc175b9c0f1b6a831c399e269772661', cachedir)
212 if checkexists is None:
213 # Don't keep the test entry around unless it existed beforehand.
216 # map in all the files in the cache directory, up to max slots.
217 # after max slots, try to delete the excess blocks.
219 # this gives the calling process ownership of all the blocks
222 for root, dirs, files in os.walk(cachedir):
224 if not name.endswith(cacheblock_suffix):
227 blockpath = os.path.join(root, name)
228 res = os.stat(blockpath)
230 if len(name) == (32+len(cacheblock_suffix)) and not name.startswith("tmp"):
231 blocks.append((name[0:32], res.st_atime))
232 elif name.startswith("tmp") and ((time.time() - res.st_mtime) > 60):
233 # found a temporary file more than 1 minute old,
240 # sort by access time (atime), going from most recently
241 # accessed (highest timestamp) to least recently accessed
242 # (lowest timestamp).
243 blocks.sort(key=lambda x: x[1], reverse=True)
245 # Map in all the files we found, up to maxslots, if we exceed
246 # maxslots, start throwing things out.
247 cachelist: collections.OrderedDict = collections.OrderedDict()
249 got = DiskCacheSlot.get_from_disk(b[0], cachedir)
252 if len(cachelist) < maxslots:
253 cachelist[got.locator] = got
255 # we found more blocks than maxslots, try to
256 # throw it out of the cache.