private RetryState getAndUpdateRetryState(final Path dir) {
        final Path retryStateFile = getRetryStateFile(dir);
        RetryState updatedRetryState;
        if (Files.isRegularFile(retryStateFile)) {
            try (RandomAccessFile reader = new RandomAccessFile(retryStateFile.toFile(), "rwd");
                    FileChannel channel = reader.getChannel()) {
                final ByteBuffer byteBuffer = ByteBuffer.allocate(RetryState.TOTAL_BYTES);
                // First read the existing value
                channel.read(byteBuffer);
                byteBuffer.flip();
                final RetryState existingRetryState = RetryState.deserialise(byteBuffer);
                updatedRetryState = existingRetryState.cloneAndUpdate();
                LOGGER.debug("'{}' - retryStateFile: {}, existingRetryState: {}, updatedRetryState: {}",
                        destinationName, retryStateFile, existingRetryState, updatedRetryState);

                // Get ready for writing back to the file
                channel.position(0);
                byteBuffer.flip();
                updatedRetryState.serialise(byteBuffer);
                byteBuffer.flip();
                final int writeCount = channel.write(byteBuffer);
                if (writeCount != RetryState.TOTAL_BYTES) {
                    throw new IllegalStateException(LogUtil.message("Unexpected writeCount {}, expecting {}",
                            writeCount, RetryState.TOTAL_BYTES));
                }
            } catch (IOException e) {
                LOGGER.error("'{}' - Error updating retry file '{}'. " +
                             "Retry state cannot be updated so this directory will be " +
                             "retried indefinitely. Error: {}",
                        destinationName, retryStateFile, LogUtil.exceptionMessage(e), e);
                updatedRetryState = RetryState.initial();
            }
        } else {
            updatedRetryState = RetryState.initial();
            if (Files.exists(retryStateFile)) {
                LOGGER.error("'{}' - Retry state file '{}' exists but is not a file. " +
                             "Retry state cannot be updated so this directory will be " +
                             "retried indefinitely. ", destinationName, retryStateFile);
                // Means we will never update the state, but not a lot we can do
            } else {
                try {
                    Files.write(retryStateFile,
                            updatedRetryState.serialise(),
                            StandardOpenOption.CREATE_NEW,
                            StandardOpenOption.WRITE);
                } catch (IOException e) {
                    LOGGER.error(e::getMessage, e);
                }
            }
        }
        return updatedRetryState;
    }
@return The {@link RetryState} after initialising/updating it for the attempt that has just
happened.
stroom-proxy/stroom-proxy-app/src/main/java/stroom/proxy/app/handler/RetryingForwardDestination.java