Need information

Sean Mooney smooney at redhat.com
Mon Nov 14 14:36:10 UTC 2022


On Mon, 2022-11-14 at 18:33 +0530, Gk Gk wrote:
> Hi All,
> 
> At the core of nova-api, I am trying to trace the function which executes
> the SQL query for listing
> all the instances for a user. I want to  know where in the code this query
> is executed. I have traced it till this point in the code:
> 
> ----
> if self.cells:
> results = context.scatter_gather_cells(ctx, self.cells,
> context.CELL_TIMEOUT,
> query_wrapper, do_query)
> ---

we do not enbed sql directly in our code.
thats generally bad pratice so most moderen opensrouce proejct will either use an orm or create a centralised
db module that is called into to avoid poluting code with SQL

nova does both 

https://github.com/openstack/nova/blob/master/nova/db/main/api.py#L1547-L1844

instance_get_all, instance_get_all_by_filters and  instance_get_all_by_filters_sort are the primay ways to list instances in the cell db

those function use sqlachemy to generate the sql


> 
> in the file "
> https://github.com/openstack/nova/blob/c97507dfcd57cce9d76670d3b0d48538900c00e9/nova/compute/multi_cell_list.py#L218
> "
> 
> But where in the above file, is the SQL query executed ?  Please help me

so instance list starts here
https://github.com/openstack/nova/blob/2eb358cdcec36fcfe5388ce6982d2961ca949d0a/nova/api/openstack/compute/servers.py#L116
which calls _getServers
https://github.com/openstack/nova/blob/2eb358cdcec36fcfe5388ce6982d2961ca949d0a/nova/api/openstack/compute/servers.py#L174
after preparing the inputs for the get_all function call it invokes it here
https://github.com/openstack/nova/blob/2eb358cdcec36fcfe5388ce6982d2961ca949d0a/nova/api/openstack/compute/servers.py#L327-L331
which is implemented here
https://github.com/openstack/nova/blob/2eb358cdcec36fcfe5388ce6982d2961ca949d0a/nova/compute/api.py#L2991

initally that fucntion buils d a list of instance that currently have build request then it callse
instance_list.get_instance_objects_sorted
https://github.com/openstack/nova/blob/2eb358cdcec36fcfe5388ce6982d2961ca949d0a/nova/compute/api.py#L3138
that calls  get_instances_sorted which just the InstanceLister to eventually call 
 db.instance_get_all_by_filters_sort
https://github.com/openstack/nova/blob/2eb358cdcec36fcfe5388ce6982d2961ca949d0a/nova/compute/instance_list.py#L83
fhat finally gets us to instance_get_all_by_filters_sort which is the funciton that generates the sql query
https://github.com/openstack/nova/blob/master/nova/db/main/api.py#L1618
if that seams very complicated its because each nova cell has its own DB instance and we also need to supprot pagination of the
results

> 
> 
> Thanks
> Kumar




More information about the openstack-discuss mailing list