use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.
the class SnapshotsTest method testValidDeletedSnapshotFilename.
@Test
public void testValidDeletedSnapshotFilename() {
OffsetAndEpoch snapshotId = new OffsetAndEpoch(TestUtils.RANDOM.nextInt(Integer.MAX_VALUE), TestUtils.RANDOM.nextInt(Integer.MAX_VALUE));
Path path = Snapshots.snapshotPath(TestUtils.tempDirectory().toPath(), snapshotId);
Path deletedPath = Snapshots.deleteRename(path, snapshotId);
SnapshotPath snapshotPath = Snapshots.parse(deletedPath).get();
assertEquals(snapshotId, snapshotPath.snapshotId);
assertTrue(snapshotPath.deleted);
}
use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.
the class KafkaRaftMetricsTest method shouldRecordVoterQuorumState.
@Test
public void shouldRecordVoterQuorumState() throws IOException {
QuorumState state = buildQuorumState(Utils.mkSet(localId, 1, 2));
state.initialize(new OffsetAndEpoch(0L, 0));
raftMetrics = new KafkaRaftMetrics(metrics, "raft", state);
assertEquals("unattached", getMetric(metrics, "current-state").metricValue());
assertEquals((double) -1L, getMetric(metrics, "current-leader").metricValue());
assertEquals((double) -1L, getMetric(metrics, "current-vote").metricValue());
assertEquals((double) 0, getMetric(metrics, "current-epoch").metricValue());
assertEquals((double) -1L, getMetric(metrics, "high-watermark").metricValue());
state.transitionToCandidate();
assertEquals("candidate", getMetric(metrics, "current-state").metricValue());
assertEquals((double) -1L, getMetric(metrics, "current-leader").metricValue());
assertEquals((double) localId, getMetric(metrics, "current-vote").metricValue());
assertEquals((double) 1, getMetric(metrics, "current-epoch").metricValue());
assertEquals((double) -1L, getMetric(metrics, "high-watermark").metricValue());
state.candidateStateOrThrow().recordGrantedVote(1);
state.transitionToLeader(2L, accumulator);
assertEquals("leader", getMetric(metrics, "current-state").metricValue());
assertEquals((double) localId, getMetric(metrics, "current-leader").metricValue());
assertEquals((double) localId, getMetric(metrics, "current-vote").metricValue());
assertEquals((double) 1, getMetric(metrics, "current-epoch").metricValue());
assertEquals((double) -1L, getMetric(metrics, "high-watermark").metricValue());
state.leaderStateOrThrow().updateLocalState(0, new LogOffsetMetadata(5L));
state.leaderStateOrThrow().updateReplicaState(1, 0, new LogOffsetMetadata(5L));
assertEquals((double) 5L, getMetric(metrics, "high-watermark").metricValue());
state.transitionToFollower(2, 1);
assertEquals("follower", getMetric(metrics, "current-state").metricValue());
assertEquals((double) 1, getMetric(metrics, "current-leader").metricValue());
assertEquals((double) -1, getMetric(metrics, "current-vote").metricValue());
assertEquals((double) 2, getMetric(metrics, "current-epoch").metricValue());
assertEquals((double) 5L, getMetric(metrics, "high-watermark").metricValue());
state.followerStateOrThrow().updateHighWatermark(OptionalLong.of(10L));
assertEquals((double) 10L, getMetric(metrics, "high-watermark").metricValue());
state.transitionToVoted(3, 2);
assertEquals("voted", getMetric(metrics, "current-state").metricValue());
assertEquals((double) -1, getMetric(metrics, "current-leader").metricValue());
assertEquals((double) 2, getMetric(metrics, "current-vote").metricValue());
assertEquals((double) 3, getMetric(metrics, "current-epoch").metricValue());
assertEquals((double) 10L, getMetric(metrics, "high-watermark").metricValue());
state.transitionToUnattached(4);
assertEquals("unattached", getMetric(metrics, "current-state").metricValue());
assertEquals((double) -1, getMetric(metrics, "current-leader").metricValue());
assertEquals((double) -1, getMetric(metrics, "current-vote").metricValue());
assertEquals((double) 4, getMetric(metrics, "current-epoch").metricValue());
assertEquals((double) 10L, getMetric(metrics, "high-watermark").metricValue());
}
use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.
the class MetadataDelta method apply.
public MetadataImage apply() {
FeaturesImage newFeatures;
if (featuresDelta == null) {
newFeatures = image.features();
} else {
newFeatures = featuresDelta.apply();
}
ClusterImage newCluster;
if (clusterDelta == null) {
newCluster = image.cluster();
} else {
newCluster = clusterDelta.apply();
}
TopicsImage newTopics;
if (topicsDelta == null) {
newTopics = image.topics();
} else {
newTopics = topicsDelta.apply();
}
ConfigurationsImage newConfigs;
if (configsDelta == null) {
newConfigs = image.configs();
} else {
newConfigs = configsDelta.apply();
}
ClientQuotasImage newClientQuotas;
if (clientQuotasDelta == null) {
newClientQuotas = image.clientQuotas();
} else {
newClientQuotas = clientQuotasDelta.apply();
}
ProducerIdsImage newProducerIds;
if (producerIdsDelta == null) {
newProducerIds = image.producerIds();
} else {
newProducerIds = producerIdsDelta.apply();
}
AclsImage newAcls;
if (aclsDelta == null) {
newAcls = image.acls();
} else {
newAcls = aclsDelta.apply();
}
return new MetadataImage(new OffsetAndEpoch(highestOffset, highestEpoch), newFeatures, newCluster, newTopics, newConfigs, newClientQuotas, newProducerIds, newAcls);
}
use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.
the class SnapshotWriterReaderTest method testSnapshotDelimiters.
@Test
public void testSnapshotDelimiters() throws Exception {
int recordsPerBatch = 1;
int batches = 0;
int delimiterCount = 2;
long magicTimestamp = 0xDEADBEEF;
OffsetAndEpoch id = new OffsetAndEpoch(recordsPerBatch * batches, 3);
RaftClientTestContext.Builder contextBuilder = new RaftClientTestContext.Builder(localId, voters);
RaftClientTestContext context = contextBuilder.build();
context.pollUntil(() -> context.currentLeader().equals(OptionalInt.of(localId)));
context.advanceLocalLeaderHighWatermarkToLogEndOffset();
// Create an empty snapshot and freeze it immediately
try (SnapshotWriter<String> snapshot = context.client.createSnapshot(id.offset - 1, id.epoch, magicTimestamp).get()) {
assertEquals(id, snapshot.snapshotId());
snapshot.freeze();
}
// Verify that an empty snapshot has only the Header and Footer
try (SnapshotReader<String> reader = readSnapshot(context, id, Integer.MAX_VALUE)) {
RawSnapshotReader snapshot = context.log.readSnapshot(id).get();
int recordCount = validateDelimiters(snapshot, magicTimestamp);
assertEquals((recordsPerBatch * batches) + delimiterCount, recordCount);
}
}
use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.
the class SnapshotWriterReaderTest method testAppendToFrozenSnapshot.
@Test
public void testAppendToFrozenSnapshot() throws Exception {
int recordsPerBatch = 3;
int batches = 3;
OffsetAndEpoch id = new OffsetAndEpoch(recordsPerBatch * batches, 3);
List<List<String>> expected = buildRecords(recordsPerBatch, batches);
RaftClientTestContext.Builder contextBuilder = new RaftClientTestContext.Builder(localId, voters);
for (List<String> batch : expected) {
contextBuilder.appendToLog(id.epoch, batch);
}
RaftClientTestContext context = contextBuilder.build();
context.pollUntil(() -> context.currentLeader().equals(OptionalInt.of(localId)));
int epoch = context.currentEpoch();
context.advanceLocalLeaderHighWatermarkToLogEndOffset();
try (SnapshotWriter<String> snapshot = context.client.createSnapshot(id.offset - 1, id.epoch, 0).get()) {
assertEquals(id, snapshot.snapshotId());
expected.forEach(batch -> {
assertDoesNotThrow(() -> snapshot.append(batch));
});
snapshot.freeze();
assertThrows(RuntimeException.class, () -> snapshot.append(expected.get(0)));
}
}
Aggregations