Skip to content

Commit

Permalink
Removed InterruptedException from the close() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed Dec 5, 2024
1 parent 754d308 commit b8a7658
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

import io.hotmoka.closeables.api.OnCloseHandler;
Expand Down Expand Up @@ -69,11 +71,13 @@ public abstract class NodeDecoratorImpl<N extends Node> implements Node {
*/
private final AtomicBoolean isClosed = new AtomicBoolean();

private final static Logger LOGGER = Logger.getLogger(NodeDecoratorImpl.class.getName());

/**
* We need this intermediate definition since two instances of a method reference
* are not the same, nor equals.
*/
private final OnCloseHandler this_close = this::close;
private final OnCloseHandler this_close = this::tryClose;

/**
* Creates a decorated node.
Expand Down Expand Up @@ -108,11 +112,20 @@ protected final void ensureNotClosed() throws ClosedNodeException {
}

@Override
public void close() throws InterruptedException, NodeException {
public void close() throws NodeException {
if (!isClosed.getAndSet(true))
parent.close();
}

private void tryClose() {
try {
close();
}
catch (NodeException e) {
LOGGER.log(Level.SEVERE, "cannot close the decoted node", e);
}
}

@Override
public StorageReference getManifest() throws NodeException, TimeoutException, InterruptedException {
return parent.getManifest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,8 @@ public interface Node extends AutoCloseable, OnCloseHandlersContainer {
/**
* Closes the node.
*
* @throws InterruptedException if the current thread has been interrupted while closing the node
* @throws NodeException if the node is not able to close correctly
*/
@Override
void close() throws InterruptedException, NodeException;
void close() throws NodeException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected DiskStore enterHead() {
}

@Override
protected void closeResources() throws NodeException, InterruptedException {
protected void closeResources() throws NodeException {
try {
mempool.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BiConsumer;
import java.util.logging.Level;
Expand Down Expand Up @@ -188,9 +187,14 @@ protected AbstractLocalNodeImpl(C config, boolean init) throws NodeException {
}

@Override
public final void close() throws InterruptedException, NodeException {
if (stopNewCalls())
closeResources();
public final void close() throws NodeException {
try {
if (stopNewCalls())
closeResources();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

@Override
Expand Down Expand Up @@ -550,11 +554,8 @@ protected final void publish(TransactionReference reference, S store) throws Nod
}
}

protected void closeResources() throws NodeException, InterruptedException {
long start = System.currentTimeMillis();

protected void closeResources() throws NodeException {
executors.shutdownNow();
executors.awaitTermination(5000 - System.currentTimeMillis() + start, TimeUnit.MILLISECONDS);
}

/**
Expand Down Expand Up @@ -707,9 +708,6 @@ private void addShutdownHook() { // TODO: do we really need it? It seems that it
try {
close();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
catch (NodeException e) {
LOGGER.log(Level.SEVERE, "The shutdown hook of the node failed", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@

import io.hotmoka.annotations.GuardedBy;
import io.hotmoka.annotations.ThreadSafe;
import io.hotmoka.exceptions.CheckRunnable;
import io.hotmoka.exceptions.CheckSupplier;
import io.hotmoka.exceptions.UncheckConsumer;
import io.hotmoka.exceptions.UncheckFunction;
import io.hotmoka.exceptions.functions.ConsumerWithExceptions3;
import io.hotmoka.node.api.NodeException;
import io.hotmoka.node.local.AbstractLocalNode;
Expand Down Expand Up @@ -150,7 +146,7 @@ protected final Environment getEnvironment() {
}

@Override
protected void closeResources() throws NodeException, InterruptedException {
protected void closeResources() throws NodeException {
try {
super.closeResources();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ protected MokamintStore getStoreOfHead() throws NodeException, InterruptedExcept
}

@Override
protected void closeResources() throws NodeException, InterruptedException {
protected void closeResources() throws NodeException {
try {
mokamintNode.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ protected NodeException mkException(Exception cause) {
}

@Override
protected void closeResources(CloseReason reason) throws NodeException, InterruptedException {
protected void closeResources(CloseReason reason) throws NodeException {
super.closeResources(reason);
LOGGER.info(logPrefix + "closed with reason: " + reason);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public NodeInfo getNodeInfo() throws NodeException, TimeoutException, Interrupte
}

@Override
protected void closeResources() throws NodeException, InterruptedException {
protected void closeResources() throws NodeException {
try {
closeTendermintAndABCI();
}
Expand Down Expand Up @@ -264,7 +264,7 @@ private static long bytesToLong(final byte[] b) {
return result;
}

private void closeTendermintAndABCI() throws NodeException, InterruptedException {
private void closeTendermintAndABCI() throws NodeException {
try {
if (tendermint != null)
tendermint.close();
Expand All @@ -277,11 +277,16 @@ private void closeTendermintAndABCI() throws NodeException, InterruptedException
}
}

private void closeABCI() throws InterruptedException {
private void closeABCI() {
if (abci != null && !abci.isShutdown()) {
abci.shutdown();
abci.awaitTermination();
}
try {
abci.awaitTermination();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

private static void copyRecursively(Path src, Path dest) throws IOException {
Expand Down Expand Up @@ -378,18 +383,23 @@ private Tendermint(TendermintNodeConfig config) throws IOException, InterruptedE
}

@Override
public void close() throws InterruptedException, IOException {
public void close() throws IOException {
// the following is important under Windows, since the shell script thats starts Tendermint
// under Windows spawns it as a subprocess
process.descendants().forEach(ProcessHandle::destroy);
process.destroy();
process.waitFor();

if (isWindows)
// this seems important under Windows
try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
LOGGER.info(br.lines().collect(Collectors.joining()));
}

try {
process.waitFor();
if (isWindows)
// this seems important under Windows
try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
LOGGER.info(br.lines().collect(Collectors.joining()));
}
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

LOGGER.info("the Tendermint process has been shut down");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ public void beforeAll(ExtensionContext context) throws Exception {
privateKeyOfGamete = keys.getPrivate();

Node wrapped;
node = wrapped = mkDiskNode();
//node = wrapped = mkDiskNode();
//node = wrapped = mkMokamintNodeConnectedToPeer();
//node = wrapped = mkMokamintNetwork(4);
//node = wrapped = mkTendermintNode();
//node = mkRemoteNode(wrapped = mkDiskNode());
node = mkRemoteNode(wrapped = mkDiskNode());
//node = mkRemoteNode(wrapped = mkMokamintNetwork(1));
//node = mkRemoteNode(wrapped = mkTendermintNode());
//node = wrapped = mkRemoteNode("ec2-54-194-239-91.eu-west-1.compute.amazonaws.com:8080");
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ Note: submodules whose artifact must be deployed on Maven Central must activate
<hotmoka.version>1.7.0</hotmoka.version>
<io.takamaka.code.version>1.3.1</io.takamaka.code.version>
<io.hotmoka.annotations.version>1.4.1</io.hotmoka.annotations.version>
<io.hotmoka.cli.version>1.2.2</io.hotmoka.cli.version>
<io.hotmoka.cli.version>1.2.3</io.hotmoka.cli.version>
<io.hotmoka.crypto.version>1.4.3</io.hotmoka.crypto.version>
<io.hotmoka.websockets.version>1.4.5</io.hotmoka.websockets.version>
<io.hotmoka.websockets.version>1.5.0</io.hotmoka.websockets.version>
<io.hotmoka.marshalling.version>1.4.5</io.hotmoka.marshalling.version>
<io.hotmoka.exceptions.version>1.5.0</io.hotmoka.exceptions.version>
<io.hotmoka.testing.version>1.4.1</io.hotmoka.testing.version>
<io.hotmoka.xodus.version>1.4.6</io.hotmoka.xodus.version>
<io.hotmoka.closeables.version>1.4.1</io.hotmoka.closeables.version>
<io.hotmoka.closeables.version>1.5.0</io.hotmoka.closeables.version>
<mokamint.version>1.1.0</mokamint.version>
</properties>

Expand Down

0 comments on commit b8a7658

Please sign in to comment.