Skip to content

Commit

Permalink
[chore](backup) Fix the db name of the restored view (apache#38075)
Browse files Browse the repository at this point in the history
Cherry-pick apache#37412

Previously, during restore, the database name in the CREATE VIEW
statement was not modified, causing the restored view to be unviewable
with the SHOW VIEW command. This PR retains the original cluster's
database name in the BackupMeta and manually replaces it with the new
cluster's database name in the CREATE VIEW statement during restore.
  • Loading branch information
w41ter authored and weixingyu12 committed Jul 28, 2024
1 parent 6be814e commit a521e26
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ private void prepareAndSendSnapshotTask() {
}
}

backupMeta = new BackupMeta(copiedTables, copiedResources);
backupMeta = new BackupMeta(db.getName(), copiedTables, copiedResources);

// send tasks
for (AgentTask task : batchTask.getAllTasks()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

public class BackupMeta implements Writable {

@SerializedName(value = "db")
private String dbName;
// tbl name -> tbl
@SerializedName(value = "tblNameMap")
private Map<String, Table> tblNameMap = Maps.newHashMap();
Expand All @@ -52,7 +54,9 @@ public class BackupMeta implements Writable {
private BackupMeta() {
}

public BackupMeta(List<Table> tables, List<Resource> resources) {
public BackupMeta(String dbName, List<Table> tables, List<Resource> resources) {
this.dbName = dbName;

for (Table table : tables) {
tblNameMap.put(table.getName(), table);
tblIdMap.put(table.getId(), table);
Expand All @@ -62,6 +66,10 @@ public BackupMeta(List<Table> tables, List<Resource> resources) {
}
}

public String getDbName() {
return dbName;
}

public Map<String, Table> getTables() {
return tblNameMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,8 @@ private void checkAndPrepareMeta() {
return;
}
} else {
remoteView.resetIdsForRestore(env);
String srcDbName = backupMeta.getDbName();
remoteView.resetIdsForRestore(env, srcDbName, db.getFullName());
restoredTbls.add(remoteView);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ public String getFullName() {
return fullQualifiedName;
}

public String getName() {
String[] strs = fullQualifiedName.split(":");
return strs.length == 2 ? strs[1] : strs[0];
}

public void setNameWithLock(String newName) {
writeLock();
try {
Expand Down
7 changes: 6 additions & 1 deletion fe/fe-core/src/main/java/org/apache/doris/catalog/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,13 @@ public View clone() {
return copied;
}

public void resetIdsForRestore(Env env) {
public void resetIdsForRestore(Env env, String srcDbName, String dbName) {
id = env.getNextId();

// the source db name is not setted in old BackupMeta, keep compatible with the old one.
if (srcDbName != null) {
inlineViewDef = inlineViewDef.replaceAll(srcDbName, dbName);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public Status getSnapshotInfoFile(String label, String backupTimestamp, List<Bac
List<Table> tbls = Lists.newArrayList();
tbls.add(tbl);
List<Resource> resources = Lists.newArrayList();
BackupMeta backupMeta = new BackupMeta(tbls, resources);
BackupMeta backupMeta = new BackupMeta(null, tbls, resources);
Map<Long, SnapshotInfo> snapshotInfos = Maps.newHashMap();
for (Partition part : tbl.getPartitions()) {
for (MaterializedIndex idx : part.getMaterializedIndices(IndexExtState.VISIBLE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ boolean await(long timeout, TimeUnit unit) {
List<Table> tbls = Lists.newArrayList();
List<Resource> resources = Lists.newArrayList();
tbls.add(expectedRestoreTbl);
backupMeta = new BackupMeta(tbls, resources);
backupMeta = new BackupMeta(null, tbls, resources);
}

@Test
Expand Down

0 comments on commit a521e26

Please sign in to comment.