Skip to content
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

Use DataTypeRegistry to cache date type load and reuse in different scenario #33918

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.shardingsphere.infra.database.core.metadata.data.loader.type.TableMetaDataLoader;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeRegistry;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;

import java.sql.SQLException;
Expand Down Expand Up @@ -61,6 +62,7 @@ public static Map<String, SchemaMetaData> load(final Collection<MetaDataLoaderMa
Map<String, SchemaMetaData> result = new LinkedHashMap<>(materials.size(), 1F);
Collection<Future<Collection<SchemaMetaData>>> futures = new LinkedList<>();
for (MetaDataLoaderMaterial each : materials) {
DataTypeRegistry.load(each.getDataSource(), each.getStorageType().getType());
futures.add(EXECUTOR_SERVICE.submit(() -> load(each)));
}
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.shardingsphere.infra.database.core.metadata.database.datatype;

import com.cedarsoftware.util.CaseInsensitiveMap;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
* Data type registry.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Slf4j
public final class DataTypeRegistry {

private static final Map<String, Map<String, Integer>> DATA_TYPES = new ConcurrentHashMap<>();

/**
* Load data types.
*
* @param dataSource data source to be loaded
* @param databaseType database type
* @throws SQLWrapperException SQL wrapper exception
*/
public static void load(final DataSource dataSource, final String databaseType) {
try (Connection connection = dataSource.getConnection()) {
DatabaseMetaData databaseMetaData = connection.getMetaData();
if (!DATA_TYPES.containsKey(databaseType)) {
DATA_TYPES.put(databaseType, new CaseInsensitiveMap<>(new DataTypeLoader().load(databaseMetaData, TypedSPILoader.getService(DatabaseType.class, databaseType))));
}
} catch (final SQLException ex) {
throw new SQLWrapperException(ex);
}
}

/**
* Get data type.
*
* @param databaseType database type
* @param dataTypeName data type name
* @return data type
*/
public static Optional<Integer> getDataType(final String databaseType, final String dataTypeName) {
if (!DATA_TYPES.containsKey(databaseType)) {
return Optional.empty();
}
if (!DATA_TYPES.get(databaseType).containsKey(dataTypeName)) {
log.warn("Cannot find data type: {}.", dataTypeName);
return Optional.empty();
}
return Optional.of(DATA_TYPES.get(databaseType).get(dataTypeName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeLoader;
import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeRegistry;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -81,16 +82,15 @@ public Collection<SchemaMetaData> load(final MetaDataLoaderMaterial material) th
private Map<String, Collection<ColumnMetaData>> loadColumnMetaDataMap(final Connection connection, final Collection<String> tables) throws SQLException {
Map<String, Collection<ColumnMetaData>> result = new HashMap<>();
try (PreparedStatement preparedStatement = connection.prepareStatement(getTableMetaDataSQL(tables))) {
Map<String, Integer> dataTypes = new DataTypeLoader().load(connection.getMetaData(), getType());
Map<String, Collection<String>> tablePrimaryKeys = loadTablePrimaryKeys(connection, tables);
Map<String, Map<String, Boolean>> tableGenerated = loadTableGenerated(connection, tables);
preparedStatement.setString(1, connection.getCatalog());
preparedStatement.setString(2, "PUBLIC");
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
ColumnMetaData columnMetaData = loadColumnMetaData(dataTypes, resultSet, tablePrimaryKeys.getOrDefault(tableName, Collections.emptyList()),
tableGenerated.getOrDefault(tableName, new HashMap<>()));
ColumnMetaData columnMetaData =
loadColumnMetaData(resultSet, tablePrimaryKeys.getOrDefault(tableName, Collections.emptyList()), tableGenerated.getOrDefault(tableName, new HashMap<>()));
if (!result.containsKey(tableName)) {
result.put(tableName, new LinkedList<>());
}
Expand All @@ -101,15 +101,14 @@ private Map<String, Collection<ColumnMetaData>> loadColumnMetaDataMap(final Conn
return result;
}

private ColumnMetaData loadColumnMetaData(final Map<String, Integer> dataTypeMap, final ResultSet resultSet, final Collection<String> primaryKeys,
final Map<String, Boolean> tableGenerated) throws SQLException {
private ColumnMetaData loadColumnMetaData(final ResultSet resultSet, final Collection<String> primaryKeys, final Map<String, Boolean> tableGenerated) throws SQLException {
String columnName = resultSet.getString("COLUMN_NAME");
String dataType = resultSet.getString("DATA_TYPE");
boolean primaryKey = primaryKeys.contains(columnName);
boolean generated = tableGenerated.getOrDefault(columnName, Boolean.FALSE);
boolean isVisible = resultSet.getBoolean("IS_VISIBLE");
boolean isNullable = "YES".equals(resultSet.getString("IS_NULLABLE"));
return new ColumnMetaData(columnName, dataTypeMap.get(dataType), primaryKey, generated, false, isVisible, false, isNullable);
return new ColumnMetaData(columnName, DataTypeRegistry.getDataType(getDatabaseType(), dataType).orElse(Types.OTHER), primaryKey, generated, false, isVisible, false, isNullable);
}

private String getTableMetaDataSQL(final Collection<String> tables) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeRegistry;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.h2.type.H2DatabaseType;
Expand Down Expand Up @@ -67,6 +68,7 @@ void assertLoadWithoutTables() throws SQLException {
"SELECT C.TABLE_NAME TABLE_NAME, C.COLUMN_NAME COLUMN_NAME, COALESCE(I.IS_GENERATED, FALSE) IS_GENERATED FROM INFORMATION_SCHEMA.COLUMNS C RIGHT JOIN"
+ " INFORMATION_SCHEMA.INDEXES I ON C.TABLE_NAME=I.TABLE_NAME WHERE C.TABLE_CATALOG=? AND C.TABLE_SCHEMA=?")
.executeQuery()).thenReturn(generatedInfo);
DataTypeRegistry.load(dataSource, "H2");
assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.emptyList(), "foo_ds", dataSource, new H2DatabaseType(), "sharding_db")));
}

Expand All @@ -91,6 +93,7 @@ void assertLoadWithTables() throws SQLException {
"SELECT C.TABLE_NAME TABLE_NAME, C.COLUMN_NAME COLUMN_NAME, COALESCE(I.IS_GENERATED, FALSE) IS_GENERATED FROM INFORMATION_SCHEMA.COLUMNS C"
+ " RIGHT JOIN INFORMATION_SCHEMA.INDEXES I ON C.TABLE_NAME=I.TABLE_NAME WHERE C.TABLE_CATALOG=? AND C.TABLE_SCHEMA=? AND C.TABLE_NAME IN ('tbl')")
.executeQuery()).thenReturn(generatedInfo);
DataTypeRegistry.load(dataSource, "H2");
assertTableMetaDataMap(getDialectTableMetaDataLoader().load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), "foo_ds", dataSource, new H2DatabaseType(), "sharding_db")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,16 @@
import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeLoader;
import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeRegistry;
import org.apache.thrift.TException;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;

/**
* Hive meta data loader.
Expand Down Expand Up @@ -83,27 +80,18 @@ public Collection<SchemaMetaData> load(final MetaDataLoaderMaterial material) th
}

private Collection<TableMetaData> getTableMetaData(final Collection<String> tables, final HiveMetaStoreClient storeClient, final MetaDataLoaderMaterial material) throws TException, SQLException {
Map<String, Integer> dataTypes = getDataType(material.getDataSource());
Collection<TableMetaData> result = new LinkedList<>();
for (String each : tables) {
GetTableRequest req = new GetTableRequest(material.getDefaultSchemaName(), each);
result.add(new TableMetaData(each, getColumnMetaData(storeClient.getTable(req), dataTypes), Collections.emptyList(), Collections.emptyList()));
result.add(new TableMetaData(each, getColumnMetaData(storeClient.getTable(req)), Collections.emptyList(), Collections.emptyList()));
}
return result;
}

private Map<String, Integer> getDataType(final DataSource dataSource) throws SQLException {
try (
Connection connection = dataSource.getConnection()) {
return new DataTypeLoader().load(connection.getMetaData(), getType());
}
}

private Collection<ColumnMetaData> getColumnMetaData(final Table table, final Map<String, Integer> dataTypes) {
private Collection<ColumnMetaData> getColumnMetaData(final Table table) {
Collection<ColumnMetaData> result = new LinkedList<>();
for (FieldSchema each : table.getSd().getCols()) {
result.add(new ColumnMetaData(each.getName(), null == dataTypes.get(each.getType()) ? Types.VARCHAR : dataTypes.get(each.getType()),
false, false, false, false, false, false));
result.add(new ColumnMetaData(each.getName(), DataTypeRegistry.getDataType(getDatabaseType(), each.getType()).orElse(Types.VARCHAR), false, false, false, false, false, false));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeLoader;
import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeRegistry;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.TableType;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -131,13 +132,12 @@ private Map<String, Collection<ColumnMetaData>> loadColumnMetaDataMap(final Data
try (
Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(getTableMetaDataSQL(tables))) {
Map<String, Integer> dataTypes = new DataTypeLoader().load(connection.getMetaData(), getType());
String databaseName = "".equals(connection.getCatalog()) ? GlobalDataSourceRegistry.getInstance().getCachedDatabaseTables().get(tables.iterator().next()) : connection.getCatalog();
preparedStatement.setString(1, databaseName);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
ColumnMetaData columnMetaData = loadColumnMetaData(dataTypes, resultSet);
ColumnMetaData columnMetaData = loadColumnMetaData(resultSet);
if (!result.containsKey(tableName)) {
result.put(tableName, new LinkedList<>());
}
Expand All @@ -148,7 +148,7 @@ private Map<String, Collection<ColumnMetaData>> loadColumnMetaDataMap(final Data
return result;
}

private ColumnMetaData loadColumnMetaData(final Map<String, Integer> dataTypeMap, final ResultSet resultSet) throws SQLException {
private ColumnMetaData loadColumnMetaData(final ResultSet resultSet) throws SQLException {
String columnName = resultSet.getString("COLUMN_NAME");
String dataType = resultSet.getString("DATA_TYPE");
boolean primaryKey = "PRI".equalsIgnoreCase(resultSet.getString("COLUMN_KEY"));
Expand All @@ -159,7 +159,7 @@ private ColumnMetaData loadColumnMetaData(final Map<String, Integer> dataTypeMap
boolean visible = !"INVISIBLE".equalsIgnoreCase(extra);
boolean unsigned = resultSet.getString("COLUMN_TYPE").toUpperCase().contains("UNSIGNED");
boolean nullable = "YES".equals(resultSet.getString("IS_NULLABLE"));
return new ColumnMetaData(columnName, dataTypeMap.get(dataType), primaryKey, generated, caseSensitive, visible, unsigned, nullable);
return new ColumnMetaData(columnName, DataTypeRegistry.getDataType(getDatabaseType(), dataType).orElse(Types.OTHER), primaryKey, generated, caseSensitive, visible, unsigned, nullable);
}

private String getTableMetaDataSQL(final Collection<String> tables) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.SchemaMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.database.datatype.DataTypeRegistry;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType;
Expand Down Expand Up @@ -57,6 +58,7 @@ void assertLoadWithoutTables() throws SQLException {
ResultSet indexResultSet = mockIndexMetaDataResultSet();
when(dataSource.getConnection().prepareStatement("SELECT TABLE_NAME, INDEX_NAME, NON_UNIQUE, COLUMN_NAME FROM information_schema.statistics "
+ "WHERE TABLE_SCHEMA=? and TABLE_NAME IN ('tbl') ORDER BY NON_UNIQUE, INDEX_NAME, SEQ_IN_INDEX").executeQuery()).thenReturn(indexResultSet);
DataTypeRegistry.load(dataSource, "MySQL");
assertTableMetaDataMap(dialectMetaDataLoader.load(new MetaDataLoaderMaterial(Collections.emptyList(), "foo_ds", dataSource, new MySQLDatabaseType(), "sharding_db")));
}

Expand All @@ -71,6 +73,7 @@ void assertLoadWithTables() throws SQLException {
when(dataSource.getConnection().prepareStatement("SELECT TABLE_NAME, INDEX_NAME, NON_UNIQUE, COLUMN_NAME FROM information_schema.statistics WHERE TABLE_SCHEMA=? and TABLE_NAME IN ('tbl') "
+ "ORDER BY NON_UNIQUE, INDEX_NAME, SEQ_IN_INDEX")
.executeQuery()).thenReturn(indexResultSet);
DataTypeRegistry.load(dataSource, "MySQL");
assertTableMetaDataMap(dialectMetaDataLoader.load(new MetaDataLoaderMaterial(Collections.singletonList("tbl"), "foo_ds", dataSource, new MySQLDatabaseType(), "sharding_db")));
}

Expand Down
Loading
Loading