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

[improvement](jdbc catalog) Optimize JdbcCatalog case mapping stability #41510

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -48,6 +48,7 @@
import org.apache.doris.datasource.infoschema.ExternalMysqlDatabase;
import org.apache.doris.datasource.jdbc.JdbcExternalDatabase;
import org.apache.doris.datasource.lakesoul.LakeSoulExternalDatabase;
import org.apache.doris.datasource.mapping.IdentifierMapping;
import org.apache.doris.datasource.maxcompute.MaxComputeExternalDatabase;
import org.apache.doris.datasource.metacache.MetaCache;
import org.apache.doris.datasource.operations.ExternalMetadataOps;
Expand Down Expand Up @@ -149,6 +150,9 @@ public abstract class ExternalCatalog
protected Optional<Boolean> useMetaCache = Optional.empty();
protected MetaCache<ExternalDatabase<? extends ExternalTable>> metaCache;

protected IdentifierMapping identifierMapping;
private boolean mappingsInitialized = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why using a separator flag mappingsInitialized?
Why not just using initialized?


public ExternalCatalog() {
}

Expand Down Expand Up @@ -181,6 +185,10 @@ protected List<String> listDatabaseNames() {
}
}

// only for forward to master
protected void buildDatabaseMapping() {
}

// Will be called when creating catalog(so when as replaying)
// to add some default properties if missing.
public void setDefaultPropsIfMissing(boolean isReplay) {
Expand Down Expand Up @@ -209,6 +217,10 @@ public void checkWhenCreating() throws DdlException {
*/
public abstract List<String> listTableNames(SessionContext ctx, String dbName);

// only for forward to master
protected void buildTableMapping(SessionContext ctx, String dbName) {
}

/**
* check if the specified table exist.
*
Expand Down Expand Up @@ -273,6 +285,10 @@ public final synchronized void makeSureInitialized() {
}
initialized = true;
}
if (!mappingsInitialized) {
buildDatabaseMapping();
mappingsInitialized = true;
}
}

protected final void initLocalObjects() {
Expand Down Expand Up @@ -398,6 +414,7 @@ private List<String> getFilteredDatabaseNames() {
public void onRefresh(boolean invalidCache) {
this.objectCreated = false;
this.initialized = false;
this.mappingsInitialized = false;
synchronized (this.propLock) {
this.convertedProperties = null;
}
Expand Down Expand Up @@ -756,6 +773,7 @@ public void gsonPostProcess() throws IOException {
}
this.propLock = new byte[0];
this.initialized = false;
this.mappingsInitialized = false;
setDefaultPropsIfMissing(true);
if (tableAutoAnalyzePolicy == null) {
tableAutoAnalyzePolicy = Maps.newHashMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public abstract class ExternalDatabase<T extends ExternalTable>

private MetaCache<T> metaCache;

private boolean mappingsInitialized = false;

/**
* Create external database.
*
Expand All @@ -117,6 +119,7 @@ public void setTableExtCatalog(ExternalCatalog extCatalog) {

public void setUnInitialized(boolean invalidCache) {
this.initialized = false;
this.mappingsInitialized = false;
this.invalidCacheInInit = invalidCache;
if (extCatalog.getUseMetaCache().isPresent()) {
if (extCatalog.getUseMetaCache().get() && metaCache != null) {
Expand Down Expand Up @@ -170,6 +173,10 @@ public final synchronized void makeSureInitialized() {
}
initialized = true;
}
if (!mappingsInitialized) {
extCatalog.buildTableMapping(null, name);
mappingsInitialized = true;
}
}

public void replayInitDb(InitDatabaseLog log, ExternalCatalog catalog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.doris.datasource.jdbc.client.JdbcClient;
import org.apache.doris.datasource.jdbc.client.JdbcClientConfig;
import org.apache.doris.datasource.jdbc.client.JdbcClientException;
import org.apache.doris.datasource.mapping.DefaultIdentifierMapping;
import org.apache.doris.proto.InternalService;
import org.apache.doris.proto.InternalService.PJdbcTestConnectionRequest;
import org.apache.doris.proto.InternalService.PJdbcTestConnectionResult;
Expand All @@ -57,6 +58,7 @@
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

@Getter
public class JdbcExternalCatalog extends ExternalCatalog {
Expand Down Expand Up @@ -119,19 +121,16 @@ public void onRefresh(boolean invalidCache) {
super.onRefresh(invalidCache);
if (jdbcClient != null) {
jdbcClient.closeClient();
jdbcClient = null;
}
}

@Override
public void onRefreshCache(boolean invalidCache) {
onRefresh(invalidCache);
}

@Override
public void onClose() {
super.onClose();
if (jdbcClient != null) {
jdbcClient.closeClient();
jdbcClient = null;
}
}

Expand Down Expand Up @@ -232,8 +231,6 @@ protected void initLocalObjectsImpl() {
.setDriverUrl(getDriverUrl())
.setDriverClass(getDriverClass())
.setOnlySpecifiedDatabase(getOnlySpecifiedDatabase())
.setIsLowerCaseMetaNames(getLowerCaseMetaNames())
.setMetaNamesMapping(getMetaNamesMapping())
.setIncludeDatabaseMap(getIncludeDatabaseMap())
.setExcludeDatabaseMap(getExcludeDatabaseMap())
.setConnectionPoolMinSize(getConnectionPoolMinSize())
Expand All @@ -243,22 +240,77 @@ protected void initLocalObjectsImpl() {
.setConnectionPoolKeepAlive(isConnectionPoolKeepAlive());

jdbcClient = JdbcClient.createJdbcClient(jdbcClientConfig);
identifierMapping = new DefaultIdentifierMapping(Boolean.parseBoolean(getLowerCaseMetaNames()),
getMetaNamesMapping());
}

@Override
protected List<String> listDatabaseNames() {
return jdbcClient.getDatabaseNameList();
return getMappedDatabaseNames();
}

@Override
protected void buildDatabaseMapping() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method's logic is same as listDatabaseNames(), extract one

getMappedDatabaseNames();
}

private List<String> getMappedDatabaseNames() {
return identifierMapping.fromRemoteDatabaseName(jdbcClient.getDatabaseNameList());
}

protected String getRemoteDatabaseName(String dbName) {
return identifierMapping.toRemoteDatabaseName(dbName);
}

@Override
public List<String> listTableNames(SessionContext ctx, String dbName) {
makeSureInitialized();
return jdbcClient.getTablesNameList(dbName);
String remoteDbName = getRemoteDatabaseName(dbName);
return identifierMapping.fromRemoteTableName(remoteDbName, jdbcClient.getTablesNameList(remoteDbName));
}

@Override
protected void buildTableMapping(SessionContext ctx, String dbName) {
String remoteDbName = getRemoteDatabaseName(dbName);
identifierMapping.fromRemoteTableName(getRemoteDatabaseName(dbName),
jdbcClient.getTablesNameList(remoteDbName));
}

protected String getRemoteTableName(String dbName, String tblName) {
return identifierMapping.toRemoteTableName(getRemoteDatabaseName(dbName), tblName);
}

@Override
public boolean tableExist(SessionContext ctx, String dbName, String tblName) {
makeSureInitialized();
return jdbcClient.isTableExist(dbName, tblName);
String remoteDbName = getRemoteDatabaseName(dbName);
String remoteTblName = getRemoteTableName(dbName, tblName);
return jdbcClient.isTableExist(remoteDbName, remoteTblName);
}

public List<Column> listColumns(String dbName, String tblName) {
makeSureInitialized();
String remoteDbName = getRemoteDatabaseName(dbName);
String remoteTblName = getRemoteTableName(dbName, tblName);

List<Column> remoteColumns = jdbcClient.getColumnsFromJdbc(remoteDbName, remoteTblName);

List<String> remoteColumnNames = remoteColumns.stream()
.map(Column::getName)
.collect(Collectors.toList());
List<String> localColumnNames = identifierMapping.fromRemoteColumnName(remoteDbName, remoteTblName,
remoteColumnNames);

for (int i = 0; i < remoteColumns.size(); i++) {
remoteColumns.get(i).setName(localColumnNames.get(i));
}

return remoteColumns;
}

protected Map<String, String> getRemoteColumnNames(String dbName, String tblName) {
return identifierMapping.toRemoteColumnNames(getRemoteDatabaseName(dbName),
getRemoteTableName(dbName, tblName));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.doris.statistics.util.StatisticsUtil;
import org.apache.doris.thrift.TTableDescriptor;

import com.google.common.collect.Maps;
import org.apache.commons.text.StringSubstitutor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -86,21 +87,29 @@ public TTableDescriptor toThrift() {

@Override
public Optional<SchemaCacheValue> initSchema() {
return Optional.of(new SchemaCacheValue(((JdbcExternalCatalog) catalog).getJdbcClient()
.getColumnsFromJdbc(dbName, name)));
return Optional.of(new SchemaCacheValue(((JdbcExternalCatalog) catalog).listColumns(dbName, name)));
}

private JdbcTable toJdbcTable() {
List<Column> schema = getFullSchema();
JdbcExternalCatalog jdbcCatalog = (JdbcExternalCatalog) catalog;
String fullDbName = this.dbName + "." + this.name;
JdbcTable jdbcTable = new JdbcTable(this.id, fullDbName, schema, TableType.JDBC_EXTERNAL_TABLE);
jdbcCatalog.configureJdbcTable(jdbcTable, fullDbName);
String fullTableName = this.dbName + "." + this.name;
JdbcTable jdbcTable = new JdbcTable(this.id, fullTableName, schema, TableType.JDBC_EXTERNAL_TABLE);
jdbcCatalog.configureJdbcTable(jdbcTable, fullTableName);

// Set remote properties
jdbcTable.setRemoteDatabaseName(jdbcCatalog.getJdbcClient().getRemoteDatabaseName(this.dbName));
jdbcTable.setRemoteTableName(jdbcCatalog.getJdbcClient().getRemoteTableName(this.dbName, this.name));
jdbcTable.setRemoteColumnNames(jdbcCatalog.getJdbcClient().getRemoteColumnNames(this.dbName, this.name));
jdbcTable.setRemoteDatabaseName(jdbcCatalog.getRemoteDatabaseName(this.dbName));
jdbcTable.setRemoteTableName(jdbcCatalog.getRemoteTableName(this.dbName, this.name));
Map<String, String> remoteColumnNames = jdbcCatalog.getRemoteColumnNames(this.dbName, this.name);
if (!remoteColumnNames.isEmpty()) {
jdbcTable.setRemoteColumnNames(remoteColumnNames);
} else {
remoteColumnNames = Maps.newHashMap();
for (Column column : schema) {
remoteColumnNames.put(column.getName(), column.getName());
}
jdbcTable.setRemoteColumnNames(remoteColumnNames);
}

return jdbcTable;
}
Expand Down

This file was deleted.

Loading
Loading