Clean up data when store receiving image occurs error

Clean up already received data from backend store to prevent potential
leaking when driver receiving image occurs error such as
ImageSizeLimitExceeded exception.

Fixes bug: 1214276

Change-Id: Ice1de4d1c61a62fff778acbbeb9bc27d03ed7ab4
Signed-off-by: Zhi Yan Liu <zhiyanl@cn.ibm.com>
This commit is contained in:
Zhi Yan Liu 2013-08-20 21:40:29 +08:00 committed by Gerrit Code Review
parent 35fcfdda09
commit 436c256955
6 changed files with 337 additions and 53 deletions

View File

@ -22,6 +22,7 @@ from oslo.config import cfg
import urlparse
from glance.common import exception
from glance.openstack.common import excutils
import glance.openstack.common.log as logging
import glance.store
import glance.store.base
@ -185,8 +186,14 @@ class Store(glance.store.base.Store):
LOG.debug(_("Adding a new image to GridFS with id %s and size %s") %
(image_id, image_size))
self.fs.put(image_file, _id=image_id)
image = self._get_file(loc)
try:
self.fs.put(image_file, _id=image_id)
image = self._get_file(loc)
except:
# Note(zhiyan): clean up already received data when
# error occurs such as ImageSizeLimitExceeded exception.
with excutils.save_and_reraise_exception():
self.fs.delete(image_id)
LOG.debug(_("Uploaded image %s, md5 %s, length %s to GridFS") %
(image._id, image.md5, image.length))

View File

