diff --git a/core/trino-main/src/main/java/io/trino/exchange/ExchangeManagerRegistry.java b/core/trino-main/src/main/java/io/trino/exchange/ExchangeManagerRegistry.java index 85e287ccb11d..2e50db92d09d 100644 --- a/core/trino-main/src/main/java/io/trino/exchange/ExchangeManagerRegistry.java +++ b/core/trino-main/src/main/java/io/trino/exchange/ExchangeManagerRegistry.java @@ -20,6 +20,7 @@ import io.trino.spi.exchange.ExchangeManager; import io.trino.spi.exchange.ExchangeManagerFactory; +import javax.annotation.PreDestroy; import javax.inject.Inject; import java.io.File; @@ -106,6 +107,19 @@ public ExchangeManager getExchangeManager() return exchangeManager; } + @PreDestroy + public void shutdown() + { + try { + if (this.exchangeManager != null) { + exchangeManager.shutdown(); + } + } + catch (Throwable t) { + log.error(t, "Error shutting down exchange manager: %s", exchangeManager); + } + } + private static Map loadProperties(File configFile) { try { diff --git a/core/trino-spi/src/main/java/io/trino/spi/exchange/ExchangeManager.java b/core/trino-spi/src/main/java/io/trino/spi/exchange/ExchangeManager.java index 2399bcdb136f..ca9c6d1c836c 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/exchange/ExchangeManager.java +++ b/core/trino-spi/src/main/java/io/trino/spi/exchange/ExchangeManager.java @@ -73,4 +73,13 @@ public interface ExchangeManager * @return {@link ExchangeSource} used by the engine to read data from an exchange */ ExchangeSource createSource(); + + /** + * Shutdown the exchange manager by releasing any held resources such as + * threads, sockets, etc. This method will only be called when no + * queries are using the exchange manager. After this method is called, + * no methods will be called on the exchange manager or any objects obtained + * from exchange manager. + */ + default void shutdown() {} }