-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Enable query cancellation for MSQE + cancel using client-provided id #14823
Open
albertobastos
wants to merge
31
commits into
apache:master
Choose a base branch
from
albertobastos:cancel-with-cqid
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 2 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
f7a9488
add cancelClientQuery operation for SingleStageBroker (only numerical…
albertobastos 39a4f94
avoid synchronized BiMap and checkstyle
albertobastos c969abd
Merge remote-tracking branch 'origin' into cancel-with-cqid
albertobastos 8162bc6
Merge branch 'master' into cancel-with-cqid
albertobastos aa8c120
Merge branch 'master' of github.com:albertobastos/pinot into cancel-w…
albertobastos 7a5f713
add cancel feature (with queryId and clientQueryId) to MSQE, some ref…
albertobastos a9d1e49
set and delete clientRequestId on MSQE
albertobastos 97e7b5d
fix unimplemented method
albertobastos 65f73a0
fix I/O parameter and related tests
albertobastos e3a9a5e
Merge branch 'master' of github.com:albertobastos/pinot into cancel-w…
albertobastos fe5c846
add clientRequestId on response test
albertobastos ae3260c
Merge branch 'master' of github.com:albertobastos/pinot into cancel-w…
albertobastos 2eb506e
add sleep and random functions for further tests
albertobastos 5dd5409
Merge branch 'master' of github.com:albertobastos/pinot into cancel-w…
albertobastos e9bbdac
override test server conf
albertobastos 9dcc393
add missing superclass call
albertobastos 5ac7d9e
add some cancel query test using internal sleep function with a trick
albertobastos a46659c
Merge branch 'master' of github.com:albertobastos/pinot into cancel-w…
albertobastos 110fb16
bring master
albertobastos 47fb9bb
reuse same broker endpoint for internal and client-based cancellation
albertobastos 52998d3
add javadoc
albertobastos e2678af
add mapping comments
albertobastos c201cf4
refactor base broker methods
albertobastos c60b953
return immutable view instead of copy
albertobastos 6042ad2
enable sleep(ms) function only during testing
albertobastos b458a5b
reduce unit test wait time
albertobastos 9d0f335
replace constant with literal on test
albertobastos 1e00bf6
linter
albertobastos d3061ba
Merge branch 'master' of github.com:albertobastos/pinot into cancel-w…
albertobastos a0e1e83
remove embarassing npe
albertobastos d0e6393
Merge branch 'master' of github.com:albertobastos/pinot into cancel-w…
albertobastos 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 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
This file contains 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 |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.CompletionService; | ||
|
@@ -141,6 +142,7 @@ public abstract class BaseSingleStageBrokerRequestHandler extends BaseBrokerRequ | |
protected final boolean _enableDistinctCountBitmapOverride; | ||
protected final int _queryResponseLimit; | ||
protected final Map<Long, QueryServers> _queriesById; | ||
protected final Map<Long, String> _clientQueryIds; | ||
protected final boolean _enableMultistageMigrationMetric; | ||
protected ExecutorService _multistageCompileExecutor; | ||
protected BlockingQueue<Pair<String, String>> _multistageCompileQueryQueue; | ||
|
@@ -160,7 +162,13 @@ public BaseSingleStageBrokerRequestHandler(PinotConfiguration config, String bro | |
config.getProperty(Broker.CONFIG_OF_BROKER_QUERY_RESPONSE_LIMIT, Broker.DEFAULT_BROKER_QUERY_RESPONSE_LIMIT); | ||
boolean enableQueryCancellation = | ||
Boolean.parseBoolean(config.getProperty(Broker.CONFIG_OF_BROKER_ENABLE_QUERY_CANCELLATION)); | ||
_queriesById = enableQueryCancellation ? new ConcurrentHashMap<>() : null; | ||
if (enableQueryCancellation) { | ||
_queriesById = new ConcurrentHashMap<>(); | ||
_clientQueryIds = new ConcurrentHashMap<>(); | ||
} else { | ||
_queriesById = null; | ||
_clientQueryIds = null; | ||
} | ||
|
||
_enableMultistageMigrationMetric = _config.getProperty(Broker.CONFIG_OF_BROKER_ENABLE_MULTISTAGE_MIGRATION_METRIC, | ||
Broker.DEFAULT_ENABLE_MULTISTAGE_MIGRATION_METRIC); | ||
|
@@ -210,13 +218,13 @@ public void shutDown() { | |
|
||
@Override | ||
public Map<Long, String> getRunningQueries() { | ||
Preconditions.checkState(_queriesById != null, "Query cancellation is not enabled on broker"); | ||
Preconditions.checkState(isQueryCancellationEnabled(), "Query cancellation is not enabled on broker"); | ||
return _queriesById.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue()._query)); | ||
} | ||
|
||
@VisibleForTesting | ||
Set<ServerInstance> getRunningServers(long requestId) { | ||
Preconditions.checkState(_queriesById != null, "Query cancellation is not enabled on broker"); | ||
Preconditions.checkState(isQueryCancellationEnabled(), "Query cancellation is not enabled on broker"); | ||
QueryServers queryServers = _queriesById.get(requestId); | ||
return queryServers != null ? queryServers._servers : Collections.emptySet(); | ||
} | ||
|
@@ -225,7 +233,12 @@ Set<ServerInstance> getRunningServers(long requestId) { | |
public boolean cancelQuery(long requestId, int timeoutMs, Executor executor, HttpClientConnectionManager connMgr, | ||
Map<String, Integer> serverResponses) | ||
throws Exception { | ||
Preconditions.checkState(_queriesById != null, "Query cancellation is not enabled on broker"); | ||
Preconditions.checkState(isQueryCancellationEnabled(), "Query cancellation is not enabled on broker"); | ||
return cancelQueryByRequestId(requestId, timeoutMs, executor, connMgr, serverResponses); | ||
} | ||
|
||
private boolean cancelQueryByRequestId(long requestId, int timeoutMs, Executor executor, | ||
HttpClientConnectionManager connMgr, Map<String, Integer> serverResponses) throws Exception { | ||
QueryServers queryServers = _queriesById.get(requestId); | ||
if (queryServers == null) { | ||
return false; | ||
|
@@ -275,6 +288,21 @@ public boolean cancelQuery(long requestId, int timeoutMs, Executor executor, Htt | |
return true; | ||
} | ||
|
||
@Override | ||
public boolean cancelQueryByClientId(String clientQueryId, int timeoutMs, Executor executor, | ||
HttpClientConnectionManager connMgr, Map<String, Integer> serverResponses) | ||
throws Exception { | ||
Preconditions.checkState(isQueryCancellationEnabled(), "Query cancellation is not enabled on broker"); | ||
Optional<Long> requestId = _clientQueryIds.entrySet().stream() | ||
.filter(e -> clientQueryId.equals(e.getValue())).map(Map.Entry::getKey).findFirst(); | ||
if (requestId.isPresent()) { | ||
return cancelQueryByRequestId(requestId.get(), timeoutMs, executor, connMgr, serverResponses); | ||
} else { | ||
LOGGER.warn("query cancellation cannot be performed due to unknown client query id: {}", clientQueryId); | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
protected BrokerResponse handleRequest(long requestId, String query, SqlNodeAndOptions sqlNodeAndOptions, | ||
JsonNode request, @Nullable RequesterIdentity requesterIdentity, RequestContext requestContext, | ||
|
@@ -797,7 +825,7 @@ protected BrokerResponse handleRequest(long requestId, String query, SqlNodeAndO | |
} | ||
} | ||
BrokerResponseNative brokerResponse; | ||
if (_queriesById != null) { | ||
if (isQueryCancellationEnabled()) { | ||
// Start to track the running query for cancellation just before sending it out to servers to avoid any | ||
// potential failures that could happen before sending it out, like failures to calculate the routing table etc. | ||
// TODO: Even tracking the query as late as here, a potential race condition between calling cancel API and | ||
|
@@ -807,13 +835,16 @@ protected BrokerResponse handleRequest(long requestId, String query, SqlNodeAndO | |
// condition makes cancel API less reliable. This should be rare as it assumes sending queries out to | ||
// servers takes time, but will address later if needed. | ||
_queriesById.put(requestId, new QueryServers(query, offlineRoutingTable, realtimeRoutingTable)); | ||
String cqid = maybeSaveClientQueryId(requestId, sqlNodeAndOptions); | ||
LOGGER.debug("Keep track of running query: {}", requestId); | ||
try { | ||
brokerResponse = processBrokerRequest(requestId, brokerRequest, serverBrokerRequest, offlineBrokerRequest, | ||
offlineRoutingTable, realtimeBrokerRequest, realtimeRoutingTable, remainingTimeMs, serverStats, | ||
requestContext); | ||
brokerResponse.setClientRequestId(cqid); | ||
} finally { | ||
_queriesById.remove(requestId); | ||
maybeRemoveClientQueryId(requestId); | ||
LOGGER.debug("Remove track of running query: {}", requestId); | ||
} | ||
} else { | ||
|
@@ -865,6 +896,48 @@ protected BrokerResponse handleRequest(long requestId, String query, SqlNodeAndO | |
} | ||
} | ||
|
||
private String maybeSaveClientQueryId(long requestId, SqlNodeAndOptions sqlNodeAndOptions) { | ||
if (!isQueryCancellationEnabled()) { | ||
return null; | ||
} | ||
String clientQueryId = extractClientQueryId(sqlNodeAndOptions); | ||
if (StringUtils.isBlank(clientQueryId)) { | ||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (nit) in general we don't recommend returning NULL as a coding practice |
||
} | ||
String prev = _clientQueryIds.put(requestId, clientQueryId); | ||
if (!clientQueryId.equals(prev)) { | ||
LOGGER.warn("client query id override for id {} (old: {}, new: {})", requestId, prev, clientQueryId); | ||
} else { | ||
LOGGER.info("client query id stored for requestId {}: {}", requestId, clientQueryId); | ||
} | ||
return clientQueryId; | ||
} | ||
|
||
private boolean maybeRemoveClientQueryId(long requestId) { | ||
if (!isQueryCancellationEnabled()) { | ||
return false; | ||
} | ||
// we protected insertion with isBlank, so null is enough to assume that no entry exists | ||
String clientQueryId = _clientQueryIds.remove(requestId); | ||
if (clientQueryId != null) { | ||
LOGGER.debug("client query id {} removed for requestId {}", clientQueryId, requestId); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
private String extractClientQueryId(SqlNodeAndOptions sqlNodeAndOptions) { | ||
if (sqlNodeAndOptions.getOptions() == null) { | ||
return null; | ||
} | ||
return sqlNodeAndOptions.getOptions().get(QueryOptionKey.CLIENT_QUERY_ID); | ||
} | ||
|
||
private boolean isQueryCancellationEnabled() { | ||
return _queriesById != null; | ||
} | ||
|
||
@VisibleForTesting | ||
static String addRoutingPolicyInErrMsg(String errorMessage, String realtimeRoutingPolicy, | ||
String offlineRoutingPolicy) { | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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.
Could we use the same endpoint we already have?
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.
We could expand the already existing endpoint by adding a
@QueryParam
to determine if the provided id is either internal or client-based, being internal as default.The only drawback here is that internal ids are
long
whereas client ids arestring
, so type validation could no longer been done by Jersey but by the method itself.47fb9bb786
The Controller scenario is different, though. There the existing endpoint is
DELETE /query/{brokerId}/{queryId}
, but for clientid-based cancellations we do not want to know the exact broker where the query felt into, so we need an endpoint such asDELETE /clientQuery/{clientQueryId}
. Can't see how to unify these two.