Skip to content

Commit cea607d

Browse files
committed
Events listing and deletion optimizations
1 parent f5b4858 commit cea607d

File tree

13 files changed

+189
-48
lines changed

13 files changed

+189
-48
lines changed

api/src/main/java/org/apache/cloudstack/api/command/user/event/DeleteEventsCmd.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.apache.cloudstack.context.CallContext;
3232

3333
import com.cloud.event.Event;
34-
import com.cloud.exception.InvalidParameterValueException;
3534
import com.cloud.user.Account;
3635

3736
@APICommand(name = "deleteEvents", description = "Delete one or more events.", responseObject = SuccessResponse.class, entityType = {Event.class},
@@ -97,17 +96,12 @@ public long getEntityOwnerId() {
9796

9897
@Override
9998
public void execute() {
100-
if (ids == null && type == null && endDate == null) {
101-
throw new InvalidParameterValueException("either ids, type or enddate must be specified");
102-
} else if (startDate != null && endDate == null) {
103-
throw new InvalidParameterValueException("enddate must be specified with startdate parameter");
104-
}
10599
boolean result = _mgr.deleteEvents(this);
106100
if (result) {
107101
SuccessResponse response = new SuccessResponse(getCommandName());
108102
setResponseObject(response);
109103
} else {
110-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Unable to delete Events, one or more parameters has invalid values");
104+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Unable to delete any events. One or more parameters have invalid values.");
111105
}
112106
}
113107
}

api/src/main/java/org/apache/cloudstack/api/command/user/event/ListEventsCmd.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ public boolean getArchived() {
127127

128128
@Override
129129
public void execute() {
130-
131130
ListResponse<EventResponse> response = _queryService.searchForEvents(this);
132131
response.setResponseName(getCommandName());
133132
setResponseObject(response);

engine/schema/src/main/java/com/cloud/domain/dao/DomainDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,7 @@ public interface DomainDao extends GenericDao<DomainVO, Long> {
4242

4343
List<Long> getDomainChildrenIds(String path);
4444

45+
List<Long> getDomainAndChildrenIds(long domainId);
46+
4547
boolean domainIdListContainsAccessibleDomain(String domainIdList, Account caller, Long domainId);
4648
}

engine/schema/src/main/java/com/cloud/domain/dao/DomainDaoImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.sql.PreparedStatement;
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
22+
import java.util.ArrayList;
2223
import java.util.HashSet;
2324
import java.util.List;
2425
import java.util.Set;
@@ -238,6 +239,15 @@ public List<Long> getDomainChildrenIds(String path) {
238239
return customSearch(sc, null);
239240
}
240241

242+
@Override
243+
public List<Long> getDomainAndChildrenIds(long domainId) {
244+
DomainVO domain = findById(domainId);
245+
if (domain != null) {
246+
return getDomainChildrenIds(domain.getPath());
247+
}
248+
return new ArrayList<>();
249+
}
250+
241251
@Override
242252
public boolean isChildDomain(Long parentId, Long childId) {
243253
if ((parentId == null) || (childId == null)) {

engine/schema/src/main/java/com/cloud/event/dao/EventDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
public interface EventDao extends GenericDao<EventVO, Long> {
2828
public List<EventVO> searchAllEvents(SearchCriteria<EventVO> sc, Filter filter);
2929

30-
public List<EventVO> listOlderEvents(Date oldTime);
31-
3230
EventVO findCompletedEvent(long startId);
3331

3432
public List<EventVO> listToArchiveOrDeleteEvents(List<Long> ids, String type, Date startDate, Date endDate, List<Long> accountIds);
3533

3634
public void archiveEvents(List<EventVO> events);
3735

36+
long purgeAll(List<Long> ids, Date startDate, Date endDate, Date limitDate, String type, Long accountId, List<Long> domainIds,
37+
long limitPerQuery);
3838
}

engine/schema/src/main/java/com/cloud/event/dao/EventDaoImpl.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121

2222

23+
import org.apache.commons.collections4.CollectionUtils;
2324
import org.springframework.stereotype.Component;
2425

2526
import com.cloud.event.Event.State;
@@ -46,9 +47,12 @@ public EventDaoImpl() {
4647
ToArchiveOrDeleteEventSearch = createSearchBuilder();
4748
ToArchiveOrDeleteEventSearch.and("id", ToArchiveOrDeleteEventSearch.entity().getId(), Op.IN);
4849
ToArchiveOrDeleteEventSearch.and("type", ToArchiveOrDeleteEventSearch.entity().getType(), Op.EQ);
50+
ToArchiveOrDeleteEventSearch.and("accountId", ToArchiveOrDeleteEventSearch.entity().getAccountId(), Op.EQ);
4951
ToArchiveOrDeleteEventSearch.and("accountIds", ToArchiveOrDeleteEventSearch.entity().getAccountId(), Op.IN);
52+
ToArchiveOrDeleteEventSearch.and("domainIds", ToArchiveOrDeleteEventSearch.entity().getDomainId(), Op.IN);
5053
ToArchiveOrDeleteEventSearch.and("createdDateB", ToArchiveOrDeleteEventSearch.entity().getCreateDate(), Op.BETWEEN);
5154
ToArchiveOrDeleteEventSearch.and("createdDateL", ToArchiveOrDeleteEventSearch.entity().getCreateDate(), Op.LTEQ);
55+
ToArchiveOrDeleteEventSearch.and("createdDateLT", ToArchiveOrDeleteEventSearch.entity().getCreateDate(), Op.LT);
5256
ToArchiveOrDeleteEventSearch.and("archived", ToArchiveOrDeleteEventSearch.entity().getArchived(), Op.EQ);
5357
ToArchiveOrDeleteEventSearch.done();
5458
}
@@ -58,16 +62,6 @@ public List<EventVO> searchAllEvents(SearchCriteria<EventVO> sc, Filter filter)
5862
return listIncludingRemovedBy(sc, filter);
5963
}
6064

61-
@Override
62-
public List<EventVO> listOlderEvents(Date oldTime) {
63-
if (oldTime == null)
64-
return null;
65-
SearchCriteria<EventVO> sc = createSearchCriteria();
66-
sc.addAnd("createDate", SearchCriteria.Op.LT, oldTime);
67-
sc.addAnd("archived", SearchCriteria.Op.EQ, false);
68-
return listIncludingRemovedBy(sc, null);
69-
}
70-
7165
@Override
7266
public EventVO findCompletedEvent(long startId) {
7367
SearchCriteria<EventVO> sc = CompletedEventSearch.create();
@@ -112,4 +106,28 @@ public void archiveEvents(List<EventVO> events) {
112106
txn.close();
113107
}
114108
}
109+
110+
@Override
111+
public long purgeAll(List<Long> ids, Date startDate, Date endDate, Date limitDate, String type, Long accountId,
112+
List<Long> domainIds, long limitPerQuery) {
113+
SearchCriteria<EventVO> sc = ToArchiveOrDeleteEventSearch.create();
114+
115+
if (CollectionUtils.isNotEmpty(ids)) {
116+
sc.setParameters("id", ids.toArray(new Object[0]));
117+
}
118+
if (startDate != null && endDate != null) {
119+
sc.setParameters("createdDateB", startDate, endDate);
120+
} else if (endDate != null) {
121+
sc.setParameters("createdDateL", endDate);
122+
}
123+
sc.setParametersIfNotNull("createdDateLT", limitDate);
124+
sc.setParametersIfNotNull("type", type);
125+
sc.setParametersIfNotNull("accountId", accountId);
126+
if (CollectionUtils.isNotEmpty(domainIds)) {
127+
sc.setParameters("domainIds", domainIds.toArray(new Object[0]));
128+
}
129+
sc.setParameters("archived", false);
130+
131+
return batchExpunge(sc, limitPerQuery);
132+
}
115133
}

engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public class DbUpgradeUtils {
2525

2626
public static void addIndexIfNeeded(Connection conn, String tableName, String... columnNames) {
2727
String indexName = dao.generateIndexName(tableName, columnNames);
28+
addIndexWithNameIfNeeded(conn, tableName, indexName, columnNames);
29+
}
2830

31+
public static void addIndexWithNameIfNeeded(Connection conn, String tableName, String indexName, String... columnNames) {
2932
if (!dao.indexExists(conn, tableName, indexName)) {
3033
dao.createIndex(conn, tableName, indexName, columnNames);
3134
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.upgrade.dao;
18+
19+
import com.cloud.utils.exception.CloudRuntimeException;
20+
21+
import java.io.InputStream;
22+
import java.sql.Connection;
23+
24+
public class Upgrade42010to42020 implements DbUpgrade {
25+
26+
@Override
27+
public String[] getUpgradableVersionRange() {
28+
return new String[]{"4.20.1.0", "4.20.2.0"};
29+
}
30+
31+
@Override
32+
public String getUpgradedVersion() {
33+
return "4.20.2.0";
34+
}
35+
36+
@Override
37+
public boolean supportsRollingUpgrade() {
38+
return false;
39+
}
40+
41+
@Override
42+
public InputStream[] getPrepareScripts() {
43+
final String scriptFile = "META-INF/db/schema-42010to42020.sql";
44+
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
45+
if (script == null) {
46+
throw new CloudRuntimeException("Unable to find " + scriptFile);
47+
}
48+
49+
return new InputStream[]{script};
50+
}
51+
52+
@Override
53+
public void performDataMigration(Connection conn) {
54+
addIndexes(conn);
55+
}
56+
57+
private void addIndexes(Connection conn) {
58+
DbUpgradeUtils.addIndexWithNameIfNeeded(conn, "event", "i_event__multiple_columns_for_generic_search",
59+
"account_id", "domain_id", "archived", "display", "resource_type", "resource_id", "start_id", "type", "level",
60+
"created", "id");
61+
}
62+
63+
@Override
64+
public InputStream[] getCleanupScripts() {
65+
final String scriptFile = "META-INF/db/schema-42010to42020-cleanup.sql";
66+
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
67+
if (script == null) {
68+
throw new CloudRuntimeException("Unable to find " + scriptFile);
69+
}
70+
71+
return new InputStream[]{script};
72+
}
73+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
--;
19+
-- Schema upgrade cleanup from 4.20.1.0 to 4.20.2.0
20+
--;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
--;
19+
-- Schema upgrade from 4.20.1.0 to 4.20.2.0
20+
--;

0 commit comments

Comments
 (0)