[fix](backup) Read BackupMeta with the specified meta version (#38396)

Cherry-pick #38370
This commit is contained in:
walter
2024-07-26 13:59:48 +08:00
committed by GitHub
parent c93f3bd24e
commit df96db3f96
2 changed files with 29 additions and 24 deletions

View File

@ -66,9 +66,7 @@ import org.apache.commons.collections.CollectionUtils;
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;
@ -498,22 +496,25 @@ public class BackupHandler extends MasterDaemon implements Writable {
// Create a restore job
RestoreJob restoreJob;
if (stmt.isLocal()) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(stmt.getMeta());
DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
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);
} catch (IOException e) {
LOG.warn("create restore job failed, current meta version {}", Env.getCurrentEnvJournalVersion(), e);
throw new DdlException("create restore job failed", e);
int metaVersion = stmt.getMetaVersion();
if (metaVersion == -1) {
metaVersion = jobInfo.metaVersion;
}
BackupMeta backupMeta;
try {
backupMeta = BackupMeta.fromBytes(stmt.getMeta(), metaVersion);
} catch (IOException 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(),

View File

@ -26,6 +26,7 @@ import org.apache.doris.persist.gson.GsonUtils;
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;
@ -34,6 +35,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
@ -83,11 +85,18 @@ public class BackupMeta implements Writable {
}
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))) {
try (DataInputStream dis = new DataInputStream(stream)) {
BackupMeta backupMeta = BackupMeta.read(dis);
return backupMeta;
} finally {
@ -105,11 +114,6 @@ public class BackupMeta implements Writable {
}
}
public boolean compatibleWith(BackupMeta other) {
// TODO
return false;
}
public static BackupMeta read(DataInput in) throws IOException {
BackupMeta backupMeta = new BackupMeta();
backupMeta.readFields(in);