Skip to content

Commit

Permalink
[fix](restore) Fix clean restore with view (#40620)
Browse files Browse the repository at this point in the history
While the previous impl only considered OLAP tables, restore included
ODBC and Views, both of which were not recorded in
backupOlapTableObjects and were accidentally deleted.
  • Loading branch information
w41ter authored Sep 13, 2024
1 parent 3c8f9f7 commit 3af0943
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
28 changes: 17 additions & 11 deletions fe/fe-core/src/main/java/org/apache/doris/backup/RestoreJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -2036,22 +2036,28 @@ private Status allTabletCommitted(boolean isReplay) {
}

private Status dropAllNonRestoredTableAndPartitions(Database db) {
Set<String> restoredViews = jobInfo.newBackupObjects.views.stream()
.map(view -> view.name).collect(Collectors.toSet());

try {
for (Table table : db.getTables()) {
long tableId = table.getId();
String tableName = table.getName();
TableType tableType = table.getType();
BackupOlapTableInfo backupTableInfo = jobInfo.backupOlapTableObjects.get(tableName);
if (tableType != TableType.OLAP && tableType != TableType.ODBC && tableType != TableType.VIEW) {
continue;
}
if (tableType == TableType.OLAP && backupTableInfo != null) {
// drop the non restored partitions.
dropNonRestoredPartitions(db, (OlapTable) table, backupTableInfo);
} else if (isCleanTables) {
// otherwise drop the entire table.
LOG.info("drop non restored table {}({}). {}", tableName, tableId, this);
boolean isForceDrop = false; // move this table into recyclebin.
if (tableType == TableType.OLAP) {
BackupOlapTableInfo backupTableInfo = jobInfo.backupOlapTableObjects.get(tableName);
if (tableType == TableType.OLAP && backupTableInfo != null) {
// drop the non restored partitions.
dropNonRestoredPartitions(db, (OlapTable) table, backupTableInfo);
} else if (isCleanTables) {
// otherwise drop the entire table.
LOG.info("drop non restored table {}, table id: {}. {}", tableName, tableId, this);
boolean isForceDrop = false; // move this table into recyclebin.
env.getInternalCatalog().dropTableWithoutCheck(db, table, isForceDrop);
}
} else if (tableType == TableType.VIEW && isCleanTables && !restoredViews.contains(tableName)) {
LOG.info("drop non restored view {}, table id: {}. {}", tableName, tableId, this);
boolean isForceDrop = false; // move this view into recyclebin.
env.getInternalCatalog().dropTableWithoutCheck(db, table, isForceDrop);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ suite("test_backup_restore_clean_restore", "backup_restore") {
)
"""


sql "INSERT INTO ${dbName}.${tableName2} VALUES ${values.join(",")}"
result = sql "SELECT * FROM ${dbName}.${tableName2}"
assertEquals(result.size(), numRows);
Expand Down Expand Up @@ -106,6 +107,25 @@ suite("test_backup_restore_clean_restore", "backup_restore") {
result = sql "SELECT * FROM ${dbName}.${tableName3}"
assertEquals(result.size(), numRows);

// view 1 must exists
String viewName1 = "${tableNamePrefix}_4"
sql "DROP VIEW IF EXISTS ${dbName}.${viewName1}"
sql """
CREATE VIEW ${dbName}.${viewName1} (k1, k2)
AS
SELECT id as k1, count as k2 FROM ${dbName}.${tableName1}
WHERE id in (1,3,5,7,9)
"""

// view 2 will be deleted
String viewName2 = "${tableNamePrefix}_5"
sql "DROP VIEW IF EXISTS ${dbName}.${viewName2}"
sql """
CREATE VIEW ${dbName}.${viewName2} (k1, k2)
AS
SELECT id as k1, count as k2 FROM ${dbName}.${tableName3}
WHERE id in (1,3,5,7,9)
"""

sql """
BACKUP SNAPSHOT ${dbName}.${snapshotName}
Expand All @@ -117,13 +137,14 @@ suite("test_backup_restore_clean_restore", "backup_restore") {
def snapshot = syncer.getSnapshotTimestamp(repoName, snapshotName)
assertTrue(snapshot != null)

// restore table1, partition 3 of table2
// restore table1, partition 3 of table2, view1
sql """
RESTORE SNAPSHOT ${dbName}.${snapshotName}
FROM `${repoName}`
ON (
`${tableName1}`,
`${tableName2}` PARTITION (`p3`)
`${tableName2}` PARTITION (`p3`),
`${viewName1}`
)
PROPERTIES
(
Expand All @@ -144,12 +165,23 @@ suite("test_backup_restore_clean_restore", "backup_restore") {
result = sql "SELECT * FROM ${dbName}.${tableName2}"
assertEquals(result.size(), numRows-10)

// view1 are exists
result = sql """ SHOW VIEW FROM ${tableName1} FROM ${dbName} """
assertEquals(result.size(), 1)

// view2 are dropped
result = sql """
SHOW TABLE STATUS FROM ${dbName} LIKE "${viewName2}"
"""
assertEquals(result.size(), 0)

// table3 are dropped
result = sql """
SHOW TABLE STATUS FROM ${dbName} LIKE "${tableName3}"
"""
assertEquals(result.size(), 0)

sql "DROP VIEW ${dbName}.${viewName1}"
sql "DROP TABLE ${dbName}.${tableName1} FORCE"
sql "DROP TABLE ${dbName}.${tableName2} FORCE"
sql "DROP DATABASE ${dbName} FORCE"
Expand Down

0 comments on commit 3af0943

Please sign in to comment.