3636import org .apache .doris .journal .bdbje .BDBTool ;
3737import org .apache .doris .journal .bdbje .BDBToolOptions ;
3838import org .apache .doris .persist .meta .MetaReader ;
39+ import org .apache .doris .qe .Coordinator ;
40+ import org .apache .doris .qe .QeProcessorImpl ;
3941import org .apache .doris .qe .QeService ;
4042import org .apache .doris .qe .SimpleScheduler ;
4143import org .apache .doris .service .ExecuteEnv ;
6365import java .nio .channels .OverlappingFileLockException ;
6466import java .nio .file .StandardOpenOption ;
6567import java .time .LocalDate ;
68+ import java .util .List ;
6669import java .util .concurrent .TimeUnit ;
70+ import java .util .concurrent .atomic .AtomicBoolean ;
6771
6872public class DorisFE {
6973 private static final Logger LOG = LogManager .getLogger (DorisFE .class );
@@ -82,6 +86,12 @@ public class DorisFE {
8286 private static FileChannel processLockFileChannel ;
8387 private static FileLock processFileLock ;
8488
89+ // set to true when all servers are ready.
90+ private static final AtomicBoolean serverReady = new AtomicBoolean (false );
91+
92+ // HTTP server instance, used for graceful shutdown
93+ private static HttpServer httpServer ;
94+
8595 public static void main (String [] args ) {
8696 // Every doris version should have a final meta version, it should not change
8797 // between small releases. Add a check here to avoid mistake.
@@ -144,7 +154,19 @@ public static void start(String dorisHomeDir, String pidDir, String[] args, Star
144154 }
145155
146156 Log4jConfig .initLogging (dorisHomeDir + "/conf/" );
147- Runtime .getRuntime ().addShutdownHook (new Thread (LogManager ::shutdown ));
157+ // Add shutdown hook for graceful exit
158+ Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
159+ LOG .info ("Received shutdown signal, starting graceful shutdown..." );
160+ serverReady .set (false );
161+ gracefulShutdown ();
162+
163+ // Shutdown HTTP server after main process graceful shutdown is complete
164+ if (httpServer != null ) {
165+ httpServer .shutdown ();
166+ }
167+
168+ LogManager .shutdown ();
169+ }));
148170
149171 // set dns cache ttl
150172 java .security .Security .setProperty ("networkaddress.cache.ttl" , "60" );
@@ -202,7 +224,7 @@ public static void start(String dorisHomeDir, String pidDir, String[] args, Star
202224 feServer .start ();
203225
204226 if (options .enableHttpServer ) {
205- HttpServer httpServer = new HttpServer ();
227+ httpServer = new HttpServer ();
206228 httpServer .setPort (Config .http_port );
207229 httpServer .setHttpsPort (Config .https_port );
208230 httpServer .setMaxHttpPostSize (Config .jetty_server_max_http_post_size );
@@ -231,11 +253,14 @@ public static void start(String dorisHomeDir, String pidDir, String[] args, Star
231253
232254 ThreadPoolManager .registerAllThreadPoolMetric ();
233255 startMonitor ();
256+
257+ serverReady .set (true );
258+ // JVM will exit when shutdown hook is completed
234259 while (true ) {
235260 Thread .sleep (2000 );
236261 }
237262 } catch (Throwable e ) {
238- // Some exception may thrown before LOG is inited.
263+ // Some exception may throw before LOG is inited.
239264 // So need to print to stdout
240265 e .printStackTrace ();
241266 LOG .error ("" , e );
@@ -584,4 +609,25 @@ public static class StartupOptions {
584609 public boolean enableHttpServer = true ;
585610 public boolean enableQeService = true ;
586611 }
612+
613+ public static boolean isServerReady () {
614+ return serverReady .get ();
615+ }
616+
617+ private static void gracefulShutdown () {
618+ // wait for all queries to finish
619+ try {
620+ long now = System .currentTimeMillis ();
621+ List <Coordinator > allCoordinators = QeProcessorImpl .INSTANCE .getAllCoordinators ();
622+ while (!allCoordinators .isEmpty () && System .currentTimeMillis () - now < 300 * 1000L ) {
623+ Thread .sleep (1000 );
624+ allCoordinators = QeProcessorImpl .INSTANCE .getAllCoordinators ();
625+ LOG .info ("waiting {} queries to finish before shutdown" , allCoordinators .size ());
626+ }
627+ } catch (Throwable t ) {
628+ LOG .error ("" , t );
629+ }
630+
631+ LOG .info ("graceful shutdown finished" );
632+ }
587633}
0 commit comments