Search in sources :

Example 31 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 32 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 33 with ClusterException

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

the class ConsensusModuleAgent method onReplayNewLeadershipTermEvent.

void onReplayNewLeadershipTermEvent(final long leadershipTermId, final long logPosition, final long timestamp, final long termBaseLogPosition, final TimeUnit timeUnit, final int appVersion) {
    if (timeUnit != clusterTimeUnit) {
        ctx.countedErrorHandler().onError(new ClusterException("incompatible timestamp units: " + clusterTimeUnit + " log=" + timeUnit, AeronException.Category.FATAL));
        unexpectedTermination();
    }
    if (SemanticVersion.major(ctx.appVersion()) != SemanticVersion.major(appVersion)) {
        ctx.countedErrorHandler().onError(new ClusterException("incompatible version: " + SemanticVersion.toString(ctx.appVersion()) + " log=" + SemanticVersion.toString(appVersion), AeronException.Category.FATAL));
        unexpectedTermination();
    }
    leadershipTermId(leadershipTermId);
    if (null != election) {
        election.onReplayNewLeadershipTermEvent(logRecordingId, leadershipTermId, logPosition, timestamp, termBaseLogPosition);
    }
}
Also used : ClusterException(io.aeron.cluster.client.ClusterException)

Example 34 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