Merge "Adjust lock acquiring logic"
This commit is contained in:
commit
62eb26542e
|
@ -34,7 +34,6 @@ Commands (from conductors) are received via RPC calls.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import errno
|
|
||||||
from eventlet.green import subprocess
|
from eventlet.green import subprocess
|
||||||
import fcntl
|
import fcntl
|
||||||
import fileinput
|
import fileinput
|
||||||
|
@ -583,25 +582,7 @@ class AgentManager(service.PeriodicService):
|
||||||
"""
|
"""
|
||||||
lock_file_fd = os.open(
|
lock_file_fd = os.open(
|
||||||
constants.NETWORK_CONFIG_LOCK_FILE, os.O_CREAT | os.O_RDONLY)
|
constants.NETWORK_CONFIG_LOCK_FILE, os.O_CREAT | os.O_RDONLY)
|
||||||
count = 1
|
return utils.acquire_file_lock(lock_file_fd)
|
||||||
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
|
|
||||||
|
|
||||||
def _release_network_config_lock(self, lockfd):
|
def _release_network_config_lock(self, lockfd):
|
||||||
""" Release the lock guarding apply_network_config.sh """
|
""" Release the lock guarding apply_network_config.sh """
|
||||||
|
|
|
@ -1370,6 +1370,34 @@ def _get_cinder_device_info(dbapi, forihostid):
|
||||||
return cinder_device, cinder_size_gib
|
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 skip_udev_partition_probe(function):
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
"""Decorator to skip partition rescanning in udev
|
"""Decorator to skip partition rescanning in udev
|
||||||
|
@ -1397,7 +1425,7 @@ def skip_udev_partition_probe(function):
|
||||||
device_node = kwargs.get('device_node', None)
|
device_node = kwargs.get('device_node', None)
|
||||||
if device_node:
|
if device_node:
|
||||||
with open(device_node, 'r') as f:
|
with open(device_node, 'r') as f:
|
||||||
fcntl.flock(f, fcntl.LOCK_SH | fcntl.LOCK_NB)
|
acquire_file_lock(f)
|
||||||
try:
|
try:
|
||||||
return function(*args, **kwargs)
|
return function(*args, **kwargs)
|
||||||
finally:
|
finally:
|
||||||
|
|
Loading…
Reference in New Issue