1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.db.purge;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.utils.System2;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
import org.sonar.db.component.ResourceDao;
import org.sonar.db.component.ResourceDto;
import static org.sonar.api.utils.DateUtils.dateToLong;
/**
* @since 2.14
*/
public class PurgeDao implements Dao {
private static final Logger LOG = LoggerFactory.getLogger(PurgeDao.class);
private final MyBatis mybatis;
private final ResourceDao resourceDao;
private final System2 system2;
public PurgeDao(MyBatis mybatis, ResourceDao resourceDao, System2 system2) {
this.mybatis = mybatis;
this.resourceDao = resourceDao;
this.system2 = system2;
}
public PurgeDao purge(PurgeConfiguration conf, PurgeListener listener, PurgeProfiler profiler) {
DbSession session = mybatis.openSession(true);
try {
purge(session, conf, listener, profiler);
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
return this;
}
public void purge(DbSession session, PurgeConfiguration conf, PurgeListener listener, PurgeProfiler profiler) {
PurgeMapper mapper = session.getMapper(PurgeMapper.class);
PurgeCommands commands = new PurgeCommands(session, mapper, profiler);
List<ResourceDto> projects = getProjects(conf.rootProjectIdUuid().getId(), session);
for (ResourceDto project : projects) {
LOG.debug("-> Clean " + project.getLongName() + " [id=" + project.getId() + "]");
deleteAbortedBuilds(project, commands);
purge(project, conf.scopesWithoutHistoricalData(), commands);
}
for (ResourceDto project : projects) {
disableOrphanResources(project, session, mapper, listener);
}
deleteOldClosedIssues(conf, mapper);
}
private void deleteOldClosedIssues(PurgeConfiguration conf, PurgeMapper mapper) {
Date toDate = conf.maxLiveDateOfClosedIssues();
mapper.deleteOldClosedIssueChanges(conf.rootProjectIdUuid().getUuid(), dateToLong(toDate));
mapper.deleteOldClosedIssues(conf.rootProjectIdUuid().getUuid(), dateToLong(toDate));
}
private void deleteAbortedBuilds(ResourceDto project, PurgeCommands commands) {
if (hasAbortedBuilds(project.getId(), commands)) {
LOG.debug("<- Delete aborted builds");
PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
.setIslast(false)
.setStatus(new String[] {"U"})
.setRootProjectId(project.getId());
commands.deleteSnapshots(query);
}
}
private boolean hasAbortedBuilds(Long projectId, PurgeCommands commands) {
PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
.setIslast(false)
.setStatus(new String[] {"U"})
.setResourceId(projectId);
return !commands.selectSnapshotIds(query).isEmpty();
}
private void purge(ResourceDto project, String[] scopesWithoutHistoricalData, PurgeCommands purgeCommands) {
List<Long> projectSnapshotIds = purgeCommands.selectSnapshotIds(
PurgeSnapshotQuery.create()
.setResourceId(project.getId())
.setIslast(false)
.setNotPurged(true)
);
for (final Long projectSnapshotId : projectSnapshotIds) {
LOG.debug("<- Clean snapshot " + projectSnapshotId);
if (!ArrayUtils.isEmpty(scopesWithoutHistoricalData)) {
PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
.setIslast(false)
.setScopes(scopesWithoutHistoricalData)
.setRootSnapshotId(projectSnapshotId);
purgeCommands.deleteSnapshots(query);
}
PurgeSnapshotQuery query = PurgeSnapshotQuery.create().setRootSnapshotId(projectSnapshotId).setNotPurged(true);
purgeCommands.purgeSnapshots(query);
// must be executed at the end for reentrance
purgeCommands.purgeSnapshots(PurgeSnapshotQuery.create().setId(projectSnapshotId).setNotPurged(true));
}
}
private void disableOrphanResources(final ResourceDto project, final SqlSession session, final PurgeMapper purgeMapper, final PurgeListener purgeListener) {
final List<IdUuidPair> componentIdUuids = new ArrayList<>();
session.select("org.sonar.db.purge.PurgeMapper.selectComponentIdUuidsToDisable", project.getId(), new ResultHandler() {
@Override
public void handleResult(ResultContext resultContext) {
IdUuidPair componentIdUuid = (IdUuidPair) resultContext.getResultObject();
if (componentIdUuid.getId() != null) {
componentIdUuids.add(componentIdUuid);
}
}
});
for (IdUuidPair componentIdUuid : componentIdUuids) {
disableResource(componentIdUuid, purgeMapper);
purgeListener.onComponentDisabling(componentIdUuid.getUuid());
}
session.commit();
}
public List<PurgeableSnapshotDto> selectPurgeableSnapshots(long resourceId) {
DbSession session = mybatis.openSession(true);
try {
return selectPurgeableSnapshots(resourceId, session);
} finally {
MyBatis.closeQuietly(session);
}
}
public List<PurgeableSnapshotDto> selectPurgeableSnapshots(long resourceId, DbSession session) {
List<PurgeableSnapshotDto> result = Lists.newArrayList();
result.addAll(mapper(session).selectPurgeableSnapshotsWithEvents(resourceId));
result.addAll(mapper(session).selectPurgeableSnapshotsWithoutEvents(resourceId));
// sort by date
Collections.sort(result);
return result;
}
public PurgeDao deleteResourceTree(IdUuidPair rootIdUuid, PurgeProfiler profiler) {
DbSession session = mybatis.openSession(true);
try {
return deleteResourceTree(session, rootIdUuid, profiler);
} finally {
MyBatis.closeQuietly(session);
}
}
public PurgeDao deleteResourceTree(DbSession session, IdUuidPair rootIdUuid, PurgeProfiler profiler) {
deleteProject(rootIdUuid, mapper(session), new PurgeCommands(session, profiler));
deleteFileSources(rootIdUuid.getUuid(), new PurgeCommands(session, profiler));
return this;
}
private static void deleteFileSources(String rootUuid, PurgeCommands commands) {
commands.deleteFileSources(rootUuid);
}
private static void deleteProject(IdUuidPair rootProjectId, PurgeMapper mapper, PurgeCommands commands) {
List<IdUuidPair> childrenIdUuid = mapper.selectProjectIdUuidsByRootId(rootProjectId.getId());
for (IdUuidPair childId : childrenIdUuid) {
deleteProject(childId, mapper, commands);
}
List<IdUuidPair> componentIdUuids = mapper.selectComponentIdUuidsByRootId(rootProjectId.getId());
commands.deleteResources(componentIdUuids);
}
private void disableResource(IdUuidPair componentIdUuid, PurgeMapper mapper) {
long componentId = componentIdUuid.getId();
mapper.deleteResourceIndex(Arrays.asList(componentId));
mapper.setSnapshotIsLastToFalse(componentId);
mapper.deleteFileSourcesByUuid(componentIdUuid.getUuid());
mapper.disableResource(componentId);
mapper.resolveResourceIssuesNotAlreadyResolved(componentIdUuid.getUuid(), system2.now());
}
public PurgeDao deleteSnapshots(PurgeSnapshotQuery query, PurgeProfiler profiler) {
final DbSession session = mybatis.openSession(true);
try {
return deleteSnapshots(query, session, profiler);
} finally {
MyBatis.closeQuietly(session);
}
}
public PurgeDao deleteSnapshots(PurgeSnapshotQuery query, DbSession session, PurgeProfiler profiler) {
new PurgeCommands(session, profiler).deleteSnapshots(query);
return this;
}
/**
* Load the whole tree of projects, including the project given in parameter.
*/
private List<ResourceDto> getProjects(long rootProjectId, SqlSession session) {
List<ResourceDto> projects = Lists.newArrayList();
projects.add(resourceDao.getResource(rootProjectId, session));
projects.addAll(resourceDao.getDescendantProjects(rootProjectId, session));
return projects;
}
public List<String> selectPurgeableFiles(DbSession dbSession, Long projectId) {
return mapper(dbSession).selectPurgeableFileUuids(projectId);
}
private PurgeMapper mapper(DbSession session) {
return session.getMapper(PurgeMapper.class);
}
}
|