Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ public interface BackupAdmin extends Closeable {
*/
void mergeBackups(String[] backupIds) throws IOException;

/**
* Show backup history command
* @param n last n backup sessions
* @return list of backup info objects
* @throws IOException exception
*/
List<BackupInfo> getHistory(int n) throws IOException;

/**
* Show backup history command with filters
* @param n last n backup sessions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.backup.util.BackupUtils;
Expand All @@ -46,13 +47,22 @@ public class BackupInfo implements Comparable<BackupInfo> {
private static final Logger LOG = LoggerFactory.getLogger(BackupInfo.class);
private static final int MAX_FAILED_MESSAGE_LENGTH = 1024;

public interface Filter {
/**
* Filter interface
* @param info backup info
* @return true if info passes filter, false otherwise
*/
boolean apply(BackupInfo info);
public interface Filter extends Predicate<BackupInfo> {
/** Returns true if the BackupInfo passes the filter, false otherwise */
@Override
boolean test(BackupInfo backupInfo);
}

public static Filter withRoot(String backupRoot) {
return info -> info.getBackupRootDir().equals(backupRoot);
}

public static Filter withType(BackupType type) {
return info -> info.getType() == type;
}

public static Filter withState(BackupState state) {
return info -> info.getState() == state;
}

/**
Expand All @@ -61,8 +71,7 @@ public interface Filter {
public enum BackupState {
RUNNING,
COMPLETE,
FAILED,
ANY
FAILED
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
*/
package org.apache.hadoop.hbase.backup.impl;

import static org.apache.hadoop.hbase.backup.BackupInfo.withRoot;
import static org.apache.hadoop.hbase.backup.BackupInfo.withState;
import static org.apache.hadoop.hbase.backup.BackupInfo.withType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -74,7 +78,7 @@ public BackupInfo getBackupInfo(String backupId) throws IOException {
BackupInfo backupInfo;
try (final BackupSystemTable table = new BackupSystemTable(conn)) {
if (backupId == null) {
ArrayList<BackupInfo> recentSessions = table.getBackupInfos(BackupState.RUNNING);
List<BackupInfo> recentSessions = table.getBackupInfos(withState(BackupState.RUNNING));
if (recentSessions.isEmpty()) {
LOG.warn("No ongoing sessions found.");
return null;
Expand Down Expand Up @@ -111,7 +115,7 @@ public int deleteBackups(String[] backupIds) throws IOException {
}

// Step 2: Make sure there is no failed session
List<BackupInfo> list = sysTable.getBackupInfos(BackupState.RUNNING);
List<BackupInfo> list = sysTable.getBackupInfos(withState(BackupState.RUNNING));
if (list.size() != 0) {
// ailed sessions found
LOG.warn("Failed backup session found. Run backup repair tool first.");
Expand Down Expand Up @@ -301,7 +305,7 @@ private List<BackupInfo> getAffectedBackupSessions(BackupInfo backupInfo, TableN
LOG.debug("GetAffectedBackupInfos for: " + backupInfo.getBackupId() + " table=" + tn);
long ts = backupInfo.getStartTs();
List<BackupInfo> list = new ArrayList<>();
List<BackupInfo> history = table.getBackupHistory(backupInfo.getBackupRootDir());
List<BackupInfo> history = table.getBackupHistory(withRoot(backupInfo.getBackupRootDir()));
// Scan from most recent to backupInfo
// break when backupInfo reached
for (BackupInfo info : history) {
Expand Down Expand Up @@ -367,49 +371,10 @@ private boolean isLastBackupSession(BackupSystemTable table, TableName tn, long
return false;
}

@Override
public List<BackupInfo> getHistory(int n) throws IOException {
try (final BackupSystemTable table = new BackupSystemTable(conn)) {
List<BackupInfo> history = table.getBackupHistory();

if (history.size() <= n) {
return history;
}

List<BackupInfo> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(history.get(i));
}
return list;
}
}

@Override
public List<BackupInfo> getHistory(int n, BackupInfo.Filter... filters) throws IOException {
if (filters.length == 0) {
return getHistory(n);
}

try (final BackupSystemTable table = new BackupSystemTable(conn)) {
List<BackupInfo> history = table.getBackupHistory();
List<BackupInfo> result = new ArrayList<>();
for (BackupInfo bi : history) {
if (result.size() == n) {
break;
}

boolean passed = true;
for (int i = 0; i < filters.length; i++) {
if (!filters[i].apply(bi)) {
passed = false;
break;
}
}
if (passed) {
result.add(bi);
}
}
return result;
return table.getBackupInfos(n, filters);
}
}

Expand Down Expand Up @@ -672,7 +637,7 @@ private void checkIfValidForMerge(String[] backupIds, BackupSystemTable table)
// Filter 1 : backupRoot
// Filter 2 : time range filter
// Filter 3 : table filter
BackupInfo.Filter destinationFilter = info -> info.getBackupRootDir().equals(backupDest);
BackupInfo.Filter destinationFilter = withRoot(backupDest);

BackupInfo.Filter timeRangeFilter = info -> {
long time = info.getStartTs();
Expand All @@ -684,10 +649,10 @@ private void checkIfValidForMerge(String[] backupIds, BackupSystemTable table)
return !Collections.disjoint(allTables, tables);
};

BackupInfo.Filter typeFilter = info -> info.getType() == BackupType.INCREMENTAL;
BackupInfo.Filter stateFilter = info -> info.getState() == BackupState.COMPLETE;
BackupInfo.Filter typeFilter = withType(BackupType.INCREMENTAL);
BackupInfo.Filter stateFilter = withState(BackupState.COMPLETE);

List<BackupInfo> allInfos = table.getBackupHistory(-1, destinationFilter, timeRangeFilter,
List<BackupInfo> allInfos = table.getBackupHistory(destinationFilter, timeRangeFilter,
tableFilter, typeFilter, stateFilter);
if (allInfos.size() != allBackups.size()) {
// Yes we have at least one hole in backup image sequence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.hadoop.hbase.backup.impl;

import static org.apache.hadoop.hbase.backup.BackupInfo.withState;
import static org.apache.hadoop.hbase.backup.BackupRestoreConstants.OPTION_BACKUP_LIST_DESC;
import static org.apache.hadoop.hbase.backup.BackupRestoreConstants.OPTION_BANDWIDTH;
import static org.apache.hadoop.hbase.backup.BackupRestoreConstants.OPTION_BANDWIDTH_DESC;
Expand Down Expand Up @@ -153,7 +154,7 @@ public void execute() throws IOException {
if (requiresNoActiveSession()) {
// Check active session
try (BackupSystemTable table = new BackupSystemTable(conn)) {
List<BackupInfo> sessions = table.getBackupInfos(BackupState.RUNNING);
List<BackupInfo> sessions = table.getBackupInfos(withState(BackupState.RUNNING));

if (sessions.size() > 0) {
System.err.println("Found backup session in a RUNNING state: ");
Expand Down Expand Up @@ -528,7 +529,7 @@ public void execute() throws IOException {
if (backupId != null) {
info = sysTable.readBackupInfo(backupId);
} else {
List<BackupInfo> infos = sysTable.getBackupInfos(BackupState.RUNNING);
List<BackupInfo> infos = sysTable.getBackupInfos(withState(BackupState.RUNNING));
if (infos != null && infos.size() > 0) {
info = infos.get(0);
backupId = info.getBackupId();
Expand Down Expand Up @@ -594,18 +595,15 @@ private void executeDeleteOlderThan(CommandLine cmdline) throws IOException {
throw new IOException(value + " is not an integer number");
}
final long fdays = days;
BackupInfo.Filter dateFilter = new BackupInfo.Filter() {
@Override
public boolean apply(BackupInfo info) {
long currentTime = EnvironmentEdgeManager.currentTime();
long maxTsToDelete = currentTime - fdays * 24 * 3600 * 1000;
return info.getCompleteTs() <= maxTsToDelete;
}
BackupInfo.Filter dateFilter = info -> {
long currentTime = EnvironmentEdgeManager.currentTime();
long maxTsToDelete = currentTime - fdays * 24 * 3600 * 1000;
return info.getCompleteTs() <= maxTsToDelete;
};
List<BackupInfo> history = null;
try (final BackupSystemTable sysTable = new BackupSystemTable(conn);
BackupAdminImpl admin = new BackupAdminImpl(conn)) {
history = sysTable.getBackupHistory(-1, dateFilter);
history = sysTable.getBackupHistory(dateFilter);
String[] backupIds = convertToBackupIds(history);
int deleted = admin.deleteBackups(backupIds);
System.out.println("Deleted " + deleted + " backups. Total older than " + days + " days: "
Expand Down Expand Up @@ -679,7 +677,7 @@ public void execute() throws IOException {
final BackupSystemTable sysTable = new BackupSystemTable(conn)) {
// Failed backup
BackupInfo backupInfo;
List<BackupInfo> list = sysTable.getBackupInfos(BackupState.RUNNING);
List<BackupInfo> list = sysTable.getBackupInfos(withState(BackupState.RUNNING));
if (list.size() == 0) {
// No failed sessions found
System.out.println("REPAIR status: no failed sessions found."
Expand Down Expand Up @@ -860,35 +858,30 @@ public void execute() throws IOException {
int n = parseHistoryLength();
final TableName tableName = getTableName();
final String setName = getTableSetName();
BackupInfo.Filter tableNameFilter = new BackupInfo.Filter() {
@Override
public boolean apply(BackupInfo info) {
if (tableName == null) {
return true;
}

List<TableName> names = info.getTableNames();
return names.contains(tableName);
BackupInfo.Filter tableNameFilter = info -> {
if (tableName == null) {
return true;
}
};
BackupInfo.Filter tableSetFilter = new BackupInfo.Filter() {
@Override
public boolean apply(BackupInfo info) {
if (setName == null) {
return true;
}

String backupId = info.getBackupId();
return backupId.startsWith(setName);
List<TableName> names = info.getTableNames();
return names.contains(tableName);
};
BackupInfo.Filter tableSetFilter = info -> {
if (setName == null) {
return true;
}

String backupId = info.getBackupId();
return backupId.startsWith(setName);
};
Path backupRootPath = getBackupRootPath();
List<BackupInfo> history;
if (backupRootPath == null) {
// Load from backup system table
super.execute();
try (final BackupSystemTable sysTable = new BackupSystemTable(conn)) {
history = sysTable.getBackupHistory(n, tableNameFilter, tableSetFilter);
history = sysTable.getBackupHistory(tableNameFilter, tableSetFilter);
history = history.subList(0, Math.min(n, history.size()));
}
} else {
// load from backup FS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.apache.hadoop.hbase.backup.impl;

import static org.apache.hadoop.hbase.backup.BackupInfo.withState;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -248,7 +250,7 @@ public BackupInfo createBackupInfo(String backupId, BackupType type, List<TableN
* @throws IOException exception
*/
private String getOngoingBackupId() throws IOException {
ArrayList<BackupInfo> sessions = systemTable.getBackupInfos(BackupState.RUNNING);
List<BackupInfo> sessions = systemTable.getBackupInfos(withState(BackupState.RUNNING));
if (sessions.size() == 0) {
return null;
}
Expand Down Expand Up @@ -370,16 +372,10 @@ public void deleteBulkLoadedRows(List<byte[]> rows) throws IOException {
}

/**
* Get all completed backup information (in desc order by time)
* @return history info of BackupCompleteData
* @throws IOException exception
* Get all backup information, ordered by descending start time. I.e. from newest to oldest.
*/
public List<BackupInfo> getBackupHistory() throws IOException {
return systemTable.getBackupHistory();
}

public ArrayList<BackupInfo> getBackupHistory(boolean completed) throws IOException {
return systemTable.getBackupHistory(completed);
public List<BackupInfo> getBackupHistory(BackupInfo.Filter... filters) throws IOException {
return systemTable.getBackupHistory(filters);
}

/**
Expand Down
Loading