Objects changes for horizontal-scale support

Add objects APIs for creating/stealing/releasing baylocks.

Change-Id: I756ea8b6983a51f1447dc77b99ba1ac407f7debf
Partial-Implements: blueprint horizontal-scale
This commit is contained in:
Hongbin Lu 2015-04-10 23:34:17 +00:00
parent 5269daad88
commit 91545bff6b
4 changed files with 105 additions and 0 deletions

View File

@ -13,6 +13,7 @@
# under the License.
from magnum.objects import bay
from magnum.objects import baylock
from magnum.objects import baymodel
from magnum.objects import container
from magnum.objects import node
@ -23,6 +24,7 @@ from magnum.objects import service
Container = container.Container
Bay = bay.Bay
BayLock = baylock.BayLock
BayModel = baymodel.BayModel
Node = node.Node
Pod = pod.Pod
@ -30,6 +32,7 @@ ReplicationController = rc.ReplicationController
Service = service.Service
__all__ = (Bay,
BayLock,
BayModel,
Container,
Node,

42
magnum/objects/baylock.py Normal file
View File

@ -0,0 +1,42 @@
#
# 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.
from magnum.db import api as dbapi
from magnum.objects import base
from magnum.objects import utils as obj_utils
class BayLock(base.MagnumObject):
# Version 1.0: Initial version
VERSION = '1.0'
dbapi = dbapi.get_instance()
fields = {
'id': int,
'bay_uuid': obj_utils.str_or_none,
'conductor_id': obj_utils.str_or_none,
}
@base.remotable_classmethod
def create(cls, bay_uuid, conductor_id):
return cls.dbapi.create_bay_lock(bay_uuid, conductor_id)
@base.remotable_classmethod
def steal(cls, bay_uuid, old_conductor_id, new_conductor_id):
return cls.dbapi.steal_bay_lock(bay_uuid, old_conductor_id,
new_conductor_id)
@base.remotable_classmethod
def release(cls, bay_uuid, conductor_id):
return cls.dbapi.release_bay_lock(bay_uuid, conductor_id)

View File

@ -228,3 +228,12 @@ def create_test_rc(**kw):
del service['id']
dbapi = db_api.get_instance()
return dbapi.create_rc(service)
def get_test_baylock(**kw):
return {
'id': kw.get('id', 42),
'bay_uuid': kw.get('bay_uuid', '5d12f6fd-a196-4bf0-ae4c-1f639a523a52'),
'conductor_id': kw.get('conductor_id',
'72625085-c507-4410-9b28-cd7cf1fbf1ad'),
}

View File

@ -0,0 +1,51 @@
# 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 mock
import uuid
from magnum import objects
from magnum.tests.unit.db import base
from magnum.tests.unit.db import utils
class TestBayLockObject(base.DbTestCase):
def setUp(self):
super(TestBayLockObject, self).setUp()
baylock_dict = utils.get_test_baylock()
self.bay_uuid = baylock_dict['bay_uuid']
self.conductor_id = baylock_dict['conductor_id']
def test_create(self):
with mock.patch.object(self.dbapi, 'create_bay_lock',
autospec=True) as mock_create_baylock:
objects.BayLock.create(self.bay_uuid, self.conductor_id)
mock_create_baylock.assert_called_once_with(self.bay_uuid,
self.conductor_id)
def test_steal(self):
with mock.patch.object(self.dbapi, 'steal_bay_lock',
autospec=True) as mock_steal_baylock:
old_conductor_id = self.conductor_id
new_conductor_id = str(uuid.uuid4())
objects.BayLock.steal(self.bay_uuid, old_conductor_id,
new_conductor_id)
mock_steal_baylock.assert_called_once_with(self.bay_uuid,
old_conductor_id, new_conductor_id)
def test_release(self):
with mock.patch.object(self.dbapi, 'release_bay_lock',
autospec=True) as mock_release_baylock:
objects.BayLock.release(self.bay_uuid, self.conductor_id)
mock_release_baylock.assert_called_once_with(self.bay_uuid,
self.conductor_id)