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

[fix](mtmv)use name instead of id in meta of MTMV #39355

Merged
merged 13 commits into from
Aug 21, 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
3 changes: 2 additions & 1 deletion fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ private boolean processAlterOlapTable(AlterTableStmt stmt, OlapTable olapTable,

olapTable.checkNormalStateForAlter();
boolean needProcessOutsideTableLock = false;
String oldTableName = olapTable.getName();
if (currentAlterOps.checkTableStoragePolicy(alterClauses)) {
String tableStoragePolicy = olapTable.getStoragePolicy();
String currentStoragePolicy = currentAlterOps.getTableStoragePolicy(alterClauses);
Expand Down Expand Up @@ -284,7 +285,7 @@ private boolean processAlterOlapTable(AlterTableStmt stmt, OlapTable olapTable,
throw new DdlException("Invalid alter operations: " + currentAlterOps);
}
if (needChangeMTMVState(alterClauses)) {
Env.getCurrentEnv().getMtmvService().alterTable(olapTable);
Env.getCurrentEnv().getMtmvService().alterTable(olapTable, oldTableName);
}
return needProcessOutsideTableLock;
}
Expand Down
9 changes: 9 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
import org.apache.doris.mtmv.MTMVRelation;
import org.apache.doris.mtmv.MTMVService;
import org.apache.doris.mtmv.MTMVStatus;
import org.apache.doris.mtmv.MTMVUtil;
import org.apache.doris.mysql.authenticate.AuthenticateType;
import org.apache.doris.mysql.authenticate.AuthenticatorManager;
import org.apache.doris.mysql.privilege.AccessControllerManager;
Expand Down Expand Up @@ -1716,6 +1717,14 @@ public boolean postProcessAfterMetadataReplayed(boolean waitCatalogReady) {

auth.rectifyPrivs();
catalogMgr.registerCatalogRefreshListener(this);
// MTMV needs to be compatible with old metadata, and during the compatibility process,
// it needs to wait for all catalog data to be ready, so it cannot be processed through gsonPostProcess()
// We catch all possible exceptions to avoid FE startup failure
try {
MTMVUtil.compatibleMTMV(catalogMgr);
} catch (Throwable t) {
LOG.warn("compatibleMTMV failed", t);
}
return true;
}

Expand Down
21 changes: 18 additions & 3 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.doris.common.Pair;
import org.apache.doris.common.io.Text;
import org.apache.doris.common.util.PropertyAnalyzer;
import org.apache.doris.datasource.CatalogMgr;
import org.apache.doris.job.common.TaskStatus;
import org.apache.doris.job.extensions.mtmv.MTMVTask;
import org.apache.doris.mtmv.MTMVCache;
Expand All @@ -47,9 +48,6 @@

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
//import com.google.gson.JsonElement;
//import com.google.gson.JsonObject;
//import com.google.gson.JsonParser;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -489,4 +487,21 @@ public String toInfoString() {
sb.append('}');
return sb.toString();
}

/**
* Previously, ID was used to store the related table of materialized views,
* but when the catalog is deleted, the ID will change, so name is used instead.
* The logic here is to be compatible with older versions by converting ID to name
*/
public void compatible(CatalogMgr catalogMgr) {
if (mvPartitionInfo != null) {
mvPartitionInfo.compatible(catalogMgr);
}
if (relation != null) {
relation.compatible(catalogMgr);
}
if (refreshSnapshot != null) {
refreshSnapshot.compatible(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

package org.apache.doris.event;

import org.apache.doris.common.AnalysisException;

public class DataChangeEvent extends TableEvent {
public DataChangeEvent(long ctlId, long dbId, long tableId) {
public DataChangeEvent(long ctlId, long dbId, long tableId) throws AnalysisException {
super(EventType.DATA_CHANGE, ctlId, dbId, tableId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

package org.apache.doris.event;

import org.apache.doris.common.AnalysisException;

public class DropPartitionEvent extends TableEvent {
public DropPartitionEvent(long ctlId, long dbId, long tableId) {
public DropPartitionEvent(long ctlId, long dbId, long tableId) throws AnalysisException {
super(EventType.DROP_PARTITION, ctlId, dbId, tableId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

package org.apache.doris.event;

import org.apache.doris.common.AnalysisException;

public class ReplacePartitionEvent extends TableEvent {
public ReplacePartitionEvent(long ctlId, long dbId, long tableId) {
public ReplacePartitionEvent(long ctlId, long dbId, long tableId) throws AnalysisException {
super(EventType.REPLACE_PARTITION, ctlId, dbId, tableId);
}
}
32 changes: 31 additions & 1 deletion fe/fe-core/src/main/java/org/apache/doris/event/TableEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,31 @@

package org.apache.doris.event;

import org.apache.doris.catalog.DatabaseIf;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.datasource.CatalogIf;

public abstract class TableEvent extends Event {
protected final long ctlId;
protected final String ctlName;
protected final long dbId;
protected final String dbName;
protected final long tableId;
protected final String tableName;

public TableEvent(EventType eventType, long ctlId, long dbId, long tableId) {
public TableEvent(EventType eventType, long ctlId, long dbId, long tableId) throws AnalysisException {
super(eventType);
this.ctlId = ctlId;
this.dbId = dbId;
this.tableId = tableId;
CatalogIf catalog = Env.getCurrentEnv().getCatalogMgr().getCatalogOrAnalysisException(ctlId);
DatabaseIf db = catalog.getDbOrAnalysisException(dbId);
TableIf table = db.getTableOrAnalysisException(tableId);
this.ctlName = catalog.getName();
this.dbName = db.getFullName();
this.tableName = table.getName();
}

public long getCtlId() {
Expand All @@ -41,12 +56,27 @@ public long getTableId() {
return tableId;
}

public String getCtlName() {
return ctlName;
}

public String getDbName() {
return dbName;
}

public String getTableName() {
return tableName;
}

@Override
public String toString() {
return "TableEvent{"
+ "ctlId=" + ctlId
+ ", ctlName='" + ctlName + '\''
+ ", dbId=" + dbId
+ ", dbName='" + dbName + '\''
+ ", tableId=" + tableId
+ ", tableName='" + tableName + '\''
+ "} " + super.toString();
}
}
94 changes: 72 additions & 22 deletions fe/fe-core/src/main/java/org/apache/doris/mtmv/BaseTableInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,60 +18,98 @@
package org.apache.doris.mtmv;

import org.apache.doris.catalog.DatabaseIf;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.datasource.CatalogIf;
import org.apache.doris.datasource.CatalogMgr;
import org.apache.doris.datasource.InternalCatalog;

import com.google.common.base.Objects;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class BaseTableInfo {
private static final Logger LOG = LogManager.getLogger(BaseTableInfo.class);

// The MTMV needs to record the name to avoid changing the ID after rebuilding the same named base table,
// which may make the materialized view unusable.
// The previous version stored the ID, so it is temporarily kept for compatibility with the old version
@SerializedName("ti")
@Deprecated
private long tableId;
@SerializedName("di")
@Deprecated
private long dbId;
@SerializedName("ci")
@Deprecated
private long ctlId;

public BaseTableInfo(long tableId, long dbId) {
this.tableId = java.util.Objects.requireNonNull(tableId, "tableId is null");
this.dbId = java.util.Objects.requireNonNull(dbId, "dbId is null");
this.ctlId = InternalCatalog.INTERNAL_CATALOG_ID;
}

public BaseTableInfo(long tableId, long dbId, long ctlId) {
this.tableId = java.util.Objects.requireNonNull(tableId, "tableId is null");
this.dbId = java.util.Objects.requireNonNull(dbId, "dbId is null");
this.ctlId = java.util.Objects.requireNonNull(ctlId, "ctlId is null");
}
@SerializedName("tn")
private String tableName;
@SerializedName("dn")
private String dbName;
@SerializedName("cn")
private String ctlName;

public BaseTableInfo(TableIf table) {
java.util.Objects.requireNonNull(table, "table is null");
DatabaseIf database = table.getDatabase();
java.util.Objects.requireNonNull(database, "database is null");
CatalogIf catalog = database.getCatalog();
java.util.Objects.requireNonNull(database, "catalog is null");
this.tableId = table.getId();
this.dbId = database.getId();
this.ctlId = catalog.getId();
this.tableName = table.getName();
this.dbName = database.getFullName();
this.ctlName = catalog.getName();
}

// for replay MTMV, can not use `table.getDatabase();`,because database not added to catalog
public BaseTableInfo(OlapTable table, long dbId) {
java.util.Objects.requireNonNull(table, "table is null");
this.tableId = table.getId();
this.dbId = dbId;
this.ctlId = InternalCatalog.INTERNAL_CATALOG_ID;
this.tableName = table.getName();
this.dbName = table.getDBName();
this.ctlName = InternalCatalog.INTERNAL_CATALOG_NAME;
}

public String getTableName() {
return tableName;
}

public String getDbName() {
return dbName;
}

public String getCtlName() {
return ctlName;
}

@Deprecated
public long getTableId() {
return tableId;
}

@Deprecated
public long getDbId() {
return dbId;
}

@Deprecated
public long getCtlId() {
return ctlId;
}

public void setTableName(String tableName) {
this.tableName = tableName;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -81,31 +119,43 @@ public boolean equals(Object o) {
return false;
}
BaseTableInfo that = (BaseTableInfo) o;
return Objects.equal(tableId, that.tableId)
&& Objects.equal(dbId, that.dbId)
&& Objects.equal(ctlId, that.ctlId);
// for compatibility
if (StringUtils.isEmpty(ctlName) || StringUtils.isEmpty(that.ctlName)) {
return Objects.equal(tableId, that.tableId) && Objects.equal(
dbId, that.dbId) && Objects.equal(ctlId, that.ctlId);
} else {
return Objects.equal(tableName, that.tableName) && Objects.equal(
dbName, that.dbName) && Objects.equal(ctlName, that.ctlName);
}
}

@Override
public int hashCode() {
return Objects.hashCode(tableId, dbId, ctlId);
return Objects.hashCode(tableName, dbName, ctlName);
}

@Override
public String toString() {
return "BaseTableInfo{"
+ "tableId=" + tableId
+ ", dbId=" + dbId
+ ", ctlId=" + ctlId
+ "tableName='" + tableName + '\''
+ ", dbName='" + dbName + '\''
+ ", ctlName='" + ctlName + '\''
+ '}';
}

public String getTableName() {
public void compatible(CatalogMgr catalogMgr) {
if (!StringUtils.isEmpty(ctlName)) {
return;
}
try {
return MTMVUtil.getTable(this).getName();
CatalogIf catalog = catalogMgr.getCatalogOrAnalysisException(ctlId);
DatabaseIf db = catalog.getDbOrAnalysisException(dbId);
TableIf table = db.getTableOrAnalysisException(tableId);
this.ctlName = catalog.getName();
this.dbName = db.getFullName();
this.tableName = table.getName();
} catch (AnalysisException e) {
LOG.warn("can not get table: " + this);
return "";
LOG.warn("MTMV compatible failed, ctlId: {}, dbId: {}, tableId: {}", ctlId, dbId, tableId, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public interface MTMVHookService {
*
* @param table
*/
void alterTable(Table table);
void alterTable(Table table, String oldTableName);

/**
* Triggered when pause mtmv
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void dropTable(Table table) {
}

@Override
public void alterTable(Table table) {
public void alterTable(Table table, String oldTableName) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.doris.analysis.Expr;
import org.apache.doris.catalog.Column;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.datasource.CatalogMgr;

import com.google.gson.annotations.SerializedName;

Expand Down Expand Up @@ -149,4 +150,11 @@ public String toNameString() {
+ '}';
}
}

public void compatible(CatalogMgr catalogMgr) {
if (relatedTable == null) {
return;
}
relatedTable.compatible(catalogMgr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ private static boolean isSyncWithBaseTable(MTMVRefreshContext context, String mt
}
MTMVSnapshotIf baseTableCurrentSnapshot = baseTable.getTableSnapshot(context);
return mtmv.getRefreshSnapshot()
.equalsWithBaseTable(mtmvPartitionName, baseTable.getId(), baseTableCurrentSnapshot);
.equalsWithBaseTable(mtmvPartitionName, new BaseTableInfo(baseTable), baseTableCurrentSnapshot);
}

/**
Expand Down Expand Up @@ -496,8 +496,8 @@ private static MTMVRefreshPartitionSnapshot generatePartitionSnapshot(MTMVRefres
if (!(table instanceof MTMVRelatedTableIf)) {
continue;
}
refreshPartitionSnapshot.getTables()
.put(table.getId(), ((MTMVRelatedTableIf) table).getTableSnapshot(context));
refreshPartitionSnapshot.addTableSnapshot(baseTableInfo,
((MTMVRelatedTableIf) table).getTableSnapshot(context));
}
return refreshPartitionSnapshot;
}
Expand Down
Loading
Loading