Merge lp:~cbehrens/nova/lp854239 into lp:~hudson-openstack/nova/trunk

Proposed by Chris Behrens
Status: Merged
Approved by: Devin Carlen
Approved revision: 1598
Merged at revision: 1597
Proposed branch: lp:~cbehrens/nova/lp854239
Merge into: lp:~hudson-openstack/nova/trunk
Diff against target: 230 lines (+37/-51)
7 files modified
nova/compute/api.py (+3/-2)
nova/db/api.py (+6/-7)
nova/db/sqlalchemy/api.py (+12/-28)
nova/network/manager.py (+3/-6)
nova/tests/fake_network.py (+5/-3)
nova/tests/test_compute.py (+2/-2)
nova/tests/test_db_api.py (+6/-3)
To merge this branch: bzr merge lp:~cbehrens/nova/lp854239
Reviewer Review Type Date Requested Status
Devin Carlen (community) Approve
Jason Kölker (community) Approve
Kevin L. Mitchell (community) Approve
Josh Kearney (community) Approve
Brian Waldon Pending
Review via email: mp+76121@code.launchpad.net

Description of the change

Reverted some changes to instance_get_all_by_filters() that was added in rev 1594. An additional argument for filtering on instance uuids is not needed, as you can add 'uuid: uuid_list' into the filters dictionary. Just needed to add 'uuid' as an exact_match_filter. This restores the filtering to do a single DB query.

Also updated ID/UUID mapping code to be a little more efficient, by returning a dictionary of 'ID: UUID'... vs a list.

Fixed a test that assumed list order.

A couple of typo fixes and a pep8 issue in trunk also fixed.

To post a comment you must log in.
Revision history for this message
Chris Behrens (cbehrens) wrote :

Merged trunk and resolved conflict.

Revision history for this message
Josh Kearney (jk0) wrote :

These all look like reasonable changes to me.

review: Approve
Revision history for this message
Kevin L. Mitchell (klmitch) wrote :

LGTM...

review: Approve
Revision history for this message
Jason Kölker (jason-koelker) wrote :

Is mas Bueno!

review: Approve
Revision history for this message
Devin Carlen (devcamcar) wrote :

