-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
bugfix : check if table meta cache should be refreshed in AT mode #4734
Merged
Merged
Changes from 58 commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
ea0daa4
todo
Bughue 1ac4cbb
simulate nullpointerExp
Bughue 68f2687
fix npe
Bughue dc21dcd
fix npe
Bughue 42ee542
fix npe
Bughue 7de5d38
format
Bughue de27bf6
format
Bughue af64796
先去掉单测,让构建都完成,方便review
Bughue a7800e2
Merge branch 'develop' of github.com:seata/seata into dev_4572_new_fe…
Bughue 1679535
remove t-w-r
Bughue d6b8610
Merge branch 'develop' into dev_4572_new_feild_null
Bughue fccf6a9
Merge branch 'develop' of github.com:seata/seata into dev_4572_new_fe…
Bughue 13c8601
Merge branch 'develop' into dev_4572_new_feild_null
Bughue 6faf8e7
Merge branch 'develop' into dev_4572_new_feild_null
Bughue fbade58
Merge branch 'develop' into dev_4572_new_feild_null
Bughue 329618c
fix merge
Bughue 4056fc2
fix test
Bughue 9397cca
fix test
Bughue b2d712f
Merge branch 'develop' into dev_4572_new_feild_null
Bughue 2689577
Merge branch 'develop' into dev_4572_new_feild_null
funky-eyes c3de060
Merge branch 'develop' into dev_4572_new_feild_null
funky-eyes 8a34079
rename exp
Bughue db46e53
Merge remote-tracking branch 'origin/dev_4572_new_feild_null' into de…
Bughue b488939
Merge branch 'develop' into dev_4572_new_feild_null
funky-eyes 355f106
npe
Bughue 9209696
Merge remote-tracking branch 'origin/dev_4572_new_feild_null' into de…
Bughue 4fbfca4
optimize
Bughue 90ea894
ignore unrefreshable
Bughue 57960dd
remove todo
Bughue 363b019
checkstyle
Bughue ef60d17
checkstyle
Bughue ac4166c
Merge branch 'develop' into dev_4572_new_feild_null
funky-eyes fd7aad3
checkstyle
Bughue 125f042
Merge remote-tracking branch 'origin/dev_4572_new_feild_null' into de…
Bughue 20c1edc
changes
Bughue d1ce758
Merge branch 'develop' of github.com:seata/seata into dev_4572_new_fe…
Bughue b64d227
optimize
Bughue 6062d52
optimize
Bughue 933c3b9
private tableMeta
Bughue 8db15a3
Merge branch 'develop' of github.com:seata/seata into dev_4572_new_fe…
Bughue ccf9ecf
tableMeta refresh event
Bughue e8b9a6b
check style
Bughue c901589
check style
Bughue 072f609
currentTimeMillis
Bughue 026f1b5
check style
Bughue 996bfb5
Merge branch 'develop' into dev_4572_new_feild_null
Bughue 1ac5bf2
comment
Bughue 711e659
Merge branch 'develop' into dev_4572_new_feild_null
Bughue eb47a94
comment
Bughue 332081b
Merge remote-tracking branch 'origin/dev_4572_new_feild_null' into de…
Bughue ef87ff3
fix test
Bughue 3b85ba1
Merge branch 'develop' into dev_4572_new_feild_null
Bughue de81450
fix test
Bughue 155c5f0
Merge remote-tracking branch 'origin/dev_4572_new_feild_null' into de…
Bughue a8d8781
fix test
Bughue febc6a0
fix test
Bughue 9672177
Merge branch 'develop' into dev_4572_new_feild_null
Bughue 16ca49a
Merge branch 'develop' into dev_4572_new_feild_null
Bughue 8b22aef
optimize
Bughue 5b4e206
Merge remote-tracking branch 'origin/dev_4572_new_feild_null' into de…
Bughue 96bae53
optimize
Bughue 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
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
38 changes: 38 additions & 0 deletions
38
rm-datasource/src/main/java/io/seata/rm/datasource/exception/TableMetaException.java
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* Licensed 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 io.seata.rm.datasource.exception; | ||
|
||
import java.sql.SQLException; | ||
|
||
/** | ||
* The type TableMetaException exception. | ||
* | ||
* @author Bughue | ||
*/ | ||
public class TableMetaException extends SQLException { | ||
private String columnName; | ||
private String tableName; | ||
|
||
public TableMetaException(String tableName, String columnName) { | ||
this.columnName = columnName; | ||
this.tableName = tableName; | ||
} | ||
|
||
public String getTableName() { | ||
return tableName; | ||
} | ||
|
||
public String getColumnName() { | ||
return columnName; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,19 +15,53 @@ | |
*/ | ||
package io.seata.rm.datasource.sql.struct; | ||
|
||
import java.sql.Connection; | ||
import java.util.Map; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.seata.common.ConfigurationKeys; | ||
import io.seata.common.loader.EnhancedServiceLoader; | ||
import io.seata.common.thread.NamedThreadFactory; | ||
import io.seata.common.util.CollectionUtils; | ||
import io.seata.config.ConfigurationFactory; | ||
import io.seata.rm.datasource.DataSourceProxy; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static io.seata.common.DefaultValues.DEFAULT_CLIENT_TABLE_META_CHECK_ENABLE; | ||
import static io.seata.common.DefaultValues.DEFAULT_TABLE_META_CHECKER_INTERVAL; | ||
|
||
/** | ||
* @author guoyao | ||
*/ | ||
public class TableMetaCacheFactory { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(TableMetaCacheFactory.class); | ||
|
||
private static final Map<String, TableMetaCache> TABLE_META_CACHE_MAP = new ConcurrentHashMap<>(); | ||
|
||
private static final Map<String, TableMetaRefreshHolder> TABLE_META_REFRESH_HOLDER_MAP = new ConcurrentHashMap<>(); | ||
|
||
private static final long TABLE_META_REFRESH_INTERVAL_TIME = 1000L; | ||
|
||
/** | ||
* Enable the table meta checker | ||
*/ | ||
private static boolean ENABLE_TABLE_META_CHECKER_ENABLE = ConfigurationFactory.getInstance() | ||
.getBoolean(ConfigurationKeys.CLIENT_TABLE_META_CHECK_ENABLE, DEFAULT_CLIENT_TABLE_META_CHECK_ENABLE); | ||
|
||
/** | ||
* Table meta checker interval | ||
*/ | ||
private static final long TABLE_META_CHECKER_INTERVAL = ConfigurationFactory.getInstance() | ||
.getLong(ConfigurationKeys.CLIENT_TABLE_META_CHECKER_INTERVAL, DEFAULT_TABLE_META_CHECKER_INTERVAL); | ||
|
||
|
||
/** | ||
* get table meta cache | ||
* | ||
|
@@ -38,4 +72,70 @@ public static TableMetaCache getTableMetaCache(String dbType) { | |
return CollectionUtils.computeIfAbsent(TABLE_META_CACHE_MAP, dbType, | ||
key -> EnhancedServiceLoader.load(TableMetaCache.class, dbType)); | ||
} | ||
|
||
/** | ||
* register table meta | ||
* | ||
* @param dataSourceProxy | ||
*/ | ||
public static void registerTableMeta(DataSourceProxy dataSourceProxy) { | ||
TableMetaRefreshHolder holder = new TableMetaRefreshHolder(dataSourceProxy); | ||
TABLE_META_REFRESH_HOLDER_MAP.put(dataSourceProxy.getResourceId(), holder); | ||
} | ||
|
||
/** | ||
* public tableMeta refresh event | ||
*/ | ||
public static void tableMetaRefreshEvent(String resourceId) { | ||
TableMetaRefreshHolder refreshHolder = TABLE_META_REFRESH_HOLDER_MAP.get(resourceId); | ||
boolean offer = refreshHolder.tableMetaRefreshQueue.offer(System.currentTimeMillis()); | ||
if (!offer) { | ||
LOGGER.error("table refresh event offer error:{}", resourceId); | ||
} | ||
} | ||
|
||
static class TableMetaRefreshHolder { | ||
private long lastRefreshFinishTime; | ||
private DataSourceProxy dataSource; | ||
private BlockingQueue<Long> tableMetaRefreshQueue; | ||
|
||
|
||
private final Executor tableMetaRefreshExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, | ||
new LinkedBlockingQueue(), new NamedThreadFactory("tableMetaRefresh", 1, true)); | ||
|
||
TableMetaRefreshHolder(DataSourceProxy dataSource) { | ||
this.dataSource = dataSource; | ||
this.lastRefreshFinishTime = System.currentTimeMillis() - TABLE_META_REFRESH_INTERVAL_TIME; | ||
this.tableMetaRefreshQueue = new LinkedBlockingQueue<>(); | ||
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. limited the queue length |
||
|
||
tableMetaRefreshExecutor.execute(() -> { | ||
while (true) { | ||
// 1. check table meta | ||
if (ENABLE_TABLE_META_CHECKER_ENABLE | ||
&& System.currentTimeMillis() - lastRefreshFinishTime > TABLE_META_CHECKER_INTERVAL) { | ||
tableMetaRefreshEvent(dataSource.getResourceId()); | ||
} | ||
|
||
// 2. refresh table meta | ||
try { | ||
Long eventTime = tableMetaRefreshQueue.take(); | ||
// if it has bean refreshed not long ago, skip | ||
if (eventTime - lastRefreshFinishTime > TABLE_META_REFRESH_INTERVAL_TIME) { | ||
try (Connection connection = dataSource.getConnection()) { | ||
TableMetaCache tableMetaCache = | ||
TableMetaCacheFactory.getTableMetaCache(dataSource.getDbType()); | ||
tableMetaCache.refresh(connection, dataSource.getResourceId()); | ||
} | ||
lastRefreshFinishTime = System.currentTimeMillis(); | ||
} | ||
} catch (Exception exx) { | ||
LOGGER.error("table refresh error:{}", exx.getMessage(), exx); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
|
||
|
||
} | ||
} |
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 |
---|---|---|
|
@@ -33,7 +33,11 @@ | |
import javax.sql.rowset.serial.SerialDatalink; | ||
import javax.sql.rowset.serial.SerialJavaObject; | ||
import javax.sql.rowset.serial.SerialRef; | ||
|
||
import com.google.common.collect.Interner; | ||
import com.google.common.collect.Interners; | ||
import io.seata.common.exception.ShouldNeverHappenException; | ||
import io.seata.rm.datasource.exception.TableMetaException; | ||
import io.seata.rm.datasource.sql.serial.SerialArray; | ||
import static io.seata.rm.datasource.exec.oracle.OracleJdbcType.TIMESTAMP_WITH_LOCAL_TIME_ZONE; | ||
import static io.seata.rm.datasource.exec.oracle.OracleJdbcType.TIMESTAMP_WITH_TIME_ZONE; | ||
|
@@ -55,6 +59,8 @@ public class TableRecords implements java.io.Serializable { | |
|
||
private List<Row> rows = new ArrayList<Row>(); | ||
|
||
private static final Interner<String> TABLE_NAME_POOL = Interners.newWeakInterner(); | ||
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. where used? |
||
|
||
/** | ||
* Gets table name. | ||
* | ||
|
@@ -196,7 +202,7 @@ public static TableRecords buildRecords(TableMeta tmeta, ResultSet resultSet) th | |
List<Field> fields = new ArrayList<>(columnCount); | ||
for (int i = 1; i <= columnCount; i++) { | ||
String colName = resultSetMetaData.getColumnName(i); | ||
ColumnMeta col = tmeta.getColumnMeta(colName); | ||
ColumnMeta col = getColumnMeta(tmeta,colName); | ||
int dataType = col.getDataType(); | ||
Field field = new Field(); | ||
field.setName(col.getColumnName()); | ||
|
@@ -259,6 +265,20 @@ public static TableRecords buildRecords(TableMeta tmeta, ResultSet resultSet) th | |
return records; | ||
} | ||
|
||
/** | ||
* check if the column is null and return | ||
* | ||
* @param tmeta the table meta | ||
* @param colName the column nmae | ||
*/ | ||
private static ColumnMeta getColumnMeta(TableMeta tmeta , String colName) throws SQLException { | ||
ColumnMeta col = tmeta.getColumnMeta(colName); | ||
if (col == null) { | ||
throw new TableMetaException(tmeta.getTableName(), colName); | ||
} | ||
return col; | ||
} | ||
|
||
/** | ||
* since there is no parameterless constructor for Blob, Clob and NClob just like mysql, | ||
* it needs to be converted to Serial_ type | ||
|
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.
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.
原定时刷新缓存的功能在这次更新后失效了?
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.
tableMeta缓存刷新失效? #6660