@@ -84,33 +84,41 @@ MPPGatherTaskSetPtr MPPQuery::addMPPGatherTaskSet(const MPPGatherId & gather_id)
8484 return ptr;
8585}
8686
87- void MPPTaskMonitor::waitAllMPPTasksFinish (const std::unique_ptr<Context> & global_context )
87+ void MPPTaskMonitor::waitAllMPPTasksFinish (const std::unique_ptr<Context> & context )
8888{
8989 // The maximum seconds TiFlash will wait for all current MPP tasks to finish before shutting down
9090 static constexpr const char * GRACEFUL_WIAT_BEFORE_SHUTDOWN = " flash.graceful_wait_before_shutdown" ;
9191 // The default value of flash.graceful_wait_before_shutdown
9292 static constexpr UInt64 DEFAULT_GRACEFUL_WAIT_BEFORE_SHUTDOWN = 600 ;
93- auto graceful_wait_before_shutdown = global_context->getUsersConfig ()->getUInt64 (
94- GRACEFUL_WIAT_BEFORE_SHUTDOWN,
95- DEFAULT_GRACEFUL_WAIT_BEFORE_SHUTDOWN);
93+ auto graceful_wait_before_shutdown
94+ = context->getUsersConfig ()->getUInt64 (GRACEFUL_WIAT_BEFORE_SHUTDOWN, DEFAULT_GRACEFUL_WAIT_BEFORE_SHUTDOWN);
9695 LOG_INFO (log, " Start to wait all MPPTasks to finish, timeout={}s" , graceful_wait_before_shutdown);
96+ UInt64 graceful_wait_before_shutdown_ms = graceful_wait_before_shutdown * 1000 ;
9797 Stopwatch watch;
9898 // The first sleep before checking to reduce the chance of missing MPP tasks that are still in the process of being dispatched
9999 std::this_thread::sleep_for (std::chrono::seconds (1 ));
100+ bool all_tasks_finished = false ;
100101 while (true )
101102 {
102103 auto elapsed_ms = watch.elapsedMilliseconds ();
104+ if (!all_tasks_finished)
103105 {
104106 std::unique_lock lock (mu);
105107 if (monitored_tasks.empty ())
108+ all_tasks_finished = true ;
109+ }
110+ if (all_tasks_finished)
111+ {
112+ // Also needs to check if all MPP gRPC connections are finished
113+ if (GET_METRIC (tiflash_coprocessor_handling_request_count, type_mpp_establish_conn).Value () == 0 )
106114 {
107- LOG_INFO (log, " All MPPTasks have finished after {}ms" , elapsed_ms);
115+ LOG_INFO (log, " All MPP tasks have finished after {}ms" , elapsed_ms);
108116 break ;
109117 }
110118 }
111- if (elapsed_ms >= graceful_wait_before_shutdown * 1000 )
119+ if (elapsed_ms >= graceful_wait_before_shutdown_ms )
112120 {
113- LOG_WARNING (log, " Timed out waiting for MPP tasks to finish after {}ms" , elapsed_ms);
121+ LOG_WARNING (log, " Timed out waiting for all MPP tasks to finish after {}ms" , elapsed_ms);
114122 break ;
115123 }
116124 std::this_thread::sleep_for (std::chrono::milliseconds (200 ));
0 commit comments