-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rollback snapshot purging functionality
- Loading branch information
1 parent
f6b14fc
commit 22466c3
Showing
8 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,9 @@ rm -rf /var/log/zkpolicy | |
%postun -p /bin/sh | ||
|
||
%changelog | ||
* Tue Oct 6 2020 Christos Arvanitis <[email protected]> 1.0.1-18 | ||
- Add rollback snapshot purge feature | ||
|
||
* Fri Jul 18 2020 Christos Arvanitis <[email protected]> 1.0.1-8 | ||
- Add MIT License | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright © 2020, CERN | ||
* This software is distributed under the terms of the MIT Licence, | ||
* copied verbatim in the file 'LICENSE'. In applying this licence, | ||
* CERN does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an Intergovernmental Organization | ||
* or submit itself to any jurisdiction. | ||
*/ | ||
package ch.cern; | ||
|
||
import java.io.File; | ||
import java.io.FilenameFilter; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.AccessLevel; | ||
|
||
|
||
/** | ||
* Purge rollback snapshots. | ||
*/ | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class ZKPurgeRollback { | ||
private static Logger logger = LogManager.getLogger(ZKPurgeRollback.class); | ||
|
||
private File rollbackDir; | ||
/** | ||
* Purge rollback snapshots retaining retainCount number of snapshots. | ||
* | ||
* @param retainCount Number of snapshots to retain | ||
* @throws IOException | ||
*/ | ||
public void purgeRollback(Integer retainCount) throws IOException { | ||
// Sort files lexicographically | ||
final File[] files = rollbackDir.listFiles(new RollbackFilenameFilter()); | ||
|
||
if (files != null) { | ||
Arrays.sort(files); | ||
Integer totalSnapshotNum = files.length; | ||
Integer cnt = 0; | ||
for (File rollbackFile : files) { | ||
if (totalSnapshotNum - cnt > retainCount) { | ||
logger.info("Purging: " + rollbackFile.getPath()); | ||
if (!rollbackFile.delete()) { | ||
logger.error("Failed to delete " + rollbackFile.getPath()); | ||
throw new IOException("Failed to delete " + rollbackFile.getPath()); | ||
} | ||
} else { | ||
break; | ||
} | ||
cnt = cnt + 1; | ||
} | ||
} | ||
} | ||
|
||
static class RollbackFilenameFilter implements FilenameFilter { | ||
@Override | ||
public boolean accept(final File dir, final String name) { | ||
return name.matches("^ROLLBACK_STATE.*\\.yml"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright © 2020, CERN | ||
* This software is distributed under the terms of the MIT Licence, | ||
* copied verbatim in the file 'LICENSE'. In applying this licence, | ||
* CERN does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an Intergovernmental Organization | ||
* or submit itself to any jurisdiction. | ||
*/ | ||
package ch.cern; | ||
|
||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Option; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import ch.cern.ZKPolicyDefs.Cli.PurgeRollback; | ||
|
||
@Command(name = "purge-rollback", aliases = { | ||
"p" }, description = PurgeRollback.DESCRIPTION, helpCommand = true, mixinStandardHelpOptions = true) | ||
public class ZKPurgeRollbackCli implements Runnable { | ||
private static Logger logger = LogManager.getLogger(ZKPurgeRollbackCli.class); | ||
|
||
@Option(names = { "-r", "--retain-count" }, required = true, description = PurgeRollback.RETAIN_CNT_DESCRIPTION) | ||
Integer retainCount; | ||
|
||
@Option(names = { "-d", | ||
"--rollback-dir" }, required = false, defaultValue = PurgeRollback.ROLLBACK_DIR_DEFAULT, description = PurgeRollback.ROLLBACK_DIR_DESCRIPTION) | ||
File rollbackDir; | ||
|
||
@Override | ||
public void run() { | ||
ZKPurgeRollback zkPurge = new ZKPurgeRollback(rollbackDir); | ||
try { | ||
zkPurge.purgeRollback(this.retainCount); | ||
} catch (IOException e) { | ||
System.out.println(e.toString()); | ||
logger.error("Exception occurred!", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters