-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add dynamic default query context #19144
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
Open
jtuglu1
wants to merge
4
commits into
apache:master
Choose a base branch
from
jtuglu1:dynamic-query-context
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
...c/test/java/org/apache/druid/testing/embedded/server/EmbeddedBrokerDynamicConfigTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.druid.testing.embedded.server; | ||
|
|
||
| import org.apache.druid.audit.AuditInfo; | ||
| import org.apache.druid.common.config.JacksonConfigManager; | ||
| import org.apache.druid.common.utils.IdUtils; | ||
| import org.apache.druid.indexing.common.task.TaskBuilder; | ||
| import org.apache.druid.server.QueryBlocklistRule; | ||
| import org.apache.druid.server.broker.BrokerDynamicConfig; | ||
| import org.apache.druid.server.http.BrokerDynamicConfigSyncer; | ||
| import org.apache.druid.testing.embedded.EmbeddedBroker; | ||
| import org.apache.druid.testing.embedded.EmbeddedClusterApis; | ||
| import org.apache.druid.testing.embedded.EmbeddedCoordinator; | ||
| import org.apache.druid.testing.embedded.EmbeddedDruidCluster; | ||
| import org.apache.druid.testing.embedded.EmbeddedHistorical; | ||
| import org.apache.druid.testing.embedded.EmbeddedIndexer; | ||
| import org.apache.druid.testing.embedded.EmbeddedOverlord; | ||
| import org.apache.druid.testing.embedded.indexing.Resources; | ||
| import org.apache.druid.testing.embedded.junit5.EmbeddedClusterTestBase; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.Timeout; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Integration test for broker dynamic configuration | ||
| */ | ||
| public class EmbeddedBrokerDynamicConfigTest extends EmbeddedClusterTestBase | ||
| { | ||
| // Fixed datasource ingested once for all tests; restored before each test since the | ||
| // base class @BeforeEach would otherwise assign a fresh name. | ||
| private String fixedDataSource; | ||
|
|
||
| private final EmbeddedCoordinator coordinator = new EmbeddedCoordinator(); | ||
| private final EmbeddedOverlord overlord = new EmbeddedOverlord(); | ||
| private final EmbeddedIndexer indexer = new EmbeddedIndexer(); | ||
| private final EmbeddedHistorical historical = new EmbeddedHistorical(); | ||
| private final EmbeddedBroker broker = new EmbeddedBroker(); | ||
|
|
||
| @Override | ||
| protected EmbeddedDruidCluster createCluster() | ||
| { | ||
| indexer.addProperty("druid.segment.handoff.pollDuration", "PT0.1s"); | ||
|
|
||
| return EmbeddedDruidCluster.withEmbeddedDerbyAndZookeeper() | ||
| .useLatchableEmitter() | ||
| .addServer(overlord) | ||
| .addServer(coordinator) | ||
| .addServer(indexer) | ||
| .addServer(historical) | ||
| .addServer(broker); | ||
| } | ||
|
|
||
| @BeforeAll | ||
| @Override | ||
| public void setup() throws Exception | ||
| { | ||
| fixedDataSource = EmbeddedClusterApis.createTestDatasourceName(); | ||
| dataSource = fixedDataSource; | ||
| super.setup(); | ||
| ingestData(); | ||
| cluster.callApi().waitForAllSegmentsToBeAvailable(fixedDataSource, coordinator, broker); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| @Override | ||
| protected void refreshDatasourceName() | ||
| { | ||
| dataSource = fixedDataSource; | ||
| } | ||
|
|
||
| @Test | ||
| @Timeout(30) | ||
| public void testQueryBlocklistBlocksMatchingQueries() | ||
| { | ||
| // Baseline: query succeeds before blocklist is applied | ||
| String initialResult = cluster.callApi().runSql("SELECT COUNT(*) FROM %s", dataSource); | ||
| Assertions.assertFalse(initialResult.isBlank()); | ||
|
|
||
| // Apply blocklist rule that matches all queries on this datasource | ||
| QueryBlocklistRule blockRule = new QueryBlocklistRule( | ||
| "block-test-datasource", | ||
| Set.of(dataSource), | ||
| null, | ||
| null | ||
| ); | ||
| updateBrokerDynamicConfig( | ||
| BrokerDynamicConfig.builder() | ||
| .withQueryBlocklist(List.of(blockRule)) | ||
| .build() | ||
| ); | ||
|
|
||
| // Query should now throw due to FORBIDDEN blocklist rule | ||
| Assertions.assertThrows( | ||
| RuntimeException.class, | ||
| () -> cluster.callApi().runSql("SELECT COUNT(*) FROM %s", dataSource) | ||
| ); | ||
|
|
||
| // Clear the blocklist and verify queries resume | ||
| updateBrokerDynamicConfig(BrokerDynamicConfig.builder().build()); | ||
| String finalResult = cluster.callApi().runSql("SELECT COUNT(*) FROM %s", dataSource); | ||
| Assertions.assertFalse(finalResult.isBlank()); | ||
| } | ||
|
|
||
| @Test | ||
| @Timeout(30) | ||
| public void testDynamicQueryContextTimeoutCausesQueryToFail() | ||
| { | ||
| // Baseline: query succeeds before a timeout context is applied | ||
| String initialResult = cluster.callApi().runSql("SELECT COUNT(*) FROM %s", dataSource); | ||
| Assertions.assertFalse(initialResult.isBlank()); | ||
|
|
||
| // Apply a 1ms timeout via dynamic query context — any real query will expire before responding | ||
| updateBrokerDynamicConfig( | ||
| BrokerDynamicConfig.builder() | ||
| .withQueryContext(Map.of("timeout", 1)) | ||
| .build() | ||
| ); | ||
|
|
||
| // Query should now throw due to the timeout being exceeded | ||
| Assertions.assertThrows( | ||
| RuntimeException.class, | ||
| () -> cluster.callApi().runSql("SELECT COUNT(*) FROM %s", dataSource) | ||
| ); | ||
|
|
||
| // Clear the dynamic context and verify queries resume | ||
| updateBrokerDynamicConfig(BrokerDynamicConfig.builder().build()); | ||
| String finalResult = cluster.callApi().runSql("SELECT COUNT(*) FROM %s", dataSource); | ||
| Assertions.assertFalse(finalResult.isBlank()); | ||
| } | ||
|
|
||
| private void ingestData() | ||
| { | ||
| cluster.callApi().runTask( | ||
| TaskBuilder.ofTypeIndex() | ||
| .dataSource(dataSource) | ||
| .isoTimestampColumn("time") | ||
| .csvInputFormatWithColumns("time", "item", "value") | ||
| .inlineInputSourceWithData(Resources.InlineData.CSV_10_DAYS) | ||
| .segmentGranularity("DAY") | ||
| .dimensions() | ||
| .withId(IdUtils.getRandomId()), | ||
| overlord | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Updates the broker dynamic config on the coordinator and synchronously broadcasts it | ||
| * to all brokers. | ||
| * | ||
| * Uses {@link JacksonConfigManager} directly to avoid the HTTP endpoint's builder-merge | ||
| * semantics, which cannot distinguish "clear to empty" from "not specified" when fields | ||
| * are omitted via {@code @JsonInclude(NON_EMPTY)}. | ||
| */ | ||
| private void updateBrokerDynamicConfig(BrokerDynamicConfig config) | ||
| { | ||
| coordinator.bindings() | ||
| .getInstance(JacksonConfigManager.class) | ||
| .set(BrokerDynamicConfig.CONFIG_KEY, config, new AuditInfo("test", "test@test.com", "Testing", "127.0.0.1")); | ||
| coordinator.bindings() | ||
| .getInstance(BrokerDynamicConfigSyncer.class) | ||
| .broadcastConfigToBrokers(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
processing/src/main/java/org/apache/druid/query/QueryContextProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.druid.query; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Provides the default query context applied to all incoming queries before per-query overrides are merged in. | ||
| * | ||
| * <p>On non-broker nodes this is backed by static runtime properties ({@link DefaultQueryConfig}). | ||
| * On brokers it is backed by {@code BrokerViewOfBrokerConfig}, which merges the static defaults with | ||
| * operator-supplied overrides pushed dynamically from the Coordinator. | ||
| */ | ||
| public interface QueryContextProvider | ||
| { | ||
| Map<String, Object> getContext(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the blocking we have this warning:
I think we need a similar warning for context. Maybe put this warning at the top of the "Broker dynamic configuration" section and make clear it applies to everything in here. I say this because in case default query context properties are "important" (i.e. operators really want them to always be set) they may be surprised that they are in fact not necessarily always going to be set.
This also makes me think at some point we might want to add a mode to make them actually guaranteed, i.e., block Broker startup until first sync of dynamic config. Doesn't have to be done in this PR.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I think we can make this an opt-in config on the broker, but I think it should be a separate change.