@ -28,6 +28,7 @@ from oslo.config import cfg
from glance.common import exception
from glance.common import utils
from glance.openstack.common import excutils
import glance.openstack.common.log as logging
import glance.store
import glance.store.base
@ -224,27 +225,62 @@ class Store(glance.store.base.Store):
LOG.debug(msg)
raise exception.NotFound(msg)
def _create_image(self, fsid, ioctx, name, size, order):
def _create_image(self, fsid, ioctx, image_name, size, order):
"""
Create an rbd image. If librbd supports it,
make it a cloneable snapshot, so that copy-on-write
volumes can be created from it.
:param image_name Image's name
:retval `glance.store.rbd.StoreLocation` object
"""
librbd = rbd.RBD()
if hasattr(rbd, 'RBD_FEATURE_LAYERING'):
librbd.create(ioctx, name, size, order, old_format=False,
librbd.create(ioctx, image_name, size, order, old_format=False,
features=rbd.RBD_FEATURE_LAYERING)
return StoreLocation({
'fsid': fsid,
'pool': self.pool,
'image': name,
'image': image_name,
'snapshot': DEFAULT_SNAPNAME,
})
else:
librbd.create(ioctx, name, size, order, old_format=True)
return StoreLocation({'image': name})
librbd.create(ioctx, image_name, size, order, old_format=True)
return StoreLocation({'image': image_name})
def _delete_image(self, image_name, snapshot_name):
"""
Find the image file to delete.
:param image_name Image's name
:param snapshot_name Image snapshot's name
:raises NotFound if image does not exist;
InUseByStore if image is in use or snapshot unprotect failed
"""
with rados.Rados(conffile=self.conf_file, rados_id=self.user) as conn:
with conn.open_ioctx(self.pool) as ioctx:
if snapshot_name:
with rbd.Image(ioctx, image_name) as image:
try:
image.unprotect_snap(snapshot_name)
except rbd.ImageBusy:
log_msg = _("snapshot %s@%s could not be "
"unprotected because it is in use")
LOG.debug(log_msg % (image_name, snapshot_name))
raise exception.InUseByStore()
image.remove_snap(snapshot_name)
try:
rbd.RBD().remove(ioctx, image_name)
except rbd.ImageNotFound:
raise exception.NotFound(
_("RBD image %s does not exist") % image_name)
except rbd.ImageBusy:
log_msg = _("image %s could not be removed "
"because it is in use")
LOG.debug(log_msg % image_name)
raise exception.InUseByStore()
def add(self, image_id, image_file, image_size):
"""
@ -269,57 +305,42 @@ class Store(glance.store.base.Store):
fsid = conn.get_fsid()
with conn.open_ioctx(self.pool) as ioctx:
order = int(math.log(self.chunk_size, 2))
LOG.debug('creating image %s with order %d',
image_name, order)
LOG.debug('creating image %s with order %d', image_name, order)
try:
location = self._create_image(fsid, ioctx, image_name,
image_size, order)
loc = self._create_image(fsid, ioctx, image_name,
image_size, order)
except rbd.ImageExists:
raise exception.Duplicate(
_('RBD image %s already exists') % image_id)
with rbd.Image(ioctx, image_name) as image:
offset = 0
chunks = utils.chunkreadable(image_file, self.chunk_size)
for chunk in chunks:
offset += image.write(chunk, offset)
checksum.update(chunk)
if location.snapshot:
image.create_snap(location.snapshot)
image.protect_snap(location.snapshot)
try:
with rbd.Image(ioctx, image_name) as image:
offset = 0
chunks = utils.chunkreadable(image_file,
self.chunk_size)
for chunk in chunks:
offset += image.write(chunk, offset)
checksum.update(chunk)
if loc.snapshot:
image.create_snap(loc.snapshot)
image.protect_snap(loc.snapshot)
except:
# Note(zhiyan): clean up already received data when
# error occurs such as ImageSizeLimitExceeded exception.
with excutils.save_and_reraise_exception():
self._delete_image(loc.image, loc.snapshot)
return (location.get_uri(), image_size, checksum.hexdigest(), {})
return (loc.get_uri(), image_size, checksum.hexdigest(), {})
def delete(self, location):
"""
Takes a `glance.store.location.Location` object that indicates
where to find the image file to delete
where to find the image file to delete.
:location `glance.store.location.Location` object, supplied
from glance.store.location.get_location_from_uri()
:raises NotFound if image does not exist
:raises NotFound if image does not exist;
InUseByStore if image is in use or snapshot unprotect failed
"""
loc = location.store_location
with rados.Rados(conffile=self.conf_file, rados_id=self.user) as conn:
with conn.open_ioctx(self.pool) as ioctx:
if loc.snapshot:
with rbd.Image(ioctx, loc.image) as image:
try:
image.unprotect_snap(loc.snapshot)
except rbd.ImageBusy:
log_msg = _("snapshot %s@%s could not be "
"unprotected because it is in use")
LOG.debug(log_msg % (loc.image, loc.snapshot))
raise exception.InUseByStore()
image.remove_snap(loc.snapshot)
try:
rbd.RBD().remove(ioctx, loc.image)
except rbd.ImageNotFound:
raise exception.NotFound(
_('RBD image %s does not exist') % loc.image)
except rbd.ImageBusy:
log_msg = _("image %s could not be removed"
"because it is in use")
LOG.debug(log_msg % loc.image)
raise exception.InUseByStore()
self._delete_image(loc.image, loc.snapshot)

View File

@ -22,6 +22,7 @@ import hashlib
from oslo.config import cfg
from glance.common import exception
from glance.openstack.common import excutils
import glance.openstack.common.log as logging
from glance.openstack.common import processutils
import glance.store
@ -271,13 +272,19 @@ class Store(glance.store.base.Store):
image.create(image_size)
total = left = image_size
while left > 0:
length = min(self.chunk_size, left)
data = image_file.read(length)
image.write(data, total - left, length)
left -= length
checksum.update(data)
try:
total = left = image_size
while left > 0:
length = min(self.chunk_size, left)
data = image_file.read(length)
image.write(data, total - left, length)
left -= length
checksum.update(data)
except:
# Note(zhiyan): clean up already received data when
# error occurs such as ImageSizeLimitExceeded exception.
with excutils.save_and_reraise_exception():
image.delete()
return (location.get_uri(), image_size, checksum.hexdigest(), {})

View File

@ -0,0 +1,97 @@
# Copyright 2013 OpenStack Foundation
# 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 StringIO
import stubout
from glance.common import exception
from glance.common import utils
from glance.store.gridfs import Store
from glance.tests.unit import base
try:
import gridfs
import pymongo
except ImportError:
pymongo = None
GRIDFS_CONF = {'verbose': True,
'debug': True,
'default_store': 'gridfs',
'mongodb_store_uri': 'mongodb://fake_store_uri',
'mongodb_store_db': 'fake_store_db'}
def stub_out_gridfs(stubs):
class FakeMongoClient(object):
def __init__(self, *args, **kwargs):
pass
def __getitem__(self, key):
return None
class FakeGridFS(object):
image_data = {}
called_commands = []
def __init__(self, *args, **kwargs):
pass
def exists(self, image_id):
self.called_commands.append('exists')
return False
def put(self, image_file, _id):
self.called_commands.append('put')
data = None
while True:
data = image_file.read(64)
if data:
self.image_data[_id] = \
self.image_data.setdefault(_id, '') + data
else:
break
def delete(self, _id):
self.called_commands.append('delete')
if pymongo is not None:
stubs.Set(pymongo, 'MongoClient', FakeMongoClient)
stubs.Set(gridfs, 'GridFS', FakeGridFS)
class TestStore(base.StoreClearingUnitTest):
def setUp(self):
"""Establish a clean test environment"""
self.config(**GRIDFS_CONF)
super(TestStore, self).setUp()
self.stubs = stubout.StubOutForTesting()
stub_out_gridfs(self.stubs)
self.store = Store()
self.addCleanup(self.stubs.UnsetAll)
def test_cleanup_when_add_image_exception(self):
if pymongo is None:
msg = 'GridFS store can not add images, skip test.'
self.skipTest(msg)
self.assertRaises(exception.ImageSizeLimitExceeded,
self.store.add,
'fake_image_id',
utils.LimitingReader(StringIO.StringIO('xx'), 1),
2)
self.assertEqual(self.store.fs.called_commands,
['exists', 'put', 'delete'])

View File

@ -0,0 +1,92 @@
# Copyright 2013 OpenStack Foundation
# 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 contextlib
import StringIO
import stubout
from glance.common import exception
from glance.common import utils
from glance.store.rbd import Store
from glance.store.rbd import StoreLocation
from glance.tests.unit import base
try:
import rados
import rbd
except ImportError:
rbd = None
RBD_CONF = {'verbose': True,
'debug': True,
'default_store': 'rbd'}
FAKE_CHUNKSIZE = 1
class TestStore(base.StoreClearingUnitTest):
def setUp(self):
"""Establish a clean test environment"""
self.config(**RBD_CONF)
super(TestStore, self).setUp()
self.stubs = stubout.StubOutForTesting()
self.store = Store()
self.store.chunk_size = FAKE_CHUNKSIZE
self.addCleanup(self.stubs.UnsetAll)
def test_cleanup_when_add_image_exception(self):
if rbd is None:
msg = 'RBD store can not add images, skip test.'
self.skipTest(msg)
called_commands = []
class FakeConnection(object):
@contextlib.contextmanager
def open_ioctx(self, *args, **kwargs):
yield None
class FakeImage(object):
def write(self, *args, **kwargs):
called_commands.append('write')
return FAKE_CHUNKSIZE
@contextlib.contextmanager
def _fake_rados(*args, **kwargs):
yield FakeConnection()
@contextlib.contextmanager
def _fake_image(*args, **kwargs):
yield FakeImage()
def _fake_create_image(*args, **kwargs):
called_commands.append('create')
return StoreLocation({'image': 'fake_image',
'snapshot': 'fake_snapshot'})
def _fake_delete_image(*args, **kwargs):
called_commands.append('delete')
self.stubs.Set(rados, 'Rados', _fake_rados)
self.stubs.Set(rbd, 'Image', _fake_image)
self.stubs.Set(self.store, '_create_image', _fake_create_image)
self.stubs.Set(self.store, '_delete_image', _fake_delete_image)
self.assertRaises(exception.ImageSizeLimitExceeded,
self.store.add,
'fake_image_id',
utils.LimitingReader(StringIO.StringIO('xx'), 1),
2)
self.assertEqual(called_commands, ['create', 'write', 'delete'])

View File

@ -0,0 +1,60 @@
# Copyright 2013 OpenStack Foundation
# 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 StringIO
import stubout
from glance.common import exception
from glance.common import utils
from glance.openstack.common import processutils
import glance.store.sheepdog
from glance.store.sheepdog import Store
from glance.tests.unit import base
SHEEPDOG_CONF = {'verbose': True,
'debug': True,
'default_store': 'sheepdog'}
class TestStore(base.StoreClearingUnitTest):
def setUp(self):
"""Establish a clean test environment"""
def _fake_execute(*cmd, **kwargs):
pass
self.config(**SHEEPDOG_CONF)
super(TestStore, self).setUp()
self.stubs = stubout.StubOutForTesting()
self.stubs.Set(processutils, 'execute', _fake_execute)
self.store = Store()
self.addCleanup(self.stubs.UnsetAll)
def test_cleanup_when_add_image_exception(self):
called_commands = []
def _fake_run_command(self, command, data, *params):
called_commands.append(command)
self.stubs.Set(glance.store.sheepdog.SheepdogImage,
'_run_command', _fake_run_command)
self.assertRaises(exception.ImageSizeLimitExceeded,
self.store.add,
'fake_image_id',
utils.LimitingReader(StringIO.StringIO('xx'), 1),
2)
self.assertEqual(called_commands, ['list -r', 'create', 'delete'])