Search in sources :

Example 26 with ApiVersion

use of org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion in project kafka by apache.

the class ApiVersionsResponse method intersectForwardableApis.

/**
 * Find the common range of supported API versions between the locally
 * known range and that of another set.
 *
 * @param listenerType the listener type which constrains the set of exposed APIs
 * @param minRecordVersion min inter broker magic
 * @param activeControllerApiVersions controller ApiVersions
 * @return commonly agreed ApiVersion collection
 */
public static ApiVersionCollection intersectForwardableApis(final ApiMessageType.ListenerType listenerType, final RecordVersion minRecordVersion, final Map<ApiKeys, ApiVersion> activeControllerApiVersions) {
    ApiVersionCollection apiKeys = new ApiVersionCollection();
    for (ApiKeys apiKey : ApiKeys.apisForListener(listenerType)) {
        if (apiKey.minRequiredInterBrokerMagic <= minRecordVersion.value) {
            ApiVersion brokerApiVersion = toApiVersion(apiKey);
            final ApiVersion finalApiVersion;
            if (!apiKey.forwardable) {
                finalApiVersion = brokerApiVersion;
            } else {
                Optional<ApiVersion> intersectVersion = intersect(brokerApiVersion, activeControllerApiVersions.getOrDefault(apiKey, null));
                if (intersectVersion.isPresent()) {
                    finalApiVersion = intersectVersion.get();
                } else {
                    // Controller doesn't support this API key, or there is no intersection.
                    continue;
                }
            }
            apiKeys.add(finalApiVersion.duplicate());
        }
    }
    return apiKeys;
}
Also used : ApiVersionCollection(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion)

Example 27 with ApiVersion

use of org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion in project kafka by apache.

the class TransactionManagerTest method initializeTransactionManager.

private void initializeTransactionManager(Optional<String> transactionalId) {
    Metrics metrics = new Metrics(time);
    apiVersions.update("0", new NodeApiVersions(Arrays.asList(new ApiVersion().setApiKey(ApiKeys.INIT_PRODUCER_ID.id).setMinVersion((short) 0).setMaxVersion((short) 3), new ApiVersion().setApiKey(ApiKeys.PRODUCE.id).setMinVersion((short) 0).setMaxVersion((short) 7))));
    this.transactionManager = new TransactionManager(logContext, transactionalId.orElse(null), transactionTimeoutMs, DEFAULT_RETRY_BACKOFF_MS, apiVersions);
    int batchSize = 16 * 1024;
    int deliveryTimeoutMs = 3000;
    long totalSize = 1024 * 1024;
    String metricGrpName = "producer-metrics";
    this.brokerNode = new Node(0, "localhost", 2211);
    this.accumulator = new RecordAccumulator(logContext, batchSize, CompressionType.NONE, 0, 0L, deliveryTimeoutMs, metrics, metricGrpName, time, apiVersions, transactionManager, new BufferPool(totalSize, batchSize, metrics, time, metricGrpName));
    this.sender = new Sender(logContext, this.client, this.metadata, this.accumulator, true, MAX_REQUEST_SIZE, ACKS_ALL, MAX_RETRIES, new SenderMetricsRegistry(metrics), this.time, REQUEST_TIMEOUT, 50, transactionManager, apiVersions);
}
Also used : ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) Metrics(org.apache.kafka.common.metrics.Metrics) NodeApiVersions(org.apache.kafka.clients.NodeApiVersions) Node(org.apache.kafka.common.Node)

Example 28 with ApiVersion

use of org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion in project kafka by apache.

the class TransactionManagerTest method testAbortTransactionAndReuseSequenceNumberOnError.

@Test
public void testAbortTransactionAndReuseSequenceNumberOnError() throws InterruptedException {
    apiVersions.update("0", new NodeApiVersions(Arrays.asList(new ApiVersion().setApiKey(ApiKeys.INIT_PRODUCER_ID.id).setMinVersion((short) 0).setMaxVersion((short) 1), new ApiVersion().setApiKey(ApiKeys.PRODUCE.id).setMinVersion((short) 0).setMaxVersion((short) 7))));
    doInitTransactions();
    transactionManager.beginTransaction();
    transactionManager.maybeAddPartition(tp0);
    Future<RecordMetadata> responseFuture0 = appendToAccumulator(tp0);
    prepareAddPartitionsToTxnResponse(Errors.NONE, tp0, epoch, producerId);
    prepareProduceResponse(Errors.NONE, producerId, epoch);
    // Send AddPartitionsRequest
    runUntil(() -> transactionManager.isPartitionAdded(tp0));
    runUntil(responseFuture0::isDone);
    Future<RecordMetadata> responseFuture1 = appendToAccumulator(tp0);
    prepareProduceResponse(Errors.NONE, producerId, epoch);
    runUntil(responseFuture1::isDone);
    Future<RecordMetadata> responseFuture2 = appendToAccumulator(tp0);
    prepareProduceResponse(Errors.TOPIC_AUTHORIZATION_FAILED, producerId, epoch);
    // Receive abortable error
    runUntil(responseFuture2::isDone);
    assertTrue(transactionManager.hasAbortableError());
    TransactionalRequestResult abortResult = transactionManager.beginAbort();
    prepareEndTxnResponse(Errors.NONE, TransactionResult.ABORT, producerId, epoch);
    runUntil(abortResult::isCompleted);
    assertTrue(abortResult.isSuccessful());
    abortResult.await();
    // make sure we are ready for a transaction now.
    assertTrue(transactionManager.isReady());
    transactionManager.beginTransaction();
    transactionManager.maybeAddPartition(tp0);
    prepareAddPartitionsToTxnResponse(Errors.NONE, tp0, epoch, producerId);
    // Send AddPartitionsRequest
    runUntil(() -> transactionManager.isPartitionAdded(tp0));
    assertEquals(2, transactionManager.sequenceNumber(tp0).intValue());
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) NodeApiVersions(org.apache.kafka.clients.NodeApiVersions) Test(org.junit.jupiter.api.Test)

Aggregations

ApiVersion (org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion)28 Test (org.junit.jupiter.api.Test)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)7 ApiVersionCollection (org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersionCollection)6 ApiVersionsResponse (org.apache.kafka.common.requests.ApiVersionsResponse)6 NodeApiVersions (org.apache.kafka.clients.NodeApiVersions)5 ApiVersionsResponseData (org.apache.kafka.common.message.ApiVersionsResponseData)4 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)3 EnumSource (org.junit.jupiter.params.provider.EnumSource)3 ByteBuffer (java.nio.ByteBuffer)2 Node (org.apache.kafka.common.Node)2 DeleteGroupsResponseData (org.apache.kafka.common.message.DeleteGroupsResponseData)2 DeletableGroupResult (org.apache.kafka.common.message.DeleteGroupsResponseData.DeletableGroupResult)2 DeletableGroupResultCollection (org.apache.kafka.common.message.DeleteGroupsResponseData.DeletableGroupResultCollection)2 ListenerName (org.apache.kafka.common.network.ListenerName)2 NioEchoServer (org.apache.kafka.common.network.NioEchoServer)2 SaslChannelBuilder (org.apache.kafka.common.network.SaslChannelBuilder)2 ApiVersionsRequest (org.apache.kafka.common.requests.ApiVersionsRequest)2 DeleteGroupsResponse (org.apache.kafka.common.requests.DeleteGroupsResponse)2