Search in sources :

Example 6 with RegisterBrokerRecord

use of org.apache.kafka.common.metadata.RegisterBrokerRecord 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 7 with RegisterBrokerRecord

use of org.apache.kafka.common.metadata.RegisterBrokerRecord in project kafka by apache.

the class LocalLogManagerTest method testCommits.

/**
 * Test that all the log managers see all the commits.
 */
@Test
public void testCommits() throws Exception {
    try (LocalLogManagerTestEnv env = LocalLogManagerTestEnv.createWithMockListeners(3, Optional.empty())) {
        LeaderAndEpoch leaderInfo = env.waitForLeader();
        int leaderId = leaderInfo.leaderId().orElseThrow(() -> new AssertionError("Current leader is undefined"));
        LocalLogManager activeLogManager = env.logManagers().get(leaderId);
        int epoch = activeLogManager.leaderAndEpoch().epoch();
        List<ApiMessageAndVersion> messages = Arrays.asList(new ApiMessageAndVersion(new RegisterBrokerRecord().setBrokerId(0), (short) 0), new ApiMessageAndVersion(new RegisterBrokerRecord().setBrokerId(1), (short) 0), new ApiMessageAndVersion(new RegisterBrokerRecord().setBrokerId(2), (short) 0));
        assertEquals(3, activeLogManager.scheduleAppend(epoch, messages));
        for (LocalLogManager logManager : env.logManagers()) {
            waitForLastCommittedOffset(3, logManager);
        }
        List<MockMetaLogManagerListener> listeners = env.logManagers().stream().map(m -> (MockMetaLogManagerListener) m.listeners().get(0)).collect(Collectors.toList());
        env.close();
        for (MockMetaLogManagerListener listener : listeners) {
            List<String> events = listener.serializedEvents();
            assertEquals(SHUTDOWN, events.get(events.size() - 1));
            int foundIndex = 0;
            for (String event : events) {
                if (event.startsWith(COMMIT)) {
                    assertEquals(messages.get(foundIndex).message().toString(), event.substring(COMMIT.length() + 1));
                    foundIndex++;
                }
            }
            assertEquals(messages.size(), foundIndex);
        }
    }
}
Also used : Arrays(java.util.Arrays) TestUtils(org.apache.kafka.test.TestUtils) LAST_COMMITTED_OFFSET(org.apache.kafka.metalog.MockMetaLogManagerListener.LAST_COMMITTED_OFFSET) OptionalInt(java.util.OptionalInt) Collectors(java.util.stream.Collectors) SHUTDOWN(org.apache.kafka.metalog.MockMetaLogManagerListener.SHUTDOWN) Test(org.junit.jupiter.api.Test) List(java.util.List) RegisterBrokerRecord(org.apache.kafka.common.metadata.RegisterBrokerRecord) COMMIT(org.apache.kafka.metalog.MockMetaLogManagerListener.COMMIT) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) Optional(java.util.Optional) LeaderAndEpoch(org.apache.kafka.raft.LeaderAndEpoch) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Timeout(org.junit.jupiter.api.Timeout) RegisterBrokerRecord(org.apache.kafka.common.metadata.RegisterBrokerRecord) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) LeaderAndEpoch(org.apache.kafka.raft.LeaderAndEpoch) Test(org.junit.jupiter.api.Test)

Example 8 with RegisterBrokerRecord

use of org.apache.kafka.common.metadata.RegisterBrokerRecord in project kafka by apache.

the class ClusterControlManagerTest method testReplay.

