Skip to content

Commit e846f53

Browse files
MarkSymsCtxWescoeur
authored andcommitted
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 20bf7c4 commit e846f53

3 files changed

Lines changed: 2 additions & 91 deletions

File tree

drivers/cleanup.py

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

31333133

3134-
def _xapi_enabled(session, hostref):
3135-
host = session.xenapi.host.get_record(hostref)
3136-
return host['enabled']
3137-
3138-
3139-
def _ensure_xapi_initialised(session):
3140-
"""
3141-
Don't want to start GC until Xapi is fully initialised
3142-
"""
3143-
local_session = None
3144-
if session is None:
3145-
local_session = util.get_localAPI_session()
3146-
session = local_session
3147-
3148-
try:
3149-
hostref = session.xenapi.host.get_by_uuid(util.get_this_host())
3150-
while not _xapi_enabled(session, hostref):
3151-
util.SMlog("Xapi not ready, GC waiting")
3152-
time.sleep(15)
3153-
finally:
3154-
if local_session is not None:
3155-
local_session.xenapi.session.logout()
3156-
31573134
def _gc(session, srUuid, dryRun=False, immediate=False):
31583135
init(srUuid)
3159-
_ensure_xapi_initialised(session)
31603136
sr = SR.getInstance(srUuid, session)
31613137
if not sr.gcEnabled(False):
31623138
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
@@ -189,8 +189,7 @@ def test_term_handler(self):
189189

190190
@mock.patch('cleanup._create_init_file', autospec=True)
191191
@mock.patch('cleanup.SR', autospec=True)
192-
@mock.patch('cleanup._ensure_xapi_initialised', autospec=True)
193-
def test_loop_exits_on_term(self, mock_init, mock_sr, mock_check_xapi):
192+
def test_loop_exits_on_term(self, mock_init, mock_sr):
194193
# Set the term signel
195194
cleanup.receiveSignal(signal.SIGTERM, None)
196195
mock_session = mock.MagicMock(name='MockSession')
@@ -1685,71 +1684,6 @@ def test_tag_children_for_relink_blocked(self):
16851684

16861685
self.assertGreater(self.mock_time_sleep.call_count, 5)
16871686

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

0 commit comments

Comments
 (0)