From 77416aff31e00b5caee49c61d5a3980b7258225c Mon Sep 17 00:00:00 2001 From: wuwen Date: Wed, 10 Jul 2024 09:56:42 +0800 Subject: [PATCH] bugfix: fix cache scheduled refresh issue (#6661) --- changes/en-us/develop.md | 3 ++- changes/zh-cn/develop.md | 2 ++ .../sql/struct/TableMetaCacheFactory.java | 14 +++++++------- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/changes/en-us/develop.md b/changes/en-us/develop.md index 44801bc04e7..e29d625f6ef 100644 --- a/changes/en-us/develop.md +++ b/changes/en-us/develop.md @@ -15,6 +15,7 @@ Add changes here for all PR submitted to the develop branch. - [[#6409](https://github.com/seata/seata/pull/6409)] fix the failure of RemotingParser to prevent AT mode from executing. - [[#6628](https://github.com/seata/seata/pull/6628)] fix Alibaba Dubbo convert error. - [[#6632](https://github.com/seata/seata/pull/6632)] fix hsf ConsumerModel convert error. +- [[#6661](https://github.com/seata/seata/pull/6661)] fix `tableMeta` cache scheduled refresh issue ### optimize: - [[#6044](https://github.com/seata/seata/pull/6044)] optimize derivative product check base on mysql @@ -35,6 +36,6 @@ Thanks to these contributors for their code commits. Please report an unintended - [ptyin](https://github.com/ptyin) - [funky-eyes](https://github.com/funky-eyes) - [laywin](https://github.com/laywin) - +- [wuwen5](https://github.com/wuwen5) Also, we receive many valuable issues, questions and advices from our community. Thanks for you all. diff --git a/changes/zh-cn/develop.md b/changes/zh-cn/develop.md index 1320e42f5cd..b8e5a9b844c 100644 --- a/changes/zh-cn/develop.md +++ b/changes/zh-cn/develop.md @@ -15,6 +15,7 @@ - [[#6409](https://github.com/seata/seata/pull/6409)] 修复 RemotingParser 失败导致 AT 模式无法执行的问题 - [[#6628](https://github.com/seata/seata/pull/6628)] 修复 Alibaba Dubbo 转换错误 - [[#6632](https://github.com/seata/seata/pull/6632)] 修复 hsf ConsumerModel 转换错误 +- [[#6661](https://github.com/seata/seata/pull/6661)] 修复`tableMeta`缓存定时刷新失效问题 ### optimize: - [[#6044](https://github.com/seata/seata/pull/6044)] 优化MySQL衍生数据库判断逻辑 @@ -35,5 +36,6 @@ - [ptyin](https://github.com/ptyin) - [funky-eyes](https://github.com/funky-eyes) - [laywin](https://github.com/laywin) +- [wuwen5](https://github.com/wuwen5) 同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。 diff --git a/rm-datasource/src/main/java/io/seata/rm/datasource/sql/struct/TableMetaCacheFactory.java b/rm-datasource/src/main/java/io/seata/rm/datasource/sql/struct/TableMetaCacheFactory.java index 848ce378084..f5c297cdd30 100644 --- a/rm-datasource/src/main/java/io/seata/rm/datasource/sql/struct/TableMetaCacheFactory.java +++ b/rm-datasource/src/main/java/io/seata/rm/datasource/sql/struct/TableMetaCacheFactory.java @@ -93,7 +93,7 @@ public static void registerTableMeta(DataSourceProxy dataSourceProxy) { */ public static void tableMetaRefreshEvent(String resourceId) { TableMetaRefreshHolder refreshHolder = TABLE_META_REFRESH_HOLDER_MAP.get(resourceId); - boolean offer = refreshHolder.tableMetaRefreshQueue.offer(System.currentTimeMillis()); + boolean offer = refreshHolder.tableMetaRefreshQueue.offer(System.nanoTime()); if (!offer) { LOGGER.error("table refresh event offer error:{}", resourceId); } @@ -110,28 +110,28 @@ static class TableMetaRefreshHolder { TableMetaRefreshHolder(DataSourceProxy dataSource) { this.dataSource = dataSource; - this.lastRefreshFinishTime = System.currentTimeMillis() - TABLE_META_REFRESH_INTERVAL_TIME; + this.lastRefreshFinishTime = System.nanoTime() - TimeUnit.MILLISECONDS.toNanos(TABLE_META_REFRESH_INTERVAL_TIME); this.tableMetaRefreshQueue = new LinkedBlockingQueue<>(MAX_QUEUE_SIZE); tableMetaRefreshExecutor.execute(() -> { while (true) { // 1. check table meta - if (ENABLE_TABLE_META_CHECKER_ENABLE - && System.currentTimeMillis() - lastRefreshFinishTime > TABLE_META_CHECKER_INTERVAL) { + if (ENABLE_TABLE_META_CHECKER_ENABLE + && System.nanoTime() - lastRefreshFinishTime > TimeUnit.MILLISECONDS.toNanos(TABLE_META_CHECKER_INTERVAL)) { tableMetaRefreshEvent(dataSource.getResourceId()); } // 2. refresh table meta try { - Long eventTime = tableMetaRefreshQueue.take(); + Long eventTime = tableMetaRefreshQueue.poll(TABLE_META_REFRESH_INTERVAL_TIME, TimeUnit.MILLISECONDS); // if it has bean refreshed not long ago, skip - if (eventTime - lastRefreshFinishTime > TABLE_META_REFRESH_INTERVAL_TIME) { + if (eventTime != null && eventTime - lastRefreshFinishTime > TimeUnit.MILLISECONDS.toNanos(TABLE_META_REFRESH_INTERVAL_TIME)) { try (Connection connection = dataSource.getConnection()) { TableMetaCache tableMetaCache = TableMetaCacheFactory.getTableMetaCache(dataSource.getDbType()); tableMetaCache.refresh(connection, dataSource.getResourceId()); } - lastRefreshFinishTime = System.currentTimeMillis(); + lastRefreshFinishTime = System.nanoTime(); } } catch (Exception exx) { LOGGER.error("table refresh error:{}", exx.getMessage(), exx);