Fix constraint not found when modify column not null retry

This commit is contained in:
Hongqin-Li
2023-10-12 06:09:57 +00:00
committed by ob-robot
parent 64085e394b
commit a36e9faa64
2 changed files with 56 additions and 7 deletions

View File

@ -1156,6 +1156,29 @@ int ObConstraintTask::set_foreign_key_constraint_validated()
return ret;
}
int ObConstraintTask::check_column_is_nullable(const uint64_t column_id, bool &is_nullable) const
{
int ret = OB_SUCCESS;
share::schema::ObSchemaGetterGuard schema_guard;
const share::schema::ObTableSchema *table_schema = nullptr;
const share::schema::ObColumnSchemaV2 *column_schema = nullptr;
if (OB_FAIL(share::schema::ObMultiVersionSchemaService::get_instance().get_tenant_schema_guard(
tenant_id_, schema_guard))) {
LOG_WARN("get tenant schema guard failed", K(ret), K(tenant_id_));
} else if (OB_FAIL(schema_guard.get_table_schema(tenant_id_, object_id_, table_schema))) {
LOG_WARN("get table schema failed", K(ret), K(tenant_id_), K(object_id_));
} else if (OB_ISNULL(table_schema)) {
ret = OB_TABLE_NOT_EXIST;
LOG_WARN("table dropped", K(ret), K(tenant_id_), K(object_id_));
} else if (OB_ISNULL(column_schema = table_schema->get_column_schema(column_id))) {
ret = OB_ERR_COLUMN_NOT_FOUND;
LOG_WARN("column not found", K(ret), K(column_id));
} else {
is_nullable = column_schema->is_nullable();
}
return ret;
}
int ObConstraintTask::set_check_constraint_validated()
{
int ret = OB_SUCCESS;
@ -1179,8 +1202,6 @@ int ObConstraintTask::set_check_constraint_validated()
alter_table_arg.based_schema_object_infos_.reset();
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(ObDDLUtil::refresh_alter_table_arg(tenant_id_, object_id_, OB_INVALID_ID/*foreign_key_id*/, alter_table_arg))) {
LOG_WARN("failed to refresh name for alter table schema", K(ret));
} else {
alter_table_arg.index_arg_list_.reset();
alter_table_arg.foreign_key_arg_list_.reset();
@ -1211,6 +1232,7 @@ int ObConstraintTask::set_check_constraint_validated()
alter_table_arg.alter_table_schema_.set_tenant_id(tenant_id_);
if (is_table_hidden_) {
if (!is_oracle_mode) {
// no need to refresh_alter_table_arg because MODIFY_NOT_NULL_COLUMN_STATE_TASK use constraint id instead of name
// only mysql mode support modify not null column during offline ddl, support oracle later.
ObSArray<uint64_t> unused_ids;
alter_table_arg.ddl_task_type_ = share::MODIFY_NOT_NULL_COLUMN_STATE_TASK;
@ -1224,7 +1246,6 @@ int ObConstraintTask::set_check_constraint_validated()
}
}
} else {
// do nothing if is_table_hidden_ because schema is not set novalidate before.
if ((obrpc::ObAlterTableArg::ADD_CONSTRAINT == alter_table_arg.alter_constraint_type_
&& (*iter)->is_validated())
|| (obrpc::ObAlterTableArg::ALTER_CONSTRAINT_STATE == alter_table_arg.alter_constraint_type_
@ -1232,10 +1253,14 @@ int ObConstraintTask::set_check_constraint_validated()
alter_table_arg.exec_tenant_id_ = tenant_id_;
uint64_t column_id = OB_INVALID_ID;
if (is_oracle_mode) {
if (OB_FAIL(ObDDLUtil::refresh_alter_table_arg(tenant_id_, object_id_, OB_INVALID_ID/*foreign_key_id*/, alter_table_arg))) {
LOG_WARN("failed to refresh name for alter table schema", K(ret));
} else {
alter_table_arg.alter_constraint_type_ = obrpc::ObAlterTableArg::ALTER_CONSTRAINT_STATE;
(*iter)->set_is_modify_validate_flag(true);
(*iter)->set_validate_flag(CST_FK_VALIDATED);
(*iter)->set_need_validate_data(false);
}
} else {
alter_table_arg.alter_constraint_type_ = obrpc::ObAlterTableArg::DROP_CONSTRAINT;
if (OB_ISNULL((*iter)->cst_col_begin())) {
@ -1256,6 +1281,26 @@ int ObConstraintTask::set_check_constraint_validated()
column->drop_not_null_cst();
}
}
if (OB_FAIL(ret)) {
} else if (OB_UNLIKELY(OB_INVALID_ID == column_id)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid column id", K(ret), K(alter_table_arg), K(column_id));
} else if (OB_FAIL(ObDDLUtil::refresh_alter_table_arg(tenant_id_, object_id_, OB_INVALID_ID/*foreign_key_id*/, alter_table_arg))) {
if (OB_ERR_CONTRAINT_NOT_FOUND == ret) {
bool is_nullable = false;
if (OB_FAIL(check_column_is_nullable(column_id, is_nullable))) { // overwrite ret
LOG_WARN("failed to check column is nullable", K(ret));
} else if (is_nullable) {
ret = OB_ERR_CONTRAINT_NOT_FOUND;
LOG_WARN("column is nullable without constraint, maybe constraint dropped by others", K(ret));
} else {
ret = OB_NO_NEED_UPDATE;
LOG_INFO("already not null, maybe on retry", K(target_object_id_), K(column_id));
}
} else {
LOG_WARN("failed to refresh name for alter table schema", K(ret));
}
}
}
DEBUG_SYNC(CONSTRAINT_BEFORE_SET_CHECK_CONSTRAINT_VALIDATED_BEFORE_ALTER_TABLE);
if (OB_FAIL(ret)) {
@ -1263,6 +1308,9 @@ int ObConstraintTask::set_check_constraint_validated()
alter_table(alter_table_arg, res))) {
LOG_WARN("alter table failed", K(ret));
}
if (OB_NO_NEED_UPDATE == ret) {
ret = OB_SUCCESS;
}
}
}
}

View File

@ -120,6 +120,7 @@ private:
int send_check_constraint_request();
int send_fk_constraint_request();
int set_foreign_key_constraint_validated();
int check_column_is_nullable(const uint64_t column_id, bool &is_nullable) const;
int set_check_constraint_validated();
int set_constraint_validated();
int set_new_not_null_column_validate();