Commit 46c64ac2 authored by Romain Courteaud's avatar Romain Courteaud

slapos_slap_tool: reduce number of sql queries

No need to query the catalog multiple times for every partitions
parent ecc0d024
...@@ -181,12 +181,16 @@ class SlapTool(BaseTool): ...@@ -181,12 +181,16 @@ class SlapTool(BaseTool):
slap_computer._computer_partition_list = [] slap_computer._computer_partition_list = []
slap_computer._software_release_list = \ slap_computer._software_release_list = \
self._getSoftwareReleaseValueListForComputer(computer_id) self._getSoftwareReleaseValueListForComputer(computer_id)
for computer_partition in self.getPortalObject().portal_catalog.unrestrictedSearchResults(
parent_uid=parent_uid, unrestrictedSearchResults = self.getPortalObject().portal_catalog.unrestrictedSearchResults
validation_state="validated",
portal_type="Computer Partition"): computer_partition_list = unrestrictedSearchResults(
slap_computer._computer_partition_list.append( parent_uid=parent_uid,
self._getSlapPartitionByPackingList(_assertACI(computer_partition.getObject()))) validation_state="validated",
portal_type="Computer Partition"
)
self._calculateSlapComputerInformation(slap_computer, computer_partition_list)
return dumps(slap_computer) return dumps(slap_computer)
def _fillComputerInformationCache(self, computer_id, user): def _fillComputerInformationCache(self, computer_id, user):
...@@ -235,6 +239,40 @@ class SlapTool(BaseTool): ...@@ -235,6 +239,40 @@ class SlapTool(BaseTool):
self.activate(activity='SQLQueue', tag=tag)._fillComputerInformationCache( self.activate(activity='SQLQueue', tag=tag)._fillComputerInformationCache(
computer_id, user) computer_id, user)
def _calculateSlapComputerInformation(self, slap_computer, computer_partition_list):
if len(computer_partition_list) == 0:
return
unrestrictedSearchResults = self.getPortalObject().portal_catalog.unrestrictedSearchResults
computer_partition_uid_list = [x.uid for x in computer_partition_list]
grouped_software_instance_list = unrestrictedSearchResults(
portal_type="Software Instance",
default_aggregate_uid=computer_partition_uid_list,
validation_state="validated",
group_by_list=['default_aggregate_uid'],
select_list=['default_aggregate_uid', 'count(*)']
)
slave_software_instance_list = unrestrictedSearchResults(
default_aggregate_uid=computer_partition_uid_list,
portal_type='Slave Instance',
validation_state="validated",
select_list=['default_aggregate_uid'],
**{"slapos_item.slap_state": "start_requested"}
)
for computer_partition in computer_partition_list:
software_instance_list = [x for x in grouped_software_instance_list if (x.default_aggregate_uid == computer_partition.getUid())]
if (len(software_instance_list) == 1) and (software_instance_list[0]['count(*)'] > 1):
software_instance_list = software_instance_list + software_instance_list
slap_computer._computer_partition_list.append(
self._getSlapPartitionByPackingList(
_assertACI(computer_partition.getObject()),
software_instance_list,
[x for x in slave_software_instance_list if (x.default_aggregate_uid == computer_partition.getUid())]
)
)
def _getComputerInformation(self, computer_id, user): def _getComputerInformation(self, computer_id, user):
user_document = _assertACI(self.getPortalObject().portal_catalog.unrestrictedGetResultValue( user_document = _assertACI(self.getPortalObject().portal_catalog.unrestrictedGetResultValue(
reference=user, portal_type=['Person', 'Computer', 'Software Instance'])) reference=user, portal_type=['Person', 'Computer', 'Software Instance']))
...@@ -277,9 +315,8 @@ class SlapTool(BaseTool): ...@@ -277,9 +315,8 @@ class SlapTool(BaseTool):
parent_uid=parent_uid, parent_uid=parent_uid,
validation_state="validated", validation_state="validated",
portal_type="Computer Partition") portal_type="Computer Partition")
for computer_partition in computer_partition_list:
slap_computer._computer_partition_list.append( self._calculateSlapComputerInformation(slap_computer, computer_partition_list)
self._getSlapPartitionByPackingList(_assertACI(computer_partition.getObject())))
return dumps(slap_computer) return dumps(slap_computer)
@UnrestrictedMethod @UnrestrictedMethod
...@@ -918,9 +955,10 @@ class SlapTool(BaseTool): ...@@ -918,9 +955,10 @@ class SlapTool(BaseTool):
LOG('SlapTool', INFO, 'Issue during parsing xml:', error=True) LOG('SlapTool', INFO, 'Issue during parsing xml:', error=True)
return result_dict return result_dict
def _getSlapPartitionByPackingList(self, computer_partition_document): def _getSlapPartitionByPackingList(self, computer_partition_document,
software_instance_list,
slave_instance_sql_list):
computer = computer_partition_document computer = computer_partition_document
portal = self.getPortalObject()
while computer.getPortalType() != 'Computer': while computer.getPortalType() != 'Computer':
computer = computer.getParentValue() computer = computer.getParentValue()
computer_id = computer.getReference().decode("UTF-8") computer_id = computer.getReference().decode("UTF-8")
...@@ -934,20 +972,13 @@ class SlapTool(BaseTool): ...@@ -934,20 +972,13 @@ class SlapTool(BaseTool):
software_instance = None software_instance = None
if computer_partition_document.getSlapState() == 'busy': if computer_partition_document.getSlapState() == 'busy':
software_instance_list = portal.portal_catalog.unrestrictedSearchResults(
portal_type="Software Instance",
default_aggregate_uid=computer_partition_document.getUid(),
validation_state="validated",
limit=2,
)
software_instance_count = len(software_instance_list) software_instance_count = len(software_instance_list)
if software_instance_count == 1: if software_instance_count == 1:
software_instance = _assertACI(software_instance_list[0].getObject()) software_instance = _assertACI(software_instance_list[0].getObject())
elif software_instance_count > 1: elif software_instance_count > 1:
# XXX do not prevent the system to work if one partition is broken # XXX do not prevent the system to work if one partition is broken
raise NotImplementedError, "Too many instances %s linked to %s" % \ raise NotImplementedError, "Too many instances linked to %s" % \
([x.path for x in software_instance_list], computer_partition_document.getRelativeUrl()
computer_partition_document.getRelativeUrl())
if software_instance is not None: if software_instance is not None:
state = software_instance.getSlapState() state = software_instance.getSlapState()
...@@ -965,7 +996,9 @@ class SlapTool(BaseTool): ...@@ -965,7 +996,9 @@ class SlapTool(BaseTool):
slap_partition._need_modification = 1 slap_partition._need_modification = 1
parameter_dict = self._getSoftwareInstanceAsParameterDict( parameter_dict = self._getSoftwareInstanceAsParameterDict(
software_instance) software_instance,
slave_instance_sql_list=slave_instance_sql_list
)
# software instance has to define an xml parameter # software instance has to define an xml parameter
slap_partition._parameter_dict = self._instanceXmlToDict( slap_partition._parameter_dict = self._instanceXmlToDict(
parameter_dict.pop('xml')) parameter_dict.pop('xml'))
...@@ -1515,7 +1548,7 @@ class SlapTool(BaseTool): ...@@ -1515,7 +1548,7 @@ class SlapTool(BaseTool):
return software_instance return software_instance
@UnrestrictedMethod @UnrestrictedMethod
def _getSoftwareInstanceAsParameterDict(self, software_instance): def _getSoftwareInstanceAsParameterDict(self, software_instance, slave_instance_sql_list=None):
portal = software_instance.getPortalObject() portal = software_instance.getPortalObject()
computer_partition = software_instance.getAggregateValue(portal_type="Computer Partition") computer_partition = software_instance.getAggregateValue(portal_type="Computer Partition")
timestamp = int(computer_partition.getModificationDate()) timestamp = int(computer_partition.getModificationDate())
...@@ -1546,12 +1579,13 @@ class SlapTool(BaseTool): ...@@ -1546,12 +1579,13 @@ class SlapTool(BaseTool):
slave_instance_list = [] slave_instance_list = []
if (software_instance.getPortalType() == "Software Instance"): if (software_instance.getPortalType() == "Software Instance"):
append = slave_instance_list.append append = slave_instance_list.append
slave_instance_sql_list = portal.portal_catalog.unrestrictedSearchResults( if slave_instance_sql_list is None:
default_aggregate_uid=computer_partition.getUid(), slave_instance_sql_list = portal.portal_catalog.unrestrictedSearchResults(
portal_type='Slave Instance', default_aggregate_uid=computer_partition.getUid(),
validation_state="validated", portal_type='Slave Instance',
**{"slapos_item.slap_state": "start_requested"} validation_state="validated",
) **{"slapos_item.slap_state": "start_requested"}
)
for slave_instance in slave_instance_sql_list: for slave_instance in slave_instance_sql_list:
slave_instance = _assertACI(slave_instance.getObject()) slave_instance = _assertACI(slave_instance.getObject())
# XXX Use catalog to filter more efficiently # XXX Use catalog to filter more efficiently
......
...@@ -6,6 +6,12 @@ ...@@ -6,6 +6,12 @@
</pickle> </pickle>
<pickle> <pickle>
<dictionary> <dictionary>
<item>
<key> <string>_recorded_property_dict</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item> <item>
<key> <string>default_reference</string> </key> <key> <string>default_reference</string> </key>
<value> <string>SlapTool</string> </value> <value> <string>SlapTool</string> </value>
...@@ -49,13 +55,28 @@ ...@@ -49,13 +55,28 @@
<item> <item>
<key> <string>workflow_history</string> </key> <key> <string>workflow_history</string> </key>
<value> <value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent> <persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value> </value>
</item> </item>
</dictionary> </dictionary>
</pickle> </pickle>
</record> </record>
<record id="2" aka="AAAAAAAAAAI="> <record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary/>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle> <pickle>
<global name="PersistentMapping" module="Persistence.mapping"/> <global name="PersistentMapping" module="Persistence.mapping"/>
</pickle> </pickle>
...@@ -68,7 +89,7 @@ ...@@ -68,7 +89,7 @@
<item> <item>
<key> <string>component_validation_workflow</string> </key> <key> <string>component_validation_workflow</string> </key>
<value> <value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent> <persistent> <string encoding="base64">AAAAAAAAAAQ=</string> </persistent>
</value> </value>
</item> </item>
</dictionary> </dictionary>
...@@ -77,7 +98,7 @@ ...@@ -77,7 +98,7 @@
</dictionary> </dictionary>
</pickle> </pickle>
</record> </record>
<record id="3" aka="AAAAAAAAAAM="> <record id="4" aka="AAAAAAAAAAQ=">
<pickle> <pickle>
<global name="WorkflowHistoryList" module="Products.ERP5Type.Workflow"/> <global name="WorkflowHistoryList" module="Products.ERP5Type.Workflow"/>
</pickle> </pickle>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment