You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
11 KiB

7 years ago
7 years ago
Remove RowExclusiveLock on exception_rule The goal of the modified method is to create or remove the relationship (in the M2m relation tabel) between the tested model (such as sale_order) and the exception rules. When the ORM writes on ExceptionRule.sale_ids (using the example of sale_exception), it will first proceeds with these updates: * an UPDATE on exception_rule to set the write_date * INSERT or DELETE on the relation table * but then, as "write" is called on the exception rule, the ORM will trigger the api.depends to recompute all the "main_exception_ids" of the records (sales, ...) related to it, leading to an UPDATE for each sale order We end up with RowExclusiveLock on such records: * All the records of the relation table added / deleted for the current sale order * All the records of exception_rule matching the current sale order * All the records of sale_order related to the exception rules matching the current sale order The first one is expected, the next 2 are not. We can remove the lock on the exception_rule table by removing `_log_access`, however in any case, the main_exception_ids computed field will continue to lock many sale orders, effectively preventing 2 sales orders with the same exception to be confirmed at the same time. Reversing the write by writing on SaleOrder instead of ExceptionRule fixes the 2 unexpected locks. It should not result in more queries: the "to remove" part generates a DELETE on the relation table for the rule to remove and the "to add" part generates an INSERT for the rule to add, both will be exactly the same in both cases. Related to #1642 Replaces #1638
5 years ago
Remove RowExclusiveLock on exception_rule The goal of the modified method is to create or remove the relationship (in the M2m relation tabel) between the tested model (such as sale_order) and the exception rules. When the ORM writes on ExceptionRule.sale_ids (using the example of sale_exception), it will first proceeds with these updates: * an UPDATE on exception_rule to set the write_date * INSERT or DELETE on the relation table * but then, as "write" is called on the exception rule, the ORM will trigger the api.depends to recompute all the "main_exception_ids" of the records (sales, ...) related to it, leading to an UPDATE for each sale order We end up with RowExclusiveLock on such records: * All the records of the relation table added / deleted for the current sale order * All the records of exception_rule matching the current sale order * All the records of sale_order related to the exception rules matching the current sale order The first one is expected, the next 2 are not. We can remove the lock on the exception_rule table by removing `_log_access`, however in any case, the main_exception_ids computed field will continue to lock many sale orders, effectively preventing 2 sales orders with the same exception to be confirmed at the same time. Reversing the write by writing on SaleOrder instead of ExceptionRule fixes the 2 unexpected locks. It should not result in more queries: the "to remove" part generates a DELETE on the relation table for the rule to remove and the "to add" part generates an INSERT for the rule to add, both will be exactly the same in both cases. Related to #1642 Replaces #1638
5 years ago
Remove RowExclusiveLock on exception_rule The goal of the modified method is to create or remove the relationship (in the M2m relation tabel) between the tested model (such as sale_order) and the exception rules. When the ORM writes on ExceptionRule.sale_ids (using the example of sale_exception), it will first proceeds with these updates: * an UPDATE on exception_rule to set the write_date * INSERT or DELETE on the relation table * but then, as "write" is called on the exception rule, the ORM will trigger the api.depends to recompute all the "main_exception_ids" of the records (sales, ...) related to it, leading to an UPDATE for each sale order We end up with RowExclusiveLock on such records: * All the records of the relation table added / deleted for the current sale order * All the records of exception_rule matching the current sale order * All the records of sale_order related to the exception rules matching the current sale order The first one is expected, the next 2 are not. We can remove the lock on the exception_rule table by removing `_log_access`, however in any case, the main_exception_ids computed field will continue to lock many sale orders, effectively preventing 2 sales orders with the same exception to be confirmed at the same time. Reversing the write by writing on SaleOrder instead of ExceptionRule fixes the 2 unexpected locks. It should not result in more queries: the "to remove" part generates a DELETE on the relation table for the rule to remove and the "to add" part generates an INSERT for the rule to add, both will be exactly the same in both cases. Related to #1642 Replaces #1638
5 years ago
Remove RowExclusiveLock on exception_rule The goal of the modified method is to create or remove the relationship (in the M2m relation tabel) between the tested model (such as sale_order) and the exception rules. When the ORM writes on ExceptionRule.sale_ids (using the example of sale_exception), it will first proceeds with these updates: * an UPDATE on exception_rule to set the write_date * INSERT or DELETE on the relation table * but then, as "write" is called on the exception rule, the ORM will trigger the api.depends to recompute all the "main_exception_ids" of the records (sales, ...) related to it, leading to an UPDATE for each sale order We end up with RowExclusiveLock on such records: * All the records of the relation table added / deleted for the current sale order * All the records of exception_rule matching the current sale order * All the records of sale_order related to the exception rules matching the current sale order The first one is expected, the next 2 are not. We can remove the lock on the exception_rule table by removing `_log_access`, however in any case, the main_exception_ids computed field will continue to lock many sale orders, effectively preventing 2 sales orders with the same exception to be confirmed at the same time. Reversing the write by writing on SaleOrder instead of ExceptionRule fixes the 2 unexpected locks. It should not result in more queries: the "to remove" part generates a DELETE on the relation table for the rule to remove and the "to add" part generates an INSERT for the rule to add, both will be exactly the same in both cases. Related to #1642 Replaces #1638
5 years ago
  1. # Copyright 2011 Raphaël Valyi, Renato Lima, Guewen Baconnier, Sodexis
  2. # Copyright 2017 Akretion (http://www.akretion.com)
  3. # Mourad EL HADJ MIMOUNE <mourad.elhadj.mimoune@akretion.com>
  4. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  5. import time
  6. import html
  7. from odoo import api, fields, models, _
  8. from odoo.exceptions import UserError, ValidationError
  9. from odoo.tools.safe_eval import safe_eval
  10. from odoo import osv
  11. class ExceptionRule(models.Model):
  12. _name = "exception.rule"
  13. _description = "Exception Rule"
  14. _order = "active desc, sequence asc"
  15. name = fields.Char("Exception Name", required=True, translate=True)
  16. description = fields.Text("Description", translate=True)
  17. sequence = fields.Integer(
  18. string="Sequence",
  19. help="Gives the sequence order when applying the test",
  20. )
  21. model = fields.Selection(selection=[], string="Apply on", required=True)
  22. exception_type = fields.Selection(
  23. selection=[
  24. ("by_domain", "By domain"),
  25. ("by_py_code", "By python code"),
  26. ("by_method", "By method"),
  27. ],
  28. string="Exception Type",
  29. required=True,
  30. default="by_py_code",
  31. help="By python code: allow to define any arbitrary check\n"
  32. "By domain: limited to a selection by an odoo domain:\n"
  33. " performance can be better when exceptions "
  34. " are evaluated with several records",
  35. )
  36. domain = fields.Char("Domain")
  37. method = fields.Selection(selection=[], string="Method", readonly=True)
  38. active = fields.Boolean("Active", default=True)
  39. code = fields.Text(
  40. "Python Code",
  41. help="Python code executed to check if the exception apply or "
  42. "not. Use failed = True to block the exception",
  43. )
  44. @api.constrains("exception_type", "domain", "code", "model")
  45. def check_exception_type_consistency(self):
  46. for rule in self:
  47. if (
  48. (rule.exception_type == "by_py_code" and not rule.code)
  49. or (rule.exception_type == "by_domain" and not rule.domain)
  50. or (rule.exception_type == "by_method" and not rule.method)
  51. ):
  52. raise ValidationError(
  53. _(
  54. "There is a problem of configuration, python code, "
  55. "domain or method is missing to match the exception "
  56. "type."
  57. )
  58. )
  59. @api.multi
  60. def _get_domain(self):
  61. """ override me to customize domains according exceptions cases """
  62. self.ensure_one()
  63. return safe_eval(self.domain)
  64. class BaseExceptionMethod(models.AbstractModel):
  65. _name = "base.exception.method"
  66. _description = "Exception Rule Methods"
  67. @api.multi
  68. def _get_main_records(self):
  69. """
  70. Used in case we check exceptions on a record but write these
  71. exceptions on a parent record. Typical example is with
  72. sale.order.line. We check exceptions on some sale order lines but
  73. write these exceptions on the sale order, so they are visible.
  74. """
  75. return self
  76. @api.multi
  77. def _reverse_field(self):
  78. raise NotImplementedError()
  79. def _rule_domain(self):
  80. """Filter exception.rules.
  81. By default, only the rules with the correct model
  82. will be used.
  83. """
  84. return [("model", "=", self._name)]
  85. @api.multi
  86. def detect_exceptions(self):
  87. """List all exception_ids applied on self
  88. Exception ids are also written on records
  89. """
  90. rules = self.env["exception.rule"].sudo().search(self._rule_domain())
  91. all_exception_ids = []
  92. rules_to_remove = {}
  93. rules_to_add = {}
  94. for rule in rules:
  95. records_with_exception = self._detect_exceptions(rule)
  96. reverse_field = self._reverse_field()
  97. main_records = self._get_main_records()
  98. commons = main_records & rule[reverse_field]
  99. to_remove = commons - records_with_exception
  100. to_add = records_with_exception - commons
  101. # we expect to always work on the same model type
  102. if rule.id not in rules_to_remove:
  103. rules_to_remove[rule.id] = main_records.browse()
  104. rules_to_remove[rule.id] |= to_remove
  105. if rule.id not in rules_to_add:
  106. rules_to_add[rule.id] = main_records.browse()
  107. rules_to_add[rule.id] |= to_add
  108. if records_with_exception:
  109. all_exception_ids.append(rule.id)
  110. # Cumulate all the records to attach to the rule
  111. # before linking. We don't want to call "rule.write()"
  112. # which would:
  113. # * write on write_date so lock the expection.rule
  114. # * trigger the recomputation of "main_exception_id" on
  115. # all the sale orders related to the rule, locking them all
  116. # and preventing concurrent writes
  117. # Reversing the write by writing on SaleOrder instead of
  118. # ExceptionRule fixes the 2 kinds of unexpected locks.
  119. # It should not result in more queries than writing on ExceptionRule:
  120. # the "to remove" part generates one DELETE per rule on the relation
  121. # table
  122. # and the "to add" part generates one INSERT (with unnest) per rule.
  123. for rule_id, records in rules_to_remove.items():
  124. records.write({"exception_ids": [(3, rule_id)]})
  125. for rule_id, records in rules_to_add.items():
  126. records.write(({"exception_ids": [(4, rule_id)]}))
  127. return all_exception_ids
  128. @api.model
  129. def _exception_rule_eval_context(self, rec):
  130. return {
  131. "time": time,
  132. "self": rec,
  133. # object, obj: deprecated.
  134. # should be removed in future migrations
  135. "object": rec,
  136. "obj": rec,
  137. # copy context to prevent side-effects of eval
  138. # should be deprecated too, accesible through self.
  139. "context": self.env.context.copy(),
  140. }
  141. @api.model
  142. def _rule_eval(self, rule, rec):
  143. expr = rule.code
  144. space = self._exception_rule_eval_context(rec)
  145. try:
  146. safe_eval(
  147. expr, space, mode="exec", nocopy=True
  148. ) # nocopy allows to return 'result'
  149. except Exception as e:
  150. raise UserError(
  151. _(
  152. "Error when evaluating the exception.rule "
  153. "rule:\n %s \n(%s)"
  154. )
  155. % (rule.name, e)
  156. )
  157. return space.get("failed", False)
  158. @api.multi
  159. def _detect_exceptions(self, rule):
  160. if rule.exception_type == "by_py_code":
  161. return self._detect_exceptions_by_py_code(rule)
  162. elif rule.exception_type == "by_domain":
  163. return self._detect_exceptions_by_domain(rule)
  164. elif rule.exception_type == "by_method":
  165. return self._detect_exceptions_by_method(rule)
  166. @api.multi
  167. def _get_base_domain(self):
  168. return [("ignore_exception", "=", False), ("id", "in", self.ids)]
  169. @api.multi
  170. def _detect_exceptions_by_py_code(self, rule):
  171. """
  172. Find exceptions found on self.
  173. """
  174. domain = self._get_base_domain()
  175. records = self.search(domain)
  176. records_with_exception = self.env[self._name]
  177. for record in records:
  178. if self._rule_eval(rule, record):
  179. records_with_exception |= record
  180. return records_with_exception
  181. @api.multi
  182. def _detect_exceptions_by_domain(self, rule):
  183. """
  184. Find exceptions found on self.
  185. """
  186. base_domain = self._get_base_domain()
  187. rule_domain = rule._get_domain()
  188. domain = osv.expression.AND([base_domain, rule_domain])
  189. return self.search(domain)
  190. @api.multi
  191. def _detect_exceptions_by_method(self, rule):
  192. """
  193. Find exceptions found on self.
  194. """
  195. base_domain = self._get_base_domain()
  196. records = self.search(base_domain)
  197. return getattr(records, rule.method)()
  198. class BaseException(models.AbstractModel):
  199. _inherit = "base.exception.method"
  200. _name = "base.exception"
  201. _order = "main_exception_id asc"
  202. _description = "Exception"
  203. main_exception_id = fields.Many2one(
  204. "exception.rule",
  205. compute="_compute_main_error",
  206. string="Main Exception",
  207. store=True,
  208. )
  209. exceptions_summary = fields.Html(
  210. "Exceptions Summary", compute="_compute_exceptions_summary"
  211. )
  212. exception_ids = fields.Many2many(
  213. "exception.rule", string="Exceptions", copy=False
  214. )
  215. ignore_exception = fields.Boolean("Ignore Exceptions", copy=False)
  216. @api.multi
  217. def action_ignore_exceptions(self):
  218. self.write({"ignore_exception": True})
  219. return True
  220. @api.depends("exception_ids", "ignore_exception")
  221. def _compute_main_error(self):
  222. for rec in self:
  223. if not rec.ignore_exception and rec.exception_ids:
  224. rec.main_exception_id = rec.exception_ids[0]
  225. else:
  226. rec.main_exception_id = False
  227. @api.depends("exception_ids", "ignore_exception")
  228. def _compute_exceptions_summary(self):
  229. for rec in self:
  230. if rec.exception_ids and not rec.ignore_exception:
  231. rec.exceptions_summary = "<ul>%s</ul>" % "".join(
  232. [
  233. "<li>%s: <i>%s</i></li>"
  234. % tuple(map(html.escape, (e.name, e.description)))
  235. for e in rec.exception_ids
  236. ]
  237. )
  238. @api.multi
  239. def _popup_exceptions(self):
  240. action = self._get_popup_action().read()[0]
  241. action.update(
  242. {
  243. "context": {
  244. "active_id": self.ids[0],
  245. "active_ids": self.ids,
  246. "active_model": self._name,
  247. }
  248. }
  249. )
  250. return action
  251. @api.model
  252. def _get_popup_action(self):
  253. return self.env.ref("base_exception.action_exception_rule_confirm")
  254. @api.multi
  255. def _check_exception(self):
  256. """
  257. This method must be used in a constraint that must be created in the
  258. object that inherits for base.exception.
  259. for sale :
  260. @api.constrains('ignore_exception',)
  261. def sale_check_exception(self):
  262. ...
  263. ...
  264. self._check_exception
  265. """
  266. exception_ids = self.detect_exceptions()
  267. if exception_ids:
  268. exceptions = self.env["exception.rule"].browse(exception_ids)
  269. raise ValidationError("\n".join(exceptions.mapped("name")))