Skip to content

Commit

Permalink
introducing enums and removing pre allocated files
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushsatyam146 committed Dec 13, 2024
1 parent d972a4a commit 982faa4
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 181 deletions.
52 changes: 32 additions & 20 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ memory.keys_limit = 200000000
memory.lfu_log_factor = 10
# Persistence Configuration
persistence.enabled = true
persistence.enabled = false
persistence.aof_file = "./dice-master.aof"
persistence.persistence_enabled = true
persistence.write_aof_on_cleanup = false
Expand All @@ -87,18 +87,18 @@ network.io_buffer_length = 512
network.io_buffer_length_max = 51200
# WAL Configuration
LogDir = "tmp/deicdeb-wal-lt"
LogDir = "tmp/dicedb-wal"
Enabled = "true"
WalMode = "buffered"
WriteMode = "default"
BufferSizeMB = 1
RotationMode = "segemnt-size"
MaxSegmentSizeMB = 16
SegmentRotationTime = 60
BufferSyncInterval = 200
MaxSegmentRotationTime = 60s
BufferSyncInterval = 200ms
RetentionMode = "num-segments"
MaxSegmentCount = 10
SegmentRetentionDuration = 600
MaxSegmentRetentionDuration = 600s
RecoveryMode = "strict"`
)

Expand Down Expand Up @@ -168,28 +168,40 @@ type memory struct {
}

type persistence struct {
Enabled bool `config:"enabled" default:"true"`
Enabled bool `config:"enabled" default:"false"`
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 WALConfig struct {
LogDir string `config:"log_dir" default:"tmp/deicdeb-wal-lt"`
Enabled bool `config:"enabled" default:"true"`
WalMode string `config:"mode" default:"buffered" validate:"oneof=buffered unbuffered"`
WriteMode string `config:"write_mode" default:"default" validate:"oneof=default fsync"`
BufferSizeMB int `config:"buffer_size" default:"1" validate:"min=1"`
RotationMode string `config:"rotation_mode" default:"segemnt-size" validate:"oneof=segment-size time"`
MaxSegmentSizeMB int64 `config:"buffer_size" default:"16" validate:"min=1"`
SegmentRotationTime time.Duration `config:"max_segment_rotation_time" default:"60s" validate:"min=1s"`
BufferSyncInterval time.Duration `config:"max_segment_rotation_time" default:"200ms" validate:"min=1ms"`
RetentionMode string `config:"retention_mode" default:"num-segments" validate:"oneof=num-segments time checkpoint"`
MaxSegmentCount int `config:"max_segment_count" default:"10" validate:"min=1"`
SegmentRetentionDuration time.Duration `config:"max_segment_retention_time" default:"600s" validate:"min=1s"`
RecoveryMode string `config:"recovery_mode" default:"strict" validate:"oneof=strict truncate ignore"`
// Directory where WAL log files will be stored
LogDir string `config:"log_dir" default:"tmp/dicedb-wal"`
// Whether WAL is enabled
Enabled bool `config:"enabled" default:"true"`
// WAL buffering mode: 'buffered' (writes buffered in memory) or 'unbuffered' (immediate disk writes)
WalMode string `config:"wal_mode" default:"buffered" validate:"oneof=buffered unbuffered"`
// Write mode: 'default' (OS handles syncing) or 'fsync' (explicit fsync after writes)
WriteMode string `config:"write_mode" default:"default" validate:"oneof=default fsync"`
// Size of the write buffer in megabytes
BufferSizeMB int `config:"buffer_size_mb" default:"1" validate:"min=1"`
// How WAL rotation is triggered: 'segment-size' (based on file size) or 'time' (based on duration)
RotationMode string `config:"rotation_mode" default:"segemnt-size" validate:"oneof=segment-size time"`
// Maximum size of a WAL segment file in megabytes before rotation
MaxSegmentSizeMB int `config:"max_segment_size_mb" default:"16" validate:"min=1"`
// Time interval in seconds after which WAL segment is rotated when using time-based rotation
MaxSegmentRotationTime time.Duration `config:"max_segment_rotation_time" default:"60s" validate:"min=1s"`
// Time interval in Milliseconds after which buffered WAL data is synced to disk
BufferSyncInterval time.Duration `config:"buffer_sync_interval" default:"200ms" validate:"min=1ms"`
// How old segments are removed: 'num-segments' (keep N latest), 'time' (by age), or 'checkpoint' (after checkpoint)
RetentionMode string `config:"retention_mode" default:"num-segments" validate:"oneof=num-segments time checkpoint"`
// Maximum number of WAL segment files to retain when using num-segments retention
MaxSegmentCount int `config:"max_segment_count" default:"10" validate:"min=1"`
// Time interval in Seconds till which WAL segments are retained when using time-based retention
MaxSegmentRetentionDuration time.Duration `config:"max_segment_retention_duration" default:"600s" validate:"min=1s"`
// How to handle WAL corruption on recovery: 'strict' (fail), 'truncate' (truncate at corruption), 'ignore' (skip corrupted)
RecoveryMode string `config:"recovery_mode" default:"strict" validate:"oneof=strict truncate ignore"`
}

type logging struct {
Expand Down
34 changes: 34 additions & 0 deletions config/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func validateConfig(config *Config) error {
validate := validator.New()
validate.RegisterStructValidation(validateShardCount, Config{})
validate.RegisterStructValidation(validateWALConfig, Config{})

if err := validate.Struct(config); err != nil {
validationErrors, ok := err.(validator.ValidationErrors)
Expand Down Expand Up @@ -95,3 +96,36 @@ func applyDefaultValuesFromTags(config *Config, fieldName string) error {
log.Printf("Setting default value for %s to: %s", fieldName, defaultValue)
return nil
}

func validateWALConfig(sl validator.StructLevel) {
config := sl.Current().Interface().(Config)

// LogDir validation
if config.WAL.LogDir == "" {
sl.ReportError(config.WAL.LogDir, "LogDir", "LogDir", "required", "cannot be empty")
}

// MaxSegmentSize validation
if config.WAL.MaxSegmentSizeMB <= 0 {
sl.ReportError(config.WAL.MaxSegmentSizeMB, "MaxSegmentSize", "MaxSegmentSize", "gt", "must be greater than 0")
}

// MaxSegmentCount validation
if config.WAL.MaxSegmentCount <= 0 {
sl.ReportError(config.WAL.MaxSegmentCount, "MaxSegmentCount", "MaxSegmentCount", "gt", "must be greater than 0")
}

// BufferSize validation
if config.WAL.BufferSizeMB <= 0 {
sl.ReportError(config.WAL.BufferSizeMB, "BufferSize", "BufferSize", "gt", "must be greater than 0")
}

// WALMode and WriteMode compatibility checks
if config.WAL.WalMode == "buffered" && config.WAL.WriteMode == "fsync" {
sl.ReportError(config.WAL.WalMode, "WALMode", "WALMode", "incompatible", "walMode 'buffered' cannot be used with writeMode 'fsync'")
}

if config.WAL.WalMode == "unbuffered" && config.WAL.WriteMode == "default" {
sl.ReportError(config.WAL.WalMode, "WALMode", "WALMode", "incompatible", "walMode 'unbuffered' cannot have writeMode as 'default'")
}
}
10 changes: 5 additions & 5 deletions dicedb.conf
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ memory.keys_limit = 200000000
memory.lfu_log_factor = 10

# Persistence Configuration
persistence.enabled = true
persistence.enabled = false
persistence.aof_file = "./dice-master.aof"
persistence.persistence_enabled = true
persistence.write_aof_on_cleanup = false
Expand All @@ -59,16 +59,16 @@ network.io_buffer_length = 512
network.io_buffer_length_max = 51200

# WAL Configuration
LogDir = "tmp/deicdeb-wal-lt"
LogDir = "tmp/dicedb-wal"
Enabled = "true"
WalMode = "buffered"
WriteMode = "default"
BufferSizeMB = 1
RotationMode = "segemnt-size"
MaxSegmentSizeMB = 16
SegmentRotationTime = 60s
MaxSegmentRotationTime = 60s
BufferSyncInterval = 200ms
RetentionMode = "num-segments"
MaxSegmentCount = 10
SegmentRetentionDuration = 600s
RecoveryMode = "strict"`
MaxSegmentRetentionDuration = 600s
RecoveryMode = "strict"
65 changes: 33 additions & 32 deletions internal/wal/wal.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/wal/wal.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ syntax = "proto3";
package wal;
option go_package = "internal/wal";

message WAL_Entry {
message WALEntry {
string version = 1; // Version of the WAL entry (e.g., "v1.0")
uint64 logSequenceNumber = 2; // Log Sequence Number (LSN)
uint64 log_sequence_number = 2; // Log Sequence Number (LSN)
bytes data = 3; // The actual data being logged
uint32 CRC = 4; // Cyclic Redundancy Check for integrity
uint32 crc32 = 4; // Cyclic Redundancy Check for integrity
int64 timestamp = 5; // Timestamp for the WAL entry (epoch time in nanoseconds)
}
Loading

0 comments on commit 982faa4

Please sign in to comment.