makes uri unique to avoid duplicated pools

I added an index to the mongodb collection to make uri unique and
avoid the creation of pools with duplicated URIs.

Closes-bug: 1416206

Change-Id: Ia1e9ceb0a89673238a71b48de1ea22eaa7d79087
This commit is contained in:
dynarro 2015-03-30 17:22:22 +02:00
parent 2cac45f1ce
commit 1e992e19a4
11 changed files with 51 additions and 17 deletions

View File

@ -51,7 +51,7 @@ class PoolCatalogTest(testing.TestBase):
self.pools_ctrl.create(self.pool, 100, 'mongodb://localhost:27017')
self.pools_ctrl.create(self.pool2, 100,
'mongodb://localhost:27017',
'mongodb://127.0.0.1:27017',
group=self.pool_group)
self.catalogue_ctrl.insert(self.project, self.queue, self.pool)
self.catalog = pooling.Catalog(self.conf, cache, control)

View File

@ -33,6 +33,10 @@ POOLS_INDEX = [
('n', 1)
]
URI_INDEX = [
('u', 1)
]
# NOTE(cpp-cabrera): used for get/list operations. There's no need to
# show the marker or the _id - they're implementation details.
OMIT_FIELDS = (('_id', False),)
@ -53,6 +57,11 @@ class PoolsController(base.PoolsBase):
name='pools_name',
unique=True)
self._col.ensure_index(URI_INDEX,
background=True,
name='pools_uri',
unique=True)
@utils.raises_conn_error
def _list(self, marker=None, limit=10, detailed=False):
query = {}

View File

@ -68,7 +68,11 @@ class ControllerBaseTest(testing.TestBase):
else:
uri = "mongodb://localhost:27017"
for i in range(4):
options = {'database': "zaqar_test_pools_" + str(i)}
db_name = "zaqar_test_pools_" + str(i)
# NOTE(dynarro): we need to create a unique uri.
uri = "%s/%s" % (uri, db_name)
options = {'database': db_name}
self.control.pools_controller.create(six.text_type(i),
100, uri, options=options)
self.driver = self.driver_class(self.conf, cache, self.control)
@ -1099,20 +1103,20 @@ class PoolsControllerTest(ControllerBaseTest):
def test_create_succeeds(self):
self.pools_controller.create(str(uuid.uuid1()),
100, 'localhost',
100, 'localhost:13124',
options={})
def test_create_replaces_on_duplicate_insert(self):
name = str(uuid.uuid1())
self.pools_controller.create(name,
100, 'localhost',
100, 'localhost:76553',
options={})
self.pools_controller.create(name,
111, 'localhost2',
111, 'localhost:758353',
options={})
entry = self.pools_controller.get(name)
self._pool_expects(entry, xname=name, xweight=111,
xlocation='localhost2')
xlocation='localhost:758353')
def _pool_expects(self, pool, xname, xweight, xlocation):
self.assertIn('name', pool)
@ -1398,7 +1402,7 @@ class FlavorsControllerTest(ControllerBaseTest):
capabilities={})
pool2 = 'another_pool'
self.pools_controller.create(pool2, 100, 'localhost',
self.pools_controller.create(pool2, 100, 'localhost:27017',
group=pool2, options={})
self.addCleanup(self.pools_controller.delete, pool2)
@ -1487,7 +1491,8 @@ class FlavorsControllerTest(ControllerBaseTest):
name_gen = lambda i: chr(ord('A') + i)
for i in range(15):
pool = str(i)
self.pools_controller.create(pool, 100, 'localhost',
uri = 'localhost:2701' + pool
self.pools_controller.create(pool, 100, uri,
group=pool, options={})
self.addCleanup(self.pools_controller.delete, pool)

View File

@ -43,6 +43,7 @@ class TestDefaultLimits(base.V1Base):
def test_queue_listing(self):
# 2 queues to list
self.addCleanup(self.simulate_delete, self.queue_path + '/q2')
self.simulate_put(self.queue_path + '/q2')
self.assertEqual(self.srmock.status, falcon.HTTP_201)

View File

@ -34,10 +34,9 @@ class MessagesBaseTest(base.V1Base):
def setUp(self):
super(MessagesBaseTest, self).setUp()
if self.conf.pooling:
for i in range(4):
uri = self.conf['drivers:management_store:mongodb'].uri
uri = "%s/%s" % ('mongodb://localhost:27017', str(i))
doc = {'weight': 100, 'uri': uri}
self.simulate_put(self.url_prefix + '/pools/' + str(i),
body=jsonutils.dumps(doc))

View File

@ -38,6 +38,7 @@ def pool(test, name, weight, uri, options={}):
:returns: (name, weight, uri, options)
:rtype: see above
"""
uri = "%s/%s" % (uri, str(uuid.uuid4()))
doc = {'weight': weight, 'uri': uri, 'options': options}
path = test.url_prefix + '/pools/' + name
@ -67,6 +68,7 @@ def pools(test, count, uri):
{str(i): i})
for i in range(count)]
for path, weight, option in args:
uri = "%s/%s" % (uri, str(uuid.uuid4()))
doc = {'weight': weight, 'uri': uri, 'options': option}
test.simulate_put(path, body=jsonutils.dumps(doc))
@ -160,7 +162,10 @@ class PoolsBaseTest(base.V1Base):
self.assertIn('weight', pool)
self.assertEqual(pool['weight'], xweight)
self.assertIn('uri', pool)
self.assertEqual(pool['uri'], xuri)
# NOTE(dynarro): we are using startwith because we are adding to
# pools UUIDs, to avoid dupplications
self.assertTrue(pool['uri'].startswith(xuri))
def test_get_works(self):
result = self.simulate_get(self.pool)

View File

@ -39,7 +39,7 @@ class MessagesBaseTest(base.V1_1Base):
if self.conf.pooling:
for i in range(4):
uri = self.conf['drivers:management_store:mongodb'].uri
uri = "%s/%s" % ('mongodb://localhost:27017', str(i))
doc = {'weight': 100, 'uri': uri}
self.simulate_put(self.url_prefix + '/pools/' + str(i),
body=jsonutils.dumps(doc))

View File

@ -38,11 +38,13 @@ def pool(test, name, weight, uri, group=None, options={}):
:returns: (name, weight, uri, options)
:rtype: see above
"""
uri = "%s/%s" % (uri, str(uuid.uuid4()))
doc = {'weight': weight, 'uri': uri,
'group': group, 'options': options}
path = test.url_prefix + '/pools/' + name
test.simulate_put(path, body=jsonutils.dumps(doc))
test.addCleanup(test.simulate_delete, path)
try:
yield name, weight, uri, group, options
@ -68,6 +70,7 @@ def pools(test, count, uri, group):
{str(i): i})
for i in range(count)]
for path, weight, option in args:
uri = "%s/%s" % (uri, str(uuid.uuid4()))
doc = {'weight': weight, 'uri': uri,
'group': group, 'options': option}
test.simulate_put(path, body=jsonutils.dumps(doc))
@ -164,7 +167,10 @@ class PoolsBaseTest(base.V1_1Base):
self.assertIn('weight', pool)
self.assertEqual(pool['weight'], xweight)
self.assertIn('uri', pool)
self.assertEqual(pool['uri'], xuri)
# NOTE(dynarro): we are using startwith because we are adding to
# pools UUIDs, to avoid dupplications
self.assertTrue(pool['uri'].startswith(xuri))
def test_get_works(self):
result = self.simulate_get(self.pool)

