12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
+ use std:: future:: Future ;
15
16
use std:: sync:: Arc ;
16
17
17
18
use common_base:: base:: tokio:: sync:: Semaphore ;
@@ -56,11 +57,7 @@ impl SegmentsIO {
56
57
return Ok ( vec ! [ ] ) ;
57
58
}
58
59
59
- let ctx = self . ctx . clone ( ) ;
60
- let max_runtime_threads = ctx. get_settings ( ) . get_max_threads ( ) ? as usize ;
61
- let max_io_requests = ctx. get_settings ( ) . get_max_storage_io_requests ( ) ? as usize ;
62
-
63
- // 1.1 combine all the tasks.
60
+ // combine all the tasks.
64
61
let mut iter = segment_locations. iter ( ) ;
65
62
let tasks = std:: iter:: from_fn ( move || {
66
63
if let Some ( location) = iter. next ( ) {
@@ -74,22 +71,42 @@ impl SegmentsIO {
74
71
}
75
72
} ) ;
76
73
77
- // 1.2 build the runtime.
78
- let semaphore = Semaphore :: new ( max_io_requests) ;
79
- let segments_runtime = Arc :: new ( Runtime :: with_worker_threads (
80
- max_runtime_threads,
81
- Some ( "fuse-req-segments-worker" . to_owned ( ) ) ,
82
- ) ?) ;
74
+ try_join_futures (
75
+ self . ctx . clone ( ) ,
76
+ tasks,
77
+ "fuse-req-segments-worker" . to_owned ( ) ,
78
+ )
79
+ . await
80
+ }
81
+ }
83
82
84
- // 1.3 spawn all the tasks to the runtime.
85
- let join_handlers = segments_runtime. try_spawn_batch ( semaphore, tasks) . await ?;
83
+ pub async fn try_join_futures < Fut > (
84
+ ctx : Arc < dyn TableContext > ,
85
+ futures : impl IntoIterator < Item = Fut > ,
86
+ thread_name : String ,
87
+ ) -> Result < Vec < Fut :: Output > >
88
+ where
89
+ Fut : Future + Send + ' static ,
90
+ Fut :: Output : Send + ' static ,
91
+ {
92
+ let max_runtime_threads = ctx. get_settings ( ) . get_max_threads ( ) ? as usize ;
93
+ let max_io_requests = ctx. get_settings ( ) . get_max_storage_io_requests ( ) ? as usize ;
86
94
87
- // 1.4 get all the result.
88
- let joint = future:: try_join_all ( join_handlers)
89
- . instrument ( tracing:: debug_span!( "read_segments_join_all" ) )
90
- . await
91
- . map_err ( |e| ErrorCode :: StorageOther ( format ! ( "read segments failure, {}" , e) ) ) ?;
95
+ // 1. build the runtime.
96
+ let semaphore = Semaphore :: new ( max_io_requests) ;
97
+ let segments_runtime = Arc :: new ( Runtime :: with_worker_threads (
98
+ max_runtime_threads,
99
+ Some ( thread_name) ,
100
+ ) ?) ;
92
101
93
- Ok ( joint)
94
- }
102
+ // 2. spawn all the tasks to the runtime.
103
+ let join_handlers = segments_runtime. try_spawn_batch ( semaphore, futures) . await ?;
104
+
105
+ // 3. get all the result.
106
+ let joint = future:: try_join_all ( join_handlers)
107
+ . instrument ( tracing:: debug_span!( "try_join_futures_all" ) )
108
+ . await
109
+ . map_err ( |e| ErrorCode :: StorageOther ( format ! ( "try join futures failure, {}" , e) ) ) ?;
110
+
111
+ Ok ( joint)
95
112
}
0 commit comments