Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop transport listeners asynchronously during server shutdown #3661

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@
import javax.management.QueryExp;
import java.io.File;
import java.lang.management.ManagementPermission;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
* Class for handling Server management functionalilty.
Expand Down Expand Up @@ -75,13 +81,44 @@ public void startMaintenance() throws Exception {
secMan.checkPermission(new ManagementPermission("control"));
}
log.info("Starting to switch to maintenance mode...");
stopTransportListeners();
destroyTransportListeners();
waitForRequestCompletion();
}

/**
* Stop Transport Listeners asynchronously and wait for the completion of the tasks
*/
private void stopTransportListeners() {
ExecutorService transportListenerShutdownPool = Executors.newFixedThreadPool(inTransports.size());
List<Future<Void>> listenerShutdownFutures = new ArrayList<>();
for (TransportInDescription tinDesc : inTransports.values()) {
TransportListener transport = tinDesc.getReceiver();
transport.stop();
Future<Void> future = transportListenerShutdownPool.submit(new TransportListenerShutdownTask(transport));
listenerShutdownFutures.add(future);
}

// Wait until shutting down the transport listeners before proceeding
for (Future<Void> future : listenerShutdownFutures) {
try {
future.get();
} catch (Exception e) {
log.error("Error while completing transport listener shutdown", e);
}
}
transportListenerShutdownPool.shutdown();
log.info("Stopped all transport listeners");
}

waitForRequestCompletion();
/**
* Destroy Transport Listeners
*/
private void destroyTransportListeners() {
// Destroy the TransportListener at the end to clear up resources
for (TransportInDescription tinDesc : inTransports.values()) {
TransportListener transport = tinDesc.getReceiver();
transport.destroy();
}
}

/**
Expand Down Expand Up @@ -264,4 +301,24 @@ public void endMaintenance() throws Exception {
}
log.info("Switched to normal mode");
}

/**
* Callable task to pause and shutdown a transport listener
*/
private class TransportListenerShutdownTask implements Callable<Void> {
private TransportListener transport;

public TransportListenerShutdownTask(TransportListener transport) {
this.transport = transport;
}

public Void call() throws Exception {
try {
transport.stop();
} catch (Exception e) {
log.error("Error while stopping Transport Listener", e);
}
return null;
}
}
}