-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(all):tune the sequence of resources closure #5447
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
Changes from all commits
277c7a1
a1107c3
63a4dc5
de5c9ed
754d9ce
567357c
d3d33b0
06b333b
fc95152
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * java-tron is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * java-tron is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package org.tron.common.application; | ||
|
|
||
| import com.google.common.base.Objects; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.eclipse.jetty.server.Server; | ||
|
|
||
| @Slf4j(topic = "rpc") | ||
| public abstract class HttpService implements Service { | ||
|
|
||
| protected Server apiServer; | ||
| protected int port; | ||
|
|
||
| @Override | ||
| public void blockUntilShutdown() { | ||
| if (apiServer != null) { | ||
| try { | ||
| apiServer.join(); | ||
| } catch (InterruptedException e) { | ||
| logger.warn("{}", e.getMessage()); | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void start() { | ||
| if (apiServer != null) { | ||
| try { | ||
| apiServer.start(); | ||
| logger.info("{} started, listening on {}", this.getClass().getSimpleName(), port); | ||
| } catch (Exception e) { | ||
| logger.error("{}", this.getClass().getSimpleName(), e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void stop() { | ||
| if (apiServer != null) { | ||
| logger.info("{} shutdown...", this.getClass().getSimpleName()); | ||
| try { | ||
| apiServer.stop(); | ||
| } catch (Exception e) { | ||
| logger.warn("{}", this.getClass().getSimpleName(), e); | ||
| } | ||
| logger.info("{} shutdown complete", this.getClass().getSimpleName()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| HttpService that = (HttpService) o; | ||
| return port == that.port; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(getClass().getSimpleName(), port); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * java-tron is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * java-tron is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package org.tron.common.application; | ||
|
|
||
| import com.google.common.base.Objects; | ||
| import io.grpc.Server; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Slf4j(topic = "rpc") | ||
| public abstract class RpcService implements Service { | ||
|
|
||
| protected Server apiServer; | ||
| protected int port; | ||
|
|
||
| @Override | ||
| public void blockUntilShutdown() { | ||
| if (apiServer != null) { | ||
| try { | ||
| apiServer.awaitTermination(); | ||
| } catch (InterruptedException e) { | ||
| logger.warn("{}", e.getMessage()); | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void start() { | ||
| if (apiServer != null) { | ||
| try { | ||
| apiServer.start(); | ||
| logger.info("{} started, listening on {}", this.getClass().getSimpleName(), port); | ||
| } catch (IOException e) { | ||
| logger.error("{}", this.getClass().getSimpleName(), e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void stop() { | ||
| if (apiServer != null) { | ||
| logger.info("{} shutdown...", this.getClass().getSimpleName()); | ||
| try { | ||
| apiServer.shutdown().awaitTermination(5, TimeUnit.SECONDS); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| logger.warn("{}", this.getClass().getSimpleName(), e); | ||
| } | ||
| logger.info("{} shutdown complete", this.getClass().getSimpleName()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| RpcService that = (RpcService) o; | ||
| return port == that.port; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(getClass().getSimpleName(), port); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,17 +15,19 @@ | |
|
|
||
| package org.tron.common.application; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.Set; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.tron.common.parameter.CommonParameter; | ||
|
|
||
| @Slf4j(topic = "app") | ||
| public class ServiceContainer { | ||
|
|
||
| private ArrayList<Service> services; | ||
| private final Set<Service> services; | ||
|
|
||
| public ServiceContainer() { | ||
| this.services = new ArrayList<>(); | ||
| this.services = Collections.synchronizedSet(new LinkedHashSet<>()); | ||
| } | ||
|
|
||
| public void add(Service service) { | ||
|
|
@@ -34,31 +36,38 @@ public void add(Service service) { | |
|
|
||
|
|
||
| public void init() { | ||
| for (Service service : this.services) { | ||
| this.services.forEach(service -> { | ||
| logger.debug("Initing {}.", service.getClass().getSimpleName()); | ||
| service.init(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void init(CommonParameter parameter) { | ||
| for (Service service : this.services) { | ||
| this.services.forEach(service -> { | ||
| logger.debug("Initing {}.", service.getClass().getSimpleName()); | ||
| service.init(parameter); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void start() { | ||
| logger.debug("Starting services."); | ||
| for (Service service : this.services) { | ||
| logger.info("Starting api services."); | ||
| this.services.forEach(service -> { | ||
| logger.debug("Starting {}.", service.getClass().getSimpleName()); | ||
| service.start(); | ||
| } | ||
| }); | ||
| logger.info("All api services started."); | ||
| } | ||
|
|
||
| public void stop() { | ||
| for (Service service : this.services) { | ||
| logger.info("Stopping api services."); | ||
| this.services.forEach(service -> { | ||
| logger.debug("Stopping {}.", service.getClass().getSimpleName()); | ||
| service.stop(); | ||
| } | ||
| }); | ||
| logger.info("All api services stopped."); | ||
| } | ||
|
|
||
| public void blockUntilShutdown() { | ||
| this.services.stream().findFirst().ifPresent(Service::blockUntilShutdown); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only check the first service is stopped?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
|
|
||
| import org.springframework.beans.factory.support.DefaultListableBeanFactory; | ||
| import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
| import org.tron.program.FullNode; | ||
| import org.tron.core.config.TronLogShutdownHook; | ||
|
|
||
| public class TronApplicationContext extends AnnotationConfigApplicationContext { | ||
|
|
||
|
|
@@ -25,10 +25,15 @@ public TronApplicationContext(String... basePackages) { | |
| public void doClose() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I see, all singletons' close logic was delivered to Spring, is there any dependency between them, or may occur any exceptions when the close order was not as expected?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bean dependencies are managed by spring, and initialization and destroy are in reverse order. |
||
| logger.info("******** start to close ********"); | ||
| Application appT = ApplicationFactory.create(this); | ||
| appT.shutdownServices(); | ||
| appT.shutdown(); | ||
| super.doClose(); | ||
| logger.info("******** close end ********"); | ||
| FullNode.shutDownSign = true; | ||
| TronLogShutdownHook.shutDown = true; | ||
| } | ||
|
|
||
| @Override | ||
| public void registerShutdownHook() { | ||
| super.registerShutdownHook(); | ||
| TronLogShutdownHook.shutDown = false; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the purpose of changing List to Set, if two objects execute
init()with the same class and port but without executingstart(), they will be equal according to the above modification?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, block duplicate service.