diff --git a/amoro-ams/src/main/java/org/apache/amoro/server/AmoroServiceContainer.java b/amoro-ams/src/main/java/org/apache/amoro/server/AmoroServiceContainer.java index 997f7b78cf..c9d205a077 100644 --- a/amoro-ams/src/main/java/org/apache/amoro/server/AmoroServiceContainer.java +++ b/amoro-ams/src/main/java/org/apache/amoro/server/AmoroServiceContainer.java @@ -126,11 +126,14 @@ public static void main(String[] args) { service.dispose(); LOG.info("AMS service has been shut down"); })); + service.startRestServices(); while (true) { try { service.waitLeaderShip(); - service.startService(); + service.startOptimizingService(); service.waitFollowerShip(); + // become follower, dispose optimizingService stop + service.stopOptimizingService(); } catch (Exception e) { LOG.error("AMS start error", e); } finally { @@ -143,6 +146,14 @@ public static void main(String[] args) { } } + private void stopOptimizingService() { + if (optimizingService != null) { + LOG.info("AMS Become follower and Stopping optimizing service..."); + optimizingService.dispose(); + optimizingService = null; + } + } + public void waitLeaderShip() throws Exception { haContainer.waitLeaderShip(); } @@ -151,14 +162,21 @@ public void waitFollowerShip() throws Exception { haContainer.waitFollowerShip(); } - public void startService() throws Exception { + public void startRestServices() throws Exception { EventsManager.getInstance(); MetricManager.getInstance(); catalogManager = new DefaultCatalogManager(serviceConfig); tableManager = new DefaultTableManager(serviceConfig, catalogManager); optimizerManager = new DefaultOptimizerManager(serviceConfig, catalogManager); + terminalManager = new TerminalManager(serviceConfig, catalogManager); + initHttpService(); + startHttpService(); + registerAmsServiceMetric(); + } + + public void startOptimizingService() throws Exception { tableService = new DefaultTableService(serviceConfig, catalogManager); optimizingService = @@ -180,14 +198,9 @@ public void startService() throws Exception { tableService.initialize(); LOG.info("AMS table service have been initialized"); tableManager.setTableService(tableService); - terminalManager = new TerminalManager(serviceConfig, catalogManager); initThriftService(); startThriftService(); - - initHttpService(); - startHttpService(); - registerAmsServiceMetric(); } private void addHandlerChain(RuntimeHandlerChain chain) { diff --git a/amoro-ams/src/test/java/org/apache/amoro/server/AmsEnvironment.java b/amoro-ams/src/test/java/org/apache/amoro/server/AmsEnvironment.java index f20e6d1d55..9102929b30 100644 --- a/amoro-ams/src/test/java/org/apache/amoro/server/AmsEnvironment.java +++ b/amoro-ams/src/test/java/org/apache/amoro/server/AmsEnvironment.java @@ -349,7 +349,8 @@ private void startAms() throws Exception { serviceConfig.set( AmoroManagementConf.REFRESH_EXTERNAL_CATALOGS_INTERVAL, Duration.ofMillis(1000L)); - serviceContainer.startService(); + serviceContainer.startRestServices(); + serviceContainer.startOptimizingService(); break; } catch (TTransportException e) { if (e.getCause() instanceof BindException) {