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;
}
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);
}
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());
}
Aggregations