Skip to content

Commit

Permalink
Refactor transaction log managers to use singleton pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
dsplayerX committed May 27, 2024
1 parent 45e30eb commit 7b66b83
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import io.ballerina.runtime.internal.diagnostics.RuntimeDiagnosticLog;
import io.ballerina.runtime.internal.errors.ErrorCodes;
import io.ballerina.runtime.internal.util.RuntimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -35,6 +33,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -46,9 +45,8 @@
*
* @since 2201.9.0
*/
public class FileRecoveryLog implements RecoveryLog {
public final class FileRecoveryLog implements RecoveryLog {

private static final Logger log = LoggerFactory.getLogger(FileRecoveryLog.class);
private static final String LOG_FILE_NUMBER = "(\\d+)";
private static final String LOG_FILE_EXTENSION = ".log";
private final String baseFileName;
Expand All @@ -61,6 +59,7 @@ public class FileRecoveryLog implements RecoveryLog {
private Map<String, TransactionLogRecord> existingLogs;
private static final PrintStream stderr = System.err;
private final RuntimeDiagnosticLog diagnosticLog = new RuntimeDiagnosticLog();
private static FileRecoveryLog instance;

/**
* Initializes a new FileRecoveryLog instance with the given base file name.
Expand All @@ -70,16 +69,25 @@ public class FileRecoveryLog implements RecoveryLog {
* @param recoveryLogDir The directory to store the recovery log files in.
* @param deleteOldLogs Whether to delete old log files when creating a new one.
*/
public FileRecoveryLog(String baseFileName, int checkpointInterval, Path recoveryLogDir, boolean deleteOldLogs) {
private FileRecoveryLog(String baseFileName, int checkpointInterval, Path recoveryLogDir, boolean deleteOldLogs) {
this.baseFileName = baseFileName;
this.recoveryLogDir = recoveryLogDir;
this.deleteOldLogs = deleteOldLogs;
this.checkpointInterval = checkpointInterval;
this.existingLogs = new HashMap<>();
this.logFile = createNextVersion();
this.numOfPutsSinceLastCheckpoint = 0;
}

public static FileRecoveryLog getInstance(String baseFileName, int checkpointInterval, Path recoveryLogDir,
boolean deleteOldLogs) {
if (instance != null) {
throw new IllegalStateException("instance already exists");
}
instance = new FileRecoveryLog(baseFileName, checkpointInterval, recoveryLogDir, deleteOldLogs);
instance.logFile = instance.createNextVersion();
return instance;
}

/**
* Creates the next version of the recovery log file, cleaning up the previous one.
*
Expand Down Expand Up @@ -194,9 +202,6 @@ public void putAll(Map<String, TransactionLogRecord> trxRecords) {
public Map<String, TransactionLogRecord> getPendingLogs() {
Map<String, TransactionLogRecord> pendingTransactions = new HashMap<>();
Map<String, TransactionLogRecord> transactionLogs = readLogsFromFile(logFile);
if (transactionLogs == null) {
return null;
}
for (Map.Entry<String, TransactionLogRecord> entry : transactionLogs.entrySet()) {
String trxId = entry.getKey();
TransactionLogRecord trxRecord = entry.getValue();
Expand Down Expand Up @@ -234,7 +239,7 @@ private void writeToFile(String str, boolean force) {
*/
private Map<String, TransactionLogRecord> readLogsFromFile(File file) {
if (!file.exists() || file.length() == 0) {
return null;
return Collections.emptyMap();
}
if (fileLockAndChannel != null) {
closeEverything();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@
*
* @since 2201.9.0
*/
public class InMemoryRecoveryLog implements RecoveryLog {
public final class InMemoryRecoveryLog implements RecoveryLog {

private final Map<String, TransactionLogRecord> transactionLogs;
private int numOfPutsSinceLastCheckpoint;
private final Map<String, TransactionLogRecord> transactionLogs = new ConcurrentHashMap<>();
private int numOfPutsSinceLastCheckpoint = 0;
private static InMemoryRecoveryLog instance;

public InMemoryRecoveryLog() {
this.transactionLogs = new ConcurrentHashMap<>();
this.numOfPutsSinceLastCheckpoint = 0;
private InMemoryRecoveryLog() {
}

public static InMemoryRecoveryLog getInstance() {
if (instance == null) {
instance = new InMemoryRecoveryLog();
}
return instance;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @since 2201.9.0
*/
public class LogManager {

private final FileRecoveryLog fileRecoveryLog;
private final InMemoryRecoveryLog inMemoryRecoveryLog;

Expand All @@ -40,8 +41,9 @@ public class LogManager {
* @param deleteOldLogs the delete old logs flag
*/
public LogManager(String baseFileName, int checkpointInterval, Path recoveryLogDir, boolean deleteOldLogs) {
this.fileRecoveryLog = new FileRecoveryLog(baseFileName, checkpointInterval, recoveryLogDir, deleteOldLogs);
this.inMemoryRecoveryLog = new InMemoryRecoveryLog();
this.fileRecoveryLog =
FileRecoveryLog.getInstance(baseFileName, checkpointInterval, recoveryLogDir, deleteOldLogs);
this.inMemoryRecoveryLog = InMemoryRecoveryLog.getInstance();
init();
}

Expand Down

0 comments on commit 7b66b83

Please sign in to comment.