use of org.apache.kafka.common.requests.OffsetFetchResponse in project kafka by apache.
the class ListConsumerGroupOffsetsHandlerTest method buildResponseWithPartitionError.
private OffsetFetchResponse buildResponseWithPartitionError(Errors error) {
Map<TopicPartition, PartitionData> responseData = new HashMap<>();
responseData.put(t0p0, new OffsetFetchResponse.PartitionData(10, Optional.empty(), "", Errors.NONE));
responseData.put(t0p1, new OffsetFetchResponse.PartitionData(10, Optional.empty(), "", error));
OffsetFetchResponse response = new OffsetFetchResponse(Errors.NONE, responseData);
return response;
}
use of org.apache.kafka.common.requests.OffsetFetchResponse in project kafka by apache.
the class KafkaAdminClientTest method testListConsumerGroupOffsetsRetryBackoff.
@Test
public void testListConsumerGroupOffsetsRetryBackoff() throws Exception {
MockTime time = new MockTime();
int retryBackoff = 100;
try (final AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, mockCluster(3, 0), newStrMap(AdminClientConfig.RETRY_BACKOFF_MS_CONFIG, "" + retryBackoff))) {
MockClient mockClient = env.kafkaClient();
mockClient.setNodeApiVersions(NodeApiVersions.create());
AtomicLong firstAttemptTime = new AtomicLong(0);
AtomicLong secondAttemptTime = new AtomicLong(0);
mockClient.prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
mockClient.prepareResponse(body -> {
firstAttemptTime.set(time.milliseconds());
return true;
}, new OffsetFetchResponse(Errors.NOT_COORDINATOR, Collections.emptyMap()));
mockClient.prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
mockClient.prepareResponse(body -> {
secondAttemptTime.set(time.milliseconds());
return true;
}, new OffsetFetchResponse(Errors.NONE, Collections.emptyMap()));
final KafkaFuture<Map<TopicPartition, OffsetAndMetadata>> future = env.adminClient().listConsumerGroupOffsets("group-0").partitionsToOffsetAndMetadata();
TestUtils.waitForCondition(() -> mockClient.numAwaitingResponses() == 1, "Failed awaiting ListConsumerGroupOffsets first request failure");
TestUtils.waitForCondition(() -> ((KafkaAdminClient) env.adminClient()).numPendingCalls() == 1, "Failed to add retry ListConsumerGroupOffsets call on first failure");
time.sleep(retryBackoff);
future.get();
long actualRetryBackoff = secondAttemptTime.get() - firstAttemptTime.get();
assertEquals(retryBackoff, actualRetryBackoff, "ListConsumerGroupOffsets retry did not await expected backoff!");
}
}
use of org.apache.kafka.common.requests.OffsetFetchResponse in project kafka by apache.
the class ConsumerCoordinatorTest method supportStableFlag.
private void supportStableFlag(final short upperVersion, final boolean expectThrows) {
ConsumerCoordinator coordinator = new ConsumerCoordinator(rebalanceConfig, new LogContext(), consumerClient, assignors, metadata, subscriptions, new Metrics(time), consumerId + groupId, time, false, autoCommitIntervalMs, null, true);
client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
client.setNodeApiVersions(NodeApiVersions.create(ApiKeys.OFFSET_FETCH.id, (short) 0, upperVersion));
long offset = 500L;
String metadata = "blahblah";
Optional<Integer> leaderEpoch = Optional.of(15);
OffsetFetchResponse.PartitionData data = new OffsetFetchResponse.PartitionData(offset, leaderEpoch, metadata, Errors.NONE);
client.prepareResponse(new OffsetFetchResponse(Errors.NONE, singletonMap(t1p, data)));
if (expectThrows) {
assertThrows(UnsupportedVersionException.class, () -> coordinator.fetchCommittedOffsets(singleton(t1p), time.timer(Long.MAX_VALUE)));
} else {
Map<TopicPartition, OffsetAndMetadata> fetchedOffsets = coordinator.fetchCommittedOffsets(singleton(t1p), time.timer(Long.MAX_VALUE));
assertNotNull(fetchedOffsets);
assertEquals(new OffsetAndMetadata(offset, leaderEpoch, metadata), fetchedOffsets.get(t1p));
}
}
use of org.apache.kafka.common.requests.OffsetFetchResponse in project kafka by apache.
the class ConsumerCoordinatorTest method testTopicAuthorizationFailedInOffsetFetch.
@Test
public void testTopicAuthorizationFailedInOffsetFetch() {
client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
OffsetFetchResponse.PartitionData data = new OffsetFetchResponse.PartitionData(-1, Optional.empty(), "", Errors.TOPIC_AUTHORIZATION_FAILED);
client.prepareResponse(new OffsetFetchResponse(Errors.NONE, singletonMap(t1p, data)));
TopicAuthorizationException exception = assertThrows(TopicAuthorizationException.class, () -> coordinator.fetchCommittedOffsets(singleton(t1p), time.timer(Long.MAX_VALUE)));
assertEquals(singleton(topic1), exception.unauthorizedTopics());
}
use of org.apache.kafka.common.requests.OffsetFetchResponse in project kafka by apache.
the class ListConsumerGroupOffsetsHandler method handleResponse.
@Override
public ApiResult<CoordinatorKey, Map<TopicPartition, OffsetAndMetadata>> handleResponse(Node coordinator, Set<CoordinatorKey> groupIds, AbstractResponse abstractResponse) {
validateKeys(groupIds);
final OffsetFetchResponse response = (OffsetFetchResponse) abstractResponse;
// the groupError will contain the group level error for v0-v8 OffsetFetchResponse
Errors groupError = response.groupLevelError(groupId.idValue);
if (groupError != Errors.NONE) {
final Map<CoordinatorKey, Throwable> failed = new HashMap<>();
final Set<CoordinatorKey> groupsToUnmap = new HashSet<>();
handleGroupError(groupId, groupError, failed, groupsToUnmap);
return new ApiResult<>(Collections.emptyMap(), failed, new ArrayList<>(groupsToUnmap));
} else {
final Map<TopicPartition, OffsetAndMetadata> groupOffsetsListing = new HashMap<>();
response.partitionDataMap(groupId.idValue).forEach((topicPartition, partitionData) -> {
final Errors error = partitionData.error;
if (error == Errors.NONE) {
final long offset = partitionData.offset;
final String metadata = partitionData.metadata;
final Optional<Integer> leaderEpoch = partitionData.leaderEpoch;
// Negative offset indicates that the group has no committed offset for this partition
if (offset < 0) {
groupOffsetsListing.put(topicPartition, null);
} else {
groupOffsetsListing.put(topicPartition, new OffsetAndMetadata(offset, leaderEpoch, metadata));
}
} else {
log.warn("Skipping return offset for {} due to error {}.", topicPartition, error);
}
});
return ApiResult.completed(groupId, groupOffsetsListing);
}
}
Aggregations