@Test
public void testReplay() {
    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));
    RegisterBrokerRecord brokerRecord = new RegisterBrokerRecord().setBrokerEpoch(100).setBrokerId(1);
    brokerRecord.endPoints().add(new BrokerEndpoint().setSecurityProtocol(SecurityProtocol.PLAINTEXT.id).setPort((short) 9092).setName("PLAINTEXT").setHost("example.com"));
    clusterControl.replay(brokerRecord);
    clusterControl.checkBrokerEpoch(1, 100);
    assertThrows(StaleBrokerEpochException.class, () -> clusterControl.checkBrokerEpoch(1, 101));
    assertThrows(StaleBrokerEpochException.class, () -> clusterControl.checkBrokerEpoch(2, 100));
    assertFalse(clusterControl.unfenced(0));
    assertFalse(clusterControl.unfenced(1));
    UnfenceBrokerRecord unfenceBrokerRecord = new UnfenceBrokerRecord().setId(1).setEpoch(100);
    clusterControl.replay(unfenceBrokerRecord);
    assertFalse(clusterControl.unfenced(0));
    assertTrue(clusterControl.unfenced(1));
}
Also used : 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) LogContext(org.apache.kafka.common.utils.LogContext) 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 9 with RegisterBrokerRecord

use of org.apache.kafka.common.metadata.RegisterBrokerRecord in project kafka by apache.

the class ClusterControlManagerTest method testUnregister.

@Test
public void testUnregister() throws Exception {
    RegisterBrokerRecord brokerRecord = new RegisterBrokerRecord().setBrokerId(1).setBrokerEpoch(100).setIncarnationId(Uuid.fromString("fPZv1VBsRFmnlRvmGcOW9w")).setRack("arack");
    brokerRecord.endPoints().add(new BrokerEndpoint().setSecurityProtocol(SecurityProtocol.PLAINTEXT.id).setPort((short) 9092).setName("PLAINTEXT").setHost("example.com"));
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    ClusterControlManager clusterControl = new ClusterControlManager(new LogContext(), Uuid.randomUuid().toString(), new MockTime(0, 0, 0), snapshotRegistry, 1000, new StripedReplicaPlacer(new Random()), new MockControllerMetrics());
    clusterControl.activate();
    clusterControl.replay(brokerRecord);
    assertEquals(new BrokerRegistration(1, 100, Uuid.fromString("fPZv1VBsRFmnlRvmGcOW9w"), Collections.singletonMap("PLAINTEXT", new Endpoint("PLAINTEXT", SecurityProtocol.PLAINTEXT, "example.com", 9092)), Collections.emptyMap(), Optional.of("arack"), true), clusterControl.brokerRegistrations().get(1));
    UnregisterBrokerRecord unregisterRecord = new UnregisterBrokerRecord().setBrokerId(1).setBrokerEpoch(100);
    clusterControl.replay(unregisterRecord);
    assertFalse(clusterControl.brokerRegistrations().containsKey(1));
}
Also used : SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) Random(java.util.Random) Endpoint(org.apache.kafka.common.Endpoint) BrokerEndpoint(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint) RegisterBrokerRecord(org.apache.kafka.common.metadata.RegisterBrokerRecord) UnregisterBrokerRecord(org.apache.kafka.common.metadata.UnregisterBrokerRecord) BrokerEndpoint(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint) LogContext(org.apache.kafka.common.utils.LogContext) BrokerRegistration(org.apache.kafka.metadata.BrokerRegistration) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with RegisterBrokerRecord

use of org.apache.kafka.common.metadata.RegisterBrokerRecord in project kafka by apache.

the class ClusterControlManager method registerBroker.

/**
 * Process an incoming broker registration request.
 */
