use of org.apache.kafka.clients.admin.DescribeProducersResult.PartitionProducerState in project kafka by apache.
the class DescribeProducersHandler method handleResponse.
@Override
public ApiResult<TopicPartition, PartitionProducerState> handleResponse(Node broker, Set<TopicPartition> keys, AbstractResponse abstractResponse) {
DescribeProducersResponse response = (DescribeProducersResponse) abstractResponse;
Map<TopicPartition, PartitionProducerState> completed = new HashMap<>();
Map<TopicPartition, Throwable> failed = new HashMap<>();
List<TopicPartition> unmapped = new ArrayList<>();
for (DescribeProducersResponseData.TopicResponse topicResponse : response.data().topics()) {
for (DescribeProducersResponseData.PartitionResponse partitionResponse : topicResponse.partitions()) {
TopicPartition topicPartition = new TopicPartition(topicResponse.name(), partitionResponse.partitionIndex());
Errors error = Errors.forCode(partitionResponse.errorCode());
if (error != Errors.NONE) {
ApiError apiError = new ApiError(error, partitionResponse.errorMessage());
handlePartitionError(topicPartition, apiError, failed, unmapped);
continue;
}
List<ProducerState> activeProducers = partitionResponse.activeProducers().stream().map(activeProducer -> {
OptionalLong currentTransactionFirstOffset = activeProducer.currentTxnStartOffset() < 0 ? OptionalLong.empty() : OptionalLong.of(activeProducer.currentTxnStartOffset());
OptionalInt coordinatorEpoch = activeProducer.coordinatorEpoch() < 0 ? OptionalInt.empty() : OptionalInt.of(activeProducer.coordinatorEpoch());
return new ProducerState(activeProducer.producerId(), activeProducer.producerEpoch(), activeProducer.lastSequence(), activeProducer.lastTimestamp(), coordinatorEpoch, currentTransactionFirstOffset);
}).collect(Collectors.toList());
completed.put(topicPartition, new PartitionProducerState(activeProducers));
}
}
return new ApiResult<>(completed, failed, unmapped);
}
use of org.apache.kafka.clients.admin.DescribeProducersResult.PartitionProducerState in project kafka by apache.
the class DescribeProducersHandlerTest method testCompletedResult.
@Test
public void testCompletedResult() {
TopicPartition topicPartition = new TopicPartition("foo", 5);
DescribeProducersOptions options = new DescribeProducersOptions().brokerId(1);
DescribeProducersHandler handler = newHandler(options);
PartitionResponse partitionResponse = sampleProducerState(topicPartition);
DescribeProducersResponse response = describeProducersResponse(singletonMap(topicPartition, partitionResponse));
Node node = new Node(3, "host", 1);
ApiResult<TopicPartition, PartitionProducerState> result = handler.handleResponse(node, mkSet(topicPartition), response);
assertEquals(mkSet(topicPartition), result.completedKeys.keySet());
assertEquals(emptyMap(), result.failedKeys);
assertEquals(emptyList(), result.unmappedKeys);
PartitionProducerState producerState = result.completedKeys.get(topicPartition);
assertMatchingProducers(partitionResponse, producerState);
}
use of org.apache.kafka.clients.admin.DescribeProducersResult.PartitionProducerState in project kafka by apache.
the class DescribeProducersHandlerTest method assertFatalError.
private Throwable assertFatalError(TopicPartition topicPartition, Errors error) {
ApiResult<TopicPartition, PartitionProducerState> result = handleResponseWithError(new DescribeProducersOptions(), topicPartition, error);
assertEquals(emptyMap(), result.completedKeys);
assertEquals(emptyList(), result.unmappedKeys);
assertEquals(mkSet(topicPartition), result.failedKeys.keySet());
return result.failedKeys.get(topicPartition);
}
use of org.apache.kafka.clients.admin.DescribeProducersResult.PartitionProducerState in project kafka by apache.
the class DescribeProducersHandlerTest method testFatalNotLeaderErrorIfStaticMapped.
@Test
public void testFatalNotLeaderErrorIfStaticMapped() {
TopicPartition topicPartition = new TopicPartition("foo", 5);
DescribeProducersOptions options = new DescribeProducersOptions().brokerId(1);
ApiResult<TopicPartition, PartitionProducerState> result = handleResponseWithError(options, topicPartition, Errors.NOT_LEADER_OR_FOLLOWER);
assertEquals(emptyMap(), result.completedKeys);
assertEquals(emptyList(), result.unmappedKeys);
assertEquals(mkSet(topicPartition), result.failedKeys.keySet());
Throwable exception = result.failedKeys.get(topicPartition);
assertTrue(exception instanceof NotLeaderOrFollowerException);
}
use of org.apache.kafka.clients.admin.DescribeProducersResult.PartitionProducerState in project kafka by apache.
the class TransactionsCommandTest method expectDescribeProducers.
private void expectDescribeProducers(TopicPartition topicPartition, long producerId, short producerEpoch, long lastTimestamp, OptionalInt coordinatorEpoch, OptionalLong txnStartOffset) {
PartitionProducerState partitionProducerState = new PartitionProducerState(singletonList(new ProducerState(producerId, producerEpoch, 500, lastTimestamp, coordinatorEpoch, txnStartOffset)));
DescribeProducersResult result = Mockito.mock(DescribeProducersResult.class);
Mockito.when(result.all()).thenReturn(completedFuture(singletonMap(topicPartition, partitionProducerState)));
Mockito.when(admin.describeProducers(Collections.singletonList(topicPartition), new DescribeProducersOptions())).thenReturn(result);
}
Aggregations