sync wait created tenant in restore tenant sql thread

This commit is contained in:
hamstersox
2023-02-24 12:06:52 +00:00
committed by ob-robot
parent e615feb42d
commit 29d421b18d
4 changed files with 127 additions and 0 deletions

View File

@ -721,3 +721,39 @@ int ObRestoreUtil::get_restore_ls_palf_base_info(
}
return ret;
}
int ObRestoreUtil::check_physical_restore_finish(
common::ObISQLClient &proxy, uint64_t tenant_id, bool &is_finish, bool &is_failed) {
int ret = OB_SUCCESS;
is_failed = false;
is_finish = false;
ObSqlString sql;
char status_str[OB_DEFAULT_STATUS_LENTH] = "";
int64_t real_length = 0;
HEAP_VAR(ObMySQLProxy::ReadResult, res) {
common::sqlclient::ObMySQLResult *result = nullptr;
int64_t cnt = 0;
if (OB_FAIL(sql.assign_fmt("select status from %s where tenant_id=%lu and restore_tenant_id=%lu",
OB_ALL_RESTORE_JOB_HISTORY_TNAME, OB_SYS_TENANT_ID, tenant_id))) {
LOG_WARN("failed to assign fmt", K(ret));
} else if (OB_FAIL(proxy.read(res, OB_SYS_TENANT_ID, sql.ptr()))) {
LOG_WARN("failed to exec sql", K(ret), K(sql));
} else if (OB_ISNULL(result = res.get_result())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("result is null", K(ret));
} else if (OB_FAIL(result->next())) {
if (OB_ITER_END == ret) {
ret = OB_SUCCESS;
} else {
LOG_WARN("failed to get next", K(ret), K(tenant_id));
}
} else {
EXTRACT_STRBUF_FIELD_MYSQL(*result, OB_STR_STATUS, status_str, OB_DEFAULT_STATUS_LENTH, real_length);
if (OB_SUCC(ret)) {
is_finish = true;
is_failed = 0 == STRCMP(status_str, "FAIL");
}
}
}
return ret;
}

View File

