Ensure intended indices on project_id are created for mongo

Fixes bug 1291666

Previously, despite the clear intent of the code, the mongodb
driver failed to create indices on project_id for the meter
and resource collections, due to a index name clash.

Now, we ensure a different index name is used for the user_id
and project_id indices on each collection.

The previously missing index is created in the background to
avoid blocking other DB operations when upgrading against an
existing mongo instance with large collections.

Change-Id: Ia3c6765445f16ab9beafbd7fd9265a045814003f
This commit is contained in:
Eoghan Glynn 2014-03-12 23:36:12 +00:00
parent 8fb8d26ee6
commit 9ffba6fb70

View File

@ -453,18 +453,24 @@ class Connection(pymongo_base.Connection):
# project_id values are usually mutually exclusive in the
# queries, so the database won't take advantage of an index
# including both.
name_qualifier = dict(user_id='', project_id='project_')
background = dict(user_id=False, project_id=True)
for primary in ['user_id', 'project_id']:
name = 'resource_%sidx' % name_qualifier[primary]
self.db.resource.ensure_index([
(primary, pymongo.ASCENDING),
('source', pymongo.ASCENDING),
], name='resource_idx')
], name=name, background=background[primary])
name = 'meter_%sidx' % name_qualifier[primary]
self.db.meter.ensure_index([
('resource_id', pymongo.ASCENDING),
(primary, pymongo.ASCENDING),
('counter_name', pymongo.ASCENDING),
('timestamp', pymongo.ASCENDING),
('source', pymongo.ASCENDING),
], name='meter_idx')
], name=name, background=background[primary])
self.db.resource.ensure_index([('last_sample_timestamp',
pymongo.DESCENDING)],
name='last_sample_timestamp_idx',