Merge "Adjust lock acquiring logic"

This commit is contained in:
Zuul 2021-04-15 21:15:56 +00:00 committed by Gerrit Code Review
commit 62eb26542e
2 changed files with 30 additions and 21 deletions

View File

@ -34,7 +34,6 @@ Commands (from conductors) are received via RPC calls.
"""
from __future__ import print_function
import errno
from eventlet.green import subprocess
import fcntl
import fileinput
@ -583,25 +582,7 @@ class AgentManager(service.PeriodicService):
"""
lock_file_fd = os.open(
constants.NETWORK_CONFIG_LOCK_FILE, os.O_CREAT | os.O_RDONLY)
count = 1
delay = 5
max_count = 5
while count <= max_count:
try:
fcntl.flock(lock_file_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
return lock_file_fd
except IOError as e:
# raise on unrelated IOErrors
if e.errno != errno.EAGAIN:
raise
else:
LOG.info("Could not acquire lock({}): {} ({}/{}), "
"will retry".format(lock_file_fd, str(e),
count, max_count))
time.sleep(delay)
count += 1
LOG.error("Failed to acquire lock (fd={})".format(lock_file_fd))
return 0
return utils.acquire_file_lock(lock_file_fd)
def _release_network_config_lock(self, lockfd):
""" Release the lock guarding apply_network_config.sh """

View File

@ -1370,6 +1370,34 @@ def _get_cinder_device_info(dbapi, forihostid):
return cinder_device, cinder_size_gib
def acquire_file_lock(lockfd, max_retry=5, wait_interval=5):
"""
This method is to acquire a lock for the given file descriptor to
avoid conflict with other processes trying accessing the same file.
:returns: fd of the lock, if successful. 0 on error.
"""
count = 1
while count <= max_retry:
try:
fcntl.flock(lockfd, fcntl.LOCK_EX | fcntl.LOCK_NB)
LOG.debug("Successfully acquired lock (fd={})".format(lockfd))
return lockfd
except IOError as e:
# raise on unrelated IOErrors
if e.errno != errno.EAGAIN:
raise
else:
LOG.info("Could not acquire lock({}): {} ({}/{}), "
"will retry".format(lockfd, str(e),
count, max_retry))
time.sleep(wait_interval)
count += 1
LOG.error("Failed to acquire lock (fd={}). Stop trying...".format(lockfd))
return 0
def skip_udev_partition_probe(function):
def wrapper(*args, **kwargs):
"""Decorator to skip partition rescanning in udev
@ -1397,7 +1425,7 @@ def skip_udev_partition_probe(function):
device_node = kwargs.get('device_node', None)
if device_node:
with open(device_node, 'r') as f:
fcntl.flock(f, fcntl.LOCK_SH | fcntl.LOCK_NB)
acquire_file_lock(f)
try:
return function(*args, **kwargs)
finally: