[MERGE][IMP] mail: limit cache invalidation

This merge goes through various cache invalidation done in mail application
in order to effectively limit it to records and/or fields to invalidate.
Purpose is to avoid having too much cache invalidation that causes to
fetch data again, therefore adding computation and queries to various mail
features. See sub commits for more details about the changes.

All performance tests are improved of a few queries as there is less data
to fetch again from database. On overall community runbot this leads to a
gain of about 16K queries when installing all 205 modules. This merge is
related to task ID 1834147. Closes #24061 .
This commit is contained in:
Thibault Delavallée
2018-04-10 12:21:32 +02:00
7 changed files with 35 additions and 29 deletions
@@ -90,6 +90,7 @@ class TestAccountCustomerInvoice(AccountTestUsers):
# I verify that invoice is now in Paid state
assert (self.account_invoice_customer0.state == 'paid'), "Invoice is not in Paid state"
self.partner3.invalidate_cache(ids=self.partner3.ids)
total_after_confirm = self.partner3.total_invoiced
self.assertEquals(total_after_confirm - total_before_confirm, self.account_invoice_customer0.amount_untaxed_signed)
@@ -190,6 +190,7 @@ class TestHolidaysFlow(TestHrHolidaysBase):
})
hol2_user_group = hol2.sudo(self.user_hruser_id)
# Check left days: - 1 virtual remaining day
hol_status_2_employee_group.invalidate_cache()
_check_holidays_status(hol_status_2_employee_group, 2.0, 0.0, 2.0, 1.0)
# HrManager validates the first step
+2 -1
View File
@@ -57,7 +57,8 @@ class Followers(models.Model):
if 'res_model' in vals or 'res_id' in vals:
self._invalidate_documents()
res = super(Followers, self).write(vals)
self._invalidate_documents()
if any(x in vals for x in ['res_model', 'res_id', 'partner_id']):
self._invalidate_documents()
return res
@api.multi
+12 -9
View File
@@ -717,8 +717,14 @@ class Message(models.Model):
def _invalidate_documents(self):
""" Invalidate the cache of the documents followed by ``self``. """
for record in self:
if record.model and record.res_id:
self.env[record.model].invalidate_cache(ids=[record.res_id])
if record.model and record.res_id and 'message_ids' in self.env[record.model]:
self.env[record.model].invalidate_cache(fnames=[
'message_ids',
'message_unread',
'message_unread_counter',
'message_needaction',
'message_needaction_counter',
], ids=[record.res_id])
@api.model
def create(self, values):
@@ -764,7 +770,8 @@ class Message(models.Model):
if tracking_values_cmd:
message.sudo().write({'tracking_value_ids': tracking_values_cmd})
message._invalidate_documents()
if values.get('model') and values.get('res_id'):
message._invalidate_documents()
return message
@@ -780,7 +787,8 @@ class Message(models.Model):
if 'model' in vals or 'res_id' in vals:
self._invalidate_documents()
res = super(Message, self).write(vals)
self._invalidate_documents()
if 'notification_ids' in vals or 'model' in vals or 'res_id' in vals:
self._invalidate_documents()
return res
@api.multi
@@ -856,9 +864,4 @@ class Message(models.Model):
channels_sudo._notify(self)
# Discard cache, because child / parent allow reading and therefore
# change access rights.
if self.parent_id:
self.parent_id.invalidate_cache()
return True
-1
View File
@@ -2084,7 +2084,6 @@ class MailThread(models.AbstractModel):
channel_ids, dict((cid, subtype_ids) for cid in channel_ids),
customer_ids=customer_ids, check_existing=True, existing_policy='force')
self.invalidate_cache()
return True
@api.multi
@@ -99,6 +99,7 @@ class TestMailActivity(BaseFunctionalTest):
self.assertEqual(self.test_record.activity_state, 'overdue')
self.assertEqual(self.test_record.activity_user_id, self.user_employee)
self.test_record.invalidate_cache(ids=self.test_record.ids)
self.assertEqual(self.test_record.activity_ids, act1 | act2 | act3)
# Perform todo activities for admin
+18 -18
View File
@@ -77,7 +77,7 @@ class TestMailPerformance(TransactionCase):
'partner_id': self.env.ref('base.res_partner_12').id,
})
with self.assertQueryCount(admin=7, demo=7): # test_mail only: 7 - 7
with self.assertQueryCount(admin=6, demo=6): # test_mail only: 6 - 6
record.track = 'X'
@users('admin', 'demo')
@@ -93,7 +93,7 @@ class TestMailPerformance(TransactionCase):
@warmup
def test_create_mail_with_tracking(self):
""" Create records inheriting from 'mail.thread' (with field tracking). """
with self.assertQueryCount(admin=15, demo=15): # test_mail only: 15 - 15
with self.assertQueryCount(admin=14, demo=14): # test_mail only: 14 - 14
self.env['test_performance.mail'].create({'name': 'X'})
@users('admin', 'emp')
@@ -160,14 +160,14 @@ class TestAdvMailPerformance(TransactionCase):
'default_res_model': 'mail.test.activity',
})
with self.assertQueryCount(admin=11, emp=15): # test_mail only: 11 - 15
with self.assertQueryCount(admin=9, emp=13): # test_mail only: 9 - 13
activity = MailActivity.create({
'summary': 'Test Activity',
'res_id': record.id,
'activity_type_id': self.env.ref('mail.mail_activity_data_todo').id,
})
with self.assertQueryCount(margin=1, admin=56, emp=85): # test_mail only: 56 - 85
with self.assertQueryCount(margin=1, admin=49, emp=73): # test_mail only: 49 - 73
activity.action_feedback(feedback='Zizisse Done !')
@users('admin', 'emp')
@@ -176,12 +176,12 @@ class TestAdvMailPerformance(TransactionCase):
def test_adv_activity_mixin(self):
record = self.env['mail.test.activity'].create({'name': 'Test'})
with self.assertQueryCount(admin=11, emp=15): # test_mail only: 11 - 15
with self.assertQueryCount(admin=9, emp=13): # test_mail only: 9 - 13
record.action_start('Test Start')
record.write({'name': 'Dupe write'})
with self.assertQueryCount(margin=1, admin=56, emp=86): # test_mail only: 56 - 85
with self.assertQueryCount(margin=1, admin=51, emp=75): # test_mail only: 51 - 75
record.action_close('Dupe feedback')
self.assertEqual(record.activity_ids, self.env['mail.activity'])
@@ -193,7 +193,7 @@ class TestAdvMailPerformance(TransactionCase):
self.user_test.write({'notification_type': 'email'})
record = self.env['mail.test.track'].create({'name': 'Test'})
with self.assertQueryCount(margin=1, admin=82, emp=107): # test_mail only: 80 - 105
with self.assertQueryCount(margin=1, admin=81, emp=105): # test_mail only: 79 - 103
record.write({
'user_id': self.user_test.id,
})
@@ -247,7 +247,7 @@ class TestAdvMailPerformance(TransactionCase):
def test_message_post_one_email_notification(self):
record = self.env['mail.test.simple'].create({'name': 'Test'})
with self.assertQueryCount(margin=1, admin=75, emp=101): # test_mail only: 73 - 99
with self.assertQueryCount(margin=1, admin=74, emp=99): # com runbot: 72 - 97 // test_mail only: 72 - 97
record.message_post(
body='<p>Test Post Performances with an email ping</p>',
partner_ids=self.customer.ids,
@@ -275,7 +275,7 @@ class TestAdvMailPerformance(TransactionCase):
with self.assertQueryCount(admin=6, emp=6): # test_mail only: 6 - 6
record.message_subscribe(partner_ids=self.user_test.partner_id.ids)
with self.assertQueryCount(admin=3, emp=3): # test_mail only: 3 - 3
with self.assertQueryCount(admin=2, emp=2): # test_mail only: 2 - 2
record.message_subscribe(partner_ids=self.user_test.partner_id.ids)
@mute_logger('odoo.models.unlink')
@@ -288,7 +288,7 @@ class TestAdvMailPerformance(TransactionCase):
with self.assertQueryCount(admin=5, emp=5): # test_mail only: 5 - 5
record.message_subscribe(partner_ids=self.user_test.partner_id.ids, subtype_ids=subtype_ids)
with self.assertQueryCount(admin=14, emp=14): # test_mail only: 14 - 14
with self.assertQueryCount(admin=12, emp=12): # test_mail only: 12 - 12
record.message_subscribe(partner_ids=self.user_test.partner_id.ids, subtype_ids=subtype_ids)
@@ -382,7 +382,7 @@ class TestHeavyMailPerformance(TransactionCase):
self.umbrella.message_subscribe(self.user_portal.partner_id.ids)
record = self.umbrella.sudo(self.env.user)
with self.assertQueryCount(admin=116, emp=147): # com runbot 114 - 145 // test_mail only: 112 - 143
with self.assertQueryCount(admin=114, emp=143): # com runbot 112 - 141 // test_mail only: 110 - 139
record.message_post(
body='<p>Test Post Performances</p>',
message_type='comment',
@@ -399,7 +399,7 @@ class TestHeavyMailPerformance(TransactionCase):
record = self.umbrella.sudo(self.env.user)
template_id = self.env.ref('test_mail.mail_test_tpl').id
with self.assertQueryCount(admin=138, emp=183): # com runbot 136 - 181 // test_mail only: 134 - 179
with self.assertQueryCount(admin=133, emp=174): # com runbot 131 - 172 // test_mail only: 129 - 170
record.message_post_with_template(template_id, message_type='comment', composition_mode='comment')
self.assertEqual(record.message_ids[0].body, '<p>Adding stuff on %s</p>' % record.name)
@@ -446,7 +446,7 @@ class TestHeavyMailPerformance(TransactionCase):
self.assertEqual(rec.message_channel_ids, self.channel)
# subscribe existing and new followers with force=True, meaning all will have the same subtypes
with self.assertQueryCount(admin=42, emp=43): # test_mail only: 42 - 43
with self.assertQueryCount(admin=42, emp=42): # test_mail only: 42 - 42
rec.message_subscribe(
partner_ids=pids,
channel_ids=cids,
@@ -469,7 +469,7 @@ class TestHeavyMailPerformance(TransactionCase):
})
self.assertEqual(rec.message_partner_ids, self.partners | self.env.user.partner_id)
with self.assertQueryCount(admin=84, emp=111): # test_mail only: 82 - 109
with self.assertQueryCount(admin=82, emp=108): # test_mail only: 80 - 106
rec.write({'user_id': self.user_portal.id})
self.assertEqual(rec.message_partner_ids, self.partners | self.env.user.partner_id | self.user_portal.partner_id)
@@ -492,7 +492,7 @@ class TestHeavyMailPerformance(TransactionCase):
customer_id = self.customer.id
user_id = self.user_portal.id
with self.assertQueryCount(admin=237, emp=286): # test_mail only: 230 - 279
with self.assertQueryCount(margin=1, admin=235, emp=284): # test_mail only: 228 - 277
rec = self.env['mail.test.full'].create({
'name': 'Test',
'umbrella_id': umbrella_id,
@@ -521,7 +521,7 @@ class TestHeavyMailPerformance(TransactionCase):
})
self.assertEqual(rec.message_partner_ids, self.user_portal.partner_id | self.env.user.partner_id)
with self.assertQueryCount(admin=149, emp=173): # test_mail only: 144 - 168
with self.assertQueryCount(admin=147, emp=172): # test_mail only: 142 - 167
rec.write({
'name': 'Test2',
'umbrella_id': self.umbrella.id,
@@ -559,7 +559,7 @@ class TestHeavyMailPerformance(TransactionCase):
})
self.assertEqual(rec.message_partner_ids, self.user_portal.partner_id | self.env.user.partner_id)
with self.assertQueryCount(admin=155, emp=183): # test_mail only: 150 - 178
with self.assertQueryCount(admin=152, emp=180): # test_mail only: 147 - 175
rec.write({
'name': 'Test2',
'umbrella_id': umbrella_id,
@@ -593,7 +593,7 @@ class TestHeavyMailPerformance(TransactionCase):
})
self.assertEqual(rec.message_partner_ids, self.partners | self.env.user.partner_id | self.user_portal.partner_id)
with self.assertQueryCount(admin=61, emp=84): # test_mail only: 59 - 82
with self.assertQueryCount(admin=57, emp=78): # test_mail only: 55 - 76
rec.write({
'name': 'Test2',
'customer_id': customer_id,