zabbix-agent2/src/libs/zbxdbcache/dbconfig.c
/******************************************************************************
 *                                                                            *
 * Function: zbx_dc_get_trigger_timers                                        *
 *                                                                            *
 * Purpose: gets timers from trigger queue                                    *
 *                                                                            *
 * Parameters: timers     - [OUT] the timer triggers that must be processed   *
 *             now        - [IN] current time                                 *
 *             soft_limit - [IN] the number of timers to return unless timers *
 *                               of the same trigger are split over multiple  *
 *                               batches.                                     *
 *                                                                            *
 *             hard_limit - [IN] the maximum number of timers to return       *
 *                                                                            *
 * Comments: This function locks corresponding triggers in configuration      *
 *           cache.                                                           *
 *           If the returned timer has lock field set, then trigger is        *
 *           already being processed and should not be recalculated.          *
 *                                                                            *
 ******************************************************************************/
zbx_dc_get_trigger_timers

Purpose: gets timers from trigger queue

Parameters: timers     - [OUT] the timer triggers that must be processed
now        - [IN] current time
soft_limit - [IN] the number of timers to return unless timers
of the same trigger are split over multiple
batches.

hard_limit - [IN] the maximum number of timers to return

Comments: This function locks corresponding triggers in configuration
cache.
If the returned timer has lock field set, then trigger is
already being processed and should not be recalculated.
zbx_dc_get_trigger_timers Purpose : gets timers from trigger queue Parameters : timers - [ OUT ] the timer triggers that must be processed now - [ IN ] current time soft_limit - [ IN ] the number of timers to return unless timers of the same trigger are split over multiple batches . hard_limit - [ IN ] the maximum number of timers to return Comments : This function locks corresponding triggers in configuration cache . If the returned timer has lock field set then trigger is already being processed and should not be recalculated .
void	zbx_dc_get_trigger_timers(zbx_vector_ptr_t *timers, int now, int soft_limit, int hard_limit)
{
	zbx_trigger_timer_t	*first_timer = NULL, *timer;
	int			found = 0;
	zbx_binary_heap_elem_t	*elem;
	RDLOCK_CACHE;
	if (SUCCEED != zbx_binary_heap_empty(&config->trigger_queue))
	{
		elem = zbx_binary_heap_find_min(&config->trigger_queue);
		timer = (zbx_trigger_timer_t *)elem->data;
		if (timer->exec_ts.sec <= now)
			found = 1;
	}
	UNLOCK_CACHE;
	if (0 == found)
		return;
	WRLOCK_CACHE;
	while (SUCCEED != zbx_binary_heap_empty(&config->trigger_queue) && timers->values_num < hard_limit)
	{
		ZBX_DC_TRIGGER		*dc_trigger;
		elem = zbx_binary_heap_find_min(&config->trigger_queue);
		timer = (zbx_trigger_timer_t *)elem->data;
		if (timer->exec_ts.sec > now)
			break;
		if (NULL != first_timer && (timer->triggerid != first_timer->triggerid ||
				0 != zbx_timespec_compare(&timer->eval_ts, &first_timer->eval_ts)))
		{
			first_timer = NULL;
		}
		if (timers->values_num >= soft_limit && NULL == first_timer)
			break;
		zbx_binary_heap_remove_min(&config->trigger_queue);
		if (SUCCEED != trigger_timer_validate(timer, &dc_trigger))
		{
			dc_trigger_timer_free(timer);
			continue;
		}
		zbx_vector_ptr_append(timers, timer);
		if (0 == dc_trigger->locked || ZBX_TRIGGER_TIMER_FUNCTION_TREND != timer->type ||
				(NULL != first_timer && 1 == first_timer->lock))
		{
			timer->exec_ts.sec = 0;
		}
		if (0 == dc_trigger->locked)
			dc_trigger->locked = timer->lock = 1;
		if (NULL == first_timer)
			first_timer = timer;
	}
	UNLOCK_CACHE;
}
void zbx_dc_get_trigger_timers ( zbx_vector_ptr_t * timers , int now , int soft_limit , int hard_limit ) { zbx_trigger_timer_t * first_timer = NULL , * timer ; int found = 0 ; zbx_binary_heap_elem_t * elem ; RDLOCK_CACHE ; if ( SUCCEED != zbx_binary_heap_empty ( & config -> trigger_queue ) ) { elem = zbx_binary_heap_find_min ( & config -> trigger_queue ) ; timer = ( zbx_trigger_timer_t * ) elem -> data ; if ( timer -> exec_ts . sec <= now ) found = 1 ; } UNLOCK_CACHE ; if ( 0 == found ) return ; WRLOCK_CACHE ; while ( SUCCEED != zbx_binary_heap_empty ( & config -> trigger_queue ) && timers -> values_num < hard_limit ) { ZBX_DC_TRIGGER * dc_trigger ; elem = zbx_binary_heap_find_min ( & config -> trigger_queue ) ; timer = ( zbx_trigger_timer_t * ) elem -> data ; if ( timer -> exec_ts . sec > now ) break ; if ( NULL != first_timer && ( timer -> triggerid != first_timer -> triggerid || 0 != zbx_timespec_compare ( & timer -> eval_ts , & first_timer -> eval_ts ) ) ) { first_timer = NULL ; } if ( timers -> values_num >= soft_limit && NULL == first_timer ) break ; zbx_binary_heap_remove_min ( & config -> trigger_queue ) ; if ( SUCCEED != trigger_timer_validate ( timer , & dc_trigger ) ) { dc_trigger_timer_free ( timer ) ; continue ; } zbx_vector_ptr_append ( timers , timer ) ; if ( 0 == dc_trigger -> locked || ZBX_TRIGGER_TIMER_FUNCTION_TREND != timer -> type || ( NULL != first_timer && 1 == first_timer -> lock ) ) { timer -> exec_ts . sec = 0 ; } if ( 0 == dc_trigger -> locked ) dc_trigger -> locked = timer -> lock = 1 ; if ( NULL == first_timer ) first_timer = timer ; } UNLOCK_CACHE ; }
Function: zbx_dc_get_trigger_timers                                        
                                                                            
 Purpose: gets timers from trigger queue                                    
                                                                            
 Parameters: timers     - [OUT] the timer triggers that must be processed   
             now        - [IN] current time                                 
             soft_limit - [IN] the number of timers to return unless timers 
                               of the same trigger are split over multiple  
                               batches.
Function : zbx_dc_get_trigger_timers Purpose : gets timers from trigger queue Parameters : timers - [ OUT ] the timer triggers that must be processed now - [ IN ] current time soft_limit - [ IN ] the number of timers to return unless timers of the same trigger are split over multiple batches .
/* first_timer stores the first timer from a list of timers of the same trigger with the same */ /* evaluation timestamp. Reset first_timer if the conditions do not apply.                    */ /* use soft limit to avoid (mostly) splitting multiple functions of the same trigger */ /* over consequent batches                                                           */ /* Trigger expression must be calculated using function evaluation time. If a trigger is locked   */ /* keep rescheduling its timer until trigger is unlocked and can be calculated using the required */ /* evaluation time. However there are exceptions when evaluation time of a locked trigger is      */ /* acceptable to evaluate other functions:                                                        */ /*  1) time functions uses current time, so trigger evaluation time does not affect their results */ /*  2) trend function of the same trigger with the same evaluation timestamp is being             */ /*     evaluated by the same process                                                              */ /* resetting execution timer will cause a new execution time to be set */ /* when timer is put back into queue                                   */ /* remember if the timer locked trigger, so it would unlock during rescheduling */