xy720 commented on code in PR #47300: URL: https://github.com/apache/doris/pull/47300#discussion_r2005181006
########## fe/fe-core/src/main/java/org/apache/doris/cloud/backup/CloudRestoreJob.java: ########## @@ -0,0 +1,649 @@ +// 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.cloud.backup; + +import org.apache.doris.analysis.RestoreStmt; +import org.apache.doris.backup.BackupJobInfo; +import org.apache.doris.backup.RestoreFileMapping.IdChain; +import org.apache.doris.backup.RestoreJob; +import org.apache.doris.backup.SnapshotInfo; +import org.apache.doris.backup.Status; +import org.apache.doris.catalog.Database; +import org.apache.doris.catalog.Env; +import org.apache.doris.catalog.EnvFactory; +import org.apache.doris.catalog.FsBroker; +import org.apache.doris.catalog.Index; +import org.apache.doris.catalog.MaterializedIndex; +import org.apache.doris.catalog.MaterializedIndexMeta; +import org.apache.doris.catalog.OlapTable; +import org.apache.doris.catalog.Partition; +import org.apache.doris.catalog.Replica; +import org.apache.doris.catalog.ReplicaAllocation; +import org.apache.doris.catalog.Table; +import org.apache.doris.catalog.TableIf; +import org.apache.doris.catalog.Tablet; +import org.apache.doris.cloud.catalog.CloudEnv; +import org.apache.doris.cloud.catalog.CloudPartition; +import org.apache.doris.cloud.catalog.CloudReplica; +import org.apache.doris.cloud.catalog.CloudTablet; +import org.apache.doris.cloud.common.util.CopyUtil; +import org.apache.doris.cloud.datasource.CloudInternalCatalog; +import org.apache.doris.cloud.proto.Cloud; +import org.apache.doris.cloud.qe.ComputeGroupException; +import org.apache.doris.cloud.system.CloudSystemInfoService; +import org.apache.doris.common.Config; +import org.apache.doris.common.DdlException; +import org.apache.doris.common.Pair; +import org.apache.doris.common.UserException; +import org.apache.doris.datasource.property.S3ClientBEProperties; +import org.apache.doris.qe.AutoCloseConnectContext; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.task.AgentBatchTask; +import org.apache.doris.task.AgentTask; +import org.apache.doris.task.AgentTaskExecutor; +import org.apache.doris.task.AgentTaskQueue; +import org.apache.doris.task.DownloadTask; + +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.gson.annotations.SerializedName; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class CloudRestoreJob extends RestoreJob { + + private static final Logger LOG = LogManager.getLogger(CloudRestoreJob.class); + + private static final String PROP_STORAGE_VAULT_NAME = RestoreStmt.PROP_STORAGE_VAULT_NAME; + + @SerializedName("storageVaultName") + private String storageVaultName = null; + + @SerializedName("cloudClusterName") + private String cloudClusterName = null; + + private String storageVaultId = null; + + private String cloudClusterId = null; + + private Map<OlapTable, Cloud.CreateTabletsRequest.Builder> tabletsPerTable = new HashMap<>(); + + public enum MetaSeriviceOperation { + PREPARE, + COMMIT, + DROP + } + + public CloudRestoreJob() { + super(); + } + + public CloudRestoreJob(JobType jobType) { + super(jobType); + } + + public CloudRestoreJob(String label, String backupTs, long dbId, String dbName, BackupJobInfo jobInfo, + boolean allowLoad, ReplicaAllocation replicaAlloc, long timeoutMs, int metaVersion, boolean reserveReplica, + boolean reserveDynamicPartitionEnable, boolean isBeingSynced, boolean isCleanTables, + boolean isCleanPartitions, boolean isAtomicRestore, Env env, long repoId, + String storageVaultName) { + super(label, backupTs, dbId, dbName, jobInfo, allowLoad, replicaAlloc, timeoutMs, metaVersion, reserveReplica, + false, reserveDynamicPartitionEnable, isBeingSynced, isCleanTables, isCleanPartitions, + isAtomicRestore, env, repoId); + this.storageVaultName = storageVaultName; + properties.put(PROP_STORAGE_VAULT_NAME, String.valueOf(storageVaultName)); + ConnectContext context = ConnectContext.get(); + if (context != null) { + String clusterName = ""; + try { + clusterName = context.getCloudCluster(); + } catch (ComputeGroupException e) { + LOG.warn("failed to get compute group name", e); + } + if (!Strings.isNullOrEmpty(clusterName)) { + this.cloudClusterName = clusterName; + this.cloudClusterId = ((CloudSystemInfoService) Env.getCurrentSystemInfo()).getCloudClusterIdByName( + cloudClusterName); + } + } + } + + private AutoCloseConnectContext buildConnectContext() throws UserException { + if (Strings.isNullOrEmpty(cloudClusterName)) { + throw new UserException("cloud cluster name is not set."); + } + if (ConnectContext.get() == null) { + ConnectContext ctx = new ConnectContext(); + ctx.setCloudCluster(cloudClusterName); + return new AutoCloseConnectContext(ctx); + } else { + ConnectContext.get().setCloudCluster(cloudClusterName); + return null; + } + } + + @Override + public synchronized void run() { + if (state == RestoreJobState.FINISHED || state == RestoreJobState.CANCELLED) { + return; + } + try (AutoCloseConnectContext r = buildConnectContext()) { + super.run(); + } catch (UserException e) { + LOG.error("failed to run cloud restore job", e); + } + } + + @Override + public void checkIfNeedCancel() { + super.checkIfNeedCancel(); + if ((cloudClusterId = ((CloudSystemInfoService) Env.getCurrentSystemInfo()).getCloudClusterIdByName( + cloudClusterName)) == null) { + status = new Status(Status.ErrCode.NOT_FOUND, "cluster " + cloudClusterName + + " has been removed"); + } + } + + @Override + public void checkStorageVault(OlapTable localTable) { + Preconditions.checkNotNull(storageVaultName); + if (((CloudEnv) Env.getCurrentEnv()).getEnableStorageVault()) { + if (localTable.getStorageVaultId().isEmpty()) { + status = new Status(Status.ErrCode.COMMON_ERROR, "local table " + localTable.getName() + + " has no storage vault."); + return; + } + String localStorageVaultName = localTable.getStorageVaultName(); + if (!localStorageVaultName.equals(storageVaultName)) { + // currently we only support unique storage vault name in one restore job + status = new Status(Status.ErrCode.COMMON_ERROR, + "local table " + localTable.getName() + " storage vault is " + localStorageVaultName + + ", but restore job storage vault is " + storageVaultName); + } + } + } + + @Override + public void doCreateReplicas() { + try { + handleMetaObject(MetaSeriviceOperation.PREPARE); + // send create tablets requests + boolean needSetStorageVault = ((CloudEnv) Env.getCurrentEnv()).getEnableStorageVault(); + for (Map.Entry<OlapTable, Cloud.CreateTabletsRequest.Builder> entry : tabletsPerTable.entrySet()) { + OlapTable table = entry.getKey(); + Cloud.CreateTabletsRequest.Builder requestBuilder = entry.getValue(); + Cloud.CreateTabletsResponse resp = sendCreateTabletsRequests(requestBuilder, table, + needSetStorageVault); + if (resp.hasStorageVaultId()) { + storageVaultId = resp.getStorageVaultId(); + needSetStorageVault = false; + } + } + // set storage vault for new restoring table + for (Table table : restoredTbls) { + if (table.getType() == TableIf.TableType.OLAP) { + OlapTable olapTable = (OlapTable) table; + if (olapTable.getStorageVaultId().isEmpty() && storageVaultId != null) { + olapTable.setStorageVaultId(storageVaultId); + } + } + } + } catch (Exception e) { + status = new Status(Status.ErrCode.COMMON_ERROR, e.getMessage()); Review Comment: created tablets will be recycle by ms, because we inserted a recycle partition key into fdb here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org