root/sql/sprocs/stratcon.rollup_metric_numeric.sql

Revision 603ae821327d8821616dd53f5b84e484c52ba16d, 6.7 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

patch for rolling limits from Bjorn

  • Property mode set to 100644
Line 
1 CREATE OR REPLACE FUNCTION stratcon.rollup_metric_numeric(in_roll text) RETURNS int AS $$
2 DECLARE
3     v_rec           stratcon.metric_numeric_rollup_segment%rowtype;
4     v_segment       stratcon.metric_numeric_rollup%rowtype;
5     v_conf          RECORD;
6     v_sql           TEXT;
7     v_current_whence TIMESTAMPTZ;
8     v_min_whence    TIMESTAMPTZ;
9     v_whence        TIMESTAMPTZ;
10     v_taskid        INTEGER;
11     v_locked        BOOLEAN;
12     v_this_roll     TEXT;
13     v_stored_rollup INTEGER;
14     v_stored_rollup_tm TIMESTAMPTZ;
15     v_offset        INTEGER;
16     v_init          BOOLEAN := FALSE;
17     v_i             SMALLINT;
18     v_temprec       RECORD;
19     v_count         INTEGER;
20 BEGIN
21
22     v_i := 0;
23
24     -- Get rollup config based on given name, and fail if its wrong name.
25     SELECT * FROM metric_numeric_rollup_config WHERE rollup = in_roll INTO v_conf;
26     IF NOT FOUND THEN
27         raise exception 'Given rollup name is invalid! [%]', in_roll;
28     END IF;
29
30     -- Get task id - used for locking - based on given roll name
31     v_this_roll := 'rollup_metric_numeric_'||in_roll;
32     SELECT id FROM tasklock WHERE "name" = v_this_roll INTO v_taskid;
33     IF v_taskid IS NULL THEN
34         INSERT INTO tasklock (id, "name") VALUES (nextval('tasklock_id_seq'), v_this_roll) RETURNING id into v_taskid;
35     END IF;
36
37     -- Try to lock task_id - to make sure only one stratcon.rollup_metric_numeric_generic() runs at a time for this particular in_roll.
38     SELECT pg_try_advisory_lock(43191, v_taskid) INTO v_locked;
39     IF v_locked = false THEN
40         RAISE NOTICE 'rollup for metric numeric (%) already running', in_roll;
41         RETURN 0;
42     END IF;
43
44    v_current_whence := 'epoch'::timestamptz + '1 second'::INTERVAL * v_conf.seconds * floor(extract( epoch FROM now() ) / v_conf.seconds);
45
46     LOOP
47         IF v_i > 12 THEN
48             perform pg_advisory_unlock(43191, v_taskid);
49             RETURN 1;
50         END IF;
51
52         SELECT MIN(whence) FROM metric_numeric_rollup_queue
53          WHERE "interval" = in_roll and whence < v_current_whence INTO v_min_whence;
54         EXIT WHEN NOT FOUND OR v_min_whence IS NULL;
55
56         -- now() in following query is just a placeholder to get named field (use_whence) in temprec.
57         FOR v_temprec IN SELECT *, v_min_whence as use_whence
58                            FROM noit.metric_numeric_rollup_config WHERE dependent_on = in_roll
59         LOOP
60             -- Following formula gives equivalent of date_trunc(..) but working on basically any unit - like "10 minutes"
61             -- The unit has to be given in seconds, AND provided as v_temprec.seconds
62             v_temprec.use_whence := 'epoch'::timestamptz + '1 second'::INTERVAL * v_temprec.seconds * floor(extract( epoch FROM v_temprec.use_whence ) / v_temprec.seconds);
63
64 RAISE NOTICE '(%,%)',v_temprec.rollup, v_temprec.use_whence;
65             -- Poor mans UPSERT :)
66             INSERT INTO metric_numeric_rollup_queue ("interval", whence)
67                 SELECT v_temprec.rollup, v_temprec.use_whence
68                 WHERE NOT EXISTS (
69                     SELECT * FROM metric_numeric_rollup_queue WHERE ( "interval", whence ) = ( v_temprec.rollup, v_temprec.use_whence )
70                 );
71         END LOOP;
72
73         IF in_roll = '5m' THEN
74             v_sql := 'SELECT * FROM stratcon.window_robust_derive('||quote_literal(v_min_whence)||')';
75         ELSE
76             v_sql := 'SELECT sid, name, '||quote_literal(v_min_whence)||' as rollup_time, ';
77             v_sql := v_sql || ' SUM(coalesce(count_rows, 0)) as count_rows, ';
78             v_sql := v_sql || ' (SUM(avg_value*coalesce(count_rows,0))/SUM(coalesce(count_rows, 0))) as avg_value,';
79             v_sql := v_sql || ' (SUM(counter_dev*coalesce(count_rows,0))/SUM(coalesce(count_rows, 0))) as counter_dev ';
80             v_sql := v_sql || ' FROM stratcon.unroll_metric_numeric('||quote_literal(v_min_whence)||',';
81             v_sql := v_sql || quote_literal(v_min_whence + (v_conf.seconds - 1) * '1 second'::interval) || ',' || quote_literal(v_conf.dependent_on) ||')';
82             v_sql := v_sql || ' GROUP BY sid, name'; 
83         END IF;
84 RAISE NOTICE 'v_sql was (%)',v_sql;
85
86         FOR v_rec IN EXECUTE v_sql LOOP
87             v_stored_rollup := floor( extract('epoch' from v_rec.rollup_time) / v_conf.span ) * v_conf.span;
88             v_stored_rollup_tm := 'epoch'::timestamptz + v_stored_rollup * '1 second'::interval;
89             v_offset        := floor( ( extract('epoch' from v_rec.rollup_time) - v_stored_rollup) / v_conf.seconds );
90
91             v_sql := 'SELECT * FROM metric_numeric_rollup_'||in_roll||' WHERE rollup_time = '||quote_literal(v_stored_rollup_tm);
92             v_sql := v_sql ||' and sid='||v_rec.sid||' and name = '|| quote_literal(v_rec.name);
93
94             EXECUTE v_sql INTO v_segment;
95             GET DIAGNOSTICS v_count = ROW_COUNT;
96             IF v_count = 0 THEN
97                 v_segment := stratcon.init_metric_numeric_rollup( in_roll );
98                 v_init := true;
99                 RAISE NOTICE 'didnt find sid %, name %, rollup_time %, offset %', v_rec.sid, v_rec.name, v_stored_rollup_tm, v_offset;
100             END IF;
101
102             v_segment.sid                   := v_rec.sid;
103             v_segment.name                  := v_rec.name;
104             v_segment.count_rows[v_offset]  := v_rec.count_rows;
105             v_segment.avg_value[v_offset]   := v_rec.avg_value;
106             v_segment.counter_dev[v_offset] := v_rec.counter_dev;
107
108             IF v_init THEN
109                 v_sql := 'INSERT INTO metric_numeric_rollup_'||in_roll||' (sid,name,rollup_time,count_rows,avg_value,counter_dev)
110                     VALUES ($1,$2,$3,$4,$5,$6)';
111                 EXECUTE v_sql USING v_segment.sid, v_segment.name, v_stored_rollup_tm, v_segment.count_rows, v_segment.avg_value, v_segment.counter_dev;
112                 v_init := false;
113             ELSE
114                 v_sql := 'UPDATE metric_numeric_rollup_'||in_roll;
115                 v_sql := v_sql || ' SET (count_rows,avg_value,counter_dev) = ($1,$2,$3)';
116                 v_sql := v_sql || ' WHERE rollup_time = $4  AND sid = $5 AND name = $6';
117                 EXECUTE v_sql USING v_segment.count_rows, v_segment.avg_value, v_segment.counter_dev, v_stored_rollup_tm, v_segment.sid, v_segment.name;
118             END IF;
119
120         END LOOP;
121
122         v_i := v_i + 1;
123
124         -- Delete from whence log table
125         DELETE FROM metric_numeric_rollup_queue WHERE whence=v_min_whence AND "interval"=in_roll;
126
127         v_min_whence := NULL;
128     END LOOP;
129
130     perform pg_advisory_unlock(43191, v_taskid);
131
132     RETURN 0;
133
134 EXCEPTION
135     WHEN RAISE_EXCEPTION THEN
136        perform pg_advisory_unlock(43191, v_taskid);
137        RAISE EXCEPTION '%', SQLERRM;
138     WHEN OTHERS THEN
139        perform pg_advisory_unlock(43191, v_taskid);
140        RAISE NOTICE '%', SQLERRM;
141 END
142 $$ LANGUAGE plpgsql
143 SECURITY DEFINER
144 ;
Note: See TracBrowser for help on using the browser.