public ControllerResult<BrokerRegistrationReply> registerBroker(BrokerRegistrationRequestData request, long brokerEpoch, FeatureMapAndEpoch finalizedFeatures) {
    if (heartbeatManager == null) {
        throw new RuntimeException("ClusterControlManager is not active.");
    }
    if (!clusterId.equals(request.clusterId())) {
        throw new InconsistentClusterIdException("Expected cluster ID " + clusterId + ", but got cluster ID " + request.clusterId());
    }
    int brokerId = request.brokerId();
    BrokerRegistration existing = brokerRegistrations.get(brokerId);
    if (existing != null) {
        if (heartbeatManager.hasValidSession(brokerId)) {
            if (!existing.incarnationId().equals(request.incarnationId())) {
                throw new DuplicateBrokerRegistrationException("Another broker is " + "registered with that broker id.");
            }
        } else {
            if (!existing.incarnationId().equals(request.incarnationId())) {
                // Remove any existing session for the old broker incarnation.
                heartbeatManager.remove(brokerId);
                existing = null;
            }
        }
    }
    RegisterBrokerRecord record = new RegisterBrokerRecord().setBrokerId(brokerId).setIncarnationId(request.incarnationId()).setBrokerEpoch(brokerEpoch).setRack(request.rack());
    for (BrokerRegistrationRequestData.Listener listener : request.listeners()) {
        record.endPoints().add(new BrokerEndpoint().setHost(listener.host()).setName(listener.name()).setPort(listener.port()).setSecurityProtocol(listener.securityProtocol()));
    }
    for (BrokerRegistrationRequestData.Feature feature : request.features()) {
        Optional<VersionRange> finalized = finalizedFeatures.map().get(feature.name());
        if (finalized.isPresent()) {
            if (!finalized.get().contains(new VersionRange(feature.minSupportedVersion(), feature.maxSupportedVersion()))) {
                throw new UnsupportedVersionException("Unable to register because " + "the broker has an unsupported version of " + feature.name());
            }
        }
        record.features().add(new BrokerFeature().setName(feature.name()).setMinSupportedVersion(feature.minSupportedVersion()).setMaxSupportedVersion(feature.maxSupportedVersion()));
    }
    if (existing == null) {
        heartbeatManager.touch(brokerId, true, -1);
    } else {
        heartbeatManager.touch(brokerId, existing.fenced(), -1);
    }
    List<ApiMessageAndVersion> records = new ArrayList<>();
    records.add(new ApiMessageAndVersion(record, REGISTER_BROKER_RECORD.highestSupportedVersion()));
    return ControllerResult.atomicOf(records, new BrokerRegistrationReply(brokerEpoch));
}
Also used : BrokerFeature(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerFeature) BrokerRegistrationRequestData(org.apache.kafka.common.message.BrokerRegistrationRequestData) ArrayList(java.util.ArrayList) BrokerRegistrationReply(org.apache.kafka.metadata.BrokerRegistrationReply) VersionRange(org.apache.kafka.metadata.VersionRange) BrokerRegistration(org.apache.kafka.metadata.BrokerRegistration) Endpoint(org.apache.kafka.common.Endpoint) BrokerEndpoint(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint) InconsistentClusterIdException(org.apache.kafka.common.errors.InconsistentClusterIdException) DuplicateBrokerRegistrationException(org.apache.kafka.common.errors.DuplicateBrokerRegistrationException) RegisterBrokerRecord(org.apache.kafka.common.metadata.RegisterBrokerRecord) BrokerEndpoint(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Aggregations

RegisterBrokerRecord (org.apache.kafka.common.metadata.RegisterBrokerRecord)11 Test (org.junit.jupiter.api.Test)7 BrokerEndpoint (org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint)6 Endpoint (org.apache.kafka.common.Endpoint)5 LogContext (org.apache.kafka.common.utils.LogContext)5 MockTime (org.apache.kafka.common.utils.MockTime)5 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)5 SnapshotRegistry (org.apache.kafka.timeline.SnapshotRegistry)5 Random (java.util.Random)4 UnfenceBrokerRecord (org.apache.kafka.common.metadata.UnfenceBrokerRecord)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 List (java.util.List)2 BrokerFeature (org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerFeature)2 UnregisterBrokerRecord (org.apache.kafka.common.metadata.UnregisterBrokerRecord)2 BrokerRegistration (org.apache.kafka.metadata.BrokerRegistration)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Optional (java.util.Optional)1