/
test_allocation_candidates.py
3141 lines (2782 loc) · 124 KB
/
test_allocation_candidates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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 collections
import os_resource_classes as orc
import os_traits
from oslo_utils.fixture import uuidsentinel as uuids
import sqlalchemy as sa
from placement import exception
from placement import lib as placement_lib
from placement.objects import allocation_candidate as ac_obj
from placement.objects import research_context as res_ctx
from placement.objects import resource_class as rc_obj
from placement.objects import resource_provider as rp_obj
from placement.objects import trait as trait_obj
from placement.tests.functional.db import test_base as tb
def _req_group_search_context(context, **kwargs):
resources = {
orc.VCPU: 2,
orc.MEMORY_MB: 256,
orc.SRIOV_NET_VF: 1,
}
request = placement_lib.RequestGroup(
use_same_provider=False,
resources=kwargs.get('resources', resources),
required_traits=kwargs.get('required_traits', {}),
forbidden_traits=kwargs.get('forbidden_traits', {}),
member_of=kwargs.get('member_of', []),
forbidden_aggs=kwargs.get('forbidden_aggs', []),
in_tree=kwargs.get('in_tree', None),
)
has_trees = res_ctx._has_provider_trees(context)
sharing = res_ctx.get_sharing_providers(context)
rg_ctx = res_ctx.RequestGroupSearchContext(
context, request, has_trees, sharing)
return rg_ctx
class ProviderDBHelperTestCase(tb.PlacementDbBaseTestCase):
def test_get_provider_ids_matching(self):
# These RPs are named based on whether we expect them to be 'incl'uded
# or 'excl'uded in the result.
# No inventory records. This one should never show up in a result.
self._create_provider('no_inventory')
# Inventory of adequate CPU and memory, no allocations against it.
excl_big_cm_noalloc = self._create_provider('big_cm_noalloc')
tb.add_inventory(excl_big_cm_noalloc, orc.VCPU, 15)
tb.add_inventory(excl_big_cm_noalloc, orc.MEMORY_MB,
4096, max_unit=2048)
# Inventory of adequate memory and disk, no allocations against it.
excl_big_md_noalloc = self._create_provider('big_md_noalloc')
tb.add_inventory(excl_big_md_noalloc, orc.MEMORY_MB,
4096, max_unit=2048)
tb.add_inventory(excl_big_md_noalloc, orc.DISK_GB, 2000)
# Adequate inventory, no allocations against it.
incl_biginv_noalloc = self._create_provider('biginv_noalloc')
tb.add_inventory(incl_biginv_noalloc, orc.VCPU, 15)
tb.add_inventory(incl_biginv_noalloc, orc.MEMORY_MB,
4096, max_unit=2048)
tb.add_inventory(incl_biginv_noalloc, orc.DISK_GB, 2000)
# No allocations, but inventory unusable. Try to hit all the possible
# reasons for exclusion.
# VCPU min_unit too high
excl_badinv_min_unit = self._create_provider('badinv_min_unit')
tb.add_inventory(excl_badinv_min_unit, orc.VCPU, 12, min_unit=6)
tb.add_inventory(excl_badinv_min_unit, orc.MEMORY_MB,
4096, max_unit=2048)
tb.add_inventory(excl_badinv_min_unit, orc.DISK_GB, 2000)
# MEMORY_MB max_unit too low
excl_badinv_max_unit = self._create_provider('badinv_max_unit')
tb.add_inventory(excl_badinv_max_unit, orc.VCPU, 15)
tb.add_inventory(excl_badinv_max_unit, orc.MEMORY_MB,
4096, max_unit=512)
tb.add_inventory(excl_badinv_max_unit, orc.DISK_GB, 2000)
# DISK_GB unsuitable step_size
excl_badinv_step_size = self._create_provider('badinv_step_size')
tb.add_inventory(excl_badinv_step_size, orc.VCPU, 15)
tb.add_inventory(excl_badinv_step_size, orc.MEMORY_MB,
4096, max_unit=2048)
tb.add_inventory(excl_badinv_step_size, orc.DISK_GB, 2000, step_size=7)
# Not enough total VCPU
excl_badinv_total = self._create_provider('badinv_total')
tb.add_inventory(excl_badinv_total, orc.VCPU, 4)
tb.add_inventory(excl_badinv_total, orc.MEMORY_MB,
4096, max_unit=2048)
tb.add_inventory(excl_badinv_total, orc.DISK_GB, 2000)
# Too much reserved MEMORY_MB
excl_badinv_reserved = self._create_provider('badinv_reserved')
tb.add_inventory(excl_badinv_reserved, orc.VCPU, 15)
tb.add_inventory(excl_badinv_reserved, orc.MEMORY_MB,
4096, max_unit=2048, reserved=3500)
tb.add_inventory(excl_badinv_reserved, orc.DISK_GB, 2000)
# DISK_GB allocation ratio blows it up
excl_badinv_alloc_ratio = self._create_provider('badinv_alloc_ratio')
tb.add_inventory(excl_badinv_alloc_ratio, orc.VCPU, 15)
tb.add_inventory(excl_badinv_alloc_ratio,
orc.MEMORY_MB, 4096, max_unit=2048)
tb.add_inventory(excl_badinv_alloc_ratio, orc.DISK_GB,
2000, allocation_ratio=0.5)
# Inventory consumed in one RC, but available in the others
excl_1invunavail = self._create_provider('1invunavail')
tb.add_inventory(excl_1invunavail, orc.VCPU, 10)
self.allocate_from_provider(excl_1invunavail, orc.VCPU, 7)
tb.add_inventory(excl_1invunavail, orc.MEMORY_MB, 4096)
self.allocate_from_provider(excl_1invunavail, orc.MEMORY_MB, 1024)
tb.add_inventory(excl_1invunavail, orc.DISK_GB, 2000)
self.allocate_from_provider(excl_1invunavail, orc.DISK_GB, 400)
# Inventory all consumed
excl_allused = self._create_provider('allused')
tb.add_inventory(excl_allused, orc.VCPU, 10)
self.allocate_from_provider(excl_allused, orc.VCPU, 7)
tb.add_inventory(excl_allused, orc.MEMORY_MB, 4000)
self.allocate_from_provider(excl_allused, orc.MEMORY_MB, 1500)
self.allocate_from_provider(excl_allused, orc.MEMORY_MB, 2000)
tb.add_inventory(excl_allused, orc.DISK_GB, 1500)
self.allocate_from_provider(excl_allused, orc.DISK_GB, 1)
# Inventory available in requested classes, but unavailable in others
incl_extra_full = self._create_provider('extra_full')
tb.add_inventory(incl_extra_full, orc.VCPU, 20)
self.allocate_from_provider(incl_extra_full, orc.VCPU, 15)
tb.add_inventory(incl_extra_full, orc.MEMORY_MB, 4096)
self.allocate_from_provider(incl_extra_full, orc.MEMORY_MB, 1024)
tb.add_inventory(incl_extra_full, orc.DISK_GB, 2000)
self.allocate_from_provider(incl_extra_full, orc.DISK_GB, 400)
tb.add_inventory(incl_extra_full, orc.PCI_DEVICE, 4)
self.allocate_from_provider(incl_extra_full, orc.PCI_DEVICE, 1)
self.allocate_from_provider(incl_extra_full, orc.PCI_DEVICE, 3)
# Inventory available in a unrequested classes, not in requested ones
excl_extra_avail = self._create_provider('extra_avail')
# Incompatible step size
tb.add_inventory(excl_extra_avail, orc.VCPU, 10, step_size=3)
# Not enough left after reserved + used
tb.add_inventory(excl_extra_avail, orc.MEMORY_MB, 4096,
max_unit=2048, reserved=2048)
self.allocate_from_provider(excl_extra_avail, orc.MEMORY_MB, 1040)
# Allocation ratio math
tb.add_inventory(excl_extra_avail, orc.DISK_GB, 2000,
allocation_ratio=0.5)
tb.add_inventory(excl_extra_avail, orc.IPV4_ADDRESS, 48)
custom_special = rc_obj.ResourceClass(self.ctx, name='CUSTOM_SPECIAL')
custom_special.create()
tb.add_inventory(excl_extra_avail, 'CUSTOM_SPECIAL', 100)
self.allocate_from_provider(excl_extra_avail, 'CUSTOM_SPECIAL', 99)
resources = {
orc.VCPU: 5,
orc.MEMORY_MB: 1024,
orc.DISK_GB: 1500
}
# Run it!
rg_ctx = _req_group_search_context(self.ctx, resources=resources)
res = res_ctx.get_provider_ids_matching(rg_ctx)
# We should get all the incl_* RPs
expected = [incl_biginv_noalloc, incl_extra_full]
self.assertEqual(set((rp.id, rp.id) for rp in expected), set(res))
# Now request that the providers must have a set of required traits and
# that this results in no results returned, since we haven't yet
# associated any traits with the providers
avx2_t = trait_obj.Trait.get_by_name(
self.ctx, os_traits.HW_CPU_X86_AVX2)
# get_provider_ids_matching()'s required_traits and forbidden_traits
# arguments maps, keyed by trait name, of the trait internal ID
req_traits = {os_traits.HW_CPU_X86_AVX2: avx2_t.id}
rg_ctx = _req_group_search_context(
self.ctx,
resources=resources,
required_traits=req_traits,
)
res = res_ctx.get_provider_ids_matching(rg_ctx)
self.assertEqual([], res)
# Next let's set the required trait to an excl_* RPs.
# This should result in no results returned as well.
excl_big_md_noalloc.set_traits([avx2_t])
res = res_ctx.get_provider_ids_matching(rg_ctx)
self.assertEqual([], res)
# OK, now add the trait to one of the incl_* providers and verify that
# provider now shows up in our results
incl_biginv_noalloc.set_traits([avx2_t])
res = res_ctx.get_provider_ids_matching(rg_ctx)
rp_ids = [r[0] for r in res]
self.assertEqual([incl_biginv_noalloc.id], rp_ids)
# Let's see if the in_tree filter works
rg_ctx = _req_group_search_context(
self.ctx,
resources=resources,
in_tree=uuids.biginv_noalloc,
)
res = res_ctx.get_provider_ids_matching(rg_ctx)
rp_ids = [r[0] for r in res]
self.assertEqual([incl_biginv_noalloc.id], rp_ids)
# We don't get anything if the specified tree doesn't satisfy the
# requirements in the first place
self.assertRaises(exception.ResourceProviderNotFound,
_req_group_search_context,
self.ctx, resources=resources,
in_tree=uuids.allused)
def test_get_provider_ids_matching_with_multiple_forbidden(self):
rp1 = self._create_provider('rp1', uuids.agg1)
tb.add_inventory(rp1, orc.VCPU, 64)
rp2 = self._create_provider('rp2', uuids.agg1)
trait_two, = tb.set_traits(rp2, 'CUSTOM_TWO')
tb.add_inventory(rp2, orc.VCPU, 64)
rp3 = self._create_provider('rp3')
trait_three, = tb.set_traits(rp3, 'CUSTOM_THREE')
tb.add_inventory(rp3, orc.VCPU, 64)
resources = {orc.VCPU: 4}
forbidden_traits = {trait_two.name: trait_two.id,
trait_three.name: trait_three.id}
member_of = [[uuids.agg1]]
rg_ctx = _req_group_search_context(
self.ctx,
resources=resources,
forbidden_traits=forbidden_traits,
member_of=member_of)
res = res_ctx.get_provider_ids_matching(rg_ctx)
self.assertEqual({(rp1.id, rp1.id)}, set(res))
def test_get_provider_ids_matching_with_aggregates(self):
rp1 = self._create_provider('rp1', uuids.agg1, uuids.agg2)
rp2 = self._create_provider('rp2', uuids.agg2, uuids.agg3)
rp3 = self._create_provider('rp3', uuids.agg3, uuids.agg4)
rp4 = self._create_provider('rp4', uuids.agg4, uuids.agg1)
rp5 = self._create_provider('rp5')
tb.add_inventory(rp1, orc.VCPU, 64)
tb.add_inventory(rp2, orc.VCPU, 64)
tb.add_inventory(rp3, orc.VCPU, 64)
tb.add_inventory(rp4, orc.VCPU, 64)
tb.add_inventory(rp5, orc.VCPU, 64)
resources = {orc.VCPU: 4}
rg_ctx = _req_group_search_context(
self.ctx,
resources=resources,
member_of=[[uuids.agg1]],
)
expected_rp = [rp1, rp4]
res = res_ctx.get_provider_ids_matching(rg_ctx)
self.assertEqual(set((rp.id, rp.id) for rp in expected_rp), set(res))
rg_ctx = _req_group_search_context(
self.ctx,
resources=resources,
member_of=[[uuids.agg1, uuids.agg2]],
)
expected_rp = [rp1, rp2, rp4]
res = res_ctx.get_provider_ids_matching(rg_ctx)
self.assertEqual(set((rp.id, rp.id) for rp in expected_rp), set(res))
rg_ctx = _req_group_search_context(
self.ctx,
resources=resources,
member_of=[[uuids.agg1, uuids.agg2], [uuids.agg4]],
)
expected_rp = [rp4]
res = res_ctx.get_provider_ids_matching(rg_ctx)
self.assertEqual(set((rp.id, rp.id) for rp in expected_rp), set(res))
rg_ctx = _req_group_search_context(
self.ctx,
resources=resources,
forbidden_aggs=[uuids.agg1],
)
expected_rp = [rp2, rp3, rp5]
res = res_ctx.get_provider_ids_matching(rg_ctx)
self.assertEqual(set((rp.id, rp.id) for rp in expected_rp), set(res))
rg_ctx = _req_group_search_context(
self.ctx,
resources=resources,
forbidden_aggs=[uuids.agg1, uuids.agg2],
)
expected_rp = [rp3, rp5]
res = res_ctx.get_provider_ids_matching(rg_ctx)
self.assertEqual(set((rp.id, rp.id) for rp in expected_rp), set(res))
rg_ctx = _req_group_search_context(
self.ctx,
resources=resources,
member_of=[[uuids.agg1, uuids.agg2]],
forbidden_aggs=[uuids.agg3, uuids.agg4],
)
expected_rp = [rp1]
res = res_ctx.get_provider_ids_matching(rg_ctx)
self.assertEqual(set((rp.id, rp.id) for rp in expected_rp), set(res))
rg_ctx = _req_group_search_context(
self.ctx,
resources=resources,
member_of=[[uuids.agg1]],
forbidden_aggs=[uuids.agg1],
)
expected_rp = []
res = res_ctx.get_provider_ids_matching(rg_ctx)
self.assertEqual(set((rp.id, rp.id) for rp in expected_rp), set(res))
def test_get_provider_ids_having_all_traits(self):
def run(traitnames, expected_ids):
tmap = {}
if traitnames:
tmap = trait_obj.ids_from_names(self.ctx, traitnames)
obs = res_ctx._get_provider_ids_having_all_traits(self.ctx, tmap)
self.assertEqual(sorted(expected_ids), sorted(obs))
# No traits. This will never be returned, because it's illegal to
# invoke the method with no traits.
self._create_provider('cn1')
# One trait
cn2 = self._create_provider('cn2')
tb.set_traits(cn2, 'HW_CPU_X86_TBM')
# One the same as cn2
cn3 = self._create_provider('cn3')
tb.set_traits(cn3, 'HW_CPU_X86_TBM', 'HW_CPU_X86_TSX',
'HW_CPU_X86_SGX')
# Disjoint
cn4 = self._create_provider('cn4')
tb.set_traits(cn4, 'HW_CPU_X86_SSE2', 'HW_CPU_X86_SSE3', 'CUSTOM_FOO')
# Request with no traits not allowed
self.assertRaises(
ValueError,
res_ctx._get_provider_ids_having_all_traits, self.ctx, None)
self.assertRaises(
ValueError,
res_ctx._get_provider_ids_having_all_traits, self.ctx, {})
# Common trait returns both RPs having it
run(['HW_CPU_X86_TBM'], [cn2.id, cn3.id])
# Just the one
run(['HW_CPU_X86_TSX'], [cn3.id])
run(['HW_CPU_X86_TSX', 'HW_CPU_X86_SGX'], [cn3.id])
run(['CUSTOM_FOO'], [cn4.id])
# Including the common one still just gets me cn3
run(['HW_CPU_X86_TBM', 'HW_CPU_X86_SGX'], [cn3.id])
run(['HW_CPU_X86_TBM', 'HW_CPU_X86_TSX', 'HW_CPU_X86_SGX'], [cn3.id])
# Can't be satisfied
run(['HW_CPU_X86_TBM', 'HW_CPU_X86_TSX', 'CUSTOM_FOO'], [])
run(['HW_CPU_X86_TBM', 'HW_CPU_X86_TSX', 'HW_CPU_X86_SGX',
'CUSTOM_FOO'], [])
run(['HW_CPU_X86_SGX', 'HW_CPU_X86_SSE3'], [])
run(['HW_CPU_X86_TBM', 'CUSTOM_FOO'], [])
run(['HW_CPU_X86_BMI'], [])
trait_obj.Trait(self.ctx, name='CUSTOM_BAR').create()
run(['CUSTOM_BAR'], [])
class ProviderTreeDBHelperTestCase(tb.PlacementDbBaseTestCase):
def _get_rp_ids_matching_names(self, names):
"""Utility function to look up resource provider IDs from a set of
supplied provider names directly from the API DB.
"""
names = map(str, names)
sel = sa.select([rp_obj._RP_TBL.c.id])
sel = sel.where(rp_obj._RP_TBL.c.name.in_(names))
with self.placement_db.get_engine().connect() as conn:
rp_ids = set([r[0] for r in conn.execute(sel)])
return rp_ids
def test_get_trees_matching_all(self):
"""Creates a few provider trees having different inventories and
allocations and tests the get_trees_matching_all_resources() utility
function to ensure that matching trees and resource providers are
returned.
"""
def _run_test(expected_trees, expected_rps, **kwargs):
"""Helper function to validate the test result"""
# NOTE(jaypipes): get_trees_matching_all() expects a dict of
# resource class internal identifiers, not string names
if not expected_trees:
try:
self.assertRaises(exception.ResourceProviderNotFound,
_req_group_search_context,
self.ctx, **kwargs)
return
except Exception:
pass
rg_ctx = _req_group_search_context(self.ctx, **kwargs)
rw_ctx = res_ctx.RequestWideSearchContext(
self.ctx, placement_lib.RequestWideParams(), True)
results = res_ctx.get_trees_matching_all(rg_ctx, rw_ctx)
tree_ids = self._get_rp_ids_matching_names(expected_trees)
rp_ids = self._get_rp_ids_matching_names(expected_rps)
self.assertEqual(tree_ids, results.trees)
self.assertEqual(rp_ids, results.rps)
# Before we even set up any providers, verify that the short-circuits
# work to return empty lists
_run_test([], [])
# We are setting up 3 trees of providers that look like this:
#
# compute node (cn)
# / \
# / \
# numa cell 0 numa cell 1
# | |
# | |
# pf 0 pf 1
#
for x in ('1', '2', '3'):
name = 'cn' + x
cn = self._create_provider(name)
tb.add_inventory(cn, orc.VCPU, 16)
tb.add_inventory(cn, orc.MEMORY_MB, 32768)
name = 'cn' + x + '_numa0'
numa_cell0 = self._create_provider(name, parent=cn.uuid)
name = 'cn' + x + '_numa1'
numa_cell1 = self._create_provider(name, parent=cn.uuid)
name = 'cn' + x + '_numa0_pf0'
pf0 = self._create_provider(name, parent=numa_cell0.uuid)
tb.add_inventory(pf0, orc.SRIOV_NET_VF, 8)
name = 'cn' + x + '_numa1_pf1'
pf1 = self._create_provider(name, parent=numa_cell1.uuid)
tb.add_inventory(pf1, orc.SRIOV_NET_VF, 8)
if x == '1':
# Associate the first compute node with agg1 and agg2
cn.set_aggregates([uuids.agg1, uuids.agg2])
if x == '2':
# Associate the second PF on the second compute node with agg2
pf1.set_aggregates([uuids.agg2])
if x == '3':
# Associate the first compute node with agg2 and agg3
cn.set_aggregates([uuids.agg2, uuids.agg3])
# Associate the second PF on the second compute node with agg4
pf1.set_aggregates([uuids.agg4])
# Mark the second PF on the third compute node as having
# GENEVE offload enabled
tb.set_traits(pf1, os_traits.HW_NIC_OFFLOAD_GENEVE)
# Doesn't really make a whole lot of logical sense, but allows
# us to test situations where the same trait is associated with
# multiple providers in the same tree and one of the providers
# has inventory we will use...
tb.set_traits(cn, os_traits.HW_NIC_OFFLOAD_GENEVE)
# First, we test that all the candidates are returned
expected_trees = ['cn1', 'cn2', 'cn3']
expected_rps = ['cn1', 'cn1_numa0_pf0', 'cn1_numa1_pf1',
'cn2', 'cn2_numa0_pf0', 'cn2_numa1_pf1',
'cn3', 'cn3_numa0_pf0', 'cn3_numa1_pf1']
_run_test(expected_trees, expected_rps)
# Let's see if the tree_root_id filter works
expected_trees = ['cn1']
expected_rps = ['cn1', 'cn1_numa0_pf0', 'cn1_numa1_pf1']
_run_test(expected_trees, expected_rps, in_tree=uuids.cn1)
# Let's see if the aggregate filter works
# 1. rps in agg1
# All rps under cn1 should be included because aggregate on a root
# spans the whole tree
member_of = [[uuids.agg1]]
expected_trees = ['cn1']
expected_rps = ['cn1', 'cn1_numa0_pf0', 'cn1_numa1_pf1']
_run_test(expected_trees, expected_rps, member_of=member_of)
# 2. rps in agg2
# cn2 doesn't come up because while cn2_numa1_pf1 is in agg2, aggs on
# non-root does NOT span the whole tree. Thus cn2 can't provide VCPU
# or MEMORY_MB resource
member_of = [[uuids.agg2]]
expected_trees = ['cn1', 'cn3']
expected_rps = ['cn1', 'cn1_numa0_pf0', 'cn1_numa1_pf1',
'cn3', 'cn3_numa0_pf0', 'cn3_numa1_pf1']
_run_test(expected_trees, expected_rps, member_of=member_of)
# 3. rps in agg1 or agg3
# cn1 in agg1 and cn3 in agg3 comes up
member_of = [[uuids.agg1, uuids.agg3]]
expected_trees = ['cn1', 'cn3']
expected_rps = ['cn1', 'cn1_numa0_pf0', 'cn1_numa1_pf1',
'cn3', 'cn3_numa0_pf0', 'cn3_numa1_pf1']
_run_test(expected_trees, expected_rps, member_of=member_of)
# 4. rps in (agg1 or agg2) and (agg3)
# cn1 is not in agg3
member_of = [[uuids.agg1, uuids.agg2], [uuids.agg3]]
expected_trees = ['cn3']
expected_rps = ['cn3', 'cn3_numa0_pf0', 'cn3_numa1_pf1']
_run_test(expected_trees, expected_rps, member_of=member_of)
# 5. rps not in agg1
# All rps under cn1 are excluded
forbidden_aggs = [uuids.agg1]
expected_trees = ['cn2', 'cn3']
expected_rps = ['cn2', 'cn2_numa0_pf0', 'cn2_numa1_pf1',
'cn3', 'cn3_numa0_pf0', 'cn3_numa1_pf1']
_run_test(expected_trees, expected_rps, forbidden_aggs=forbidden_aggs)
# 6. rps not in agg2
# All rps under cn1, under cn3 and pf1 on cn2 are excluded
forbidden_aggs = [uuids.agg2]
expected_trees = ['cn2']
expected_rps = ['cn2', 'cn2_numa0_pf0']
_run_test(expected_trees, expected_rps, forbidden_aggs=forbidden_aggs)
# 7. rps neither in agg1 nor in agg4
# All rps under cn1 and pf1 on cn3 are excluded
forbidden_aggs = [uuids.agg1, uuids.agg4]
expected_trees = ['cn2', 'cn3']
expected_rps = ['cn2', 'cn2_numa0_pf0', 'cn2_numa1_pf1',
'cn3', 'cn3_numa0_pf0']
_run_test(expected_trees, expected_rps, forbidden_aggs=forbidden_aggs)
# 8. rps in agg3 and neither in agg1 nor in agg4
# cn2 is not in agg3 so excluded
member_of = [[uuids.agg3]]
forbidden_aggs = [uuids.agg1, uuids.agg4]
expected_trees = ['cn3']
expected_rps = ['cn3', 'cn3_numa0_pf0']
_run_test(expected_trees, expected_rps, member_of=member_of,
forbidden_aggs=forbidden_aggs)
# 9. rps in agg1 or agg3 and not in agg3
# ...which means rps in agg1 but not in agg3
member_of = [[uuids.agg1, uuids.agg3]]
forbidden_aggs = [uuids.agg3]
expected_trees = ['cn1']
expected_rps = ['cn1', 'cn1_numa0_pf0', 'cn1_numa1_pf1']
_run_test(expected_trees, expected_rps, member_of=member_of,
forbidden_aggs=forbidden_aggs)
# 10. rps in agg1 and not in agg1
# ...which results in no rp
member_of = [[uuids.agg1]]
forbidden_aggs = [uuids.agg1]
expected_trees = []
expected_rps = []
_run_test(expected_trees, expected_rps, member_of=member_of,
forbidden_aggs=forbidden_aggs)
# OK, now consume all the VFs in the second compute node and verify
# only the first and third computes are returned as root providers from
# get_trees_matching_all()
cn2_pf0 = rp_obj.ResourceProvider.get_by_uuid(self.ctx,
uuids.cn2_numa0_pf0)
self.allocate_from_provider(cn2_pf0, orc.SRIOV_NET_VF, 8)
cn2_pf1 = rp_obj.ResourceProvider.get_by_uuid(self.ctx,
uuids.cn2_numa1_pf1)
self.allocate_from_provider(cn2_pf1, orc.SRIOV_NET_VF, 8)
# cn2 had all its VFs consumed, so we should only get cn1 and cn3's IDs
# as the root provider IDs.
expected_trees = ['cn1', 'cn3']
expected_rps = ['cn1', 'cn1_numa0_pf0', 'cn1_numa1_pf1',
'cn3', 'cn3_numa0_pf0', 'cn3_numa1_pf1']
_run_test(expected_trees, expected_rps)
# OK, now we're going to add a required trait to the mix. The only
# provider that is decorated with the HW_NIC_OFFLOAD_GENEVE trait is
# the second physical function on the third compute host. So we should
# only get the third compute node back if we require that trait
geneve_t = trait_obj.Trait.get_by_name(
self.ctx, os_traits.HW_NIC_OFFLOAD_GENEVE)
# required_traits parameter is a dict of trait name to internal ID
req_traits = {
geneve_t.name: geneve_t.id,
}
expected_trees = ['cn3']
# NOTE(tetsuro): Actually we also get providers without traits here.
# This is reported as bug#1771707 and from users' view the bug is now
# fixed out of this get_trees_matching_all() function by checking
# traits later again in _check_traits_for_alloc_request().
# But ideally, we'd like to have only pf1 from cn3 here using SQL
# query in get_trees_matching_all() function for optimization.
# provider_names = ['cn3', 'cn3_numa1_pf1']
expected_rps = ['cn3', 'cn3_numa0_pf0', 'cn3_numa1_pf1']
_run_test(expected_trees, expected_rps, required_traits=req_traits)
# Add in a required trait that no provider has associated with it and
# verify that there are no returned allocation candidates
avx2_t = trait_obj.Trait.get_by_name(
self.ctx, os_traits.HW_CPU_X86_AVX2)
# required_traits parameter is a dict of trait name to internal ID
req_traits = {
geneve_t.name: geneve_t.id,
avx2_t.name: avx2_t.id,
}
_run_test([], [], required_traits=req_traits)
# If we add the AVX2 trait as forbidden, not required, then we
# should get back the original cn3
req_traits = {
geneve_t.name: geneve_t.id,
}
forbidden_traits = {
avx2_t.name: avx2_t.id,
}
expected_trees = ['cn3']
# NOTE(tetsuro): Actually we also get providers without traits here.
# This is reported as bug#1771707 and from users' view the bug is now
# fixed out of this get_trees_matching_all() function by checking
# traits later again in _check_traits_for_alloc_request().
# But ideally, we'd like to have only pf1 from cn3 here using SQL
# query in get_trees_matching_all() function for optimization.
# provider_names = ['cn3', 'cn3_numa1_pf1']
expected_rps = ['cn3', 'cn3_numa0_pf0', 'cn3_numa1_pf1']
_run_test(expected_trees, expected_rps,
required_traits=req_traits,
forbidden_traits=forbidden_traits)
# Consume all the VFs in first and third compute nodes and verify
# no more providers are returned
cn1_pf0 = rp_obj.ResourceProvider.get_by_uuid(self.ctx,
uuids.cn1_numa0_pf0)
self.allocate_from_provider(cn1_pf0, orc.SRIOV_NET_VF, 8)
cn1_pf1 = rp_obj.ResourceProvider.get_by_uuid(self.ctx,
uuids.cn1_numa1_pf1)
self.allocate_from_provider(cn1_pf1, orc.SRIOV_NET_VF, 8)
cn3_pf0 = rp_obj.ResourceProvider.get_by_uuid(self.ctx,
uuids.cn3_numa0_pf0)
self.allocate_from_provider(cn3_pf0, orc.SRIOV_NET_VF, 8)
cn3_pf1 = rp_obj.ResourceProvider.get_by_uuid(self.ctx,
uuids.cn3_numa1_pf1)
self.allocate_from_provider(cn3_pf1, orc.SRIOV_NET_VF, 8)
_run_test([], [], required_traits=req_traits,
forbidden_traits=forbidden_traits)
def _make_trees_with_traits(self):
# We are setting up 6 trees of providers with following traits:
#
# compute node (cn)
# / \
# pf 0 pf 1
#
# +-----+----------------+---------------------+---------------------+
# | | cn | pf0 | pf1 |
# +-----+----------------+---------------------+---------------------+
# |tree1|HW_CPU_X86_AVX2 | |HW_NIC_OFFLOAD_GENEVE|
# +-----+----------------+---------------------+---------------------+
# |tree2|STORAGE_DISK_SSD| | |
# +-----+----------------+---------------------+---------------------+
# |tree3|HW_CPU_X86_AVX2 | | |
# | |STORAGE_DISK_SSD| | |
# +-----+----------------+---------------------+---------------------+
# |tree4| |HW_NIC_ACCEL_SSL | |
# | | |HW_NIC_OFFLOAD_GENEVE| |
# +-----+----------------+---------------------+---------------------+
# |tree5| |HW_NIC_ACCEL_SSL |HW_NIC_OFFLOAD_GENEVE|
# +-----+----------------+---------------------+---------------------+
# |tree6| |HW_NIC_ACCEL_SSL |HW_NIC_ACCEL_SSL |
# +-----+----------------+---------------------+---------------------+
# |tree7| | | |
# +-----+----------------+---------------------+---------------------+
#
rp_ids = set()
for x in ('1', '2', '3', '4', '5', '6', '7'):
name = 'cn' + x
cn = self._create_provider(name)
name = 'cn' + x + '_pf0'
pf0 = self._create_provider(name, parent=cn.uuid)
name = 'cn' + x + '_pf1'
pf1 = self._create_provider(name, parent=cn.uuid)
rp_ids |= set([cn.id, pf0.id, pf1.id])
if x == '1':
tb.set_traits(cn, os_traits.HW_CPU_X86_AVX2)
tb.set_traits(pf1, os_traits.HW_NIC_OFFLOAD_GENEVE)
if x == '2':
tb.set_traits(cn, os_traits.STORAGE_DISK_SSD)
if x == '3':
tb.set_traits(cn, os_traits.HW_CPU_X86_AVX2,
os_traits.STORAGE_DISK_SSD)
if x == '4':
tb.set_traits(pf0, os_traits.HW_NIC_ACCEL_SSL,
os_traits.HW_NIC_OFFLOAD_GENEVE)
if x == '5':
tb.set_traits(pf0, os_traits.HW_NIC_ACCEL_SSL)
tb.set_traits(pf1, os_traits.HW_NIC_OFFLOAD_GENEVE)
if x == '6':
tb.set_traits(pf0, os_traits.HW_NIC_ACCEL_SSL)
tb.set_traits(pf1, os_traits.HW_NIC_ACCEL_SSL)
avx2_t = trait_obj.Trait.get_by_name(
self.ctx, os_traits.HW_CPU_X86_AVX2)
ssd_t = trait_obj.Trait.get_by_name(
self.ctx, os_traits.STORAGE_DISK_SSD)
geneve_t = trait_obj.Trait.get_by_name(
self.ctx, os_traits.HW_NIC_OFFLOAD_GENEVE)
ssl_t = trait_obj.Trait.get_by_name(
self.ctx, os_traits.HW_NIC_ACCEL_SSL)
return rp_ids, avx2_t, ssd_t, geneve_t, ssl_t
def test_get_trees_with_traits(self):
"""Creates a few provider trees having different traits and tests the
_get_trees_with_traits() utility function to ensure that only the
root provider IDs of matching traits are returned.
"""
rp_ids, avx2_t, ssd_t, geneve_t, ssl_t = self._make_trees_with_traits()
# Case1: required on root
required_traits = {
avx2_t.name: avx2_t.id,
}
forbidden_traits = {}
rp_tuples_with_trait = res_ctx._get_trees_with_traits(
self.ctx, rp_ids, required_traits, forbidden_traits)
tree_root_ids = set([p[1] for p in rp_tuples_with_trait])
provider_names = ['cn1', 'cn3']
expect_root_ids = self._get_rp_ids_matching_names(provider_names)
self.assertEqual(expect_root_ids, tree_root_ids)
# Case1': required on root with forbidden traits
# Let's validate that cn3 dissapears
required_traits = {
avx2_t.name: avx2_t.id,
}
forbidden_traits = {
ssd_t.name: ssd_t.id,
}
rp_tuples_with_trait = res_ctx._get_trees_with_traits(
self.ctx, rp_ids, required_traits, forbidden_traits)
tree_root_ids = set([p[1] for p in rp_tuples_with_trait])
provider_names = ['cn1']
expect_root_ids = self._get_rp_ids_matching_names(provider_names)
self.assertEqual(expect_root_ids, tree_root_ids)
# Case2: multiple required on root
required_traits = {
avx2_t.name: avx2_t.id,
ssd_t.name: ssd_t.id
}
forbidden_traits = {}
rp_tuples_with_trait = res_ctx._get_trees_with_traits(
self.ctx, rp_ids, required_traits, forbidden_traits)
tree_root_ids = set([p[1] for p in rp_tuples_with_trait])
provider_names = ['cn3']
expect_root_ids = self._get_rp_ids_matching_names(provider_names)
self.assertEqual(expect_root_ids, tree_root_ids)
# Case3: required on child
required_traits = {
geneve_t.name: geneve_t.id
}
forbidden_traits = {}
rp_tuples_with_trait = res_ctx._get_trees_with_traits(
self.ctx, rp_ids, required_traits, forbidden_traits)
tree_root_ids = set([p[1] for p in rp_tuples_with_trait])
provider_names = ['cn1', 'cn4', 'cn5']
expect_root_ids = self._get_rp_ids_matching_names(provider_names)
self.assertEqual(expect_root_ids, tree_root_ids)
# Case3': required on child with forbidden traits
# Let's validate that cn4 dissapears
required_traits = {
geneve_t.name: geneve_t.id
}
forbidden_traits = {
ssl_t.name: ssl_t.id
}
rp_tuples_with_trait = res_ctx._get_trees_with_traits(
self.ctx, rp_ids, required_traits, forbidden_traits)
tree_root_ids = set([p[1] for p in rp_tuples_with_trait])
provider_names = ['cn1', 'cn5']
expect_root_ids = self._get_rp_ids_matching_names(provider_names)
self.assertEqual(expect_root_ids, tree_root_ids)
# Case4: multiple required on child
required_traits = {
geneve_t.name: geneve_t.id,
ssl_t.name: ssl_t.id
}
forbidden_traits = {}
rp_tuples_with_trait = res_ctx._get_trees_with_traits(
self.ctx, rp_ids, required_traits, forbidden_traits)
tree_root_ids = set([p[1] for p in rp_tuples_with_trait])
provider_names = ['cn4', 'cn5']
expect_root_ids = self._get_rp_ids_matching_names(provider_names)
self.assertEqual(expect_root_ids, tree_root_ids)
# Case5: required on root and child
required_traits = {
avx2_t.name: avx2_t.id,
geneve_t.name: geneve_t.id
}
forbidden_traits = {}
rp_tuples_with_trait = res_ctx._get_trees_with_traits(
self.ctx, rp_ids, required_traits, forbidden_traits)
tree_root_ids = set([p[1] for p in rp_tuples_with_trait])
provider_names = ['cn1']
expect_root_ids = self._get_rp_ids_matching_names(provider_names)
self.assertEqual(expect_root_ids, tree_root_ids)
def test_get_roots_with_traits(self):
_, avx2_t, ssd_t, geneve_t, ssl_t = self._make_trees_with_traits()
def do_test(required=None, forbidden=None, expected=None):
actual = res_ctx._get_roots_with_traits(
self.ctx,
set(trait.id for trait in required or []),
set(trait.id for trait in forbidden or []))
if expected:
expected = self._get_rp_ids_matching_names(
'cn%d' % d for d in expected)
self.assertEqual(expected or set(), actual)
# One of required/forbidden must be specified
self.assertRaises(ValueError, do_test)
# AVX2 is on cn1 and cn3
do_test(required=[avx2_t], expected=(1, 3))
# Multiple required
do_test(required=[avx2_t, ssd_t], expected=(3,))
# No match on roots for a trait on children
do_test(required=[geneve_t])
# ...even if including a trait also on roots
do_test(required=[geneve_t, ssd_t])
# Forbid traits not on any roots. These are on non-root providers...
do_test(forbidden=[geneve_t, ssl_t], expected=(1, 2, 3, 4, 5, 6, 7))
# ...and this one is nowhere in the environment.
hdd_t = trait_obj.Trait.get_by_name(
self.ctx, os_traits.STORAGE_DISK_HDD)
do_test(forbidden=[hdd_t], expected=(1, 2, 3, 4, 5, 6, 7))
# Forbid traits just on roots
do_test(forbidden=[avx2_t, ssd_t], expected=(4, 5, 6, 7))
# Forbid traits on roots and children
do_test(forbidden=[ssd_t, ssl_t, geneve_t], expected=(1, 4, 5, 6, 7))
# Required & forbidden both on roots
do_test(required=[avx2_t], forbidden=[ssd_t], expected=(1,))
# Same, but adding forbidden not on roots has no effect
do_test(required=[avx2_t], forbidden=[ssd_t, ssl_t], expected=(1,))
# Required on roots, forbidden only on children
do_test(
required=[avx2_t, ssd_t], forbidden=[ssl_t, geneve_t],
expected=(3,))
# Required & forbidden overlap. No results because it is impossible for
# one provider to both have and not have a trait. (Unreachable in real
# life due to conflict check in the handler.)
do_test(required=[avx2_t, ssd_t], forbidden=[ssd_t, geneve_t])
class AllocationCandidatesTestCase(tb.PlacementDbBaseTestCase):
"""Tests a variety of scenarios with both shared and non-shared resource
providers that the AllocationCandidates.get_by_requests() method returns a
set of alternative allocation requests and provider summaries that may be
used by the scheduler to sort/weigh the options it has for claiming
resources against providers.
"""
def setUp(self):
super(AllocationCandidatesTestCase, self).setUp()
self.requested_resources = {
orc.VCPU: 1,
orc.MEMORY_MB: 64,
orc.DISK_GB: 1500,
}
# For debugging purposes, populated by _create_provider and used by
# _validate_allocation_requests to make failure results more readable.
self.rp_uuid_to_name = {}
def _get_allocation_candidates(self, groups=None, rqparams=None):
if groups is None:
groups = {'': placement_lib.RequestGroup(
use_same_provider=False,
resources=self.requested_resources)}
if rqparams is None:
rqparams = placement_lib.RequestWideParams()
return ac_obj.AllocationCandidates.get_by_requests(
self.ctx, groups, rqparams)
def _mappings_to_suffix(self, mappings):
"""Turn a dict of AllocationRequest mappings keyed on suffix to
a dict, keyed by uuid, of lists of suffixes.
"""
suffixes_by_uuid = collections.defaultdict(set)
for suffix, rps in mappings.items():
for rp_uuid in rps:
suffixes_by_uuid[rp_uuid].add(suffix)
listed_sorted_suffixes = {}
for rp_uuid, suffixes in suffixes_by_uuid.items():
listed_sorted_suffixes[rp_uuid] = sorted(list(suffixes))
return listed_sorted_suffixes
def _validate_allocation_requests(self, expected, candidates,
expect_suffixes=False):
"""Assert correctness of allocation requests in allocation candidates.
This is set up to make it easy for the caller to specify the expected
result, to make that expected structure readable for someone looking at
the test case, and to make test failures readable for debugging.
:param expected: A list of lists of tuples representing the expected
allocation requests, of the form:
[
[(resource_provider_name, resource_class_name, resource_count),
...,
],
...
]
:param candidates: The result from AllocationCandidates.get_by_requests
:param expect_suffixes: If True, validate the AllocationRequest
mappings in the results, found as a list of
suffixes in 4th member of the tuple described
above.
"""
# Extract/convert allocation requests from candidates
observed = []
for ar in candidates.allocation_requests:
suffixes_by_uuid = self._mappings_to_suffix(ar.mappings)
rrs = []
for rr in ar.resource_requests:
req_tuple = (self.rp_uuid_to_name[rr.resource_provider.uuid],
rr.resource_class, rr.amount)
if expect_suffixes:
req_tuple = (
req_tuple +
(suffixes_by_uuid[rr.resource_provider.uuid], ))
rrs.append(req_tuple)
rrs.sort()
observed.append(rrs)
observed.sort()
# Sort the guts of the expected structure
for rr in expected: