Skip to content

Commit

Permalink
CP-49720 Move LOCK_TYPE_RUNNING from cleanup.py to lock.py
Browse files Browse the repository at this point in the history
In the process, rename LOCK_TYPE_RUNNING to LOCK_TYPE_GC_RUNNING to
describe *what* is running when it's running. This matches
LOCK_TYPE_GC_ACTIVE which remains in cleanup.py because it is only used
there.

Rename the global variables in cleanup.py to describe *what* is
Active/Running.

Signed-off-by: Tim Smith <[email protected]>
  • Loading branch information
Tim Smith committed Jun 6, 2024
1 parent c6b591a commit f841603
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 65 deletions.
4 changes: 2 additions & 2 deletions drivers/FileSR.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import time
import glob
from uuid import uuid4
from lock import Lock
from lock import Lock, LOCK_TYPE_GC_RUNNING
import xmlrpc.client
import XenAPI # pylint: disable=import-error
from constants import CBTLOG_TAG
Expand Down Expand Up @@ -381,7 +381,7 @@ def _kickGC(self):
# don't bother if an instance already running (this is just an
# optimization to reduce the overhead of forking a new process if we
# don't have to, but the process will check the lock anyways)
lockRunning = Lock(cleanup.LOCK_TYPE_RUNNING, self.uuid)
lockRunning = Lock(LOCK_TYPE_GC_RUNNING, self.uuid)
if not lockRunning.acquireNoblock():
if cleanup.should_preempt(self.session, self.uuid):
util.SMlog("Aborting currently-running coalesce of garbage VDI")
Expand Down
5 changes: 3 additions & 2 deletions drivers/LVHDSR.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
import cleanup
import blktap2
from journaler import Journaler
from lock import Lock
from lock import Lock, LOCK_TYPE_GC_RUNNING
import cleanup
from refcounter import RefCounter
from ipc import IPCFlag
from lvmanager import LVActivator
Expand Down Expand Up @@ -1292,7 +1293,7 @@ def _kickGC(self):
# don't bother if an instance already running (this is just an
# optimization to reduce the overhead of forking a new process if we
# don't have to, but the process will check the lock anyways)
lockRunning = Lock(cleanup.LOCK_TYPE_RUNNING, self.uuid)
lockRunning = Lock(LOCK_TYPE_GC_RUNNING, self.uuid)
if not lockRunning.acquireNoblock():
if cleanup.should_preempt(self.session, self.uuid):
util.SMlog("Aborting currently-running coalesce of garbage VDI")
Expand Down
47 changes: 23 additions & 24 deletions drivers/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@
# process "lock", used simply as an indicator that a process already exists
# that is doing GC/coalesce on this SR (such a process holds the lock, and we
# check for the fact by trying the lock).
LOCK_TYPE_RUNNING = "running"
lockRunning = None
lockGCRunning = None

# process "lock" to indicate that the GC process has been activated but may not
# yet be running, stops a second process from being started.
LOCK_TYPE_GC_ACTIVE = "gc_active"
lockActive = None
lockGCActive = None

# Default coalesce error rate limit, in messages per minute. A zero value
# disables throttling, and a negative value disables error reporting.
Expand Down Expand Up @@ -2934,7 +2933,7 @@ def abortTest():


def _gcLoop(sr, dryRun=False, immediate=False):
if not lockActive.acquireNoblock():
if not lockGCActive.acquireNoblock():
Util.log("Another GC instance already active, exiting")
return

Expand Down Expand Up @@ -2971,7 +2970,7 @@ def _gcLoop(sr, dryRun=False, immediate=False):
Util.log("No work, exiting")
break

if not lockRunning.acquireNoblock():
if not lockGCRunning.acquireNoblock():
Util.log("Unable to acquire GC running lock.")
return
try:
Expand Down Expand Up @@ -3013,15 +3012,15 @@ def _gcLoop(sr, dryRun=False, immediate=False):
continue

finally:
lockRunning.release()
lockGCRunning.release()
except:
task_status = "failure"
raise
finally:
sr.xapi.set_task_status(task_status)
Util.log("GC process exiting, no work left")
_create_init_file(sr.uuid)
lockActive.release()
lockGCActive.release()


