Use monotonic time for hashring reset

hashring use time.time() to calculate intervals, replace with
monotonic so it will not be affected by system time jump.

Change-Id: I17569359f4d2c0f2f24ca8b50773c4d210ed8deb
This commit is contained in:
Kaifeng Wang 2023-05-07 15:09:50 +08:00
parent 7f281392c2
commit b48dfd44c7
2 changed files with 3 additions and 3 deletions

View File

@ -40,7 +40,7 @@ class HashRingManager(object):
@property
def ring(self):
interval = CONF.hash_ring_reset_interval
limit = time.time() - interval
limit = time.monotonic() - interval
if not self.cache:
return self._load_hash_rings()
@ -56,7 +56,7 @@ class HashRingManager(object):
if hash_rings is None or updated_at < limit:
LOG.debug('Rebuilding cached hash rings')
hash_rings = self._load_hash_rings()
self.__class__._hash_rings = hash_rings, time.time()
self.__class__._hash_rings = hash_rings, time.monotonic()
LOG.debug('Finished rebuilding hash rings, available drivers '
'are %s', ', '.join(hash_rings))
return hash_rings

View File

@ -129,7 +129,7 @@ class HashRingManagerTestCase(db_base.DbTestCase):
self.ring_manager.__class__._hash_rings = (
self.ring_manager.__class__._hash_rings[0],
time.time() - 31
time.monotonic() - 31
)
ring = self.ring_manager.get_ring('hardware-type', '')
self.assertEqual(2, len(ring))