Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default parent directory for storing logs and persistent data(WAL) #1359

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"os"
"path/filepath"
"runtime"
"time"

"github.com/dicedb/dice/internal/server/utils"
Expand Down Expand Up @@ -85,9 +86,13 @@ auth.password = ""
# Network Configuration
network.io_buffer_length = 512
network.io_buffer_length_max = 51200`
linuxPath = "/var/lib/dicedb"
darwinPath = "~/Library/Application Support/dicedb"
windowsPath = "C:\\ProgramData\\dicedb"
)

var (
DefaultParentDir = "."
CustomConfigFilePath = utils.EmptyStr
CustomConfigDirPath = utils.EmptyStr
)
Expand Down Expand Up @@ -229,6 +234,51 @@ func loadDiceConfig(configFilePath string) error {
return parser.Loadconfig(DiceConfig)
}

// ConfigureParentDirPaths Creates the default parent directory which can be used for logs and persistent data
func ConfigureParentDirPaths() {
var err error
DefaultParentDir, err = createDefaultParentDir()
if err != nil {
slog.Warn("Failed to create default preferences directory", slog.String("error", err.Error()))
}
}

func createDefaultParentDir() (string, error) {
// Get the default directory path for the OS
prefDir := GetDefaultParentDirPath()
// Create the directory (MkdirAll handles "already exists" gracefully)
if err := os.MkdirAll(prefDir, os.ModePerm); err != nil {
return "", fmt.Errorf("failed to create preferences directory: %w", err)
}

return prefDir, nil
}

func GetDefaultParentDirPath() string {
switch runtime.GOOS {
case "linux":
return linuxPath
case "darwin":
return expandHomeDirDarwin(darwinPath)
case "windows":
return windowsPath
default:
slog.Warn("unsupported OS, defaulting to current directory")
return "."
}
}

func expandHomeDirDarwin(path string) string {
if path != "" && path[0] == '~' {
home, err := os.UserHomeDir()
if err != nil {
return path // Fallback if home directory can't be resolved
}
return filepath.Join(home, path[1:])
}
return path
}

func MergeFlags(flags *Config) {
flagset := flag.CommandLine
flagset.Visit(func(f *flag.Flag) {
Expand Down
5 changes: 2 additions & 3 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func Execute() {
fmt.Println(" -enable-watch Enable support for .WATCH commands and real-time reactivity (default: false)")
fmt.Println(" -enable-profiling Enable profiling and capture critical metrics and traces in .prof files (default: false)")
fmt.Println(" -log-level Log level, values: info, debug (default: \"info\")")
fmt.Println(" -log-dir Log directory path (default: \"/tmp/dicedb\")")
fmt.Printf(" -log-dir Log directory path (default: \"%s\")\n", config.GetDefaultParentDirPath())
fmt.Println(" -enable-persistence Enable write-ahead logging (default: false)")
fmt.Println(" -restore-wal Restore the database from the WAL files (default: false)")
fmt.Println(" -wal-engine WAL engine to use, values: sqlite, aof (default: \"null\")")
Expand All @@ -137,6 +137,7 @@ func Execute() {
}

flag.Parse()
config.ConfigureParentDirPaths()

if len(os.Args) > 2 {
switch os.Args[1] {
Expand Down Expand Up @@ -224,8 +225,6 @@ func Execute() {
defaultConfig(&flagsConfig)
}
}

defaultConfig(&flagsConfig)
}

func defaultConfig(flags *config.Config) {
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"runtime/trace"
Expand Down Expand Up @@ -54,16 +55,17 @@ func main() {

wl, _ = wal.NewNullWAL()
if config.DiceConfig.Persistence.Enabled {
WALDir := filepath.Join(config.DefaultParentDir, config.DiceConfig.Persistence.WALDir)
if config.DiceConfig.Persistence.WALEngine == "sqlite" {
_wl, err := wal.NewSQLiteWAL(config.DiceConfig.Persistence.WALDir)
_wl, err := wal.NewSQLiteWAL(WALDir)
if err != nil {
slog.Warn("could not create WAL with", slog.String("wal-engine", config.DiceConfig.Persistence.WALEngine), slog.Any("error", err))
sigs <- syscall.SIGKILL
return
}
wl = _wl
} else if config.DiceConfig.Persistence.WALEngine == "aof" {
_wl, err := wal.NewAOFWAL(config.DiceConfig.Persistence.WALDir)
_wl, err := wal.NewAOFWAL(WALDir)
if err != nil {
slog.Warn("could not create WAL with", slog.String("wal-engine", config.DiceConfig.Persistence.WALEngine), slog.Any("error", err))
sigs <- syscall.SIGKILL
Expand Down
Loading