def _xapi_enabled(session, hostref):
Expand Down Expand Up @@ -3069,19 +3068,19 @@ def _abort(srUuid, soft=False):
soft: If set to True and there is a pending abort signal, the function
doesn't do anything. If set to False, a new abort signal is issued.
returns: If soft is set to False, we return True holding lockActive. If
returns: If soft is set to False, we return True holding lockGCActive. If
soft is set to False and an abort signal is pending, we return False
without holding lockActive. An exception is raised in case of error."""
without holding lockGCActive. An exception is raised in case of error."""
Util.log("=== SR %s: abort ===" % (srUuid))
init(srUuid)
if not lockActive.acquireNoblock():
if not lockGCActive.acquireNoblock():
gotLock = False
Util.log("Aborting currently-running instance (SR %s)" % srUuid)
abortFlag = IPCFlag(srUuid)
if not abortFlag.set(FLAG_TYPE_ABORT, soft):
return False
for i in range(SR.LOCK_RETRY_ATTEMPTS):
gotLock = lockActive.acquireNoblock()
gotLock = lockGCActive.acquireNoblock()
if gotLock:
break
time.sleep(SR.LOCK_RETRY_INTERVAL)
Expand All @@ -3093,12 +3092,12 @@ def _abort(srUuid, soft=False):


def init(srUuid):
global lockRunning
if not lockRunning:
lockRunning = lock.Lock(LOCK_TYPE_RUNNING, srUuid)
global lockActive
if not lockActive:
lockActive = LockActive(srUuid)
global lockGCRunning
if not lockGCRunning:
lockGCRunning = lock.Lock(lock.LOCK_TYPE_GC_RUNNING, srUuid)
global lockGCActive
if not lockGCActive:
lockGCActive = LockActive(srUuid)


class LockActive:
Expand Down Expand Up @@ -3166,7 +3165,7 @@ def abort(srUuid, soft=False):
"""
if _abort(srUuid, soft):
Util.log("abort: releasing the process lock")
lockActive.release()
lockGCActive.release()
return True
else:
return False
Expand Down Expand Up @@ -3224,7 +3223,7 @@ def gc_force(session, srUuid, force=False, dryRun=False, lockSR=False):
Util.log("=== SR %s: gc_force ===" % srUuid)
init(srUuid)
sr = SR.getInstance(srUuid, session, lockSR, True)
if not lockActive.acquireNoblock():
if not lockGCActive.acquireNoblock():
abort(srUuid)
else:
Util.log("Nothing was running, clear to proceed")
Expand All @@ -3240,7 +3239,7 @@ def gc_force(session, srUuid, force=False, dryRun=False, lockSR=False):
finally:
sr.cleanup()
sr.logFilter.logState()
lockActive.release()
lockGCActive.release()


def get_state(srUuid):
Expand All @@ -3249,8 +3248,8 @@ def get_state(srUuid):
locking.
"""
init(srUuid)
if lockActive.acquireNoblock():
lockActive.release()
if lockGCActive.acquireNoblock():
lockGCActive.release()
return False
return True

Expand Down Expand Up @@ -3323,9 +3322,9 @@ def abort_optional_reenable(uuid):
ret = _abort(uuid)
input("Press enter to re-enable...")
print("GC/coalesce re-enabled")
lockRunning.release()
lockGCRunning.release()
if ret:
lockActive.release()
lockGCActive.release()


##############################################################################
Expand Down
3 changes: 1 addition & 2 deletions drivers/iscsilib.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import lock
import glob
import tempfile
from cleanup import LOCK_TYPE_RUNNING
from configparser import RawConfigParser
import io

Expand Down Expand Up @@ -54,7 +53,7 @@ def doexec_locked(cmd):
"""Executes via util.doexec the command specified whilst holding lock"""
_lock = None
if os.path.basename(cmd[0]) == 'iscsiadm':
_lock = lock.Lock(LOCK_TYPE_RUNNING, 'iscsiadm')
_lock = lock.Lock(lock.LOCK_TYPE_GC_RUNNING, 'iscsiadm')
_lock.acquire()
# util.SMlog("%s" % cmd)
(rc, stdout, stderr) = util.doexec(cmd)
Expand Down
1 change: 1 addition & 0 deletions drivers/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

VERBOSE = True

LOCK_TYPE_GC_RUNNING = "gc_running"

class LockException(util.SMException):
pass
Expand Down
3 changes: 1 addition & 2 deletions drivers/resetvdis.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@

def reset_sr(session, host_uuid, sr_uuid, is_sr_master):
from vhdutil import LOCK_TYPE_SR
from cleanup import LOCK_TYPE_RUNNING

cleanup.abort(sr_uuid)

gc_lock = lock.Lock(LOCK_TYPE_RUNNING, sr_uuid)
gc_lock = lock.Lock(lock.LOCK_TYPE_GC_RUNNING, sr_uuid)
sr_lock = lock.Lock(LOCK_TYPE_SR, sr_uuid)
gc_lock.acquire()
sr_lock.acquire()
Expand Down
Loading

0 comments on commit f841603

Please sign in to comment.