Search in sources :

Example 31 with ApiMessageAndVersion

use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.

the class ProducerIdControlManagerTest method testSnapshotIterator.

@Test
public void testSnapshotIterator() {
    ProducerIdsBlock range = null;
    for (int i = 0; i < 100; i++) {
        range = generateProducerIds(producerIdControlManager, i % 4, 100);
    }
    Iterator<List<ApiMessageAndVersion>> snapshotIterator = producerIdControlManager.iterator(Long.MAX_VALUE);
    assertTrue(snapshotIterator.hasNext());
    List<ApiMessageAndVersion> batch = snapshotIterator.next();
    assertEquals(1, batch.size(), "Producer IDs record batch should only contain a single record");
    assertEquals(range.firstProducerId() + range.size(), ((ProducerIdsRecord) batch.get(0).message()).nextProducerId());
    assertFalse(snapshotIterator.hasNext(), "Producer IDs iterator should only contain a single batch");
    ProducerIdControlManager newProducerIdManager = new ProducerIdControlManager(clusterControl, snapshotRegistry);
    snapshotIterator = producerIdControlManager.iterator(Long.MAX_VALUE);
    while (snapshotIterator.hasNext()) {
        snapshotIterator.next().forEach(message -> newProducerIdManager.replay((ProducerIdsRecord) message.message()));
    }
    // Verify that after reloading state from this "snapshot", we don't produce any overlapping IDs
    long lastProducerID = range.firstProducerId() + range.size() - 1;
    range = generateProducerIds(producerIdControlManager, 1, 100);
    assertTrue(range.firstProducerId() > lastProducerID);
}
Also used : ProducerIdsRecord(org.apache.kafka.common.metadata.ProducerIdsRecord) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ProducerIdsBlock(org.apache.kafka.server.common.ProducerIdsBlock) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 32 with ApiMessageAndVersion

use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.

the class ClusterControlManagerTest method testIterator.

@Test
public void testIterator() throws Exception {
    MockTime time = new MockTime(0, 0, 0);
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    ClusterControlManager clusterControl = new ClusterControlManager(new LogContext(), Uuid.randomUuid().toString(), time, snapshotRegistry, 1000, new StripedReplicaPlacer(new Random()), new MockControllerMetrics());
    clusterControl.activate();
    assertFalse(clusterControl.unfenced(0));
    for (int i = 0; i < 3; i++) {
        RegisterBrokerRecord brokerRecord = new RegisterBrokerRecord().setBrokerEpoch(100).setBrokerId(i).setRack(null);
        brokerRecord.endPoints().add(new BrokerEndpoint().setSecurityProtocol(SecurityProtocol.PLAINTEXT.id).setPort((short) 9092 + i).setName("PLAINTEXT").setHost("example.com"));
        clusterControl.replay(brokerRecord);
    }
    for (int i = 0; i < 2; i++) {
        UnfenceBrokerRecord unfenceBrokerRecord = new UnfenceBrokerRecord().setId(i).setEpoch(100);
        clusterControl.replay(unfenceBrokerRecord);
    }
    RecordTestUtils.assertBatchIteratorContains(Arrays.asList(Arrays.asList(new ApiMessageAndVersion(new RegisterBrokerRecord().setBrokerEpoch(100).setBrokerId(0).setRack(null).setEndPoints(new BrokerEndpointCollection(Collections.singleton(new BrokerEndpoint().setSecurityProtocol(SecurityProtocol.PLAINTEXT.id).setPort((short) 9092).setName("PLAINTEXT").setHost("example.com")).iterator())).setFenced(false), (short) 0)), Arrays.asList(new ApiMessageAndVersion(new RegisterBrokerRecord().setBrokerEpoch(100).setBrokerId(1).setRack(null).setEndPoints(new BrokerEndpointCollection(Collections.singleton(new BrokerEndpoint().setSecurityProtocol(SecurityProtocol.PLAINTEXT.id).setPort((short) 9093).setName("PLAINTEXT").setHost("example.com")).iterator())).setFenced(false), (short) 0)), Arrays.asList(new ApiMessageAndVersion(new RegisterBrokerRecord().setBrokerEpoch(100).setBrokerId(2).setRack(null).setEndPoints(new BrokerEndpointCollection(Collections.singleton(new BrokerEndpoint().setSecurityProtocol(SecurityProtocol.PLAINTEXT.id).setPort((short) 9094).setName("PLAINTEXT").setHost("example.com")).iterator())).setFenced(true), (short) 0))), clusterControl.iterator(Long.MAX_VALUE));
}
Also used : BrokerEndpointCollection(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpointCollection) LogContext(org.apache.kafka.common.utils.LogContext) Endpoint(org.apache.kafka.common.Endpoint) BrokerEndpoint(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint) SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) Random(java.util.Random) RegisterBrokerRecord(org.apache.kafka.common.metadata.RegisterBrokerRecord) BrokerEndpoint(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) UnfenceBrokerRecord(org.apache.kafka.common.metadata.UnfenceBrokerRecord) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 33 with ApiMessageAndVersion

