[ZBX-3920] trigger addDependencies() creates duplicates Created: 2011 Jul 05  Updated: 2017 May 30  Resolved: 2012 Oct 05

Status: Closed
Project: ZABBIX BUGS AND ISSUES
Component/s: API (A)
Affects Version/s: 1.8.4
Fix Version/s: 2.0.4rc1, 2.1.0

Type: Incident report Priority: Minor
Reporter: Sheldon Hearn Assignee: Unassigned
Resolution: Fixed Votes: 2
Labels: api
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MySQL RDBMS using InnoDB engine for tables


Issue Links:
Duplicate
is duplicated by ZBX-4144 API for dependency creation allow dup... Closed
is duplicated by ZBX-5890 Duplicate entries in trigger_depends ... Closed

 Description   

The API method trigger.addDependencies does not check whether dependencies exist prior to adding them, and there is no unique constraint across trigger_depends.triggerid_up and trigger_depends.triggerid_down to prevent the addition.

We had poorly written code that was unconditionally adding a dependency over and over, e.g.

  1. request:
    {
    "jsonrpc":"2.0",
    "method":"trigger.addDependencies",
    "params":[
    { "triggerid": "100100000065000", "dependsOnTriggerid": "100100000064000" }

    ,
    ],
    "auth":"038e1d7b1735c6a5436ee9eae095879e",
    "id":2
    }

The problem is, when we checked what this was doing, through the API, it looked like trigger.addDependencies was being very clever, and doing nothing if the dependency already existed:

  1. request:
    {
    "jsonrpc":"2.0",
    "method":"trigger.get",
    "params":{
    "filter": { "description": ["Sample trigger 1"], }

    ,
    "include_dependencies": "refer"
    },
    "auth":"6f38cddc44cfbb6c1bd186f9a220b5a0",
    "id":2
    }

No matter how many times we added the same dependency with trigger.addDependencies, trigger.get would show us the dependency only once.

However, each call to trigger.addDependencies added another row to the trigger_depends table. By the time we noticed what was happening, we had over half a million rows in the trigger_depends table, the increasing every half an hour when our script was run from cron.

We have subsequently modified our code to "include_dependencies" in the trigger.get request, and only trigger.addDependencies if the dependencies we want are missing. This reduces the likelihood of duplicate rows in trigger_depends table considerably.

Possible solutions that occur to me include:

1) Make trigger.addDependencies() fail when it receives a request to add an already existing dependency.
2) Throw a unique constraint across trigger_depends.triggerid_up and trigger_depends.triggerid_down and just let the mysql exception explode upward.
3) Make trigger.addDependencies() silently ignore a request to add an already existing dependency. Perhaps log a warning.



 Comments   
Comment by Alexey Fukalov [ 2012 Sep 24 ]

(1)
Not sure but it looks like than instead of select in loop it can be done with one select with grouping and count. If it's not or too slow, it should be moved to checkDependencies method as there already exists same loop with same SELECT.

oleg.egorov RESOLVED

Vedmak some code corrections needed as we discussed.

oleg.egorov RESOLVED

Vedmak CLSOED

Comment by Alexey Fukalov [ 2012 Sep 24 ]

(2)
Code style is not correct in some places:

if($DplTriggers){
$UnqTriggers  = array_unique($trigger['dependencies']);

oleg.egorov
REW 30386
RESOLVED

Vedmak CLSOED

Comment by Alexey Fukalov [ 2012 Sep 28 ]

(3)
When try to add duplicate dependency error is thrown:
SQL statement execution has failed \"INSERT INTO trigger_depends (triggerid_down,triggerid_up,triggerdepid) VALUES ('13645','13664','41')

oleg.egorov RESOLVED

Vedmak seems there is still needed separate select as discussed.

oleg.egorov RESOLVED

Comment by Alexey Fukalov [ 2012 Oct 04 ]

Vedmak as discussed, select logic and error message should be changed.

oleg.egorov RESOLVED IN svn://svn.zabbix.com/branches/dev/ZBX-3920 r30669

Vedmak seems it doesn't catch db duplicates, show message about insert sql failure.

Vedmak Please review my changes in r30671 ,can be merged if everything is ok.

oleg.egorov CLOSED

Comment by Oleg Egorov (Inactive) [ 2012 Oct 05 ]

Fixed in pre-2.0.4 r30672 and pre-2.1.0 (trunk) r30673.

Generated at Fri Apr 26 10:36:30 EEST 2024 using Jira 9.12.4#9120004-sha1:625303b708afdb767e17cb2838290c41888e9ff0.