@@ -6,7 +6,7 @@ use crate::task::{
6
6
} ;
7
7
use dashmap:: DashMap ;
8
8
use datafusion:: physical_plan:: ExecutionPlan ;
9
- use std:: collections:: BTreeMap ;
9
+ use std:: collections:: { BTreeMap , VecDeque } ;
10
10
use std:: hash:: { Hash , Hasher } ;
11
11
use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
12
12
use std:: sync:: Arc ;
@@ -30,9 +30,9 @@ impl Copy for TaskId {}
30
30
31
31
#[ derive( Debug ) ]
32
32
pub struct State {
33
- // queue: Mutex<VecDeque<QueryKey>>,
34
33
// The queue used to order queries by executor usage.
35
- queue : Mutex < BTreeMap < Duration , u64 > > ,
34
+ // queue: Mutex<BTreeMap<Duration, u64>>,
35
+ queue : Mutex < VecDeque < u64 > > ,
36
36
start_ts : SystemTime ,
37
37
38
38
query_id_counter : AtomicU64 ,
@@ -47,7 +47,8 @@ pub struct State {
47
47
impl State {
48
48
pub fn new ( notify : Arc < Notify > ) -> Self {
49
49
Self {
50
- queue : Mutex :: new ( BTreeMap :: new ( ) ) ,
50
+ // queue: Mutex::new(BTreeMap::new()),
51
+ queue : Mutex :: new ( VecDeque :: new ( ) ) ,
51
52
start_ts : SystemTime :: now ( ) ,
52
53
query_id_counter : AtomicU64 :: new ( 0 ) ,
53
54
table : DashMap :: new ( ) ,
@@ -67,7 +68,8 @@ impl State {
67
68
query. time = time;
68
69
69
70
self . table . insert ( id, RwLock :: new ( query) ) ;
70
- self . queue . lock ( ) . await . insert ( time, id) ;
71
+ // self.queue.lock().await.insert(time, id);
72
+ self . queue . lock ( ) . await . push_back ( id) ;
71
73
72
74
self . notify . notify_waiters ( ) ;
73
75
id
@@ -86,7 +88,8 @@ impl State {
86
88
}
87
89
88
90
pub async fn next_task ( & self ) -> Option < ( TaskId , Arc < dyn ExecutionPlan > ) > {
89
- let Some ( ( duration, query_id) ) = self . queue . lock ( ) . await . pop_first ( ) else {
91
+ // let Some((duration, query_id)) = self.queue.lock().await.pop_first() else {
92
+ let Some ( query_id) = self . queue . lock ( ) . await . pop_front ( ) else {
90
93
return None ;
91
94
} ;
92
95
let query = self . table . get ( & query_id) . unwrap ( ) ;
@@ -102,7 +105,8 @@ impl State {
102
105
. update_stage_status ( task. task_id . stage_id , StageStatus :: Running ( 0 ) )
103
106
. unwrap ( ) ;
104
107
if let QueryQueueStatus :: Available = guard. get_queue_status ( ) {
105
- self . queue . lock ( ) . await . insert ( duration, query_id) ;
108
+ // self.queue.lock().await.insert(duration, query_id);
109
+ self . queue . lock ( ) . await . push_back ( query_id) ;
106
110
self . notify . notify_waiters ( ) ;
107
111
}
108
112
@@ -129,14 +133,19 @@ impl State {
129
133
_ => unreachable ! ( ) ,
130
134
}
131
135
132
- let new_time = guard. time + SystemTime :: now ( ) . duration_since ( ts) . unwrap ( ) ;
133
136
let mut queue = self . queue . lock ( ) . await ;
134
- let _ = queue. remove ( & guard. time ) ;
135
- if let QueryQueueStatus :: Available = guard. get_queue_status ( ) {
136
- queue. insert ( new_time, task_id. query_id ) ;
137
- self . notify . notify_waiters ( ) ;
137
+ // let new_time = guard.time + SystemTime::now().duration_since(ts).unwrap();
138
+ // let _ = queue.remove(&guard.time);
139
+ // if let QueryQueueStatus::Available = guard.get_queue_status() {
140
+ // queue.insert(new_time, task_id.query_id);
141
+ // self.notify.notify_waiters();
142
+ // }
143
+ // guard.time = new_time;
144
+ if QueryQueueStatus :: Available == guard. get_queue_status ( )
145
+ && !queue. contains ( & task_id. query_id )
146
+ {
147
+ queue. push_back ( task_id. query_id ) ;
138
148
}
139
- guard. time = new_time;
140
149
}
141
150
}
142
151
@@ -148,12 +157,18 @@ impl State {
148
157
#[ cfg( test) ]
149
158
mod tests {
150
159
use rand:: Rng ;
151
- use std:: { fs, time:: { Duration , SystemTime } } ;
152
- use tokio:: { sync:: { Mutex , Notify } , time:: sleep} ;
160
+ use std:: {
161
+ fs,
162
+ time:: { Duration , SystemTime } ,
163
+ } ;
164
+ use tokio:: {
165
+ sync:: { Mutex , Notify } ,
166
+ time:: sleep,
167
+ } ;
153
168
154
- use crate :: { parser:: ExecutionPlanParser , query_graph:: QueryGraph } ;
155
169
use crate :: queue:: State ;
156
170
use crate :: task:: TaskStatus ;
171
+ use crate :: { parser:: ExecutionPlanParser , query_graph:: QueryGraph } ;
157
172
use std:: { cmp:: min, sync:: Arc } ;
158
173
159
174
// Deprecated, use test_queue_conc instead
@@ -282,9 +297,7 @@ mod tests {
282
297
// Add a bunch of queries with staggered submission time
283
298
let start_enqueue = SystemTime :: now ( ) ;
284
299
for plan in long_plans {
285
- queue
286
- . add_query ( Arc :: clone ( & plan) )
287
- . await ;
300
+ queue. add_query ( Arc :: clone ( & plan) ) . await ;
288
301
sleep ( Duration :: from_millis ( 10 ) ) . await ;
289
302
}
290
303
let enq_time = SystemTime :: now ( ) . duration_since ( start_enqueue) . unwrap ( ) ;
0 commit comments