AshinGau commented on code in PR #14428: URL: https://github.com/apache/doris/pull/14428#discussion_r1034425888
########## fe/fe-core/src/main/java/org/apache/doris/job/ResourceQueue.java: ########## @@ -0,0 +1,141 @@ +// 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 +// with 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 org.apache.doris.job; + +import org.apache.doris.common.UserException; +import org.apache.doris.job.AsyncJob.JobStatus; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicLong; + +public class ResourceQueue { + private static final AtomicLong QUEUE_ID = new AtomicLong(0); + + private final long id; + private final String name; + private final ResourceQueueConfig config; + private final MatchingPolicy policy; + private final AsyncJobManager jobMgr; + + // jobId -> job + private final Map<Long, AsyncJob> allJobs; + private final BlockingQueue<AsyncJob> pendingQueue; + private final Map<Long, AsyncJob> runningJobs; + private final Map<Long, AsyncJob> finishedJobs; + private final Semaphore runningSemaphore; + + + public ResourceQueue(String name, AsyncJobManager jobMgr, ResourceQueueConfig config, MatchingPolicy policy) { + this.id = QUEUE_ID.getAndIncrement(); + this.name = name; + this.jobMgr = jobMgr; + this.config = config; + this.policy = policy; + + allJobs = new HashMap<>(); + pendingQueue = new ArrayBlockingQueue<>(config.max_queue_size()); + runningJobs = new HashMap<>(); + finishedJobs = new HashMap<>(); + runningSemaphore = new Semaphore(config.max_concurrency()); + } + + public boolean match(AsyncJob job) { + return policy.match(job.getUser()); + } + + public synchronized boolean addJob(AsyncJob job) throws UserException { + if (allJobs.containsKey(job.jobId())) { + job.setJobStatus(JobStatus.CANCELED); + throw new UserException(String.format("Submit the same job(jobId=%d)", job.jobId())); + } + job.setJobStatus(JobStatus.PENDING); + if (pendingQueue.size() < config.max_queue_size() && pendingQueue.offer(job)) { + allJobs.put(job.jobId(), job); + } + job.setJobStatus(JobStatus.CANCELED); + return false; + } + + public AsyncJob cancelJob(long jobId) { + AsyncJob job = allJobs.get(jobId); + if (job != null) { + synchronized (this) { + job = allJobs.get(jobId); + } + } + if (job != null) { + // cancel should be Idempotent + job.cancel(); + synchronized (this) { + if (finishedJobs.putIfAbsent(jobId, job) == null) { + if (runningJobs.remove(jobId) == null) { + pendingQueue.remove(job); + } + } + } + } + return job; + } + + public ResourceQueueConfig getConfig() { + return config; + } + + public String getName() { + return name; + } + + public boolean canAddJob() { + return pendingQueue.size() < config.max_queue_size(); + } + + public int numPendingJobs() { + return pendingQueue.size(); + } + + public int numRunningJobs() { + return runningJobs.size(); + } + + public long queueId() { + return id; + } + + public void run() { Review Comment: done -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
