Skip to content

Commit

Permalink
refactor: simplifing persistence properties
Browse files Browse the repository at this point in the history
  • Loading branch information
vinitparekh17 committed Nov 20, 2024
1 parent 77b4295 commit a9b8879
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 33 deletions.
35 changes: 9 additions & 26 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ memory.keys_limit = 200000000
memory.lfu_log_factor = 10
# Persistence Configuration
persistence.enabled = true
persistence.aof_file = "./dice-master.aof"
persistence.persistence_enabled = true
persistence.write_aof_on_cleanup = false
persistence.enable-wal = true
persistence.wal-dir = "./"
persistence.restore-wal = false
persistence.wal-engine = "aof"
Expand Down Expand Up @@ -154,13 +154,12 @@ type memory struct {
}

type persistence struct {
AOFFile string `config:"aof_file" default:"./dice-master.aof" validate:"filepath"`
PersistenceEnabled bool `config:"persistence_enabled" default:"true"`
WriteAOFOnCleanup bool `config:"write_aof_on_cleanup" default:"false"`
EnableWAL bool `config:"enable-wal" default:"true"`
WALDir string `config:"wal-dir" default:"./" validate:"dirpath"`
RestoreFromWAL bool `config:"restore-wal" default:"false"`
WALEngine string `config:"wal-engine" default:"aof" validate:"oneof=sqlite aof"`
Enabled bool `config:"enabled" default:"true"`
AOFFile string `config:"aof_file" default:"./dice-master.aof" validate:"filepath"`
WriteAOFOnCleanup bool `config:"write_aof_on_cleanup" default:"false"`
WALDir string `config:"wal-dir" default:"./" validate:"dirpath"`
RestoreFromWAL bool `config:"restore-wal" default:"false"`
WALEngine string `config:"wal-engine" default:"aof" validate:"oneof=sqlite aof"`
}

type logging struct {
Expand Down Expand Up @@ -263,28 +262,12 @@ func MergeFlags(flags *Config) {
DiceConfig.Performance.EnableWatch = flags.Performance.EnableWatch
case "log-dir":
DiceConfig.Logging.LogDir = flags.Logging.LogDir
case "enable-wal":
DiceConfig.Persistence.EnableWAL = flags.Persistence.EnableWAL
case "persistence-enable":
DiceConfig.Persistence.Enabled = flags.Persistence.Enabled
case "restore-from-wal":
DiceConfig.Persistence.RestoreFromWAL = flags.Persistence.RestoreFromWAL
case "wal-engine":
DiceConfig.Persistence.WALEngine = flags.Persistence.WALEngine
}
})
}

// This function returns the config file path based on the OS
// func getConfigPath() string {
// switch runtime.GOOS {
// case "windows":
// FileLocation = filepath.Join("C:", "ProgramData", "dice", DefaultConfigName)
// case "darwin", "linux":
// FileLocation = filepath.Join(string(filepath.Separator), "etc", "dice", DefaultConfigName)
// default:
// // Default to current directory if OS is unknown
// FileLocation = filepath.Join(".", DefaultConfigName)
// }
// return FileLocation
// }

// ResetConfig resets the DiceConfig to default configurations. This function is only used for testing purposes
4 changes: 2 additions & 2 deletions dicedb.conf
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ performance.enable_multithreading = false
performance.store_map_init_size = 1024000
performance.adhoc_req_chan_buf_size = 20
performance.enable_profiling = false
performance.enable_watch = true
performance.enable_watch = false
performance.num_shards = -1

# Memory Configuration
Expand All @@ -40,10 +40,10 @@ memory.keys_limit = 200000000
memory.lfu_log_factor = 10

# Persistence Configuration
persistence.enabled = true
persistence.aof_file = "./dice-master.aof"
persistence.persistence_enabled = true
persistence.write_aof_on_cleanup = false
persistence.enable-wal = true
persistence.wal-dir = "./"
persistence.restore-wal = false
persistence.wal-engine = "aof"
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func Execute() {
flag.StringVar(&flagsConfig.Logging.LogLevel, "log-level", "info", "log level, values: info, debug")
flag.StringVar(&config.DiceConfig.Logging.LogDir, "log-dir", "/tmp/dicedb", "log directory path")

flag.BoolVar(&flagsConfig.Persistence.EnableWAL, "enable-wal", false, "enable write-ahead logging")
flag.BoolVar(&flagsConfig.Persistence.Enabled, "persistence-enable", false, "enable write-ahead logging")
flag.BoolVar(&flagsConfig.Persistence.RestoreFromWAL, "restore-wal", false, "restore the database from the WAL files")
flag.StringVar(&flagsConfig.Persistence.WALEngine, "wal-engine", "null", "wal engine to use, values: sqlite, aof")

Expand Down
2 changes: 1 addition & 1 deletion internal/shard/shard_thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (shard *ShardThread) processRequest(op *ops.StoreOp) {
// cleanup handles cleanup logic when the shard stops.
func (shard *ShardThread) cleanup() {
close(shard.ReqChan)
if !config.DiceConfig.Persistence.WriteAOFOnCleanup {
if !config.DiceConfig.Persistence.Enabled || !config.DiceConfig.Persistence.WriteAOFOnCleanup {
return
}

Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func main() {
)

wl, _ = wal.NewNullWAL()
slog.Info("running with", slog.Bool("enable-wal", config.DiceConfig.Persistence.EnableWAL))
if config.DiceConfig.Persistence.EnableWAL {
slog.Info("running with", slog.Bool("persistence-enabled", config.DiceConfig.Persistence.Enabled))
if config.DiceConfig.Persistence.Enabled {
if config.DiceConfig.Persistence.WALEngine == "sqlite" {
_wl, err := wal.NewSQLiteWAL(config.DiceConfig.Persistence.WALDir)
if err != nil {
Expand Down Expand Up @@ -191,7 +191,7 @@ func main() {

close(sigs)

if config.DiceConfig.Persistence.EnableWAL {
if config.DiceConfig.Persistence.Enabled {
wal.ShutdownBG()
}

Expand Down

0 comments on commit a9b8879

Please sign in to comment.