lgtm!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'nova/compute/api.py'
2--- nova/compute/api.py 2011-09-19 14:35:08 +0000
3+++ nova/compute/api.py 2011-09-19 22:43:17 +0000
4@@ -937,9 +937,10 @@
5 filters)
6 # NOTE(jkoelker) It is possible that we will get the same
7 # instance uuid twice (one for ipv4 and ipv6)
8- ids = set([r['instance_uuid'] for r in res])
9+ uuids = set([r['instance_uuid'] for r in res])
10+ filters['uuid'] = uuids
11
12- return self.db.instance_get_all_by_filters(context, filters, ids)
13+ return self.db.instance_get_all_by_filters(context, filters)
14
15 def _cast_compute_message(self, method, context, instance_id, host=None,
16 params=None):
17
18=== modified file 'nova/db/api.py'
19--- nova/db/api.py 2011-09-19 21:02:29 +0000
20+++ nova/db/api.py 2011-09-19 22:43:17 +0000
21@@ -468,7 +468,7 @@
22
23
24 def virtual_interface_get_all(context):
25- """Gets all virtual interfaces from the table,"""
26+ """Gets all virtual interfaces from the table"""
27 return IMPL.virtual_interface_get_all(context)
28
29
30@@ -510,10 +510,9 @@
31 return IMPL.instance_get_all(context)
32
33
34-def instance_get_all_by_filters(context, filters, instance_uuids=None):
35+def instance_get_all_by_filters(context, filters):
36 """Get all instances that match all filters."""
37- return IMPL.instance_get_all_by_filters(context, filters,
38- instance_uuids=instance_uuids)
39+ return IMPL.instance_get_all_by_filters(context, filters)
40
41
42 def instance_get_active_by_window(context, begin, end=None, project_id=None):
43@@ -617,9 +616,9 @@
44 return IMPL.instance_get_actions(context, instance_id)
45
46
47-def instance_get_uuids_by_ids(context, ids):
48- """Return the UUIDs of the instances given the ids"""
49- return IMPL.instance_get_uuids_by_ids(context, ids)
50+def instance_get_id_to_uuid_mapping(context, ids):
51+ """Return a dictionary containing 'ID: UUID' given the ids"""
52+ return IMPL.instance_get_id_to_uuid_mapping(context, ids)
53
54
55 ###################
56
57=== modified file 'nova/db/sqlalchemy/api.py'
58--- nova/db/sqlalchemy/api.py 2011-09-19 21:02:29 +0000
59+++ nova/db/sqlalchemy/api.py 2011-09-19 22:43:17 +0000
60@@ -1204,7 +1204,7 @@
61
62
63 @require_context
64-def instance_get_all_by_filters(context, filters, instance_uuids=None):
65+def instance_get_all_by_filters(context, filters):
66 """Return instances that match all filters. Deleted instances
67 will be returned by default, unless there's a filter that says
68 otherwise"""
69@@ -1235,7 +1235,7 @@
70 """Do exact match against a column. value to match can be a list
71 so you can match any value in the list.
72 """
73- if isinstance(value, list):
74+ if isinstance(value, list) or isinstance(value, set):
75 column_attr = getattr(models.Instance, column)
76 return query.filter(column_attr.in_(value))
77 else:
78@@ -1269,7 +1269,7 @@
79 # Filters for exact matches that we can do along with the SQL query...
80 # For other filters that don't match this, we will do regexp matching
81 exact_match_filter_names = ['project_id', 'user_id', 'image_ref',
82- 'vm_state', 'instance_type_id', 'deleted']
83+ 'vm_state', 'instance_type_id', 'deleted', 'uuid']
84
85 query_filters = [key for key in filters.iterkeys()
86 if key in exact_match_filter_names]
87@@ -1280,30 +1280,10 @@
88 query_prefix = _exact_match_filter(query_prefix, filter_name,
89 filters.pop(filter_name))
90
91- db_instances = query_prefix.all()
92- db_uuids = [instance['uuid'] for instance in db_instances \
93- if instance['uuid']]
94-
95- if instance_uuids is None:
96- instance_uuids = []
97-
98- uuids = []
99-
100- # NOTE(jkoelker): String UUIDs only!
101- if not instance_uuids:
102- uuids = db_uuids
103- elif not db_instances:
104- uuids = instance_uuids
105- else:
106- uuids = list(set(instance_uuids) & set(db_uuids))
107-
108- if not uuids:
109+ instances = query_prefix.all()
110+ if not instances:
111 return []
112
113- instances = session.query(models.Instance).\
114- filter(models.Instance.uuid.in_(uuids)).\
115- all()
116-
117 # Now filter on everything else for regexp matching..
118 # For filters not in the list, we'll attempt to use the filter_name
119 # as a column name in Instance..
120@@ -1321,6 +1301,8 @@
121 filter_l = lambda instance: _regexp_filter_by_column(instance,
122 filter_name, filter_re)
123 instances = filter(filter_l, instances)
124+ if not instances:
125+ break
126
127 return instances
128
129@@ -1565,13 +1547,15 @@
130
131
132 @require_context
133-def instance_get_uuids_by_ids(context, ids):
134+def instance_get_id_to_uuid_mapping(context, ids):
135 session = get_session()
136 instances = session.query(models.Instance).\
137 filter(models.Instance.id.in_(ids)).\
138 all()
139- return [{'uuid': instance['uuid'],
140- 'id': instance['id']} for instance in instances]
141+ mapping = {}
142+ for instance in instances:
143+ mapping[instance['id']] = instance['uuid']
144+ return mapping
145
146
147 ###################
148
149=== modified file 'nova/network/manager.py'
150--- nova/network/manager.py 2011-09-19 20:16:49 +0000
151+++ nova/network/manager.py 2011-09-19 22:43:17 +0000
152@@ -410,7 +410,7 @@
153 ip_filter = re.compile(str(filters.get('ip')))
154 ipv6_filter = re.compile(str(filters.get('ip6')))
155
156- # NOTE(jkoelker) Should probably figur out a better way to do
157+ # NOTE(jkoelker) Should probably figure out a better way to do
158 # this. But for now it "works", this could suck on
159 # large installs.
160
161@@ -448,12 +448,9 @@
162
163 # NOTE(jkoelker) Until we switch over to instance_uuid ;)
164 ids = [res['instance_id'] for res in results]
165- uuids = self.db.instance_get_uuids_by_ids(context, ids)
166+ uuid_map = self.db.instance_get_id_to_uuid_mapping(context, ids)
167 for res in results:
168- uuid = [u['uuid'] for u in uuids if u['id'] == res['instance_id']]
169- # NOTE(jkoelker) UUID must exist, so no test here
170- res['instance_uuid'] = uuid[0]
171-
172+ res['instance_uuid'] = uuid_map.get(res['instance_id'])
173 return results
174
175 def _get_networks_for_instance(self, context, instance_id, project_id,
176
177=== modified file 'nova/tests/fake_network.py'
178--- nova/tests/fake_network.py 2011-09-15 19:25:52 +0000
179+++ nova/tests/fake_network.py 2011-09-19 22:43:17 +0000
180@@ -105,10 +105,12 @@
181 'floating_ips': [floats[2]]}]}]
182 return vifs
183
184- def instance_get_uuids_by_ids(self, context, ids):
185+ def instance_get_id_to_uuid_mapping(self, context, ids):
186 # NOTE(jkoelker): This is just here until we can rely on UUIDs
187- return [{'uuid': str(utils.gen_uuid()),
188- 'id': id} for id in ids]
189+ mapping = {}
190+ for id in ids:
191+ mapping[id] = str(utils.gen_uuid())
192+ return mapping
193
194 def __init__(self):
195 self.db = self.FakeDB()
196
197=== modified file 'nova/tests/test_compute.py'
198--- nova/tests/test_compute.py 2011-09-19 14:35:08 +0000
199+++ nova/tests/test_compute.py 2011-09-19 22:43:17 +0000
200@@ -1033,8 +1033,8 @@
201 'get_instance_uuids_by_ip_filter',
202 network_manager.get_instance_uuids_by_ip_filter)
203 self.stubs.Set(network_manager.db,
204- 'instance_get_uuids_by_ids',
205- db.instance_get_uuids_by_ids)
206+ 'instance_get_id_to_uuid_mapping',
207+ db.instance_get_id_to_uuid_mapping)
208
209 instance_id1 = self._create_instance({'display_name': 'woot',
210 'id': 0})
211
212=== modified file 'nova/tests/test_db_api.py'
213--- nova/tests/test_db_api.py 2011-09-19 21:02:29 +0000
214+++ nova/tests/test_db_api.py 2011-09-19 22:43:17 +0000
215@@ -94,9 +94,12 @@
216 db.instance_destroy(self.context, inst1.id)
217 result = db.instance_get_all_by_filters(self.context.elevated(), {})
218 self.assertEqual(2, len(result))
219- self.assertEqual(result[0].id, inst1.id)
220- self.assertEqual(result[1].id, inst2.id)
221- self.assertTrue(result[0].deleted)
222+ self.assertIn(inst1.id, [result[0].id, result[1].id])
223+ self.assertIn(inst2.id, [result[0].id, result[1].id])
224+ if inst1.id == result[0].id:
225+ self.assertTrue(result[0].deleted)
226+ else:
227+ self.assertTrue(result[1].deleted)
228
229 def test_migration_get_all_unconfirmed(self):
230 ctxt = context.get_admin_context()