diff --git a/aeron-archive/src/main/java/io/aeron/archive/ArchiveMarkFile.java b/aeron-archive/src/main/java/io/aeron/archive/ArchiveMarkFile.java index 4fe4d90878..d67eafdb79 100644 --- a/aeron-archive/src/main/java/io/aeron/archive/ArchiveMarkFile.java +++ b/aeron-archive/src/main/java/io/aeron/archive/ArchiveMarkFile.java @@ -15,6 +15,7 @@ */ package io.aeron.archive; +import io.aeron.Aeron; import io.aeron.CommonContext; import io.aeron.archive.client.ArchiveException; import io.aeron.archive.codecs.mark.MarkFileHeaderDecoder; @@ -278,7 +279,14 @@ public ArchiveMarkFile( */ public void close() { - CloseHelper.close(markFile); + if (!markFile.isClosed()) + { + CloseHelper.close(markFile); + final UnsafeBuffer emptyBuffer = new UnsafeBuffer(); + headerEncoder.wrap(emptyBuffer, 0); + headerDecoder.wrap(emptyBuffer, 0, 0, 0); + errorBuffer.wrap(emptyBuffer, 0, 0); + } } /** @@ -289,7 +297,7 @@ public void close() */ public long archiveId() { - return headerDecoder.archiveId(); + return markFile.isClosed() ? Aeron.NULL_VALUE : headerDecoder.archiveId(); } /** @@ -297,7 +305,10 @@ public long archiveId() */ public void signalReady() { - markFile.signalReady(SEMANTIC_VERSION); + if (!markFile.isClosed()) + { + markFile.signalReady(SEMANTIC_VERSION); + } } /** @@ -320,7 +331,7 @@ public void updateActivityTimestamp(final long nowMs) */ public long activityTimestampVolatile() { - return markFile.timestampVolatile(); + return markFile.isClosed() ? Aeron.NULL_VALUE : markFile.timestampVolatile(); } /** diff --git a/aeron-cluster/src/main/java/io/aeron/cluster/service/ClusterMarkFile.java b/aeron-cluster/src/main/java/io/aeron/cluster/service/ClusterMarkFile.java index a0c0f1e667..6c2f1ad4f2 100644 --- a/aeron-cluster/src/main/java/io/aeron/cluster/service/ClusterMarkFile.java +++ b/aeron-cluster/src/main/java/io/aeron/cluster/service/ClusterMarkFile.java @@ -40,8 +40,6 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.function.Consumer; -import static io.aeron.Aeron.NULL_VALUE; - /** * Used to indicate if a cluster service is running and what configuration it is using. Errors encountered by * the service are recorded within this file by a {@link org.agrona.concurrent.errors.DistinctErrorLog}. @@ -233,7 +231,7 @@ else if (SemanticVersion.major(version) != MAJOR_VERSION) null, null); buffer = markFile.buffer(); - candidateTermId = NULL_VALUE; + candidateTermId = Aeron.NULL_VALUE; } headerOffset = HEADER_OFFSET; @@ -332,7 +330,14 @@ public static boolean isConsensusModuleMarkFile(final Path path, final BasicFile */ public void close() { - CloseHelper.close(markFile); + if (!markFile.isClosed()) + { + CloseHelper.close(markFile); + final UnsafeBuffer emptyBuffer = new UnsafeBuffer(); + headerEncoder.wrap(emptyBuffer, 0); + headerDecoder.wrap(emptyBuffer, 0, 0, 0); + errorBuffer.wrap(emptyBuffer, 0, 0); + } } /** @@ -353,7 +358,8 @@ public boolean isClosed() */ public long candidateTermId() { - return buffer.getLongVolatile(headerOffset + MarkFileHeaderDecoder.candidateTermIdEncodingOffset()); + return markFile.isClosed() ? Aeron.NULL_VALUE : + buffer.getLongVolatile(headerOffset + MarkFileHeaderDecoder.candidateTermIdEncodingOffset()); } /** @@ -363,7 +369,7 @@ public long candidateTermId() */ public int memberId() { - return headerDecoder.memberId(); + return markFile.isClosed() ? Aeron.NULL_VALUE : headerDecoder.memberId(); } /** @@ -373,7 +379,10 @@ public int memberId() */ public void memberId(final int memberId) { - headerEncoder.memberId(memberId); + if (!markFile.isClosed()) + { + headerEncoder.memberId(memberId); + } } /** @@ -383,7 +392,7 @@ public void memberId(final int memberId) */ public int clusterId() { - return headerDecoder.clusterId(); + return markFile.isClosed() ? Aeron.NULL_VALUE : headerDecoder.clusterId(); } /** @@ -393,7 +402,10 @@ public int clusterId() */ public void clusterId(final int clusterId) { - headerEncoder.clusterId(clusterId); + if (!markFile.isClosed()) + { + headerEncoder.clusterId(clusterId); + } } /** @@ -401,7 +413,10 @@ public void clusterId(final int clusterId) */ public void signalReady() { - markFile.signalReady(SEMANTIC_VERSION); + if (!markFile.isClosed()) + { + markFile.signalReady(SEMANTIC_VERSION); + } } /** @@ -409,7 +424,10 @@ public void signalReady() */ public void signalFailedStart() { - markFile.signalReady(VERSION_FAILED); + if (!markFile.isClosed()) + { + markFile.signalReady(VERSION_FAILED); + } } /** @@ -432,7 +450,7 @@ public void updateActivityTimestamp(final long nowMs) */ public long activityTimestampVolatile() { - return markFile.timestampVolatile(); + return markFile.isClosed() ? Aeron.NULL_VALUE : markFile.timestampVolatile(); } /**