mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-05 08:03:01 +00:00
fix(task): find unfinished tasks through the status index (#8529)
HasUnfinishedSyncTasks, GetAllUnFinishSyncTasks and GetTimedOutUnfinishedTasks exclude the two terminal statuses with "status != ?" or "NOT IN". PostgreSQL and SQLite cannot use idx_tasks_status for that filter, so every check reads all finished tasks. The async_task_poll scheduler runs the existence check every 15 seconds. On a PostgreSQL table with 1.4 million finished tasks, one check took about 27 seconds, and the database kept scanning the table even when no task was unfinished. Write the filter as the key ranges around FAILURE and SUCCESS. It matches the same rows, and all three databases answer it from the status index. Closes #8528
This commit is contained in:
1 parent
b697786325
commit
0d0d396f48
2 files changed
+14
-5
No files matched your search
+13
-4
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/QuantumNous/new-api/pkg/jsplugin"
|
||||
commonRelay "github.com/QuantumNous/new-api/relay/common"
|
||||
"github.com/QuantumNous/new-api/relaykit/dto"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TaskStatus string
|
||||
@@ -399,9 +400,18 @@ func TaskGetAllTasks(startIdx int, num int, queryParams SyncTaskQueryParams) []*
|
||||
return tasks
|
||||
}
|
||||
|
||||
// unfinishedTasks keeps tasks whose status is neither FAILURE nor SUCCESS.
|
||||
// It writes the condition as the ranges around the two terminal statuses
|
||||
// (FAILURE sorts before SUCCESS), so the database reads only unfinished rows
|
||||
// from the status index instead of scanning every finished task.
|
||||
func unfinishedTasks(tx *gorm.DB) *gorm.DB {
|
||||
return tx.Where("status < ? OR (status > ? AND status < ?) OR status > ?",
|
||||
TaskStatusFailure, TaskStatusFailure, TaskStatusSuccess, TaskStatusSuccess)
|
||||
}
|
||||
|
||||
func GetTimedOutUnfinishedTasks(cutoffUnix int64, limit int) []*Task {
|
||||
var tasks []*Task
|
||||
err := DB.Where("status NOT IN ?", []string{TaskStatusFailure, TaskStatusSuccess}).
|
||||
err := DB.Scopes(unfinishedTasks).
|
||||
Where("submit_time < ?", cutoffUnix).
|
||||
Order("submit_time").
|
||||
Limit(limit).
|
||||
@@ -416,7 +426,7 @@ func GetAllUnFinishSyncTasks(limit int) []*Task {
|
||||
var tasks []*Task
|
||||
var err error
|
||||
// Upstream progress can reach 100% before the task enters a terminal state.
|
||||
err = DB.Where("status != ?", TaskStatusFailure).Where("status != ?", TaskStatusSuccess).Limit(limit).Order("id").Find(&tasks).Error
|
||||
err = DB.Scopes(unfinishedTasks).Limit(limit).Order("id").Find(&tasks).Error
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -430,8 +440,7 @@ func GetAllUnFinishSyncTasks(limit int) []*Task {
|
||||
func HasUnfinishedSyncTasks() bool {
|
||||
var id int64
|
||||
err := DB.Model(&Task{}).
|
||||
Where("status != ?", TaskStatusFailure).
|
||||
Where("status != ?", TaskStatusSuccess).
|
||||
Scopes(unfinishedTasks).
|
||||
Limit(1).
|
||||
Pluck("id", &id).Error
|
||||
return err == nil && id != 0
|
||||
|
||||
@@ -149,7 +149,7 @@ func TestUnfinishedTaskQueriesDatabaseMatrix(t *testing.T) {
|
||||
assert.False(t, HasUnfinishedSyncTasks())
|
||||
|
||||
const cutoff int64 = 1_800_000_000
|
||||
for _, status := range []TaskStatus{TaskStatusNotStart, TaskStatusSubmitted, TaskStatusQueued, TaskStatusInProgress, TaskStatusSuccess, TaskStatusFailure} {
|
||||
for _, status := range []TaskStatus{"", TaskStatusNotStart, TaskStatusSubmitted, TaskStatusQueued, TaskStatusInProgress, TaskStatusUnknown, TaskStatusSuccess, TaskStatusFailure} {
|
||||
for _, progress := range []string{"99%", "100%"} {
|
||||
t.Run(string(status)+"/"+progress, func(t *testing.T) {
|
||||
task := &Task{TaskID: "query_candidate", Status: status, Progress: progress, SubmitTime: cutoff - 1}
|
||||
|
||||
Reference in new issue
Block a user