Browse Source

Improve performance on get and create/update/delete requests

This change follows up on the previous performance improvements
and does the same for crud requests.
I9d67f0966561baaefb50ae97b943ff6593e194eb
I32328c5206b9cd6fb8d8764c079f22b6ea8bfa9e

We remove from_sqlalchemy_model and the related object intermediary
representation from the get_loabalancer, get_pool, get_listener,
get_healthmonitor, get_l7policy, get_l7policy_rule,
get_pool_member.

Instead it more directly transforms from the SQLAlchemy model to the
required api dictionary format. Thus this entirely skips loading a few
relations that walking the intermediary object model triggered without
the target output needing them.

This is in particular important on calls like _get_driver_for_loadbalancer
as it previously would load the loadbalancer and all related objects
just to get the provider name for the loadbalancer.

Story: 2004949
Task: 29365

Change-Id: Ic268ea3cdcf809f62ba1ab3ebf8ab6d22f871404
Erik Olof Gunnar Andersson 2 months ago
parent
commit
0295eccea6

+ 31
- 0
neutron_lbaas/db/loadbalancer/loadbalancer_dbv2.py View File

@@ -426,6 +426,10 @@ class LoadBalancerPluginDbv2(base_db.CommonDbMixin,
426 426
         lb_db = self._get_resource(context, models.LoadBalancer, id)
427 427
         return data_models.LoadBalancer.from_sqlalchemy_model(lb_db)
428 428
 
429
+    def get_loadbalancer_as_api_dict(self, context, id):
430
+        lb_db = self._get_resource(context, models.LoadBalancer, id)
431
+        return lb_db.to_api_dict
432
+
429 433
     def _validate_listener_data(self, context, listener):
430 434
         pool_id = listener.get('default_pool_id')
431 435
         lb_id = listener.get('loadbalancer_id')
@@ -614,6 +618,10 @@ class LoadBalancerPluginDbv2(base_db.CommonDbMixin,
614 618
         listener_db = self._get_resource(context, models.Listener, id)
615 619
         return data_models.Listener.from_sqlalchemy_model(listener_db)
616 620
 
621
+    def get_listener_as_api_dict(self, context, id):
622
+        listener_db = self._get_resource(context, models.Listener, id)
623
+        return listener_db.to_api_dict
624
+
617 625
     def _create_session_persistence_db(self, session_info, pool_id):
618 626
         session_info['pool_id'] = pool_id
619 627
         return models.SessionPersistenceV2(**session_info)
@@ -744,6 +752,10 @@ class LoadBalancerPluginDbv2(base_db.CommonDbMixin,
744 752
         pool_db = self._get_resource(context, models.PoolV2, id)
745 753
         return data_models.Pool.from_sqlalchemy_model(pool_db)
746 754
 
755
+    def get_pool_as_api_dict(self, context, id):
756
+        pool_db = self._get_resource(context, models.PoolV2, id)
757
+        return pool_db.to_api_dict
758
+
747 759
     def create_pool_member(self, context, member, pool_id):
748 760
         try:
749 761
             with context.session.begin(subtransactions=True):
@@ -790,6 +802,10 @@ class LoadBalancerPluginDbv2(base_db.CommonDbMixin,
790 802
         member_db = self._get_resource(context, models.MemberV2, id)
791 803
         return data_models.Member.from_sqlalchemy_model(member_db)
792 804
 
805
+    def get_pool_member_as_api_dict(self, context, id):
806
+        member_db = self._get_resource(context, models.MemberV2, id)
807
+        return member_db.to_api_dict
808
+
793 809
     def delete_member(self, context, id):
794 810
         with context.session.begin(subtransactions=True):
795 811
             member_db = self._get_resource(context, models.MemberV2, id)
@@ -838,6 +854,10 @@ class LoadBalancerPluginDbv2(base_db.CommonDbMixin,
838 854
         hm_db = self._get_resource(context, models.HealthMonitorV2, id)
839 855
         return data_models.HealthMonitor.from_sqlalchemy_model(hm_db)
840 856
 
857
+    def get_healthmonitor_as_api_dict(self, context, id):
858
+        hm_db = self._get_resource(context, models.HealthMonitorV2, id)
859
+        return hm_db.to_api_dict
860
+
841 861
     def get_healthmonitors(self, context, filters=None):
842 862
         filters = filters or {}
843 863
         hm_dbs = self._get_resources(context, models.HealthMonitorV2,
@@ -946,6 +966,10 @@ class LoadBalancerPluginDbv2(base_db.CommonDbMixin,
946 966
         l7policy_db = self._get_resource(context, models.L7Policy, id)
947 967
         return data_models.L7Policy.from_sqlalchemy_model(l7policy_db)
948 968
 
969
+    def get_l7policy_as_api_dict(self, context, id):
970
+        l7policy_db = self._get_resource(context, models.L7Policy, id)
971
+        return l7policy_db.to_api_dict
972
+
949 973
     def get_l7policies(self, context, filters=None):
950 974
         l7policy_dbs = self._get_resources(context, models.L7Policy,
951 975
                                            filters=filters)
@@ -1010,6 +1034,13 @@ class LoadBalancerPluginDbv2(base_db.CommonDbMixin,
1010 1034
                 l7policy_id=l7policy_id, rule_id=id)
1011 1035
         return data_models.L7Rule.from_sqlalchemy_model(rule_db)
1012 1036
 
1037
+    def get_l7policy_rule_as_api_dict(self, context, id, l7policy_id):
1038
+        rule_db = self._get_resource(context, models.L7Rule, id)
1039
+        if rule_db.l7policy_id != l7policy_id:
1040
+            raise l7.RuleNotFoundForL7Policy(
1041
+                l7policy_id=l7policy_id, rule_id=id)
1042
+        return rule_db.to_api_dict
1043
+
1013 1044
     def get_l7policy_rules(self, context, l7policy_id, filters=None):
1014 1045
         if filters:
1015 1046
             filters.update(filters)

+ 24
- 24
neutron_lbaas/services/loadbalancer/plugin.py View File

@@ -145,9 +145,9 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
145 145
                                 provider)
146 146
 
147 147
     def _get_driver_for_loadbalancer(self, context, loadbalancer_id):
148
-        lb = self.db.get_loadbalancer(context, loadbalancer_id)
148
+        lb = self.db.get_loadbalancer_as_api_dict(context, loadbalancer_id)
149 149
         try:
150
-            return self.drivers[lb.provider.provider_name]
150
+            return self.drivers[lb['provider']]
151 151
         except KeyError:
152 152
             raise n_exc.Invalid(
153 153
                 _("Error retrieving provider for load balancer. Possible "
@@ -395,7 +395,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
395 395
                 lbaas_agentschedulerv2.NoActiveLbaasAgent) as no_agent:
396 396
             self.db.delete_loadbalancer(context, lb_db.id)
397 397
             raise no_agent
398
-        return self.db.get_loadbalancer(context, lb_db.id).to_api_dict()
398
+        return self.db.get_loadbalancer_as_api_dict(context, lb_db.id)
399 399
 
400 400
     def create_graph(self, context, graph):
401 401
         loadbalancer = graph.get('graph', {}).get('loadbalancer')
@@ -437,7 +437,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
437 437
         self._call_driver_operation(context,
438 438
                                     driver.load_balancer.update,
439 439
                                     updated_lb, old_db_entity=old_lb)
440
-        return self.db.get_loadbalancer(context, id).to_api_dict()
440
+        return self.db.get_loadbalancer_as_api_dict(context, id)
441 441
 
442 442
     def delete_loadbalancer(self, context, id):
443 443
         old_lb = self.db.get_loadbalancer(context, id)
@@ -459,7 +459,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
459 459
             context, driver.load_balancer.delete, db_lb)
460 460
 
461 461
     def get_loadbalancer(self, context, id, fields=None):
462
-        return self.db.get_loadbalancer(context, id).to_api_dict()
462
+        return self.db.get_loadbalancer_as_api_dict(context, id)
463 463
 
464 464
     def get_loadbalancers(self, context, filters=None, fields=None):
465 465
         return self.db.get_loadbalancers_as_api_dict(context, filters=filters)
@@ -579,7 +579,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
579 579
         self._call_driver_operation(
580 580
             context, driver.listener.create, listener_db)
581 581
 
582
-        return self.db.get_listener(context, listener_db.id).to_api_dict()
582
+        return self.db.get_listener_as_api_dict(context, listener_db.id)
583 583
 
584 584
     def _check_listener_pool_lb_match(self, context, listener_id, pool_id):
585 585
         listener = self.db.get_listener(context, listener_id)
@@ -639,7 +639,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
639 639
             listener_db,
640 640
             old_db_entity=curr_listener_db)
641 641
 
642
-        return self.db.get_listener(context, id).to_api_dict()
642
+        return self.db.get_listener_as_api_dict(context, id)
643 643
 
644 644
     def delete_listener(self, context, id):
645 645
         old_listener = self.db.get_listener(context, id)
@@ -658,7 +658,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
658 658
             context, driver.listener.delete, listener_db)
659 659
 
660 660
     def get_listener(self, context, id, fields=None):
661
-        return self.db.get_listener(context, id).to_api_dict()
661
+        return self.db.get_listener_as_api_dict(context, id)
662 662
 
663 663
     def get_listeners(self, context, filters=None, fields=None):
664 664
         return self.db.get_listeners_as_api_dict(
@@ -739,7 +739,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
739 739
                                     updated_pool,
740 740
                                     old_db_entity=old_pool)
741 741
 
742
-        return self.db.get_pool(context, id).to_api_dict()
742
+        return self.db.get_pool_as_api_dict(context, id)
743 743
 
744 744
     def delete_pool(self, context, id):
745 745
         old_pool = self.db.get_pool(context, id)
@@ -761,7 +761,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
761 761
             context, filters=filters)
762 762
 
763 763
     def get_pool(self, context, id, fields=None):
764
-        return self.db.get_pool(context, id).to_api_dict()
764
+        return self.db.get_pool_as_api_dict(context, id)
765 765
 
766 766
     def _check_pool_exists(self, context, pool_id):
767 767
         if not self.db._resource_exists(context, models.PoolV2, pool_id):
@@ -771,15 +771,16 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
771 771
     def create_pool_member(self, context, pool_id, member):
772 772
         member = member.get('member')
773 773
         self.db.check_subnet_exists(context, member['subnet_id'])
774
-        db_pool = self.db.get_pool(context, pool_id)
774
+        db_pool = self.db.get_pool_as_api_dict(context, pool_id)
775
+        lb_id = db_pool['loadbalancers'][0]['id']
775 776
         self.db.test_and_set_status(context, models.LoadBalancer,
776
-                                    db_pool.root_loadbalancer.id,
777
-                                    n_constants.PENDING_UPDATE)
777
+                                    lb_id, n_constants.PENDING_UPDATE)
778
+
778 779
         try:
779 780
             member_db = self.db.create_pool_member(context, member, pool_id)
780 781
         except Exception as exc:
781 782
             self.db.update_loadbalancer_provisioning_status(
782
-                context, db_pool.root_loadbalancer.id)
783
+                context, lb_id)
783 784
             raise exc
784 785
 
785 786
         driver = self._get_driver_for_loadbalancer(
@@ -788,7 +789,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
788 789
                                     driver.member.create,
789 790
                                     member_db)
790 791
 
791
-        return self.db.get_pool_member(context, member_db.id).to_api_dict()
792
+        return self.db.get_pool_member_as_api_dict(context, member_db.id)
792 793
 
793 794
     def update_pool_member(self, context, id, pool_id, member):
794 795
         self._check_pool_exists(context, pool_id)
@@ -810,7 +811,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
810 811
                                     updated_member,
811 812
                                     old_db_entity=old_member)
