This is an automated email from the ASF dual-hosted git repository.
RosiKyu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack.git
The following commit(s) were added to refs/heads/main by this push:
new 5232d6d258c Events listing, deletion and archival optimizations
(#11443)
5232d6d258c is described below
commit 5232d6d258c4fc80fcdd769e9c1ee4bc6bbfbbb7
Author: Fabricio Duarte <[email protected]>
AuthorDate: Tue Nov 17 14:39:52 2026 -0300
Events listing, deletion and archival optimizations (#11443)
* Events listing, deletion and archival optimizations
* do not drop index that does not exist
* address reviews
* Treat event archive/delete with no matching rows as success
* fix a few regressions and bugs
---
.../api/command/user/event/ArchiveEventsCmd.java | 10 +-
.../api/command/user/event/DeleteEventsCmd.java | 8 +-
.../api/command/user/event/ListEventsCmd.java | 1 -
.../main/java/com/cloud/event/dao/EventDao.java | 14 +-
.../java/com/cloud/event/dao/EventDaoImpl.java | 110 ++++++-------
.../cloud/upgrade/dao/DatabaseAccessObject.java | 14 ++
.../java/com/cloud/upgrade/dao/DbUpgradeUtils.java | 11 ++
.../com/cloud/upgrade/dao/Upgrade42300to2400.java | 31 ++++
.../java/com/cloud/api/query/QueryManagerImpl.java | 2 +-
.../configuration/ConfigurationManagerImpl.java | 2 +-
.../com/cloud/server/ManagementServerImpl.java | 103 +++++++------
.../com/cloud/event/EventControlsUnitTest.java | 88 -----------
.../com/cloud/server/ManagementServerImplTest.java | 171 ++++++++++++++++++++-
13 files changed, 337 insertions(+), 228 deletions(-)
diff --git
a/api/src/main/java/org/apache/cloudstack/api/command/user/event/ArchiveEventsCmd.java
b/api/src/main/java/org/apache/cloudstack/api/command/user/event/ArchiveEventsCmd.java
index e449f4b002b..aaf3e350b96 100644
---
a/api/src/main/java/org/apache/cloudstack/api/command/user/event/ArchiveEventsCmd.java
+++
b/api/src/main/java/org/apache/cloudstack/api/command/user/event/ArchiveEventsCmd.java
@@ -31,7 +31,6 @@ import org.apache.cloudstack.api.response.SuccessResponse;
import org.apache.cloudstack.context.CallContext;
import com.cloud.event.Event;
-import com.cloud.exception.InvalidParameterValueException;
import com.cloud.user.Account;
@APICommand(name = "archiveEvents", description = "Archive one or more
events.", responseObject = SuccessResponse.class, entityType = {Event.class},
@@ -97,17 +96,12 @@ public class ArchiveEventsCmd extends BaseCmd {
@Override
public void execute() {
- if (ids == null && type == null && endDate == null) {
- throw new InvalidParameterValueException("either ids, type or
enddate must be specified");
- } else if (startDate != null && endDate == null) {
- throw new InvalidParameterValueException("enddate must be
specified with startdate parameter");
- }
boolean result = _mgr.archiveEvents(this);
if (result) {
SuccessResponse response = new SuccessResponse(getCommandName());
setResponseObject(response);
- } else {
- throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Unable
to archive Events, one or more parameters has invalid values");
+ return;
}
+ throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Unable to
archive Events. One or more parameters have invalid values.");
}
}
diff --git
a/api/src/main/java/org/apache/cloudstack/api/command/user/event/DeleteEventsCmd.java
b/api/src/main/java/org/apache/cloudstack/api/command/user/event/DeleteEventsCmd.java
index db183a08e5a..d2b3b392aec 100644
---
a/api/src/main/java/org/apache/cloudstack/api/command/user/event/DeleteEventsCmd.java
+++
b/api/src/main/java/org/apache/cloudstack/api/command/user/event/DeleteEventsCmd.java
@@ -31,7 +31,6 @@ import org.apache.cloudstack.api.response.SuccessResponse;
import org.apache.cloudstack.context.CallContext;
import com.cloud.event.Event;
-import com.cloud.exception.InvalidParameterValueException;
import com.cloud.user.Account;
@APICommand(name = "deleteEvents", description = "Delete one or more events.",
responseObject = SuccessResponse.class, entityType = {Event.class},
@@ -97,17 +96,12 @@ public class DeleteEventsCmd extends BaseCmd {
@Override
public void execute() {
- if (ids == null && type == null && endDate == null) {
- throw new InvalidParameterValueException("either ids, type or
enddate must be specified");
- } else if (startDate != null && endDate == null) {
- throw new InvalidParameterValueException("enddate must be
specified with startdate parameter");
- }
boolean result = _mgr.deleteEvents(this);
if (result) {
SuccessResponse response = new SuccessResponse(getCommandName());
setResponseObject(response);
} else {
- throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Unable
to delete Events, one or more parameters has invalid values");
+ throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Unable
to delete any events. One or more parameters have invalid values.");
}
}
}
diff --git
a/api/src/main/java/org/apache/cloudstack/api/command/user/event/ListEventsCmd.java
b/api/src/main/java/org/apache/cloudstack/api/command/user/event/ListEventsCmd.java
index b86d1f8b956..a7ef3560dac 100644
---
a/api/src/main/java/org/apache/cloudstack/api/command/user/event/ListEventsCmd.java
+++
b/api/src/main/java/org/apache/cloudstack/api/command/user/event/ListEventsCmd.java
@@ -134,7 +134,6 @@ public class ListEventsCmd extends
BaseListProjectAndAccountResourcesCmd {
@Override
public void execute() {
-
ListResponse<EventResponse> response =
_queryService.searchForEvents(this);
response.setResponseName(getCommandName());
setResponseObject(response);
diff --git a/engine/schema/src/main/java/com/cloud/event/dao/EventDao.java
b/engine/schema/src/main/java/com/cloud/event/dao/EventDao.java
index 03716237cdb..1b6f2cc27a1 100644
--- a/engine/schema/src/main/java/com/cloud/event/dao/EventDao.java
+++ b/engine/schema/src/main/java/com/cloud/event/dao/EventDao.java
@@ -21,17 +21,9 @@ import java.util.List;
import com.cloud.event.Event;
import com.cloud.event.EventVO;
-import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDao;
-import com.cloud.utils.db.SearchCriteria;
public interface EventDao extends GenericDao<EventVO, Long> {
- public List<EventVO> searchAllEvents(SearchCriteria<EventVO> sc, Filter
filter);
-
- public List<EventVO> listOlderEvents(Date oldTime);
-
- EventVO findCompletedEvent(long startId);
-
/**
* Finds the last non-archived start event matching the specified criteria.
* Events are ordered by ID in descending order, returning the most recent
one.
@@ -44,8 +36,10 @@ public interface EventDao extends GenericDao<EventVO, Long> {
*/
EventVO findLastEvent(String type, Event.State state, Long resourceId,
String resourceType);
- public List<EventVO> listToArchiveOrDeleteEvents(List<Long> ids, String
type, Date startDate, Date endDate, List<Long> accountIds);
+ long archiveEvents(List<Long> ids, String type, Date startDate, Date
endDate, Long accountId, List<Long> domainIds,
+ long limitPerQuery);
- public void archiveEvents(List<EventVO> events);
+ long deleteEvents(List<Long> ids, Date startDate, Date endDate, Date
limitDate, String type, Long accountId, List<Long> domainIds,
+ long limitPerQuery);
}
diff --git a/engine/schema/src/main/java/com/cloud/event/dao/EventDaoImpl.java
b/engine/schema/src/main/java/com/cloud/event/dao/EventDaoImpl.java
index 94a7d6a1679..21b95e5d57a 100644
--- a/engine/schema/src/main/java/com/cloud/event/dao/EventDaoImpl.java
+++ b/engine/schema/src/main/java/com/cloud/event/dao/EventDaoImpl.java
@@ -18,15 +18,13 @@ package com.cloud.event.dao;
import java.util.Date;
import java.util.List;
-import java.util.stream.Collectors;
-import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import com.cloud.event.Event.State;
import com.cloud.event.EventVO;
-import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
@@ -35,31 +33,23 @@ import com.cloud.utils.db.UpdateBuilder;
@Component
public class EventDaoImpl extends GenericDaoBase<EventVO, Long> implements
EventDao {
- protected final SearchBuilder<EventVO> CompletedEventSearch;
+
protected final SearchBuilder<EventVO> ToArchiveOrDeleteEventSearch;
- protected final SearchBuilder<EventVO> ArchiveByIdsSearch;
protected final SearchBuilder<EventVO> LastStartEventSearch;
public EventDaoImpl() {
- CompletedEventSearch = createSearchBuilder();
- CompletedEventSearch.and("state",
CompletedEventSearch.entity().getState(), SearchCriteria.Op.EQ);
- CompletedEventSearch.and("startId",
CompletedEventSearch.entity().getStartId(), SearchCriteria.Op.EQ);
- CompletedEventSearch.and("archived",
CompletedEventSearch.entity().getArchived(), Op.EQ);
- CompletedEventSearch.done();
-
ToArchiveOrDeleteEventSearch = createSearchBuilder();
ToArchiveOrDeleteEventSearch.and("id",
ToArchiveOrDeleteEventSearch.entity().getId(), Op.IN);
ToArchiveOrDeleteEventSearch.and("type",
ToArchiveOrDeleteEventSearch.entity().getType(), Op.EQ);
- ToArchiveOrDeleteEventSearch.and("accountIds",
ToArchiveOrDeleteEventSearch.entity().getAccountId(), Op.IN);
+ ToArchiveOrDeleteEventSearch.and("accountId",
ToArchiveOrDeleteEventSearch.entity().getAccountId(), Op.EQ);
+ ToArchiveOrDeleteEventSearch.and("domainIds",
ToArchiveOrDeleteEventSearch.entity().getDomainId(), Op.IN);
ToArchiveOrDeleteEventSearch.and("createdDateB",
ToArchiveOrDeleteEventSearch.entity().getCreateDate(), Op.BETWEEN);
+ ToArchiveOrDeleteEventSearch.and("createdDateG",
ToArchiveOrDeleteEventSearch.entity().getCreateDate(), Op.GTEQ);
ToArchiveOrDeleteEventSearch.and("createdDateL",
ToArchiveOrDeleteEventSearch.entity().getCreateDate(), Op.LTEQ);
+ ToArchiveOrDeleteEventSearch.and("createdDateLT",
ToArchiveOrDeleteEventSearch.entity().getCreateDate(), Op.LT);
ToArchiveOrDeleteEventSearch.and("archived",
ToArchiveOrDeleteEventSearch.entity().getArchived(), Op.EQ);
ToArchiveOrDeleteEventSearch.done();
- ArchiveByIdsSearch = createSearchBuilder();
- ArchiveByIdsSearch.and("id", ArchiveByIdsSearch.entity().getId(),
Op.IN);
- ArchiveByIdsSearch.done();
-
LastStartEventSearch = createSearchBuilder();
LastStartEventSearch.and("type",
LastStartEventSearch.entity().getType(), Op.EQ);
LastStartEventSearch.and("state",
LastStartEventSearch.entity().getState(), Op.EQ);
@@ -69,30 +59,6 @@ public class EventDaoImpl extends GenericDaoBase<EventVO,
Long> implements Event
LastStartEventSearch.done();
}
- @Override
- public List<EventVO> searchAllEvents(SearchCriteria<EventVO> sc, Filter
filter) {
- return listIncludingRemovedBy(sc, filter);
- }
-
- @Override
- public List<EventVO> listOlderEvents(Date oldTime) {
- if (oldTime == null)
- return null;
- SearchCriteria<EventVO> sc = createSearchCriteria();
- sc.addAnd("createDate", SearchCriteria.Op.LT, oldTime);
- sc.addAnd("archived", SearchCriteria.Op.EQ, false);
- return listIncludingRemovedBy(sc, null);
- }
-
- @Override
- public EventVO findCompletedEvent(long startId) {
- SearchCriteria<EventVO> sc = CompletedEventSearch.create();
- sc.setParameters("state", State.Completed);
- sc.setParameters("startId", startId);
- sc.setParameters("archived", false);
- return findOneIncludingRemovedBy(sc);
- }
-
@Override
public EventVO findLastEvent(String type, State state, Long resourceId,
String resourceType) {
SearchCriteria<EventVO> sc = LastStartEventSearch.create();
@@ -104,39 +70,63 @@ public class EventDaoImpl extends GenericDaoBase<EventVO,
Long> implements Event
return findLastOneBy(sc);
}
- @Override
- public List<EventVO> listToArchiveOrDeleteEvents(List<Long> ids, String
type, Date startDate, Date endDate, List<Long> accountIds) {
+ private SearchCriteria<EventVO> createEventSearchCriteria(List<Long> ids,
String type, Date startDate, Date endDate,
+ Date limitDate,
Long accountId, List<Long> domainIds) {
SearchCriteria<EventVO> sc = ToArchiveOrDeleteEventSearch.create();
- if (ids != null) {
- sc.setParameters("id", ids.toArray(new Object[ids.size()]));
+
+ if (CollectionUtils.isNotEmpty(ids)) {
+ sc.setParameters("id", ids.toArray(new Object[0]));
}
- if (type != null) {
- sc.setParameters("type", type);
+ if (CollectionUtils.isNotEmpty(domainIds)) {
+ sc.setParameters("domainIds", domainIds.toArray(new Object[0]));
}
if (startDate != null && endDate != null) {
sc.setParameters("createdDateB", startDate, endDate);
} else if (endDate != null) {
sc.setParameters("createdDateL", endDate);
+ } else if (startDate != null) {
+ sc.setParameters("createdDateG", startDate);
}
- if (accountIds != null && !accountIds.isEmpty()) {
- sc.setParameters("accountIds", accountIds.toArray(new
Object[accountIds.size()]));
- }
+ sc.setParametersIfNotNull("accountId", accountId);
+ sc.setParametersIfNotNull("createdDateLT", limitDate);
+ sc.setParametersIfNotNull("type", type);
sc.setParameters("archived", false);
- return search(sc, null);
+
+ return sc;
+ }
+
+ private boolean allIdsMatch(List<Long> ids, SearchCriteria<EventVO> sc) {
+ return CollectionUtils.isEmpty(ids) || getCountIncludingRemoved(sc) ==
ids.size();
}
@Override
- public void archiveEvents(List<EventVO> events) {
- if (CollectionUtils.isEmpty(events)) {
- return;
+ public long archiveEvents(List<Long> ids, String type, Date startDate,
Date endDate, Long accountId, List<Long> domainIds,
+ long limitPerQuery) {
+ SearchCriteria<EventVO> sc = createEventSearchCriteria(ids, type,
startDate, endDate, null, accountId, domainIds);
+ if (!allIdsMatch(ids, sc)) {
+ return 0L;
}
- List<Long> ids =
events.stream().map(EventVO::getId).collect(Collectors.toList());
- SearchCriteria<EventVO> sc = ArchiveByIdsSearch.create();
- sc.setParameters("id", ids.toArray(new Object[ids.size()]));
- EventVO eventForUpdate = createForUpdate();
- eventForUpdate.setArchived(true);
- UpdateBuilder ub = getUpdateBuilder(eventForUpdate);
- update(ub, sc, null);
+ long totalArchived = 0L;
+ int archived;
+ do {
+ EventVO eventForUpdate = createForUpdate();
+ eventForUpdate.setArchived(true);
+ UpdateBuilder ub = getUpdateBuilder(eventForUpdate);
+ archived = update(ub, sc, limitPerQuery > 0 ? (int)
Math.min(limitPerQuery, Integer.MAX_VALUE) : null);
+ totalArchived += archived;
+ } while (limitPerQuery > 0 && archived >= limitPerQuery);
+
+ return totalArchived;
+ }
+
+ @Override
+ public long deleteEvents(List<Long> ids, Date startDate, Date endDate,
Date limitDate, String type, Long accountId,
+ List<Long> domainIds, long limitPerQuery) {
+ SearchCriteria<EventVO> sc = createEventSearchCriteria(ids, type,
startDate, endDate, limitDate, accountId, domainIds);
+ if (!allIdsMatch(ids, sc)) {
+ return 0L;
+ }
+ return batchExpunge(sc, limitPerQuery);
}
}
diff --git
a/engine/schema/src/main/java/com/cloud/upgrade/dao/DatabaseAccessObject.java
b/engine/schema/src/main/java/com/cloud/upgrade/dao/DatabaseAccessObject.java
index f5775a56097..e91ee822088 100644
---
a/engine/schema/src/main/java/com/cloud/upgrade/dao/DatabaseAccessObject.java
+++
b/engine/schema/src/main/java/com/cloud/upgrade/dao/DatabaseAccessObject.java
@@ -144,6 +144,20 @@ public class DatabaseAccessObject {
}
}
+ public void dropIndexIfExists(Connection conn, String tableName, String
indexName) {
+ if (!indexExists(conn, tableName, indexName)) {
+ return;
+ }
+ String stmt = String.format("DROP INDEX %s ON %s", indexName,
tableName);
+ logger.debug("Statement: {}", stmt);
+ try (PreparedStatement pstmt = conn.prepareStatement(stmt)) {
+ pstmt.execute();
+ logger.debug("Dropped index {}", indexName);
+ } catch (SQLException e) {
+ logger.warn("Unable to drop index {}", indexName, e);
+ }
+ }
+
public void renameIndex(Connection conn, String tableName, String oldName,
String newName) {
String stmt = String.format("ALTER TABLE %s RENAME INDEX %s TO %s",
tableName, oldName, newName);
logger.debug("Statement: {}", stmt);
diff --git
a/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeUtils.java
b/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeUtils.java
index be073fcce77..0e9354e0c9d 100644
--- a/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeUtils.java
+++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeUtils.java
@@ -25,12 +25,23 @@ public class DbUpgradeUtils {
public static void addIndexIfNeeded(Connection conn, String tableName,
String... columnNames) {
String indexName = dao.generateIndexName(tableName, columnNames);
+ addIndexWithNameIfNeeded(conn, tableName, indexName, columnNames);
+ }
+ public static void addIndexWithNameIfNeeded(Connection conn, String
tableName, String indexName, String... columnNames) {
if (!dao.indexExists(conn, tableName, indexName)) {
dao.createIndex(conn, tableName, indexName, columnNames);
}
}
+ public static boolean indexExists(Connection conn, String tableName,
String indexName) {
+ return dao.indexExists(conn, tableName, indexName);
+ }
+
+ public static void dropIndexIfExists(Connection conn, String tableName,
String indexName) {
+ dao.dropIndexIfExists(conn, tableName, indexName);
+ }
+
public static void renameIndexIfNeeded(Connection conn, String tableName,
String oldName, String newName) {
if (!dao.indexExists(conn, tableName, oldName)) {
dao.renameIndex(conn, tableName, oldName, newName);
diff --git
a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42300to2400.java
b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42300to2400.java
index ce217cef9e7..f90e100481c 100644
--- a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42300to2400.java
+++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42300to2400.java
@@ -16,6 +16,8 @@
// under the License.
package com.cloud.upgrade.dao;
+import java.sql.Connection;
+
public class Upgrade42300to2400 extends DbUpgradeAbstractImpl implements
DbUpgrade, DbUpgradeSystemVmTemplate {
@Override
@@ -27,4 +29,33 @@ public class Upgrade42300to2400 extends
DbUpgradeAbstractImpl implements DbUpgra
public String getUpgradedVersion() {
return "24.0.0";
}
+
+ @Override
+ public void performDataMigration(Connection conn) {
+ addIndexes(conn);
+ dropOldIndexes(conn);
+ }
+
+ private void addIndexes(Connection conn) {
+ DbUpgradeUtils.addIndexWithNameIfNeeded(conn, "event",
"i_event__archived__created",
+ "archived", "created");
+ DbUpgradeUtils.addIndexWithNameIfNeeded(conn, "event",
"i_event__account_id__archived__created",
+ "account_id", "archived", "created");
+ DbUpgradeUtils.addIndexWithNameIfNeeded(conn, "event",
"i_event__domain_id__archived__created",
+ "domain_id", "archived", "created");
+ DbUpgradeUtils.addIndexWithNameIfNeeded(conn, "event",
"i_event__resource_type__resource_id__archived__created",
+ "resource_type", "resource_id", "archived", "created");
+ }
+
+ private void dropOldIndexes(Connection conn) {
+ if (DbUpgradeUtils.indexExists(conn, "event",
"i_event__archived__created")) {
+ DbUpgradeUtils.dropIndexIfExists(conn, "event", "archived");
+ }
+ if (DbUpgradeUtils.indexExists(conn, "event",
"i_event__account_id__archived__created")) {
+ DbUpgradeUtils.dropIndexIfExists(conn, "event",
"i_event__account_id");
+ }
+ if (DbUpgradeUtils.indexExists(conn, "event",
"i_event__resource_type__resource_id__archived__created")) {
+ DbUpgradeUtils.dropIndexIfExists(conn, "event",
"i_event__resource_type__resource_id");
+ }
+ }
}
diff --git a/server/src/main/java/com/cloud/api/query/QueryManagerImpl.java
b/server/src/main/java/com/cloud/api/query/QueryManagerImpl.java
index 8d8586cccf2..6057978a263 100644
--- a/server/src/main/java/com/cloud/api/query/QueryManagerImpl.java
+++ b/server/src/main/java/com/cloud/api/query/QueryManagerImpl.java
@@ -969,7 +969,7 @@ public class QueryManagerImpl extends
MutualExclusiveIdsManagerBase implements Q
searchFilter.addOrderBy(EventVO.class, "id", false);
SearchBuilder<EventVO> eventSearchBuilder =
eventDao.createSearchBuilder();
- eventSearchBuilder.select(null, Func.DISTINCT,
eventSearchBuilder.entity().getId());
+ eventSearchBuilder.select(null, Func.NATIVE,
eventSearchBuilder.entity().getId());
accountMgr.buildACLSearchBuilder(eventSearchBuilder, domainId,
isRecursive, permittedAccounts, listProjectResourcesCriteria);
eventSearchBuilder.and("id", eventSearchBuilder.entity().getId(),
SearchCriteria.Op.EQ);
diff --git
a/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java
b/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java
index 70ec6b3d8b0..cca2ea1ae7d 100644
--- a/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java
+++ b/server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java
@@ -564,7 +564,7 @@ public class ConfigurationManagerImpl extends ManagerBase
implements Configurati
public static final ConfigKey<Long> DELETE_QUERY_BATCH_SIZE = new
ConfigKey<>("Advanced", Long.class, "delete.query.batch.size", "0",
"Indicates the limit applied while deleting entries in bulk. With
this, the delete query will apply the limit as many times as necessary," +
" to delete all the entries. This is advised when
retaining several days of records, which can lead to slowness. <= 0 means that
no limit will " +
- "be applied. Default value is 0. For now, this is used for
deletion of VM stats, volume stats, and usage records.", true);
+ "be applied. Default value is 0. For now, this is used for
deletion of VM stats, volume stats, events, and usage records.", true);
private static final String IOPS_READ_RATE = "IOPS Read";
private static final String IOPS_WRITE_RATE = "IOPS Write";
diff --git a/server/src/main/java/com/cloud/server/ManagementServerImpl.java
b/server/src/main/java/com/cloud/server/ManagementServerImpl.java
index e6faa4126aa..136ccb8c095 100644
--- a/server/src/main/java/com/cloud/server/ManagementServerImpl.java
+++ b/server/src/main/java/com/cloud/server/ManagementServerImpl.java
@@ -742,7 +742,6 @@ import com.cloud.domain.dao.DomainDao;
import com.cloud.event.ActionEvent;
import com.cloud.event.ActionEventUtils;
import com.cloud.event.EventTypes;
-import com.cloud.event.EventVO;
import com.cloud.event.dao.EventDao;
import com.cloud.exception.AgentUnavailableException;
import com.cloud.exception.ConcurrentOperationException;
@@ -1229,57 +1228,73 @@ public class ManagementServerImpl extends
MutualExclusiveIdsManagerBase implemen
}
@Override
- public boolean archiveEvents(final ArchiveEventsCmd cmd) {
- final Account caller = getCaller();
- final List<Long> ids = cmd.getIds();
- boolean result = true;
- List<Long> permittedAccountIds = new ArrayList<>();
+ public boolean archiveEvents(ArchiveEventsCmd cmd) {
+ List<Long> ids = cmd.getIds();
+ String type = cmd.getType();
+ Date startDate = cmd.getStartDate();
+ Date endDate = cmd.getEndDate();
- if (_accountService.isNormalUser(caller.getId()) || caller.getType()
== Account.Type.PROJECT) {
- permittedAccountIds.add(caller.getId());
- } else {
- final DomainVO domain = _domainDao.findById(caller.getDomainId());
- final List<Long> permittedDomainIds =
_domainDao.getDomainChildrenIds(domain.getPath());
- permittedAccountIds =
_accountDao.getAccountIdsForDomains(permittedDomainIds);
+ validateEventOperationParameters(ids, type, startDate, endDate);
+
+ Long accountId = null;
+ List<Long> domainIds = null;
+ Account caller = getCaller();
+ Account.Type callerType = caller.getType();
+
+ if (callerType == Account.Type.NORMAL || callerType ==
Account.Type.PROJECT) {
+ accountId = caller.getId();
+ } else if (callerType != Account.Type.ADMIN) {
+ domainIds =
_domainDao.getDomainAndChildrenIds(caller.getDomainId());
+ if (CollectionUtils.isEmpty(domainIds)) {
+ throw new CloudRuntimeException(String.format("Unable to
determine the domains of account %s", caller.getUuid()));
+ }
}
- final List<EventVO> events =
_eventDao.listToArchiveOrDeleteEvents(ids, cmd.getType(), cmd.getStartDate(),
cmd.getEndDate(), permittedAccountIds);
- final ControlledEntity[] sameOwnerEvents = events.toArray(new
ControlledEntity[events.size()]);
- _accountMgr.checkAccess(CallContext.current().getCallingAccount(),
null, false, sameOwnerEvents);
+ long totalArchived = _eventDao.archiveEvents(ids, type, startDate,
endDate, accountId, domainIds,
+ ConfigurationManagerImpl.DELETE_QUERY_BATCH_SIZE.value());
- if (ids != null && events.size() < ids.size()) {
- return false;
- }
- _eventDao.archiveEvents(events);
- return result;
+ return CollectionUtils.isEmpty(ids) || totalArchived > 0;
}
@Override
- public boolean deleteEvents(final DeleteEventsCmd cmd) {
- final Account caller = getCaller();
- final List<Long> ids = cmd.getIds();
- boolean result = true;
- List<Long> permittedAccountIds = new ArrayList<>();
+ public boolean deleteEvents(DeleteEventsCmd cmd) {
+ List<Long> ids = cmd.getIds();
+ String type = cmd.getType();
+ Date startDate = cmd.getStartDate();
+ Date endDate = cmd.getEndDate();
- if (_accountMgr.isNormalUser(caller.getId()) || caller.getType() ==
Account.Type.PROJECT) {
- permittedAccountIds.add(caller.getId());
- } else {
- final DomainVO domain = _domainDao.findById(caller.getDomainId());
- final List<Long> permittedDomainIds =
_domainDao.getDomainChildrenIds(domain.getPath());
- permittedAccountIds =
_accountDao.getAccountIdsForDomains(permittedDomainIds);
+ validateEventOperationParameters(ids, type, startDate, endDate);
+
+ Long accountId = null;
+ List<Long> domainIds = null;
+ Account caller = getCaller();
+ Account.Type callerType = caller.getType();
+
+ if (callerType == Account.Type.NORMAL || callerType ==
Account.Type.PROJECT) {
+ accountId = caller.getId();
+ } else if (callerType != Account.Type.ADMIN) {
+ domainIds =
_domainDao.getDomainAndChildrenIds(caller.getDomainId());
+ if (CollectionUtils.isEmpty(domainIds)) {
+ throw new CloudRuntimeException(String.format("Unable to
determine the domains of account %s", caller.getUuid()));
+ }
}
- final List<EventVO> events =
_eventDao.listToArchiveOrDeleteEvents(ids, cmd.getType(), cmd.getStartDate(),
cmd.getEndDate(), permittedAccountIds);
- final ControlledEntity[] sameOwnerEvents = events.toArray(new
ControlledEntity[events.size()]);
- _accountMgr.checkAccess(CallContext.current().getCallingAccount(),
null, false, sameOwnerEvents);
+ long totalRemoved = _eventDao.deleteEvents(ids, startDate, endDate,
null, type, accountId, domainIds,
+ ConfigurationManagerImpl.DELETE_QUERY_BATCH_SIZE.value());
- if (ids != null && events.size() < ids.size()) {
- return false;
+ return CollectionUtils.isEmpty(ids) || totalRemoved > 0;
+ }
+
+ protected void validateEventOperationParameters(List<Long> ids, String
type, Date startDate, Date endDate) {
+ if (ids != null && ids.isEmpty()) {
+ throw new InvalidParameterValueException("'ids' must not be empty
when provided.");
}
- for (final EventVO event : events) {
- _eventDao.remove(event.getId());
+ if (CollectionUtils.isEmpty(ids) && ObjectUtils.allNull(type,
endDate)) {
+ throw new InvalidParameterValueException("Either 'ids', 'type' or
'enddate' must be specified.");
+ }
+ if (startDate != null && endDate == null) {
+ throw new InvalidParameterValueException("'enddate' must be
specified when 'startdate' parameter is provided.");
}
- return result;
}
@Override
@@ -4500,12 +4515,10 @@ public class ManagementServerImpl extends
MutualExclusiveIdsManagerBase implemen
final Calendar purgeCal = Calendar.getInstance();
purgeCal.add(Calendar.DAY_OF_YEAR, -_purgeDelay);
final Date purgeTime = purgeCal.getTime();
- logger.debug("Deleting events older than: " + purgeTime);
- final List<EventVO> oldEvents =
_eventDao.listOlderEvents(purgeTime);
- logger.debug("Found " + oldEvents.size() + " events to be
purged");
- for (final EventVO event : oldEvents) {
- _eventDao.expunge(event.getId());
- }
+ logger.debug("Starting to purge all event rows older than
[{}].", purgeTime);
+ long totalRemoved = _eventDao.deleteEvents(null, null,
null, purgeTime, null, null, null,
+
ConfigurationManagerImpl.DELETE_QUERY_BATCH_SIZE.value());
+ logger.info("Purged a total of [{}] event rows older than
[{}].", totalRemoved, purgeTime);
} catch (final Exception e) {
logger.error("Exception ", e);
} finally {
diff --git a/server/src/test/java/com/cloud/event/EventControlsUnitTest.java
b/server/src/test/java/com/cloud/event/EventControlsUnitTest.java
deleted file mode 100644
index 8a968ed5eb9..00000000000
--- a/server/src/test/java/com/cloud/event/EventControlsUnitTest.java
+++ /dev/null
@@ -1,88 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements. See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership. The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied. See the License for the
-// specific language governing permissions and limitations
-// under the License.
-package com.cloud.event;
-
-import com.cloud.event.dao.EventDao;
-import com.cloud.server.ManagementServerImpl;
-import com.cloud.user.Account;
-import com.cloud.user.AccountManager;
-import junit.framework.TestCase;
-import org.apache.cloudstack.acl.ControlledEntity;
-import org.apache.cloudstack.acl.SecurityChecker.AccessType;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.LogManager;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.mockito.Spy;
-
-import java.util.Date;
-import java.util.List;
-
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyList;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.when;
-
-public class EventControlsUnitTest extends TestCase {
- private Logger logger = LogManager.getLogger(EventControlsUnitTest.class);
-
- @Spy
- ManagementServerImpl _mgmtServer = new ManagementServerImpl();
- @Mock
- AccountManager _accountMgr;
- @Mock
- EventDao _eventDao;
- List<EventVO> _events = null;
- private AutoCloseable closeable;
-
- @Override
- @Before
- public void setUp() {
- closeable = MockitoAnnotations.openMocks(this);
- _mgmtServer._eventDao = _eventDao;
- _mgmtServer._accountMgr = _accountMgr;
- doNothing().when(_accountMgr).checkAccess(any(Account.class),
any(AccessType.class), any(Boolean.class), any(ControlledEntity.class));
- when(_eventDao.listToArchiveOrDeleteEvents(anyList(), anyString(),
any(Date.class), any(Date.class), anyList())).thenReturn(_events);
- }
-
- @Override
- @After
- public void tearDown() throws Exception {
- closeable.close();
- }
-
- @Test
- public void testInjected() throws Exception {
- logger.info("Starting test to archive and delete events");
- archiveEvents();
- deleteEvents();
- logger.info("archive/delete events: TEST PASSED");
- }
-
- protected void archiveEvents() {
- // archive alerts
- doNothing().when(_eventDao).archiveEvents(_events);
- }
-
- protected void deleteEvents() {
- // delete alerts
- }
-}
diff --git
a/server/src/test/java/com/cloud/server/ManagementServerImplTest.java
b/server/src/test/java/com/cloud/server/ManagementServerImplTest.java
index 74c9c5e6682..71a68453197 100644
--- a/server/src/test/java/com/cloud/server/ManagementServerImplTest.java
+++ b/server/src/test/java/com/cloud/server/ManagementServerImplTest.java
@@ -23,6 +23,10 @@ import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import com.cloud.configuration.ConfigurationManagerImpl;
+import org.apache.cloudstack.api.command.user.event.ArchiveEventsCmd;
+import org.apache.cloudstack.api.command.user.event.DeleteEventsCmd;
+import org.apache.commons.lang3.time.DateUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -42,6 +46,7 @@ import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -80,6 +85,7 @@ import com.cloud.deploy.DeploymentPlanner;
import com.cloud.deploy.DeploymentPlanningManager;
import com.cloud.domain.dao.DomainDao;
import com.cloud.api.ApiDBUtils;
+import com.cloud.event.dao.EventDao;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.gpu.GPU;
@@ -240,6 +246,9 @@ public class ManagementServerImplTest {
@Mock
ExtensionsManager extensionManager;
+ @Mock
+ EventDao eventDao;
+
@Spy
@InjectMocks
ManagementServerImpl spy = new ManagementServerImpl();
@@ -275,13 +284,6 @@ public class ManagementServerImplTest {
public void setup() throws IllegalAccessException, NoSuchFieldException {
closeable = MockitoAnnotations.openMocks(this);
CallContext.register(Mockito.mock(User.class),
Mockito.mock(Account.class));
- spy._accountMgr = accountManager;
- spy.userDataDao = userDataDao;
- spy.templateDao = templateDao;
- spy._userVmDao = userVmDao;
- spy.annotationDao = annotationDao;
- spy._detailsDao = hostDetailsDao;
- spy.userDataManager = userDataManager;
spy.setHostAllocators(List.of(hostAllocator));
@@ -1163,6 +1165,161 @@ public class ManagementServerImplTest {
Mockito.verify(extensionManager).getInstanceConsole(virtualMachineMock, host);
}
+ @Test(expected = InvalidParameterValueException.class)
+ public void
validateEventOperationParametersTestIdsAndTypeAndEndDateIsNullThrowInvalidParameterValueException()
{
+ spy.validateEventOperationParameters(null, null, new Date(), null);
+ }
+
+ @Test(expected = InvalidParameterValueException.class)
+ public void
validateEventOperationParametersTestStartDateIsNotNullAndEndDateIsNullThrowInvalidParameterValueException()
{
+ spy.validateEventOperationParameters(List.of(1L), null, new Date(),
null);
+ }
+
+ @Test(expected = InvalidParameterValueException.class)
+ public void
validateEventOperationParametersTestEmptyIdsThrowInvalidParameterValueException()
{
+ spy.validateEventOperationParameters(Collections.emptyList(), "type",
null, null);
+ }
+
+ private ArchiveEventsCmd createArchiveEventsCmdForTests() {
+ ArchiveEventsCmd cmd = Mockito.mock(ArchiveEventsCmd.class);
+ Date endDate = new Date();
+ Mockito.when(cmd.getIds()).thenReturn(List.of(10L, 11L, 12L, 13L));
+ Mockito.when(cmd.getType()).thenReturn("type");
+ Mockito.when(cmd.getEndDate()).thenReturn(endDate);
+ Mockito.when(cmd.getStartDate()).thenReturn(DateUtils.addDays(endDate,
-1));
+ return cmd;
+ }
+
+ @Test
+ public void archiveEventsTestArchivesOnlyOwnEventsWhenCallerIsNormalUser()
{
+ Long callerId = 5L;
+ ArchiveEventsCmd cmd = createArchiveEventsCmdForTests();
+
+ Mockito.doReturn(Account.Type.NORMAL).when(account).getType();
+ Mockito.doReturn(callerId).when(account).getId();
+ Mockito.doReturn(account).when(spy).getCaller();
+
+ spy.archiveEvents(cmd);
+
+ Mockito.verify(spy).validateEventOperationParameters(cmd.getIds(),
cmd.getType(), cmd.getStartDate(), cmd.getEndDate());
+ Mockito.verify(eventDao).archiveEvents(cmd.getIds(), cmd.getType(),
cmd.getStartDate(), cmd.getEndDate(), callerId,
+ null,
ConfigurationManagerImpl.DELETE_QUERY_BATCH_SIZE.value());
+ }
+
+ @Test
+ public void
archiveEventsTestArchivesOnlyEventsOfOwnDomainWhenCallerIsAdmin() {
+ Long callerDomainId = 6L;
+ List<Long> domainIds = List.of(callerDomainId, 7L, 8L);
+ ArchiveEventsCmd cmd = createArchiveEventsCmdForTests();
+
+ Mockito.doReturn(Account.Type.DOMAIN_ADMIN).when(account).getType();
+ Mockito.doReturn(callerDomainId).when(account).getDomainId();
+ Mockito.doReturn(account).when(spy).getCaller();
+
Mockito.doReturn(domainIds).when(domainDao).getDomainAndChildrenIds(callerDomainId);
+
+ spy.archiveEvents(cmd);
+
+ Mockito.verify(spy).validateEventOperationParameters(cmd.getIds(),
cmd.getType(), cmd.getStartDate(), cmd.getEndDate());
+ Mockito.verify(eventDao).archiveEvents(cmd.getIds(), cmd.getType(),
cmd.getStartDate(), cmd.getEndDate(), null,
+ domainIds,
ConfigurationManagerImpl.DELETE_QUERY_BATCH_SIZE.value());
+ }
+
+ @Test(expected = CloudRuntimeException.class)
+ public void archiveEventsTestFailsWhenCallerDomainIsNotFound() {
+ ArchiveEventsCmd cmd = createArchiveEventsCmdForTests();
+
+ Mockito.doReturn(Account.Type.DOMAIN_ADMIN).when(account).getType();
+ Mockito.doReturn(6L).when(account).getDomainId();
+ Mockito.doReturn(account).when(spy).getCaller();
+
Mockito.doReturn(Collections.emptyList()).when(domainDao).getDomainAndChildrenIds(6L);
+
+ spy.archiveEvents(cmd);
+ }
+
+ @Test
+ public void archiveEventsTestDoesNotFilterDomainsWhenCallerIsRootAdmin() {
+ ArchiveEventsCmd cmd = createArchiveEventsCmdForTests();
+
+ Mockito.doReturn(Account.Type.ADMIN).when(account).getType();
+ Mockito.doReturn(account).when(spy).getCaller();
+
+ spy.archiveEvents(cmd);
+
+ Mockito.verify(domainDao,
Mockito.never()).getDomainAndChildrenIds(Mockito.anyLong());
+ Mockito.verify(eventDao).archiveEvents(cmd.getIds(), cmd.getType(),
cmd.getStartDate(), cmd.getEndDate(), null,
+ null,
ConfigurationManagerImpl.DELETE_QUERY_BATCH_SIZE.value());
+ }
+
+ private DeleteEventsCmd createDeleteEventsCmdForTests() {
+ DeleteEventsCmd cmd = Mockito.mock(DeleteEventsCmd.class);
+ Date endDate = new Date();
+ Mockito.when(cmd.getIds()).thenReturn(List.of(10L, 11L, 12L, 13L));
+ Mockito.when(cmd.getType()).thenReturn("type");
+ Mockito.when(cmd.getEndDate()).thenReturn(endDate);
+ Mockito.when(cmd.getStartDate()).thenReturn(DateUtils.addDays(endDate,
-1));
+ return cmd;
+ }
+
+ @Test
+ public void deleteEventsTestDeletesOnlyOwnEventsWhenCallerIsNormalUser() {
+ Long callerId = 5L;
+ DeleteEventsCmd cmd = createDeleteEventsCmdForTests();
+
+ Mockito.doReturn(Account.Type.NORMAL).when(account).getType();
+ Mockito.doReturn(callerId).when(account).getId();
+ Mockito.doReturn(account).when(spy).getCaller();
+
+ spy.deleteEvents(cmd);
+
+ Mockito.verify(spy).validateEventOperationParameters(cmd.getIds(),
cmd.getType(), cmd.getStartDate(), cmd.getEndDate());
+ Mockito.verify(eventDao).deleteEvents(cmd.getIds(),
cmd.getStartDate(), cmd.getEndDate(), null, cmd.getType(),
+ callerId, null,
ConfigurationManagerImpl.DELETE_QUERY_BATCH_SIZE.value());
+ }
+
+ @Test
+ public void deleteEventsTestDeletesOnlyEventsOfOwnDomainCallerIsAdmin() {
+ Long callerDomainId = 6L;
+ List<Long> domainIds = List.of(callerDomainId, 7L, 8L);
+ DeleteEventsCmd cmd = createDeleteEventsCmdForTests();
+
+ Mockito.doReturn(Account.Type.DOMAIN_ADMIN).when(account).getType();
+ Mockito.doReturn(callerDomainId).when(account).getDomainId();
+ Mockito.doReturn(account).when(spy).getCaller();
+
Mockito.doReturn(domainIds).when(domainDao).getDomainAndChildrenIds(callerDomainId);
+
+ spy.deleteEvents(cmd);
+
+ Mockito.verify(spy).validateEventOperationParameters(cmd.getIds(),
cmd.getType(), cmd.getStartDate(), cmd.getEndDate());
+ Mockito.verify(eventDao).deleteEvents(cmd.getIds(),
cmd.getStartDate(), cmd.getEndDate(), null, cmd.getType(),
+ null, domainIds,
ConfigurationManagerImpl.DELETE_QUERY_BATCH_SIZE.value());
+ }
+
+ @Test(expected = CloudRuntimeException.class)
+ public void deleteEventsTestFailsWhenCallerDomainIsNotFound() {
+ DeleteEventsCmd cmd = createDeleteEventsCmdForTests();
+
+ Mockito.doReturn(Account.Type.DOMAIN_ADMIN).when(account).getType();
+ Mockito.doReturn(6L).when(account).getDomainId();
+ Mockito.doReturn(account).when(spy).getCaller();
+
Mockito.doReturn(Collections.emptyList()).when(domainDao).getDomainAndChildrenIds(6L);
+
+ spy.deleteEvents(cmd);
+ }
+
+ @Test
+ public void deleteEventsTestDoesNotFilterDomainsWhenCallerIsRootAdmin() {
+ DeleteEventsCmd cmd = createDeleteEventsCmdForTests();
+
+ Mockito.doReturn(Account.Type.ADMIN).when(account).getType();
+ Mockito.doReturn(account).when(spy).getCaller();
+
+ spy.deleteEvents(cmd);
+
+ Mockito.verify(domainDao,
Mockito.never()).getDomainAndChildrenIds(Mockito.anyLong());
+ Mockito.verify(eventDao).deleteEvents(cmd.getIds(),
cmd.getStartDate(), cmd.getEndDate(), null, cmd.getType(),
+ null, null,
ConfigurationManagerImpl.DELETE_QUERY_BATCH_SIZE.value());
+ }
+
@Test
public void
getCapableSuitableHostsTestHostArchIsNotFilteredWhenNoSuitableHostsAreFound() {
List<Host> compatibleHosts = List.of(hostMock);