View File

@ -91,7 +91,7 @@ class FlavorsBaseTest(base.V2Base):
self.pool_path = self.url_prefix + '/pools/' + self.pool
self.pool_doc = {'weight': 100,
'group': self.pool_group,
'uri': 'mongodb://localhost:27017'}
'uri': 'mongodb://localhost:27017/test'}
self.simulate_put(self.pool_path, body=jsonutils.dumps(self.pool_doc))
self.flavor = 'test-flavor'

View File

@ -38,9 +38,13 @@ class MessagesBaseTest(base.V2Base):
self.default_message_ttl = self.boot.transport._defaults.message_ttl
if self.conf.pooling:
uri = "mongodb://localhost:27017"
for i in range(4):
uri = self.conf['drivers:management_store:mongodb'].uri
doc = {'weight': 100, 'uri': uri}
db_name = "zaqar_test_pools_" + str(i)
# NOTE(dynarro): we need to create a unique uri.
uri = "%s/%s" % (uri, db_name)
options = {'database': db_name}
doc = {'weight': 100, 'uri': uri, 'options': options}
self.simulate_put(self.url_prefix + '/pools/' + str(i),
body=jsonutils.dumps(doc))
self.assertEqual(self.srmock.status, falcon.HTTP_201)

View File

@ -38,6 +38,7 @@ def pool(test, name, weight, uri, group=None, options={}):
:returns: (name, weight, uri, options)
:rtype: see above
"""
uri = "%s/%s" % (uri, str(uuid.uuid4()))
doc = {'weight': weight, 'uri': uri,
'group': group, 'options': options}
path = test.url_prefix + '/pools/' + name
@ -68,6 +69,7 @@ def pools(test, count, uri, group):
{str(i): i})
for i in range(count)]
for path, weight, option in args:
uri = "%s/%s" % (uri, str(uuid.uuid4()))
doc = {'weight': weight, 'uri': uri,
'group': group, 'options': option}
test.simulate_put(path, body=jsonutils.dumps(doc))
@ -164,7 +166,10 @@ class PoolsBaseTest(base.V2Base):
self.assertIn('weight', pool)
self.assertEqual(pool['weight'], xweight)
self.assertIn('uri', pool)
self.assertEqual(pool['uri'], xuri)
# NOTE(dynarro): we are using startwith because we are adding to
# pools UUIDs, to avoid dupplications
self.assertTrue(pool['uri'].startswith(xuri))
def test_get_works(self):
result = self.simulate_get(self.pool)