812 813
 
813
-        return self.db.get_pool_member(context, id).to_api_dict()
814
+        return self.db.get_pool_member_as_api_dict(context, id)
814 815
 
815 816
     def delete_pool_member(self, context, id, pool_id):
816 817
         self._check_pool_exists(context, pool_id)
@@ -834,7 +835,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
834 835
 
835 836
     def get_pool_member(self, context, id, pool_id, fields=None):
836 837
         self._check_pool_exists(context, pool_id)
837
-        return self.db.get_pool_member(context, id).to_api_dict()
838
+        return self.db.get_pool_member_as_api_dict(context, id)
838 839
 
839 840
     def _check_pool_already_has_healthmonitor(self, context, pool_id):
840 841
         pool = self.db.get_pool(context, pool_id)
@@ -863,7 +864,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
863 864
         self._call_driver_operation(context,
864 865
                                     driver.health_monitor.create,
865 866
                                     db_hm)
866
-        return self.db.get_healthmonitor(context, db_hm.id).to_api_dict()
867
+        return self.db.get_healthmonitor_as_api_dict(context, db_hm.id)
867 868
 
868 869
     def update_healthmonitor(self, context, id, healthmonitor):
869 870
         healthmonitor = healthmonitor.get('healthmonitor')
@@ -885,7 +886,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
885 886
                                     updated_hm,
