Browse Source
* [Fix-4222][Master]Add the priority queue to ensure that tasks are submitted according to priority. * [Fix-4222][Master]Add the priority queue to ensure that tasks are submitted according to priority. * [Fix-4222][Master]Remove useless import * [Fix-4222][Master]Reformat code style * [Fix-4222][Master]Reformat code style * [Fix-4222][Master]Reformat code style * [Fix-4222][Master]add PeerTaskInstancePriorityQueueTest * [Fix-4222][Master]Fix code smell * [Fix-4222][Master]Reformat code style * [Fix-4222][Master]Fix code smell Co-authored-by: xingchun-chen <55787491+xingchun-chen@users.noreply.github.com>pull/3/MERGE
lgcareer
4 years ago
committed by
GitHub
8 changed files with 342 additions and 52 deletions
@ -0,0 +1,44 @@ |
|||||||
|
/* |
||||||
|
* 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.dolphinscheduler.service.exceptions; |
||||||
|
|
||||||
|
/** |
||||||
|
* task priority queue exception |
||||||
|
*/ |
||||||
|
public class TaskPriorityQueueException extends Exception { |
||||||
|
|
||||||
|
/** |
||||||
|
* Construct a new runtime exception with the detail message |
||||||
|
* |
||||||
|
* @param message message |
||||||
|
*/ |
||||||
|
public TaskPriorityQueueException(String message) { |
||||||
|
super(message); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Construct a new runtime exception with the detail message and cause |
||||||
|
* |
||||||
|
* @param message message |
||||||
|
* @param cause cause |
||||||
|
*/ |
||||||
|
public TaskPriorityQueueException(String message, Throwable cause) { |
||||||
|
super(message, cause); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,127 @@ |
|||||||
|
/* |
||||||
|
* 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.dolphinscheduler.service.queue; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.dao.entity.TaskInstance; |
||||||
|
import org.apache.dolphinscheduler.service.exceptions.TaskPriorityQueueException; |
||||||
|
|
||||||
|
import java.util.Comparator; |
||||||
|
import java.util.Iterator; |
||||||
|
import java.util.PriorityQueue; |
||||||
|
|
||||||
|
/** |
||||||
|
* Task instances priority queue implementation |
||||||
|
* All the task instances are in the same process instance. |
||||||
|
*/ |
||||||
|
public class PeerTaskInstancePriorityQueue implements TaskPriorityQueue<TaskInstance> { |
||||||
|
/** |
||||||
|
* queue size |
||||||
|
*/ |
||||||
|
private static final Integer QUEUE_MAX_SIZE = 3000; |
||||||
|
|
||||||
|
/** |
||||||
|
* queue |
||||||
|
*/ |
||||||
|
private PriorityQueue<TaskInstance> queue = new PriorityQueue<>(QUEUE_MAX_SIZE, new TaskInfoComparator()); |
||||||
|
|
||||||
|
/** |
||||||
|
* put task instance to priority queue |
||||||
|
* |
||||||
|
* @param taskInstance taskInstance |
||||||
|
* @throws TaskPriorityQueueException |
||||||
|
*/ |
||||||
|
public void put(TaskInstance taskInstance) throws TaskPriorityQueueException { |
||||||
|
queue.add(taskInstance); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* take task info |
||||||
|
* |
||||||
|
* @return task instance |
||||||
|
* @throws TaskPriorityQueueException |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public TaskInstance take() throws TaskPriorityQueueException { |
||||||
|
return queue.poll(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* peek taskInfo |
||||||
|
* |
||||||
|
* @return task instance |
||||||
|
*/ |
||||||
|
public TaskInstance peek() { |
||||||
|
return queue.peek(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* queue size |
||||||
|
* |
||||||
|
* @return size |
||||||
|
*/ |
||||||
|
public int size() { |
||||||
|
return queue.size(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* whether contains the task instance |
||||||
|
* |
||||||
|
* @param taskInstance task instance |
||||||
|
* @return true is contains |
||||||
|
*/ |
||||||
|
public boolean contains(TaskInstance taskInstance) { |
||||||
|
return queue.contains(taskInstance); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* remove task |
||||||
|
* |
||||||
|
* @param taskInstance task instance |
||||||
|
* @return true if remove success |
||||||
|
*/ |
||||||
|
public boolean remove(TaskInstance taskInstance) { |
||||||
|
return queue.remove(taskInstance); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* get iterator |
||||||
|
* |
||||||
|
* @return Iterator |
||||||
|
*/ |
||||||
|
public Iterator<TaskInstance> iterator() { |
||||||
|
return queue.iterator(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* TaskInfoComparator |
||||||
|
*/ |
||||||
|
private class TaskInfoComparator implements Comparator<TaskInstance> { |
||||||
|
|
||||||
|
/** |
||||||
|
* compare o1 o2 |
||||||
|
* |
||||||
|
* @param o1 o1 |
||||||
|
* @param o2 o2 |
||||||
|
* @return compare result |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public int compare(TaskInstance o1, TaskInstance o2) { |
||||||
|
return o1.getTaskInstancePriority().compareTo(o2.getTaskInstancePriority()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,89 @@ |
|||||||
|
/* |
||||||
|
* 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 queue; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.common.enums.Priority; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.TaskInstance; |
||||||
|
import org.apache.dolphinscheduler.service.queue.PeerTaskInstancePriorityQueue; |
||||||
|
|
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
/** |
||||||
|
* Task instances priority queue implementation |
||||||
|
* All the task instances are in the same process instance. |
||||||
|
*/ |
||||||
|
public class PeerTaskInstancePriorityQueueTest { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testPut() throws Exception { |
||||||
|
PeerTaskInstancePriorityQueue queue = new PeerTaskInstancePriorityQueue(); |
||||||
|
TaskInstance taskInstanceHigPriority = createTaskInstance("high", Priority.HIGH); |
||||||
|
TaskInstance taskInstanceMediumPriority = createTaskInstance("high", Priority.MEDIUM); |
||||||
|
queue.put(taskInstanceHigPriority); |
||||||
|
queue.put(taskInstanceMediumPriority); |
||||||
|
Assert.assertEquals(2,queue.size()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testPeek() throws Exception { |
||||||
|
PeerTaskInstancePriorityQueue queue = getPeerTaskInstancePriorityQueue(); |
||||||
|
int peekBeforeLength = queue.size(); |
||||||
|
queue.peek(); |
||||||
|
Assert.assertEquals(peekBeforeLength,queue.size()); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testTake() throws Exception { |
||||||
|
PeerTaskInstancePriorityQueue queue = getPeerTaskInstancePriorityQueue(); |
||||||
|
int peekBeforeLength = queue.size(); |
||||||
|
queue.take(); |
||||||
|
Assert.assertTrue(queue.size() < peekBeforeLength); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* get queue |
||||||
|
* |
||||||
|
* @return queue |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
private PeerTaskInstancePriorityQueue getPeerTaskInstancePriorityQueue() throws Exception { |
||||||
|
PeerTaskInstancePriorityQueue queue = new PeerTaskInstancePriorityQueue(); |
||||||
|
TaskInstance taskInstanceHigPriority = createTaskInstance("high", Priority.HIGH); |
||||||
|
TaskInstance taskInstanceMediumPriority = createTaskInstance("high", Priority.MEDIUM); |
||||||
|
queue.put(taskInstanceHigPriority); |
||||||
|
queue.put(taskInstanceMediumPriority); |
||||||
|
return queue; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* create task instance |
||||||
|
* |
||||||
|
* @param name name |
||||||
|
* @param priority priority |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private TaskInstance createTaskInstance(String name, Priority priority) { |
||||||
|
TaskInstance taskInstance = new TaskInstance(); |
||||||
|
taskInstance.setName(name); |
||||||
|
taskInstance.setTaskInstancePriority(priority); |
||||||
|
return taskInstance; |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue