use of org.apache.kafka.common.message.AlterReplicaLogDirsResponseData.AlterReplicaLogDirTopicResult in project kafka by apache.
the class AlterReplicaLogDirsRequestTest method testErrorResponse.
@Test
public void testErrorResponse() {
AlterReplicaLogDirsRequestData data = new AlterReplicaLogDirsRequestData().setDirs(new AlterReplicaLogDirCollection(singletonList(new AlterReplicaLogDir().setPath("/data0").setTopics(new AlterReplicaLogDirTopicCollection(singletonList(new AlterReplicaLogDirTopic().setName("topic").setPartitions(asList(0, 1, 2))).iterator()))).iterator()));
AlterReplicaLogDirsResponse errorResponse = new AlterReplicaLogDirsRequest.Builder(data).build().getErrorResponse(123, new LogDirNotFoundException("/data0"));
assertEquals(1, errorResponse.data().results().size());
AlterReplicaLogDirTopicResult topicResponse = errorResponse.data().results().get(0);
assertEquals("topic", topicResponse.topicName());
assertEquals(3, topicResponse.partitions().size());
for (int i = 0; i < 3; i++) {
assertEquals(i, topicResponse.partitions().get(i).partitionIndex());
assertEquals(Errors.LOG_DIR_NOT_FOUND.code(), topicResponse.partitions().get(i).errorCode());
}
}
use of org.apache.kafka.common.message.AlterReplicaLogDirsResponseData.AlterReplicaLogDirTopicResult in project kafka by apache.
the class AlterReplicaLogDirsResponseTest method testErrorCounts.
@Test
public void testErrorCounts() {
AlterReplicaLogDirsResponseData data = new AlterReplicaLogDirsResponseData().setResults(asList(new AlterReplicaLogDirTopicResult().setTopicName("t0").setPartitions(asList(new AlterReplicaLogDirPartitionResult().setPartitionIndex(0).setErrorCode(Errors.LOG_DIR_NOT_FOUND.code()), new AlterReplicaLogDirPartitionResult().setPartitionIndex(1).setErrorCode(Errors.NONE.code()))), new AlterReplicaLogDirTopicResult().setTopicName("t1").setPartitions(asList(new AlterReplicaLogDirPartitionResult().setPartitionIndex(0).setErrorCode(Errors.LOG_DIR_NOT_FOUND.code())))));
Map<Errors, Integer> counts = new AlterReplicaLogDirsResponse(data).errorCounts();
assertEquals(2, counts.size());
assertEquals(Integer.valueOf(2), counts.get(Errors.LOG_DIR_NOT_FOUND));
assertEquals(Integer.valueOf(1), counts.get(Errors.NONE));
}
use of org.apache.kafka.common.message.AlterReplicaLogDirsResponseData.AlterReplicaLogDirTopicResult in project kafka by apache.
the class AlterReplicaLogDirsRequest method getErrorResponse.
public AlterReplicaLogDirsResponse getErrorResponse(int throttleTimeMs, Throwable e) {
AlterReplicaLogDirsResponseData data = new AlterReplicaLogDirsResponseData();
data.setResults(this.data.dirs().stream().flatMap(alterDir -> alterDir.topics().stream().map(topic -> new AlterReplicaLogDirTopicResult().setTopicName(topic.name()).setPartitions(topic.partitions().stream().map(partitionId -> new AlterReplicaLogDirsResponseData.AlterReplicaLogDirPartitionResult().setErrorCode(Errors.forException(e).code()).setPartitionIndex(partitionId)).collect(Collectors.toList())))).collect(Collectors.toList()));
return new AlterReplicaLogDirsResponse(data.setThrottleTimeMs(throttleTimeMs));
}
use of org.apache.kafka.common.message.AlterReplicaLogDirsResponseData.AlterReplicaLogDirTopicResult in project kafka by apache.
the class KafkaAdminClient method alterReplicaLogDirs.
@Override
public AlterReplicaLogDirsResult alterReplicaLogDirs(Map<TopicPartitionReplica, String> replicaAssignment, final AlterReplicaLogDirsOptions options) {
final Map<TopicPartitionReplica, KafkaFutureImpl<Void>> futures = new HashMap<>(replicaAssignment.size());
for (TopicPartitionReplica replica : replicaAssignment.keySet()) futures.put(replica, new KafkaFutureImpl<>());
Map<Integer, AlterReplicaLogDirsRequestData> replicaAssignmentByBroker = new HashMap<>();
for (Map.Entry<TopicPartitionReplica, String> entry : replicaAssignment.entrySet()) {
TopicPartitionReplica replica = entry.getKey();
String logDir = entry.getValue();
int brokerId = replica.brokerId();
AlterReplicaLogDirsRequestData value = replicaAssignmentByBroker.computeIfAbsent(brokerId, key -> new AlterReplicaLogDirsRequestData());
AlterReplicaLogDir alterReplicaLogDir = value.dirs().find(logDir);
if (alterReplicaLogDir == null) {
alterReplicaLogDir = new AlterReplicaLogDir();
alterReplicaLogDir.setPath(logDir);
value.dirs().add(alterReplicaLogDir);
}
AlterReplicaLogDirTopic alterReplicaLogDirTopic = alterReplicaLogDir.topics().find(replica.topic());
if (alterReplicaLogDirTopic == null) {
alterReplicaLogDirTopic = new AlterReplicaLogDirTopic().setName(replica.topic());
alterReplicaLogDir.topics().add(alterReplicaLogDirTopic);
}
alterReplicaLogDirTopic.partitions().add(replica.partition());
}
final long now = time.milliseconds();
for (Map.Entry<Integer, AlterReplicaLogDirsRequestData> entry : replicaAssignmentByBroker.entrySet()) {
final int brokerId = entry.getKey();
final AlterReplicaLogDirsRequestData assignment = entry.getValue();
runnable.call(new Call("alterReplicaLogDirs", calcDeadlineMs(now, options.timeoutMs()), new ConstantNodeIdProvider(brokerId)) {
@Override
public AlterReplicaLogDirsRequest.Builder createRequest(int timeoutMs) {
return new AlterReplicaLogDirsRequest.Builder(assignment);
}
@Override
public void handleResponse(AbstractResponse abstractResponse) {
AlterReplicaLogDirsResponse response = (AlterReplicaLogDirsResponse) abstractResponse;
for (AlterReplicaLogDirTopicResult topicResult : response.data().results()) {
for (AlterReplicaLogDirPartitionResult partitionResult : topicResult.partitions()) {
TopicPartitionReplica replica = new TopicPartitionReplica(topicResult.topicName(), partitionResult.partitionIndex(), brokerId);
KafkaFutureImpl<Void> future = futures.get(replica);
if (future == null) {
log.warn("The partition {} in the response from broker {} is not in the request", new TopicPartition(topicResult.topicName(), partitionResult.partitionIndex()), brokerId);
} else if (partitionResult.errorCode() == Errors.NONE.code()) {
future.complete(null);
} else {
future.completeExceptionally(Errors.forCode(partitionResult.errorCode()).exception());
}
}
}
// The server should send back a response for every replica. But do a sanity check anyway.
completeUnrealizedFutures(futures.entrySet().stream().filter(entry -> entry.getKey().brokerId() == brokerId), replica -> "The response from broker " + brokerId + " did not contain a result for replica " + replica);
}
@Override
void handleFailure(Throwable throwable) {
// Only completes the futures of brokerId
completeAllExceptionally(futures.entrySet().stream().filter(entry -> entry.getKey().brokerId() == brokerId).map(Map.Entry::getValue), throwable);
}
}, now);
}
return new AlterReplicaLogDirsResult(new HashMap<>(futures));
}
Aggregations