Search in sources :

Example 76 with ApiMessageAndVersion

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

the class ProducerIdControlManager method generateNextProducerId.

ControllerResult<ProducerIdsBlock> generateNextProducerId(int brokerId, long brokerEpoch) {
    clusterControlManager.checkBrokerEpoch(brokerId, brokerEpoch);
    long firstProducerIdInBlock = nextProducerId.get();
    if (firstProducerIdInBlock > Long.MAX_VALUE - ProducerIdsBlock.PRODUCER_ID_BLOCK_SIZE) {
        throw new UnknownServerException("Exhausted all producerIds as the next block's end producerId " + "has exceeded the int64 type limit");
    }
    ProducerIdsBlock block = new ProducerIdsBlock(brokerId, firstProducerIdInBlock, ProducerIdsBlock.PRODUCER_ID_BLOCK_SIZE);
    long newNextProducerId = block.nextBlockFirstId();
    ProducerIdsRecord record = new ProducerIdsRecord().setNextProducerId(newNextProducerId).setBrokerId(brokerId).setBrokerEpoch(brokerEpoch);
    return ControllerResult.of(Collections.singletonList(new ApiMessageAndVersion(record, (short) 0)), block);
}
Also used : ProducerIdsRecord(org.apache.kafka.common.metadata.ProducerIdsRecord) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ProducerIdsBlock(org.apache.kafka.server.common.ProducerIdsBlock) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException)

Example 77 with ApiMessageAndVersion

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

the class ProducerIdControlManager method iterator.

Iterator<List<ApiMessageAndVersion>> iterator(long epoch) {
    List<ApiMessageAndVersion> records = new ArrayList<>(1);
    long producerId = nextProducerId.get(epoch);
    if (producerId > 0) {
        records.add(new ApiMessageAndVersion(new ProducerIdsRecord().setNextProducerId(producerId).setBrokerId(0).setBrokerEpoch(0L), (short) 0));
    }
    return Collections.singleton(records).iterator();
}
Also used : ProducerIdsRecord(org.apache.kafka.common.metadata.ProducerIdsRecord) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ArrayList(java.util.ArrayList)

Example 78 with ApiMessageAndVersion

use of org.apache.kafka.server.common.ApiMessageAndVersion 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)

Example 79 with ApiMessageAndVersion

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

the class AclsImage method write.

public void write(Consumer<List<ApiMessageAndVersion>> out) {
    List<ApiMessageAndVersion> batch = new ArrayList<>();
    for (Entry<Uuid, StandardAcl> entry : acls.entrySet()) {
        StandardAclWithId aclWithId = new StandardAclWithId(entry.getKey(), entry.getValue());
        batch.add(new ApiMessageAndVersion(aclWithId.toRecord(), (short) 0));
    }
    out.accept(batch);
}
Also used : Uuid(org.apache.kafka.common.Uuid) StandardAclWithId(org.apache.kafka.metadata.authorizer.StandardAclWithId) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ArrayList(java.util.ArrayList) StandardAcl(org.apache.kafka.metadata.authorizer.StandardAcl)

Example 80 with ApiMessageAndVersion

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

the class ClientQuotaImage method write.

public void write(ClientQuotaEntity entity, Consumer<List<ApiMessageAndVersion>> out) {
    List<ApiMessageAndVersion> records = new ArrayList<>(quotas.size());
    for (Entry<String, Double> entry : quotas.entrySet()) {
        records.add(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(entityToData(entity)).setKey(entry.getKey()).setValue(entry.getValue()).setRemove(false), CLIENT_QUOTA_RECORD.highestSupportedVersion()));
    }
    out.accept(records);
}
Also used : ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ArrayList(java.util.ArrayList) ClientQuotaRecord(org.apache.kafka.common.metadata.ClientQuotaRecord)

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