blob: d60ff774e3af12ce71c253b712446dbfc18aaf2e [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001-- Copyright 2018 The Chromium Authors. All Rights Reserved.
2--
3-- Use of this source code is governed by a BSD-style
4-- license that can be found in the LICENSE file or at
5-- https://developers.google.com/open-source/licenses/bsd
6
7
8DROP PROCEDURE IF EXISTS BackfillIssueSnapshotsCcs;
9DROP PROCEDURE IF EXISTS BackfillIssueSnapshotsComponents;
10DROP PROCEDURE IF EXISTS BackfillIssueSnapshotsLabels;
11DROP PROCEDURE IF EXISTS BackfillIssueSnapshotsHotlists;
12DROP PROCEDURE IF EXISTS BackfillIssueSnapshotsChunk;
13DROP PROCEDURE IF EXISTS BackfillIssueSnapshotsManyChunks;
14
15delimiter //
16
17CREATE PROCEDURE BackfillIssueSnapshotsLabels(IN c_issue_id INT, IN c_issuesnapshot_id INT)
18BEGIN
19
20 DECLARE done INT DEFAULT FALSE;
21
22 DECLARE c_label_id INT;
23
24 DECLARE curs CURSOR FOR
25 SELECT label_id
26 FROM Issue2Label
27 WHERE issue_id = c_issue_id;
28
29 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
30 OPEN curs;
31
32 label_loop: LOOP
33 FETCH curs INTO c_label_id;
34 IF done THEN
35 LEAVE label_loop;
36 END IF;
37
38 INSERT INTO IssueSnapshot2Label
39 (issuesnapshot_id, label_id)
40 VALUES
41 (c_issuesnapshot_id, c_label_id);
42
43 END LOOP;
44
45 CLOSE curs;
46END;
47
48
49CREATE PROCEDURE BackfillIssueSnapshotsCcs(IN c_issue_id INT, IN c_issuesnapshot_id INT)
50BEGIN
51
52 DECLARE done INT DEFAULT FALSE;
53
54 DECLARE c_cc_id INT UNSIGNED;
55
56 DECLARE curs CURSOR FOR
57 SELECT cc_id
58 FROM Issue2Cc
59 WHERE issue_id = c_issue_id;
60
61 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
62 OPEN curs;
63
64 cc_loop: LOOP
65 FETCH curs INTO c_cc_id;
66 IF done THEN
67 LEAVE cc_loop;
68 END IF;
69
70 INSERT INTO IssueSnapshot2Cc
71 (issuesnapshot_id, cc_id)
72 VALUES
73 (c_issuesnapshot_id, c_cc_id);
74
75 END LOOP;
76
77 CLOSE curs;
78END;
79
80
81CREATE PROCEDURE BackfillIssueSnapshotsComponents(IN c_issue_id INT, IN c_issuesnapshot_id INT)
82BEGIN
83
84 DECLARE done INT DEFAULT FALSE;
85
86 DECLARE c_component_id INT;
87
88 DECLARE curs CURSOR FOR
89 SELECT component_id
90 FROM Issue2Component
91 WHERE issue_id = c_issue_id;
92
93 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
94 OPEN curs;
95
96 component_loop: LOOP
97 FETCH curs INTO c_component_id;
98 IF done THEN
99 LEAVE component_loop;
100 END IF;
101
102 INSERT INTO IssueSnapshot2Component
103 (issuesnapshot_id, component_id)
104 VALUES
105 (c_issuesnapshot_id, c_component_id);
106
107 END LOOP;
108
109 CLOSE curs;
110END;
111
112
113CREATE PROCEDURE BackfillIssueSnapshotsHotlists(IN c_issue_id INT, IN c_issuesnapshot_id INT)
114BEGIN
115
116 DECLARE done INT DEFAULT FALSE;
117
118 DECLARE c_hotlist_id INT;
119
120 DECLARE curs CURSOR FOR
121 SELECT hotlist_id
122 FROM Hotlist2Issue
123 WHERE issue_id = c_issue_id;
124
125 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
126 OPEN curs;
127
128 hotlist_loop: LOOP
129 FETCH curs INTO c_hotlist_id;
130 IF done THEN
131 LEAVE hotlist_loop;
132 END IF;
133
134 INSERT INTO IssueSnapshot2Hotlist
135 (issuesnapshot_id, hotlist_id)
136 VALUES
137 (c_issuesnapshot_id, c_hotlist_id);
138
139 END LOOP;
140
141 CLOSE curs;
142END;
143
144
145CREATE PROCEDURE BackfillIssueSnapshotsChunk(IN chunk_size INT UNSIGNED,
146 IN chunk_offset INT UNSIGNED)
147BEGIN
148
149 DECLARE done TINYINT DEFAULT FALSE;
150
151 DECLARE c_issue_id INT;
152 DECLARE c_issue_shard INT;
153 DECLARE c_issue_project_id INT;
154 DECLARE c_issue_local_id INT;
155 DECLARE c_issue_status_id INT;
156 DECLARE c_issue_opened INT;
157 DECLARE c_issue_closed INT;
158 DECLARE c_issue_is_open BOOLEAN;
159 DECLARE c_reporter_id INT UNSIGNED;
160 DECLARE c_owner_id INT UNSIGNED;
161 DECLARE c_issuesnapshot_id INT;
162 DECLARE total_counter INT UNSIGNED DEFAULT 0;
163 DECLARE write_counter INT UNSIGNED DEFAULT 0;
164
165 DECLARE curs CURSOR FOR
166 SELECT i.id, i.shard, i.project_id, i.local_id, i.status_id, i.opened,
167 -- If a snapshot for this Issue already exists, make the new snapshot's
168 -- period_end the period_start of the existing snapshot.
169 (SELECT IFNULL((
170 SELECT period_start
171 FROM IssueSnapshot
172 WHERE issue_id = i.id
173 ORDER BY period_start ASC
174 LIMIT 1
175 ), 4294967295)),
176 sd.means_open,
177 i.reporter_id, i.owner_id
178 FROM Issue i
179 JOIN StatusDef sd ON i.status_id = sd.id
180 WHERE i.id >= chunk_offset AND i.id < chunk_offset + chunk_size;
181
182 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
183
184 OPEN curs;
185
186 issue_loop: LOOP
187 FETCH curs INTO c_issue_id, c_issue_shard, c_issue_project_id,
188 c_issue_local_id, c_issue_status_id, c_issue_opened, c_issue_closed,
189 c_issue_is_open, c_reporter_id, c_owner_id;
190 IF done THEN
191 SELECT 'Final chunk status',
192 c_issue_id AS 'Processing Issue ID:',
193 total_counter AS 'Issues fetched',
194 write_counter AS 'Snapshots written';
195 LEAVE issue_loop;
196 END IF;
197
198 -- Indicate progress.
199 IF (SELECT c_issue_id % 100 = 0) THEN
200 SELECT 'Chunk status',
201 c_issue_id AS 'Processing Issue ID:',
202 total_counter AS 'Issues fetched',
203 write_counter AS 'Snapshots written';
204 END IF;
205
206 SET total_counter = total_counter + 1;
207
208 INSERT INTO IssueSnapshot
209 (issue_id, shard, project_id, local_id, status_id, period_start,
210 period_end, is_open, reporter_id, owner_id)
211 VALUES
212 (c_issue_id, c_issue_shard, c_issue_project_id,
213 c_issue_local_id, c_issue_status_id, c_issue_opened,
214 c_issue_closed, c_issue_is_open, c_reporter_id, c_owner_id);
215
216 SET write_counter = write_counter + 1;
217
218 SET c_issuesnapshot_id = LAST_INSERT_ID();
219 -- Add a tiny sleep here to reduce replication pressure on write.
220 SET @throwaway = (SELECT SLEEP(0.1));
221
222 -- Backfill labels.
223 CALL BackfillIssueSnapshotsLabels(c_issue_id, c_issuesnapshot_id);
224 CALL BackfillIssueSnapshotsCcs(c_issue_id, c_issuesnapshot_id);
225 CALL BackfillIssueSnapshotsComponents(c_issue_id, c_issuesnapshot_id);
226 CALL BackfillIssueSnapshotsHotlists(c_issue_id, c_issuesnapshot_id);
227
228 END LOOP;
229
230 CLOSE curs;
231END;
232
233
234CREATE PROCEDURE BackfillIssueSnapshotsManyChunks(
235 IN num_chunks SMALLINT UNSIGNED,
236 IN chunk_size SMALLINT UNSIGNED)
237BEGIN
238 DECLARE chunk_i INT DEFAULT 0;
239
240 -- Handle no results found ("cursor is not open")
241 DECLARE CONTINUE HANDLER FOR SQLSTATE '24000' BEGIN END;
242
243 WHILE chunk_i < num_chunks DO
244
245 SELECT CONCAT('Backfilling chunk ', chunk_i + 1, ' of ', num_chunks) AS '';
246 SELECT chunk_size, chunk_i, chunk_i * chunk_size AS 'chunk offset';
247
248 CALL BackfillIssueSnapshotsChunk(chunk_size, chunk_i * chunk_size);
249
250 SELECT SLEEP(1);
251
252 SET chunk_i = chunk_i + 1;
253 END WHILE;
254END;
255
256
257//
258
259
260delimiter ;