-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract GracefulExecutorServicesShutdown and use it in PulsarService
- handle graceful / forcefully shutdown also for PulsarService executors
- Loading branch information
Showing
5 changed files
with
324 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...oker/src/main/java/org/apache/pulsar/broker/service/GracefulExecutorServicesShutdown.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.broker.service; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
|
||
/** | ||
* This a builder like class for providing a fluent API for graceful shutdown | ||
* | ||
* Executors are added with the {@link #shutdown(ExecutorService...)} | ||
* method. The {@link ExecutorService#shutdown()} method is called immediately. | ||
* | ||
* Calling the {@link #handle()} method returns a future which completes when all executors | ||
* have been terminated. The executors will be polled frequently for completion. If the shutdown times out | ||
* or the future is cancelled, all executors will be terminated. | ||
*/ | ||
public class GracefulExecutorServicesShutdown { | ||
private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(15); | ||
private final List<ExecutorService> executorServices = new ArrayList<>(); | ||
private Duration timeout = DEFAULT_TIMEOUT; | ||
|
||
private GracefulExecutorServicesShutdown() { | ||
|
||
} | ||
|
||
/** | ||
* Initiates a new shutdown for one or many {@link ExecutorService}s. | ||
* | ||
* @return a new instance for controlling graceful shutdown | ||
*/ | ||
public static GracefulExecutorServicesShutdown initiate() { | ||
return new GracefulExecutorServicesShutdown(); | ||
} | ||
|
||
/** | ||
* Calls {@link ExecutorService#shutdown()} and enlists the executor as part of the | ||
* shutdown handling. | ||
* | ||
* @param executorServices one or many executors to shutdown | ||
* @return the current instance for controlling graceful shutdown | ||
*/ | ||
public GracefulExecutorServicesShutdown shutdown(ExecutorService... executorServices) { | ||
for (ExecutorService executorService : executorServices) { | ||
if (executorService != null) { | ||
executorService.shutdown(); | ||
this.executorServices.add(executorService); | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the timeout for graceful shutdown. | ||
* | ||
* @param timeout duration for the timeout | ||
* @return the current instance for controlling graceful shutdown | ||
*/ | ||
public GracefulExecutorServicesShutdown timeout(Duration timeout) { | ||
this.timeout = timeout; | ||
return this; | ||
} | ||
|
||
/** | ||
* Starts the handler for polling frequently for the completion of enlisted executors. | ||
* | ||
* If the shutdown times out or the future is cancelled, all executors will be terminated. | ||
* Supports {@link CompletableFuture#cancel(boolean)} for forceful shutdown of executors. | ||
* | ||
* @return a future for completion of executors | ||
*/ | ||
public CompletableFuture<Void> handle() { | ||
return new GracefulExecutorServicesShutdownHandler(timeout.toMillis(), executorServices) | ||
.startShutdownHandler(); | ||
} | ||
} |
Oops, something went wrong.