Search in sources :

Example 21 with OffsetAndEpoch

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);
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) Path(java.nio.file.Path) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 22 with OffsetAndEpoch

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());
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) QuorumState(org.apache.kafka.raft.QuorumState) LogOffsetMetadata(org.apache.kafka.raft.LogOffsetMetadata) Test(org.junit.jupiter.api.Test)

Example 23 with OffsetAndEpoch

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);
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch)

Example 24 with OffsetAndEpoch

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);
    }
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) RaftClientTestContext(org.apache.kafka.raft.RaftClientTestContext) Test(org.junit.jupiter.api.Test)

Example 25 with OffsetAndEpoch

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)));
    }
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) RaftClientTestContext(org.apache.kafka.raft.RaftClientTestContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

OffsetAndEpoch (org.apache.kafka.raft.OffsetAndEpoch)25 Test (org.junit.jupiter.api.Test)22 Path (java.nio.file.Path)8 UnalignedMemoryRecords (org.apache.kafka.common.record.UnalignedMemoryRecords)8 QuorumState (org.apache.kafka.raft.QuorumState)7 ByteBuffer (java.nio.ByteBuffer)4 RaftClientTestContext (org.apache.kafka.raft.RaftClientTestContext)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Record (org.apache.kafka.common.record.Record)3 RecordBatch (org.apache.kafka.common.record.RecordBatch)3 SimpleRecord (org.apache.kafka.common.record.SimpleRecord)3 UnalignedFileRecords (org.apache.kafka.common.record.UnalignedFileRecords)3 GrowableBufferSupplier (org.apache.kafka.common.utils.BufferSupplier.GrowableBufferSupplier)3 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Arrays (java.util.Arrays)2 Iterator (java.util.Iterator)2 Optional (java.util.Optional)2