[fix](catalog)fix when modifying comments in property, it will modify the comments in the catalog (#24857)

- fix when modifying comments in property, it will modify the comments in the catalog
- add `alter catalog modify comment` to modify comment for catalog
- abstract some logic of `alter catalog` to parent class
This commit is contained in:
zhangdong
2023-10-11 23:16:19 +08:00
committed by GitHub
parent 73c3e3ab55
commit d1f59a4025
19 changed files with 263 additions and 70 deletions

View File

@ -0,0 +1,49 @@
// 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.doris.analysis;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.UserException;
import com.google.common.base.Strings;
public class AlterCatalogCommentStmt extends AlterCatalogStmt {
private final String comment;
public AlterCatalogCommentStmt(String catalogName, String comment) {
super(catalogName);
this.comment = comment;
}
public String getComment() {
return comment;
}
@Override
public void analyze(Analyzer analyzer) throws UserException {
super.analyze(analyzer);
if (Strings.isNullOrEmpty(comment)) {
throw new AnalysisException("New comment is not set.");
}
}
@Override
public String toSql() {
return "ALTER CATALOG " + catalogName + " MODIFY COMMENT " + comment;
}
}

View File

@ -17,35 +17,24 @@
package org.apache.doris.analysis;
import org.apache.doris.catalog.Env;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.FeNameFormat;
import org.apache.doris.common.UserException;
import org.apache.doris.common.util.Util;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.qe.ConnectContext;
import com.google.common.base.Strings;
/**
* Statement for alter the catalog name.
*/
public class AlterCatalogNameStmt extends DdlStmt {
private final String catalogName;
public class AlterCatalogNameStmt extends AlterCatalogStmt {
private final String newCatalogName;
public AlterCatalogNameStmt(String catalogName, String newCatalogName) {
this.catalogName = catalogName;
super(catalogName);
this.newCatalogName = newCatalogName;
}
public String getCatalogName() {
return catalogName;
}
public String getNewCatalogName() {
return newCatalogName;
}
@ -53,17 +42,6 @@ public class AlterCatalogNameStmt extends DdlStmt {
@Override
public void analyze(Analyzer analyzer) throws UserException {
super.analyze(analyzer);
Util.checkCatalogAllRules(catalogName);
if (catalogName.equals(InternalCatalog.INTERNAL_CATALOG_NAME)) {
throw new AnalysisException("Internal catalog can't be alter.");
}
if (!Env.getCurrentEnv().getAccessManager().checkCtlPriv(
ConnectContext.get(), catalogName, PrivPredicate.ALTER)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_CATALOG_ACCESS_DENIED,
analyzer.getQualifiedUser(), catalogName);
}
if (Strings.isNullOrEmpty(newCatalogName)) {
throw new AnalysisException("New catalog name is not set");

View File

@ -17,40 +17,21 @@
package org.apache.doris.analysis;
import org.apache.doris.catalog.Env;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.UserException;
import org.apache.doris.common.util.PrintableMap;
import org.apache.doris.common.util.PropertyAnalyzer;
import org.apache.doris.common.util.Util;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.qe.ConnectContext;
import java.util.Map;
/**
* Statement for alter the catalog property.
*/
public class AlterCatalogPropertyStmt extends DdlStmt {
private final String catalogName;
private final String comment;
public class AlterCatalogPropertyStmt extends AlterCatalogStmt {
private final Map<String, String> newProperties;
public AlterCatalogPropertyStmt(String catalogName, Map<String, String> newProperties) {
this.catalogName = catalogName;
super(catalogName);
this.newProperties = newProperties;
this.comment = newProperties.getOrDefault("comment", "");
}
public String getCatalogName() {
return catalogName;
}
public String getComment() {
return comment;
}
public Map<String, String> getNewProperties() {
@ -60,16 +41,6 @@ public class AlterCatalogPropertyStmt extends DdlStmt {
@Override
public void analyze(Analyzer analyzer) throws UserException {
super.analyze(analyzer);
Util.checkCatalogAllRules(catalogName);
if (!Env.getCurrentEnv().getAccessManager().checkCtlPriv(
ConnectContext.get(), catalogName, PrivPredicate.ALTER)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_CATALOG_ACCESS_DENIED,
analyzer.getQualifiedUser(), catalogName);
}
if (catalogName.equals(InternalCatalog.INTERNAL_CATALOG_NAME)) {
throw new AnalysisException("Internal catalog can't be alter.");
}
PropertyAnalyzer.checkCatalogProperties(newProperties, true);
}

View File

@ -0,0 +1,55 @@
// 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.doris.analysis;
import org.apache.doris.catalog.Env;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.UserException;
import org.apache.doris.common.util.Util;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.qe.ConnectContext;
public class AlterCatalogStmt extends DdlStmt {
protected final String catalogName;
public AlterCatalogStmt(String catalogName) {
this.catalogName = catalogName;
}
@Override
public void analyze(Analyzer analyzer) throws UserException {
super.analyze(analyzer);
Util.checkCatalogAllRules(catalogName);
if (!Env.getCurrentEnv().getAccessManager().checkCtlPriv(
ConnectContext.get(), catalogName, PrivPredicate.ALTER)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_CATALOG_ACCESS_DENIED,
analyzer.getQualifiedUser(), catalogName);
}
if (catalogName.equals(InternalCatalog.INTERNAL_CATALOG_NAME)) {
throw new AnalysisException("Internal catalog can't be alter.");
}
}
public String getCatalogName() {
return catalogName;
}
}

View File

@ -17,6 +17,7 @@
package org.apache.doris.datasource;
import org.apache.doris.analysis.AlterCatalogCommentStmt;
import org.apache.doris.analysis.AlterCatalogNameStmt;
import org.apache.doris.analysis.AlterCatalogPropertyStmt;
import org.apache.doris.analysis.CreateCatalogStmt;
@ -55,16 +56,15 @@ public class CatalogFactory {
} else if (stmt instanceof AlterCatalogPropertyStmt) {
log.setCatalogId(catalogId);
log.setNewProps(((AlterCatalogPropertyStmt) stmt).getNewProperties());
String newComment = ((AlterCatalogPropertyStmt) stmt).getComment();
if (!Strings.isNullOrEmpty(newComment)) {
log.setComment(newComment);
}
} else if (stmt instanceof AlterCatalogNameStmt) {
log.setCatalogId(catalogId);
log.setNewCatalogName(((AlterCatalogNameStmt) stmt).getNewCatalogName());
} else if (stmt instanceof RefreshCatalogStmt) {
log.setCatalogId(catalogId);
log.setInvalidCache(((RefreshCatalogStmt) stmt).isInvalidCache());
} else if (stmt instanceof AlterCatalogCommentStmt) {
log.setCatalogId(catalogId);
log.setComment(((AlterCatalogCommentStmt) stmt).getComment());
} else {
throw new RuntimeException("Unknown stmt for catalog manager " + stmt.getClass().getSimpleName());
}

View File

@ -154,6 +154,9 @@ public interface CatalogIf<T extends DatabaseIf> {
String getComment();
default void setComment(String comment) {
}
default long getLastUpdateTime() {
return -1L;
}

View File

@ -17,6 +17,7 @@
package org.apache.doris.datasource;
import org.apache.doris.analysis.AlterCatalogCommentStmt;
import org.apache.doris.analysis.AlterCatalogNameStmt;
import org.apache.doris.analysis.AlterCatalogPropertyStmt;
import org.apache.doris.analysis.CreateCatalogStmt;
@ -332,6 +333,24 @@ public class CatalogMgr implements Writable, GsonPostProcessable {
}
}
/**
* Modify the catalog comment to a new one and write the meta log.
*/
public void alterCatalogComment(AlterCatalogCommentStmt stmt) throws UserException {
writeLock();
try {
CatalogIf catalog = nameToCatalog.get(stmt.getCatalogName());
if (catalog == null) {
throw new DdlException("No catalog found with name: " + stmt.getCatalogName());
}
CatalogLog log = CatalogFactory.createCatalogLog(catalog.getId(), stmt);
replayAlterCatalogComment(log);
Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_ALTER_CATALOG_COMMENT, log);
} finally {
writeUnlock();
}
}
/**
* Modify the catalog property and write the meta log.
*/
@ -558,6 +577,21 @@ public class CatalogMgr implements Writable, GsonPostProcessable {
}
}
/**
* Reply for alter catalog comment event.
*/
public void replayAlterCatalogComment(CatalogLog log) {
writeLock();
try {
CatalogIf catalog = idToCatalog.get(log.getCatalogId());
if (catalog != null) {
catalog.setComment(log.getComment());
}
} finally {
writeUnlock();
}
}
public List<CatalogIf> listCatalogs() {
return nameToCatalog.values().stream().collect(Collectors.toList());
}

View File

@ -413,7 +413,6 @@ public abstract class ExternalCatalog
@Override
public void modifyCatalogProps(Map<String, String> props) {
modifyComment(props);
catalogProperty.modifyCatalogProps(props);
notifyPropertiesUpdated(props);
}
@ -426,11 +425,6 @@ public abstract class ExternalCatalog
catalogProperty.rollBackCatalogProps(props);
}
private void modifyComment(Map<String, String> props) {
setComment(props.getOrDefault("comment", comment));
props.remove("comment");
}
public long getLastUpdateTime() {
return lastUpdateTime;
}

View File

@ -734,6 +734,7 @@ public class JournalEntity implements Writable {
case OperationType.OP_CREATE_CATALOG:
case OperationType.OP_DROP_CATALOG:
case OperationType.OP_ALTER_CATALOG_NAME:
case OperationType.OP_ALTER_CATALOG_COMMENT:
case OperationType.OP_ALTER_CATALOG_PROPS:
case OperationType.OP_REFRESH_CATALOG: {
data = CatalogLog.read(in);

View File

@ -921,6 +921,11 @@ public class EditLog {
env.getCatalogMgr().replayAlterCatalogName(log);
break;
}
case OperationType.OP_ALTER_CATALOG_COMMENT: {
CatalogLog log = (CatalogLog) journal.getData();
env.getCatalogMgr().replayAlterCatalogComment(log);
break;
}
case OperationType.OP_ALTER_CATALOG_PROPS: {
CatalogLog log = (CatalogLog) journal.getData();
env.getCatalogMgr().replayAlterCatalogProps(log, null, true);

View File

@ -341,6 +341,8 @@ public class OperationType {
public static final short OP_DELETE_TABLE_STATS = 457;
public static final short OP_ALTER_CATALOG_COMMENT = 458;
/**
* Get opcode name by op code.
**/

View File

@ -29,6 +29,7 @@ import org.apache.doris.analysis.AdminSetPartitionVersionStmt;
import org.apache.doris.analysis.AdminSetReplicaStatusStmt;
import org.apache.doris.analysis.AdminSetReplicaVersionStmt;
import org.apache.doris.analysis.AdminSetTableStatusStmt;
import org.apache.doris.analysis.AlterCatalogCommentStmt;
import org.apache.doris.analysis.AlterCatalogNameStmt;
import org.apache.doris.analysis.AlterCatalogPropertyStmt;
import org.apache.doris.analysis.AlterColocateGroupStmt;
@ -336,6 +337,8 @@ public class DdlExecutor {
env.getCatalogMgr().dropCatalog((DropCatalogStmt) ddlStmt);
} else if (ddlStmt instanceof AlterCatalogNameStmt) {
env.getCatalogMgr().alterCatalogName((AlterCatalogNameStmt) ddlStmt);
} else if (ddlStmt instanceof AlterCatalogCommentStmt) {
env.getCatalogMgr().alterCatalogComment((AlterCatalogCommentStmt) ddlStmt);
} else if (ddlStmt instanceof AlterCatalogPropertyStmt) {
env.getCatalogMgr().alterCatalogProps((AlterCatalogPropertyStmt) ddlStmt);
} else if (ddlStmt instanceof CleanLabelStmt) {