@@ -27,14 +27,14 @@ use databend_common_exception::Result;
27
27
use databend_common_exception:: ResultExt ;
28
28
use futures:: future;
29
29
use futures:: FutureExt ;
30
+ use log:: info;
30
31
use log:: warn;
31
32
use tokio:: runtime:: Builder ;
32
33
use tokio:: runtime:: Handle ;
33
34
use tokio:: sync:: oneshot;
34
35
use tokio:: sync:: OwnedSemaphorePermit ;
35
36
use tokio:: sync:: Semaphore ;
36
37
37
- // use tokio::task::JoinHandle;
38
38
use crate :: runtime:: catch_unwind:: CatchUnwindFuture ;
39
39
use crate :: runtime:: drop_guard;
40
40
use crate :: runtime:: memory:: MemStat ;
@@ -88,7 +88,7 @@ pub trait TrySpawn {
88
88
///
89
89
/// It allows to return an error before spawning the task.
90
90
#[ track_caller]
91
- fn try_spawn < T > ( & self , task : T ) -> Result < JoinHandle < T :: Output > >
91
+ fn try_spawn < T > ( & self , task : T , name : Option < String > ) -> Result < JoinHandle < T :: Output > >
92
92
where
93
93
T : Future + Send + ' static ,
94
94
T :: Output : Send + ' static ;
@@ -102,18 +102,18 @@ pub trait TrySpawn {
102
102
T : Future + Send + ' static ,
103
103
T :: Output : Send + ' static ,
104
104
{
105
- self . try_spawn ( task) . unwrap ( )
105
+ self . try_spawn ( task, None ) . unwrap ( )
106
106
}
107
107
}
108
108
109
109
impl < S : TrySpawn > TrySpawn for Arc < S > {
110
110
#[ track_caller]
111
- fn try_spawn < T > ( & self , task : T ) -> Result < JoinHandle < T :: Output > >
111
+ fn try_spawn < T > ( & self , task : T , name : Option < String > ) -> Result < JoinHandle < T :: Output > >
112
112
where
113
113
T : Future + Send + ' static ,
114
114
T :: Output : Send + ' static ,
115
115
{
116
- self . as_ref ( ) . try_spawn ( task)
116
+ self . as_ref ( ) . try_spawn ( task, name )
117
117
}
118
118
119
119
#[ track_caller]
@@ -149,10 +149,14 @@ impl Runtime {
149
149
150
150
let handle = runtime. handle ( ) . clone ( ) ;
151
151
152
+ let n = name. clone ( ) ;
152
153
// Block the runtime to shutdown.
153
154
let join_handler = Thread :: spawn ( move || {
154
- // We ignore channel is closed.
155
155
let _ = runtime. block_on ( recv_stop) ;
156
+ info ! (
157
+ "Runtime({:?}) received shutdown signal, start to shut down" ,
158
+ n
159
+ ) ;
156
160
157
161
match !cfg ! ( debug_assertions) {
158
162
true => false ,
@@ -257,7 +261,11 @@ impl Runtime {
257
261
#[ allow( clippy:: disallowed_methods) ]
258
262
tokio:: task:: block_in_place ( || {
259
263
self . handle
260
- . block_on ( location_future ( future, std:: panic:: Location :: caller ( ) ) )
264
+ . block_on ( location_future (
265
+ future,
266
+ std:: panic:: Location :: caller ( ) ,
267
+ None ,
268
+ ) )
261
269
. with_context ( || "failed to block on future" . to_string ( ) )
262
270
. flatten ( )
263
271
} )
@@ -348,20 +356,28 @@ impl Runtime {
348
356
349
357
impl TrySpawn for Runtime {
350
358
#[ track_caller]
351
- fn try_spawn < T > ( & self , task : T ) -> Result < JoinHandle < T :: Output > >
359
+ fn try_spawn < T > ( & self , task : T , name : Option < String > ) -> Result < JoinHandle < T :: Output > >
352
360
where
353
361
T : Future + Send + ' static ,
354
362
T :: Output : Send + ' static ,
355
363
{
356
364
let task = ThreadTracker :: tracking_future ( task) ;
357
- let task = match ThreadTracker :: query_id ( ) {
358
- None => async_backtrace:: location!( String :: from( GLOBAL_TASK_DESC ) ) . frame ( task) ,
359
- Some ( query_id) => {
360
- async_backtrace:: location!( format!( "Running query {} spawn task" , query_id) )
361
- . frame ( task)
365
+
366
+ let location_name = {
367
+ if let Some ( name) = name {
368
+ name
369
+ } else {
370
+ match ThreadTracker :: query_id ( ) {
371
+ None => String :: from ( GLOBAL_TASK_DESC ) ,
372
+ Some ( query_id) => {
373
+ format ! ( "Running query {} spawn task" , query_id)
374
+ }
375
+ }
362
376
}
363
377
} ;
364
378
379
+ let task = async_backtrace:: location!( location_name) . frame ( task) ;
380
+
365
381
#[ expect( clippy:: disallowed_methods) ]
366
382
Ok ( JoinHandle :: create ( self . handle . spawn ( task) ) )
367
383
}
@@ -380,6 +396,7 @@ impl Drop for Dropper {
380
396
// Send a signal to say i am dropping.
381
397
if let Some ( close_sender) = self . close . take ( ) {
382
398
if close_sender. send ( ( ) ) . is_ok ( ) {
399
+ info ! ( "close_sender to shutdown Runtime is sent" ) ;
383
400
match self . join_handler . take ( ) . unwrap ( ) . join ( ) {
384
401
Err ( e) => warn ! ( "Runtime dropper panic, {:?}" , e) ,
385
402
Ok ( true ) => {
@@ -436,7 +453,25 @@ where
436
453
F :: Output : Send + ' static ,
437
454
{
438
455
#[ expect( clippy:: disallowed_methods) ]
439
- tokio:: spawn ( location_future ( future, std:: panic:: Location :: caller ( ) ) )
456
+ tokio:: spawn ( location_future (
457
+ future,
458
+ std:: panic:: Location :: caller ( ) ,
459
+ None ,
460
+ ) )
461
+ }
462
+
463
+ #[ track_caller]
464
+ pub fn spawn_named < F > ( future : F , name : String ) -> tokio:: task:: JoinHandle < F :: Output >
465
+ where
466
+ F : Future + Send + ' static ,
467
+ F :: Output : Send + ' static ,
468
+ {
469
+ #[ expect( clippy:: disallowed_methods) ]
470
+ tokio:: spawn ( location_future (
471
+ future,
472
+ std:: panic:: Location :: caller ( ) ,
473
+ Some ( name) ,
474
+ ) )
440
475
}
441
476
442
477
#[ track_caller]
@@ -446,7 +481,11 @@ where
446
481
F :: Output : Send + ' static ,
447
482
{
448
483
#[ expect( clippy:: disallowed_methods) ]
449
- tokio:: task:: spawn_local ( location_future ( future, std:: panic:: Location :: caller ( ) ) )
484
+ tokio:: task:: spawn_local ( location_future (
485
+ future,
486
+ std:: panic:: Location :: caller ( ) ,
487
+ None ,
488
+ ) )
450
489
}
451
490
452
491
#[ track_caller]
@@ -476,8 +515,11 @@ where
476
515
pub fn block_on < F : Future > ( future : F ) -> F :: Output {
477
516
#[ expect( clippy:: disallowed_methods) ]
478
517
tokio:: task:: block_in_place ( || {
479
- tokio:: runtime:: Handle :: current ( )
480
- . block_on ( location_future ( future, std:: panic:: Location :: caller ( ) ) )
518
+ tokio:: runtime:: Handle :: current ( ) . block_on ( location_future (
519
+ future,
520
+ std:: panic:: Location :: caller ( ) ,
521
+ None ,
522
+ ) )
481
523
} )
482
524
}
483
525
@@ -487,14 +529,19 @@ pub fn try_block_on<F: Future>(future: F) -> std::result::Result<F::Output, F> {
487
529
Err ( _) => Err ( future) ,
488
530
#[ expect( clippy:: disallowed_methods) ]
489
531
Ok ( handler) => Ok ( tokio:: task:: block_in_place ( || {
490
- handler. block_on ( location_future ( future, std:: panic:: Location :: caller ( ) ) )
532
+ handler. block_on ( location_future (
533
+ future,
534
+ std:: panic:: Location :: caller ( ) ,
535
+ None ,
536
+ ) )
491
537
} ) ) ,
492
538
}
493
539
}
494
540
495
541
fn location_future < F > (
496
542
future : F ,
497
543
frame_location : & ' static Location ,
544
+ frame_name : Option < String > ,
498
545
) -> impl Future < Output = F :: Output >
499
546
where
500
547
F : Future ,
@@ -506,9 +553,13 @@ where
506
553
// TODO: tracking payload
507
554
let future = ThreadTracker :: tracking_future ( future) ;
508
555
509
- let frame_name = std:: any:: type_name :: < F > ( )
510
- . trim_end_matches ( "::{{closure}}" )
511
- . to_string ( ) ;
556
+ let frame_name = if let Some ( n) = frame_name {
557
+ n
558
+ } else {
559
+ std:: any:: type_name :: < F > ( )
560
+ . trim_end_matches ( "::{{closure}}" )
561
+ . to_string ( )
562
+ } ;
512
563
513
564
async_backtrace:: location!(
514
565
frame_name,
0 commit comments