Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libvirt: Kill rsync/scp processes before deleting instance
In the resize operation, during copying files from source to destination compute node scp/rsync processes are not aborted after the instance is deleted because linux kernel doesn't delete instance files physically until all processes using the file handle is closed completely. Hence rsync/scp process keeps on running until it transfers 100% of file data. Added new module instancejobtracker to libvirt driver which will add, remove or terminate the processes running against particular instances. Added callback methods to execute call which will store the pid of scp/rsync process in cache as a key: value pair and to remove the pid from the cache after process completion. Process id will be used to kill the process if it is running while deleting the instance. Instance uuid is used as a key in the cache and pid will be the value. Conflicts: nova/virt/libvirt/driver.py SecurityImpact Closes-bug: #1387543 Change-Id: Ie03acc00a7c904aec13c90ae6a53938d08e5e0c9 (cherry picked from commit 7ab75d5)
- Loading branch information
1 parent
90e1eac
commit b5020a0
Showing
5 changed files
with
168 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright 2015 NTT corp. | ||
# All Rights Reserved. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
|
||
import collections | ||
import errno | ||
import os | ||
import signal | ||
|
||
from oslo_log import log as logging | ||
|
||
from nova.i18n import _LE | ||
from nova.i18n import _LW | ||
|
||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class InstanceJobTracker(object): | ||
def __init__(self): | ||
self.jobs = collections.defaultdict(list) | ||
|
||
def add_job(self, instance, pid): | ||
"""Appends process_id of instance to cache. | ||
This method will store the pid of a process in cache as | ||
a key: value pair which will be used to kill the process if it | ||
is running while deleting the instance. Instance uuid is used as | ||
a key in the cache and pid will be the value. | ||
:param instance: Object of instance | ||
:param pid: Id of the process | ||
""" | ||
self.jobs[instance.uuid].append(pid) | ||
|
||
def remove_job(self, instance, pid): | ||
"""Removes pid of process from cache. | ||
This method will remove the pid of a process from the cache. | ||
:param instance: Object of instance | ||
:param pid: Id of the process | ||
""" | ||
uuid = instance.uuid | ||
if uuid in self.jobs and pid in self.jobs[uuid]: | ||
self.jobs[uuid].remove(pid) | ||
|
||
# remove instance.uuid if no pid's remaining | ||
if not self.jobs[uuid]: | ||
self.jobs.pop(uuid, None) | ||
|
||
def terminate_jobs(self, instance): | ||
"""Kills the running processes for given instance. | ||
This method is used to kill all running processes of the instance if | ||
it is deleted in between. | ||
:param instance: Object of instance | ||
""" | ||
pids_to_remove = list(self.jobs.get(instance.uuid, [])) | ||
for pid in pids_to_remove: | ||
try: | ||
# Try to kill the process | ||
os.kill(pid, signal.SIGKILL) | ||
except OSError as exc: | ||
if exc.errno != errno.ESRCH: | ||
LOG.error(_LE('Failed to kill process %(pid)s ' | ||
'due to %(reason)s, while deleting the ' | ||
'instance.'), {'pid': pid, 'reason': exc}, | ||
instance=instance) | ||
|
||
try: | ||
# Check if the process is still alive. | ||
os.kill(pid, 0) | ||
except OSError as exc: | ||
if exc.errno != errno.ESRCH: | ||
LOG.error(_LE('Unexpected error while checking process ' | ||
'%(pid)s.'), {'pid': pid}, | ||
instance=instance) | ||
else: | ||
# The process is still around | ||
LOG.warn(_LW("Failed to kill a long running process " | ||
"%(pid)s related to the instance when " | ||
"deleting it."), {'pid': pid}, | ||
instance=instance) | ||
|
||
self.remove_job(instance, pid) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters