Repository navigation
Engine=Kafka support for rack-aware consumption for Amazon MSK, IMDSv2 - #81323
Conversation
|
Tested against AWS/MSK |
|
Handles #81402 |
|
@zvonand , could you review this, please. |
zvonand
left a comment
There was a problem hiding this comment.
LGTM, but I wonder if it somehow possible to add a test (an integration test mocking AWS or some smarter way)
| DECLARE(String, kafka_sasl_mechanism, "", "SASL mechanism to use for authentication. Supported: GSSAPI, PLAIN, SCRAM-SHA-256, SCRAM-SHA-512, OAUTHBEARER.", 0) \ | ||
| DECLARE(String, kafka_sasl_username, "", "SASL username for use with the PLAIN and SASL-SCRAM-.. mechanisms", 0) \ | ||
| DECLARE(String, kafka_sasl_password, "", "SASL password for use with the PLAIN and SASL-SCRAM-.. mechanisms", 0) \ | ||
| DECLARE(String, kafka_autodetect_client_rack, "", "Use cloud facilities to set client.rack to communicate with the nearest kafka replica. Supported: 'MSK' (for AWS)", 0) \ |
There was a problem hiding this comment.
Maybe this setting itself can be an enum? (like StreamingHandleErrorMode )
There was a problem hiding this comment.
Maybe this setting itself can be an enum? (like StreamingHandleErrorMode )
It was String in 1a498f5
@filimonov believes that on/off approach is better here.
There was a problem hiding this comment.
Enum is ok. Maybe the AWS_MSK can be a better (less ambiguous) name
off / AWS_MSK and eventually others one day - like confluent_aws (i think it uses zone name not id as MSK, https://docs.confluent.io/operator/current/co-configure-rack-awareness.html)
maybe something for google managed kafka https://cloud.google.com/managed-service-for-apache-kafka/docs/overview etc.
|
Maybe would be nice to add a line to docs. Official docs from AWS: https://aws.amazon.com/blogs/big-data/reduce-network-traffic-costs-of-your-amazon-msk-consumers-with-rack-awareness/ (you need to add When configured properly that command Should output like like And then it should be enough to enable |
Can be a bit tricky - we would need to mock IMDSv2 somewhere (+have a possibility to overwrite that ip in clickhouse code). And still it will not be an end-to-end test... So the manual end-to-end test (which was done) is more important here. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
It needs to be added to Kafka2.
|
Workflow [PR], commit [8845133] Summary: ✅ AI ReviewSummaryThis PR adds Findings❌ Blockers
ClickHouse Rules
Performance & Safety
Final VerdictStatus: Minimum required actions:
|
@alexey-milovidov What I don't like about this code, is how it coexists with PlacementInfo::getAvailabilityZone() . |
| std::vector<std::pair<bool /* used if AUTO */, AZGetter>> az_getters = | ||
| { | ||
| /// order of getters reflects DB::S3::AZFacilities, except AUTO, which is skipped | ||
| {true, [](){return AWSEC2MetadataClient::getAWSZoneID();}}, /// AWS_ZONE_ID |
There was a problem hiding this comment.
getRunningAvailabilityZone now tries AWS_ZONE_ID first for AZFacilities::AUTO, but CloudPlacementInfo::initialize calls S3::getRunningAvailabilityZone() with the default AUTO. Before this PR, that path returned AWS zone name (eu-central-1a); after this change it returns zone ID (euc1-az2), which changes placement.use_imds semantics globally (Keeper/ZooKeeper client rack selection and any other PlacementInfo consumers).
Can we keep the old default behavior for generic callers (e.g. AUTO = AWS_ZONE_NAME first), and use explicit AWS_ZONE_ID only for kafka_autodetect_client_rack?
| ENGINE = MergeTree() | ||
| ORDER BY time; | ||
|
|
||
| {create_query_generator(kafka_table, "t UInt64, `e.x` String", brokers='kafka1:19092', topic_list=topname, consumer_group=topname, format='JSONEachRow', settings={'kafka_autodetect_client_rack':'AWS_ZONE_ID', 'input_format_import_nested_json':1})}; |
There was a problem hiding this comment.
kafka_autodetect_client_rack='AWS_ZONE_ID', but the setting now documents/support for AWS_ZONE_NAME, GCP_ZONE, CLICKHOUSE, and AUTO as well.
Given that provider ordering/selection is the risky part of this change, please add at least focused coverage for:
AUTOprovider order/behavior,CLICKHOUSEpath,- one negative case (unknown facility logs error and does not set
client.rack).
Without those, regressions in provider selection logic can slip through while this test still passes.
| DECLARE(String, kafka_sasl_mechanism, "", "SASL mechanism to use for authentication. Supported: GSSAPI, PLAIN, SCRAM-SHA-256, SCRAM-SHA-512, OAUTHBEARER.", 0) \ | ||
| DECLARE(String, kafka_sasl_username, "", "SASL username for use with the PLAIN and SASL-SCRAM-.. mechanisms", 0) \ | ||
| DECLARE(String, kafka_sasl_password, "", "SASL password for use with the PLAIN and SASL-SCRAM-.. mechanisms", 0) \ | ||
| DECLARE(String, kafka_autodetect_client_rack, "", "Automatically sets client.rack to prefer the nearest Kafka replicas. Supported values: 'AWS_ZONE_ID', 'AWS_ZONE_NAME', 'GCP_ZONE', 'CLICKHOUSE', 'AUTO'. Empty string disables the feature.", 0) \ |
There was a problem hiding this comment.
💡 The PR metadata currently says "currently supported value is MSK", but this implementation exposes AWS_ZONE_ID, AWS_ZONE_NAME, GCP_ZONE, CLICKHOUSE, and AUTO.
Please update the changelog entry text in the PR description so user-facing release notes match the actual setting contract.
| LOG_ERROR(params.log, "Failed to determine client.rack via facility {}.", autodetect_rack); | ||
| } | ||
| else | ||
| LOG_ERROR(params.log, "Unknown kafka_autodetect_client_rack facility."); |
There was a problem hiding this comment.
Please include the provided value and expected facilities in the message, e.g. Unknown kafka_autodetect_client_rack facility '<value>'; expected one of AWS_ZONE_ID, AWS_ZONE_NAME, GCP_ZONE, CLICKHOUSE, AUTO.
| if (token_response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) | ||
| Poco::StreamCopier::copyToString(token_rs, token_str); | ||
| if (token_str.empty()) | ||
| throw DB::Exception(ErrorCodes::AWS_ERROR, "Failed to get AWS availability zone (token). HTTP response code: {}", token_response.getStatus()); |
There was a problem hiding this comment.
getAvailabilityZoneOrException now hard-requires an IMDSv2 token (/latest/api/token) and throws immediately when token fetch fails. Unlike AWSEC2MetadataClient::getDefaultCredentialsSecurely, this path has no fallback to IMDSv1 metadata reads.
That is a backward-compatibility regression for environments where token fetch is unavailable (older/custom metadata proxies, restrictive network policies, or IMDSv1-only compatibility setups): kafka_autodetect_client_rack and placement.use_imds can no longer detect AZ even though plain metadata GET would still work.
Please mirror the existing credential logic here: if token fetch fails with a non-fatal response, retry AZ request without the token header before giving up.
There was a problem hiding this comment.
@filimonov , could you reconfirm that it is a good idea to abandon IMDSv1 completely?
There was a problem hiding this comment.
"older/custom metadata proxies, restrictive network policies, or IMDSv1-only compatibility setups" - are those here now with us? In that room? :)
We are solving concrete task for nodes living in AWS infrastructure. In AWS infrastructure currently it's impossible to have v2 not working while v1 works (you can either forbid IMDS fully, or forbid v1, so i see no real life scenarios when fallback maybe useful).
Unless somebody will show usecase - that fallback to v1 sounds like YAGNI.
| @pytest.mark.parametrize( | ||
| ("autodetect_facility", "expected_rack", "topic_name"), | ||
| [ | ||
| ("AWS_ZONE_NAME_THEN_GCP_ZONE", "eu-central-1a", "zone_awareness_auto"), |
There was a problem hiding this comment.
AUTO currently gets exercised only in the happy path where AWS metadata is available (endpoint.py always serves /latest/.../availability-zone), so this test never validates the fallback branch to GCP_ZONE.
Given the new provider-selection logic, the risky part is exactly the fallback order. Please add a focused case that makes AWS AZ lookup fail (for example by returning non-200 for AWS metadata endpoints) and verifies kafka_autodetect_client_rack='AWS_ZONE_NAME_THEN_GCP_ZONE' sets client.rack from /computeMetadata/v1/instance/zone.
| def placement_availability_zone(): | ||
| if SERVICE != "aws": | ||
| abort(404, "Not Found") | ||
| abort(404, "Not Found") |
There was a problem hiding this comment.
404 for /latest/meta-data/placement/availability-zone, so the suite never validates the direct AWS_ZONE_NAME path end-to-end (it only validates fallback behavior).
Please add one happy-path case where this endpoint returns a zone name (for example eu-central-1a) and assert kafka_autodetect_client_rack='AWS_ZONE_NAME' sets client.rack accordingly.
LLVM Coverage Report
Changed lines: 93.18% (82/88) · Uncovered code |
|
Hello @alexey-milovidov , since it is your name is written on the red flag, could you have a look, please? If I understand your request "It needs to be added to Kafka2" correctly, it is addressed, test for Kafka2 is created. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is overengineered, and we will have trouble renaming/removing various options for AZ-detection (I'd prefer it to be a single option, and possibly just take it from an explicit value in a config). However, this PR is acceptable - we can deal with it.
|
Also, it creates a tech debt with Kafka2. |
Alexey, could you explain, please. The only relation with Kafka2 that came to my mind is Kafka2 should be covered by tests. And it is covered. |
Issue ClickHouse#81402 reported that availability-zone detection failed on EC2 instances configured for "IMDSv2 only (token required)", because `getAvailabilityZoneOrException` issued a plain unauthenticated `GET` against the instance metadata service, which such instances reject with `401 Unauthorized`. The behavior was fixed in master by ClickHouse#81323 (`kafka_aws_az_aware`), which makes `getAvailabilityZoneOrException` acquire an IMDSv2 session token via `PUT /latest/api/token` and send it with the availability-zone `GET`, falling back to the token-less IMDSv1 request when the metadata service does not implement the token endpoint. That change did not add a test covering the IMDSv2-only scenario in `test_placement_info`. This adds that missing coverage: a new mock metadata server `imdsv2_server.py` that issues tokens on `PUT /latest/api/token` and rejects `GET` requests without a valid `x-aws-ec2-metadata-token` header (`401`), plus a `node_imds_v2` instance and a `test_placement_info_from_imdsv2_only` case that asserts the availability zone is still loaded against such an endpoint. The test originates from ClickHouse#103549, which proposed an equivalent fix before ClickHouse#81323 was merged; that pull request is now superseded for the production change, so only its regression test is kept here. Co-Authored-By: ashrithb <[email protected]> Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Documentation entry for user-facing changes
Details
Handles #80445
Version info
26.5.1.409