Search in sources :

Example 1 with ClusterException

use of io.aeron.cluster.client.ClusterException in project Aeron by real-logic.

the class ConsensusModuleAgent method loadSnapshot.

private void loadSnapshot(final RecordingLog.Snapshot snapshot, final AeronArchive archive) {
    final String channel = ctx.replayChannel();
    final int streamId = ctx.replayStreamId();
    final int sessionId = (int) archive.startReplay(snapshot.recordingId, 0, NULL_LENGTH, channel, streamId);
    final String replaySubscriptionChannel = ChannelUri.addSessionId(channel, sessionId);
    try (Subscription subscription = aeron.addSubscription(replaySubscriptionChannel, streamId)) {
        final Image image = awaitImage(sessionId, subscription);
        final ConsensusModuleSnapshotLoader snapshotLoader = new ConsensusModuleSnapshotLoader(image, this);
        while (true) {
            final int fragments = snapshotLoader.poll();
            if (0 == fragments) {
                if (snapshotLoader.isDone()) {
                    break;
                }
                if (image.isClosed()) {
                    pollArchiveEvents();
                    throw new ClusterException("snapshot ended unexpectedly: " + image);
                }
            }
            idle(fragments);
        }
        final int appVersion = snapshotLoader.appVersion();
        if (SemanticVersion.major(ctx.appVersion()) != SemanticVersion.major(appVersion)) {
            throw new ClusterException("incompatible version: " + SemanticVersion.toString(ctx.appVersion()) + " snapshot=" + SemanticVersion.toString(appVersion));
        }
        final TimeUnit timeUnit = snapshotLoader.timeUnit();
        if (timeUnit != clusterTimeUnit) {
            throw new ClusterException("incompatible time unit: " + clusterTimeUnit + " snapshot=" + timeUnit);
        }
        pendingServiceMessages.forEach(this::serviceSessionMessageReset, Integer.MAX_VALUE);
    }
    timerService.currentTime(clusterClock.time());
    leadershipTermId(snapshot.leadershipTermId);
    commitPosition.setOrdered(snapshot.logPosition);
    expectedAckPosition = snapshot.logPosition;
}
Also used : ClusterException(io.aeron.cluster.client.ClusterException) TimeUnit(java.util.concurrent.TimeUnit)

Example 2 with ClusterException

use of io.aeron.cluster.client.ClusterException in project Aeron by real-logic.

the class ClusterTool method updateRecordingLog.

private static void updateRecordingLog(final File clusterDir, final List<RecordingLog.Entry> entries) {
    final Path recordingLog = clusterDir.toPath().resolve(RecordingLog.RECORDING_LOG_FILE_NAME);
    try {
        if (entries.isEmpty()) {
            Files.delete(recordingLog);
        } else {
            final Path newRecordingLog = clusterDir.toPath().resolve(RecordingLog.RECORDING_LOG_FILE_NAME + ".tmp");
            Files.deleteIfExists(newRecordingLog);
            final ByteBuffer byteBuffer = ByteBuffer.allocateDirect(RecordingLog.ENTRY_LENGTH).order(LITTLE_ENDIAN);
            final UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer);
            try (FileChannel fileChannel = FileChannel.open(newRecordingLog, CREATE_NEW, WRITE)) {
                long position = 0;
                for (final RecordingLog.Entry e : entries) {
                    RecordingLog.writeEntryToBuffer(e, buffer);
                    byteBuffer.limit(RecordingLog.ENTRY_LENGTH).position(0);
                    if (RecordingLog.ENTRY_LENGTH != fileChannel.write(byteBuffer, position)) {
                        throw new ClusterException("failed to write recording");
                    }
                    position += RecordingLog.ENTRY_LENGTH;
                }
            } finally {
                BufferUtil.free(byteBuffer);
            }
            Files.delete(recordingLog);
            Files.move(newRecordingLog, recordingLog);
        }
    } catch (final IOException ex) {
        throw new UncheckedIOException(ex);
    }
}
Also used : Path(java.nio.file.Path) ClusterException(io.aeron.cluster.client.ClusterException) FileChannel(java.nio.channels.FileChannel) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 3 with ClusterException

use of io.aeron.cluster.client.ClusterException in project Aeron by real-logic.

the class ClusterBackupAgent method slowTick.

private int slowTick(final long nowMs) {
    int workCount = aeronClientInvoker.invoke();
    if (aeron.isClosed()) {
        throw new AgentTerminationException("unexpected Aeron close");
    }
    if (nowMs >= markFileUpdateDeadlineMs) {
        markFileUpdateDeadlineMs = nowMs + MARK_FILE_UPDATE_INTERVAL_MS;
        markFile.updateActivityTimestamp(nowMs);
    }
    workCount += pollBackupArchiveEvents();
    if (NULL_VALUE == correlationId && null != clusterArchive) {
        final String errorResponse = clusterArchive.pollForErrorResponse();
        if (null != errorResponse) {
            ctx.countedErrorHandler().onError(new ClusterException("cluster archive - " + errorResponse, WARN));
            state(RESET_BACKUP, nowMs);
        }
    }
    return workCount;
}
Also used : AgentTerminationException(org.agrona.concurrent.AgentTerminationException) ClusterException(io.aeron.cluster.client.ClusterException)

Example 4 with ClusterException

use of io.aeron.cluster.client.ClusterException in project Aeron by real-logic.

the class Election method ensureRecordingLogCoherent.

private void ensureRecordingLogCoherent(final long leadershipTermId, final long logTermBasePosition, final long logPosition, final long nowNs) {
    final long recordingId = consensusModuleAgent.logRecordingId();
    if (NULL_VALUE == recordingId) {
        if (0 == logPosition) {
            return;
        }
        throw new AgentTerminationException("log recording id not found");
    }
    final long timestamp = ctx.clusterClock().timeUnit().convert(nowNs, TimeUnit.NANOSECONDS);
    final RecordingLog recordingLog = ctx.recordingLog();
    RecordingLog.Entry lastTerm = recordingLog.findLastTerm();
    if (null == lastTerm) {
        for (long termId = 0; termId < leadershipTermId; termId++) {
            recordingLog.appendTerm(recordingId, termId, 0, timestamp);
            recordingLog.commitLogPosition(termId, 0);
        }
        recordingLog.appendTerm(recordingId, leadershipTermId, 0, timestamp);
        if (NULL_VALUE != logPosition) {
            recordingLog.commitLogPosition(leadershipTermId, logPosition);
        }
    } else if (lastTerm.leadershipTermId < leadershipTermId) {
        if (NULL_VALUE == lastTerm.logPosition) {
            if (NULL_VALUE == logTermBasePosition) {
                throw new ClusterException("Prior term was not committed: " + lastTerm + " and logTermBasePosition was not specified: leadershipTermId = " + leadershipTermId + ", logTermBasePosition = " + logTermBasePosition + ", logPosition = " + logPosition + ", nowNs = " + nowNs);
            } else {
                recordingLog.commitLogPosition(lastTerm.leadershipTermId, logTermBasePosition);
                lastTerm = Objects.requireNonNull(recordingLog.findLastTerm());
            }
        }
        for (long termId = lastTerm.leadershipTermId + 1; termId < leadershipTermId; termId++) {
            recordingLog.appendTerm(recordingId, termId, lastTerm.logPosition, timestamp);
            recordingLog.commitLogPosition(termId, lastTerm.logPosition);
        }
        recordingLog.appendTerm(recordingId, leadershipTermId, lastTerm.logPosition, timestamp);
        if (NULL_VALUE != logPosition) {
            recordingLog.commitLogPosition(leadershipTermId, logPosition);
        }
    } else {
        if (NULL_VALUE != logPosition) {
            recordingLog.commitLogPosition(leadershipTermId, logPosition);
        }
    }
    recordingLog.force(ctx.fileSyncLevel());
}
Also used : AgentTerminationException(org.agrona.concurrent.AgentTerminationException) ClusterException(io.aeron.cluster.client.ClusterException)

Example 5 with ClusterException

use of io.aeron.cluster.client.ClusterException in project Aeron by real-logic.

the class LogReplication method close.

void close() {
    if (!isStopped) {
        try {
            isStopped = true;
            archive.tryStopReplication(replicationId);
        } catch (final Exception ex) {
            throw new ClusterException("failed to stop log replication", ex, AeronException.Category.WARN);
        }
    }
}
Also used : ClusterException(io.aeron.cluster.client.ClusterException) AeronException(io.aeron.exceptions.AeronException) ClusterException(io.aeron.cluster.client.ClusterException)

Aggregations

ClusterException (io.aeron.cluster.client.ClusterException)34 Test (org.junit.jupiter.api.Test)12 AgentTerminationException (org.agrona.concurrent.AgentTerminationException)6 CountersReader (org.agrona.concurrent.status.CountersReader)4 AeronException (io.aeron.exceptions.AeronException)2 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 ByteBuffer (java.nio.ByteBuffer)2 MappedByteBuffer (java.nio.MappedByteBuffer)2 FileChannel (java.nio.channels.FileChannel)2 Path (java.nio.file.Path)2 TimeUnit (java.util.concurrent.TimeUnit)2 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2