eubnara commented on code in PR #3946: URL: https://github.com/apache/ambari/pull/3946#discussion_r1970778586
########## ambari-server/src/main/java/org/apache/ambari/server/orm/dao/RequestScheduleDAO.java: ########## @@ -89,4 +110,73 @@ public void removeByPK(Long id) { public void refresh(RequestScheduleEntity requestScheduleEntity) { entityManagerProvider.get().refresh(requestScheduleEntity); } + + /** + * Find all @RequestScheduleEntity with date before provided date. + * @param clusterId cluster id + * @param beforeDateMillis timestamp in millis + * @return List<Integer> ids + */ + private List<Integer> findAllScheduleIdsBeforeDate(Long clusterId, long beforeDateMillis) { + + EntityManager entityManager = entityManagerProvider.get(); + TypedQuery<Integer> requestScheduleQuery = + entityManager.createNamedQuery("RequestScheduleEntity.findAllReqScheduleIdsInClusterBeforeDate", Integer.class); + + requestScheduleQuery.setParameter("clusterId", clusterId); + requestScheduleQuery.setParameter("beforeDate", beforeDateMillis); + + return daoUtils.selectList(requestScheduleQuery); + } + + /** + * Deletes RequestSchedule and RequestScheduleBatchRequest records in relation with RequestSchedule entries older than the given date. + * + * @param clusterId the identifier of the cluster the RequestSchedule belong to + * @param beforeDateMillis the date in milliseconds the + * @return a long representing the number of affected (deleted) records + */ + @Transactional + int cleanRequestSchedulesAndRequestScheduleBatchRequestsForClusterBeforeDate(Long clusterId, long beforeDateMillis) { + LOG.info("Deleting RequestSchedule and RequestScheduleBatchRequest entities before date " + new Date(beforeDateMillis)); + EntityManager entityManager = entityManagerProvider.get(); + List<Integer> ids = findAllScheduleIdsBeforeDate(clusterId, beforeDateMillis); + int affectedRows = 0; + + TypedQuery<RequestScheduleEntity> requestScheduleQuery = + entityManager.createNamedQuery("RequestScheduleEntity.removeByScheduleIds", RequestScheduleEntity.class); + TypedQuery<RequestScheduleBatchRequestEntity> requestScheduleBatchRequestQuery = + entityManager.createNamedQuery("RequestScheduleBatchRequestEntity.removeByScheduleIds", RequestScheduleBatchRequestEntity.class); + if (ids != null && !ids.isEmpty()) { + for (int i = 0; i < ids.size(); i += BATCH_SIZE) { + int endIndex = Math.min((i + BATCH_SIZE), ids.size()); + List<Integer> idsSubList = ids.subList(i, endIndex); + LOG.info("Deleting RequestSchedule entity batch with schedule ids: " + + idsSubList.get(0) + " - " + idsSubList.get(idsSubList.size() - 1)); + requestScheduleQuery.setParameter("scheduleIds", idsSubList); + affectedRows += requestScheduleQuery.executeUpdate(); + LOG.info("Deleting RequestScheduleBatchRequest entity batch with schedule ids: " + Review Comment: RequestSchedule is related to RequestScheduleBatchRequest. I guess that RequestSchedule is usually removed with RequestScheduleBatchRequest. - I referred to https://github.com/apache/ambari/blob/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/scheduler/RequestExecutionImpl.java#L238-L242 ```java @Override public void delete() { readWriteLock.writeLock().lock(); try { if (isPersisted) { batchRequestDAO.removeByScheduleId(requestScheduleEntity.getScheduleId()); requestScheduleDAO.remove(requestScheduleEntity); cluster.refresh(); isPersisted = false; } } finally { readWriteLock.writeLock().unlock(); } } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@ambari.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@ambari.apache.org For additional commands, e-mail: dev-h...@ambari.apache.org