Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix][broker] Fix thread safety issue in info-internal admin api for partitioned topics #19021

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.collect.Sets;
import io.netty.buffer.ByteBuf;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
Expand Down Expand Up @@ -63,6 +64,7 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.pulsar.broker.PulsarServerException;
import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.broker.admin.AdminResource;
Expand Down Expand Up @@ -1288,38 +1290,28 @@ protected void internalGetManagedLedgerInfo(AsyncResponse asyncResponse, boolean
getPartitionedTopicMetadataAsync(topicName, authoritative, false)
.thenAccept(partitionMetadata -> {
if (partitionMetadata.partitions > 0) {
final List<CompletableFuture<String>> futures =
final List<CompletableFuture<Pair<String, ManagedLedgerInfo>>> futures =
new ArrayList<>(partitionMetadata.partitions);
PartitionedManagedLedgerInfo partitionedManagedLedgerInfo = new PartitionedManagedLedgerInfo();
for (int i = 0; i < partitionMetadata.partitions; i++) {
TopicName topicNamePartition = topicName.getPartition(i);
try {
futures.add(pulsar().getAdminClient().topics()
.getInternalInfoAsync(topicNamePartition.toString())
.whenComplete((response, throwable) -> {
if (throwable != null) {
log.error("[{}] Failed to get managed info for {}",
clientAppId(), topicNamePartition, throwable);
asyncResponse.resume(new RestException(throwable));
}
.thenApply((response) -> {
try {
partitionedManagedLedgerInfo.partitions
.put(topicNamePartition.toString(), jsonMapper()
.readValue(response, ManagedLedgerInfo.class));
} catch (JsonProcessingException ex) {
log.error("[{}] Failed to parse ManagedLedgerInfo for {} from [{}]",
clientAppId(), topicNamePartition, response, ex);
return Pair.of(topicNamePartition.toString(), jsonMapper()
.readValue(response, ManagedLedgerInfo.class));
} catch (JsonProcessingException e) {
throw new UncheckedIOException(e);
}
})
);
}));
} catch (PulsarServerException e) {
log.error("[{}] Failed to get admin client while get managed info for {}" ,
clientAppId(), topicNamePartition, e);
throw new RestException(e);
}
}

FutureUtil.waitForAll(futures).handle((result, exception) -> {
FutureUtil.waitForAll(futures).whenComplete((result, exception) -> {
if (exception != null) {
Throwable t = exception.getCause();
if (t instanceof NotFoundException) {
Expand All @@ -1330,10 +1322,15 @@ protected void internalGetManagedLedgerInfo(AsyncResponse asyncResponse, boolean
asyncResponse.resume(new RestException(t));
}
}
PartitionedManagedLedgerInfo partitionedManagedLedgerInfo =
lhotari marked this conversation as resolved.
Show resolved Hide resolved
new PartitionedManagedLedgerInfo();
for (CompletableFuture<Pair<String, ManagedLedgerInfo>> infoFuture : futures) {
Pair<String, ManagedLedgerInfo> info = infoFuture.getNow(null);
partitionedManagedLedgerInfo.partitions.put(info.getKey(), info.getValue());
}
asyncResponse.resume((StreamingOutput) output -> {
jsonMapper().writer().writeValue(output, partitionedManagedLedgerInfo);
});
return null;
});
} else {
internalGetManagedLedgerInfoForNonPartitionedTopic(asyncResponse);
Expand Down