886 887
                                     old_db_entity=old_hm)
887 888
 
888
-        return self.db.get_healthmonitor(context, updated_hm.id).to_api_dict()
889
+        return self.db.get_healthmonitor_as_api_dict(context, updated_hm.id)
889 890
 
890 891
     def delete_healthmonitor(self, context, id):
891 892
         self.db.test_and_set_status(context, models.HealthMonitorV2, id,
@@ -898,7 +899,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
898 899
             context, driver.health_monitor.delete, db_hm)
899 900
 
900 901
     def get_healthmonitor(self, context, id, fields=None):
901
-        return self.db.get_healthmonitor(context, id).to_api_dict()
902
+        return self.db.get_healthmonitor_as_api_dict(context, id)
902 903
 
903 904
     def get_healthmonitors(self, context, filters=None, fields=None):
904 905
         return self.db.get_healthmonitors_as_api_dict(
@@ -956,7 +957,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
956 957
                                         updated_l7policy,
957 958
                                         old_db_entity=old_l7policy)
958 959
 
959
-        return self.db.get_l7policy(context, updated_l7policy.id).to_api_dict()
960
+        return self.db.get_l7policy_as_api_dict(context, updated_l7policy.id)
960 961
 
961 962
     def delete_l7policy(self, context, id):
962 963
         self.db.test_and_set_status(context, models.L7Policy, id,
@@ -976,7 +977,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
976 977
             context, filters=filters)
977 978
 
978 979
     def get_l7policy(self, context, id, fields=None):
979
-        return self.db.get_l7policy(context, id).to_api_dict()
980
+        return self.db.get_l7policy_as_api_dict(context, id)
980 981
 
981 982
     def _check_l7policy_exists(self, context, l7policy_id):
982 983
         if not self.db._resource_exists(context, models.L7Policy, l7policy_id):
@@ -1051,8 +1052,7 @@ class LoadBalancerPluginv2(loadbalancerv2.LoadBalancerPluginBaseV2,
1051 1052
 
1052 1053
     def get_l7policy_rule(self, context, id, l7policy_id, fields=None):
1053 1054
         self._check_l7policy_exists(context, l7policy_id)
1054
-        return self.db.get_l7policy_rule(
1055
-            context, id, l7policy_id).to_api_dict()
1055
+        return self.db.get_l7policy_rule_as_api_dict(context, id, l7policy_id)
1056 1056
 
1057 1057
     def validate_provider(self, provider):
1058 1058
         if provider not in self.drivers:

Loading…
Cancel
Save