Skip to content

Commit

Permalink
pass in the claimed chunks when backup starts to prevent pausing the …
Browse files Browse the repository at this point in the history
…queue
  • Loading branch information
Lyfts committed Aug 7, 2024
1 parent c9356d5 commit 27480ba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
6 changes: 0 additions & 6 deletions src/main/java/serverutils/data/ClaimedChunks.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public static boolean isActive() {
private final Map<ChunkDimPos, ClaimedChunk> map = new HashMap<>();
public long nextChunkloaderUpdate;
private boolean isDirty = true;
private boolean pauseQueue = false;
private static boolean forceSave = false;

public ClaimedChunks(Universe u) {
Expand All @@ -71,7 +70,6 @@ public void clear() {
}

public void processQueue() {
if (pauseQueue) return;
if (!pendingChunks.isEmpty()) {
for (ClaimedChunk chunk : pendingChunks) {
ClaimedChunk prevChunk = map.put(chunk.getPos(), chunk);
Expand Down Expand Up @@ -410,10 +408,6 @@ public boolean unloadChunk(@Nullable ForgePlayer player, ChunkDimPos pos) {
return true;
}

public void setPauseQueue(boolean pause) {
pauseQueue = pause;
}

public void forceSave() {
forceSave = true;
}
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/serverutils/task/backup/BackupTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand All @@ -24,6 +26,7 @@
import serverutils.ServerUtilitiesNotifications;
import serverutils.data.ClaimedChunks;
import serverutils.lib.data.Universe;
import serverutils.lib.math.ChunkDimPos;
import serverutils.lib.math.Ticks;
import serverutils.lib.util.FileUtils;
import serverutils.lib.util.ServerUtils;
Expand Down Expand Up @@ -99,16 +102,17 @@ public void execute(Universe universe) {

File worldDir = DimensionManager.getCurrentSaveRootDirectory();

Set<ChunkDimPos> backupChunks = new HashSet<>();
if (backups.only_backup_claimed_chunks && ClaimedChunks.isActive()) {
ClaimedChunks.instance.setPauseQueue(true);
backupChunks.addAll(ClaimedChunks.instance.getAllClaimedPositions());
BACKUP_TEMP_FOLDER.mkdirs();
}

if (backups.use_separate_thread) {
thread = new ThreadBackup(worldDir, customName);
thread = new ThreadBackup(worldDir, customName, backupChunks);
thread.start();
} else {
ThreadBackup.doBackup(worldDir, customName);
ThreadBackup.doBackup(worldDir, customName, backupChunks);
}
universe.scheduleTask(new BackupTask(true));
}
Expand Down Expand Up @@ -170,11 +174,6 @@ private void postBackup(Universe universe) {
clearOldBackups();
FileUtils.delete(BACKUP_TEMP_FOLDER);

if (backups.only_backup_claimed_chunks && ClaimedChunks.isActive()) {
ClaimedChunks.instance.setPauseQueue(false);
ClaimedChunks.instance.processQueue();
}

thread = null;
try {
MinecraftServer server = ServerUtils.getServer();
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/serverutils/task/backup/ThreadBackup.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;

import net.minecraft.nbt.CompressedStreamTools;
Expand All @@ -38,7 +39,6 @@
import it.unimi.dsi.fastutil.longs.LongSet;
import serverutils.ServerUtilities;
import serverutils.ServerUtilitiesNotifications;
import serverutils.data.ClaimedChunks;
import serverutils.lib.math.ChunkDimPos;
import serverutils.lib.math.Ticks;
import serverutils.lib.util.FileUtils;
Expand All @@ -51,21 +51,23 @@ public class ThreadBackup extends Thread {
private static long logMillis;
private final File src0;
private final String customName;
private final Set<ChunkDimPos> chunksToBackup;
public boolean isDone = false;

public ThreadBackup(File w, String s) {
src0 = w;
customName = s;
public ThreadBackup(File sourceFile, String backupName, Set<ChunkDimPos> backupChunks) {
src0 = sourceFile;
customName = backupName;
chunksToBackup = backupChunks;
setPriority(7);
}

public void run() {
isDone = false;
doBackup(src0, customName);
doBackup(src0, customName, chunksToBackup);
isDone = true;
}

public static void doBackup(File src, String customName) {
public static void doBackup(File src, String customName, Set<ChunkDimPos> chunks) {
String outName = (customName.isEmpty() ? DATE_FORMAT.format(Calendar.getInstance().getTime()) : customName)
+ ".zip";
File dstFile = null;
Expand All @@ -82,8 +84,8 @@ public static void doBackup(File src, String customName) {
zaos.setLevel(backups.compression_level);
}

if (ClaimedChunks.isActive() && backups.only_backup_claimed_chunks) {
backupRegions(src, files, zaos);
if (!chunks.isEmpty() && backups.only_backup_claimed_chunks) {
backupRegions(src, files, chunks, zaos);
} else {
compressFiles(src, files, zaos);
}
Expand Down Expand Up @@ -149,9 +151,9 @@ private static void compressFile(String entryName, File file, ZipArchiveOutputSt

}

private static void backupRegions(File sourceFolder, List<File> files, ZipArchiveOutputStream out)
throws IOException {
Long2ObjectMap<LongSet> claimedChunks = mapClaimsToRegion();
private static void backupRegions(File sourceFolder, List<File> files, Set<ChunkDimPos> chunksToBackup,
ZipArchiveOutputStream out) throws IOException {
Long2ObjectMap<LongSet> claimedChunks = mapClaimsToRegion(chunksToBackup);
Long2ObjectMap<File> regionFiles = mapRegionFilesToLong(claimedChunks);
files.removeIf(f -> f.getName().endsWith(".mca"));

Expand Down Expand Up @@ -223,9 +225,9 @@ private static Long2ObjectMap<File> mapRegionFilesToLong(Long2ObjectMap<LongSet>
return regionFiles;
}

private static Long2ObjectMap<LongSet> mapClaimsToRegion() {
private static Long2ObjectMap<LongSet> mapClaimsToRegion(Set<ChunkDimPos> chunksToBackup) {
Long2ObjectMap<LongSet> regionClaims = new Long2ObjectOpenHashMap<>();
ClaimedChunks.instance.getAllClaimedPositions().forEach(pos -> {
chunksToBackup.forEach(pos -> {
long region = pos.toRegionLong();
regionClaims.computeIfAbsent(region, k -> new LongOpenHashSet()).add(pos.toLong());
});
Expand Down

0 comments on commit 27480ba

Please sign in to comment.