@@ -53,6 +53,9 @@ use databend_common_sql::PlanExtras;
53
53
use log:: info;
54
54
use parking_lot:: Mutex ;
55
55
use pin_project_lite:: pin_project;
56
+ use tokio:: sync:: AcquireError as TokioAcquireError ;
57
+ use tokio:: sync:: OwnedSemaphorePermit ;
58
+ use tokio:: sync:: Semaphore ;
56
59
use tokio:: time:: error:: Elapsed ;
57
60
58
61
use crate :: sessions:: QueryContext ;
@@ -87,6 +90,8 @@ pub(crate) struct Inner<Data: QueueData> {
87
90
pub struct QueueManager < Data : QueueData > {
88
91
permits : usize ,
89
92
meta_store : MetaStore ,
93
+ semaphore : Arc < Semaphore > ,
94
+ global_statement_queue : bool ,
90
95
queue : Mutex < HashMap < Data :: Key , Inner < Data > > > ,
91
96
}
92
97
@@ -101,23 +106,33 @@ impl<Data: QueueData> QueueManager<Data> {
101
106
} ;
102
107
103
108
info ! ( "queue manager permits: {:?}" , permits) ;
104
- GlobalInstance :: set ( Self :: create ( permits, metastore) ) ;
109
+ GlobalInstance :: set ( Self :: create (
110
+ permits,
111
+ metastore,
112
+ conf. query . global_statement_queue ,
113
+ ) ) ;
105
114
Ok ( ( ) )
106
115
}
107
116
108
117
pub fn instance ( ) -> Arc < Self > {
109
118
GlobalInstance :: get :: < Arc < Self > > ( )
110
119
}
111
120
112
- pub fn create ( mut permits : usize , meta_store : MetaStore ) -> Arc < QueueManager < Data > > {
121
+ pub fn create (
122
+ mut permits : usize ,
123
+ meta_store : MetaStore ,
124
+ global_statement_queue : bool ,
125
+ ) -> Arc < QueueManager < Data > > {
113
126
if permits == 0 {
114
127
permits = usize:: MAX >> 4 ;
115
128
}
116
129
117
130
Arc :: new ( QueueManager {
118
131
permits,
119
132
meta_store,
133
+ global_statement_queue,
120
134
queue : Mutex :: new ( HashMap :: new ( ) ) ,
135
+ semaphore : Arc :: new ( Semaphore :: new ( permits) ) ,
121
136
} )
122
137
}
123
138
@@ -156,21 +171,35 @@ impl<Data: QueueData> QueueManager<Data> {
156
171
) ;
157
172
158
173
let timeout = data. timeout ( ) ;
159
- let semaphore_acquire = self . meta_store . new_acquired (
160
- data. get_lock_key ( ) ,
161
- self . permits as u64 ,
162
- data. get_key ( ) , // ID of this acquirer
163
- data. lock_ttl ( ) ,
164
- ) ;
165
174
166
- let future = AcquireQueueFuture :: create (
167
- Arc :: new ( data) ,
168
- tokio:: time:: timeout ( timeout, semaphore_acquire) ,
169
- self . clone ( ) ,
170
- ) ;
171
175
let start_time = SystemTime :: now ( ) ;
176
+ let acquire_res = match self . global_statement_queue {
177
+ true => {
178
+ let semaphore_acquire = self . meta_store . new_acquired (
179
+ data. get_lock_key ( ) ,
180
+ self . permits as u64 ,
181
+ data. get_key ( ) , // ID of this acquirer
182
+ data. lock_ttl ( ) ,
183
+ ) ;
172
184
173
- return match future. await {
185
+ AcquireQueueFuture :: create (
186
+ Arc :: new ( data) ,
187
+ tokio:: time:: timeout ( timeout, semaphore_acquire) ,
188
+ self . clone ( ) ,
189
+ )
190
+ . await
191
+ }
192
+ false => {
193
+ AcquireQueueFuture :: create (
194
+ Arc :: new ( data) ,
195
+ tokio:: time:: timeout ( timeout, self . semaphore . clone ( ) . acquire_owned ( ) ) ,
196
+ self . clone ( ) ,
197
+ )
198
+ . await
199
+ }
200
+ } ;
201
+
202
+ return match acquire_res {
174
203
Ok ( v) => {
175
204
info ! ( "finished acquiring from queue, length: {}" , self . length( ) ) ;
176
205
@@ -197,7 +226,7 @@ impl<Data: QueueData> QueueManager<Data> {
197
226
} ;
198
227
}
199
228
200
- Ok ( AcquireQueueGuard :: create ( None ) )
229
+ Ok ( AcquireQueueGuard :: create_global ( None ) )
201
230
}
202
231
203
232
pub ( crate ) fn add_entity ( & self , inner : Inner < Data > ) -> Data :: Key {
@@ -231,28 +260,35 @@ impl<Data: QueueData> QueueManager<Data> {
231
260
}
232
261
}
233
262
234
- pub struct AcquireQueueGuard {
235
- # [ allow ( dead_code ) ]
236
- permit : Option < Permit > ,
263
+ pub enum AcquireQueueGuard {
264
+ Global ( Option < Permit > ) ,
265
+ Local ( Option < OwnedSemaphorePermit > ) ,
237
266
}
238
267
239
268
impl Drop for AcquireQueueGuard {
240
269
fn drop ( & mut self ) {
241
- if self . permit . is_some ( ) {
242
- dec_session_running_acquired_queries ( ) ;
270
+ match self {
271
+ AcquireQueueGuard :: Local ( Some ( _) ) | AcquireQueueGuard :: Global ( Some ( _) ) => {
272
+ dec_session_running_acquired_queries ( ) ;
273
+ }
274
+ _ => { }
243
275
}
244
276
}
245
277
}
246
278
247
279
impl AcquireQueueGuard {
248
- pub fn create ( permit : Option < Permit > ) -> Self {
249
- AcquireQueueGuard { permit }
280
+ pub fn create_global ( permit : Option < Permit > ) -> Self {
281
+ AcquireQueueGuard :: Global ( permit)
282
+ }
283
+
284
+ pub fn create_local ( permit : Option < OwnedSemaphorePermit > ) -> Self {
285
+ AcquireQueueGuard :: Local ( permit)
250
286
}
251
287
}
252
288
253
289
pin_project ! {
254
- pub struct AcquireQueueFuture <Data : QueueData , T >
255
- where T : Future <Output = std:: result:: Result <std:: result:: Result <Permit , AcquireError >, Elapsed >>
290
+ pub struct AcquireQueueFuture <Data : QueueData , T , Permit , E >
291
+ where T : Future <Output = std:: result:: Result <std:: result:: Result <Permit , E >, Elapsed >>
256
292
{
257
293
#[ pin]
258
294
inner: T ,
@@ -266,8 +302,8 @@ where T: Future<Output = std::result::Result<std::result::Result<Permit, Acquir
266
302
}
267
303
}
268
304
269
- impl < Data : QueueData , T > AcquireQueueFuture < Data , T >
270
- where T : Future < Output = std:: result:: Result < std:: result:: Result < Permit , AcquireError > , Elapsed > >
305
+ impl < Data : QueueData , T , Permit , E > AcquireQueueFuture < Data , T , Permit , E >
306
+ where T : Future < Output = std:: result:: Result < std:: result:: Result < Permit , E > , Elapsed > >
271
307
{
272
308
pub fn create ( data : Arc < Data > , inner : T , mgr : Arc < QueueManager < Data > > ) -> Self {
273
309
AcquireQueueFuture {
@@ -281,53 +317,60 @@ where T: Future<Output = std::result::Result<std::result::Result<Permit, Acquire
281
317
}
282
318
}
283
319
284
- impl < Data : QueueData , T > Future for AcquireQueueFuture < Data , T >
285
- where T : Future < Output = std:: result:: Result < std:: result:: Result < Permit , AcquireError > , Elapsed > >
286
- {
287
- type Output = Result < AcquireQueueGuard > ;
320
+ macro_rules! impl_acquire_queue_future {
321
+ ( $Permit: ty, $fn_name: ident, $Error: ty) => {
322
+ impl <Data : QueueData , T > Future for AcquireQueueFuture <Data , T , $Permit, $Error>
323
+ where T : Future <Output = std:: result:: Result <std:: result:: Result <$Permit, $Error>, Elapsed >>
324
+ {
325
+ type Output = Result <AcquireQueueGuard >;
288
326
289
- fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
290
- let this = self . project ( ) ;
327
+ fn poll( self : Pin <& mut Self >, cx: & mut Context <' _>) -> Poll <Self :: Output > {
328
+ let this = self . project( ) ;
291
329
292
- if this. is_abort . load ( Ordering :: SeqCst ) {
293
- return Poll :: Ready ( Err ( Data :: remove_error_message ( this. key . take ( ) ) ) ) ;
294
- }
330
+ if this. is_abort. load( Ordering :: SeqCst ) {
331
+ return Poll :: Ready ( Err ( Data :: remove_error_message( this. key. take( ) ) ) ) ;
332
+ }
333
+
334
+ match this. inner. poll( cx) {
335
+ Poll :: Ready ( res) => {
336
+ if let Some ( key) = this. key. take( ) {
337
+ if this. manager. remove_entity( & key) . is_none( ) {
338
+ return Poll :: Ready ( Err ( Data :: remove_error_message( Some ( key) ) ) ) ;
339
+ }
340
+ }
295
341
296
- match this . inner . poll ( cx ) {
297
- Poll :: Ready ( res ) => {
298
- if let Some ( key ) = this . key . take ( ) {
299
- if this . manager . remove_entity ( & key ) . is_none ( ) {
300
- return Poll :: Ready ( Err ( Data :: remove_error_message ( Some ( key ) ) ) ) ;
342
+ Poll :: Ready ( match res {
343
+ Ok ( Ok ( v ) ) => Ok ( AcquireQueueGuard :: $fn_name ( Some ( v ) ) ) ,
344
+ Ok ( Err ( _ ) ) => Err ( ErrorCode :: TokioError ( "acquire queue failure." ) ) ,
345
+ Err ( _elapsed ) => Err ( ErrorCode :: Timeout ( "query queuing timeout" ) ) ,
346
+ } )
301
347
}
302
- }
348
+ Poll :: Pending => {
349
+ if !* this. has_pending {
350
+ * this. has_pending = true ;
351
+ }
303
352
304
- Poll :: Ready ( match res {
305
- Ok ( Ok ( v) ) => Ok ( AcquireQueueGuard :: create ( Some ( v) ) ) ,
306
- Ok ( Err ( _) ) => Err ( ErrorCode :: TokioError ( "acquire queue failure." ) ) ,
307
- Err ( _elapsed) => Err ( ErrorCode :: Timeout ( "query queuing timeout" ) ) ,
308
- } )
309
- }
310
- Poll :: Pending => {
311
- if !* this. has_pending {
312
- * this. has_pending = true ;
313
- }
353
+ if let Some ( data) = this. data. take( ) {
354
+ let waker = cx. waker( ) . clone( ) ;
355
+ * this. key = Some ( this. manager. add_entity( Inner {
356
+ data,
357
+ waker,
358
+ instant: Instant :: now( ) ,
359
+ is_abort: this. is_abort. clone( ) ,
360
+ } ) ) ;
361
+ }
314
362
315
- if let Some ( data) = this. data . take ( ) {
316
- let waker = cx. waker ( ) . clone ( ) ;
317
- * this. key = Some ( this. manager . add_entity ( Inner {
318
- data,
319
- waker,
320
- instant : Instant :: now ( ) ,
321
- is_abort : this. is_abort . clone ( ) ,
322
- } ) ) ;
363
+ Poll :: Pending
364
+ }
323
365
}
324
-
325
- Poll :: Pending
326
366
}
327
367
}
328
- }
368
+ } ;
329
369
}
330
370
371
+ impl_acquire_queue_future ! ( Permit , create_global, AcquireError ) ;
372
+ impl_acquire_queue_future ! ( OwnedSemaphorePermit , create_local, TokioAcquireError ) ;
373
+
331
374
pub struct QueryEntry {
332
375
ctx : Arc < QueryContext > ,
333
376
pub query_id : String ,
0 commit comments