diff --git a/fe/fe-core/src/main/java/org/apache/doris/backup/BackupHandler.java b/fe/fe-core/src/main/java/org/apache/doris/backup/BackupHandler.java index 8ad885d9bd6331..4184ef58db37ca 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/backup/BackupHandler.java +++ b/fe/fe-core/src/main/java/org/apache/doris/backup/BackupHandler.java @@ -67,9 +67,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.io.ByteArrayInputStream; import java.io.DataInput; -import java.io.DataInputStream; import java.io.DataOutput; import java.io.File; import java.io.IOException; @@ -512,22 +510,25 @@ private void restore(Repository repository, Database db, RestoreStmt stmt) throw // Create a restore job RestoreJob restoreJob; if (stmt.isLocal()) { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(stmt.getMeta()); - DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream); + int metaVersion = stmt.getMetaVersion(); + if (metaVersion == -1) { + metaVersion = jobInfo.metaVersion; + } + + BackupMeta backupMeta; try { - BackupMeta backupMeta = BackupMeta.read(dataInputStream); - String backupTimestamp = - TimeUtils.longToTimeString(jobInfo.getBackupTime(), - TimeUtils.getDatetimeFormatWithHyphenWithTimeZone()); - restoreJob = new RestoreJob(stmt.getLabel(), backupTimestamp, - db.getId(), db.getFullName(), jobInfo, stmt.allowLoad(), stmt.getReplicaAlloc(), - stmt.getTimeoutMs(), stmt.getMetaVersion(), stmt.reserveReplica(), - stmt.reserveDynamicPartitionEnable(), stmt.isBeingSynced(), - env, Repository.KEEP_ON_LOCAL_REPO_ID, backupMeta); + backupMeta = BackupMeta.fromBytes(stmt.getMeta(), metaVersion); } catch (IOException e) { - LOG.warn("create restore job failed, current meta version {}", Env.getCurrentEnvJournalVersion(), e); - throw new DdlException("create restore job failed", e); - } + LOG.warn("read backup meta failed, current meta version {}", Env.getCurrentEnvJournalVersion(), e); + throw new DdlException("read backup meta failed", e); + } + String backupTimestamp = TimeUtils.longToTimeString( + jobInfo.getBackupTime(), TimeUtils.getDatetimeFormatWithHyphenWithTimeZone()); + restoreJob = new RestoreJob(stmt.getLabel(), backupTimestamp, + db.getId(), db.getFullName(), jobInfo, stmt.allowLoad(), stmt.getReplicaAlloc(), + stmt.getTimeoutMs(), metaVersion, stmt.reserveReplica(), + stmt.reserveDynamicPartitionEnable(), stmt.isBeingSynced(), + env, Repository.KEEP_ON_LOCAL_REPO_ID, backupMeta); } else { restoreJob = new RestoreJob(stmt.getLabel(), stmt.getBackupTimestamp(), db.getId(), db.getFullName(), jobInfo, stmt.allowLoad(), stmt.getReplicaAlloc(), diff --git a/fe/fe-core/src/main/java/org/apache/doris/backup/BackupMeta.java b/fe/fe-core/src/main/java/org/apache/doris/backup/BackupMeta.java index c08d8c160027c8..56513e244d347b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/backup/BackupMeta.java +++ b/fe/fe-core/src/main/java/org/apache/doris/backup/BackupMeta.java @@ -30,6 +30,7 @@ import com.google.common.collect.Maps; import com.google.gson.annotations.SerializedName; +import java.io.ByteArrayInputStream; import java.io.DataInput; import java.io.DataInputStream; import java.io.DataOutput; @@ -38,12 +39,14 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.List; import java.util.Map; public class BackupMeta implements Writable, GsonPostProcessable { @SerializedName(value = "db") private String dbName; + // tbl name -> tbl @SerializedName(value = "tblNameMap") private Map tblNameMap = Maps.newHashMap(); @@ -93,12 +96,19 @@ public Table getTable(Long tblId) { } public static BackupMeta fromFile(String filePath, int metaVersion) throws IOException { - File file = new File(filePath); + return fromInputStream(new FileInputStream(filePath), metaVersion); + } + + public static BackupMeta fromBytes(byte[] bytes, int metaVersion) throws IOException { + return fromInputStream(new ByteArrayInputStream(bytes), metaVersion); + } + + protected static BackupMeta fromInputStream(InputStream stream, int metaVersion) throws IOException { MetaContext metaContext = new MetaContext(); metaContext.setMetaVersion(metaVersion); metaContext.setThreadLocalInfo(); - try (DataInputStream dis = new DataInputStream(new FileInputStream(file))) { - BackupMeta backupMeta = BackupMeta.read(dis, metaVersion); + try (DataInputStream dis = new DataInputStream(stream)) { + BackupMeta backupMeta = BackupMeta.read(dis); return backupMeta; } finally { MetaContext.remove(); @@ -115,22 +125,6 @@ public void writeToFile(File metaInfoFile) throws IOException { } } - public boolean compatibleWith(BackupMeta other) { - // TODO - return false; - } - - public static BackupMeta read(DataInput in, int metaVersion) throws IOException { - if (metaVersion < FeMetaVersion.VERSION_136) { - BackupMeta backupMeta = new BackupMeta(); - backupMeta.readFields(in); - return backupMeta; - } else { - String json = Text.readString(in); - return GsonUtils.GSON.fromJson(json, BackupMeta.class); - } - } - public static BackupMeta read(DataInput in) throws IOException { if (Env.getCurrentEnvJournalVersion() < FeMetaVersion.VERSION_136) { BackupMeta backupMeta = new BackupMeta();