use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.

the class BrokerRegistrationTest method testRoundTrip.

private void testRoundTrip(BrokerRegistration registration) {
    ApiMessageAndVersion messageAndVersion = registration.toRecord();
    BrokerRegistration registration2 = BrokerRegistration.fromRecord((RegisterBrokerRecord) messageAndVersion.message());
    assertEquals(registration, registration2);
    ApiMessageAndVersion messageAndVersion2 = registration2.toRecord();
    assertEquals(messageAndVersion, messageAndVersion2);
}
Also used : ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion)

Example 34 with ApiMessageAndVersion

use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.

the class MetadataRecordSerdeTest method testParsingRecordWithGarbageAtEnd.

/**
 * Test attempting to parse an event which has a malformed message version varint.
 */
@Test
public void testParsingRecordWithGarbageAtEnd() {
    MetadataRecordSerde serde = new MetadataRecordSerde();
    RegisterBrokerRecord message = new RegisterBrokerRecord().setBrokerId(1).setBrokerEpoch(2);
    ObjectSerializationCache cache = new ObjectSerializationCache();
    ApiMessageAndVersion messageAndVersion = new ApiMessageAndVersion(message, (short) 0);
    int size = serde.recordSize(messageAndVersion, cache);
    ByteBuffer buffer = ByteBuffer.allocate(size + 1);
    serde.write(messageAndVersion, cache, new ByteBufferAccessor(buffer));
    buffer.clear();
    assertStartsWith("Found 1 byte(s) of garbage after", assertThrows(MetadataParseException.class, () -> serde.read(new ByteBufferAccessor(buffer), size + 1)).getMessage());
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) RegisterBrokerRecord(org.apache.kafka.common.metadata.RegisterBrokerRecord) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 35 with ApiMessageAndVersion

use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.

the class KafkaClusterTestKit method close.

@Override
public void close() throws Exception {
    List<Entry<String, Future<?>>> futureEntries = new ArrayList<>();
    try {
        controllerQuorumVotersFutureManager.close();
        for (Entry<Integer, BrokerServer> entry : brokers.entrySet()) {
            int brokerId = entry.getKey();
            BrokerServer broker = entry.getValue();
            futureEntries.add(new SimpleImmutableEntry<>("broker" + brokerId, executorService.submit(broker::shutdown)));
        }
        waitForAllFutures(futureEntries);
        futureEntries.clear();
        for (Entry<Integer, ControllerServer> entry : controllers.entrySet()) {
            int controllerId = entry.getKey();
            ControllerServer controller = entry.getValue();
            futureEntries.add(new SimpleImmutableEntry<>("controller" + controllerId, executorService.submit(controller::shutdown)));
        }
        waitForAllFutures(futureEntries);
        futureEntries.clear();
        for (Entry<Integer, KafkaRaftManager<ApiMessageAndVersion>> entry : raftManagers.entrySet()) {
            int raftManagerId = entry.getKey();
            KafkaRaftManager<ApiMessageAndVersion> raftManager = entry.getValue();
            futureEntries.add(new SimpleImmutableEntry<>("raftManager" + raftManagerId, executorService.submit(raftManager::shutdown)));
        }
        waitForAllFutures(futureEntries);
        futureEntries.clear();
        Utils.delete(baseDirectory);
    } catch (Exception e) {
        for (Entry<String, Future<?>> entry : futureEntries) {
            entry.getValue().cancel(true);
        }
        throw e;
    } finally {
        executorService.shutdownNow();
        executorService.awaitTermination(5, TimeUnit.MINUTES);
    }
}
Also used : ControllerServer(kafka.server.ControllerServer) BrokerServer(kafka.server.BrokerServer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Entry(java.util.Map.Entry) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) KafkaRaftManager(kafka.raft.KafkaRaftManager)

Aggregations

ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)84 ArrayList (java.util.ArrayList)38 Test (org.junit.jupiter.api.Test)35 Uuid (org.apache.kafka.common.Uuid)23 ApiError (org.apache.kafka.common.requests.ApiError)20 LogContext (org.apache.kafka.common.utils.LogContext)17 HashMap (java.util.HashMap)16 SnapshotRegistry (org.apache.kafka.timeline.SnapshotRegistry)15 List (java.util.List)12 Map (java.util.Map)12 PartitionChangeRecord (org.apache.kafka.common.metadata.PartitionChangeRecord)12 PartitionRegistration (org.apache.kafka.metadata.PartitionRegistration)11 TopicRecord (org.apache.kafka.common.metadata.TopicRecord)8 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)7 AlterIsrRequestData (org.apache.kafka.common.message.AlterIsrRequestData)7 Collections (java.util.Collections)6 Iterator (java.util.Iterator)6 Entry (java.util.Map.Entry)6 NoSuchElementException (java.util.NoSuchElementException)6 Optional (java.util.Optional)6