java - 点燃 SqlQuery 超时和取消 - 如果/何时抛出 QueryCancelledException
问题描述
QueryCancelledException 的文档还提到,如果查询在执行时被取消或超时,则会引发检查的异常,https://ignite.apache.org/releases/2.4.0/javadoc/org/apache/ignite/cache/query /QueryCancelledException.html
这里提到了同样的方法来取消/超时长时间运行的查询, https://apacheignite-sql.readme.io/v2.4/docs/query-cancellation
但奇怪的是所有 IgniteCache.query(..) 方法的 java 文档,https://ignite.apache.org/releases/2.4.0/javadoc/org/apache/ignite/IgniteCache.html#query-org。 apache.ignite.cache.query.Query-不声明此检查异常或任何检查异常被抛出(与 QueryCursor.getAll() 方法相同)导致混淆查询超时处理的位置和方式.
我编写了下面的代码,但无法使查询超时以快速测试我的代码路径的那部分并查看它是否正确。我希望在 IgniteCache.query(..) 方法和 QueryCursor.getAll() 及其相关方法中都会引发异常。
显然,SqlQuery.setTimeout(int timeout, TimeUnit timeUnit) 的最小超时粒度是 TimeUnit.MILLISECONDS,我在初始测试期间意识到这使得强制测试超时变得更加困难。
下面的代码看起来对吗?(我想避免使用游标方法并依靠在 try-with-resources 中调用的 IgniteCache.query(..) 来检测超时)。这行得通吗?
@Scheduled(fixedDelayString = "${checkInterval}", initialDelayString = "${checkDelay}")
private final void monitorHealth() {
if(!isReady) {
return;
}
try (QueryCursor<Entry<Integer, FabricInfo>> cursor = fabricInfoCache.query(SQL_QUERY)) {
cursor.iterator();
// Reset the query time out counter..
if(retryCount != 0) {
retryCount = 0;
LOGGER.warn("Client health check query executed without getting timed out before the configured maximum number of timeout retries was reached. Reseting retryCount to zero.");
}
} catch (Exception e) {
if(e.getCause() instanceof QueryCancelledException) {
retryCount++;
LOGGER.warn("Client health check query timed out for the {} time.", retryCount);
if(retryCount > QUERY_MAX_RETRIES_VALUE) {
// Query timed out the maximum number of times..
LOGGER.error("Client health check query timed out repeatedly for the maximum number of times configured : {}. Initating a disconnect-reconnect.", retryCount);
reconnectAction();
}
} else {
if (e.getCause() instanceof IgniteClientDisconnectedException) {
LOGGER.error("Client health check query failed due to client node getting disconnected from cluster. Initating a disconnect-reconnect.", e.getCause());
} else {
// Treat other failures like CacheStoppedException, etc same as IgniteClientDisconnectedException...
LOGGER.error("Client health check query failed. Initating a disconnect-reconnect.", e.getCause());
}
reconnectAction();
}
}
}
谢谢穆图
解决方案
QueryCancelledException是从QueryCursor的方法抛出的,包装在IgniteException中,它是 RuntimeException 的子类。
调用IgniteCache#query(...)方法后不会立即执行查询。它仅在调用 QueryCursor#iterator() 方法时发生。
例如,您可以查看 Ignite 项目中的以下测试,该测试检查查询取消和超时是否得到遵守:IgniteCacheLocalQueryCancelOrTimeoutSelfTest。
推荐阅读
- google-cloud-platform - Google Text-to-speech 不作为 CloudFunction 运行
- javascript - 回调 javascript postMessage ios swift
- java - 运行 Spring MVC + Hibernate+ Rest API 项目时遇到错误
- json - 制表器不识别列?
- json - 反序列化对象c#RIOT API的json数组[不工作]
- python-3.x - 如何使用 selenium 获取 youtube 中的所有评论?
- pouchdb - 如何使用单个查询 pouchDB 获取多个文档
- python - 将包含列表的列表转换为列表,然后转换为字符串
- python - 在 Python 中预加载 MP3 短片段以消除播放间隙(用于 Alexa 技能)
- c++ - 如何实现 std::unordered_map 搜索算法?