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);
}
}
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;
}
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);
}
}
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);
}
}
}
Aggregations