Skip to content

Commit 6cdd23c

Browse files
committed
CA-395221: use systemd target for gc enable
The xapi enabled state does not reflect whether the local xapi process is able to respond to requests and only whether the host is enabled to run new VMs. It is therefore not a useful check to ensure the GC will be able to make requests of xapi. Switch instead to making the service require the xapi-init-complete.target which will at least ensure the local xapi has completed its initialisation which was the original intent of this check. Signed-off-by: Mark Syms <mark.syms@cloud.com>
1 parent 09cd547 commit 6cdd23c

File tree

3 files changed

+2
-91
lines changed

3 files changed

+2
-91
lines changed

libs/sm/cleanup.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3115,32 +3115,8 @@ def _gcLoop(sr, dryRun=False, immediate=False):
31153115
lockGCActive.release()
31163116

31173117

3118-
def _xapi_enabled(session, hostref):
3119-
host = session.xenapi.host.get_record(hostref)
3120-
return host['enabled']
3121-
3122-
3123-
def _ensure_xapi_initialised(session):
3124-
"""
3125-
Don't want to start GC until Xapi is fully initialised
3126-
"""
3127-
local_session = None
3128-
if session is None:
3129-
local_session = util.get_localAPI_session()
3130-
session = local_session
3131-
3132-
try:
3133-
hostref = session.xenapi.host.get_by_uuid(util.get_this_host())
3134-
while not _xapi_enabled(session, hostref):
3135-
util.SMlog("Xapi not ready, GC waiting")
3136-
time.sleep(15)
3137-
finally:
3138-
if local_session is not None:
3139-
local_session.xenapi.session.logout()
3140-
31413118
def _gc(session, srUuid, dryRun=False, immediate=False):
31423119
init(srUuid)
3143-
_ensure_xapi_initialised(session)
31443120
sr = SR.getInstance(srUuid, session)
31453121
if not sr.gcEnabled(False):
31463122
return

systemd/SMGC@.service

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[Unit]
22
Description=Garbage Collector for SR %I
33
DefaultDependencies=no
4+
Requires=xapi-init-complete.target
45

56
[Service]
67
Type=oneshot

tests/test_cleanup.py

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ def test_term_handler(self):
184184

185185
@mock.patch('sm.cleanup._create_init_file', autospec=True)
186186
@mock.patch('sm.cleanup.SR', autospec=True)
187-
@mock.patch('sm.cleanup._ensure_xapi_initialised', autospec=True)
188-
def test_loop_exits_on_term(self, mock_init, mock_sr, mock_check_xapi):
187+
def test_loop_exits_on_term(self, mock_init, mock_sr):
189188
# Set the term signel
190189
cleanup.receiveSignal(signal.SIGTERM, None)
191190
mock_session = mock.MagicMock(name='MockSession')
@@ -1680,71 +1679,6 @@ def test_tag_children_for_relink_blocked(self):
16801679

16811680
self.assertGreater(self.mock_time_sleep.call_count, 5)
16821681

1683-
@mock.patch('sm.cleanup.util.get_this_host', autospec=True)
1684-
@mock.patch('sm.cleanup._gcLoop', autospec=True)
1685-
@mock.patch('sm.cleanup.SR.getInstance')
1686-
def test_check_for_xapi_running(
1687-
self, mock_sr, mock_loop, mock_this_host):
1688-
"""
1689-
Check we start immediately if xapi is enabled
1690-
"""
1691-
host_uuid = uuid4()
1692-
mock_this_host.return_value = host_uuid
1693-
1694-
mock_session = mock.MagicMock(name='MockSession')
1695-
mock_session.xenapi.host.get_record.return_value = {
1696-
'enabled': True
1697-
}
1698-
sr_uuid = uuid4()
1699-
1700-
cleanup._gc(mock_session, sr_uuid, False)
1701-
1702-
@mock.patch('sm.cleanup.util.get_this_host', autospec=True)
1703-
@mock.patch('sm.cleanup.util.get_localAPI_session', autospec=True)
1704-
@mock.patch('sm.cleanup._gcLoop', autospec=True)
1705-
@mock.patch('sm.cleanup.SR.getInstance')
1706-
def test_check_for_xapi_running_no_session(
1707-
self, mock_sr, mock_loop, mock_get_session, mock_this_host):
1708-
"""
1709-
Check we start immediately if xapi is enabled
1710-
"""
1711-
host_uuid = uuid4()
1712-
mock_this_host.return_value = host_uuid
1713-
mock_session = mock.MagicMock(name='MockSession')
1714-
mock_get_session.return_value = mock_session
1715-
1716-
mock_session.xenapi.host.get_record.return_value = {
1717-
'enabled': True
1718-
}
1719-
sr_uuid = uuid4()
1720-
1721-
cleanup._gc(None, sr_uuid, False)
1722-
1723-
@mock.patch('sm.cleanup.util.get_this_host', autospec=True)
1724-
@mock.patch('sm.cleanup.util.get_localAPI_session', autospec=True)
1725-
@mock.patch('sm.cleanup._gcLoop', autospec=True)
1726-
@mock.patch('sm.cleanup.SR.getInstance')
1727-
def test_waits_for_xapi_running(
1728-
self, mock_sr, mock_loop, mock_get_session, mock_this_host):
1729-
"""
1730-
Check we start immediately if xapi is enabled
1731-
"""
1732-
host_uuid = uuid4()
1733-
mock_this_host.return_value = host_uuid
1734-
mock_session = mock.MagicMock(name='MockSession')
1735-
mock_get_session.return_value = mock_session
1736-
1737-
mock_session.xenapi.host.get_record.side_effect = [
1738-
{'enabled': False},
1739-
{'enabled': False},
1740-
{'enabled': True}
1741-
]
1742-
sr_uuid = uuid4()
1743-
1744-
cleanup._gc(None, sr_uuid, False)
1745-
1746-
self.assertEqual(3, mock_session.xenapi.host.get_record.call_count)
1747-
17481682
def init_gc_loop_sr(self):
17491683
sr_uuid = str(uuid4())
17501684
mock_sr = mock.MagicMock(spec=cleanup.SR)

0 commit comments

Comments
 (0)