weizhengte commented on code in PR #8858: URL: https://github.com/apache/incubator-doris/pull/8858#discussion_r852546394
########## fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticsJobManager.java: ########## @@ -27,47 +35,105 @@ import java.util.Map; import java.util.Set; +import java.util.concurrent.locks.ReentrantReadWriteLock; -/* -For unified management of statistics job, -including job addition, cancellation, scheduling, etc. +/** + * For unified management of statistics job, + * including job addition, cancellation, scheduling, etc. */ public class StatisticsJobManager { private static final Logger LOG = LogManager.getLogger(StatisticsJobManager.class); - // statistics job - private Map<Long, StatisticsJob> idToStatisticsJob = Maps.newConcurrentMap(); + /** + * save statistics job status information + */ + private final Map<Long, StatisticsJob> idToStatisticsJob = Maps.newConcurrentMap(); - public void createStatisticsJob(AnalyzeStmt analyzeStmt) { - // step0: init statistics job by analyzeStmt + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + + public void readLock() { + lock.readLock().lock(); + } + + public void readUnlock() { + lock.readLock().unlock(); + } + + private void writeLock() { + lock.writeLock().lock(); + } + + private void writeUnlock() { + lock.writeLock().unlock(); + } + + public Map<Long, StatisticsJob> getIdToStatisticsJob() { + return this.idToStatisticsJob; + } + + public void createStatisticsJob(AnalyzeStmt analyzeStmt) throws UserException { + // step1: init statistics job by analyzeStmt StatisticsJob statisticsJob = StatisticsJob.fromAnalyzeStmt(analyzeStmt); - // step1: get statistics to be analyzed - Set<Long> tableIdList = statisticsJob.relatedTableId(); - // step2: check restrict - checkRestrict(tableIdList); - // step3: check permission - checkPermission(); - // step4: create it - createStatisticsJob(statisticsJob); + writeLock(); + try { + // step2: check restrict + this.checkRestrict(analyzeStmt.getDb(), statisticsJob.getTblIds()); + // step3: create it + this.createStatisticsJob(statisticsJob); + } finally { + writeUnlock(); + } } - public void createStatisticsJob(StatisticsJob statisticsJob) { - idToStatisticsJob.put(statisticsJob.getId(), statisticsJob); + public void createStatisticsJob(StatisticsJob statisticsJob) throws DdlException { + // assign the id when the job is ready to run + statisticsJob.setId(Catalog.getCurrentCatalog().getNextId()); + this.idToStatisticsJob.put(statisticsJob.getId(), statisticsJob); try { Catalog.getCurrentCatalog().getStatisticsJobScheduler().addPendingJob(statisticsJob); } catch (IllegalStateException e) { LOG.info("The pending statistics job is full. Please submit it again later."); + throw new DdlException("The pending statistics job is full, Please submit it again later."); } } - // Rule1: The same table cannot have two unfinished statistics jobs - // Rule2: The unfinished statistics job could not more then Config.max_statistics_job_num - // Rule3: The job for external table is not supported - private void checkRestrict(Set<Long> tableIdList) { - // TODO - } + /** + * The statistical job has the following restrict: + * - Rule1: The same table cannot have two unfinished statistics jobs + * - Rule2: The unfinished statistics job could not more then Config.max_statistics_job_num + * - Rule3: The job for external table is not supported + */ + private void checkRestrict(Database db, Set<Long> tableIds) throws AnalysisException { + // check table type + for (Long tableId : tableIds) { + Table table = db.getTableOrAnalysisException(tableId); + if (table.getType() != Table.TableType.OLAP) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_NOT_OLAP_TABLE, db.getFullName(), table.getName(), "ANALYZE"); + } + } - private void checkPermission() { - // TODO + int unfinishedJobs = 0; + + // check table unfinished job + for (StatisticsJob statisticsJob : this.idToStatisticsJob.values()) { + StatisticsJob.JobState jobState = statisticsJob.getJobState(); + Set<Long> tblIds = statisticsJob.getTblIds(); + if (jobState == StatisticsJob.JobState.PENDING + || jobState == StatisticsJob.JobState.SCHEDULING + || jobState == StatisticsJob.JobState.RUNNING) { + for (Long tableId : tableIds) { + if (tblIds.contains(tableId)) { Review Comment: A table may have more than one statistics job. -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org