|
| 1 | +#include "../utils/TestUtility.h" |
| 2 | + |
| 3 | +#include "kafka/AdminClient.h" |
| 4 | + |
| 5 | +#include "gtest/gtest.h" |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | + |
| 9 | + |
| 10 | +using namespace KAFKA_API; |
| 11 | + |
| 12 | + |
| 13 | +TEST(AdminClient, BrokersTimeout) |
| 14 | +{ |
| 15 | + Topic topic = Utility::getRandomString(); |
| 16 | + const int numPartitions = 5; |
| 17 | + const int replicaFactor = 3; |
| 18 | + |
| 19 | + { |
| 20 | + AdminClient adminClient(KafkaTestUtility::GetKafkaClientCommonConfig()); |
| 21 | + std::cout << "[" << Utility::getCurrentTime() << "] " << adminClient.name() << " started" << std::endl; |
| 22 | + |
| 23 | + KafkaTestUtility::PauseBrokers(); |
| 24 | + |
| 25 | + // Fetch metadata, -- timeout |
| 26 | + std::cout << "[" << Utility::getCurrentTime() << "] will ListTopics" << std::endl; |
| 27 | + { |
| 28 | + auto fetchResult = adminClient.fetchBrokerMetadata(topic, std::chrono::seconds(1)); |
| 29 | + std::cout << "[" << Utility::getCurrentTime() << "] FetchMetadata: result[" << (fetchResult ? fetchResult->toString() : "NA") << "]" << std::endl; |
| 30 | + EXPECT_FALSE(fetchResult); |
| 31 | + } |
| 32 | + |
| 33 | + // List Topics, -- timeout |
| 34 | + std::cout << "[" << Utility::getCurrentTime() << "] will ListTopics" << std::endl; |
| 35 | + { |
| 36 | + auto listResult = adminClient.listTopics(std::chrono::seconds(1)); |
| 37 | + std::cout << "[" << Utility::getCurrentTime() << "] ListTopics: result[" << listResult.detail << "]" << std::endl; |
| 38 | + EXPECT_TRUE(listResult.error.value() == RD_KAFKA_RESP_ERR__TRANSPORT || listResult.error.value() == RD_KAFKA_RESP_ERR__TIMED_OUT); |
| 39 | + EXPECT_EQ(0, listResult.topics.size()); |
| 40 | + } |
| 41 | + |
| 42 | + // Create Topics, -- timeout |
| 43 | + std::cout << "[" << Utility::getCurrentTime() << "] will CreateTopics" << std::endl; |
| 44 | + { |
| 45 | + auto createResult = adminClient.createTopics({topic}, numPartitions, replicaFactor, Properties(), std::chrono::seconds(3)); |
| 46 | + std::cout << "[" << Utility::getCurrentTime() << "] createTopics: result[" << createResult.detail << "]" << std::endl; |
| 47 | + EXPECT_TRUE(createResult.error); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + KafkaTestUtility::ResumeBrokers(); |
| 52 | + |
| 53 | + // Since the brokers might not be ready during the short time, sometimes we have to retry... |
| 54 | + constexpr int maxRetry = 5; |
| 55 | + for (int i = 0; i < maxRetry; ++i) |
| 56 | + { |
| 57 | + AdminClient adminClient(KafkaTestUtility::GetKafkaClientCommonConfig()); |
| 58 | + |
| 59 | + // Create Topics, -- success |
| 60 | + std::cout << "[" << Utility::getCurrentTime() << "] will CreateTopics" << std::endl; |
| 61 | + auto createResult = adminClient.createTopics({topic}, numPartitions, replicaFactor); |
| 62 | + std::cout << "[" << Utility::getCurrentTime() << "] CreateTopics: result[" << createResult.detail << "]" << std::endl; |
| 63 | + if (!createResult.error || createResult.error.value() == RD_KAFKA_RESP_ERR_TOPIC_ALREADY_EXISTS) |
| 64 | + { |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 69 | + |
| 70 | + auto listResult = adminClient.listTopics(std::chrono::seconds(1)); |
| 71 | + if (!listResult.error && listResult.topics.count(topic) == 1) |
| 72 | + { |
| 73 | + auto metadata = adminClient.fetchBrokerMetadata(topic, std::chrono::seconds(5), false); |
| 74 | + std::cout << "[" << Utility::getCurrentTime() << "] broker metadata: " << (metadata ? metadata->toString() : "NA") << std::endl; |
| 75 | + } |
| 76 | + |
| 77 | + EXPECT_NE(maxRetry - 1, i); |
| 78 | + } |
| 79 | + |
| 80 | + KafkaTestUtility::WaitMetadataSyncUpBetweenBrokers(); |
| 81 | + |
| 82 | + { |
| 83 | + AdminClient adminClient(KafkaTestUtility::GetKafkaClientCommonConfig()); |
| 84 | + |
| 85 | + // List Topics, -- success |
| 86 | + std::cout << "[" << Utility::getCurrentTime() << "] will ListTopics" << std::endl; |
| 87 | + { |
| 88 | + auto listResult = adminClient.listTopics(); |
| 89 | + std::cout << "[" << Utility::getCurrentTime() << "] ListTopics: result[" << listResult.detail << "]" << std::endl; |
| 90 | + EXPECT_FALSE(listResult.error); |
| 91 | + EXPECT_EQ(1, listResult.topics.count(topic)); |
| 92 | + } |
| 93 | + |
| 94 | + // Fetch metadata, -- success |
| 95 | + std::cout << "[" << Utility::getCurrentTime() << "] will FetchMetadata" << std::endl; |
| 96 | + { |
| 97 | + auto fetchResult = adminClient.fetchBrokerMetadata(topic, std::chrono::seconds(5)); |
| 98 | + std::cout << "[" << Utility::getCurrentTime() << "] FetchMetadata: result[" << (fetchResult ? fetchResult->toString() : "NA") << "]" << std::endl; |
| 99 | + EXPECT_TRUE(fetchResult); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + KafkaTestUtility::PauseBrokers(); |
| 104 | + |
| 105 | + { |
| 106 | + AdminClient adminClient(KafkaTestUtility::GetKafkaClientCommonConfig()); |
| 107 | + |
| 108 | + // Delete Topics, -- timeout |
| 109 | + std::cout << "[" << Utility::getCurrentTime() << "] will DeleteTopics" << std::endl; |
| 110 | + { |
| 111 | + auto deleteResult = adminClient.deleteTopics({topic}, std::chrono::seconds(5)); |
| 112 | + std::cout << "[" << Utility::getCurrentTime() << "] DeleteTopics: result[" << deleteResult.detail << "]" << std::endl; |
| 113 | + EXPECT_TRUE(deleteResult.error); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + KafkaTestUtility::ResumeBrokers(); |
| 118 | + |
| 119 | + // Since the brokers might not be ready during the short time, sometimes we have to retry... |
| 120 | + for (int i = 0; i < maxRetry; ++i) |
| 121 | + { |
| 122 | + AdminClient adminClient(KafkaTestUtility::GetKafkaClientCommonConfig()); |
| 123 | + // Delete Topics, -- success |
| 124 | + std::cout << "[" << Utility::getCurrentTime() << "] will DeleteTopics" << std::endl; |
| 125 | + auto deleteResult = adminClient.deleteTopics({topic}); |
| 126 | + std::cout << "[" << Utility::getCurrentTime() << "] DeleteTopics: result[" << deleteResult.detail << "]" << std::endl; |
| 127 | + // In some edge cases, the result might be "timed out", while in fact the topic had already been deleted. |
| 128 | + // Then, even we keep retrying, we could only get response of "unknown topic or partition", and this should be treated as "SUCCESS". |
| 129 | + if (!deleteResult.error || deleteResult.error.value() == RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART) |
| 130 | + { |
| 131 | + break; |
| 132 | + } |
| 133 | + |
| 134 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 135 | + EXPECT_NE(maxRetry - 1, i); |
| 136 | + } |
| 137 | + |
| 138 | + KafkaTestUtility::WaitMetadataSyncUpBetweenBrokers(); |
| 139 | + |
| 140 | + { |
| 141 | + AdminClient adminClient(KafkaTestUtility::GetKafkaClientCommonConfig()); |
| 142 | + |
| 143 | + // List Topics, -- success |
| 144 | + std::cout << "[" << Utility::getCurrentTime() << "] will ListTopics" << std::endl; |
| 145 | + { |
| 146 | + auto listResult = adminClient.listTopics(std::chrono::seconds(1)); |
| 147 | + std::cout << "[" << Utility::getCurrentTime() << "] ListTopics: result[" << listResult.detail << "]" << std::endl; |
| 148 | + EXPECT_FALSE(listResult.error); |
| 149 | + EXPECT_EQ(0, listResult.topics.count(topic)); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments