Skip to content

Commit

Permalink
Move getBackupDirectories to BackupRestoreUtil and change its signatu…
Browse files Browse the repository at this point in the history
…re to cease requiring an IConfiguration but rather just take the data directory path as a String.
  • Loading branch information
mattl-netflix committed May 4, 2024
1 parent 6b3d45a commit 4ece8d3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
40 changes: 0 additions & 40 deletions priam/src/main/java/com/netflix/priam/backup/AbstractBackup.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@
import com.netflix.priam.utils.SystemUtils;
import java.io.File;
import java.io.FileFilter;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -95,40 +89,6 @@ protected String getKeyspace(File backupDir) {
*/
protected abstract void processColumnFamily(File backupDir) throws Exception;

/**
* Get all the backup directories for Cassandra.
*
* @param config to get the location of the data folder.
* @param monitoringFolder folder where cassandra backup's are configured.
* @return Set of the path(s) containing the backup folder for each columnfamily.
* @throws Exception incase of IOException.
*/
public static Set<Path> getBackupDirectories(IConfiguration config, String monitoringFolder)
throws Exception {
HashSet<Path> backupPaths = new HashSet<>();
if (config.getDataFileLocation() == null) return backupPaths;
Path dataPath = Paths.get(config.getDataFileLocation());
if (Files.exists(dataPath) && Files.isDirectory(dataPath))
try (DirectoryStream<Path> directoryStream =
Files.newDirectoryStream(dataPath, path -> Files.isDirectory(path))) {
for (Path keyspaceDirPath : directoryStream) {
try (DirectoryStream<Path> keyspaceStream =
Files.newDirectoryStream(
keyspaceDirPath, path -> Files.isDirectory(path))) {
for (Path columnfamilyDirPath : keyspaceStream) {
Path backupDirPath =
Paths.get(columnfamilyDirPath.toString(), monitoringFolder);
if (Files.exists(backupDirPath) && Files.isDirectory(backupDirPath)) {
logger.debug("Backup folder: {}", backupDirPath);
backupPaths.add(backupDirPath);
}
}
}
}
}
return backupPaths;
}

protected static File[] getSecondaryIndexDirectories(File backupDir) {
FileFilter filter = (file) -> file.getName().startsWith(".") && isAReadableDirectory(file);
return Optional.ofNullable(backupDir.listFiles(filter)).orElse(new File[] {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
package com.netflix.priam.backup;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.netflix.priam.backupv2.IMetaProxy;
import com.netflix.priam.utils.DateUtil;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -130,4 +134,36 @@ public final boolean isFiltered(String keyspace, String columnFamilyDir) {
|| includeFilter.get(keyspace).contains(columnFamilyName)));
return false;
}

/**
* Get all the backup directories for Cassandra.
*
* @param dataDirectory the location of the data folder.
* @param monitoringFolder folder where cassandra backup's are configured.
* @return Set of the path(s) containing the backup folder for each columnfamily.
* @throws Exception incase of IOException.
*/
public static ImmutableSet<Path> getBackupDirectories(
String dataDirectory, String monitoringFolder) throws Exception {
ImmutableSet.Builder<Path> backupPaths = ImmutableSet.builder();
Path dataPath = Paths.get(dataDirectory);
if (Files.exists(dataPath) && Files.isDirectory(dataPath))
try (DirectoryStream<Path> directoryStream =
Files.newDirectoryStream(dataPath, path -> Files.isDirectory(path))) {
for (Path keyspaceDirPath : directoryStream) {
try (DirectoryStream<Path> keyspaceStream =
Files.newDirectoryStream(
keyspaceDirPath, path -> Files.isDirectory(path))) {
for (Path columnfamilyDirPath : keyspaceStream) {
Path backupDirPath =
Paths.get(columnfamilyDirPath.toString(), monitoringFolder);
if (Files.exists(backupDirPath) && Files.isDirectory(backupDirPath)) {
backupPaths.add(backupDirPath);
}
}
}
}
}
return backupPaths.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public static TaskTimer getTimer(

private static void cleanOldBackups(IConfiguration configuration) throws Exception {
Set<Path> backupPaths =
AbstractBackup.getBackupDirectories(configuration, INCREMENTAL_BACKUP_FOLDER);
BackupRestoreUtil.getBackupDirectories(
configuration.getDataFileLocation(), INCREMENTAL_BACKUP_FOLDER);
for (Path backupDirPath : backupPaths) {
FileUtils.cleanDirectory(backupDirPath.toFile());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ public static TaskTimer getTimer(IBackupRestoreConfig config) throws IllegalArgu

static void cleanOldBackups(IConfiguration config) throws Exception {
// Clean up all the backup directories, if any.
Set<Path> backupPaths = AbstractBackup.getBackupDirectories(config, SNAPSHOT_FOLDER);
Set<Path> backupPaths =
BackupRestoreUtil.getBackupDirectories(
config.getDataFileLocation(), SNAPSHOT_FOLDER);
for (Path backupDirPath : backupPaths)
try (DirectoryStream<Path> directoryStream =
Files.newDirectoryStream(backupDirPath, Files::isDirectory)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.inject.Injector;
import com.netflix.priam.backup.AbstractBackup;
import com.netflix.priam.backup.BRTestModule;
import com.netflix.priam.backup.BackupRestoreUtil;
import com.netflix.priam.config.IBackupRestoreConfig;
import com.netflix.priam.config.IConfiguration;
import com.netflix.priam.connection.JMXNodeTool;
Expand Down Expand Up @@ -116,7 +117,8 @@ public void testBackupDisabled(

// snapshot V2 name should not be there.
Set<Path> backupPaths =
AbstractBackup.getBackupDirectories(configuration, AbstractBackup.SNAPSHOT_FOLDER);
BackupRestoreUtil.getBackupDirectories(
configuration.getDataFileLocation(), AbstractBackup.SNAPSHOT_FOLDER);
for (Path backupPath : backupPaths) {
Assert.assertFalse(Files.exists(Paths.get(backupPath.toString(), snapshotName)));
Assert.assertTrue(Files.exists(Paths.get(backupPath.toString(), snapshotV1Name)));
Expand Down

0 comments on commit 4ece8d3

Please sign in to comment.