X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/a04932c206425f4eb2f46e36f0acf4b7b194b865..2d4198a095e193102daa2710c6b2baba7be7c9ce:/services/fuse/arvados_fuse/__init__.py diff --git a/services/fuse/arvados_fuse/__init__.py b/services/fuse/arvados_fuse/__init__.py index 0d5ecb46ed..cab44dd579 100644 --- a/services/fuse/arvados_fuse/__init__.py +++ b/services/fuse/arvados_fuse/__init__.py @@ -1,6 +1,49 @@ -# -# FUSE driver for Arvados Keep -# +"""FUSE driver for Arvados Keep + +Architecture: + +There is one `Operations` object per mount point. It is the entry point for all +read and write requests from the llfuse module. + +The operations object owns an `Inodes` object. The inodes object stores the +mapping from numeric inode (used throughout the file system API to uniquely +identify files) to the Python objects that implement files and directories. + +The `Inodes` object owns an `InodeCache` object. The inode cache records the +memory footprint of file system objects and when they are last used. When the +cache limit is exceeded, the least recently used objects are cleared. + +File system objects inherit from `fresh.FreshBase` which manages the object lifecycle. + +File objects inherit from `fusefile.File`. Key methods are `readfrom` and `writeto` +which implement actual reads and writes. + +Directory objects inherit from `fusedir.Directory`. The directory object wraps +a Python dict which stores the mapping from filenames to directory entries. +Directory contents can be accessed through the Python operators such as `[]` +and `in`. These methods automatically check if the directory is fresh (up to +date) or stale (needs update) and will call `update` if necessary before +returing a result. + +The general FUSE operation flow is as follows: + +- The request handler is called with either an inode or file handle that is the + subject of the operation. + +- Look up the inode using the Inodes table or the file handle in the + filehandles table to get the file system object. + +- For methods that alter files or directories, check that the operation is + valid and permitted using _check_writable(). + +- Call the relevant method on the file system object. + +- Return the result. + +The FUSE driver supports the Arvados event bus. When an event is received for +an object that is live in the inode cache, that object is immediately updated. + +""" import os import sys @@ -24,15 +67,24 @@ import ciso8601 import collections import functools +import Queue + +# Default _notify_queue has a limit of 1000 items, but it really needs to be +# unlimited to avoid deadlocks, see https://arvados.org/issues/3198#note-43 for +# details. + +llfuse.capi._notify_queue = Queue.Queue() + from fusedir import sanitize_filename, Directory, CollectionDirectory, MagicDirectory, TagsDirectory, ProjectDirectory, SharedDirectory, CollectionDirectoryBase from fusefile import StringFile, FuseArvadosFile _logger = logging.getLogger('arvados.arvados_fuse') -log_handler = logging.StreamHandler() -llogger = logging.getLogger('llfuse') -llogger.addHandler(log_handler) -llogger.setLevel(logging.DEBUG) +# Uncomment this to enable llfuse debug logging. +# log_handler = logging.StreamHandler() +# llogger = logging.getLogger('llfuse') +# llogger.addHandler(log_handler) +# llogger.setLevel(logging.DEBUG) class Handle(object): """Connects a numeric file handle to a File or Directory object that has @@ -66,6 +118,17 @@ class DirectoryHandle(Handle): class InodeCache(object): + """Records the memory footprint of objects and when they are last used. + + When the cache limit is exceeded, the least recently used objects are + cleared. Clearing the object means discarding its contents to release + memory. The next time the object is accessed, it must be re-fetched from + the server. Note that the inode cache limit is a soft limit; the cache + limit may be exceeded if necessary to load very large objects, it may also + be exceeded if open file handles prevent objects from being cleared. + + """ + def __init__(self, cap, min_entries=4): self._entries = collections.OrderedDict() self._by_uuid = {} @@ -91,7 +154,6 @@ class InodeCache(object): return True def cap_cache(self): - #_logger.debug("InodeCache total is %i cap is %i", self._total, self.cap) if self._total > self.cap: for key in list(self._entries.keys()): if self._total < self.cap or len(self._entries) < self.min_entries: @@ -129,10 +191,12 @@ class Inodes(object): """Manage the set of inodes. This is the mapping from a numeric id to a concrete File or Directory object""" - def __init__(self, inode_cache): + def __init__(self, inode_cache, encoding="utf-8"): self._entries = {} self._counter = itertools.count(llfuse.ROOT_INODE) self.inode_cache = inode_cache + self.encoding = encoding + self.deferred_invalidations = [] def __getitem__(self, item): return self._entries[item] @@ -163,16 +227,26 @@ class Inodes(object): def del_entry(self, entry): if entry.ref_count == 0: - _logger.debug("Deleting inode %i", entry.inode) self.inode_cache.unmanage(entry) - llfuse.invalidate_inode(entry.inode) del self._entries[entry.inode] + with llfuse.lock_released: + entry.finalize() + self.invalidate_inode(entry.inode) entry.inode = None else: entry.dead = True _logger.debug("del_entry on inode %i with refcount %i", entry.inode, entry.ref_count) + def invalidate_inode(self, inode): + llfuse.invalidate_inode(inode) + + def invalidate_entry(self, inode, name): + llfuse.invalidate_entry(inode, name) + + def catch_exceptions(orig_func): + """Catch uncaught exceptions and log them consistently.""" + @functools.wraps(orig_func) def catch_exceptions_wrapper(self, *args, **kwargs): try: @@ -181,6 +255,12 @@ def catch_exceptions(orig_func): raise except EnvironmentError as e: raise llfuse.FUSEError(e.errno) + except arvados.errors.KeepWriteError as e: + _logger.error("Keep write error: " + str(e)) + raise llfuse.FUSEError(errno.EIO) + except arvados.errors.NotFoundError as e: + _logger.error("Block not found error: " + str(e)) + raise llfuse.FUSEError(errno.EIO) except: _logger.exception("Unhandled exception during FUSE operation") raise llfuse.FUSEError(errno.EIO) @@ -200,15 +280,15 @@ class Operations(llfuse.Operations): """ - def __init__(self, uid, gid, encoding="utf-8", inode_cache=None, num_retries=4): + def __init__(self, uid, gid, encoding="utf-8", inode_cache=None, num_retries=4, enable_write=False): super(Operations, self).__init__() if not inode_cache: inode_cache = InodeCache(cap=256*1024*1024) - self.inodes = Inodes(inode_cache) + self.inodes = Inodes(inode_cache, encoding=encoding) self.uid = uid self.gid = gid - self.encoding = encoding + self.enable_write = enable_write # dict of inode to filehandle self._filehandles = {} @@ -227,31 +307,58 @@ class Operations(llfuse.Operations): # initializing to continue self.initlock.set() + @catch_exceptions def destroy(self): if self.events: self.events.close() + self.events = None + + for k,v in self.inodes.items(): + try: + v.finalize() + except Exception as e: + _logger.exception("Error during finalize of inode %i", k) + self.inodes = None def access(self, inode, mode, ctx): return True def listen_for_events(self, api_client): - self.event = arvados.events.subscribe(api_client, + self.events = arvados.events.subscribe(api_client, [["event_type", "in", ["create", "update", "delete"]]], self.on_event) + @catch_exceptions def on_event(self, ev): if 'event_type' in ev: with llfuse.lock: item = self.inodes.inode_cache.find(ev["object_uuid"]) if item is not None: item.invalidate() - item.update() + if ev["object_kind"] == "arvados#collection": + new_attr = ev.get("properties") and ev["properties"].get("new_attributes") and ev["properties"]["new_attributes"] + + # new_attributes.modified_at currently lacks subsecond precision (see #6347) so use event_at which + # should always be the same. + #record_version = (new_attr["modified_at"], new_attr["portable_data_hash"]) if new_attr else None + record_version = (ev["event_at"], new_attr["portable_data_hash"]) if new_attr else None + + item.update(to_record_version=record_version) + else: + item.update() + + oldowner = ev.get("properties") and ev["properties"].get("old_attributes") and ev["properties"]["old_attributes"].get("owner_uuid") + olditemparent = self.inodes.inode_cache.find(oldowner) + if olditemparent is not None: + olditemparent.invalidate() + olditemparent.update() itemparent = self.inodes.inode_cache.find(ev["object_owner_uuid"]) if itemparent is not None: itemparent.invalidate() itemparent.update() + @catch_exceptions def getattr(self, inode): if inode not in self.inodes: @@ -262,8 +369,8 @@ class Operations(llfuse.Operations): entry = llfuse.EntryAttributes() entry.st_ino = inode entry.generation = 0 - entry.entry_timeout = 300 - entry.attr_timeout = 300 + entry.entry_timeout = 60 + entry.attr_timeout = 60 entry.st_mode = stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH if isinstance(e, Directory): @@ -273,7 +380,7 @@ class Operations(llfuse.Operations): if isinstance(e, FuseArvadosFile): entry.st_mode |= stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH - if e.writable(): + if self.enable_write and e.writable(): entry.st_mode |= stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH entry.st_nlink = 1 @@ -284,16 +391,29 @@ class Operations(llfuse.Operations): entry.st_size = e.size() entry.st_blksize = 512 - entry.st_blocks = (e.size()/512)+1 + entry.st_blocks = (entry.st_size/512)+1 entry.st_atime = int(e.atime()) entry.st_mtime = int(e.mtime()) entry.st_ctime = int(e.mtime()) return entry + @catch_exceptions + def setattr(self, inode, attr): + entry = self.getattr(inode) + + e = self.inodes[inode] + + if attr.st_size is not None and isinstance(e, FuseArvadosFile): + with llfuse.lock_released: + e.arvfile.truncate(attr.st_size) + entry.st_size = e.arvfile.size() + + return entry + @catch_exceptions def lookup(self, parent_inode, name): - name = unicode(name, self.encoding) + name = unicode(name, self.inodes.encoding) inode = None if name == '.': @@ -352,12 +472,7 @@ class Operations(llfuse.Operations): self.inodes.touch(handle.obj) - try: - with llfuse.lock_released: - return handle.obj.readfrom(off, size, self.num_retries) - except arvados.errors.NotFoundError as e: - _logger.warning("Block not found: " + str(e)) - raise llfuse.FUSEError(errno.EIO) + return handle.obj.readfrom(off, size, self.num_retries) @catch_exceptions def write(self, fh, off, buf): @@ -372,20 +487,18 @@ class Operations(llfuse.Operations): self.inodes.touch(handle.obj) - with llfuse.lock_released: - return handle.obj.writeto(off, buf, self.num_retries) + return handle.obj.writeto(off, buf, self.num_retries) @catch_exceptions def release(self, fh): if fh in self._filehandles: try: self._filehandles[fh].flush() - except EnvironmentError as e: - raise llfuse.FUSEError(e.errno) except Exception: - _logger.exception("Flush error") - self._filehandles[fh].release() - del self._filehandles[fh] + raise + finally: + self._filehandles[fh].release() + del self._filehandles[fh] self.inodes.inode_cache.cap_cache() def releasedir(self, fh): @@ -429,16 +542,13 @@ class Operations(llfuse.Operations): e = off while e < len(handle.entries): if handle.entries[e][1].inode in self.inodes: - try: - yield (handle.entries[e][0].encode(self.encoding), self.getattr(handle.entries[e][1].inode), e+1) - except UnicodeEncodeError: - pass + yield (handle.entries[e][0].encode(self.inodes.encoding), self.getattr(handle.entries[e][1].inode), e+1) e += 1 @catch_exceptions def statfs(self): st = llfuse.StatvfsData() - st.f_bsize = 64 * 1024 + st.f_bsize = 128 * 1024 st.f_blocks = 0 st.f_files = 0 @@ -452,6 +562,9 @@ class Operations(llfuse.Operations): return st def _check_writable(self, inode_parent): + if not self.enable_write: + raise llfuse.FUSEError(errno.EROFS) + if inode_parent in self.inodes: p = self.inodes[inode_parent] else: @@ -467,6 +580,8 @@ class Operations(llfuse.Operations): @catch_exceptions def create(self, inode_parent, name, mode, flags, ctx): + _logger.debug("arv-mount create: %i '%s' %o", inode_parent, name, mode) + p = self._check_writable(inode_parent) p.create(name)