1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
11 """Parse Arvados timestamp to unix time."""
15 return calendar.timegm(ciso8601.parse_datetime_as_naive(t).timetuple())
16 except (TypeError, ValueError):
19 def use_counter(orig_func):
20 @functools.wraps(orig_func)
21 def use_counter_wrapper(self, *args, **kwargs):
24 return orig_func(self, *args, **kwargs)
27 return use_counter_wrapper
29 def check_update(orig_func):
30 @functools.wraps(orig_func)
31 def check_update_wrapper(self, *args, **kwargs):
33 return orig_func(self, *args, **kwargs)
34 return check_update_wrapper
36 class FreshBase(object):
37 """Base class for maintaining object lifecycle.
41 * Indicate if an object is up to date (stale() == false) or needs to be
42 updated sets stale() == True). Use invalidate() to mark the object as
43 stale. An object is also automatically stale if it has not been updated
44 in `_poll_time` seconds.
46 * Record access time (atime) timestamp
48 * Manage internal use count used by the inode cache ("inc_use" and
49 "dec_use"). An object which is in use cannot be cleared by the inode
52 * Manage the kernel reference count ("inc_ref" and "dec_ref"). An object
53 which is referenced by the kernel cannot have its inode entry deleted.
55 * Record cache footprint, cache priority
57 * Record Arvados uuid at the time the object is placed in the cache
59 * Clear the object contents (invalidates the object)
63 __slots__ = ("_stale", "_poll", "_last_update", "_atime", "_poll_time", "use_count",
64 "ref_count", "cache_size", "cache_uuid", "allow_attr_cache")
69 self._last_update = time.time()
70 self._atime = time.time()
75 self.cache_uuid = None
77 # Can the kernel cache attributes?
78 self.allow_attr_cache = True
81 """Indicate that object contents should be refreshed from source."""
84 def kernel_invalidate(self):
85 """Indicate that an invalidation for this object should be sent to the kernel."""
88 # Test if the entries dict is stale.
93 return (self._last_update + self._poll_time) < self._atime
98 self._last_update = time.time()
110 return self.use_count > 0
120 return self.ref_count
122 def dec_ref(self, n):
124 return self.ref_count
127 """Determine if there are any kernel references to this
130 return self.ref_count > 0
141 def child_event(self, ev):
144 def time_to_next_poll(self):
146 t = (self._last_update + self._poll_time) - self._atime
152 return self._poll_time