[improvement](colocate table) forbit change colocate table's replica allocation (#23064)

This commit is contained in:
yujun
2023-09-02 13:54:25 +08:00
committed by GitHub
parent a542f107db
commit f1c354e0cf
6 changed files with 212 additions and 3 deletions

View File

@ -798,6 +798,9 @@ public class Alter {
// get value from properties here
// 1. replica allocation
ReplicaAllocation replicaAlloc = PropertyAnalyzer.analyzeReplicaAllocation(properties, "");
if (!replicaAlloc.isNotSet()) {
olapTable.checkChangeReplicaAllocation();
}
Env.getCurrentSystemInfo().checkReplicaAllocation(replicaAlloc);
// 2. in memory
boolean newInMemory = PropertyAnalyzer.analyzeBooleanProp(properties,

View File

@ -4487,6 +4487,9 @@ public class Env {
public void modifyTableDynamicPartition(Database db, OlapTable table, Map<String, String> properties)
throws UserException {
convertDynamicPartitionReplicaNumToReplicaAllocation(properties);
if (properties.containsKey(DynamicPartitionProperty.REPLICATION_ALLOCATION)) {
table.checkChangeReplicaAllocation();
}
Map<String, String> logProperties = new HashMap<>(properties);
TableProperty tableProperty = table.getTableProperty();
if (tableProperty == null) {
@ -4546,6 +4549,7 @@ public class Env {
}
ReplicaAllocation replicaAlloc = PropertyAnalyzer.analyzeReplicaAllocation(properties, "");
table.checkChangeReplicaAllocation();
Env.getCurrentSystemInfo().checkReplicaAllocation(replicaAlloc);
Preconditions.checkState(!replicaAlloc.isNotSet());
boolean isInMemory = partitionInfo.getIsInMemory(partition.getId());
@ -4575,8 +4579,10 @@ public class Env {
* @param properties
*/
// The caller need to hold the table write lock
public void modifyTableDefaultReplicaAllocation(Database db, OlapTable table, Map<String, String> properties) {
public void modifyTableDefaultReplicaAllocation(Database db, OlapTable table,
Map<String, String> properties) throws UserException {
Preconditions.checkArgument(table.isWriteLockHeldByCurrentThread());
table.checkChangeReplicaAllocation();
table.setReplicaAllocation(properties);
ModifyTablePropertyOperationLog info =
new ModifyTablePropertyOperationLog(db.getId(), table.getId(), table.getName(),

View File

@ -1762,6 +1762,12 @@ public class OlapTable extends Table {
}
}
public void checkChangeReplicaAllocation() throws DdlException {
if (isColocateTable()) {
throw new DdlException("Cannot change replication allocation of colocate table.");
}
}
public void setReplicationAllocation(ReplicaAllocation replicaAlloc) {
getOrCreatTableProperty().setReplicaAlloc(replicaAlloc);
}

View File

@ -117,6 +117,37 @@ public class AlterTest {
+ " PARTITION p2 values less than('2020-03-01')\n" + ")\n" + "DISTRIBUTED BY HASH(k2) BUCKETS 3\n"
+ "PROPERTIES('replication_num' = '1');");
createTable("CREATE TABLE test.colocate_tbl1\n" + "(\n" + " k1 date,\n" + " k2 int,\n" + " v1 int \n"
+ ") ENGINE=OLAP\n" + "UNIQUE KEY (k1,k2)\n"
+ "DISTRIBUTED BY HASH(k2) BUCKETS 3\n"
+ "PROPERTIES('replication_num' = '1', 'colocate_with' = 'group_1');");
createTable("CREATE TABLE test.colocate_tbl2\n" + "(\n" + " k1 date,\n" + " k2 int,\n" + " v1 int \n"
+ ") ENGINE=OLAP\n" + "UNIQUE KEY (k1,k2)\n" + "PARTITION BY RANGE(k1)\n" + "(\n"
+ " PARTITION p1 values less than('2020-02-01'),\n"
+ " PARTITION p2 values less than('2020-03-01')\n" + ")\n" + "DISTRIBUTED BY HASH(k2) BUCKETS 3\n"
+ "PROPERTIES('replication_num' = '1', 'colocate_with' = 'group_2');");
createTable("CREATE TABLE test.colocate_tbl3 (\n"
+ "`uuid` varchar(255) NULL,\n"
+ "`action_datetime` date NULL\n"
+ ")\n"
+ "DUPLICATE KEY(uuid)\n"
+ "PARTITION BY RANGE(action_datetime)()\n"
+ "DISTRIBUTED BY HASH(uuid) BUCKETS 3\n"
+ "PROPERTIES\n"
+ "(\n"
+ "\"colocate_with\" = \"group_3\",\n"
+ "\"dynamic_partition.enable\" = \"true\",\n"
+ "\"dynamic_partition.time_unit\" = \"DAY\",\n"
+ "\"dynamic_partition.end\" = \"3\",\n"
+ "\"dynamic_partition.prefix\" = \"p\",\n"
+ "\"dynamic_partition.buckets\" = \"32\",\n"
+ "\"dynamic_partition.replication_num\" = \"1\",\n"
+ "\"dynamic_partition.create_history_partition\"=\"true\",\n"
+ "\"dynamic_partition.start\" = \"-3\"\n"
+ ");\n");
createTable(
"CREATE TABLE test.tbl6\n" + "(\n" + " k1 datetime(3),\n" + " k2 datetime(3),\n"
+ " v1 int \n,"
@ -252,6 +283,36 @@ public class AlterTest {
alterTable(stmt, true);
}
@Test
public void alterTableModifyRepliaAlloc() throws Exception {
String[] tables = new String[]{"test.colocate_tbl1", "test.colocate_tbl2"};
final String errChangeReplicaAlloc = "Cannot change replication allocation of colocate table";
for (int i = 0; i < tables.length; i++) {
String sql = "alter table " + tables[i] + " set ('default.replication_allocation' = 'tag.location.default:1')";
AlterTableStmt alterTableStmt2 = (AlterTableStmt) UtFrameUtils.parseAndAnalyzeStmt(sql, connectContext);
ExceptionChecker.expectThrowsWithMsg(DdlException.class, errChangeReplicaAlloc,
() -> Env.getCurrentEnv().alterTable(alterTableStmt2));
sql = "alter table " + tables[i] + " modify partition (*) set (\n"
+ "'replication_allocation' = 'tag.location.default:1')";
AlterTableStmt alterTableStmt3 = (AlterTableStmt) UtFrameUtils.parseAndAnalyzeStmt(sql, connectContext);
ExceptionChecker.expectThrowsWithMsg(DdlException.class, errChangeReplicaAlloc,
() -> Env.getCurrentEnv().alterTable(alterTableStmt3));
}
String sql = "alter table test.colocate_tbl1 set ('replication_allocation' = 'tag.location.default:1')";
AlterTableStmt alterTableStmt1 = (AlterTableStmt) UtFrameUtils.parseAndAnalyzeStmt(sql, connectContext);
ExceptionChecker.expectThrowsWithMsg(DdlException.class, errChangeReplicaAlloc,
() -> Env.getCurrentEnv().alterTable(alterTableStmt1));
sql = "alter table test.colocate_tbl3 set (\n"
+ "'dynamic_partition.replication_allocation' = 'tag.location.default:1'\n"
+ " );";
AlterTableStmt alterTableStmt4 = (AlterTableStmt) UtFrameUtils.parseAndAnalyzeStmt(sql, connectContext);
ExceptionChecker.expectThrowsWithMsg(DdlException.class, errChangeReplicaAlloc,
() -> Env.getCurrentEnv().alterTable(alterTableStmt4));
}
@Test
public void alterTableModifyComment() throws Exception {
Database db = Env.getCurrentInternalCatalog().getDbOrMetaException("default_cluster:test");

View File

@ -404,9 +404,10 @@ public class TabletRepairAndBalanceTest {
+ " set ('replication_allocation' = 'tag.location.zone1:4')";
ExceptionChecker.expectThrows(AnalysisException.class, () -> alterTable(alterStr4));
// change col_tbl1's default replica allocation to zone2:2, which is allowed
// change col_tbl1's default replica allocation to zone2:2, which is forbidden
String alterStr5 = "alter table test.col_tbl1 set ('default.replication_allocation' = 'tag.location.zone2:2')";
ExceptionChecker.expectThrowsNoException(() -> alterTable(alterStr5));
ExceptionChecker.expectThrowsWithMsg(DdlException.class,
"Cannot change replication allocation of colocate table", () -> alterTable(alterStr5));
// Drop all tables
String dropStmt1 = "drop table test.tbl1 force";

View File

@ -0,0 +1,132 @@
// 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.
suite ("test_alter_colocate_table") {
def tbls = ["col_tbl1", "col_tbl2", "col_tbl3"]
for (def tbl : tbls) {
sql """
DROP TABLE IF EXISTS ${tbl} FORCE
"""
}
sql """
CREATE TABLE IF NOT EXISTS col_tbl1
(
k1 date,
k2 int
)
ENGINE=OLAP
UNIQUE KEY (k1,k2)
DISTRIBUTED BY HASH(k2) BUCKETS 3
PROPERTIES
(
"replication_allocation" = "tag.location.default:1",
"colocate_with" = 'x_group_1'
)
"""
sql """
CREATE TABLE IF NOT EXISTS col_tbl2
(
k1 date,
k2 int
)
ENGINE=OLAP
UNIQUE KEY (k1,k2)
PARTITION BY RANGE(k1)
(
PARTITION p1 values less than('2020-02-01'),
PARTITION p2 values less than('2020-03-01')
)
DISTRIBUTED BY HASH(k2) BUCKETS 3
PROPERTIES
(
"replication_allocation" = "tag.location.default:1",
"colocate_with" = 'x_group_2'
)
"""
sql """
CREATE TABLE col_tbl3
(
`uuid` varchar(255) NULL,
`action_datetime` date NULL
)
DUPLICATE KEY(uuid)
PARTITION BY RANGE(action_datetime)()
DISTRIBUTED BY HASH(uuid) BUCKETS 3
PROPERTIES
(
"replication_allocation" = "tag.location.default:1",
"colocate_with" = "x_group_3",
"dynamic_partition.enable" = "true",
"dynamic_partition.time_unit" = "DAY",
"dynamic_partition.end" = "3",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "3",
"dynamic_partition.replication_num" = "1",
"dynamic_partition.create_history_partition"= "true",
"dynamic_partition.replication_allocation" = "tag.location.default:1",
"dynamic_partition.start" = "-3"
);
"""
def errMsg = "Cannot change replication allocation of colocate table"
test {
sql """
ALTER TABLE col_tbl1 set (
"replication_allocation" = "tag.location.default:1"
)
"""
exception errMsg
}
test {
sql """
ALTER TABLE col_tbl3 set (
"dynamic_partition.replication_allocation" = "tag.location.default:1"
)
"""
exception errMsg
}
for (def tbl : tbls) {
test {
sql """
ALTER TABLE ${tbl} set (
"default.replication_allocation" = "tag.location.default:1"
)
"""
exception errMsg
}
test {
sql """
ALTER TABLE ${tbl} MODIFY PARTITION (*) set (
"replication_allocation" = "tag.location.default:1"
)
"""
exception errMsg
}
sql "DROP TABLE ${tbl} FORCE"
}
}