@ -69,6 +69,8 @@ public:
static int get_restore_ls_palf_base_info(const share::ObPhysicalRestoreJob &job_info,
const share::ObLSID &ls_id,
palf::PalfBaseInfo &palf_base_info);
static int check_physical_restore_finish(common::ObISQLClient &proxy, uint64_t tenant_id, bool &is_finish, bool &is_failed);
private:
static int fill_backup_info_(
const obrpc::ObPhysicalRestoreTenantArg &arg,

View File

@ -82,6 +82,12 @@ int ObPhysicalRestoreTenantExecutor::execute(
LOG_WARN("failed to remove user variable", KR(tmp_ret));
}
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(sync_wait_tenant_created_(ctx, restore_tenant_arg.tenant_name_))) {
LOG_WARN("failed to sync wait tenant created", K(ret));
}
} else {
// TODO: fix restore preview later.
ret = OB_NOT_SUPPORTED;
@ -94,6 +100,88 @@ int ObPhysicalRestoreTenantExecutor::execute(
return ret;
}
int ObPhysicalRestoreTenantExecutor::sync_wait_tenant_created_(ObExecContext &ctx, const ObString &tenant_name)
{
int ret = OB_SUCCESS;
const int64_t timeout = 3 * 60 * 1000 * 1000; // 3min
const int64_t abs_timeout = ObTimeUtility::current_time() + timeout;
const int64_t cur_time_us = ObTimeUtility::current_time();
ObTimeoutCtx timeout_ctx;
common::ObMySQLProxy *sql_proxy = nullptr;
ctx.get_physical_plan_ctx()->set_timeout_timestamp(abs_timeout);
LOG_INFO("sync wait tenant created start", K(timeout), K(abs_timeout), K(tenant_name));
if (OB_ISNULL(sql_proxy = ctx.get_sql_proxy())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("sql proxy must not be null", K(ret));
} else if (OB_FALSE_IT(THIS_WORKER.set_timeout_ts(abs_timeout))) {
} else if (OB_FAIL(timeout_ctx.set_trx_timeout_us(timeout))) {
LOG_WARN("failed to set trx timeout us", K(ret), K(timeout));
} else if (OB_FAIL(timeout_ctx.set_abs_timeout(abs_timeout))) {
LOG_WARN("failed to set abs timeout", K(ret));
} else {
ObSchemaGetterGuard schema_guard;
ObSchemaGetterGuard meta_tenant_scheam_guard;
uint64_t user_tenant_id = 0;
uint64_t meta_tenant_id = 0;
while (OB_SUCC(ret)) {
schema_guard.reset();
meta_tenant_scheam_guard.reset();
const ObTenantSchema *tenant_info = nullptr;
if (ObTimeUtility::current_time() > abs_timeout) {
ret = OB_TIMEOUT;
LOG_WARN("wait restore tenant timeout", K(ret), K(tenant_name), K(abs_timeout), "cur_time_us", ObTimeUtility::current_time());
} else if (OB_FAIL(ctx.check_status())) {
LOG_WARN("check exec ctx failed", K(ret));
} else if (OB_FAIL(GCTX.schema_service_->get_tenant_schema_guard(OB_SYS_TENANT_ID, schema_guard))) {
LOG_WARN("failed to get_tenant_schema_guard", KR(ret));
} else if (OB_FAIL(schema_guard.get_tenant_id(tenant_name, user_tenant_id))) {
LOG_WARN("failed to get tenant id from schema guard", KR(ret), K(tenant_name));
} else if (!is_user_tenant(user_tenant_id)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid user tenant id", K(ret), K(user_tenant_id));
} else if (OB_FALSE_IT(meta_tenant_id = gen_meta_tenant_id(user_tenant_id))) {
} else if (OB_FAIL(GCTX.schema_service_->get_tenant_schema_guard(meta_tenant_id, meta_tenant_scheam_guard))) {
LOG_WARN("failed to get tenant schema guard", K(ret), K(meta_tenant_id));
} else if (OB_FAIL(meta_tenant_scheam_guard.get_tenant_info(meta_tenant_id, tenant_info))) {
LOG_WARN("failed to get meta tenant schema guard", K(ret), K(meta_tenant_id));
} else if (OB_ISNULL(tenant_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("tenant schema must not be null", K(ret), K(meta_tenant_id));
} else if (!tenant_info->is_normal()) {
ret = OB_EAGAIN;
LOG_DEBUG("tenant status not normal, wait later", K(ret), K(meta_tenant_id));
} else {
break;
}
if (OB_ERR_INVALID_TENANT_NAME == ret || OB_EAGAIN == ret) {
bool is_failed = false;
bool is_finish = false;
if (OB_FAIL(ObRestoreUtil::check_physical_restore_finish(*sql_proxy, user_tenant_id, is_finish, is_failed))) {
LOG_WARN("failed to check physical restore finish", K(ret), K(user_tenant_id));
} else if (!is_finish) {
sleep(1);
LOG_DEBUG("restore not finish, wait later", K(ret), K(user_tenant_id));
} else if (is_failed) {
ret = OB_TASK_STATE_FAILED;
LOG_WARN("created tenant failed when restore.", K(ret), K(tenant_name));
} else {
break;
}
}
}
if (OB_SUCC(ret)) {
int cost_ts = (ObTimeUtility::current_time() - cur_time_us) / 1000000;
LOG_INFO("sync wait tenant created finished", K(cost_ts), K(tenant_name));
} else {
int cost_ts = (ObTimeUtility::current_time() - cur_time_us) / 1000000;
LOG_WARN("sync wait tenant created failed", K(ret), K(cost_ts), K(tenant_name));
}
}
return ret;
}
int ObPhysicalRestoreTenantExecutor::physical_restore_preview(
ObExecContext &ctx, ObPhysicalRestoreTenantStmt &stmt)
{

View File

@ -27,6 +27,7 @@ public:
virtual ~ObPhysicalRestoreTenantExecutor();
int execute(ObExecContext &ctx, ObPhysicalRestoreTenantStmt &stmt);
private:
int sync_wait_tenant_created_(ObExecContext &ctx, const ObString &tenant_name);
int physical_restore_preview(ObExecContext &ctx, ObPhysicalRestoreTenantStmt &stmt);
};
} //end namespace sql