diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/BinlogConfig.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/BinlogConfig.java index 4f95737396..f0f4efbc46 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/BinlogConfig.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/BinlogConfig.java @@ -179,4 +179,10 @@ public class BinlogConfig implements Writable { sb.append(",\n\"").append(PropertyAnalyzer.PROPERTIES_BINLOG_MAX_HISTORY_NUMS).append("\" = \"") .append(maxHistoryNums).append("\""); } + + public static BinlogConfig fromProperties(Map properties) { + BinlogConfig binlogConfig = new BinlogConfig(); + binlogConfig.mergeFromProperties(properties); + return binlogConfig; + } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java index 90f0184287..54f1b6ce8b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java @@ -854,11 +854,43 @@ public class Database extends MetaObject implements Writable, DatabaseIf return new HashMap<>(idToTable); } - public void updateDbProperties(Map properties) { + public void replayUpdateDbProperties(Map properties) { dbProperties.updateProperties(properties); binlogConfig = dbProperties.getBinlogConfig(); } + public boolean updateDbProperties(Map properties) throws DdlException { + BinlogConfig oldBinlogConfig = getBinlogConfig(); + BinlogConfig newBinlogConfig = BinlogConfig.fromProperties(properties); + if (oldBinlogConfig.equals(newBinlogConfig)) { + return false; + } + + if (newBinlogConfig.isEnable() && !oldBinlogConfig.isEnable()) { + // check all tables binlog enable is true + for (Table table : idToTable.values()) { + if (table.getType() != TableType.OLAP) { + continue; + } + + OlapTable olapTable = (OlapTable) table; + olapTable.readLock(); + try { + if (!olapTable.getBinlogConfig().isEnable()) { + String errMsg = String.format("binlog is not enable in table[%s] in db [%s]", table.getName(), + getFullName()); + throw new DdlException(errMsg); + } + } finally { + olapTable.readUnlock(); + } + } + } + + replayUpdateDbProperties(properties); + return true; + } + public BinlogConfig getBinlogConfig() { return binlogConfig; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java index fd95fe37a1..c030940a80 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java +++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java @@ -763,7 +763,10 @@ public class InternalCatalog implements CatalogIf { db.writeLockOrDdlException(); try { - db.updateDbProperties(properties); + boolean update = db.updateDbProperties(properties); + if (!update) { + return; + } AlterDatabasePropertyInfo info = new AlterDatabasePropertyInfo(dbName, properties); Env.getCurrentEnv().getEditLog().logAlterDatabaseProperty(info); @@ -777,7 +780,7 @@ public class InternalCatalog implements CatalogIf { Database db = (Database) getDbOrMetaException(dbName); db.writeLock(); try { - db.updateDbProperties(properties); + db.replayUpdateDbProperties(properties); } finally { db.writeUnlock(); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/persist/AlterDatabasePropertyInfo.java b/fe/fe-core/src/main/java/org/apache/doris/persist/AlterDatabasePropertyInfo.java index 876b13349c..56858c86a6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/persist/AlterDatabasePropertyInfo.java +++ b/fe/fe-core/src/main/java/org/apache/doris/persist/AlterDatabasePropertyInfo.java @@ -62,4 +62,8 @@ public class AlterDatabasePropertyInfo implements Writable { public static AlterDatabasePropertyInfo read(DataInput in) throws IOException { return GsonUtils.GSON.fromJson(Text.readString(in), AlterDatabasePropertyInfo.class); } + + public String toJson() { + return GsonUtils.GSON.toJson(this); + } } diff --git a/regression-test/suites/schema_change/test_alter_database_property.groovy b/regression-test/suites/schema_change/test_alter_database_property.groovy index 2a0f568f0c..4d89d973f3 100644 --- a/regression-test/suites/schema_change/test_alter_database_property.groovy +++ b/regression-test/suites/schema_change/test_alter_database_property.groovy @@ -24,6 +24,7 @@ suite("test_alter_database_property") { result = sql "show create database test_alter_database_property" logger.info("${result}") + // Case 1: alter database, set binlog enable is true sql """ alter database test_alter_database_property set properties ("binlog.enable" = "true") """ @@ -31,5 +32,62 @@ suite("test_alter_database_property") { logger.info("${result}") assertTrue(result.toString().containsIgnoreCase('"binlog.enable" = "true"')) + // Case 2: + // create table, table binlog enable is false, so database binlog enable setting true not success + sql """ + alter database test_alter_database_property set properties ("binlog.enable" = "false") + """ + result = sql "show create database test_alter_database_property" + logger.info("${result}") + assertTrue(result.toString().containsIgnoreCase('"binlog.enable" = "false"')) + // create table t1 binlog disable + sql """ + CREATE TABLE test_alter_database_property.t1 + ( + k1 INT + ) + ENGINE = olap + DISTRIBUTED BY HASH(k1) BUCKETS 3 + PROPERTIES ( + "replication_num" = "1" + ) + """ + // check enable db binlog error + assertThrows(Exception.class, { + sql """ + alter database test_alter_database_property set properties ("binlog.enable" = "true") + """ + }) + // set table t1 binlog true + sql """ + alter table test_alter_database_property.t1 set ("binlog.enable" = "true"); + """ + // check db enable binlog true + sql """ + alter database test_alter_database_property set properties ("binlog.enable" = "true") + """ + result = sql "show create database test_alter_database_property" + logger.info("${result}") + assertTrue(result.toString().containsIgnoreCase('"binlog.enable" = "true"')) + + // Case 3: table false, db can set binlog.enable = false + sql """ + CREATE TABLE test_alter_database_property.t2 + ( + k1 INT + ) + ENGINE = olap + DISTRIBUTED BY HASH(k1) BUCKETS 3 + PROPERTIES ( + "replication_num" = "1" + ) + """ + sql """ + alter database test_alter_database_property set properties ("binlog.enable" = "false") + """ + result = sql "show create database test_alter_database_property" + logger.info("${result}") + assertTrue(result.toString().containsIgnoreCase('"binlog.enable" = "false"')) + sql "drop database if exists test_alter_database_property" }