Skip to content

Commit

Permalink
feature: configurable CoW incremental backup
Browse files Browse the repository at this point in the history
  • Loading branch information
keuin committed Jan 20, 2024
1 parent 61aa66b commit 7c50d78
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 7 deletions.
21 changes: 20 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'fabric-loom' version '1.0-SNAPSHOT'
id 'maven-publish'
id 'com.github.johnrengelman.shadow' version '7.1.2'
}

sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -20,6 +21,20 @@ repositories {
// for more information about repositories.
}

shadowJar {
configurations = [project.configurations.shadow]

// mitigate log4j security problem
exclude 'org/apache/logging/log4j/core/lookup/JndiLookup.class'
exclude 'META-INF'
}

remapJar {
dependsOn(shadowJar)
mustRunAfter(shadowJar)
inputFile = tasks.shadowJar.archiveFile
}

dependencies {
implementation 'junit:junit:4.13.2'

Expand All @@ -31,6 +46,10 @@ dependencies {
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
modImplementation 'com.fasterxml.jackson.core:jackson-databind:2.13.5'
shadow 'com.fasterxml.jackson.core:jackson-databind:2.13.5'

// Uncomment the following line to enable the deprecated Fabric API modules.
// These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time.

Expand All @@ -54,7 +73,7 @@ java {

jar {
from("LICENSE") {
rename { "${it}_${project.archivesBaseName}"}
rename { "${it}_${project.archivesBaseName}" }
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ minecraft_version=1.14.4
yarn_mappings=1.14.4+build.18
loader_version=0.14.12
# Mod Properties
mod_version=1.7.0
mod_version=1.8.0
maven_group=com.keuin.kbackupfabric
archives_base_name=kbackup-fabric
# Dependencies
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 4 additions & 0 deletions src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public static void registerCommands(CommandDispatcher<ServerCommandSource> dispa
.requires(Permissions.require("kb.list", DEFAULT_REQUIRED_LEVEL))
.executes(KBCommands::list)));

dispatcher.register(CommandManager.literal("kb")
.then(CommandManager.literal("cow-info")
.executes(KBCommands::cowInfo)));

// register /kb delete [name] for deleting an existing backup. OP is required.
dispatcher.register(CommandManager.literal("kb")
.then(CommandManager.literal("delete")
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.keuin.kbackupfabric.backup.BackupFilesystemUtil;
import com.keuin.kbackupfabric.backup.suggestion.BackupNameSuggestionProvider;
import com.keuin.kbackupfabric.config.KBackupConfig;
import com.keuin.kbackupfabric.event.OnPlayerConnect;
import com.keuin.kbackupfabric.metadata.BackupMetadata;
import com.keuin.kbackupfabric.metadata.MetadataHolder;
Expand Down Expand Up @@ -32,7 +33,15 @@ public final class KBPluginEvents implements ModInitializer {

@Override
public void onInitialize() {
System.out.println("Binding events and commands ...");
PrintUtil.info("Reading config...");
try {
KBackupConfig.load();
} catch (IOException e) {
PrintUtil.error("Failed to read config file, using default values: " + e + e.getMessage());
}
boolean incCow = KBackupConfig.getInstance().getIncbakCow();
PrintUtil.info("Incremental backup CoW: " + (incCow ? "enabled" : "disabled"));
PrintUtil.info("Binding events and commands...");
CommandRegistrationCallback.EVENT.register(KBCommandsRegister::registerCommands);
OnPlayerConnect.ON_PLAYER_CONNECT.register((connection, player)
-> NotificationManager.INSTANCE.notifyPlayer(DistinctNotifiable.fromServerPlayerEntity(player)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
import com.keuin.kbackupfabric.backup.incremental.ObjectCollectionIterator;
import com.keuin.kbackupfabric.backup.incremental.ObjectElement;
import com.keuin.kbackupfabric.backup.incremental.identifier.ObjectIdentifier;
import com.keuin.kbackupfabric.config.KBackupConfig;
import com.keuin.kbackupfabric.util.FilesystemUtil;
import com.keuin.kbackupfabric.util.PrintUtil;
import com.keuin.kbackupfabric.util.cow.FileCopier;
import com.keuin.kbackupfabric.util.cow.FileCowCopier;
import com.keuin.kbackupfabric.util.cow.FileEagerCopier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.logging.Logger;
Expand All @@ -27,9 +30,26 @@ public class IncrementalBackupStorageManager {
private final Logger logger = Logger.getLogger(IncrementalBackupStorageManager.class.getName());
private final Path backupStorageBase;
private final Logger LOGGER = Logger.getLogger(IncrementalBackupStorageManager.class.getName());
private FileCopier copier;

public IncrementalBackupStorageManager(Path backupStorageBase) {
this.backupStorageBase = backupStorageBase;
if (KBackupConfig.getInstance().getIncbakCow()) {
// try to use cow copier, if failed, fallback to normal copier
try {
this.copier = FileCowCopier.getInstance();
} catch (Exception | UnsatisfiedLinkError ex) {
PrintUtil.error("Failed to initialize kbackup-cow: " + ex + ex.getMessage());
this.copier = new FileEagerCopier();
}
} else {
this.copier = new FileEagerCopier();
}
if (this.copier.isCow()) {
PrintUtil.info("Copy-on-write is enabled");
} else {
PrintUtil.info("Copy-on-write is disabled");
}
}

/**
Expand Down Expand Up @@ -85,7 +105,7 @@ IncCopyResult addObjectCollection(ObjectCollection2 collection, File collectionB
if (!contains(entry.getValue())) {
// element does not exist. copy.
logger.fine("Copy new file `" + copySourceFile.getName() + "`.");
Files.copy(copySourceFile.toPath(), copyDestination.toPath());
copier.copy(copyDestination.getAbsolutePath(), copySourceFile.getAbsolutePath());
copyCount = copyCount.addWith(new IncCopyResult(1, 1, fileBytes, fileBytes));
} else {
// element exists (file reused). Just update the stat info
Expand Down Expand Up @@ -198,7 +218,7 @@ public int restoreObjectCollection(ObjectCollection2 collection, File collection
}
}

Files.copy(copySource.toPath(), copyTarget.toPath());
copier.copy(copyTarget.getAbsolutePath(), copySource.getAbsolutePath());
++copyCount;
}

Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.keuin.kbackupfabric.config;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.keuin.kbackupfabric.util.PrintUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class KBackupConfig {

private static KBackupConfig instance = getDefault();
private static final String CONFIG_FILE = "kbackup.json";

@JsonProperty("incbak_cow")
private Boolean incbakCow;

public static KBackupConfig getInstance() {
return instance;
}

private static KBackupConfig getDefault() {
return new KBackupConfig(false);
}

public static void load() throws IOException {
File file = new File(CONFIG_FILE);
ObjectMapper om = new ObjectMapper();
try {
instance = om.readValue(file, KBackupConfig.class);
} catch (FileNotFoundException ignored) {
// generate default config file
PrintUtil.info("Config file does not exist. Creating default config: " + CONFIG_FILE);
instance = getDefault();
ObjectWriter w = om.writerWithDefaultPrettyPrinter();
w.writeValue(file, instance);
}
}

public KBackupConfig() {
}

public KBackupConfig(Boolean incbakCow) {
this.incbakCow = incbakCow;
}

public Boolean getIncbakCow() {
return this.incbakCow;
}

public void setIncbakCow(Boolean incbakCow) {
this.incbakCow = incbakCow;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public IncrementalBackupFeedback backup() {
PrintUtil.info("Incremental backup finished.");
feedback = new IncrementalBackupFeedback(true, copyResult);
} catch (IOException e) {
PrintUtil.error("Incremental backup failed: " + e + e.getMessage());
feedback = new IncrementalBackupFeedback(e);
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/keuin/kbackupfabric/ui/KBCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.keuin.kbackupfabric.operation.backup.method.ConfiguredPrimitiveBackupMethod;
import com.keuin.kbackupfabric.util.DateUtil;
import com.keuin.kbackupfabric.util.PrintUtil;
import com.keuin.kbackupfabric.util.cow.FileCowCopier;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -75,6 +76,7 @@ public static int help(CommandContext<ServerCommandSource> context) {
msgInfo(context, "/kb restore <backup_name> - Delete the whole current level and restore from given backup. /kb restore is identical with /kb list.");
msgInfo(context, "/kb confirm - Confirm and start restoring.");
msgInfo(context, "/kb cancel - Cancel the restoration to be confirmed. If cancelled, /kb confirm will not run.");
msgInfo(context, "/kb cow-info - Display copy-on-write support info (Experimental)");
return SUCCESS;
}

Expand Down Expand Up @@ -135,6 +137,15 @@ public static int list(CommandContext<ServerCommandSource> context) {
return SUCCESS;
}

public static int cowInfo(CommandContext<ServerCommandSource> context) {
try {
msgInfo(context, "KBackup-cow library version: " + FileCowCopier.getVersion());
} catch (Exception | UnsatisfiedLinkError ignored) {
msgErr(context, "KBackup-cow library is not loaded");
}
return SUCCESS;
}

/**
* Print backup information.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.keuin.kbackupfabric.util.cow;

import java.io.IOException;

public interface FileCopier {
void copy(String dst, String src) throws IOException;

boolean isCow();
}
41 changes: 41 additions & 0 deletions src/main/java/com/keuin/kbackupfabric/util/cow/FileCowCopier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.keuin.kbackupfabric.util.cow;

import com.keuin.kbackupfabric.util.PrintUtil;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

public final class FileCowCopier {
private static final AtomicBoolean initialized = new AtomicBoolean(false);

static {
try {
System.loadLibrary("kbackup_cow");
} catch (SecurityException | UnsatisfiedLinkError ignored) {
}
}

public static native void init();

public static native void copy(String dst, String src) throws IOException;

public static native String getVersion();

public static FileCopier getInstance() {
if (initialized.compareAndSet(false, true)) {
FileCowCopier.init();
PrintUtil.info("kbackup-cow version: " + FileCowCopier.getVersion());
}
return new FileCopier() {
@Override
public void copy(String dst, String src) throws IOException {
FileCowCopier.copy(dst, src);
}

@Override
public boolean isCow() {
return true;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.keuin.kbackupfabric.util.cow;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FileEagerCopier implements FileCopier {
@Override
public void copy(String dst, String src) throws IOException {
Files.copy(Paths.get(src), Paths.get(dst));
}

@Override
public boolean isCow() {
return false;
}
}

0 comments on commit 7c50d78

Please sign in to comment.