use of org.apache.kafka.common.TopicPartitionReplica in project kafka by apache.
the class KafkaAdminClientTest method testDescribeReplicaLogDirs.
@Test
public void testDescribeReplicaLogDirs() throws ExecutionException, InterruptedException {
TopicPartitionReplica tpr1 = new TopicPartitionReplica("topic", 12, 1);
TopicPartitionReplica tpr2 = new TopicPartitionReplica("topic", 12, 2);
try (AdminClientUnitTestEnv env = mockClientEnv()) {
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
String broker1log0 = "/var/data/kafka0";
String broker1log1 = "/var/data/kafka1";
String broker2log0 = "/var/data/kafka2";
int broker1Log0OffsetLag = 24;
int broker1Log0PartitionSize = 987654321;
int broker1Log1PartitionSize = 123456789;
int broker1Log1OffsetLag = 4321;
env.kafkaClient().prepareResponseFrom(new DescribeLogDirsResponse(new DescribeLogDirsResponseData().setResults(asList(prepareDescribeLogDirsResult(tpr1, broker1log0, broker1Log0PartitionSize, broker1Log0OffsetLag, false), prepareDescribeLogDirsResult(tpr1, broker1log1, broker1Log1PartitionSize, broker1Log1OffsetLag, true)))), env.cluster().nodeById(tpr1.brokerId()));
env.kafkaClient().prepareResponseFrom(prepareDescribeLogDirsResponse(Errors.KAFKA_STORAGE_ERROR, broker2log0), env.cluster().nodeById(tpr2.brokerId()));
DescribeReplicaLogDirsResult result = env.adminClient().describeReplicaLogDirs(asList(tpr1, tpr2));
Map<TopicPartitionReplica, KafkaFuture<DescribeReplicaLogDirsResult.ReplicaLogDirInfo>> values = result.values();
assertEquals(TestUtils.toSet(asList(tpr1, tpr2)), values.keySet());
assertNotNull(values.get(tpr1));
assertEquals(broker1log0, values.get(tpr1).get().getCurrentReplicaLogDir());
assertEquals(broker1Log0OffsetLag, values.get(tpr1).get().getCurrentReplicaOffsetLag());
assertEquals(broker1log1, values.get(tpr1).get().getFutureReplicaLogDir());
assertEquals(broker1Log1OffsetLag, values.get(tpr1).get().getFutureReplicaOffsetLag());
assertNotNull(values.get(tpr2));
assertNull(values.get(tpr2).get().getCurrentReplicaLogDir());
assertEquals(-1, values.get(tpr2).get().getCurrentReplicaOffsetLag());
assertNull(values.get(tpr2).get().getFutureReplicaLogDir());
assertEquals(-1, values.get(tpr2).get().getFutureReplicaOffsetLag());
}
}
use of org.apache.kafka.common.TopicPartitionReplica in project kafka by apache.
the class MockAdminClient method alterReplicaLogDirs.
@Override
public synchronized AlterReplicaLogDirsResult alterReplicaLogDirs(Map<TopicPartitionReplica, String> replicaAssignment, AlterReplicaLogDirsOptions options) {
Map<TopicPartitionReplica, KafkaFuture<Void>> results = new HashMap<>();
for (Map.Entry<TopicPartitionReplica, String> entry : replicaAssignment.entrySet()) {
TopicPartitionReplica replica = entry.getKey();
String newLogDir = entry.getValue();
KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
results.put(replica, future);
List<String> dirs = brokerLogDirs.get(replica.brokerId());
if (dirs == null) {
future.completeExceptionally(new ReplicaNotAvailableException("Can't find " + replica));
} else if (!dirs.contains(newLogDir)) {
future.completeExceptionally(new KafkaStorageException("Log directory " + newLogDir + " is offline"));
} else {
TopicMetadata metadata = allTopics.get(replica.topic());
if (metadata == null || metadata.partitions.size() <= replica.partition()) {
future.completeExceptionally(new ReplicaNotAvailableException("Can't find " + replica));
} else {
String currentLogDir = metadata.partitionLogDirs.get(replica.partition());
replicaMoves.put(replica, new ReplicaLogDirInfo(currentLogDir, 0, newLogDir, 0));
future.complete(null);
}
}
}
return new AlterReplicaLogDirsResult(results);
}
use of org.apache.kafka.common.TopicPartitionReplica 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