From 6602bbfc0854c4b014100c00e1b821f8a3190255 Mon Sep 17 00:00:00 2001 From: Zhiteng Huang Date: Thu, 4 Oct 2012 17:34:01 +0800 Subject: [PATCH] Add 'create_volume' to scheduler RPC API. Instead of directly call RPC methods, 'create_volume' should be implemented in scheduler RPC API in order to do versioning. This increases scheduler RPC version to 1.1 Change-Id: I22d5a097ec3a73c3460f4acd35c602da57f8b52d --- cinder/scheduler/manager.py | 2 +- cinder/scheduler/rpcapi.py | 11 +++++++++++ cinder/tests/scheduler/test_rpcapi.py | 11 ++++++++++- cinder/volume/api.py | 17 +++++++++-------- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/cinder/scheduler/manager.py b/cinder/scheduler/manager.py index c5ae6598f..fae6f2004 100644 --- a/cinder/scheduler/manager.py +++ b/cinder/scheduler/manager.py @@ -45,7 +45,7 @@ FLAGS.register_opt(scheduler_driver_opt) class SchedulerManager(manager.Manager): """Chooses a host to create volumes""" - RPC_API_VERSION = '1.0' + RPC_API_VERSION = '1.1' def __init__(self, scheduler_driver=None, *args, **kwargs): if not scheduler_driver: diff --git a/cinder/scheduler/rpcapi.py b/cinder/scheduler/rpcapi.py index 325edcd33..a253f0d29 100644 --- a/cinder/scheduler/rpcapi.py +++ b/cinder/scheduler/rpcapi.py @@ -31,6 +31,7 @@ class SchedulerAPI(cinder.openstack.common.rpc.proxy.RpcProxy): API version history: 1.0 - Initial version. + 1.1 - Add create_volume() method ''' RPC_API_VERSION = '1.0' @@ -39,6 +40,16 @@ class SchedulerAPI(cinder.openstack.common.rpc.proxy.RpcProxy): super(SchedulerAPI, self).__init__(topic=FLAGS.scheduler_topic, default_version=self.RPC_API_VERSION) + def create_volume(self, ctxt, topic, volume_id, snapshot_id=None, + image_id=None): + return self.cast(ctxt, self.make_msg('create_volume', + topic=topic, + volume_id=volume_id, + snapshot_id=snapshot_id, + image_id=image_id), + topic=None, + version='1.1') + def update_service_capabilities(self, ctxt, service_name, host, capabilities): self.fanout_cast(ctxt, self.make_msg('update_service_capabilities', diff --git a/cinder/tests/scheduler/test_rpcapi.py b/cinder/tests/scheduler/test_rpcapi.py index a6ba60d45..7b61fa9ab 100644 --- a/cinder/tests/scheduler/test_rpcapi.py +++ b/cinder/tests/scheduler/test_rpcapi.py @@ -40,8 +40,9 @@ class SchedulerRpcAPITestCase(test.TestCase): ctxt = context.RequestContext('fake_user', 'fake_project') rpcapi = scheduler_rpcapi.SchedulerAPI() expected_retval = 'foo' if method == 'call' else None + expected_version = kwargs.pop('version', rpcapi.RPC_API_VERSION) expected_msg = rpcapi.make_msg(method, **kwargs) - expected_msg['version'] = rpcapi.RPC_API_VERSION + expected_msg['version'] = expected_version self.fake_args = None self.fake_kwargs = None @@ -65,3 +66,11 @@ class SchedulerRpcAPITestCase(test.TestCase): self._test_scheduler_api('update_service_capabilities', rpc_method='fanout_cast', service_name='fake_name', host='fake_host', capabilities='fake_capabilities') + + def test_create_volume(self): + self._test_scheduler_api('create_volume', + rpc_method='cast', topic='fake_topic', + volume_id='fake_volume_id', + snapshot_id='fake_snapshot_id', + image_id='fake_image_id', + version='1.1') diff --git a/cinder/volume/api.py b/cinder/volume/api.py index 418b1ef78..49400b73d 100644 --- a/cinder/volume/api.py +++ b/cinder/volume/api.py @@ -34,7 +34,8 @@ from cinder.openstack.common import timeutils from cinder.volume import volume_types import cinder.policy from cinder import quota - +from cinder.scheduler import rpcapi as scheduler_rpcapi +from cinder.volume import volume_types volume_host_opt = cfg.BoolOpt('snapshot_same_host', default=True, @@ -79,6 +80,7 @@ class API(base.Base): def __init__(self, db_driver=None, image_service=None): self.image_service = (image_service or glance.get_default_image_service()) + self.scheduler_rpcapi = scheduler_rpcapi.SchedulerAPI() super(API, self).__init__(db_driver) def create(self, context, size, name, description, snapshot=None, @@ -195,6 +197,7 @@ class API(base.Base): topic = rpc.queue_get_for(context, FLAGS.volume_topic, src_volume_ref['host']) + # bypass scheduler and send request directly to volume rpc.cast(context, topic, {"method": "create_volume", @@ -202,13 +205,11 @@ class API(base.Base): "snapshot_id": snapshot_id, "image_id": image_id}}) else: - rpc.cast(context, - FLAGS.scheduler_topic, - {"method": "create_volume", - "args": {"topic": FLAGS.volume_topic, - "volume_id": volume_id, - "snapshot_id": snapshot_id, - "image_id": image_id}}) + self.scheduler_rpcapi.create_volume(context, + FLAGS.volume_topic, + volume_id, + snapshot_id=snapshot_id, + image_id=image_id) @wrap_check_policy def delete(self, context, volume, force=False): -- 2.45.2