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